Commit | Line | Data |
---|---|---|
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 | *******************************************************************************/ | |
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; | |
6c13869b FC |
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; | |
4918b8f2 | 29 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; |
73005152 BH |
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; | |
9269df72 | 41 | import 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 | |
20658947 | 45 | * utility methods for interacting with the loader/view. |
73005152 BH |
46 | */ |
47 | public class Uml2SDTestFacility { | |
20658947 | 48 | |
73005152 BH |
49 | // ------------------------------------------------------------------------ |
50 | // Attributes | |
51 | // ------------------------------------------------------------------------ | |
52 | private static Uml2SDTestFacility fInstance = null; | |
20658947 | 53 | |
73005152 BH |
54 | private TmfUml2SDSyncLoader fLoader; |
55 | private SDView fSdView; | |
56 | private TmfTraceStub fTrace = null; | |
57 | private TmfUml2SDTestTrace fParser = null; | |
58 | private TmfExperiment<TmfEvent> fExperiment = null; | |
20658947 | 59 | |
73005152 | 60 | private boolean fIsInitialized = false; |
20658947 | 61 | |
73005152 BH |
62 | // ------------------------------------------------------------------------ |
63 | // Constructors | |
64 | // ------------------------------------------------------------------------ | |
65 | private Uml2SDTestFacility() { | |
66 | } | |
67 | ||
68 | // ------------------------------------------------------------------------ | |
69 | // Operations | |
70 | // ------------------------------------------------------------------------ | |
1f2f091b | 71 | public synchronized static Uml2SDTestFacility getInstance() { |
73005152 BH |
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() { |
20658947 | 83 | |
73005152 | 84 | if (!fIsInitialized) { |
73005152 BH |
85 | |
86 | fParser = new TmfUml2SDTestTrace(); | |
87 | fTrace = setupTrace(fParser); | |
20658947 | 88 | |
73005152 BH |
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$ | |
20658947 | 96 | |
1e412478 BH |
97 | if (view != null) { |
98 | PlatformUI.getWorkbench() | |
99 | .getActiveWorkbenchWindow() | |
20658947 | 100 | .getActivePage().hideView(view); |
1e412478 | 101 | } |
20658947 | 102 | |
73005152 | 103 | view = PlatformUI.getWorkbench() |
20658947 FC |
104 | .getActiveWorkbenchWindow() |
105 | .getActivePage() | |
106 | .showView("org.eclipse.linuxtools.tmf.ui.tmfUml2SDSyncView"); //$NON-NLS-1$ | |
107 | ||
108 | } catch (final PartInitException e) { | |
73005152 BH |
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 | ||
20658947 FC |
121 | |
122 | private TmfTraceStub setupTrace(final ITmfEventParser<TmfEvent> parser) { | |
123 | ||
124 | try { | |
125 | // Create test trace object | |
126 | final URL location = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), new Path("tracesets/sdEvents"), null); //$NON-NLS-1$ | |
127 | final File test = new File(FileLocator.toFileURL(location).toURI()); | |
128 | return new TmfTraceStub(test.getPath(), 500, true, parser, null); | |
129 | } catch (final URISyntaxException e) { | |
130 | e.printStackTrace(); | |
131 | throw new RuntimeException(e); | |
132 | } catch (final IOException e) { | |
133 | e.printStackTrace(); | |
134 | throw new RuntimeException(e); | |
135 | } | |
73005152 | 136 | } |
20658947 | 137 | |
73005152 BH |
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 | } | |
20658947 | 152 | |
73005152 | 153 | /** |
20658947 | 154 | * Sleeps current thread or GUI thread for a given time. |
73005152 BH |
155 | * @param waitTimeMillis |
156 | */ | |
20658947 FC |
157 | public void delay(final long waitTimeMillis) { |
158 | final Display display = Display.getCurrent(); | |
73005152 | 159 | if (display != null) { |
20658947 | 160 | final long endTimeMillis = System.currentTimeMillis() + waitTimeMillis; |
73005152 BH |
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); | |
20658947 | 170 | } catch (final InterruptedException e) { |
73005152 BH |
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 | } | |
20658947 | 219 | |
73005152 BH |
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 | } | |
20658947 | 237 | |
73005152 BH |
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 | } | |
20658947 | 255 | |
73005152 BH |
256 | /** |
257 | * @param page number to set | |
258 | */ | |
20658947 | 259 | public void setPage(final int page) { |
73005152 BH |
260 | fLoader.pageNumberChanged(page);; |
261 | fLoader.waitForCompletion(); | |
262 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); | |
263 | } | |
20658947 | 264 | |
73005152 BH |
265 | /** |
266 | * @see org.eclipse.linuxtools.tmf.ui.tests.views.uml2sd.impl.selectExperiment(boolean) | |
267 | */ | |
268 | public void selectExperiment() { | |
269 | this.selectExperiment(true); | |
270 | } | |
20658947 | 271 | |
73005152 | 272 | /** |
20658947 | 273 | * Selects the experiment. |
73005152 BH |
274 | * @param wait true to wait for indexing to finish else false |
275 | */ | |
4f5d9f9b | 276 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
20658947 | 277 | public void selectExperiment(final boolean wait) { |
73005152 BH |
278 | fTrace = setupTrace(fParser); |
279 | ||
20658947 | 280 | final ITmfTrace traces[] = new ITmfTrace[1]; |
73005152 BH |
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 | } | |
20658947 | 292 | |
73005152 BH |
293 | /** |
294 | * Disposes the experiment. | |
295 | */ | |
296 | public void disposeExperiment() { | |
297 | fExperiment.dispose(); | |
298 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); | |
299 | } | |
20658947 | 300 | |
73005152 | 301 | /** |
20658947 | 302 | * Creates some global filter criteria and saves them to disk. |
73005152 BH |
303 | */ |
304 | public void createFilterCriteria() { | |
305 | // Create Filter Criteria and save tme | |
20658947 | 306 | final List<FilterCriteria> filterToSave = new ArrayList<FilterCriteria>(); |
73005152 BH |
307 | Criteria criteria = new Criteria(); |
308 | criteria.setLifeLineSelected(true); | |
309 | criteria.setExpression(IUml2SDTestConstants.FIRST_PLAYER_NAME); | |
310 | filterToSave.add(new FilterCriteria(criteria, true, false)); | |
20658947 | 311 | |
73005152 BH |
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 | } |