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