Commit | Line | Data |
---|---|---|
73005152 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2011, 2014 Ericsson |
64636df8 | 3 | * |
73005152 BH |
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 | |
64636df8 | 8 | * |
73005152 BH |
9 | * Contributors: |
10 | * Bernd Hufmann - Initial API and implementation | |
11 | *******************************************************************************/ | |
2bdf0193 | 12 | package org.eclipse.tracecompass.tmf.ui.tests.views.uml2sd.loader; |
73005152 | 13 | |
8e47a700 BH |
14 | import static org.junit.Assert.assertNotNull; |
15 | ||
73005152 BH |
16 | import java.io.File; |
17 | import java.io.IOException; | |
18 | import java.net.URISyntaxException; | |
19 | import java.net.URL; | |
20 | import java.util.ArrayList; | |
21 | import java.util.List; | |
22 | ||
23 | import org.eclipse.core.runtime.FileLocator; | |
24 | import org.eclipse.core.runtime.Path; | |
25 | import org.eclipse.core.runtime.jobs.Job; | |
73005152 | 26 | import org.eclipse.swt.widgets.Display; |
2bdf0193 AM |
27 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
28 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; | |
29 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal; | |
30 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; | |
31 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal; | |
32 | import org.eclipse.tracecompass.tmf.core.trace.ITmfEventParser; | |
33 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
5c5fa260 | 34 | import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment; |
2bdf0193 AM |
35 | import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer; |
36 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer; | |
37 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub; | |
38 | import org.eclipse.tracecompass.tmf.ui.tests.uml2sd.trace.TmfUml2SDTestTrace; | |
39 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.SDView; | |
40 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.Criteria; | |
41 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.FilterCriteria; | |
42 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.FilterListDialog; | |
43 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.load.LoadersManager; | |
44 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader; | |
73005152 BH |
45 | import org.eclipse.ui.IViewPart; |
46 | import org.eclipse.ui.PartInitException; | |
47 | import org.eclipse.ui.PlatformUI; | |
9269df72 | 48 | import org.osgi.framework.FrameworkUtil; |
73005152 BH |
49 | |
50 | /** | |
51 | * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides | |
20658947 | 52 | * utility methods for interacting with the loader/view. |
64636df8 BH |
53 | * |
54 | * @author Bernd Hufmann | |
73005152 BH |
55 | */ |
56 | public class Uml2SDTestFacility { | |
20658947 | 57 | |
8e47a700 BH |
58 | // ------------------------------------------------------------------------ |
59 | // Constants | |
60 | // ------------------------------------------------------------------------ | |
61 | private static final String SD_VIEW_ID = "org.eclipse.linuxtools.tmf.ui.tmfUml2SDSyncView"; | |
62 | ||
73005152 BH |
63 | // ------------------------------------------------------------------------ |
64 | // Attributes | |
65 | // ------------------------------------------------------------------------ | |
66 | private static Uml2SDTestFacility fInstance = null; | |
20658947 | 67 | |
73005152 BH |
68 | private TmfUml2SDSyncLoader fLoader; |
69 | private SDView fSdView; | |
70 | private TmfTraceStub fTrace = null; | |
71 | private TmfUml2SDTestTrace fParser = null; | |
6256d8ad | 72 | private TmfExperiment fExperiment = null; |
20658947 | 73 | |
62f62b14 | 74 | private volatile boolean fIsInitialized = false; |
20658947 | 75 | |
73005152 BH |
76 | // ------------------------------------------------------------------------ |
77 | // Constructors | |
78 | // ------------------------------------------------------------------------ | |
79 | private Uml2SDTestFacility() { | |
8e47a700 BH |
80 | fParser = new TmfUml2SDTestTrace(); |
81 | fTrace = setupTrace(fParser); | |
82 | fParser.setTrace(fTrace); | |
73005152 BH |
83 | } |
84 | ||
85 | // ------------------------------------------------------------------------ | |
86 | // Operations | |
87 | // ------------------------------------------------------------------------ | |
64636df8 BH |
88 | /** |
89 | * @return the singleton instance. | |
90 | */ | |
1f2f091b | 91 | public synchronized static Uml2SDTestFacility getInstance() { |
73005152 BH |
92 | if (fInstance == null) { |
93 | fInstance = new Uml2SDTestFacility(); | |
94 | } | |
95 | return fInstance; | |
96 | } | |
97 | ||
98 | /** | |
99 | * Initial the test facility. | |
73005152 | 100 | */ |
4f5d9f9b | 101 | public void init() { |
20658947 | 102 | |
73005152 | 103 | if (!fIsInitialized) { |
73005152 BH |
104 | IViewPart view; |
105 | try { | |
1e412478 | 106 | // Remove welcome view to avoid interference during test execution |
8e47a700 BH |
107 | hideView("org.eclipse.ui.internal.introview"); |
108 | view = showView(SD_VIEW_ID); | |
20658947 | 109 | } catch (final PartInitException e) { |
73005152 BH |
110 | throw new RuntimeException(e); |
111 | } | |
112 | ||
113 | fSdView = (SDView) view; | |
e57a3f06 AM |
114 | fLoader = (TmfUml2SDSyncLoader) LoadersManager.getInstance().createLoader( |
115 | "org.eclipse.tracecompass.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader", fSdView); | |
73005152 | 116 | |
73005152 BH |
117 | fIsInitialized = true; |
118 | } | |
119 | } | |
120 | ||
6256d8ad | 121 | private TmfTraceStub setupTrace(final ITmfEventParser parser) { |
20658947 FC |
122 | |
123 | try { | |
124 | // Create test trace object | |
cad06250 | 125 | final URL location = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), new Path("tracesets/sdEvents"), null); |
20658947 | 126 | final File test = new File(FileLocator.toFileURL(location).toURI()); |
ab186fbb | 127 | return new TmfTraceStub(test.getPath(), 500, true, parser); |
b4f71e4a FC |
128 | } catch (final TmfTraceException e) { |
129 | e.printStackTrace(); | |
130 | throw new RuntimeException(e); | |
20658947 FC |
131 | } catch (final URISyntaxException e) { |
132 | e.printStackTrace(); | |
133 | throw new RuntimeException(e); | |
134 | } catch (final IOException e) { | |
135 | e.printStackTrace(); | |
136 | throw new RuntimeException(e); | |
137 | } | |
73005152 | 138 | } |
20658947 | 139 | |
73005152 BH |
140 | /** |
141 | * Dispose the resource | |
142 | */ | |
143 | public void dispose() { | |
4f5d9f9b | 144 | if (fIsInitialized) { |
73005152 BH |
145 | // Wait for all Eclipse jobs to finish |
146 | waitForJobs(); | |
8e47a700 BH |
147 | hideView(SD_VIEW_ID); |
148 | delay(200); | |
73005152 BH |
149 | fIsInitialized = false; |
150 | } | |
151 | } | |
20658947 | 152 | |
73005152 | 153 | /** |
20658947 | 154 | * Sleeps current thread or GUI thread for a given time. |
64636df8 | 155 | * @param waitTimeMillis time in milliseconds to wait |
73005152 | 156 | */ |
20658947 FC |
157 | public void delay(final long waitTimeMillis) { |
158 | final Display display = Display.getCurrent(); | |
73005152 | 159 | if (display != null) { |
20658947 | 160 | final long endTimeMillis = System.currentTimeMillis() + waitTimeMillis; |
73005152 BH |
161 | while(System.currentTimeMillis() < endTimeMillis) { |
162 | if (!display.readAndDispatch()) { | |
99c3a878 MAL |
163 | // We do not use Display.sleep because it might never wake up |
164 | // if there is no user interaction | |
165 | try { | |
166 | Thread.sleep(Math.min(waitTimeMillis, 10)); | |
167 | } catch (final InterruptedException e) { | |
168 | // Ignored | |
b07022a9 | 169 | } |
73005152 BH |
170 | } |
171 | display.update(); | |
172 | } | |
173 | } else { | |
174 | try { | |
175 | Thread.sleep(waitTimeMillis); | |
20658947 | 176 | } catch (final InterruptedException e) { |
73005152 BH |
177 | // Ignored |
178 | } | |
179 | } | |
180 | } | |
181 | ||
8e47a700 BH |
182 | /** |
183 | * Waits for a view to be closed | |
184 | * | |
185 | * @param viewId | |
186 | * a view id | |
187 | */ | |
188 | public void waitForViewClosed(String viewId) { | |
189 | for (int i = 1; i < 5000 && (getViewPart(viewId) != null); i *= 2) { | |
190 | delay(i); | |
191 | } | |
192 | } | |
193 | ||
194 | /** | |
195 | * Waits for a view to be opened | |
196 | * | |
197 | * @param viewId | |
198 | * a view id | |
199 | */ | |
200 | public void waitForViewOpened(String viewId) { | |
201 | for (int i = 1; i < 5000 && (getViewPart(viewId) == null); i *= 2) { | |
202 | delay(i); | |
203 | } | |
204 | } | |
205 | ||
206 | private IViewPart showView(String viewId) throws PartInitException { | |
207 | IViewPart view = getViewPart(viewId); | |
208 | ||
209 | if (view == null) { | |
210 | view = PlatformUI.getWorkbench() | |
211 | .getActiveWorkbenchWindow() | |
212 | .getActivePage().showView(viewId); | |
213 | ||
214 | waitForViewOpened(viewId); | |
215 | } | |
216 | assertNotNull(view); | |
217 | return view; | |
218 | } | |
219 | ||
220 | private void hideView(String viewId) { | |
221 | IViewPart view = getViewPart(viewId); | |
222 | if (view != null) { | |
223 | PlatformUI.getWorkbench() | |
224 | .getActiveWorkbenchWindow() | |
225 | .getActivePage().hideView(view); | |
226 | } | |
227 | waitForViewClosed(viewId); | |
228 | } | |
229 | ||
230 | private static IViewPart getViewPart(String viewId) { | |
231 | return PlatformUI.getWorkbench() | |
232 | .getActiveWorkbenchWindow() | |
233 | .getActivePage() | |
234 | .findView(viewId); | |
235 | } | |
236 | ||
73005152 BH |
237 | /** |
238 | * Waits for all Eclipse jobs to finish | |
239 | */ | |
240 | public void waitForJobs() { | |
241 | while (!Job.getJobManager().isIdle()) { | |
242 | delay(IUml2SDTestConstants.WAIT_FOR_JOBS_DELAY); | |
243 | } | |
244 | } | |
245 | ||
246 | /** | |
247 | * @return current UML2SD loader | |
248 | */ | |
249 | public TmfUml2SDSyncLoader getLoader() { | |
250 | return fLoader; | |
251 | } | |
252 | ||
253 | /** | |
254 | * @return current SD view | |
255 | */ | |
256 | public SDView getSdView() { | |
257 | return fSdView; | |
258 | } | |
259 | ||
260 | /** | |
261 | * @return current trace | |
262 | */ | |
263 | public TmfTraceStub getTrace() { | |
264 | return fTrace; | |
265 | } | |
266 | ||
267 | /** | |
268 | * @return Trace parser | |
269 | */ | |
270 | public TmfUml2SDTestTrace getParser() { | |
271 | return fParser; | |
272 | } | |
273 | ||
274 | /** | |
275 | * @return current experiment. | |
276 | */ | |
6256d8ad | 277 | public TmfExperiment getExperiment() { |
73005152 BH |
278 | return fExperiment; |
279 | } | |
20658947 | 280 | |
73005152 BH |
281 | /** |
282 | * Go to next page; | |
283 | */ | |
284 | public void nextPage() { | |
285 | fLoader.nextPage(); | |
286 | fLoader.waitForCompletion(); | |
287 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); | |
288 | } | |
289 | ||
290 | /** | |
291 | * Go to previous page. | |
292 | */ | |
293 | public void prevPage() { | |
294 | fLoader.prevPage(); | |
295 | fLoader.waitForCompletion(); | |
296 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); | |
297 | } | |
20658947 | 298 | |
73005152 BH |
299 | /** |
300 | * Go to last page. | |
301 | */ | |
302 | public void lastPage() { | |
303 | fLoader.lastPage(); | |
304 | fLoader.waitForCompletion(); | |
305 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); | |
306 | } | |
307 | ||
308 | /** | |
309 | * Go to first page. | |
310 | */ | |
311 | public void firstPage() { | |
312 | fLoader.firstPage(); | |
313 | fLoader.waitForCompletion(); | |
314 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); | |
315 | } | |
20658947 | 316 | |
73005152 BH |
317 | /** |
318 | * @param page number to set | |
319 | */ | |
20658947 | 320 | public void setPage(final int page) { |
abbdd66a | 321 | fLoader.pageNumberChanged(page); |
73005152 BH |
322 | fLoader.waitForCompletion(); |
323 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); | |
324 | } | |
20658947 | 325 | |
73005152 | 326 | /** |
2bdf0193 | 327 | * @see org.eclipse.tracecompass.tmf.ui.tests.views.uml2sd.loader.Uml2SDTestFacility#selectExperiment(boolean) |
73005152 BH |
328 | */ |
329 | public void selectExperiment() { | |
330 | this.selectExperiment(true); | |
331 | } | |
20658947 | 332 | |
73005152 | 333 | /** |
20658947 | 334 | * Selects the experiment. |
73005152 BH |
335 | * @param wait true to wait for indexing to finish else false |
336 | */ | |
20658947 | 337 | public void selectExperiment(final boolean wait) { |
7e6347b0 | 338 | fParser = new TmfUml2SDTestTrace(); |
73005152 | 339 | fTrace = setupTrace(fParser); |
7e6347b0 FC |
340 | fParser.setTrace(fTrace); |
341 | ||
20658947 | 342 | final ITmfTrace traces[] = new ITmfTrace[1]; |
73005152 | 343 | traces[0] = fTrace; |
4178260e AM |
344 | fExperiment = new TmfExperiment(ITmfEvent.class, "TestExperiment", |
345 | traces, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null) { | |
8143f5a1 BH |
346 | @Override |
347 | protected ITmfTraceIndexer createIndexer(int interval) { | |
348 | return new TmfCheckpointIndexer(this, interval); | |
349 | } | |
350 | }; | |
3fcf269e | 351 | fTrace.broadcast(new TmfTraceOpenedSignal(this, fExperiment, null)); |
faa38350 | 352 | fTrace.broadcast(new TmfTraceSelectedSignal(this, fExperiment)); |
73005152 | 353 | if (wait) { |
2717e7ec PT |
354 | while (fExperiment.getNbEvents() == 0) { |
355 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); | |
356 | } | |
73005152 BH |
357 | waitForJobs(); |
358 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); | |
359 | } | |
360 | } | |
20658947 | 361 | |
73005152 BH |
362 | /** |
363 | * Disposes the experiment. | |
364 | */ | |
365 | public void disposeExperiment() { | |
62f62b14 AM |
366 | ITmfTrace trace = fTrace; |
367 | TmfExperiment experiment = fExperiment; | |
368 | if (trace == null || experiment == null) { | |
369 | throw new IllegalStateException(); | |
370 | } | |
371 | trace.broadcast(new TmfTraceClosedSignal(this, experiment)); | |
372 | experiment.dispose(); | |
73005152 BH |
373 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); |
374 | } | |
20658947 | 375 | |
73005152 | 376 | /** |
20658947 | 377 | * Creates some global filter criteria and saves them to disk. |
73005152 BH |
378 | */ |
379 | public void createFilterCriteria() { | |
380 | // Create Filter Criteria and save tme | |
ab410d88 | 381 | final List<FilterCriteria> filterToSave = new ArrayList<>(); |
73005152 BH |
382 | Criteria criteria = new Criteria(); |
383 | criteria.setLifeLineSelected(true); | |
384 | criteria.setExpression(IUml2SDTestConstants.FIRST_PLAYER_NAME); | |
385 | filterToSave.add(new FilterCriteria(criteria, true, false)); | |
20658947 | 386 | |
73005152 BH |
387 | criteria = new Criteria(); |
388 | criteria.setSyncMessageSelected(true); | |
cad06250 | 389 | criteria.setExpression("BALL_.*"); |
73005152 BH |
390 | filterToSave.add(new FilterCriteria(criteria, true, false)); |
391 | FilterListDialog.saveFiltersCriteria(filterToSave); | |
392 | } | |
393 | ||
394 | ||
395 | } |