Commit | Line | Data |
---|---|---|
fc526aef | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2013, 2015 Ericsson |
fc526aef AM |
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 | |
0fcf3b09 | 11 | * Patrick Tasse - Support selection range |
fc526aef AM |
12 | *******************************************************************************/ |
13 | ||
9722e5d7 | 14 | package org.eclipse.tracecompass.tmf.ctf.core.tests.temp.tracemanager; |
fc526aef AM |
15 | |
16 | import static org.junit.Assert.assertEquals; | |
17 | import static org.junit.Assert.assertNotNull; | |
fab18d20 | 18 | import static org.junit.Assert.assertSame; |
fc526aef | 19 | |
fab18d20 | 20 | import java.io.File; |
c14c0757 GB |
21 | import java.util.Collection; |
22 | import java.util.Collections; | |
c5648525 | 23 | import java.util.concurrent.TimeUnit; |
fab18d20 | 24 | |
6cfc180e | 25 | import org.eclipse.jdt.annotation.NonNull; |
c4d57ac1 | 26 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; |
2bdf0193 | 27 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
97c71024 | 28 | import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal; |
c4d57ac1 | 29 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; |
2bdf0193 AM |
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; | |
c4d57ac1 | 33 | import org.eclipse.tracecompass.tmf.core.signal.TmfWindowRangeUpdatedSignal; |
2bdf0193 AM |
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; | |
21852dfa | 38 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceContext; |
2bdf0193 | 39 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
5c5fa260 | 40 | import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment; |
c4d57ac1 | 41 | import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils; |
fc526aef | 42 | import org.junit.After; |
3568f816 | 43 | import org.junit.AfterClass; |
fc526aef AM |
44 | import org.junit.Before; |
45 | import org.junit.BeforeClass; | |
c5648525 | 46 | import org.junit.Rule; |
fc526aef | 47 | import org.junit.Test; |
c5648525 AM |
48 | import org.junit.rules.TestRule; |
49 | import org.junit.rules.Timeout; | |
fc526aef | 50 | |
0f8687b4 GB |
51 | import com.google.common.collect.ImmutableSet; |
52 | ||
fc526aef AM |
53 | /** |
54 | * Test suite for the {@link TmfTraceManager}. | |
55 | * | |
56 | * @author Alexandre Montplaisir | |
57 | */ | |
58 | public class TmfTraceManagerTest { | |
59 | ||
c5648525 AM |
60 | /** Time-out tests after 20 seconds */ |
61 | @Rule | |
62 | public TestRule globalTimeout= new Timeout(20, TimeUnit.SECONDS); | |
63 | ||
fc526aef AM |
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() { | |
c4d57ac1 AM |
84 | trace1 = CtfTmfTestTraceUtils.getTrace(CtfTestTrace.TRACE2); |
85 | trace2 = CtfTmfTestTraceUtils.getTrace(CtfTestTrace.KERNEL); | |
fc526aef AM |
86 | |
87 | trace1.indexTrace(true); | |
88 | trace2.indexTrace(true); | |
3568f816 BH |
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); | |
fc526aef AM |
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 | ||
3568f816 BH |
114 | /** |
115 | * Test class clean-up | |
116 | */ | |
117 | @AfterClass | |
118 | public static void tearDownClass() { | |
c4d57ac1 AM |
119 | CtfTmfTestTraceUtils.dispose(CtfTestTrace.TRACE2); |
120 | CtfTmfTestTraceUtils.dispose(CtfTestTrace.KERNEL); | |
3568f816 BH |
121 | } |
122 | ||
fc526aef AM |
123 | // ------------------------------------------------------------------------ |
124 | // Dummy actions (fake signals) | |
125 | // ------------------------------------------------------------------------ | |
126 | ||
127 | private void openTrace(ITmfTrace trace) { | |
c1831960 AM |
128 | if (trace == null) { |
129 | throw new IllegalArgumentException(); | |
130 | } | |
fc526aef AM |
131 | TmfSignalManager.dispatchSignal(new TmfTraceOpenedSignal(this, trace, null)); |
132 | selectTrace(trace); | |
133 | } | |
134 | ||
135 | private void closeTrace(ITmfTrace trace) { | |
c1831960 AM |
136 | if (trace == null) { |
137 | throw new IllegalArgumentException(); | |
138 | } | |
fc526aef | 139 | TmfSignalManager.dispatchSignal(new TmfTraceClosedSignal(this, trace)); |
fab18d20 AM |
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 | } | |
fc526aef AM |
147 | } |
148 | ||
149 | private void selectTrace(ITmfTrace trace) { | |
150 | TmfSignalManager.dispatchSignal(new TmfTraceSelectedSignal(this, trace)); | |
151 | } | |
152 | ||
6cfc180e | 153 | private void selectTimestamp(@NonNull ITmfTimestamp ts) { |
97c71024 | 154 | TmfSignalManager.dispatchSignal(new TmfSelectionRangeUpdatedSignal(this, ts)); |
fc526aef AM |
155 | } |
156 | ||
21852dfa | 157 | private void selectWindowRange(TmfTimeRange tr) { |
97c71024 | 158 | TmfSignalManager.dispatchSignal(new TmfWindowRangeUpdatedSignal(this, tr)); |
fc526aef AM |
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); | |
fab18d20 AM |
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 | ||
c14c0757 GB |
184 | Collection<ITmfTrace> expected = Collections.singleton(trace2); |
185 | Collection<ITmfTrace> actual = tm.getActiveTraceSet(); | |
fab18d20 | 186 | |
c14c0757 GB |
187 | assertEquals(1, actual.size()); |
188 | assertEquals(expected, actual); | |
fab18d20 AM |
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 | ||
c14c0757 GB |
200 | Collection<ITmfTrace> expected = ImmutableSet.of(trace1, trace2); |
201 | Collection<ITmfTrace> actual = tm.getActiveTraceSet(); | |
fab18d20 | 202 | |
c14c0757 GB |
203 | assertEquals(2, actual.size()); |
204 | assertEquals(expected, actual); | |
fab18d20 AM |
205 | } |
206 | ||
0f8687b4 GB |
207 | /** |
208 | * Test the contents of the complete trace set. | |
209 | */ | |
210 | @Test | |
211 | public void testTraceSetWithExperiment() { | |
212 | /* Test with a trace */ | |
c14c0757 GB |
213 | Collection<ITmfTrace> expected = Collections.singleton(trace1); |
214 | Collection<ITmfTrace> actual = TmfTraceManager.getTraceSetWithExperiment(trace1); | |
0f8687b4 GB |
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 | ||
fab18d20 AM |
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(); | |
3ec38c4c | 233 | String basePath = TmfTraceManager.getTemporaryDirPath() + File.separator; |
fab18d20 AM |
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)); | |
fc526aef AM |
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())); | |
21852dfa | 255 | TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef AM |
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 | ||
21852dfa AM |
269 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
270 | assertEquals(ts, selection.getStartTime()); | |
271 | assertEquals(ts, selection.getEndTime()); | |
fc526aef AM |
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); | |
21852dfa | 281 | TmfTimeRange beforeTr = tm.getCurrentTraceContext().getSelectionRange(); |
fc526aef AM |
282 | ITmfTimestamp ts = new TmfTimestamp(t2start - ONE_SECOND, SCALE); |
283 | selectTimestamp(ts); | |
284 | ||
21852dfa AM |
285 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
286 | assertEquals(beforeTr, selection); | |
fc526aef AM |
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); | |
21852dfa | 296 | TmfTimeRange beforeTr = tm.getCurrentTraceContext().getSelectionRange(); |
fc526aef AM |
297 | ITmfTimestamp ts = new TmfTimestamp(t2end + ONE_SECOND, SCALE); |
298 | selectTimestamp(ts); | |
299 | ||
21852dfa AM |
300 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
301 | assertEquals(beforeTr, selection); | |
fc526aef AM |
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)); | |
21852dfa | 313 | selectWindowRange(range); |
fc526aef | 314 | |
21852dfa AM |
315 | TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange(); |
316 | assertEquals(range, curRange); | |
fc526aef AM |
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)); | |
21852dfa | 329 | selectWindowRange(range); |
fc526aef | 330 | |
21852dfa | 331 | TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef AM |
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 | |
21852dfa | 346 | selectWindowRange(range); |
fc526aef | 347 | |
21852dfa | 348 | TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef AM |
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 | |
21852dfa | 364 | selectWindowRange(range); |
fc526aef | 365 | |
21852dfa | 366 | TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef AM |
367 | assertEquals(t2start, curRange.getStartTime().getValue()); |
368 | assertEquals(t2end, curRange.getEndTime().getValue()); | |
369 | } | |
370 | ||
371 | // ------------------------------------------------------------------------ | |
fab18d20 | 372 | // Test multiple, non-overlapping traces in parallel |
fc526aef AM |
373 | // ------------------------------------------------------------------------ |
374 | ||
fab18d20 AM |
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 */ | |
21852dfa AM |
391 | TmfTraceContext ctx = tm.getCurrentTraceContext(); |
392 | assertEquals(ts, ctx.getSelectionRange().getStartTime()); | |
393 | assertEquals(ts, ctx.getSelectionRange().getEndTime()); | |
fab18d20 AM |
394 | |
395 | /* Timestamp of trace2 should not have changed */ | |
396 | selectTrace(trace2); | |
21852dfa AM |
397 | ctx = tm.getCurrentTraceContext(); |
398 | assertEquals(trace2.getStartTime(), ctx.getSelectionRange().getStartTime()); | |
399 | assertEquals(trace2.getStartTime(), ctx.getSelectionRange().getEndTime()); | |
fab18d20 AM |
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 */ | |
21852dfa AM |
418 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
419 | assertEquals(trace1.getStartTime(), selection.getStartTime()); | |
420 | assertEquals(trace1.getStartTime(), selection.getEndTime()); | |
fab18d20 AM |
421 | |
422 | /* Timestamp of trace2 should not have changed */ | |
423 | selectTrace(trace2); | |
21852dfa AM |
424 | selection = tm.getCurrentTraceContext().getSelectionRange(); |
425 | assertEquals(trace2.getStartTime(), selection.getStartTime()); | |
426 | assertEquals(trace2.getStartTime(), selection.getEndTime()); | |
fab18d20 AM |
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 */ | |
21852dfa AM |
444 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
445 | assertEquals(trace1.getStartTime(), selection.getStartTime()); | |
446 | assertEquals(trace1.getStartTime(), selection.getEndTime()); | |
fab18d20 AM |
447 | |
448 | /* Timestamp of trace2 should not have changed */ | |
449 | selectTrace(trace2); | |
21852dfa AM |
450 | selection = tm.getCurrentTraceContext().getSelectionRange(); |
451 | assertEquals(trace2.getStartTime(), selection.getStartTime()); | |
452 | assertEquals(trace2.getStartTime(), selection.getEndTime()); | |
fab18d20 AM |
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)); | |
21852dfa | 468 | selectWindowRange(range); |
fab18d20 AM |
469 | |
470 | /* Range of trace1 should be equal to the requested one */ | |
21852dfa | 471 | assertEquals(range, tm.getCurrentTraceContext().getWindowRange()); |
fab18d20 AM |
472 | |
473 | /* The range of trace 2 should not have changed */ | |
474 | selectTrace(trace2); | |
21852dfa | 475 | assertEquals(getInitialRange(trace2), tm.getCurrentTraceContext().getWindowRange()); |
fab18d20 AM |
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)); | |
21852dfa | 493 | selectWindowRange(range); |
fab18d20 AM |
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)); | |
21852dfa | 499 | assertEquals(expectedRange, tm.getCurrentTraceContext().getWindowRange()); |
fab18d20 AM |
500 | |
501 | /* Range of trace2 should not have changed */ | |
502 | selectTrace(trace2); | |
21852dfa | 503 | assertEquals(getInitialRange(trace2), tm.getCurrentTraceContext().getWindowRange()); |
fab18d20 AM |
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)); | |
21852dfa | 520 | selectWindowRange(range); |
fab18d20 AM |
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)); | |
21852dfa | 526 | assertEquals(expectedRange, tm.getCurrentTraceContext().getWindowRange()); |
fab18d20 AM |
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)); | |
21852dfa | 533 | assertEquals(expectedRange, tm.getCurrentTraceContext().getWindowRange()); |
fab18d20 AM |
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)); | |
21852dfa | 550 | selectWindowRange(range); |
fab18d20 AM |
551 | |
552 | /* Range of trace1 should not have changed */ | |
553 | TmfTimeRange expectedRange = getInitialRange(trace1); | |
21852dfa | 554 | TmfTimeRange curRange = tm.getCurrentTraceContext().getWindowRange(); |
fab18d20 AM |
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); | |
21852dfa | 561 | curRange = tm.getCurrentTraceContext().getWindowRange(); |
fab18d20 AM |
562 | assertEquals(expectedRange.getStartTime(), curRange.getStartTime()); |
563 | assertEquals(expectedRange.getEndTime(), curRange.getEndTime()); | |
564 | } | |
565 | ||
fc526aef AM |
566 | // ------------------------------------------------------------------------ |
567 | // Test an experiment | |
568 | // ------------------------------------------------------------------------ | |
569 | ||
fab18d20 AM |
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. */ | |
21852dfa AM |
584 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
585 | assertEquals(ts, selection.getStartTime()); | |
586 | assertEquals(ts, selection.getEndTime()); | |
fab18d20 AM |
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. */ | |
21852dfa AM |
604 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
605 | assertEquals(ts, selection.getStartTime()); | |
606 | assertEquals(ts, selection.getEndTime()); | |
fab18d20 AM |
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. */ | |
21852dfa AM |
623 | TmfTimeRange selection = tm.getCurrentTraceContext().getSelectionRange(); |
624 | assertEquals(trace1.getStartTime(), selection.getStartTime()); | |
625 | assertEquals(trace1.getStartTime(), selection.getEndTime()); | |
fab18d20 AM |
626 | } |
627 | ||
fc526aef AM |
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 | */ | |
21852dfa | 639 | final TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef | 640 | |
fab18d20 AM |
641 | assertEquals(getInitialRange(trace1), actualRange); |
642 | assertEquals(getInitialRange(exp), actualRange); | |
643 | } | |
fc526aef | 644 | |
fab18d20 AM |
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); | |
fc526aef | 653 | |
fab18d20 AM |
654 | final TmfTimeRange range = new TmfTimeRange( |
655 | new TmfTimestamp(t1start - ONE_SECOND, SCALE), | |
656 | new TmfTimestamp(t1end - ONE_SECOND, SCALE)); | |
21852dfa | 657 | selectWindowRange(range); |
fab18d20 | 658 | |
21852dfa | 659 | TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange(); |
fab18d20 AM |
660 | assertEquals(t1start, actualRange.getStartTime().getValue()); |
661 | assertEquals(t1end - ONE_SECOND, actualRange.getEndTime().getValue()); | |
fc526aef AM |
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)); | |
21852dfa | 677 | selectWindowRange(range); |
fc526aef | 678 | |
21852dfa | 679 | TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef AM |
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)); | |
21852dfa | 697 | selectWindowRange(range); |
fc526aef | 698 | |
21852dfa | 699 | TmfTimeRange actualRange = tm.getCurrentTraceContext().getWindowRange(); |
fc526aef AM |
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 }; | |
4178260e AM |
709 | TmfExperiment exp = new TmfExperiment(ITmfEvent.class, "test-exp", traces, |
710 | TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null); | |
fc526aef | 711 | exp.indexTrace(true); |
3568f816 BH |
712 | // Deregister experiment from signal manager so that it doesn't |
713 | // interfere with the TmfTraceManager tests | |
714 | TmfSignalManager.deregister(exp); | |
fc526aef AM |
715 | return exp; |
716 | } | |
717 | ||
fab18d20 AM |
718 | private static TmfTimeRange getInitialRange(ITmfTrace trace) { |
719 | return new TmfTimeRange( | |
720 | trace.getStartTime(), | |
721 | calculateOffset(trace.getStartTime(), trace.getInitialRangeOffset())); | |
722 | } | |
723 | ||
fc526aef AM |
724 | /** |
725 | * Basically a "initial + offset" operation, but for ITmfTimetamp objects. | |
726 | */ | |
6cfc180e | 727 | private static @NonNull ITmfTimestamp calculateOffset(ITmfTimestamp initialTs, ITmfTimestamp offsetTs) { |
fc526aef AM |
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 | } |