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