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