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