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