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