Commit | Line | Data |
---|---|---|
fc526aef | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2013, 2015 Ericsson |
fc526aef AM |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Alexandre Montplaisir - Initial API and implementation | |
0fcf3b09 | 11 | * Patrick Tasse - Support selection range |
fc526aef AM |
12 | *******************************************************************************/ |
13 | ||
9722e5d7 | 14 | package org.eclipse.tracecompass.tmf.ctf.core.tests.temp.tracemanager; |
fc526aef AM |
15 | |
16 | import static org.junit.Assert.assertEquals; | |
17 | import static org.junit.Assert.assertNotNull; | |
fab18d20 | 18 | import static org.junit.Assert.assertSame; |
fc526aef | 19 | |
fab18d20 | 20 | import java.io.File; |
c14c0757 GB |
21 | import java.util.Collection; |
22 | import java.util.Collections; | |
c5648525 | 23 | import java.util.concurrent.TimeUnit; |
fab18d20 | 24 | |
6cfc180e | 25 | import org.eclipse.jdt.annotation.NonNull; |
c4d57ac1 | 26 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; |
2bdf0193 | 27 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
97c71024 | 28 | import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal; |
c4d57ac1 | 29 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; |
2bdf0193 AM |
30 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal; |
31 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; | |
32 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal; | |
c4d57ac1 | 33 | import org.eclipse.tracecompass.tmf.core.signal.TmfWindowRangeUpdatedSignal; |
2bdf0193 AM |
34 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; |
35 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; | |
36 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | |
37 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
21852dfa | 38 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceContext; |
2bdf0193 | 39 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
5c5fa260 | 40 | import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment; |
c4d57ac1 | 41 | import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils; |
fc526aef | 42 | import org.junit.After; |
3568f816 | 43 | import org.junit.AfterClass; |
fc526aef AM |
44 | import org.junit.Before; |
45 | import org.junit.BeforeClass; | |
c5648525 | 46 | import org.junit.Rule; |
fc526aef | 47 | import org.junit.Test; |
c5648525 AM |
48 | import org.junit.rules.TestRule; |
49 | import org.junit.rules.Timeout; | |
fc526aef | 50 | |
0f8687b4 GB |
51 | import com.google.common.collect.ImmutableSet; |
52 | ||
fc526aef AM |
53 | /** |
54 | * Test suite for the {@link TmfTraceManager}. | |
55 | * | |
56 | * @author Alexandre Montplaisir | |
57 | */ | |
58 | public class TmfTraceManagerTest { | |
59 | ||
c5648525 AM |
60 | /** Time-out tests after 20 seconds */ |
61 | @Rule | |
62 | public TestRule globalTimeout= new Timeout(20, TimeUnit.SECONDS); | |
63 | ||
fc526aef AM |
64 | private static final int SCALE = ITmfTimestamp.NANOSECOND_SCALE; |
65 | ||
66 | private static ITmfTrace trace1; | |
67 | private static final long t1start = 1331668247314038062L; | |
68 | private static final long t1end = 1331668259054285979L; | |
69 | ||
70 | private static ITmfTrace trace2; | |
71 | private static final long t2start = 1332170682440133097L; | |
72 | private static final long t2end = 1332170692664579801L; | |
73 | ||
74 | private static final long ONE_SECOND = 1000000000L; | |
75 | ||
76 | private TmfTraceManager tm; | |
77 | ||
78 | ||
79 | /** | |
80 | * Test class initialization | |
81 | */ | |
82 | @BeforeClass | |
83 | public static void setUpClass() { | |
c4d57ac1 AM |
84 | trace1 = CtfTmfTestTraceUtils.getTrace(CtfTestTrace.TRACE2); |
85 | trace2 = CtfTmfTestTraceUtils.getTrace(CtfTestTrace.KERNEL); | |
fc526aef AM |
86 | |
87 | trace1.indexTrace(true); | |
88 | trace2.indexTrace(true); | |
3568f816 BH |
89 | |
90 | // Deregister traces from signal manager so that they don't | |
91 | // interfere with the TmfTraceManager tests | |
92 | TmfSignalManager.deregister(trace1); | |
93 | TmfSignalManager.deregister(trace2); | |
fc526aef AM |
94 | } |
95 | ||
96 | /** | |
97 | * Test initialization | |
98 | */ | |
99 | @Before | |
100 | public void setUp() { | |
101 | tm = TmfTraceManager.getInstance(); | |
102 | } | |
103 | ||
104 | /** | |
105 | * Test clean-up | |
106 | */ | |
107 | @After | |
108 | public void tearDown() { | |
109 | while (tm.getActiveTrace() != null) { | |
110 | closeTrace(tm.getActiveTrace()); | |
111 | } | |
112 | } | |
113 | ||
3568f816 BH |
114 | /** |
115 | * Test class clean-up | |
116 | */ | |
117 | @AfterClass | |
118 | public static void tearDownClass() { | |
c4d57ac1 AM |
119 | CtfTmfTestTraceUtils.dispose(CtfTestTrace.TRACE2); |
120 | CtfTmfTestTraceUtils.dispose(CtfTestTrace.KERNEL); | |
3568f816 BH |
121 | } |
122 | ||
fc526aef AM |
123 | // ------------------------------------------------------------------------ |
124 | // Dummy actions (fake signals) | |
125 | // ------------------------------------------------------------------------ | |
126 | ||
127 | private void openTrace(ITmfTrace trace) { | |
c1831960 AM |
128 | if (trace == null) { |
129 | throw new IllegalArgumentException(); | |
130 | } | |
fc526aef AM |
131 | TmfSignalManager.dispatchSignal(new TmfTraceOpenedSignal(this, trace, null)); |
132 | selectTrace(trace); | |
133 | } | |
134 | ||
135 | private void closeTrace(ITmfTrace trace) { | |
c1831960 AM |
136 | if (trace == null) { |
137 | throw new IllegalArgumentException(); | |
138 | } | |
fc526aef | 139 | TmfSignalManager.dispatchSignal(new TmfTraceClosedSignal(this, trace)); |
fab18d20 AM |
140 | /* |
141 | * In TMF, the next tab would now be selected (if there are some), which | |
142 | * would select another trace automatically. | |
143 | */ | |
144 | if (tm.getOpenedTraces().size() > 0) { | |
145 | selectTrace(tm.getOpenedTraces().toArray(new ITmfTrace[0])[0]); | |
146 | } | |
fc526aef AM |
147 | } |
148 | ||
149 | private void selectTrace(ITmfTrace trace) { | |
150 | TmfSignalManager.dispatchSignal(new TmfTraceSelectedSignal(this, trace)); | |
151 | } | |
152 | ||
6cfc180e | 153 | private void selectTimestamp(@NonNull ITmfTimestamp ts) { |
97c71024 | 154 | TmfSignalManager.dispatchSignal(new TmfSelectionRangeUpdatedSignal(this, ts)); |
fc526aef AM |
155 | } |
156 | ||
21852dfa | 157 | private void selectWindowRange(TmfTimeRange tr) { |
97c71024 | 158 | TmfSignalManager.dispatchSignal(new TmfWindowRangeUpdatedSignal(this, tr)); |
fc526aef AM |
159 | } |
160 | ||
161 | // ------------------------------------------------------------------------ | |
162 | // General tests | |
163 | // ------------------------------------------------------------------------ | |
164 | ||
165 | /** | |
166 | * Test that the manager is correctly initialized | |
167 | */ | |
168 | @Test | |
169 | public void testInitialize() { | |
170 | TmfTraceManager mgr = TmfTraceManager.getInstance(); | |
171 | assertNotNull(mgr); | |
fab18d20 AM |
172 | assertSame(tm, mgr); |
173 | } | |
174 | ||
175 | /** | |
176 | * Test the contents of a trace set with one trace. | |
177 | */ | |
178 | @Test | |
179 | public void testTraceSet() { | |
180 | openTrace(trace1); | |
181 | openTrace(trace2); | |
182 | selectTrace(trace2); | |
183 | ||
c14c0757 GB |
184 | Collection<ITmfTrace> expected = Collections.singleton(trace2); |
185 | Collection<ITmfTrace> actual = tm.getActiveTraceSet(); | |
fab18d20 | 186 | |
c14c0757 GB |
187 | assertEquals(1, actual.size()); |
188 | assertEquals(expected, actual); | |
fab18d20 AM |
189 | } |
190 | ||
191 | /** | |
192 | * Test the contents of a trace set with an experiment. | |
193 | */ | |
194 | @Test | |
195 | public void testTraceSetExperiment() { | |
dbecae9c MK |
196 | final ITmfTrace localTrace1 = trace1; |
197 | final ITmfTrace localTrace2 = trace2; | |
198 | assertNotNull(localTrace1); | |
199 | assertNotNull(localTrace2); | |
200 | TmfExperiment exp = createExperiment(localTrace1, localTrace2); | |
201 | openTrace(localTrace1); | |
fab18d20 AM |
202 | openTrace(exp); |
203 | ||
dbecae9c | 204 | Collection<ITmfTrace> expected = ImmutableSet.of(localTrace1, localTrace2); |
c14c0757 | 205 | Collection<ITmfTrace> actual = tm.getActiveTraceSet(); |
fab18d20 | 206 | |
c14c0757 GB |
207 | assertEquals(2, actual.size()); |
208 | assertEquals(expected, actual); | |
fab18d20 AM |
209 | } |
210 | ||
0f8687b4 GB |
211 | /** |
212 | * Test the contents of the complete trace set. | |
213 | */ | |
214 | @Test | |
215 | public void testTraceSetWithExperiment() { | |
dbecae9c MK |
216 | final ITmfTrace localTrace1 = trace1; |
217 | final ITmfTrace localTrace2 = trace2; | |
218 | assertNotNull(localTrace1); | |
219 | assertNotNull(localTrace2); | |
0f8687b4 | 220 | /* Test with a trace */ |
dbecae9c MK |
221 | Collection<ITmfTrace> expected = Collections.singleton(localTrace1); |
222 | Collection<ITmfTrace> actual = TmfTraceManager.getTraceSetWithExperiment(localTrace1); | |
0f8687b4 GB |
223 | assertEquals(1, actual.size()); |
224 | assertEquals(expected, actual); | |
225 | ||
226 | /* Test with an experiment */ | |
dbecae9c MK |
227 | TmfExperiment exp = createExperiment(localTrace1, localTrace2); |
228 | assertNotNull(exp); | |
229 | expected = ImmutableSet.of(localTrace1, localTrace2, exp); | |
0f8687b4 GB |
230 | actual = TmfTraceManager.getTraceSetWithExperiment(exp); |
231 | assertEquals(3, actual.size()); | |
232 | assertEquals(expected, actual); | |
233 | } | |
234 | ||
fab18d20 AM |
235 | /** |
236 | * Test the {@link TmfTraceManager#getSupplementaryFileDir} method. | |
237 | */ | |
238 | @Test | |
239 | public void testSupplementaryFileDir() { | |
dbecae9c MK |
240 | final ITmfTrace localTrace1 = trace1; |
241 | final ITmfTrace localTrace2 = trace2; | |
242 | assertNotNull(localTrace1); | |
243 | assertNotNull(localTrace2); | |
244 | String name1 = localTrace1.getName(); | |
245 | String name2 = localTrace2.getName(); | |
3ec38c4c | 246 | String basePath = TmfTraceManager.getTemporaryDirPath() + File.separator; |
fab18d20 AM |
247 | |
248 | String expected1 = basePath + name1 + File.separator; | |
249 | String expected2 = basePath + name2 + File.separator; | |
250 | ||
dbecae9c MK |
251 | assertEquals(expected1, TmfTraceManager.getSupplementaryFileDir(localTrace1)); |
252 | assertEquals(expected2, TmfTraceManager.getSupplementaryFileDir(localTrace2)); | |
fc526aef AM |
253 | } |
254 | ||
255 | // ------------------------------------------------------------------------ | |
256 | // Test a single trace | |
257 | // ------------------------------------------------------------------------ | |
258 | ||
259 | /** | |
260 | * Test the initial range of a single trace. | |
261 | */ | |
262 | @Test | |
263 | public void testTraceInitialRange() { | |
264 | openTrace(trace2); | |
265 | final TmfTimeRange expectedRange = new TmfTimeRange( | |
266 | trace2.getStartTime(), | |
267 | calculateOffset(trace2.getStartTime(), trace2.getInitialRangeOffset())); | |
21852dfa | 268 | TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef AM |
269 | assertEquals(expectedRange, actualRange); |
270 | } | |
271 | ||
272 | /** | |
273 | * Try selecting a timestamp contained inside the trace's range. The trace's | |
274 | * current time should get updated correctly. | |
275 | */ | |
276 | @Test | |
277 | public void testNewTimestamp() { | |
278 | openTrace(trace2); | |
279 | ITmfTimestamp ts = new TmfTimestamp(t2start + ONE_SECOND, SCALE); | |
280 | selectTimestamp(ts); | |
281 | ||
21852dfa AM |
282 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
283 | assertEquals(ts, selection.getStartTime()); | |
284 | assertEquals(ts, selection.getEndTime()); | |
fc526aef AM |
285 | } |
286 | ||
287 | /** | |
288 | * Try selecting a timestamp happening before the trace's start. The change | |
289 | * should be ignored. | |
290 | */ | |
291 | @Test | |
292 | public void testTimestampBefore() { | |
293 | openTrace(trace2); | |
21852dfa | 294 | TmfTimeRange beforeTr = tm.getCurrentTraceContext().getSelectionRange(); |
fc526aef AM |
295 | ITmfTimestamp ts = new TmfTimestamp(t2start - ONE_SECOND, SCALE); |
296 | selectTimestamp(ts); | |
297 | ||
21852dfa AM |
298 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
299 | assertEquals(beforeTr, selection); | |
fc526aef AM |
300 | } |
301 | ||
302 | /** | |
303 | * Try selecting a timestamp happening after the trace's end. The change | |
304 | * should be ignored. | |
305 | */ | |
306 | @Test | |
307 | public void testTimestampAfter() { | |
308 | openTrace(trace2); | |
21852dfa | 309 | TmfTimeRange beforeTr = tm.getCurrentTraceContext().getSelectionRange(); |
fc526aef AM |
310 | ITmfTimestamp ts = new TmfTimestamp(t2end + ONE_SECOND, SCALE); |
311 | selectTimestamp(ts); | |
312 | ||
21852dfa AM |
313 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
314 | assertEquals(beforeTr, selection); | |
fc526aef AM |
315 | } |
316 | ||
317 | /** | |
318 | * Test selecting a normal sub-range of a single trace. | |
319 | */ | |
320 | @Test | |
321 | public void testTraceNewTimeRange() { | |
322 | openTrace(trace2); | |
323 | TmfTimeRange range = new TmfTimeRange( | |
324 | new TmfTimestamp(t2start + ONE_SECOND, SCALE), | |
325 | new TmfTimestamp(t2end - ONE_SECOND, SCALE)); | |
21852dfa | 326 | selectWindowRange(range); |
fc526aef | 327 | |
21852dfa AM |
328 | TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange(); |
329 | assertEquals(range, curRange); | |
fc526aef AM |
330 | } |
331 | ||
332 | /** | |
333 | * Test selecting a range whose start time is before the trace's start time. | |
334 | * The selected range should get clamped to the trace's range. | |
335 | */ | |
336 | @Test | |
337 | public void testTraceTimeRangeClampingStart() { | |
338 | openTrace(trace2); | |
339 | TmfTimeRange range = new TmfTimeRange( | |
340 | new TmfTimestamp(t2start - ONE_SECOND, SCALE), // minus here | |
341 | new TmfTimestamp(t2end - ONE_SECOND, SCALE)); | |
21852dfa | 342 | selectWindowRange(range); |
fc526aef | 343 | |
21852dfa | 344 | TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef AM |
345 | assertEquals(t2start, curRange.getStartTime().getValue()); |
346 | assertEquals(range.getEndTime(), curRange.getEndTime()); | |
347 | } | |
348 | ||
349 | /** | |
350 | * Test selecting a range whose end time is after the trace's end time. | |
351 | * The selected range should get clamped to the trace's range. | |
352 | */ | |
353 | @Test | |
354 | public void testTraceTimeRangeClampingEnd() { | |
355 | openTrace(trace2); | |
356 | TmfTimeRange range = new TmfTimeRange( | |
357 | new TmfTimestamp(t2start + ONE_SECOND, SCALE), | |
358 | new TmfTimestamp(t2end + ONE_SECOND, SCALE)); // plus here | |
21852dfa | 359 | selectWindowRange(range); |
fc526aef | 360 | |
21852dfa | 361 | TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef AM |
362 | assertEquals(range.getStartTime(), curRange.getStartTime()); |
363 | assertEquals(t2end, curRange.getEndTime().getValue()); | |
364 | } | |
365 | ||
366 | /** | |
367 | * Test selecting a range whose both start and end times are outside of the | |
368 | * trace's range. The selected range should get clamped to the trace's | |
369 | * range. | |
370 | */ | |
371 | @Test | |
372 | public void testTraceTimeRangeClampingBoth() { | |
373 | openTrace(trace2); | |
374 | TmfTimeRange range = new TmfTimeRange( | |
375 | new TmfTimestamp(t2start - ONE_SECOND, SCALE), // minus here | |
376 | new TmfTimestamp(t2end + ONE_SECOND, SCALE)); // plus here | |
21852dfa | 377 | selectWindowRange(range); |
fc526aef | 378 | |
21852dfa | 379 | TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef AM |
380 | assertEquals(t2start, curRange.getStartTime().getValue()); |
381 | assertEquals(t2end, curRange.getEndTime().getValue()); | |
382 | } | |
383 | ||
384 | // ------------------------------------------------------------------------ | |
fab18d20 | 385 | // Test multiple, non-overlapping traces in parallel |
fc526aef AM |
386 | // ------------------------------------------------------------------------ |
387 | ||
fab18d20 AM |
388 | /** |
389 | * Test, with two traces in parallel, when we select a timestamp that is | |
390 | * part of the first trace. | |
391 | * | |
392 | * The first trace's timestamp should be updated, but the second trace's one | |
393 | * should not change. | |
394 | */ | |
395 | @Test | |
396 | public void testTwoTracesTimestampValid() { | |
397 | openTrace(trace1); | |
398 | openTrace(trace2); | |
399 | selectTrace(trace1); | |
400 | TmfTimestamp ts = new TmfTimestamp(t1start + ONE_SECOND, SCALE); | |
401 | selectTimestamp(ts); | |
402 | ||
403 | /* Timestamp of trace1 should have been updated */ | |
21852dfa AM |
404 | TmfTraceContext ctx = tm.getCurrentTraceContext(); |
405 | assertEquals(ts, ctx.getSelectionRange().getStartTime()); | |
406 | assertEquals(ts, ctx.getSelectionRange().getEndTime()); | |
fab18d20 AM |
407 | |
408 | /* Timestamp of trace2 should not have changed */ | |
409 | selectTrace(trace2); | |
21852dfa AM |
410 | ctx = tm.getCurrentTraceContext(); |
411 | assertEquals(trace2.getStartTime(), ctx.getSelectionRange().getStartTime()); | |
412 | assertEquals(trace2.getStartTime(), ctx.getSelectionRange().getEndTime()); | |
fab18d20 AM |
413 | } |
414 | ||
415 | /** | |
416 | * Test, with two traces in parallel, when we select a timestamp that is | |
417 | * between two traces. | |
418 | * | |
419 | * None of the trace's timestamps should be updated (we are not in an | |
420 | * experiment!) | |
421 | */ | |
422 | @Test | |
423 | public void testTwoTracesTimestampInBetween() { | |
424 | openTrace(trace1); | |
425 | openTrace(trace2); | |
426 | selectTrace(trace1); | |
427 | TmfTimestamp ts = new TmfTimestamp(t1end + ONE_SECOND, SCALE); | |
428 | selectTimestamp(ts); | |
429 | ||
430 | /* Timestamp of trace1 should not have changed */ | |
21852dfa AM |
431 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
432 | assertEquals(trace1.getStartTime(), selection.getStartTime()); | |
433 | assertEquals(trace1.getStartTime(), selection.getEndTime()); | |
fab18d20 AM |
434 | |
435 | /* Timestamp of trace2 should not have changed */ | |
436 | selectTrace(trace2); | |
21852dfa AM |
437 | selection = tm.getCurrentTraceContext().getSelectionRange(); |
438 | assertEquals(trace2.getStartTime(), selection.getStartTime()); | |
439 | assertEquals(trace2.getStartTime(), selection.getEndTime()); | |
fab18d20 AM |
440 | } |
441 | ||
442 | /** | |
443 | * Test, with two traces in parallel, when we select a timestamp that is | |
444 | * completely out of the trace's range. | |
445 | * | |
446 | * None of the trace's timestamps should be updated. | |
447 | */ | |
448 | @Test | |
449 | public void testTwoTracesTimestampInvalid() { | |
450 | openTrace(trace1); | |
451 | openTrace(trace2); | |
452 | selectTrace(trace1); | |
453 | TmfTimestamp ts = new TmfTimestamp(t2end + ONE_SECOND, SCALE); | |
454 | selectTimestamp(ts); | |
455 | ||
456 | /* Timestamp of trace1 should not have changed */ | |
21852dfa AM |
457 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
458 | assertEquals(trace1.getStartTime(), selection.getStartTime()); | |
459 | assertEquals(trace1.getStartTime(), selection.getEndTime()); | |
fab18d20 AM |
460 | |
461 | /* Timestamp of trace2 should not have changed */ | |
462 | selectTrace(trace2); | |
21852dfa AM |
463 | selection = tm.getCurrentTraceContext().getSelectionRange(); |
464 | assertEquals(trace2.getStartTime(), selection.getStartTime()); | |
465 | assertEquals(trace2.getStartTime(), selection.getEndTime()); | |
fab18d20 AM |
466 | } |
467 | ||
468 | /** | |
469 | * Test, with two traces opened in parallel (not in an experiment), if we | |
470 | * select a time range valid in one of them. That trace's time range should | |
471 | * be updated, but not the other one. | |
472 | */ | |
473 | @Test | |
474 | public void testTwoTracesTimeRangeAllInOne() { | |
475 | openTrace(trace1); | |
476 | openTrace(trace2); | |
477 | selectTrace(trace1); | |
478 | TmfTimeRange range = new TmfTimeRange( | |
479 | new TmfTimestamp(t1start + ONE_SECOND, SCALE), | |
480 | new TmfTimestamp(t1end - ONE_SECOND, SCALE)); | |
21852dfa | 481 | selectWindowRange(range); |
fab18d20 AM |
482 | |
483 | /* Range of trace1 should be equal to the requested one */ | |
21852dfa | 484 | assertEquals(range, tm.getCurrentTraceContext().getWindowRange()); |
fab18d20 AM |
485 | |
486 | /* The range of trace 2 should not have changed */ | |
487 | selectTrace(trace2); | |
21852dfa | 488 | assertEquals(getInitialRange(trace2), tm.getCurrentTraceContext().getWindowRange()); |
fab18d20 AM |
489 | } |
490 | ||
491 | /** | |
492 | * Test, with two traces in parallel, when we select a time range that is | |
493 | * only partially valid for one of the traces. | |
494 | * | |
495 | * The first trace's time range should be clamped to a valid range, and the | |
496 | * second one's should not change. | |
497 | */ | |
498 | @Test | |
499 | public void testTwoTracesTimeRangePartiallyInOne() { | |
500 | openTrace(trace1); | |
501 | openTrace(trace2); | |
502 | selectTrace(trace1); | |
503 | TmfTimeRange range = new TmfTimeRange( | |
504 | new TmfTimestamp(t1start + ONE_SECOND, SCALE), | |
505 | new TmfTimestamp(t1end + ONE_SECOND, SCALE)); | |
21852dfa | 506 | selectWindowRange(range); |
fab18d20 AM |
507 | |
508 | /* Range of trace1 should get clamped to its end time */ | |
509 | TmfTimeRange expectedRange = new TmfTimeRange( | |
510 | new TmfTimestamp(t1start + ONE_SECOND, SCALE), | |
511 | new TmfTimestamp(t1end, SCALE)); | |
21852dfa | 512 | assertEquals(expectedRange, tm.getCurrentTraceContext().getWindowRange()); |
fab18d20 AM |
513 | |
514 | /* Range of trace2 should not have changed */ | |
515 | selectTrace(trace2); | |
21852dfa | 516 | assertEquals(getInitialRange(trace2), tm.getCurrentTraceContext().getWindowRange()); |
fab18d20 AM |
517 | } |
518 | ||
519 | /** | |
520 | * Test, with two traces in parallel, when we select a time range that is | |
521 | * only partially valid for both traces. | |
522 | * | |
523 | * Each trace's time range should get clamped to respectively valid ranges. | |
524 | */ | |
525 | @Test | |
526 | public void testTwoTracesTimeRangeInBoth() { | |
527 | openTrace(trace1); | |
528 | openTrace(trace2); | |
529 | selectTrace(trace1); | |
530 | TmfTimeRange range = new TmfTimeRange( | |
531 | new TmfTimestamp(t1end - ONE_SECOND, SCALE), | |
532 | new TmfTimestamp(t2start + ONE_SECOND, SCALE)); | |
21852dfa | 533 | selectWindowRange(range); |
fab18d20 AM |
534 | |
535 | /* Range of trace1 should be clamped to its end time */ | |
536 | TmfTimeRange expectedRange = new TmfTimeRange( | |
537 | new TmfTimestamp(t1end - ONE_SECOND, SCALE), | |
538 | new TmfTimestamp(t1end, SCALE)); | |
21852dfa | 539 | assertEquals(expectedRange, tm.getCurrentTraceContext().getWindowRange()); |
fab18d20 AM |
540 | |
541 | /* Range of trace2 should be clamped to its start time */ | |
542 | selectTrace(trace2); | |
543 | expectedRange = new TmfTimeRange( | |
544 | new TmfTimestamp(t2start, SCALE), | |
545 | new TmfTimestamp(t2start + ONE_SECOND, SCALE)); | |
21852dfa | 546 | assertEquals(expectedRange, tm.getCurrentTraceContext().getWindowRange()); |
fab18d20 AM |
547 | } |
548 | ||
549 | /** | |
550 | * Test, with two traces in parallel, when we select a time range that is | |
551 | * not valid for any trace. | |
552 | * | |
553 | * Each trace's time range should not be modified. | |
554 | */ | |
555 | @Test | |
556 | public void testTwoTracesTimeRangeInBetween() { | |
557 | openTrace(trace1); | |
558 | openTrace(trace2); | |
559 | selectTrace(trace1); | |
560 | TmfTimeRange range = new TmfTimeRange( | |
561 | new TmfTimestamp(t1end + ONE_SECOND, SCALE), | |
562 | new TmfTimestamp(t1end - ONE_SECOND, SCALE)); | |
21852dfa | 563 | selectWindowRange(range); |
fab18d20 AM |
564 | |
565 | /* Range of trace1 should not have changed */ | |
566 | TmfTimeRange expectedRange = getInitialRange(trace1); | |
21852dfa | 567 | TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange(); |
fab18d20 AM |
568 | assertEquals(expectedRange.getStartTime(), curRange.getStartTime()); |
569 | assertEquals(expectedRange.getEndTime(), curRange.getEndTime()); | |
570 | ||
571 | /* Range of trace2 should not have changed */ | |
572 | selectTrace(trace2); | |
573 | expectedRange = getInitialRange(trace2); | |
21852dfa | 574 | curRange = tm.getCurrentTraceContext().getWindowRange(); |
fab18d20 AM |
575 | assertEquals(expectedRange.getStartTime(), curRange.getStartTime()); |
576 | assertEquals(expectedRange.getEndTime(), curRange.getEndTime()); | |
577 | } | |
578 | ||
fc526aef AM |
579 | // ------------------------------------------------------------------------ |
580 | // Test an experiment | |
581 | // ------------------------------------------------------------------------ | |
582 | ||
fab18d20 AM |
583 | /** |
584 | * Test in an experiment when we select a timestamp that is part of one of | |
585 | * the experiment's traces. | |
586 | * | |
587 | * The experiment's current time should be correctly updated. | |
588 | */ | |
589 | @Test | |
590 | public void testExperimentTimestampInTrace() { | |
591 | TmfExperiment exp = createExperiment(trace1, trace2); | |
592 | openTrace(exp); | |
593 | TmfTimestamp ts = new TmfTimestamp(t1start + ONE_SECOND, SCALE); | |
594 | selectTimestamp(ts); | |
595 | ||
596 | /* The experiment's current time should be updated. */ | |
21852dfa AM |
597 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
598 | assertEquals(ts, selection.getStartTime()); | |
599 | assertEquals(ts, selection.getEndTime()); | |
fab18d20 AM |
600 | } |
601 | ||
602 | /** | |
603 | * Test in an experiment when we select a timestamp that is between two | |
604 | * traces in the experiment. | |
605 | * | |
606 | * The experiment's current time should still be updated, since the | |
607 | * timestamp is valid in the experiment itself. | |
608 | */ | |
609 | @Test | |
610 | public void testExperimentTimestampInBetween() { | |
611 | TmfExperiment exp = createExperiment(trace1, trace2); | |
612 | openTrace(exp); | |
613 | TmfTimestamp ts = new TmfTimestamp(t1end + ONE_SECOND, SCALE); | |
614 | selectTimestamp(ts); | |
615 | ||
616 | /* The experiment's current time should be updated. */ | |
21852dfa AM |
617 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
618 | assertEquals(ts, selection.getStartTime()); | |
619 | assertEquals(ts, selection.getEndTime()); | |
fab18d20 AM |
620 | } |
621 | ||
622 | /** | |
623 | * Test in an experiment when we select a timestamp that is outside of the | |
624 | * total range of the experiment. | |
625 | * | |
626 | * The experiment's current time should not be updated. | |
627 | */ | |
628 | @Test | |
629 | public void testExperimentTimestampInvalid() { | |
630 | TmfExperiment exp = createExperiment(trace1, trace2); | |
631 | openTrace(exp); | |
632 | TmfTimestamp ts = new TmfTimestamp(t2end + ONE_SECOND, SCALE); | |
633 | selectTimestamp(ts); | |
634 | ||
635 | /* The experiment's current time should NOT be updated. */ | |
21852dfa AM |
636 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
637 | assertEquals(trace1.getStartTime(), selection.getStartTime()); | |
638 | assertEquals(trace1.getStartTime(), selection.getEndTime()); | |
fab18d20 AM |
639 | } |
640 | ||
fc526aef AM |
641 | /** |
642 | * Test the initial range of an experiment. | |
643 | */ | |
644 | @Test | |
645 | public void testExperimentInitialRange() { | |
646 | TmfExperiment exp = createExperiment(trace1, trace2); | |
647 | openTrace(exp); | |
648 | /* | |
649 | * The initial range should be == to the initial range of the earliest | |
650 | * trace (here trace1). | |
651 | */ | |
21852dfa | 652 | final TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef | 653 | |
fab18d20 AM |
654 | assertEquals(getInitialRange(trace1), actualRange); |
655 | assertEquals(getInitialRange(exp), actualRange); | |
656 | } | |
fc526aef | 657 | |
fab18d20 AM |
658 | /** |
659 | * Test the range clamping with the start time of the range outside of the | |
660 | * earliest trace's range. Only that start time should get clamped. | |
661 | */ | |
662 | @Test | |
663 | public void testExperimentRangeClampingOne() { | |
664 | TmfExperiment exp = createExperiment(trace1, trace2); | |
665 | openTrace(exp); | |
fc526aef | 666 | |
fab18d20 AM |
667 | final TmfTimeRange range = new TmfTimeRange( |
668 | new TmfTimestamp(t1start - ONE_SECOND, SCALE), | |
669 | new TmfTimestamp(t1end - ONE_SECOND, SCALE)); | |
21852dfa | 670 | selectWindowRange(range); |
fab18d20 | 671 | |
21852dfa | 672 | TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange(); |
fab18d20 AM |
673 | assertEquals(t1start, actualRange.getStartTime().getValue()); |
674 | assertEquals(t1end - ONE_SECOND, actualRange.getEndTime().getValue()); | |
fc526aef AM |
675 | } |
676 | ||
677 | /** | |
678 | * Test the range clamping when both the start and end times of the signal's | |
679 | * range are outside of the trace's range. The range should clamp to the | |
680 | * experiment's range. | |
681 | */ | |
682 | @Test | |
683 | public void testExperimentRangeClampingBoth() { | |
684 | TmfExperiment exp = createExperiment(trace1, trace2); | |
685 | openTrace(exp); | |
686 | ||
687 | final TmfTimeRange range = new TmfTimeRange( | |
688 | new TmfTimestamp(t1start - ONE_SECOND, SCALE), | |
689 | new TmfTimestamp(t2end + ONE_SECOND, SCALE)); | |
21852dfa | 690 | selectWindowRange(range); |
fc526aef | 691 | |
21852dfa | 692 | TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef AM |
693 | assertEquals(t1start, actualRange.getStartTime().getValue()); |
694 | assertEquals(t2end, actualRange.getEndTime().getValue()); | |
695 | } | |
696 | ||
697 | /** | |
698 | * Test selecting a range in-between two disjoint traces in an experiment. | |
699 | * The range should still get correctly selected, even if no trace has any | |
700 | * events in that range. | |
701 | */ | |
702 | @Test | |
703 | public void testExperimentRangeInBetween() { | |
704 | TmfExperiment exp = createExperiment(trace1, trace2); | |
705 | openTrace(exp); | |
706 | ||
707 | final TmfTimeRange range = new TmfTimeRange( | |
708 | new TmfTimestamp(t1end + ONE_SECOND, SCALE), | |
709 | new TmfTimestamp(t2start - ONE_SECOND, SCALE)); | |
21852dfa | 710 | selectWindowRange(range); |
fc526aef | 711 | |
21852dfa | 712 | TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef AM |
713 | assertEquals(range, actualRange); |
714 | } | |
715 | ||
716 | // ------------------------------------------------------------------------ | |
717 | // Utility methods | |
718 | // ------------------------------------------------------------------------ | |
719 | ||
720 | private static TmfExperiment createExperiment(ITmfTrace t1, ITmfTrace t2) { | |
721 | ITmfTrace[] traces = new ITmfTrace[] { t1, t2 }; | |
4178260e AM |
722 | TmfExperiment exp = new TmfExperiment(ITmfEvent.class, "test-exp", traces, |
723 | TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null); | |
fc526aef | 724 | exp.indexTrace(true); |
3568f816 BH |
725 | // Deregister experiment from signal manager so that it doesn't |
726 | // interfere with the TmfTraceManager tests | |
727 | TmfSignalManager.deregister(exp); | |
fc526aef AM |
728 | return exp; |
729 | } | |
730 | ||
fab18d20 AM |
731 | private static TmfTimeRange getInitialRange(ITmfTrace trace) { |
732 | return new TmfTimeRange( | |
733 | trace.getStartTime(), | |
734 | calculateOffset(trace.getStartTime(), trace.getInitialRangeOffset())); | |
735 | } | |
736 | ||
fc526aef AM |
737 | /** |
738 | * Basically a "initial + offset" operation, but for ITmfTimetamp objects. | |
739 | */ | |
6cfc180e | 740 | private static @NonNull ITmfTimestamp calculateOffset(ITmfTimestamp initialTs, ITmfTimestamp offsetTs) { |
fc526aef AM |
741 | long start = initialTs.normalize(0, SCALE).getValue(); |
742 | long offset = offsetTs.normalize(0, SCALE).getValue(); | |
743 | return new TmfTimestamp(start + offset, SCALE); | |
744 | } | |
745 | } |