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