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