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
CommitLineData
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 12package org.eclipse.tracecompass.tmf.ui.tests.views.uml2sd.loader;
73005152 13
8e47a700
BH
14import static org.junit.Assert.assertNotNull;
15
73005152
BH
16import java.io.File;
17import java.io.IOException;
18import java.net.URISyntaxException;
19import java.net.URL;
20import java.util.ArrayList;
21import java.util.List;
22
23import org.eclipse.core.runtime.FileLocator;
24import org.eclipse.core.runtime.Path;
73005152 25import org.eclipse.swt.widgets.Display;
2bdf0193
AM
26import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
27import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
28import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
29import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
30import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
31import org.eclipse.tracecompass.tmf.core.trace.ITmfEventParser;
32import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
5c5fa260 33import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
2bdf0193
AM
34import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
35import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
36import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub;
f0beeb4a 37import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
2bdf0193
AM
38import org.eclipse.tracecompass.tmf.ui.tests.uml2sd.trace.TmfUml2SDTestTrace;
39import org.eclipse.tracecompass.tmf.ui.views.uml2sd.SDView;
40import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.Criteria;
41import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.FilterCriteria;
42import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.FilterListDialog;
43import org.eclipse.tracecompass.tmf.ui.views.uml2sd.load.LoadersManager;
44import org.eclipse.tracecompass.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader;
73005152
BH
45import org.eclipse.ui.IViewPart;
46import org.eclipse.ui.PartInitException;
47import org.eclipse.ui.PlatformUI;
9269df72 48import 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 */
56public 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() {
f0beeb4a 241 WaitUtils.waitForJobs();
73005152
BH
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 */
6256d8ad 275 public TmfExperiment getExperiment() {
73005152
BH
276 return fExperiment;
277 }
20658947 278
73005152
BH
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 }
20658947 296
73005152
BH
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 }
20658947 314
73005152
BH
315 /**
316 * @param page number to set
317 */
20658947 318 public void setPage(final int page) {
abbdd66a 319 fLoader.pageNumberChanged(page);
73005152
BH
320 fLoader.waitForCompletion();
321 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
322 }
20658947 323
73005152 324 /**
2bdf0193 325 * @see org.eclipse.tracecompass.tmf.ui.tests.views.uml2sd.loader.Uml2SDTestFacility#selectExperiment(boolean)
73005152
BH
326 */
327 public void selectExperiment() {
328 this.selectExperiment(true);
329 }
20658947 330
73005152 331 /**
20658947 332 * Selects the experiment.
73005152
BH
333 * @param wait true to wait for indexing to finish else false
334 */
20658947 335 public void selectExperiment(final boolean wait) {
7e6347b0 336 fParser = new TmfUml2SDTestTrace();
73005152 337 fTrace = setupTrace(fParser);
7e6347b0
FC
338 fParser.setTrace(fTrace);
339
20658947 340 final ITmfTrace traces[] = new ITmfTrace[1];
73005152 341 traces[0] = fTrace;
4178260e
AM
342 fExperiment = new TmfExperiment(ITmfEvent.class, "TestExperiment",
343 traces, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null) {
8143f5a1
BH
344 @Override
345 protected ITmfTraceIndexer createIndexer(int interval) {
346 return new TmfCheckpointIndexer(this, interval);
347 }
348 };
3fcf269e 349 fTrace.broadcast(new TmfTraceOpenedSignal(this, fExperiment, null));
faa38350 350 fTrace.broadcast(new TmfTraceSelectedSignal(this, fExperiment));
73005152 351 if (wait) {
2717e7ec
PT
352 while (fExperiment.getNbEvents() == 0) {
353 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
354 }
73005152
BH
355 waitForJobs();
356 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
357 }
358 }
20658947 359
73005152
BH
360 /**
361 * Disposes the experiment.
362 */
363 public void disposeExperiment() {
62f62b14
AM
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();
73005152
BH
371 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
372 }
20658947 373
73005152 374 /**
20658947 375 * Creates some global filter criteria and saves them to disk.
73005152
BH
376 */
377 public void createFilterCriteria() {
378 // Create Filter Criteria and save tme
ab410d88 379 final List<FilterCriteria> filterToSave = new ArrayList<>();
73005152
BH
380 Criteria criteria = new Criteria();
381 criteria.setLifeLineSelected(true);
382 criteria.setExpression(IUml2SDTestConstants.FIRST_PLAYER_NAME);
383 filterToSave.add(new FilterCriteria(criteria, true, false));
20658947 384
73005152
BH
385 criteria = new Criteria();
386 criteria.setSyncMessageSelected(true);
cad06250 387 criteria.setExpression("BALL_.*");
73005152
BH
388 filterToSave.add(new FilterCriteria(criteria, true, false));
389 FilterListDialog.saveFiltersCriteria(filterToSave);
390 }
391
392
393}
This page took 0.106879 seconds and 5 git commands to generate.