tmf: Add waitUntil / condition to tmf.ui.tests
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.tests / src / org / eclipse / tracecompass / tmf / ui / tests / views / uml2sd / loader / Uml2SDTestFacility.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson
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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.tracecompass.tmf.ui.tests.views.uml2sd.loader;
13
14 import static org.junit.Assert.assertNotNull;
15
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.swt.widgets.Display;
26 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
27 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
28 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
29 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
30 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
31 import org.eclipse.tracecompass.tmf.core.trace.ITmfEventParser;
32 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
33 import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
34 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
35 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
36 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub;
37 import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
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;
45 import org.eclipse.ui.IViewPart;
46 import org.eclipse.ui.PartInitException;
47 import org.eclipse.ui.PlatformUI;
48 import org.osgi.framework.FrameworkUtil;
49
50 /**
51 * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides
52 * utility methods for interacting with the loader/view.
53 *
54 * @author Bernd Hufmann
55 */
56 public class Uml2SDTestFacility {
57
58 // ------------------------------------------------------------------------
59 // Constants
60 // ------------------------------------------------------------------------
61 private static final String SD_VIEW_ID = "org.eclipse.linuxtools.tmf.ui.tmfUml2SDSyncView";
62
63 // ------------------------------------------------------------------------
64 // Attributes
65 // ------------------------------------------------------------------------
66 private static Uml2SDTestFacility fInstance = null;
67
68 private TmfUml2SDSyncLoader fLoader;
69 private SDView fSdView;
70 private TmfTraceStub fTrace = null;
71 private TmfUml2SDTestTrace fParser = null;
72 private TmfExperiment fExperiment = null;
73
74 private volatile boolean fIsInitialized = false;
75
76 // ------------------------------------------------------------------------
77 // Constructors
78 // ------------------------------------------------------------------------
79 private Uml2SDTestFacility() {
80 fParser = new TmfUml2SDTestTrace();
81 fTrace = setupTrace(fParser);
82 fParser.setTrace(fTrace);
83 }
84
85 // ------------------------------------------------------------------------
86 // Operations
87 // ------------------------------------------------------------------------
88 /**
89 * @return the singleton instance.
90 */
91 public synchronized static Uml2SDTestFacility getInstance() {
92 if (fInstance == null) {
93 fInstance = new Uml2SDTestFacility();
94 }
95 return fInstance;
96 }
97
98 /**
99 * Initial the test facility.
100 */
101 public void init() {
102
103 if (!fIsInitialized) {
104 IViewPart view;
105 try {
106 // Remove welcome view to avoid interference during test execution
107 hideView("org.eclipse.ui.internal.introview");
108 view = showView(SD_VIEW_ID);
109 } catch (final PartInitException e) {
110 throw new RuntimeException(e);
111 }
112
113 fSdView = (SDView) view;
114 fLoader = (TmfUml2SDSyncLoader) LoadersManager.getInstance().createLoader(
115 "org.eclipse.tracecompass.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader", fSdView);
116
117 fIsInitialized = true;
118 }
119 }
120
121 private TmfTraceStub setupTrace(final ITmfEventParser parser) {
122
123 try {
124 // Create test trace object
125 final URL location = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), new Path("tracesets/sdEvents"), null);
126 final File test = new File(FileLocator.toFileURL(location).toURI());
127 return new TmfTraceStub(test.getPath(), 500, true, parser);
128 } catch (final TmfTraceException e) {
129 e.printStackTrace();
130 throw new RuntimeException(e);
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 }
138 }
139
140 /**
141 * Dispose the resource
142 */
143 public void dispose() {
144 if (fIsInitialized) {
145 // Wait for all Eclipse jobs to finish
146 waitForJobs();
147 hideView(SD_VIEW_ID);
148 delay(200);
149 fIsInitialized = false;
150 }
151 }
152
153 /**
154 * Sleeps current thread or GUI thread for a given time.
155 * @param waitTimeMillis time in milliseconds to wait
156 */
157 public void delay(final long waitTimeMillis) {
158 final Display display = Display.getCurrent();
159 if (display != null) {
160 final long endTimeMillis = System.currentTimeMillis() + waitTimeMillis;
161 while(System.currentTimeMillis() < endTimeMillis) {
162 if (!display.readAndDispatch()) {
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
169 }
170 }
171 display.update();
172 }
173 } else {
174 try {
175 Thread.sleep(waitTimeMillis);
176 } catch (final InterruptedException e) {
177 // Ignored
178 }
179 }
180 }
181
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
237 /**
238 * Waits for all Eclipse jobs to finish
239 */
240 public void waitForJobs() {
241 WaitUtils.waitForJobs();
242 }
243
244 /**
245 * @return current UML2SD loader
246 */
247 public TmfUml2SDSyncLoader getLoader() {
248 return fLoader;
249 }
250
251 /**
252 * @return current SD view
253 */
254 public SDView getSdView() {
255 return fSdView;
256 }
257
258 /**
259 * @return current trace
260 */
261 public TmfTraceStub getTrace() {
262 return fTrace;
263 }
264
265 /**
266 * @return Trace parser
267 */
268 public TmfUml2SDTestTrace getParser() {
269 return fParser;
270 }
271
272 /**
273 * @return current experiment.
274 */
275 public TmfExperiment getExperiment() {
276 return fExperiment;
277 }
278
279 /**
280 * Go to next page;
281 */
282 public void nextPage() {
283 fLoader.nextPage();
284 fLoader.waitForCompletion();
285 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
286 }
287
288 /**
289 * Go to previous page.
290 */
291 public void prevPage() {
292 fLoader.prevPage();
293 fLoader.waitForCompletion();
294 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
295 }
296
297 /**
298 * Go to last page.
299 */
300 public void lastPage() {
301 fLoader.lastPage();
302 fLoader.waitForCompletion();
303 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
304 }
305
306 /**
307 * Go to first page.
308 */
309 public void firstPage() {
310 fLoader.firstPage();
311 fLoader.waitForCompletion();
312 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
313 }
314
315 /**
316 * @param page number to set
317 */
318 public void setPage(final int page) {
319 fLoader.pageNumberChanged(page);
320 fLoader.waitForCompletion();
321 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
322 }
323
324 /**
325 * @see org.eclipse.tracecompass.tmf.ui.tests.views.uml2sd.loader.Uml2SDTestFacility#selectExperiment(boolean)
326 */
327 public void selectExperiment() {
328 this.selectExperiment(true);
329 }
330
331 /**
332 * Selects the experiment.
333 * @param wait true to wait for indexing to finish else false
334 */
335 public void selectExperiment(final boolean wait) {
336 fParser = new TmfUml2SDTestTrace();
337 fTrace = setupTrace(fParser);
338 fParser.setTrace(fTrace);
339
340 final ITmfTrace traces[] = new ITmfTrace[1];
341 traces[0] = fTrace;
342 fExperiment = new TmfExperiment(ITmfEvent.class, "TestExperiment",
343 traces, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null) {
344 @Override
345 protected ITmfTraceIndexer createIndexer(int interval) {
346 return new TmfCheckpointIndexer(this, interval);
347 }
348 };
349 fTrace.broadcast(new TmfTraceOpenedSignal(this, fExperiment, null));
350 fTrace.broadcast(new TmfTraceSelectedSignal(this, fExperiment));
351 if (wait) {
352 while (fExperiment.getNbEvents() == 0) {
353 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
354 }
355 waitForJobs();
356 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
357 }
358 }
359
360 /**
361 * Disposes the experiment.
362 */
363 public void disposeExperiment() {
364 ITmfTrace trace = fTrace;
365 TmfExperiment experiment = fExperiment;
366 if (trace == null || experiment == null) {
367 throw new IllegalStateException();
368 }
369 trace.broadcast(new TmfTraceClosedSignal(this, experiment));
370 experiment.dispose();
371 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
372 }
373
374 /**
375 * Creates some global filter criteria and saves them to disk.
376 */
377 public void createFilterCriteria() {
378 // Create Filter Criteria and save tme
379 final List<FilterCriteria> filterToSave = new ArrayList<>();
380 Criteria criteria = new Criteria();
381 criteria.setLifeLineSelected(true);
382 criteria.setExpression(IUml2SDTestConstants.FIRST_PLAYER_NAME);
383 filterToSave.add(new FilterCriteria(criteria, true, false));
384
385 criteria = new Criteria();
386 criteria.setSyncMessageSelected(true);
387 criteria.setExpression("BALL_.*");
388 filterToSave.add(new FilterCriteria(criteria, true, false));
389 FilterListDialog.saveFiltersCriteria(filterToSave);
390 }
391
392
393 }
This page took 0.040842 seconds and 5 git commands to generate.