Merge branch 'master' into lttng-luna
[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 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 fTrace.broadcast(new TmfTraceClosedSignal(this, fExperiment));
156 fExperiment.dispose();
157
158 // Wait for all Eclipse jobs to finish
159 waitForJobs();
160
161 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fSdView);
162 fIsInitialized = false;
163 }
164 }
165
166 /**
167 * Sleeps current thread or GUI thread for a given time.
168 * @param waitTimeMillis time in milliseconds to wait
169 */
170 public void delay(final long waitTimeMillis) {
171 final Display display = Display.getCurrent();
172 if (display != null) {
173 final long endTimeMillis = System.currentTimeMillis() + waitTimeMillis;
174 while(System.currentTimeMillis() < endTimeMillis) {
175 if (!display.readAndDispatch()) {
176 display.sleep();
177 }
178 display.update();
179 }
180 } else {
181 try {
182 Thread.sleep(waitTimeMillis);
183 } catch (final InterruptedException e) {
184 // Ignored
185 }
186 }
187 }
188
189 /**
190 * Waits for all Eclipse jobs to finish
191 */
192 public void waitForJobs() {
193 while (!Job.getJobManager().isIdle()) {
194 delay(IUml2SDTestConstants.WAIT_FOR_JOBS_DELAY);
195 }
196 }
197
198 /**
199 * @return current UML2SD loader
200 */
201 public TmfUml2SDSyncLoader getLoader() {
202 return fLoader;
203 }
204
205 /**
206 * @return current SD view
207 */
208 public SDView getSdView() {
209 return fSdView;
210 }
211
212 /**
213 * @return current trace
214 */
215 public TmfTraceStub getTrace() {
216 return fTrace;
217 }
218
219 /**
220 * @return Trace parser
221 */
222 public TmfUml2SDTestTrace getParser() {
223 return fParser;
224 }
225
226 /**
227 * @return current experiment.
228 */
229 public TmfExperiment getExperiment() {
230 return fExperiment;
231 }
232
233 /**
234 * Go to next page;
235 */
236 public void nextPage() {
237 fLoader.nextPage();
238 fLoader.waitForCompletion();
239 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
240 }
241
242 /**
243 * Go to previous page.
244 */
245 public void prevPage() {
246 fLoader.prevPage();
247 fLoader.waitForCompletion();
248 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
249 }
250
251 /**
252 * Go to last page.
253 */
254 public void lastPage() {
255 fLoader.lastPage();
256 fLoader.waitForCompletion();
257 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
258 }
259
260 /**
261 * Go to first page.
262 */
263 public void firstPage() {
264 fLoader.firstPage();
265 fLoader.waitForCompletion();
266 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
267 }
268
269 /**
270 * @param page number to set
271 */
272 public void setPage(final int page) {
273 fLoader.pageNumberChanged(page);
274 fLoader.waitForCompletion();
275 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
276 }
277
278 /**
279 * @see org.eclipse.linuxtools.tmf.ui.tests.views.uml2sd.loader.Uml2SDTestFacility#selectExperiment(boolean)
280 */
281 public void selectExperiment() {
282 this.selectExperiment(true);
283 }
284
285 /**
286 * Selects the experiment.
287 * @param wait true to wait for indexing to finish else false
288 */
289 public void selectExperiment(final boolean wait) {
290 fParser = new TmfUml2SDTestTrace();
291 fTrace = setupTrace(fParser);
292 fParser.setTrace(fTrace);
293
294 // fTrace = setupTrace(fParser);
295
296 final ITmfTrace traces[] = new ITmfTrace[1];
297 traces[0] = fTrace;
298 fExperiment = new TmfExperiment(ITmfEvent.class, "TestExperiment", traces);
299 fTrace.broadcast(new TmfTraceOpenedSignal(this, fExperiment, null));
300 fTrace.broadcast(new TmfTraceSelectedSignal(this, fExperiment));
301 if (wait) {
302 while (fExperiment.getNbEvents() == 0) {
303 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
304 }
305 waitForJobs();
306 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
307 }
308 }
309
310 /**
311 * Disposes the experiment.
312 */
313 public void disposeExperiment() {
314 fTrace.broadcast(new TmfTraceClosedSignal(this, fExperiment));
315 fExperiment.dispose();
316 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
317 }
318
319 /**
320 * Creates some global filter criteria and saves them to disk.
321 */
322 public void createFilterCriteria() {
323 // Create Filter Criteria and save tme
324 final List<FilterCriteria> filterToSave = new ArrayList<FilterCriteria>();
325 Criteria criteria = new Criteria();
326 criteria.setLifeLineSelected(true);
327 criteria.setExpression(IUml2SDTestConstants.FIRST_PLAYER_NAME);
328 filterToSave.add(new FilterCriteria(criteria, true, false));
329
330 criteria = new Criteria();
331 criteria.setSyncMessageSelected(true);
332 criteria.setExpression("BALL_.*");
333 filterToSave.add(new FilterCriteria(criteria, true, false));
334 FilterListDialog.saveFiltersCriteria(filterToSave);
335 }
336
337
338 }
This page took 0.04073 seconds and 6 git commands to generate.