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