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
.core
.tests
.trace
;
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
.tests
.shared
.CtfTmfTestTraces
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
38 import org
.junit
.After
;
39 import org
.junit
.Before
;
40 import org
.junit
.BeforeClass
;
41 import org
.junit
.Test
;
44 * Test suite for the {@link TmfTraceManager}.
46 * @author Alexandre Montplaisir
48 public class TmfTraceManagerTest
{
50 private static final int SCALE
= ITmfTimestamp
.NANOSECOND_SCALE
;
52 private static ITmfTrace trace1
;
53 private static final long t1start
= 1331668247314038062L;
54 private static final long t1end
= 1331668259054285979L;
56 private static ITmfTrace trace2
;
57 private static final long t2start
= 1332170682440133097L;
58 private static final long t2end
= 1332170692664579801L;
60 private static final long ONE_SECOND
= 1000000000L;
62 private TmfTraceManager tm
;
66 * Test class initialization
69 public static void setUpClass() {
70 assumeTrue(CtfTmfTestTraces
.tracesExist());
71 trace1
= CtfTmfTestTraces
.getTestTrace(1);
72 trace2
= CtfTmfTestTraces
.getTestTrace(0);
74 trace1
.indexTrace(true);
75 trace2
.indexTrace(true);
83 tm
= TmfTraceManager
.getInstance();
90 public void tearDown() {
91 while (tm
.getActiveTrace() != null) {
92 closeTrace(tm
.getActiveTrace());
96 // ------------------------------------------------------------------------
97 // Dummy actions (fake signals)
98 // ------------------------------------------------------------------------
100 private void openTrace(ITmfTrace trace
) {
101 TmfSignalManager
.dispatchSignal(new TmfTraceOpenedSignal(this, trace
, null));
105 private void closeTrace(ITmfTrace trace
) {
106 TmfSignalManager
.dispatchSignal(new TmfTraceClosedSignal(this, trace
));
108 * In TMF, the next tab would now be selected (if there are some), which
109 * would select another trace automatically.
111 if (tm
.getOpenedTraces().size() > 0) {
112 selectTrace(tm
.getOpenedTraces().toArray(new ITmfTrace
[0])[0]);
116 private void selectTrace(ITmfTrace trace
) {
117 TmfSignalManager
.dispatchSignal(new TmfTraceSelectedSignal(this, trace
));
120 private void selectTimestamp(ITmfTimestamp ts
) {
121 TmfSignalManager
.dispatchSignal(new TmfTimeSynchSignal(this, ts
));
124 private void selectTimeRange(TmfTimeRange tr
) {
125 TmfSignalManager
.dispatchSignal(new TmfRangeSynchSignal(this, tr
));
128 // ------------------------------------------------------------------------
130 // ------------------------------------------------------------------------
133 * Test that the manager is correctly initialized
136 public void testInitialize() {
137 TmfTraceManager mgr
= TmfTraceManager
.getInstance();
143 * Test the contents of a trace set with one trace.
146 public void testTraceSet() {
151 ITmfTrace
[] expected
= new ITmfTrace
[] { trace2
};
152 ITmfTrace
[] actual
= tm
.getActiveTraceSet();
154 assertEquals(1, actual
.length
);
155 assertArrayEquals(expected
, actual
);
159 * Test the contents of a trace set with an experiment.
162 public void testTraceSetExperiment() {
163 TmfExperiment exp
= createExperiment(trace1
, trace2
);
167 ITmfTrace
[] expected
= new ITmfTrace
[] { trace1
, trace2
};
168 ITmfTrace
[] actual
= tm
.getActiveTraceSet();
170 assertEquals(2, actual
.length
);
171 assertArrayEquals(expected
, actual
);
175 * Test the {@link TmfTraceManager#getSupplementaryFileDir} method.
178 public void testSupplementaryFileDir() {
179 String name1
= trace1
.getName();
180 String name2
= trace2
.getName();
181 String basePath
= System
.getProperty("java.io.tmpdir") + File
.separator
;
183 String expected1
= basePath
+ name1
+ File
.separator
;
184 String expected2
= basePath
+ name2
+ File
.separator
;
186 assertEquals(expected1
, TmfTraceManager
.getSupplementaryFileDir(trace1
));
187 assertEquals(expected2
, TmfTraceManager
.getSupplementaryFileDir(trace2
));
190 // ------------------------------------------------------------------------
191 // Test a single trace
192 // ------------------------------------------------------------------------
195 * Test the initial range of a single trace.
198 public void testTraceInitialRange() {
200 final TmfTimeRange expectedRange
= new TmfTimeRange(
201 trace2
.getStartTime(),
202 calculateOffset(trace2
.getStartTime(), trace2
.getInitialRangeOffset()));
203 TmfTimeRange actualRange
= tm
.getCurrentRange();
204 assertEquals(expectedRange
, actualRange
);
208 * Try selecting a timestamp contained inside the trace's range. The trace's
209 * current time should get updated correctly.
212 public void testNewTimestamp() {
214 ITmfTimestamp ts
= new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
);
217 ITmfTimestamp afterTs
= tm
.getSelectionBeginTime();
218 assertEquals(ts
, afterTs
);
219 afterTs
= tm
.getSelectionEndTime();
220 assertEquals(ts
, afterTs
);
224 * Try selecting a timestamp happening before the trace's start. The change
228 public void testTimestampBefore() {
230 ITmfTimestamp beforeTs
= tm
.getSelectionBeginTime();
231 ITmfTimestamp ts
= new TmfTimestamp(t2start
- ONE_SECOND
, SCALE
);
234 ITmfTimestamp curTs
= tm
.getSelectionBeginTime();
235 assertEquals(beforeTs
, curTs
);
236 curTs
= tm
.getSelectionEndTime();
237 assertEquals(beforeTs
, curTs
);
241 * Try selecting a timestamp happening after the trace's end. The change
245 public void testTimestampAfter() {
247 ITmfTimestamp beforeTs
= tm
.getSelectionBeginTime();
248 ITmfTimestamp ts
= new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
);
251 ITmfTimestamp curTs
= tm
.getSelectionBeginTime();
252 assertEquals(beforeTs
, curTs
);
253 curTs
= tm
.getSelectionEndTime();
254 assertEquals(beforeTs
, curTs
);
258 * Test selecting a normal sub-range of a single trace.
261 public void testTraceNewTimeRange() {
263 TmfTimeRange range
= new TmfTimeRange(
264 new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
),
265 new TmfTimestamp(t2end
- ONE_SECOND
, SCALE
));
266 selectTimeRange(range
);
268 TmfTimeRange curRange
= tm
.getCurrentRange();
269 assertEquals(range
.getStartTime(), curRange
.getStartTime());
270 assertEquals(range
.getEndTime(), curRange
.getEndTime());
274 * Test selecting a range whose start time is before the trace's start time.
275 * The selected range should get clamped to the trace's range.
278 public void testTraceTimeRangeClampingStart() {
280 TmfTimeRange range
= new TmfTimeRange(
281 new TmfTimestamp(t2start
- ONE_SECOND
, SCALE
), // minus here
282 new TmfTimestamp(t2end
- ONE_SECOND
, SCALE
));
283 selectTimeRange(range
);
285 TmfTimeRange curRange
= tm
.getCurrentRange();
286 assertEquals(t2start
, curRange
.getStartTime().getValue());
287 assertEquals(range
.getEndTime(), curRange
.getEndTime());
291 * Test selecting a range whose end time is after the trace's end time.
292 * The selected range should get clamped to the trace's range.
295 public void testTraceTimeRangeClampingEnd() {
297 TmfTimeRange range
= new TmfTimeRange(
298 new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
),
299 new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
)); // plus here
300 selectTimeRange(range
);
302 TmfTimeRange curRange
= tm
.getCurrentRange();
303 assertEquals(range
.getStartTime(), curRange
.getStartTime());
304 assertEquals(t2end
, curRange
.getEndTime().getValue());
308 * Test selecting a range whose both start and end times are outside of the
309 * trace's range. The selected range should get clamped to the trace's
313 public void testTraceTimeRangeClampingBoth() {
315 TmfTimeRange range
= new TmfTimeRange(
316 new TmfTimestamp(t2start
- ONE_SECOND
, SCALE
), // minus here
317 new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
)); // plus here
318 selectTimeRange(range
);
320 TmfTimeRange curRange
= tm
.getCurrentRange();
321 assertEquals(t2start
, curRange
.getStartTime().getValue());
322 assertEquals(t2end
, curRange
.getEndTime().getValue());
325 // ------------------------------------------------------------------------
326 // Test multiple, non-overlapping traces in parallel
327 // ------------------------------------------------------------------------
330 * Test, with two traces in parallel, when we select a timestamp that is
331 * part of the first trace.
333 * The first trace's timestamp should be updated, but the second trace's one
337 public void testTwoTracesTimestampValid() {
341 TmfTimestamp ts
= new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
);
344 /* Timestamp of trace1 should have been updated */
345 assertEquals(ts
, tm
.getSelectionBeginTime());
346 assertEquals(ts
, tm
.getSelectionEndTime());
348 /* Timestamp of trace2 should not have changed */
350 assertEquals(trace2
.getStartTime(), tm
.getSelectionBeginTime());
351 assertEquals(trace2
.getStartTime(), tm
.getSelectionEndTime());
355 * Test, with two traces in parallel, when we select a timestamp that is
356 * between two traces.
358 * None of the trace's timestamps should be updated (we are not in an
362 public void testTwoTracesTimestampInBetween() {
366 TmfTimestamp ts
= new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
);
369 /* Timestamp of trace1 should not have changed */
370 assertEquals(trace1
.getStartTime(), tm
.getSelectionBeginTime());
371 assertEquals(trace1
.getStartTime(), tm
.getSelectionEndTime());
373 /* Timestamp of trace2 should not have changed */
375 assertEquals(trace2
.getStartTime(), tm
.getSelectionBeginTime());
376 assertEquals(trace2
.getStartTime(), tm
.getSelectionEndTime());
380 * Test, with two traces in parallel, when we select a timestamp that is
381 * completely out of the trace's range.
383 * None of the trace's timestamps should be updated.
386 public void testTwoTracesTimestampInvalid() {
390 TmfTimestamp ts
= new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
);
393 /* Timestamp of trace1 should not have changed */
394 assertEquals(trace1
.getStartTime(), tm
.getSelectionBeginTime());
395 assertEquals(trace1
.getStartTime(), tm
.getSelectionEndTime());
397 /* Timestamp of trace2 should not have changed */
399 assertEquals(trace2
.getStartTime(), tm
.getSelectionBeginTime());
400 assertEquals(trace2
.getStartTime(), tm
.getSelectionEndTime());
404 * Test, with two traces opened in parallel (not in an experiment), if we
405 * select a time range valid in one of them. That trace's time range should
406 * be updated, but not the other one.
409 public void testTwoTracesTimeRangeAllInOne() {
413 TmfTimeRange range
= new TmfTimeRange(
414 new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
),
415 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
));
416 selectTimeRange(range
);
418 /* Range of trace1 should be equal to the requested one */
419 assertEquals(range
, tm
.getCurrentRange());
421 /* The range of trace 2 should not have changed */
423 assertEquals(getInitialRange(trace2
), tm
.getCurrentRange());
427 * Test, with two traces in parallel, when we select a time range that is
428 * only partially valid for one of the traces.
430 * The first trace's time range should be clamped to a valid range, and the
431 * second one's should not change.
434 public void testTwoTracesTimeRangePartiallyInOne() {
438 TmfTimeRange range
= new TmfTimeRange(
439 new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
),
440 new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
));
441 selectTimeRange(range
);
443 /* Range of trace1 should get clamped to its end time */
444 TmfTimeRange expectedRange
= new TmfTimeRange(
445 new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
),
446 new TmfTimestamp(t1end
, SCALE
));
447 assertEquals(expectedRange
, tm
.getCurrentRange());
449 /* Range of trace2 should not have changed */
451 assertEquals(getInitialRange(trace2
), tm
.getCurrentRange());
455 * Test, with two traces in parallel, when we select a time range that is
456 * only partially valid for both traces.
458 * Each trace's time range should get clamped to respectively valid ranges.
461 public void testTwoTracesTimeRangeInBoth() {
465 TmfTimeRange range
= new TmfTimeRange(
466 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
),
467 new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
));
468 selectTimeRange(range
);
470 /* Range of trace1 should be clamped to its end time */
471 TmfTimeRange expectedRange
= new TmfTimeRange(
472 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
),
473 new TmfTimestamp(t1end
, SCALE
));
474 assertEquals(expectedRange
, tm
.getCurrentRange());
476 /* Range of trace2 should be clamped to its start time */
478 expectedRange
= new TmfTimeRange(
479 new TmfTimestamp(t2start
, SCALE
),
480 new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
));
481 assertEquals(expectedRange
, tm
.getCurrentRange());
485 * Test, with two traces in parallel, when we select a time range that is
486 * not valid for any trace.
488 * Each trace's time range should not be modified.
491 public void testTwoTracesTimeRangeInBetween() {
495 TmfTimeRange range
= new TmfTimeRange(
496 new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
),
497 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
));
498 selectTimeRange(range
);
500 /* Range of trace1 should not have changed */
501 TmfTimeRange expectedRange
= getInitialRange(trace1
);
502 TmfTimeRange curRange
= tm
.getCurrentRange();
503 assertEquals(expectedRange
.getStartTime(), curRange
.getStartTime());
504 assertEquals(expectedRange
.getEndTime(), curRange
.getEndTime());
506 /* Range of trace2 should not have changed */
508 expectedRange
= getInitialRange(trace2
);
509 curRange
= tm
.getCurrentRange();
510 assertEquals(expectedRange
.getStartTime(), curRange
.getStartTime());
511 assertEquals(expectedRange
.getEndTime(), curRange
.getEndTime());
514 // ------------------------------------------------------------------------
515 // Test an experiment
516 // ------------------------------------------------------------------------
519 * Test in an experiment when we select a timestamp that is part of one of
520 * the experiment's traces.
522 * The experiment's current time should be correctly updated.
525 public void testExperimentTimestampInTrace() {
526 TmfExperiment exp
= createExperiment(trace1
, trace2
);
528 TmfTimestamp ts
= new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
);
531 /* The experiment's current time should be updated. */
532 assertEquals(ts
, tm
.getSelectionBeginTime());
533 assertEquals(ts
, tm
.getSelectionEndTime());
537 * Test in an experiment when we select a timestamp that is between two
538 * traces in the experiment.
540 * The experiment's current time should still be updated, since the
541 * timestamp is valid in the experiment itself.
544 public void testExperimentTimestampInBetween() {
545 TmfExperiment exp
= createExperiment(trace1
, trace2
);
547 TmfTimestamp ts
= new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
);
550 /* The experiment's current time should be updated. */
551 assertEquals(ts
, tm
.getSelectionBeginTime());
552 assertEquals(ts
, tm
.getSelectionEndTime());
556 * Test in an experiment when we select a timestamp that is outside of the
557 * total range of the experiment.
559 * The experiment's current time should not be updated.
562 public void testExperimentTimestampInvalid() {
563 TmfExperiment exp
= createExperiment(trace1
, trace2
);
565 TmfTimestamp ts
= new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
);
568 /* The experiment's current time should NOT be updated. */
569 assertEquals(trace1
.getStartTime(), tm
.getSelectionBeginTime());
570 assertEquals(trace1
.getStartTime(), tm
.getSelectionEndTime());
574 * Test the initial range of an experiment.
577 public void testExperimentInitialRange() {
578 TmfExperiment exp
= createExperiment(trace1
, trace2
);
581 * The initial range should be == to the initial range of the earliest
582 * trace (here trace1).
584 final TmfTimeRange actualRange
= tm
.getCurrentRange();
586 assertEquals(getInitialRange(trace1
), actualRange
);
587 assertEquals(getInitialRange(exp
), actualRange
);
591 * Test the range clamping with the start time of the range outside of the
592 * earliest trace's range. Only that start time should get clamped.
595 public void testExperimentRangeClampingOne() {
596 TmfExperiment exp
= createExperiment(trace1
, trace2
);
599 final TmfTimeRange range
= new TmfTimeRange(
600 new TmfTimestamp(t1start
- ONE_SECOND
, SCALE
),
601 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
));
602 selectTimeRange(range
);
604 TmfTimeRange actualRange
= tm
.getCurrentRange();
605 assertEquals(t1start
, actualRange
.getStartTime().getValue());
606 assertEquals(t1end
- ONE_SECOND
, actualRange
.getEndTime().getValue());
610 * Test the range clamping when both the start and end times of the signal's
611 * range are outside of the trace's range. The range should clamp to the
612 * experiment's range.
615 public void testExperimentRangeClampingBoth() {
616 TmfExperiment exp
= createExperiment(trace1
, trace2
);
619 final TmfTimeRange range
= new TmfTimeRange(
620 new TmfTimestamp(t1start
- ONE_SECOND
, SCALE
),
621 new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
));
622 selectTimeRange(range
);
624 TmfTimeRange actualRange
= tm
.getCurrentRange();
625 assertEquals(t1start
, actualRange
.getStartTime().getValue());
626 assertEquals(t2end
, actualRange
.getEndTime().getValue());
630 * Test selecting a range in-between two disjoint traces in an experiment.
631 * The range should still get correctly selected, even if no trace has any
632 * events in that range.
635 public void testExperimentRangeInBetween() {
636 TmfExperiment exp
= createExperiment(trace1
, trace2
);
639 final TmfTimeRange range
= new TmfTimeRange(
640 new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
),
641 new TmfTimestamp(t2start
- ONE_SECOND
, SCALE
));
642 selectTimeRange(range
);
644 TmfTimeRange actualRange
= tm
.getCurrentRange();
645 assertEquals(range
, actualRange
);
648 // ------------------------------------------------------------------------
650 // ------------------------------------------------------------------------
652 private static TmfExperiment
createExperiment(ITmfTrace t1
, ITmfTrace t2
) {
653 ITmfTrace
[] traces
= new ITmfTrace
[] { t1
, t2
};
654 TmfExperiment exp
= new TmfExperiment(ITmfEvent
.class, "test-exp", traces
);
655 exp
.indexTrace(true);
659 private static TmfTimeRange
getInitialRange(ITmfTrace trace
) {
660 return new TmfTimeRange(
661 trace
.getStartTime(),
662 calculateOffset(trace
.getStartTime(), trace
.getInitialRangeOffset()));
666 * Basically a "initial + offset" operation, but for ITmfTimetamp objects.
668 private static ITmfTimestamp
calculateOffset(ITmfTimestamp initialTs
, ITmfTimestamp offsetTs
) {
669 long start
= initialTs
.normalize(0, SCALE
).getValue();
670 long offset
= offsetTs
.normalize(0, SCALE
).getValue();
671 return new TmfTimestamp(start
+ offset
, SCALE
);