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
.CtfTmfTestTrace
;
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(CtfTmfTestTrace
.TRACE2
.exists());
71 assumeTrue(CtfTmfTestTrace
.KERNEL
.exists());
72 trace1
= CtfTmfTestTrace
.TRACE2
.getTrace();
73 trace2
= CtfTmfTestTrace
.KERNEL
.getTrace();
75 trace1
.indexTrace(true);
76 trace2
.indexTrace(true);
84 tm
= TmfTraceManager
.getInstance();
91 public void tearDown() {
92 while (tm
.getActiveTrace() != null) {
93 closeTrace(tm
.getActiveTrace());
97 // ------------------------------------------------------------------------
98 // Dummy actions (fake signals)
99 // ------------------------------------------------------------------------
101 private void openTrace(ITmfTrace trace
) {
102 TmfSignalManager
.dispatchSignal(new TmfTraceOpenedSignal(this, trace
, null));
106 private void closeTrace(ITmfTrace trace
) {
107 TmfSignalManager
.dispatchSignal(new TmfTraceClosedSignal(this, trace
));
109 * In TMF, the next tab would now be selected (if there are some), which
110 * would select another trace automatically.
112 if (tm
.getOpenedTraces().size() > 0) {
113 selectTrace(tm
.getOpenedTraces().toArray(new ITmfTrace
[0])[0]);
117 private void selectTrace(ITmfTrace trace
) {
118 TmfSignalManager
.dispatchSignal(new TmfTraceSelectedSignal(this, trace
));
121 private void selectTimestamp(ITmfTimestamp ts
) {
122 TmfSignalManager
.dispatchSignal(new TmfTimeSynchSignal(this, ts
));
125 private void selectTimeRange(TmfTimeRange tr
) {
126 TmfSignalManager
.dispatchSignal(new TmfRangeSynchSignal(this, tr
));
129 // ------------------------------------------------------------------------
131 // ------------------------------------------------------------------------
134 * Test that the manager is correctly initialized
137 public void testInitialize() {
138 TmfTraceManager mgr
= TmfTraceManager
.getInstance();
144 * Test the contents of a trace set with one trace.
147 public void testTraceSet() {
152 ITmfTrace
[] expected
= new ITmfTrace
[] { trace2
};
153 ITmfTrace
[] actual
= tm
.getActiveTraceSet();
155 assertEquals(1, actual
.length
);
156 assertArrayEquals(expected
, actual
);
160 * Test the contents of a trace set with an experiment.
163 public void testTraceSetExperiment() {
164 TmfExperiment exp
= createExperiment(trace1
, trace2
);
168 ITmfTrace
[] expected
= new ITmfTrace
[] { trace1
, trace2
};
169 ITmfTrace
[] actual
= tm
.getActiveTraceSet();
171 assertEquals(2, actual
.length
);
172 assertArrayEquals(expected
, actual
);
176 * Test the {@link TmfTraceManager#getSupplementaryFileDir} method.
179 public void testSupplementaryFileDir() {
180 String name1
= trace1
.getName();
181 String name2
= trace2
.getName();
182 String basePath
= System
.getProperty("java.io.tmpdir") + File
.separator
;
184 String expected1
= basePath
+ name1
+ File
.separator
;
185 String expected2
= basePath
+ name2
+ File
.separator
;
187 assertEquals(expected1
, TmfTraceManager
.getSupplementaryFileDir(trace1
));
188 assertEquals(expected2
, TmfTraceManager
.getSupplementaryFileDir(trace2
));
191 // ------------------------------------------------------------------------
192 // Test a single trace
193 // ------------------------------------------------------------------------
196 * Test the initial range of a single trace.
199 public void testTraceInitialRange() {
201 final TmfTimeRange expectedRange
= new TmfTimeRange(
202 trace2
.getStartTime(),
203 calculateOffset(trace2
.getStartTime(), trace2
.getInitialRangeOffset()));
204 TmfTimeRange actualRange
= tm
.getCurrentRange();
205 assertEquals(expectedRange
, actualRange
);
209 * Try selecting a timestamp contained inside the trace's range. The trace's
210 * current time should get updated correctly.
213 public void testNewTimestamp() {
215 ITmfTimestamp ts
= new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
);
218 ITmfTimestamp afterTs
= tm
.getSelectionBeginTime();
219 assertEquals(ts
, afterTs
);
220 afterTs
= tm
.getSelectionEndTime();
221 assertEquals(ts
, afterTs
);
225 * Try selecting a timestamp happening before the trace's start. The change
229 public void testTimestampBefore() {
231 ITmfTimestamp beforeTs
= tm
.getSelectionBeginTime();
232 ITmfTimestamp ts
= new TmfTimestamp(t2start
- ONE_SECOND
, SCALE
);
235 ITmfTimestamp curTs
= tm
.getSelectionBeginTime();
236 assertEquals(beforeTs
, curTs
);
237 curTs
= tm
.getSelectionEndTime();
238 assertEquals(beforeTs
, curTs
);
242 * Try selecting a timestamp happening after the trace's end. The change
246 public void testTimestampAfter() {
248 ITmfTimestamp beforeTs
= tm
.getSelectionBeginTime();
249 ITmfTimestamp ts
= new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
);
252 ITmfTimestamp curTs
= tm
.getSelectionBeginTime();
253 assertEquals(beforeTs
, curTs
);
254 curTs
= tm
.getSelectionEndTime();
255 assertEquals(beforeTs
, curTs
);
259 * Test selecting a normal sub-range of a single trace.
262 public void testTraceNewTimeRange() {
264 TmfTimeRange range
= new TmfTimeRange(
265 new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
),
266 new TmfTimestamp(t2end
- ONE_SECOND
, SCALE
));
267 selectTimeRange(range
);
269 TmfTimeRange curRange
= tm
.getCurrentRange();
270 assertEquals(range
.getStartTime(), curRange
.getStartTime());
271 assertEquals(range
.getEndTime(), curRange
.getEndTime());
275 * Test selecting a range whose start time is before the trace's start time.
276 * The selected range should get clamped to the trace's range.
279 public void testTraceTimeRangeClampingStart() {
281 TmfTimeRange range
= new TmfTimeRange(
282 new TmfTimestamp(t2start
- ONE_SECOND
, SCALE
), // minus here
283 new TmfTimestamp(t2end
- ONE_SECOND
, SCALE
));
284 selectTimeRange(range
);
286 TmfTimeRange curRange
= tm
.getCurrentRange();
287 assertEquals(t2start
, curRange
.getStartTime().getValue());
288 assertEquals(range
.getEndTime(), curRange
.getEndTime());
292 * Test selecting a range whose end time is after the trace's end time.
293 * The selected range should get clamped to the trace's range.
296 public void testTraceTimeRangeClampingEnd() {
298 TmfTimeRange range
= new TmfTimeRange(
299 new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
),
300 new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
)); // plus here
301 selectTimeRange(range
);
303 TmfTimeRange curRange
= tm
.getCurrentRange();
304 assertEquals(range
.getStartTime(), curRange
.getStartTime());
305 assertEquals(t2end
, curRange
.getEndTime().getValue());
309 * Test selecting a range whose both start and end times are outside of the
310 * trace's range. The selected range should get clamped to the trace's
314 public void testTraceTimeRangeClampingBoth() {
316 TmfTimeRange range
= new TmfTimeRange(
317 new TmfTimestamp(t2start
- ONE_SECOND
, SCALE
), // minus here
318 new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
)); // plus here
319 selectTimeRange(range
);
321 TmfTimeRange curRange
= tm
.getCurrentRange();
322 assertEquals(t2start
, curRange
.getStartTime().getValue());
323 assertEquals(t2end
, curRange
.getEndTime().getValue());
326 // ------------------------------------------------------------------------
327 // Test multiple, non-overlapping traces in parallel
328 // ------------------------------------------------------------------------
331 * Test, with two traces in parallel, when we select a timestamp that is
332 * part of the first trace.
334 * The first trace's timestamp should be updated, but the second trace's one
338 public void testTwoTracesTimestampValid() {
342 TmfTimestamp ts
= new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
);
345 /* Timestamp of trace1 should have been updated */
346 assertEquals(ts
, tm
.getSelectionBeginTime());
347 assertEquals(ts
, tm
.getSelectionEndTime());
349 /* Timestamp of trace2 should not have changed */
351 assertEquals(trace2
.getStartTime(), tm
.getSelectionBeginTime());
352 assertEquals(trace2
.getStartTime(), tm
.getSelectionEndTime());
356 * Test, with two traces in parallel, when we select a timestamp that is
357 * between two traces.
359 * None of the trace's timestamps should be updated (we are not in an
363 public void testTwoTracesTimestampInBetween() {
367 TmfTimestamp ts
= new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
);
370 /* Timestamp of trace1 should not have changed */
371 assertEquals(trace1
.getStartTime(), tm
.getSelectionBeginTime());
372 assertEquals(trace1
.getStartTime(), tm
.getSelectionEndTime());
374 /* Timestamp of trace2 should not have changed */
376 assertEquals(trace2
.getStartTime(), tm
.getSelectionBeginTime());
377 assertEquals(trace2
.getStartTime(), tm
.getSelectionEndTime());
381 * Test, with two traces in parallel, when we select a timestamp that is
382 * completely out of the trace's range.
384 * None of the trace's timestamps should be updated.
387 public void testTwoTracesTimestampInvalid() {
391 TmfTimestamp ts
= new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
);
394 /* Timestamp of trace1 should not have changed */
395 assertEquals(trace1
.getStartTime(), tm
.getSelectionBeginTime());
396 assertEquals(trace1
.getStartTime(), tm
.getSelectionEndTime());
398 /* Timestamp of trace2 should not have changed */
400 assertEquals(trace2
.getStartTime(), tm
.getSelectionBeginTime());
401 assertEquals(trace2
.getStartTime(), tm
.getSelectionEndTime());
405 * Test, with two traces opened in parallel (not in an experiment), if we
406 * select a time range valid in one of them. That trace's time range should
407 * be updated, but not the other one.
410 public void testTwoTracesTimeRangeAllInOne() {
414 TmfTimeRange range
= new TmfTimeRange(
415 new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
),
416 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
));
417 selectTimeRange(range
);
419 /* Range of trace1 should be equal to the requested one */
420 assertEquals(range
, tm
.getCurrentRange());
422 /* The range of trace 2 should not have changed */
424 assertEquals(getInitialRange(trace2
), tm
.getCurrentRange());
428 * Test, with two traces in parallel, when we select a time range that is
429 * only partially valid for one of the traces.
431 * The first trace's time range should be clamped to a valid range, and the
432 * second one's should not change.
435 public void testTwoTracesTimeRangePartiallyInOne() {
439 TmfTimeRange range
= new TmfTimeRange(
440 new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
),
441 new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
));
442 selectTimeRange(range
);
444 /* Range of trace1 should get clamped to its end time */
445 TmfTimeRange expectedRange
= new TmfTimeRange(
446 new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
),
447 new TmfTimestamp(t1end
, SCALE
));
448 assertEquals(expectedRange
, tm
.getCurrentRange());
450 /* Range of trace2 should not have changed */
452 assertEquals(getInitialRange(trace2
), tm
.getCurrentRange());
456 * Test, with two traces in parallel, when we select a time range that is
457 * only partially valid for both traces.
459 * Each trace's time range should get clamped to respectively valid ranges.
462 public void testTwoTracesTimeRangeInBoth() {
466 TmfTimeRange range
= new TmfTimeRange(
467 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
),
468 new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
));
469 selectTimeRange(range
);
471 /* Range of trace1 should be clamped to its end time */
472 TmfTimeRange expectedRange
= new TmfTimeRange(
473 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
),
474 new TmfTimestamp(t1end
, SCALE
));
475 assertEquals(expectedRange
, tm
.getCurrentRange());
477 /* Range of trace2 should be clamped to its start time */
479 expectedRange
= new TmfTimeRange(
480 new TmfTimestamp(t2start
, SCALE
),
481 new TmfTimestamp(t2start
+ ONE_SECOND
, SCALE
));
482 assertEquals(expectedRange
, tm
.getCurrentRange());
486 * Test, with two traces in parallel, when we select a time range that is
487 * not valid for any trace.
489 * Each trace's time range should not be modified.
492 public void testTwoTracesTimeRangeInBetween() {
496 TmfTimeRange range
= new TmfTimeRange(
497 new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
),
498 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
));
499 selectTimeRange(range
);
501 /* Range of trace1 should not have changed */
502 TmfTimeRange expectedRange
= getInitialRange(trace1
);
503 TmfTimeRange curRange
= tm
.getCurrentRange();
504 assertEquals(expectedRange
.getStartTime(), curRange
.getStartTime());
505 assertEquals(expectedRange
.getEndTime(), curRange
.getEndTime());
507 /* Range of trace2 should not have changed */
509 expectedRange
= getInitialRange(trace2
);
510 curRange
= tm
.getCurrentRange();
511 assertEquals(expectedRange
.getStartTime(), curRange
.getStartTime());
512 assertEquals(expectedRange
.getEndTime(), curRange
.getEndTime());
515 // ------------------------------------------------------------------------
516 // Test an experiment
517 // ------------------------------------------------------------------------
520 * Test in an experiment when we select a timestamp that is part of one of
521 * the experiment's traces.
523 * The experiment's current time should be correctly updated.
526 public void testExperimentTimestampInTrace() {
527 TmfExperiment exp
= createExperiment(trace1
, trace2
);
529 TmfTimestamp ts
= new TmfTimestamp(t1start
+ ONE_SECOND
, SCALE
);
532 /* The experiment's current time should be updated. */
533 assertEquals(ts
, tm
.getSelectionBeginTime());
534 assertEquals(ts
, tm
.getSelectionEndTime());
538 * Test in an experiment when we select a timestamp that is between two
539 * traces in the experiment.
541 * The experiment's current time should still be updated, since the
542 * timestamp is valid in the experiment itself.
545 public void testExperimentTimestampInBetween() {
546 TmfExperiment exp
= createExperiment(trace1
, trace2
);
548 TmfTimestamp ts
= new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
);
551 /* The experiment's current time should be updated. */
552 assertEquals(ts
, tm
.getSelectionBeginTime());
553 assertEquals(ts
, tm
.getSelectionEndTime());
557 * Test in an experiment when we select a timestamp that is outside of the
558 * total range of the experiment.
560 * The experiment's current time should not be updated.
563 public void testExperimentTimestampInvalid() {
564 TmfExperiment exp
= createExperiment(trace1
, trace2
);
566 TmfTimestamp ts
= new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
);
569 /* The experiment's current time should NOT be updated. */
570 assertEquals(trace1
.getStartTime(), tm
.getSelectionBeginTime());
571 assertEquals(trace1
.getStartTime(), tm
.getSelectionEndTime());
575 * Test the initial range of an experiment.
578 public void testExperimentInitialRange() {
579 TmfExperiment exp
= createExperiment(trace1
, trace2
);
582 * The initial range should be == to the initial range of the earliest
583 * trace (here trace1).
585 final TmfTimeRange actualRange
= tm
.getCurrentRange();
587 assertEquals(getInitialRange(trace1
), actualRange
);
588 assertEquals(getInitialRange(exp
), actualRange
);
592 * Test the range clamping with the start time of the range outside of the
593 * earliest trace's range. Only that start time should get clamped.
596 public void testExperimentRangeClampingOne() {
597 TmfExperiment exp
= createExperiment(trace1
, trace2
);
600 final TmfTimeRange range
= new TmfTimeRange(
601 new TmfTimestamp(t1start
- ONE_SECOND
, SCALE
),
602 new TmfTimestamp(t1end
- ONE_SECOND
, SCALE
));
603 selectTimeRange(range
);
605 TmfTimeRange actualRange
= tm
.getCurrentRange();
606 assertEquals(t1start
, actualRange
.getStartTime().getValue());
607 assertEquals(t1end
- ONE_SECOND
, actualRange
.getEndTime().getValue());
611 * Test the range clamping when both the start and end times of the signal's
612 * range are outside of the trace's range. The range should clamp to the
613 * experiment's range.
616 public void testExperimentRangeClampingBoth() {
617 TmfExperiment exp
= createExperiment(trace1
, trace2
);
620 final TmfTimeRange range
= new TmfTimeRange(
621 new TmfTimestamp(t1start
- ONE_SECOND
, SCALE
),
622 new TmfTimestamp(t2end
+ ONE_SECOND
, SCALE
));
623 selectTimeRange(range
);
625 TmfTimeRange actualRange
= tm
.getCurrentRange();
626 assertEquals(t1start
, actualRange
.getStartTime().getValue());
627 assertEquals(t2end
, actualRange
.getEndTime().getValue());
631 * Test selecting a range in-between two disjoint traces in an experiment.
632 * The range should still get correctly selected, even if no trace has any
633 * events in that range.
636 public void testExperimentRangeInBetween() {
637 TmfExperiment exp
= createExperiment(trace1
, trace2
);
640 final TmfTimeRange range
= new TmfTimeRange(
641 new TmfTimestamp(t1end
+ ONE_SECOND
, SCALE
),
642 new TmfTimestamp(t2start
- ONE_SECOND
, SCALE
));
643 selectTimeRange(range
);
645 TmfTimeRange actualRange
= tm
.getCurrentRange();
646 assertEquals(range
, actualRange
);
649 // ------------------------------------------------------------------------
651 // ------------------------------------------------------------------------
653 private static TmfExperiment
createExperiment(ITmfTrace t1
, ITmfTrace t2
) {
654 ITmfTrace
[] traces
= new ITmfTrace
[] { t1
, t2
};
655 TmfExperiment exp
= new TmfExperiment(ITmfEvent
.class, "test-exp", traces
);
656 exp
.indexTrace(true);
660 private static TmfTimeRange
getInitialRange(ITmfTrace trace
) {
661 return new TmfTimeRange(
662 trace
.getStartTime(),
663 calculateOffset(trace
.getStartTime(), trace
.getInitialRangeOffset()));
667 * Basically a "initial + offset" operation, but for ITmfTimetamp objects.
669 private static ITmfTimestamp
calculateOffset(ITmfTimestamp initialTs
, ITmfTimestamp offsetTs
) {
670 long start
= initialTs
.normalize(0, SCALE
).getValue();
671 long offset
= offsetTs
.normalize(0, SCALE
).getValue();
672 return new TmfTimestamp(start
+ offset
, SCALE
);