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