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