df5999f78161892aa6ebfc9efc3b924deefe0a2c
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / temp / tracemanager / TmfTraceManagerTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 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
11 * Patrick Tasse - Support selection range
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.ctf.core.tests.temp.tracemanager;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertSame;
19
20 import java.io.File;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.concurrent.TimeUnit;
24
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
27 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
28 import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
29 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
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;
33 import org.eclipse.tracecompass.tmf.core.signal.TmfWindowRangeUpdatedSignal;
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;
38 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceContext;
39 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
40 import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
41 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils;
42 import org.junit.After;
43 import org.junit.AfterClass;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Rule;
47 import org.junit.Test;
48 import org.junit.rules.TestRule;
49 import org.junit.rules.Timeout;
50
51 import com.google.common.collect.ImmutableSet;
52
53 /**
54 * Test suite for the {@link TmfTraceManager}.
55 *
56 * @author Alexandre Montplaisir
57 */
58 public class TmfTraceManagerTest {
59
60 /** Time-out tests after 20 seconds */
61 @Rule
62 public TestRule globalTimeout= new Timeout(20, TimeUnit.SECONDS);
63
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() {
84 trace1 = CtfTmfTestTraceUtils.getTrace(CtfTestTrace.TRACE2);
85 trace2 = CtfTmfTestTraceUtils.getTrace(CtfTestTrace.KERNEL);
86
87 trace1.indexTrace(true);
88 trace2.indexTrace(true);
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);
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
114 /**
115 * Test class clean-up
116 */
117 @AfterClass
118 public static void tearDownClass() {
119 CtfTmfTestTraceUtils.dispose(CtfTestTrace.TRACE2);
120 CtfTmfTestTraceUtils.dispose(CtfTestTrace.KERNEL);
121 }
122
123 // ------------------------------------------------------------------------
124 // Dummy actions (fake signals)
125 // ------------------------------------------------------------------------
126
127 private void openTrace(ITmfTrace trace) {
128 if (trace == null) {
129 throw new IllegalArgumentException();
130 }
131 TmfSignalManager.dispatchSignal(new TmfTraceOpenedSignal(this, trace, null));
132 selectTrace(trace);
133 }
134
135 private void closeTrace(ITmfTrace trace) {
136 if (trace == null) {
137 throw new IllegalArgumentException();
138 }
139 TmfSignalManager.dispatchSignal(new TmfTraceClosedSignal(this, trace));
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 }
147 }
148
149 private void selectTrace(ITmfTrace trace) {
150 TmfSignalManager.dispatchSignal(new TmfTraceSelectedSignal(this, trace));
151 }
152
153 private void selectTimestamp(@NonNull ITmfTimestamp ts) {
154 TmfSignalManager.dispatchSignal(new TmfSelectionRangeUpdatedSignal(this, ts));
155 }
156
157 private void selectWindowRange(TmfTimeRange tr) {
158 TmfSignalManager.dispatchSignal(new TmfWindowRangeUpdatedSignal(this, tr));
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);
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
184 Collection<ITmfTrace> expected = Collections.singleton(trace2);
185 Collection<ITmfTrace> actual = tm.getActiveTraceSet();
186
187 assertEquals(1, actual.size());
188 assertEquals(expected, actual);
189 }
190
191 /**
192 * Test the contents of a trace set with an experiment.
193 */
194 @Test
195 public void testTraceSetExperiment() {
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);
202 openTrace(exp);
203
204 Collection<ITmfTrace> expected = ImmutableSet.of(localTrace1, localTrace2);
205 Collection<ITmfTrace> actual = tm.getActiveTraceSet();
206
207 assertEquals(2, actual.size());
208 assertEquals(expected, actual);
209 }
210
211 /**
212 * Test the contents of the complete trace set.
213 */
214 @Test
215 public void testTraceSetWithExperiment() {
216 final ITmfTrace localTrace1 = trace1;
217 final ITmfTrace localTrace2 = trace2;
218 assertNotNull(localTrace1);
219 assertNotNull(localTrace2);
220 /* Test with a trace */
221 Collection<ITmfTrace> expected = Collections.singleton(localTrace1);
222 Collection<ITmfTrace> actual = TmfTraceManager.getTraceSetWithExperiment(localTrace1);
223 assertEquals(1, actual.size());
224 assertEquals(expected, actual);
225
226 /* Test with an experiment */
227 TmfExperiment exp = createExperiment(localTrace1, localTrace2);
228 assertNotNull(exp);
229 expected = ImmutableSet.of(localTrace1, localTrace2, exp);
230 actual = TmfTraceManager.getTraceSetWithExperiment(exp);
231 assertEquals(3, actual.size());
232 assertEquals(expected, actual);
233 }
234
235 /**
236 * Test the {@link TmfTraceManager#getSupplementaryFileDir} method.
237 */
238 @Test
239 public void testSupplementaryFileDir() {
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();
246 String basePath = TmfTraceManager.getTemporaryDirPath() + File.separator;
247
248 String expected1 = basePath + name1 + File.separator;
249 String expected2 = basePath + name2 + File.separator;
250
251 assertEquals(expected1, TmfTraceManager.getSupplementaryFileDir(localTrace1));
252 assertEquals(expected2, TmfTraceManager.getSupplementaryFileDir(localTrace2));
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()));
268 TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange();
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
282 TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange();
283 assertEquals(ts, selection.getStartTime());
284 assertEquals(ts, selection.getEndTime());
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);
294 TmfTimeRange beforeTr = tm.getCurrentTraceContext().getSelectionRange();
295 ITmfTimestamp ts = new TmfTimestamp(t2start - ONE_SECOND, SCALE);
296 selectTimestamp(ts);
297
298 TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange();
299 assertEquals(beforeTr, selection);
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);
309 TmfTimeRange beforeTr = tm.getCurrentTraceContext().getSelectionRange();
310 ITmfTimestamp ts = new TmfTimestamp(t2end + ONE_SECOND, SCALE);
311 selectTimestamp(ts);
312
313 TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange();
314 assertEquals(beforeTr, selection);
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));
326 selectWindowRange(range);
327
328 TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange();
329 assertEquals(range, curRange);
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));
342 selectWindowRange(range);
343
344 TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange();
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
359 selectWindowRange(range);
360
361 TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange();
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
377 selectWindowRange(range);
378
379 TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange();
380 assertEquals(t2start, curRange.getStartTime().getValue());
381 assertEquals(t2end, curRange.getEndTime().getValue());
382 }
383
384 // ------------------------------------------------------------------------
385 // Test multiple, non-overlapping traces in parallel
386 // ------------------------------------------------------------------------
387
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 */
404 TmfTraceContext ctx = tm.getCurrentTraceContext();
405 assertEquals(ts, ctx.getSelectionRange().getStartTime());
406 assertEquals(ts, ctx.getSelectionRange().getEndTime());
407
408 /* Timestamp of trace2 should not have changed */
409 selectTrace(trace2);
410 ctx = tm.getCurrentTraceContext();
411 assertEquals(trace2.getStartTime(), ctx.getSelectionRange().getStartTime());
412 assertEquals(trace2.getStartTime(), ctx.getSelectionRange().getEndTime());
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 */
431 TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange();
432 assertEquals(trace1.getStartTime(), selection.getStartTime());
433 assertEquals(trace1.getStartTime(), selection.getEndTime());
434
435 /* Timestamp of trace2 should not have changed */
436 selectTrace(trace2);
437 selection = tm.getCurrentTraceContext().getSelectionRange();
438 assertEquals(trace2.getStartTime(), selection.getStartTime());
439 assertEquals(trace2.getStartTime(), selection.getEndTime());
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 */
457 TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange();
458 assertEquals(trace1.getStartTime(), selection.getStartTime());
459 assertEquals(trace1.getStartTime(), selection.getEndTime());
460
461 /* Timestamp of trace2 should not have changed */
462 selectTrace(trace2);
463 selection = tm.getCurrentTraceContext().getSelectionRange();
464 assertEquals(trace2.getStartTime(), selection.getStartTime());
465 assertEquals(trace2.getStartTime(), selection.getEndTime());
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));
481 selectWindowRange(range);
482
483 /* Range of trace1 should be equal to the requested one */
484 assertEquals(range, tm.getCurrentTraceContext().getWindowRange());
485
486 /* The range of trace 2 should not have changed */
487 selectTrace(trace2);
488 assertEquals(getInitialRange(trace2), tm.getCurrentTraceContext().getWindowRange());
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));
506 selectWindowRange(range);
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));
512 assertEquals(expectedRange, tm.getCurrentTraceContext().getWindowRange());
513
514 /* Range of trace2 should not have changed */
515 selectTrace(trace2);
516 assertEquals(getInitialRange(trace2), tm.getCurrentTraceContext().getWindowRange());
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));
533 selectWindowRange(range);
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));
539 assertEquals(expectedRange, tm.getCurrentTraceContext().getWindowRange());
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));
546 assertEquals(expectedRange, tm.getCurrentTraceContext().getWindowRange());
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));
563 selectWindowRange(range);
564
565 /* Range of trace1 should not have changed */
566 TmfTimeRange expectedRange = getInitialRange(trace1);
567 TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange();
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);
574 curRange = tm.getCurrentTraceContext().getWindowRange();
575 assertEquals(expectedRange.getStartTime(), curRange.getStartTime());
576 assertEquals(expectedRange.getEndTime(), curRange.getEndTime());
577 }
578
579 // ------------------------------------------------------------------------
580 // Test an experiment
581 // ------------------------------------------------------------------------
582
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. */
597 TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange();
598 assertEquals(ts, selection.getStartTime());
599 assertEquals(ts, selection.getEndTime());
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. */
617 TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange();
618 assertEquals(ts, selection.getStartTime());
619 assertEquals(ts, selection.getEndTime());
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. */
636 TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange();
637 assertEquals(trace1.getStartTime(), selection.getStartTime());
638 assertEquals(trace1.getStartTime(), selection.getEndTime());
639 }
640
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 */
652 final TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange();
653
654 assertEquals(getInitialRange(trace1), actualRange);
655 assertEquals(getInitialRange(exp), actualRange);
656 }
657
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);
666
667 final TmfTimeRange range = new TmfTimeRange(
668 new TmfTimestamp(t1start - ONE_SECOND, SCALE),
669 new TmfTimestamp(t1end - ONE_SECOND, SCALE));
670 selectWindowRange(range);
671
672 TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange();
673 assertEquals(t1start, actualRange.getStartTime().getValue());
674 assertEquals(t1end - ONE_SECOND, actualRange.getEndTime().getValue());
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));
690 selectWindowRange(range);
691
692 TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange();
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));
710 selectWindowRange(range);
711
712 TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange();
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 };
722 TmfExperiment exp = new TmfExperiment(ITmfEvent.class, "test-exp", traces,
723 TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null);
724 exp.indexTrace(true);
725 // Deregister experiment from signal manager so that it doesn't
726 // interfere with the TmfTraceManager tests
727 TmfSignalManager.deregister(exp);
728 return exp;
729 }
730
731 private static TmfTimeRange getInitialRange(ITmfTrace trace) {
732 return new TmfTimeRange(
733 trace.getStartTime(),
734 calculateOffset(trace.getStartTime(), trace.getInitialRangeOffset()));
735 }
736
737 /**
738 * Basically a "initial + offset" operation, but for ITmfTimetamp objects.
739 */
740 private static @NonNull ITmfTimestamp calculateOffset(ITmfTimestamp initialTs, ITmfTimestamp offsetTs) {
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 }
This page took 0.052407 seconds and 4 git commands to generate.