tmf: Do not use Display.sleep in tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.tests / src / org / eclipse / linuxtools / tmf / ui / tests / views / uml2sd / loader / Uml2SDTestFacility.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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.linuxtools.tmf.ui.tests.views.uml2sd.loader;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import java.net.URL;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.eclipse.core.runtime.FileLocator;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.jobs.Job;
24 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
25 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
26 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceClosedSignal;
27 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
28 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceSelectedSignal;
29 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
30 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
31 import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
32 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
33 import org.eclipse.linuxtools.tmf.ui.tests.uml2sd.trace.TmfUml2SDTestTrace;
34 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView;
35 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.Criteria;
36 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.FilterCriteria;
37 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.FilterListDialog;
38 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.load.LoadersManager;
39 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader;
40 import org.eclipse.swt.widgets.Display;
41 import org.eclipse.ui.IViewPart;
42 import org.eclipse.ui.PartInitException;
43 import org.eclipse.ui.PlatformUI;
44 import org.osgi.framework.FrameworkUtil;
45
46 /**
47 * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides
48 * utility methods for interacting with the loader/view.
49 *
50 * @author Bernd Hufmann
51 */
52 public class Uml2SDTestFacility {
53
54 // ------------------------------------------------------------------------
55 // Attributes
56 // ------------------------------------------------------------------------
57 private static Uml2SDTestFacility fInstance = null;
58
59 private TmfUml2SDSyncLoader fLoader;
60 private SDView fSdView;
61 private TmfTraceStub fTrace = null;
62 private TmfUml2SDTestTrace fParser = null;
63 private TmfExperiment fExperiment = null;
64
65 private volatile boolean fIsInitialized = false;
66
67 // ------------------------------------------------------------------------
68 // Constructors
69 // ------------------------------------------------------------------------
70 private Uml2SDTestFacility() {
71 }
72
73 // ------------------------------------------------------------------------
74 // Operations
75 // ------------------------------------------------------------------------
76 /**
77 * @return the singleton instance.
78 */
79 public synchronized static Uml2SDTestFacility getInstance() {
80 if (fInstance == null) {
81 fInstance = new Uml2SDTestFacility();
82 fInstance.init();
83 }
84 return fInstance;
85 }
86
87 /**
88 * Initial the test facility.
89 */
90 public void init() {
91
92 if (!fIsInitialized) {
93
94 fParser = new TmfUml2SDTestTrace();
95 fTrace = setupTrace(fParser);
96 fParser.setTrace(fTrace);
97
98 IViewPart view;
99 try {
100 // Remove welcome view to avoid interference during test execution
101 view = PlatformUI.getWorkbench()
102 .getActiveWorkbenchWindow()
103 .getActivePage()
104 .findView("org.eclipse.ui.internal.introview");
105
106 if (view != null) {
107 PlatformUI.getWorkbench()
108 .getActiveWorkbenchWindow()
109 .getActivePage().hideView(view);
110 }
111
112 view = PlatformUI.getWorkbench()
113 .getActiveWorkbenchWindow()
114 .getActivePage()
115 .showView("org.eclipse.linuxtools.tmf.ui.tmfUml2SDSyncView");
116
117 } catch (final PartInitException e) {
118 throw new RuntimeException(e);
119 }
120
121 fSdView = (SDView) view;
122 fLoader = (TmfUml2SDSyncLoader)LoadersManager.getInstance().createLoader(
123 "org.eclipse.linuxtools.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader", fSdView);
124
125 delay(3000);
126 fIsInitialized = true;
127 }
128 }
129
130
131 private TmfTraceStub setupTrace(final ITmfEventParser parser) {
132
133 try {
134 // Create test trace object
135 final URL location = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), new Path("tracesets/sdEvents"), null);
136 final File test = new File(FileLocator.toFileURL(location).toURI());
137 return new TmfTraceStub(test.getPath(), 500, true, parser);
138 } catch (final TmfTraceException e) {
139 e.printStackTrace();
140 throw new RuntimeException(e);
141 } catch (final URISyntaxException e) {
142 e.printStackTrace();
143 throw new RuntimeException(e);
144 } catch (final IOException e) {
145 e.printStackTrace();
146 throw new RuntimeException(e);
147 }
148 }
149
150 /**
151 * Dispose the resource
152 */
153 public void dispose() {
154 if (fIsInitialized) {
155 ITmfTrace trace = fTrace;
156 TmfExperiment experiment = fExperiment;
157 if (trace == null || experiment == null) {
158 throw new IllegalStateException();
159 }
160
161 trace.broadcast(new TmfTraceClosedSignal(this, experiment));
162 experiment.dispose();
163
164 // Wait for all Eclipse jobs to finish
165 waitForJobs();
166
167 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fSdView);
168 fIsInitialized = false;
169 }
170 }
171
172 /**
173 * Sleeps current thread or GUI thread for a given time.
174 * @param waitTimeMillis time in milliseconds to wait
175 */
176 public void delay(final long waitTimeMillis) {
177 final Display display = Display.getCurrent();
178 if (display != null) {
179 final long endTimeMillis = System.currentTimeMillis() + waitTimeMillis;
180 while(System.currentTimeMillis() < endTimeMillis) {
181 if (!display.readAndDispatch()) {
182 // We do not use Display.sleep because it might never wake up
183 // if there is no user interaction
184 try {
185 Thread.sleep(Math.min(waitTimeMillis, 10));
186 } catch (final InterruptedException e) {
187 // Ignored
188 }
189 }
190 display.update();
191 }
192 } else {
193 try {
194 Thread.sleep(waitTimeMillis);
195 } catch (final InterruptedException e) {
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 */
241 public TmfExperiment getExperiment() {
242 return fExperiment;
243 }
244
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 }
262
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 }
280
281 /**
282 * @param page number to set
283 */
284 public void setPage(final int page) {
285 fLoader.pageNumberChanged(page);
286 fLoader.waitForCompletion();
287 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
288 }
289
290 /**
291 * @see org.eclipse.linuxtools.tmf.ui.tests.views.uml2sd.loader.Uml2SDTestFacility#selectExperiment(boolean)
292 */
293 public void selectExperiment() {
294 this.selectExperiment(true);
295 }
296
297 /**
298 * Selects the experiment.
299 * @param wait true to wait for indexing to finish else false
300 */
301 public void selectExperiment(final boolean wait) {
302 fParser = new TmfUml2SDTestTrace();
303 fTrace = setupTrace(fParser);
304 fParser.setTrace(fTrace);
305
306 // fTrace = setupTrace(fParser);
307
308 final ITmfTrace traces[] = new ITmfTrace[1];
309 traces[0] = fTrace;
310 fExperiment = new TmfExperiment(ITmfEvent.class, "TestExperiment", traces);
311 fTrace.broadcast(new TmfTraceOpenedSignal(this, fExperiment, null));
312 fTrace.broadcast(new TmfTraceSelectedSignal(this, fExperiment));
313 if (wait) {
314 while (fExperiment.getNbEvents() == 0) {
315 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
316 }
317 waitForJobs();
318 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
319 }
320 }
321
322 /**
323 * Disposes the experiment.
324 */
325 public void disposeExperiment() {
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();
333 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
334 }
335
336 /**
337 * Creates some global filter criteria and saves them to disk.
338 */
339 public void createFilterCriteria() {
340 // Create Filter Criteria and save tme
341 final List<FilterCriteria> filterToSave = new ArrayList<>();
342 Criteria criteria = new Criteria();
343 criteria.setLifeLineSelected(true);
344 criteria.setExpression(IUml2SDTestConstants.FIRST_PLAYER_NAME);
345 filterToSave.add(new FilterCriteria(criteria, true, false));
346
347 criteria = new Criteria();
348 criteria.setSyncMessageSelected(true);
349 criteria.setExpression("BALL_.*");
350 filterToSave.add(new FilterCriteria(criteria, true, false));
351 FilterListDialog.saveFiltersCriteria(filterToSave);
352 }
353
354
355 }
This page took 0.040808 seconds and 6 git commands to generate.