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