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