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
;
14 import static org
.junit
.Assert
.assertNotNull
;
17 import java
.io
.IOException
;
18 import java
.net
.URISyntaxException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
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
;
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.
54 * @author Bernd Hufmann
56 public class Uml2SDTestFacility
{
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
61 private static final String SD_VIEW_ID
= "org.eclipse.linuxtools.tmf.ui.tmfUml2SDSyncView";
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
66 private static Uml2SDTestFacility fInstance
= null;
68 private TmfUml2SDSyncLoader fLoader
;
69 private SDView fSdView
;
70 private TmfTraceStub fTrace
= null;
71 private TmfUml2SDTestTrace fParser
= null;
72 private TmfExperiment fExperiment
= null;
74 private volatile boolean fIsInitialized
= false;
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
79 private Uml2SDTestFacility() {
80 fParser
= new TmfUml2SDTestTrace();
81 fTrace
= setupTrace(fParser
);
82 fParser
.setTrace(fTrace
);
85 // ------------------------------------------------------------------------
87 // ------------------------------------------------------------------------
89 * @return the singleton instance.
91 public synchronized static Uml2SDTestFacility
getInstance() {
92 if (fInstance
== null) {
93 fInstance
= new Uml2SDTestFacility();
99 * Initial the test facility.
103 if (!fIsInitialized
) {
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
);
113 fSdView
= (SDView
) view
;
114 fLoader
= (TmfUml2SDSyncLoader
) LoadersManager
.getInstance().createLoader(
115 "org.eclipse.tracecompass.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader", fSdView
);
117 fIsInitialized
= true;
121 private TmfTraceStub
setupTrace(final ITmfEventParser parser
) {
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
) {
130 throw new RuntimeException(e
);
131 } catch (final URISyntaxException e
) {
133 throw new RuntimeException(e
);
134 } catch (final IOException e
) {
136 throw new RuntimeException(e
);
141 * Dispose the resource
143 public void dispose() {
144 if (fIsInitialized
) {
145 // Wait for all Eclipse jobs to finish
147 hideView(SD_VIEW_ID
);
149 fIsInitialized
= false;
154 * Sleeps current thread or GUI thread for a given time.
155 * @param waitTimeMillis time in milliseconds to wait
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
166 Thread
.sleep(Math
.min(waitTimeMillis
, 10));
167 } catch (final InterruptedException e
) {
175 Thread
.sleep(waitTimeMillis
);
176 } catch (final InterruptedException e
) {
183 * Waits for a view to be closed
188 public void waitForViewClosed(String viewId
) {
189 for (int i
= 1; i
< 5000 && (getViewPart(viewId
) != null); i
*= 2) {
195 * Waits for a view to be opened
200 public void waitForViewOpened(String viewId
) {
201 for (int i
= 1; i
< 5000 && (getViewPart(viewId
) == null); i
*= 2) {
206 private IViewPart
showView(String viewId
) throws PartInitException
{
207 IViewPart view
= getViewPart(viewId
);
210 view
= PlatformUI
.getWorkbench()
211 .getActiveWorkbenchWindow()
212 .getActivePage().showView(viewId
);
214 waitForViewOpened(viewId
);
220 private void hideView(String viewId
) {
221 IViewPart view
= getViewPart(viewId
);
223 PlatformUI
.getWorkbench()
224 .getActiveWorkbenchWindow()
225 .getActivePage().hideView(view
);
227 waitForViewClosed(viewId
);
230 private static IViewPart
getViewPart(String viewId
) {
231 return PlatformUI
.getWorkbench()
232 .getActiveWorkbenchWindow()
238 * Waits for all Eclipse jobs to finish
240 public void waitForJobs() {
241 while (!Job
.getJobManager().isIdle()) {
242 delay(IUml2SDTestConstants
.WAIT_FOR_JOBS_DELAY
);
247 * @return current UML2SD loader
249 public TmfUml2SDSyncLoader
getLoader() {
254 * @return current SD view
256 public SDView
getSdView() {
261 * @return current trace
263 public TmfTraceStub
getTrace() {
268 * @return Trace parser
270 public TmfUml2SDTestTrace
getParser() {
275 * @return current experiment.
277 public TmfExperiment
getExperiment() {
284 public void nextPage() {
286 fLoader
.waitForCompletion();
287 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
291 * Go to previous page.
293 public void prevPage() {
295 fLoader
.waitForCompletion();
296 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
302 public void lastPage() {
304 fLoader
.waitForCompletion();
305 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
311 public void firstPage() {
313 fLoader
.waitForCompletion();
314 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
318 * @param page number to set
320 public void setPage(final int page
) {
321 fLoader
.pageNumberChanged(page
);
322 fLoader
.waitForCompletion();
323 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
327 * @see org.eclipse.tracecompass.tmf.ui.tests.views.uml2sd.loader.Uml2SDTestFacility#selectExperiment(boolean)
329 public void selectExperiment() {
330 this.selectExperiment(true);
334 * Selects the experiment.
335 * @param wait true to wait for indexing to finish else false
337 public void selectExperiment(final boolean wait
) {
338 fParser
= new TmfUml2SDTestTrace();
339 fTrace
= setupTrace(fParser
);
340 fParser
.setTrace(fTrace
);
342 final ITmfTrace traces
[] = new ITmfTrace
[1];
344 fExperiment
= new TmfExperiment(ITmfEvent
.class, "TestExperiment",
345 traces
, TmfExperiment
.DEFAULT_INDEX_PAGE_SIZE
, null) {
347 protected ITmfTraceIndexer
createIndexer(int interval
) {
348 return new TmfCheckpointIndexer(this, interval
);
351 fTrace
.broadcast(new TmfTraceOpenedSignal(this, fExperiment
, null));
352 fTrace
.broadcast(new TmfTraceSelectedSignal(this, fExperiment
));
354 while (fExperiment
.getNbEvents() == 0) {
355 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
358 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
363 * Disposes the experiment.
365 public void disposeExperiment() {
366 ITmfTrace trace
= fTrace
;
367 TmfExperiment experiment
= fExperiment
;
368 if (trace
== null || experiment
== null) {
369 throw new IllegalStateException();
371 trace
.broadcast(new TmfTraceClosedSignal(this, experiment
));
372 experiment
.dispose();
373 delay(IUml2SDTestConstants
.GUI_REFESH_DELAY
);
377 * Creates some global filter criteria and saves them to disk.
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));
387 criteria
= new Criteria();
388 criteria
.setSyncMessageSelected(true);
389 criteria
.setExpression("BALL_.*");
390 filterToSave
.add(new FilterCriteria(criteria
, true, false));
391 FilterListDialog
.saveFiltersCriteria(filterToSave
);