1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 * Patrick Tasse - Support selection range
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ctf
.core
.tests
.tracemanager
;
16 import static org
.junit
.Assert
.assertArrayEquals
;
17 import static org
.junit
.Assert
.assertEquals
;
18 import static org
.junit
.Assert
.assertNotNull
;
19 import static org
.junit
.Assert
.assertSame
;
20 import static org
.junit
.Assume
.assumeTrue
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfRangeSynchSignal
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceClosedSignal
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
37 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
38 import org
.junit
.After
;
39 import org
.junit
.AfterClass
;
40 import org
.junit
.Before
;
41 import org
.junit
.BeforeClass
;
42 import org
.junit
.Test
;
45 * Test suite for the {@link TmfTraceManager}.
47 * @author Alexandre Montplaisir
49 public class TmfTraceManagerTest
{
51 private static final int SCALE
= ITmfTimestamp
.NANOSECOND_SCALE
;
53 private static ITmfTrace trace1
;
54 private static final long t1start
= 1331668247314038062L;
55 private static final long t1end
= 1331668259054285979L;
57 private static ITmfTrace trace2
;
58 private static final long t2start
= 1332170682440133097L;
59 private static final long t2end
= 1332170692664579801L;
61 private static final long ONE_SECOND
= 1000000000L;
63 private TmfTraceManager tm
;
67 * Test class initialization
70 public static void setUpClass() {
71 assumeTrue(CtfTmfTestTrace
.TRACE2
.exists());
72 assumeTrue(CtfTmfTestTrace
.KERNEL
.exists());
73 trace1
= CtfTmfTestTrace
.TRACE2
.getTrace();
74 trace2
= CtfTmfTestTrace
.KERNEL
.getTrace();
76 trace1
.indexTrace(true);
77 trace2
.indexTrace(true);
79 // Deregister traces from signal manager so that they don't
80 // interfere with the TmfTraceManager tests
81 TmfSignalManager
.deregister(trace1
);
82 TmfSignalManager
.deregister(trace2
);
90 tm
= TmfTraceManager
.getInstance();
97 public void tearDown() {
98 while (tm
.getActiveTrace() != null) {
99 closeTrace(tm
.getActiveTrace());
104 * Test class clean-up
107 public static void tearDownClass() {
108 CtfTmfTestTrace
.TRACE2
.dispose();
109 CtfTmfTestTrace
.KERNEL
.dispose();
112 // ------------------------------------------------------------------------
113 // Dummy actions (fake signals)
114 // ------------------------------------------------------------------------
116 private void openTrace(ITmfTrace trace
) {
117 TmfSignalManager
.dispatchSignal(new TmfTraceOpenedSignal(this, trace
, null));
121 private void closeTrace(ITmfTrace trace
) {
122 TmfSignalManager
.dispatchSignal(new TmfTraceClosedSignal(this, trace
));
124 * In TMF, the next tab would now be selected (if there are some), which
125 * would select another trace automatically.
127 if (tm
.getOpenedTraces().size() > 0) {
128 selectTrace(tm
.getOpenedTraces().toArray(new ITmfTrace
[0])[0]);
132 private void selectTrace(ITmfTrace trace
) {
133 TmfSignalManager
.dispatchSignal(new TmfTraceSelectedSignal(this, trace
));
136 private void selectTimestamp(ITmfTimestamp ts
) {
137 TmfSignalManager
.dispatchSignal(new TmfTimeSynchSignal(this, ts
));
140 private void selectTimeRange(TmfTimeRange tr
) {
141 TmfSignalManager
.dispatchSignal(new TmfRangeSynchSignal(this, tr
));
144 // ------------------------------------------------------------------------
146 // ------------------------------------------------------------------------
149 * Test that the manager is correctly initialized
152 public void testInitialize() {
153 TmfTraceManager mgr
= TmfTraceManager
.getInstance();
159 * Test the contents of a trace set with one trace.
162 public void testTraceSet() {
167 ITmfTrace
[] expected
= new ITmfTrace
[] { trace2
};
168 ITmfTrace
[] actual
= tm
.getActiveTraceSet();
170 assertEquals(1, actual
.length
);
171 assertArrayEquals(expected
, actual
);
175 * Test the contents of a trace set with an experiment.
178 public void testTraceSetExperiment() {
179 TmfExperiment exp
= createExperiment(trace1
, trace2
);
183 ITmfTrace
[] expected
= new ITmfTrace
[] { trace1
, trace2
};
184 ITmfTrace
[] actual
= tm
.getActiveTraceSet();
186 assertEquals(2, actual
.length
);
187 assertArrayEquals(expected
, actual
);
191 * Test the {@link TmfTraceManager#getSupplementaryFileDir} method.
194 public void testSupplementaryFileDir() {
195 String name1
= trace1
.getName();
196 String name2
= trace2
.getName();
197 String basePath
= System
.getProperty("java.io.tmpdir") + File
.separator
;
199 String expected1
= basePath
+ name1
+ File
.separator
;
200 String expected2
= basePath
+ name2
+ File
.separator
;
202 assertEquals(expected1
, TmfTraceManager
.getSupplementaryFileDir(trace1
));
203 assertEquals(expected2
, TmfTraceManager
.getSupplementaryFileDir(trace2
));
206 // ------------------------------------------------------------------------
207 // Test a single trace
208 // ------------------------------------------------------------------------
211 * Test the initial range of a single trace.
214 public void testTraceInitialRange() {
216 final TmfTimeRange expectedRange
= new TmfTimeRange(
217 trace2
.getStartTime(),
218 calculateOffset(trace2
.getStartTime(), trace2
.getInitialRangeOffset()));
219 TmfTimeRange actualRange
= tm
.getCurrentRange();
220 assertEquals(expectedRange
, actualRange
);
224 * Try selecting a timestamp contained inside the trace's range. The trace's
225 * current time should get updated correctly.
228 public void testNewTimestamp() {
230 ITmfTimestamp ts
= new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
);
233 ITmfTimestamp afterTs
= tm
.getSelectionBeginTime();
234 assertEquals(ts
, afterTs
);
235 afterTs
= tm
.getSelectionEndTime();
236 assertEquals(ts
, afterTs
);
240 * Try selecting a timestamp happening before the trace's start. The change
244 public void testTimestampBefore() {
246 ITmfTimestamp beforeTs
= tm
.getSelectionBeginTime();
247 ITmfTimestamp ts
= new TmfTimestamp(t2start
- ONE_SECOND
, SCALE
);
250 ITmfTimestamp curTs
= tm
.getSelectionBeginTime();
251 assertEquals(beforeTs
, curTs
);
252 curTs
= tm
.getSelectionEndTime();
253 assertEquals(beforeTs
, curTs
);
257 * Try selecting a timestamp happening after the trace's end. The change
261 public void testTimestampAfter() {
263 ITmfTimestamp beforeTs
= tm
.getSelectionBeginTime();
264 ITmfTimestamp ts
= new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
);
267 ITmfTimestamp curTs
= tm
.getSelectionBeginTime();
268 assertEquals(beforeTs
, curTs
);
269 curTs
= tm
.getSelectionEndTime();
270 assertEquals(beforeTs
, curTs
);
274 * Test selecting a normal sub-range of a single trace.
277 public void testTraceNewTimeRange() {
279 TmfTimeRange range
= new TmfTimeRange(
280 new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
),
281 new TmfTimestamp(t2end
- ONE_SECOND
, SCALE
));
282 selectTimeRange(range
);
284 TmfTimeRange curRange
= tm
.getCurrentRange();
285 assertEquals(range
.getStartTime(), curRange
.getStartTime());
286 assertEquals(range
.getEndTime(), curRange
.getEndTime());
290 * Test selecting a range whose start time is before the trace's start time.
291 * The selected range should get clamped to the trace's range.
294 public void testTraceTimeRangeClampingStart() {
296 TmfTimeRange range
= new TmfTimeRange(
297 new TmfTimestamp(t2start
- ONE_SECOND
, SCALE
), // minus here
298 new TmfTimestamp(t2end
- ONE_SECOND
, SCALE
));
299 selectTimeRange(range
);
301 TmfTimeRange curRange
= tm
.getCurrentRange();
302 assertEquals(t2start
, curRange
.getStartTime().getValue());
303 assertEquals(range
.getEndTime(), curRange
.getEndTime());
307 * Test selecting a range whose end time is after the trace's end time.
308 * The selected range should get clamped to the trace's range.
311 public void testTraceTimeRangeClampingEnd() {
313 TmfTimeRange range
= new TmfTimeRange(
314 new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
),
315 new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
)); // plus here
316 selectTimeRange(range
);
318 TmfTimeRange curRange
= tm
.getCurrentRange();
319 assertEquals(range
.getStartTime(), curRange
.getStartTime());
320 assertEquals(t2end
, curRange
.getEndTime().getValue());
324 * Test selecting a range whose both start and end times are outside of the
325 * trace's range. The selected range should get clamped to the trace's
329 public void testTraceTimeRangeClampingBoth() {
331 TmfTimeRange range
= new TmfTimeRange(
332 new TmfTimestamp(t2start
- ONE_SECOND
, SCALE
), // minus here
333 new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
)); // plus here
334 selectTimeRange(range
);
336 TmfTimeRange curRange
= tm
.getCurrentRange();
337 assertEquals(t2start
, curRange
.getStartTime().getValue());
338 assertEquals(t2end
, curRange
.getEndTime().getValue());
341 // ------------------------------------------------------------------------
342 // Test multiple, non-overlapping traces in parallel
343 // ------------------------------------------------------------------------
346 * Test, with two traces in parallel, when we select a timestamp that is
347 * part of the first trace.
349 * The first trace's timestamp should be updated, but the second trace's one
353 public void testTwoTracesTimestampValid() {
357 TmfTimestamp ts
= new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
);
360 /* Timestamp of trace1 should have been updated */
361 assertEquals(ts
, tm
.getSelectionBeginTime());
362 assertEquals(ts
, tm
.getSelectionEndTime());
364 /* Timestamp of trace2 should not have changed */
366 assertEquals(trace2
.getStartTime(), tm
.getSelectionBeginTime());
367 assertEquals(trace2
.getStartTime(), tm
.getSelectionEndTime());
371 * Test, with two traces in parallel, when we select a timestamp that is
372 * between two traces.
374 * None of the trace's timestamps should be updated (we are not in an
378 public void testTwoTracesTimestampInBetween() {
382 TmfTimestamp ts
= new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
);
385 /* Timestamp of trace1 should not have changed */
386 assertEquals(trace1
.getStartTime(), tm
.getSelectionBeginTime());
387 assertEquals(trace1
.getStartTime(), tm
.getSelectionEndTime());
389 /* Timestamp of trace2 should not have changed */
391 assertEquals(trace2
.getStartTime(), tm
.getSelectionBeginTime());
392 assertEquals(trace2
.getStartTime(), tm
.getSelectionEndTime());
396 * Test, with two traces in parallel, when we select a timestamp that is
397 * completely out of the trace's range.
399 * None of the trace's timestamps should be updated.
402 public void testTwoTracesTimestampInvalid() {
406 TmfTimestamp ts
= new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
);
409 /* Timestamp of trace1 should not have changed */
410 assertEquals(trace1
.getStartTime(), tm
.getSelectionBeginTime());
411 assertEquals(trace1
.getStartTime(), tm
.getSelectionEndTime());
413 /* Timestamp of trace2 should not have changed */
415 assertEquals(trace2
.getStartTime(), tm
.getSelectionBeginTime());
416 assertEquals(trace2
.getStartTime(), tm
.getSelectionEndTime());
420 * Test, with two traces opened in parallel (not in an experiment), if we
421 * select a time range valid in one of them. That trace's time range should
422 * be updated, but not the other one.
425 public void testTwoTracesTimeRangeAllInOne() {
429 TmfTimeRange range
= new TmfTimeRange(
430 new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
),
431 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
));
432 selectTimeRange(range
);
434 /* Range of trace1 should be equal to the requested one */
435 assertEquals(range
, tm
.getCurrentRange());
437 /* The range of trace 2 should not have changed */
439 assertEquals(getInitialRange(trace2
), tm
.getCurrentRange());
443 * Test, with two traces in parallel, when we select a time range that is
444 * only partially valid for one of the traces.
446 * The first trace's time range should be clamped to a valid range, and the
447 * second one's should not change.
450 public void testTwoTracesTimeRangePartiallyInOne() {
454 TmfTimeRange range
= new TmfTimeRange(
455 new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
),
456 new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
));
457 selectTimeRange(range
);
459 /* Range of trace1 should get clamped to its end time */
460 TmfTimeRange expectedRange
= new TmfTimeRange(
461 new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
),
462 new TmfTimestamp(t1end
, SCALE
));
463 assertEquals(expectedRange
, tm
.getCurrentRange());
465 /* Range of trace2 should not have changed */
467 assertEquals(getInitialRange(trace2
), tm
.getCurrentRange());
471 * Test, with two traces in parallel, when we select a time range that is
472 * only partially valid for both traces.
474 * Each trace's time range should get clamped to respectively valid ranges.
477 public void testTwoTracesTimeRangeInBoth() {
481 TmfTimeRange range
= new TmfTimeRange(
482 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
),
483 new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
));
484 selectTimeRange(range
);
486 /* Range of trace1 should be clamped to its end time */
487 TmfTimeRange expectedRange
= new TmfTimeRange(
488 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
),
489 new TmfTimestamp(t1end
, SCALE
));
490 assertEquals(expectedRange
, tm
.getCurrentRange());
492 /* Range of trace2 should be clamped to its start time */
494 expectedRange
= new TmfTimeRange(
495 new TmfTimestamp(t2start
, SCALE
),
496 new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
));
497 assertEquals(expectedRange
, tm
.getCurrentRange());
501 * Test, with two traces in parallel, when we select a time range that is
502 * not valid for any trace.
504 * Each trace's time range should not be modified.
507 public void testTwoTracesTimeRangeInBetween() {
511 TmfTimeRange range
= new TmfTimeRange(
512 new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
),
513 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
));
514 selectTimeRange(range
);
516 /* Range of trace1 should not have changed */
517 TmfTimeRange expectedRange
= getInitialRange(trace1
);
518 TmfTimeRange curRange
= tm
.getCurrentRange();
519 assertEquals(expectedRange
.getStartTime(), curRange
.getStartTime());
520 assertEquals(expectedRange
.getEndTime(), curRange
.getEndTime());
522 /* Range of trace2 should not have changed */
524 expectedRange
= getInitialRange(trace2
);
525 curRange
= tm
.getCurrentRange();
526 assertEquals(expectedRange
.getStartTime(), curRange
.getStartTime());
527 assertEquals(expectedRange
.getEndTime(), curRange
.getEndTime());
530 // ------------------------------------------------------------------------
531 // Test an experiment
532 // ------------------------------------------------------------------------
535 * Test in an experiment when we select a timestamp that is part of one of
536 * the experiment's traces.
538 * The experiment's current time should be correctly updated.
541 public void testExperimentTimestampInTrace() {
542 TmfExperiment exp
= createExperiment(trace1
, trace2
);
544 TmfTimestamp ts
= new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
);
547 /* The experiment's current time should be updated. */
548 assertEquals(ts
, tm
.getSelectionBeginTime());
549 assertEquals(ts
, tm
.getSelectionEndTime());
553 * Test in an experiment when we select a timestamp that is between two
554 * traces in the experiment.
556 * The experiment's current time should still be updated, since the
557 * timestamp is valid in the experiment itself.
560 public void testExperimentTimestampInBetween() {
561 TmfExperiment exp
= createExperiment(trace1
, trace2
);
563 TmfTimestamp ts
= new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
);
566 /* The experiment's current time should be updated. */
567 assertEquals(ts
, tm
.getSelectionBeginTime());
568 assertEquals(ts
, tm
.getSelectionEndTime());
572 * Test in an experiment when we select a timestamp that is outside of the
573 * total range of the experiment.
575 * The experiment's current time should not be updated.
578 public void testExperimentTimestampInvalid() {
579 TmfExperiment exp
= createExperiment(trace1
, trace2
);
581 TmfTimestamp ts
= new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
);
584 /* The experiment's current time should NOT be updated. */
585 assertEquals(trace1
.getStartTime(), tm
.getSelectionBeginTime());
586 assertEquals(trace1
.getStartTime(), tm
.getSelectionEndTime());
590 * Test the initial range of an experiment.
593 public void testExperimentInitialRange() {
594 TmfExperiment exp
= createExperiment(trace1
, trace2
);
597 * The initial range should be == to the initial range of the earliest
598 * trace (here trace1).
600 final TmfTimeRange actualRange
= tm
.getCurrentRange();
602 assertEquals(getInitialRange(trace1
), actualRange
);
603 assertEquals(getInitialRange(exp
), actualRange
);
607 * Test the range clamping with the start time of the range outside of the
608 * earliest trace's range. Only that start time should get clamped.
611 public void testExperimentRangeClampingOne() {
612 TmfExperiment exp
= createExperiment(trace1
, trace2
);
615 final TmfTimeRange range
= new TmfTimeRange(
616 new TmfTimestamp(t1start
- ONE_SECOND
, SCALE
),
617 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
));
618 selectTimeRange(range
);
620 TmfTimeRange actualRange
= tm
.getCurrentRange();
621 assertEquals(t1start
, actualRange
.getStartTime().getValue());
622 assertEquals(t1end
- ONE_SECOND
, actualRange
.getEndTime().getValue());
626 * Test the range clamping when both the start and end times of the signal's
627 * range are outside of the trace's range. The range should clamp to the
628 * experiment's range.
631 public void testExperimentRangeClampingBoth() {
632 TmfExperiment exp
= createExperiment(trace1
, trace2
);
635 final TmfTimeRange range
= new TmfTimeRange(
636 new TmfTimestamp(t1start
- ONE_SECOND
, SCALE
),
637 new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
));
638 selectTimeRange(range
);
640 TmfTimeRange actualRange
= tm
.getCurrentRange();
641 assertEquals(t1start
, actualRange
.getStartTime().getValue());
642 assertEquals(t2end
, actualRange
.getEndTime().getValue());
646 * Test selecting a range in-between two disjoint traces in an experiment.
647 * The range should still get correctly selected, even if no trace has any
648 * events in that range.
651 public void testExperimentRangeInBetween() {
652 TmfExperiment exp
= createExperiment(trace1
, trace2
);
655 final TmfTimeRange range
= new TmfTimeRange(
656 new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
),
657 new TmfTimestamp(t2start
- ONE_SECOND
, SCALE
));
658 selectTimeRange(range
);
660 TmfTimeRange actualRange
= tm
.getCurrentRange();
661 assertEquals(range
, actualRange
);
664 // ------------------------------------------------------------------------
666 // ------------------------------------------------------------------------
668 private static TmfExperiment
createExperiment(ITmfTrace t1
, ITmfTrace t2
) {
669 ITmfTrace
[] traces
= new ITmfTrace
[] { t1
, t2
};
670 TmfExperiment exp
= new TmfExperiment(ITmfEvent
.class, "test-exp", traces
);
671 exp
.indexTrace(true);
672 // Deregister experiment from signal manager so that it doesn't
673 // interfere with the TmfTraceManager tests
674 TmfSignalManager
.deregister(exp
);
678 private static TmfTimeRange
getInitialRange(ITmfTrace trace
) {
679 return new TmfTimeRange(
680 trace
.getStartTime(),
681 calculateOffset(trace
.getStartTime(), trace
.getInitialRangeOffset()));
685 * Basically a "initial + offset" operation, but for ITmfTimetamp objects.
687 private static ITmfTimestamp
calculateOffset(ITmfTimestamp initialTs
, ITmfTimestamp offsetTs
) {
688 long start
= initialTs
.normalize(0, SCALE
).getValue();
689 long offset
= offsetTs
.normalize(0, SCALE
).getValue();
690 return new TmfTimestamp(start
+ offset
, SCALE
);