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