1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.tmf
.ui
.tests
.views
.uml2sd
.loader
;
15 import java
.io
.IOException
;
16 import java
.net
.URISyntaxException
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
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
.swt
.widgets
.Display
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfEventParser
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
.TmfExperiment
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
35 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
36 import org
.eclipse
.tracecompass
.tmf
.ui
.tests
.uml2sd
.trace
.TmfUml2SDTestTrace
;
37 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.SDView
;
38 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.dialogs
.Criteria
;
39 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.dialogs
.FilterCriteria
;
40 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.dialogs
.FilterListDialog
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.load
.LoadersManager
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.loader
.TmfUml2SDSyncLoader
;
43 import org
.eclipse
.ui
.IViewPart
;
44 import org
.eclipse
.ui
.PartInitException
;
45 import org
.eclipse
.ui
.PlatformUI
;
46 import org
.osgi
.framework
.FrameworkUtil
;
49 * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides
50 * utility methods for interacting with the loader/view.
52 * @author Bernd Hufmann
54 public class Uml2SDTestFacility
{
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
59 private static Uml2SDTestFacility fInstance
= null;
61 private TmfUml2SDSyncLoader fLoader
;
62 private SDView fSdView
;
63 private TmfTraceStub fTrace
= null;
64 private TmfUml2SDTestTrace fParser
= null;
65 private TmfExperiment fExperiment
= null;
67 private volatile boolean fIsInitialized
= false;
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
72 private Uml2SDTestFacility() {
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
79 * @return the singleton instance.
81 public synchronized static Uml2SDTestFacility
getInstance() {
82 if (fInstance
== null) {
83 fInstance
= new Uml2SDTestFacility();
90 * Initial the test facility.
94 if (!fIsInitialized
) {
96 fParser
= new TmfUml2SDTestTrace();
97 fTrace
= setupTrace(fParser
);
98 fParser
.setTrace(fTrace
);
102 // Remove welcome view to avoid interference during test execution
103 view
= PlatformUI
.getWorkbench()
104 .getActiveWorkbenchWindow()
106 .findView("org.eclipse.ui.internal.introview");
109 PlatformUI
.getWorkbench()
110 .getActiveWorkbenchWindow()
111 .getActivePage().hideView(view
);
114 view
= PlatformUI
.getWorkbench()
115 .getActiveWorkbenchWindow()
117 .showView("org.eclipse.linuxtools.tmf.ui.tmfUml2SDSyncView");
119 } catch (final PartInitException e
) {
120 throw new RuntimeException(e
);
123 fSdView
= (SDView
) view
;
124 fLoader
= (TmfUml2SDSyncLoader
) LoadersManager
.getInstance().createLoader(
125 "org.eclipse.tracecompass.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader", fSdView
);
128 fIsInitialized
= true;
133 private TmfTraceStub
setupTrace(final ITmfEventParser parser
) {
136 // Create test trace object
137 final URL location
= FileLocator
.find(FrameworkUtil
.getBundle(this.getClass()), new Path("tracesets/sdEvents"), null);
138 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
139 return new TmfTraceStub(test
.getPath(), 500, true, parser
);
140 } catch (final TmfTraceException e
) {
142 throw new RuntimeException(e
);
143 } catch (final URISyntaxException e
) {
145 throw new RuntimeException(e
);
146 } catch (final IOException e
) {
148 throw new RuntimeException(e
);
153 * Dispose the resource
155 public void dispose() {
156 if (fIsInitialized
) {
157 ITmfTrace trace
= fTrace
;
158 TmfExperiment experiment
= fExperiment
;
159 if (trace
== null || experiment
== null) {
160 throw new IllegalStateException();
163 trace
.broadcast(new TmfTraceClosedSignal(this, experiment
));
164 experiment
.dispose();
166 // Wait for all Eclipse jobs to finish
169 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fSdView
);
170 fIsInitialized
= false;
175 * Sleeps current thread or GUI thread for a given time.
176 * @param waitTimeMillis time in milliseconds to wait
178 public void delay(final long waitTimeMillis
) {
179 final Display display
= Display
.getCurrent();
180 if (display
!= null) {
181 final long endTimeMillis
= System
.currentTimeMillis() + waitTimeMillis
;
182 while(System
.currentTimeMillis() < endTimeMillis
) {
183 if (!display
.readAndDispatch()) {
184 // We do not use Display.sleep because it might never wake up
185 // if there is no user interaction
187 Thread
.sleep(Math
.min(waitTimeMillis
, 10));
188 } catch (final InterruptedException e
) {
196 Thread
.sleep(waitTimeMillis
);
197 } catch (final InterruptedException e
) {
204 * Waits for all Eclipse jobs to finish
206 public void waitForJobs() {
207 while (!Job
.getJobManager().isIdle()) {
208 delay(IUml2SDTestConstants
.WAIT_FOR_JOBS_DELAY
);
213 * @return current UML2SD loader
215 public TmfUml2SDSyncLoader
getLoader() {
220 * @return current SD view
222 public SDView
getSdView() {
227 * @return current trace
229 public TmfTraceStub
getTrace() {
234 * @return Trace parser
236 public TmfUml2SDTestTrace
getParser() {
241 * @return current experiment.
243 public TmfExperiment
getExperiment() {
250 public void nextPage() {
252 fLoader
.waitForCompletion();
253 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
257 * Go to previous page.
259 public void prevPage() {
261 fLoader
.waitForCompletion();
262 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
268 public void lastPage() {
270 fLoader
.waitForCompletion();
271 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
277 public void firstPage() {
279 fLoader
.waitForCompletion();
280 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
284 * @param page number to set
286 public void setPage(final int page
) {
287 fLoader
.pageNumberChanged(page
);
288 fLoader
.waitForCompletion();
289 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
293 * @see org.eclipse.tracecompass.tmf.ui.tests.views.uml2sd.loader.Uml2SDTestFacility#selectExperiment(boolean)
295 public void selectExperiment() {
296 this.selectExperiment(true);
300 * Selects the experiment.
301 * @param wait true to wait for indexing to finish else false
303 public void selectExperiment(final boolean wait
) {
304 fParser
= new TmfUml2SDTestTrace();
305 fTrace
= setupTrace(fParser
);
306 fParser
.setTrace(fTrace
);
308 final ITmfTrace traces
[] = new ITmfTrace
[1];
310 fExperiment
= new TmfExperiment(ITmfEvent
.class, "TestExperiment",
311 traces
, TmfExperiment
.DEFAULT_INDEX_PAGE_SIZE
, null) {
313 protected ITmfTraceIndexer
createIndexer(int interval
) {
314 return new TmfCheckpointIndexer(this, interval
);
317 fTrace
.broadcast(new TmfTraceOpenedSignal(this, fExperiment
, null));
318 fTrace
.broadcast(new TmfTraceSelectedSignal(this, fExperiment
));
320 while (fExperiment
.getNbEvents() == 0) {
321 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
324 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
329 * Disposes the experiment.
331 public void disposeExperiment() {
332 ITmfTrace trace
= fTrace
;
333 TmfExperiment experiment
= fExperiment
;
334 if (trace
== null || experiment
== null) {
335 throw new IllegalStateException();
337 trace
.broadcast(new TmfTraceClosedSignal(this, experiment
));
338 experiment
.dispose();
339 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
343 * Creates some global filter criteria and saves them to disk.
345 public void createFilterCriteria() {
346 // Create Filter Criteria and save tme
347 final List
<FilterCriteria
> filterToSave
= new ArrayList
<>();
348 Criteria criteria
= new Criteria();
349 criteria
.setLifeLineSelected(true);
350 criteria
.setExpression(IUml2SDTestConstants
.FIRST_PLAYER_NAME
);
351 filterToSave
.add(new FilterCriteria(criteria
, true, false));
353 criteria
= new Criteria();
354 criteria
.setSyncMessageSelected(true);
355 criteria
.setExpression("BALL_.*");
356 filterToSave
.add(new FilterCriteria(criteria
, true, false));
357 FilterListDialog
.saveFiltersCriteria(filterToSave
);