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