0e4f0caaa65ae2d8d621c16be8394923054ee0d0
[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.core.runtime.jobs.Job;
26 import org.eclipse.swt.widgets.Display;
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;
34 import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
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;
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 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 */
277 public TmfExperiment getExperiment() {
278 return fExperiment;
279 }
280
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 }
298
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 }
316
317 /**
318 * @param page number to set
319 */
320 public void setPage(final int page) {
321 fLoader.pageNumberChanged(page);
322 fLoader.waitForCompletion();
323 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
324 }
325
326 /**
327 * @see org.eclipse.tracecompass.tmf.ui.tests.views.uml2sd.loader.Uml2SDTestFacility#selectExperiment(boolean)
328 */
329 public void selectExperiment() {
330 this.selectExperiment(true);
331 }
332
333 /**
334 * Selects the experiment.
335 * @param wait true to wait for indexing to finish else false
336 */
337 public void selectExperiment(final boolean wait) {
338 fParser = new TmfUml2SDTestTrace();
339 fTrace = setupTrace(fParser);
340 fParser.setTrace(fTrace);
341
342 final ITmfTrace traces[] = new ITmfTrace[1];
343 traces[0] = fTrace;
344 fExperiment = new TmfExperiment(ITmfEvent.class, "TestExperiment",
345 traces, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null) {
346 @Override
347 protected ITmfTraceIndexer createIndexer(int interval) {
348 return new TmfCheckpointIndexer(this, interval);
349 }
350 };
351 fTrace.broadcast(new TmfTraceOpenedSignal(this, fExperiment, null));
352 fTrace.broadcast(new TmfTraceSelectedSignal(this, fExperiment));
353 if (wait) {
354 while (fExperiment.getNbEvents() == 0) {
355 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
356 }
357 waitForJobs();
358 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
359 }
360 }
361
362 /**
363 * Disposes the experiment.
364 */
365 public void disposeExperiment() {
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();
373 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
374 }
375
376 /**
377 * Creates some global filter criteria and saves them to disk.
378 */
379 public void createFilterCriteria() {
380 // Create Filter Criteria and save tme
381 final List<FilterCriteria> filterToSave = new ArrayList<>();
382 Criteria criteria = new Criteria();
383 criteria.setLifeLineSelected(true);
384 criteria.setExpression(IUml2SDTestConstants.FIRST_PLAYER_NAME);
385 filterToSave.add(new FilterCriteria(criteria, true, false));
386
387 criteria = new Criteria();
388 criteria.setSyncMessageSelected(true);
389 criteria.setExpression("BALL_.*");
390 filterToSave.add(new FilterCriteria(criteria, true, false));
391 FilterListDialog.saveFiltersCriteria(filterToSave);
392 }
393
394
395 }
This page took 0.039119 seconds and 4 git commands to generate.