ss: Move plugins to Trace Compass namespace
[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.core.trace.indexer.ITmfTraceIndexer;
33 import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
34 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
35 import org.eclipse.linuxtools.tmf.ui.tests.uml2sd.trace.TmfUml2SDTestTrace;
36 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView;
37 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.Criteria;
38 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.FilterCriteria;
39 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.FilterListDialog;
40 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.load.LoadersManager;
41 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader;
42 import org.eclipse.swt.widgets.Display;
43 import org.eclipse.ui.IViewPart;
44 import org.eclipse.ui.PartInitException;
45 import org.eclipse.ui.PlatformUI;
46 import org.osgi.framework.FrameworkUtil;
47
48 /**
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.
51 *
52 * @author Bernd Hufmann
53 */
54 public class Uml2SDTestFacility {
55
56 // ------------------------------------------------------------------------
57 // Attributes
58 // ------------------------------------------------------------------------
59 private static Uml2SDTestFacility fInstance = null;
60
61 private TmfUml2SDSyncLoader fLoader;
62 private SDView fSdView;
63 private TmfTraceStub fTrace = null;
64 private TmfUml2SDTestTrace fParser = null;
65 private TmfExperiment fExperiment = null;
66
67 private volatile boolean fIsInitialized = false;
68
69 // ------------------------------------------------------------------------
70 // Constructors
71 // ------------------------------------------------------------------------
72 private Uml2SDTestFacility() {
73 }
74
75 // ------------------------------------------------------------------------
76 // Operations
77 // ------------------------------------------------------------------------
78 /**
79 * @return the singleton instance.
80 */
81 public synchronized static Uml2SDTestFacility getInstance() {
82 if (fInstance == null) {
83 fInstance = new Uml2SDTestFacility();
84 fInstance.init();
85 }
86 return fInstance;
87 }
88
89 /**
90 * Initial the test facility.
91 */
92 public void init() {
93
94 if (!fIsInitialized) {
95
96 fParser = new TmfUml2SDTestTrace();
97 fTrace = setupTrace(fParser);
98 fParser.setTrace(fTrace);
99
100 IViewPart view;
101 try {
102 // Remove welcome view to avoid interference during test execution
103 view = PlatformUI.getWorkbench()
104 .getActiveWorkbenchWindow()
105 .getActivePage()
106 .findView("org.eclipse.ui.internal.introview");
107
108 if (view != null) {
109 PlatformUI.getWorkbench()
110 .getActiveWorkbenchWindow()
111 .getActivePage().hideView(view);
112 }
113
114 view = PlatformUI.getWorkbench()
115 .getActiveWorkbenchWindow()
116 .getActivePage()
117 .showView("org.eclipse.linuxtools.tmf.ui.tmfUml2SDSyncView");
118
119 } catch (final PartInitException e) {
120 throw new RuntimeException(e);
121 }
122
123 fSdView = (SDView) view;
124 fLoader = (TmfUml2SDSyncLoader)LoadersManager.getInstance().createLoader(
125 "org.eclipse.linuxtools.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader", fSdView);
126
127 delay(3000);
128 fIsInitialized = true;
129 }
130 }
131
132
133 private TmfTraceStub setupTrace(final ITmfEventParser parser) {
134
135 try {
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) {
141 e.printStackTrace();
142 throw new RuntimeException(e);
143 } catch (final URISyntaxException e) {
144 e.printStackTrace();
145 throw new RuntimeException(e);
146 } catch (final IOException e) {
147 e.printStackTrace();
148 throw new RuntimeException(e);
149 }
150 }
151
152 /**
153 * Dispose the resource
154 */
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();
161 }
162
163 trace.broadcast(new TmfTraceClosedSignal(this, experiment));
164 experiment.dispose();
165
166 // Wait for all Eclipse jobs to finish
167 waitForJobs();
168
169 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fSdView);
170 fIsInitialized = false;
171 }
172 }
173
174 /**
175 * Sleeps current thread or GUI thread for a given time.
176 * @param waitTimeMillis time in milliseconds to wait
177 */
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
186 try {
187 Thread.sleep(Math.min(waitTimeMillis, 10));
188 } catch (final InterruptedException e) {
189 // Ignored
190 }
191 }
192 display.update();
193 }
194 } else {
195 try {
196 Thread.sleep(waitTimeMillis);
197 } catch (final InterruptedException e) {
198 // Ignored
199 }
200 }
201 }
202
203 /**
204 * Waits for all Eclipse jobs to finish
205 */
206 public void waitForJobs() {
207 while (!Job.getJobManager().isIdle()) {
208 delay(IUml2SDTestConstants.WAIT_FOR_JOBS_DELAY);
209 }
210 }
211
212 /**
213 * @return current UML2SD loader
214 */
215 public TmfUml2SDSyncLoader getLoader() {
216 return fLoader;
217 }
218
219 /**
220 * @return current SD view
221 */
222 public SDView getSdView() {
223 return fSdView;
224 }
225
226 /**
227 * @return current trace
228 */
229 public TmfTraceStub getTrace() {
230 return fTrace;
231 }
232
233 /**
234 * @return Trace parser
235 */
236 public TmfUml2SDTestTrace getParser() {
237 return fParser;
238 }
239
240 /**
241 * @return current experiment.
242 */
243 public TmfExperiment getExperiment() {
244 return fExperiment;
245 }
246
247 /**
248 * Go to next page;
249 */
250 public void nextPage() {
251 fLoader.nextPage();
252 fLoader.waitForCompletion();
253 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
254 }
255
256 /**
257 * Go to previous page.
258 */
259 public void prevPage() {
260 fLoader.prevPage();
261 fLoader.waitForCompletion();
262 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
263 }
264
265 /**
266 * Go to last page.
267 */
268 public void lastPage() {
269 fLoader.lastPage();
270 fLoader.waitForCompletion();
271 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
272 }
273
274 /**
275 * Go to first page.
276 */
277 public void firstPage() {
278 fLoader.firstPage();
279 fLoader.waitForCompletion();
280 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
281 }
282
283 /**
284 * @param page number to set
285 */
286 public void setPage(final int page) {
287 fLoader.pageNumberChanged(page);
288 fLoader.waitForCompletion();
289 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
290 }
291
292 /**
293 * @see org.eclipse.linuxtools.tmf.ui.tests.views.uml2sd.loader.Uml2SDTestFacility#selectExperiment(boolean)
294 */
295 public void selectExperiment() {
296 this.selectExperiment(true);
297 }
298
299 /**
300 * Selects the experiment.
301 * @param wait true to wait for indexing to finish else false
302 */
303 public void selectExperiment(final boolean wait) {
304 fParser = new TmfUml2SDTestTrace();
305 fTrace = setupTrace(fParser);
306 fParser.setTrace(fTrace);
307
308 final ITmfTrace traces[] = new ITmfTrace[1];
309 traces[0] = fTrace;
310 fExperiment = new TmfExperiment(ITmfEvent.class, "TestExperiment", traces) {
311 @Override
312 protected ITmfTraceIndexer createIndexer(int interval) {
313 return new TmfCheckpointIndexer(this, interval);
314 }
315 };
316 fTrace.broadcast(new TmfTraceOpenedSignal(this, fExperiment, null));
317 fTrace.broadcast(new TmfTraceSelectedSignal(this, fExperiment));
318 if (wait) {
319 while (fExperiment.getNbEvents() == 0) {
320 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
321 }
322 waitForJobs();
323 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
324 }
325 }
326
327 /**
328 * Disposes the experiment.
329 */
330 public void disposeExperiment() {
331 ITmfTrace trace = fTrace;
332 TmfExperiment experiment = fExperiment;
333 if (trace == null || experiment == null) {
334 throw new IllegalStateException();
335 }
336 trace.broadcast(new TmfTraceClosedSignal(this, experiment));
337 experiment.dispose();
338 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
339 }
340
341 /**
342 * Creates some global filter criteria and saves them to disk.
343 */
344 public void createFilterCriteria() {
345 // Create Filter Criteria and save tme
346 final List<FilterCriteria> filterToSave = new ArrayList<>();
347 Criteria criteria = new Criteria();
348 criteria.setLifeLineSelected(true);
349 criteria.setExpression(IUml2SDTestConstants.FIRST_PLAYER_NAME);
350 filterToSave.add(new FilterCriteria(criteria, true, false));
351
352 criteria = new Criteria();
353 criteria.setSyncMessageSelected(true);
354 criteria.setExpression("BALL_.*");
355 filterToSave.add(new FilterCriteria(criteria, true, false));
356 FilterListDialog.saveFiltersCriteria(filterToSave);
357 }
358
359
360 }
This page took 0.041739 seconds and 5 git commands to generate.