Merge branch 'master' into lttng-kepler
[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, 2012 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.TmfEvent;
25 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
26 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
27 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
29 import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
30 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
31 import org.eclipse.linuxtools.tmf.ui.tests.uml2sd.trace.TmfUml2SDTestTrace;
32 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView;
33 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.Criteria;
34 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.FilterCriteria;
35 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.FilterListDialog;
36 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.load.LoadersManager;
37 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader;
38 import org.eclipse.swt.widgets.Display;
39 import org.eclipse.ui.IViewPart;
40 import org.eclipse.ui.PartInitException;
41 import org.eclipse.ui.PlatformUI;
42 import org.osgi.framework.FrameworkUtil;
43
44 /**
45 * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides
46 * utility methods for interacting with the loader/view.
47 *
48 * @author Bernd Hufmann
49 */
50 public class Uml2SDTestFacility {
51
52 // ------------------------------------------------------------------------
53 // Attributes
54 // ------------------------------------------------------------------------
55 private static Uml2SDTestFacility fInstance = null;
56
57 private TmfUml2SDSyncLoader fLoader;
58 private SDView fSdView;
59 private TmfTraceStub fTrace = null;
60 private TmfUml2SDTestTrace fParser = null;
61 private TmfExperiment fExperiment = null;
62
63 private boolean fIsInitialized = false;
64
65 // ------------------------------------------------------------------------
66 // Constructors
67 // ------------------------------------------------------------------------
68 private Uml2SDTestFacility() {
69 }
70
71 // ------------------------------------------------------------------------
72 // Operations
73 // ------------------------------------------------------------------------
74 /**
75 * @return the singleton instance.
76 */
77 public synchronized static Uml2SDTestFacility getInstance() {
78 if (fInstance == null) {
79 fInstance = new Uml2SDTestFacility();
80 fInstance.init();
81 }
82 return fInstance;
83 }
84
85 /**
86 * Initial the test facility.
87 */
88 public void init() {
89
90 if (!fIsInitialized) {
91
92 fParser = new TmfUml2SDTestTrace();
93 fTrace = setupTrace(fParser);
94 fParser.setTrace(fTrace);
95
96 IViewPart view;
97 try {
98 // Remove welcome view to avoid interference during test execution
99 view = PlatformUI.getWorkbench()
100 .getActiveWorkbenchWindow()
101 .getActivePage()
102 .findView("org.eclipse.ui.internal.introview"); //$NON-NLS-1$
103
104 if (view != null) {
105 PlatformUI.getWorkbench()
106 .getActiveWorkbenchWindow()
107 .getActivePage().hideView(view);
108 }
109
110 view = PlatformUI.getWorkbench()
111 .getActiveWorkbenchWindow()
112 .getActivePage()
113 .showView("org.eclipse.linuxtools.tmf.ui.tmfUml2SDSyncView"); //$NON-NLS-1$
114
115 } catch (final PartInitException e) {
116 throw new RuntimeException(e);
117 }
118
119 fSdView = (SDView) view;
120 fLoader = (TmfUml2SDSyncLoader)LoadersManager.getInstance().createLoader(
121 "org.eclipse.linuxtools.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader", fSdView); //$NON-NLS-1$
122
123 delay(3000);
124 fIsInitialized = true;
125 }
126 }
127
128
129 private TmfTraceStub setupTrace(final ITmfEventParser parser) {
130
131 try {
132 // Create test trace object
133 final URL location = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), new Path("tracesets/sdEvents"), null); //$NON-NLS-1$
134 final File test = new File(FileLocator.toFileURL(location).toURI());
135 return new TmfTraceStub(test.getPath(), 500, true, parser, null);
136 } catch (final TmfTraceException e) {
137 e.printStackTrace();
138 throw new RuntimeException(e);
139 } catch (final URISyntaxException e) {
140 e.printStackTrace();
141 throw new RuntimeException(e);
142 } catch (final IOException e) {
143 e.printStackTrace();
144 throw new RuntimeException(e);
145 }
146 }
147
148 /**
149 * Dispose the resource
150 */
151 public void dispose() {
152 if (fIsInitialized) {
153 fExperiment.dispose();
154
155 // Wait for all Eclipse jobs to finish
156 waitForJobs();
157
158 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fSdView);
159 fIsInitialized = false;
160 }
161 }
162
163 /**
164 * Sleeps current thread or GUI thread for a given time.
165 * @param waitTimeMillis time in milliseconds to wait
166 */
167 public void delay(final long waitTimeMillis) {
168 final Display display = Display.getCurrent();
169 if (display != null) {
170 final long endTimeMillis = System.currentTimeMillis() + waitTimeMillis;
171 while(System.currentTimeMillis() < endTimeMillis) {
172 if (!display.readAndDispatch()) {
173 display.sleep();
174 }
175 display.update();
176 }
177 } else {
178 try {
179 Thread.sleep(waitTimeMillis);
180 } catch (final InterruptedException e) {
181 // Ignored
182 }
183 }
184 }
185
186 /**
187 * Waits for all Eclipse jobs to finish
188 */
189 public void waitForJobs() {
190 while (!Job.getJobManager().isIdle()) {
191 delay(IUml2SDTestConstants.WAIT_FOR_JOBS_DELAY);
192 }
193 }
194
195 /**
196 * @return current UML2SD loader
197 */
198 public TmfUml2SDSyncLoader getLoader() {
199 return fLoader;
200 }
201
202 /**
203 * @return current SD view
204 */
205 public SDView getSdView() {
206 return fSdView;
207 }
208
209 /**
210 * @return current trace
211 */
212 public TmfTraceStub getTrace() {
213 return fTrace;
214 }
215
216 /**
217 * @return Trace parser
218 */
219 public TmfUml2SDTestTrace getParser() {
220 return fParser;
221 }
222
223 /**
224 * @return current experiment.
225 */
226 public TmfExperiment getExperiment() {
227 return fExperiment;
228 }
229
230 /**
231 * Go to next page;
232 */
233 public void nextPage() {
234 fLoader.nextPage();
235 fLoader.waitForCompletion();
236 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
237 }
238
239 /**
240 * Go to previous page.
241 */
242 public void prevPage() {
243 fLoader.prevPage();
244 fLoader.waitForCompletion();
245 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
246 }
247
248 /**
249 * Go to last page.
250 */
251 public void lastPage() {
252 fLoader.lastPage();
253 fLoader.waitForCompletion();
254 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
255 }
256
257 /**
258 * Go to first page.
259 */
260 public void firstPage() {
261 fLoader.firstPage();
262 fLoader.waitForCompletion();
263 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
264 }
265
266 /**
267 * @param page number to set
268 */
269 public void setPage(final int page) {
270 fLoader.pageNumberChanged(page);
271 fLoader.waitForCompletion();
272 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
273 }
274
275 /**
276 * @see org.eclipse.linuxtools.tmf.ui.tests.views.uml2sd.loader.Uml2SDTestFacility#selectExperiment(boolean)
277 */
278 public void selectExperiment() {
279 this.selectExperiment(true);
280 }
281
282 /**
283 * Selects the experiment.
284 * @param wait true to wait for indexing to finish else false
285 */
286 public void selectExperiment(final boolean wait) {
287 fParser = new TmfUml2SDTestTrace();
288 fTrace = setupTrace(fParser);
289 fParser.setTrace(fTrace);
290
291 // fTrace = setupTrace(fParser);
292
293 final ITmfTrace traces[] = new ITmfTrace[1];
294 traces[0] = fTrace;
295 fExperiment = new TmfExperiment(TmfEvent.class, "TestExperiment", traces); //$NON-NLS-1$
296 fTrace.broadcast(new TmfExperimentSelectedSignal(this, fExperiment));
297 if (wait) {
298 while (fExperiment.getNbEvents() == 0) {
299 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
300 }
301 waitForJobs();
302 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
303 }
304 }
305
306 /**
307 * Disposes the experiment.
308 */
309 public void disposeExperiment() {
310 fExperiment.dispose();
311 delay(IUml2SDTestConstants.GUI_REFESH_DELAY);
312 }
313
314 /**
315 * Creates some global filter criteria and saves them to disk.
316 */
317 public void createFilterCriteria() {
318 // Create Filter Criteria and save tme
319 final List<FilterCriteria> filterToSave = new ArrayList<FilterCriteria>();
320 Criteria criteria = new Criteria();
321 criteria.setLifeLineSelected(true);
322 criteria.setExpression(IUml2SDTestConstants.FIRST_PLAYER_NAME);
323 filterToSave.add(new FilterCriteria(criteria, true, false));
324
325 criteria = new Criteria();
326 criteria.setSyncMessageSelected(true);
327 criteria.setExpression("BALL_.*"); //$NON-NLS-1$
328 filterToSave.add(new FilterCriteria(criteria, true, false));
329 FilterListDialog.saveFiltersCriteria(filterToSave);
330 }
331
332
333 }
This page took 0.05512 seconds and 6 git commands to generate.