tmf: Add waitUntil / condition to tmf.ui.tests
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui.tests / src / org / eclipse / tracecompass / lttng2 / control / ui / tests / model / component / TraceControlTestFacility.java
CommitLineData
eb1bab5b 1/*******************************************************************************
b732adaa 2 * Copyright (c) 2011, 2014 Ericsson
cfdb727a 3 *
eb1bab5b
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
cfdb727a 8 *
eb1bab5b
BH
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
2ba3d0a1 12
9bc60be7 13package org.eclipse.tracecompass.lttng2.control.ui.tests.model.component;
eb1bab5b 14
13f36900
BH
15import static org.junit.Assert.assertNotNull;
16
d132bcc7
BH
17import org.eclipse.core.commands.ExecutionException;
18import org.eclipse.core.commands.NotEnabledException;
19import org.eclipse.core.commands.NotHandledException;
20import org.eclipse.core.commands.common.NotDefinedException;
eb1bab5b 21import org.eclipse.swt.widgets.Display;
b732adaa 22import org.eclipse.tracecompass.internal.lttng2.control.core.model.TargetNodeState;
9bc60be7
AM
23import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
24import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
b732adaa 25import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent;
9bc60be7 26import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
f0beeb4a 27import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
eb1bab5b
BH
28import org.eclipse.ui.IViewPart;
29import org.eclipse.ui.PartInitException;
30import org.eclipse.ui.PlatformUI;
d132bcc7 31import org.eclipse.ui.handlers.IHandlerService;
b732adaa 32import org.junit.Assert;
eb1bab5b
BH
33
34/**
35 * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides
cfdb727a 36 * utility methods for interacting with the loader/view.
eb1bab5b 37 */
cfdb727a 38@SuppressWarnings("javadoc")
eb1bab5b
BH
39public class TraceControlTestFacility {
40
41 // ------------------------------------------------------------------------
42 // Constants
43 // ------------------------------------------------------------------------
22a0f8f8 44 public final static int WAIT_FOR_JOBS_DELAY = 50;
a26d90be 45 public final static int GUI_REFESH_DELAY = 500;
cfdb727a 46
4e0b52e0
AM
47 public final static String DIRECTORY = "testfiles";
48 public final static String COMMAND_CATEGORY_PREFIX = "org.eclipse.linuxtools.internal.lttng2.ui.commands.control.";
49 public final static String SCEN_INIT_TEST = "Initialize";
50 public final static String SCEN_SCENARIO_SESSION_HANDLING = "SessionHandling";
51 public final static String SCEN_SCENARIO_SESSION_HANDLING_WITH_PATH = "SessionHandlingWithPath";
cfdb727a 52
eb1bab5b
BH
53 // ------------------------------------------------------------------------
54 // Attributes
55 // ------------------------------------------------------------------------
56 private static TraceControlTestFacility fInstance = null;
57 private ControlView fControlView = null;
13f36900 58 private volatile boolean fIsInitialized = false;
cfdb727a 59
eb1bab5b
BH
60 // ------------------------------------------------------------------------
61 // Constructors
62 // ------------------------------------------------------------------------
63 private TraceControlTestFacility() {
64 }
65
66 // ------------------------------------------------------------------------
67 // Operations
68 // ------------------------------------------------------------------------
69 public static TraceControlTestFacility getInstance() {
70 if (fInstance == null) {
71 fInstance = new TraceControlTestFacility();
72 }
73 return fInstance;
74 }
75
76 /**
77 * Initial the test facility.
78 */
79 public void init() {
cfdb727a 80
eb1bab5b 81 if (!fIsInitialized) {
eb1bab5b
BH
82 IViewPart view;
83 try {
13f36900
BH
84 hideView("org.eclipse.ui.internal.introview");
85 view = showView(ControlView.ID);
eb1bab5b
BH
86 } catch (PartInitException e) {
87 throw new RuntimeException(e);
88 }
eb1bab5b
BH
89 fControlView = (ControlView) view;
90
705e682f
BH
91 /*
92 * It is possible that the connections are saved due to the
93 * auto-save feature of the workbench which calls
94 * ControlView.saveState(IMemento). This can happen at any
95 * time (e.g. when calling delay()).
96 *
97 * When showing the view above ControlView.init(IMemento) is
98 * called which restores saved connections.
99 *
100 * The tests require that the ControlView is empty. So
101 * we remove all the connection nodes from the root.
102 */
103 fControlView.getTraceControlRoot().removeAllChildren();
104
eb1bab5b
BH
105 fIsInitialized = true;
106 }
107 }
108
a26d90be
BH
109 /**
110 * Disposes the facility (and GUI)
111 */
eb1bab5b
BH
112 public void dispose() {
113 if (fIsInitialized) {
114 waitForJobs();
13f36900
BH
115 hideView(ControlView.ID);
116 delay(200);
eb1bab5b
BH
117 fIsInitialized = false;
118 }
119 }
a26d90be
BH
120
121 /**
122 * Creates a delay for given time.
123 * @param waitTimeMillis - time in milli seconds
124 */
eb1bab5b
BH
125 public void delay(long waitTimeMillis) {
126 Display display = Display.getCurrent();
127 if (display != null) {
128 long endTimeMillis = System.currentTimeMillis() + waitTimeMillis;
129 while(System.currentTimeMillis() < endTimeMillis) {
130 if (!display.readAndDispatch()) {
99c3a878
MAL
131 // We do not use Display.sleep because it might never wake up
132 // if there is no user interaction
133 try {
134 Thread.sleep(Math.min(waitTimeMillis, 10));
135 } catch (final InterruptedException e) {
136 // Ignored
b07022a9 137 }
eb1bab5b
BH
138 }
139 display.update();
140 }
141 } else {
142 try {
143 Thread.sleep(waitTimeMillis);
144 } catch (InterruptedException e) {
145 // Ignored
146 }
147 }
148 }
149
b732adaa
MS
150 /**
151 * Waits for a connection to be connected
152 */
153 public void waitForConnect(TargetNodeComponent node) {
154 for (int i = 1; i < 5000 && node.getTargetNodeState() == TargetNodeState.CONNECTING; i *= 2) {
155 try {
156 Thread.sleep(i);
157 } catch (InterruptedException e) {
158 Assert.fail();
159 }
160 }
161 }
162
13f36900
BH
163 /**
164 * Waits for a view to be closed
165 */
166 public void waitForViewClosed(String viewId) {
167 for (int i = 1; i < 5000 && (getViewPart(viewId) != null); i *= 2) {
168 delay(i);
169 }
170 }
171
172 /**
173 * Waits for a view to be closed
174 */
175 public void waitForViewOpend(String viewId) {
176 for (int i = 1; i < 5000 && (getViewPart(viewId) == null); i *= 2) {
177 delay(i);
178 }
179 }
180
eb1bab5b
BH
181 /**
182 * Waits for all Eclipse jobs to finish
183 */
184 public void waitForJobs() {
f0beeb4a 185 WaitUtils.waitForJobs();
eb1bab5b
BH
186 }
187
13f36900
BH
188 private IViewPart showView(String viewId) throws PartInitException {
189 IViewPart view = getViewPart(viewId);
190
191 if (view == null) {
192 view = PlatformUI.getWorkbench()
193 .getActiveWorkbenchWindow()
194 .getActivePage().showView(viewId);
195
196 waitForViewOpend(viewId);
197 }
198 assertNotNull(view);
199 return view;
200 }
201
202 private void hideView(String viewId) {
203 IViewPart view = getViewPart(viewId);
204 if (view != null) {
205 PlatformUI.getWorkbench()
206 .getActiveWorkbenchWindow()
207 .getActivePage().hideView(view);
208 }
209 waitForViewClosed(viewId);
210 }
211
212 private static IViewPart getViewPart(String viewId) {
213 return PlatformUI.getWorkbench()
214 .getActiveWorkbenchWindow()
215 .getActivePage()
216 .findView(viewId);
217 }
218
eb1bab5b
BH
219 /**
220 * @return current control view
221 */
222 public ControlView getControlView() {
223 return fControlView;
224 }
d132bcc7 225
a26d90be
BH
226 /**
227 * Executes an Eclipse command with command ID after selecting passed component
228 * @param component - component to select in the tree
229 * @param commandId - command ID
230 * @throws ExecutionException
231 * @throws NotDefinedException
232 * @throws NotEnabledException
233 * @throws NotHandledException
234 */
235 public void executeCommand(ITraceControlComponent component, String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
236 setSelection(component);
237 executeCommand(commandId);
238 }
cfdb727a 239
a26d90be
BH
240 /**
241 * Executes an Eclipse command with command ID after selecting passed components
242 * @param components - array of components to select in the tree
243 * @param commandId - command ID
244 * @throws ExecutionException
245 * @throws NotDefinedException
246 * @throws NotEnabledException
247 * @throws NotHandledException
248 */
249 public void executeCommand(ITraceControlComponent[] components, String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
250 setSelection(components);
251 executeCommand(commandId);
252 }
253
254 /**
255 * Executes an Eclipse command with command ID
256 * @param commandId
257 * @throws ExecutionException
258 * @throws NotDefinedException
259 * @throws NotEnabledException
260 * @throws NotHandledException
261 */
d132bcc7 262 public void executeCommand(String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
346fa221
MAL
263 Object handlerServiceObject = fControlView.getSite().getService(IHandlerService.class);
264 IHandlerService handlerService = (IHandlerService) handlerServiceObject;
d132bcc7 265 handlerService.executeCommand(COMMAND_CATEGORY_PREFIX + commandId, null);
a26d90be
BH
266 waitForJobs();
267 }
268
269 /**
270 * Selects passed component
271 * @param component - component to select in the tree
272 * @param commandId - command ID
273 */
274 public void setSelection(ITraceControlComponent component) {
275 fControlView.setSelection(component);
b957fb8c
BH
276 // Selection is done in own job
277 waitForJobs();
d132bcc7
BH
278 }
279
cfdb727a 280
a26d90be
BH
281 /**
282 * Selects passed components
283 * @param components - array of component to select in the tree
284 * @param commandId - command ID
285 */
286 public void setSelection(ITraceControlComponent[] components) {
287 fControlView.setSelection(components);
288
b957fb8c
BH
289 // Selection is done in own job
290 waitForJobs();
a26d90be 291 }
cfdb727a 292
a26d90be
BH
293 /**
294 * Creates session on passed session group.
295 * @param group - session group
296 * @return - trace session group if it's successful else null
297 * @throws ExecutionException
298 * @throws NotDefinedException
299 * @throws NotEnabledException
300 * @throws NotHandledException
301 */
a26d90be
BH
302 public TraceSessionComponent createSession(ITraceControlComponent group) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
303 executeCommand(group, "createSession");
cfdb727a 304
a26d90be
BH
305 ITraceControlComponent[] sessions = group.getChildren();
306 if ((sessions == null) || (sessions.length == 0)) {
307 return null;
308 }
309 return (TraceSessionComponent)sessions[0];
310 }
cfdb727a 311
a26d90be
BH
312 /**
313 * Destroys a given session.
314 * @param session - session to destroy
315 * @throws ExecutionException
316 * @throws NotDefinedException
317 * @throws NotEnabledException
318 * @throws NotHandledException
319 */
a26d90be
BH
320 public void destroySession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
321 executeCommand(session, "destroySession");
322 }
cfdb727a 323
a26d90be
BH
324 /**
325 * Starts a given session
326 * @param session - session to start
327 * @throws ExecutionException
328 * @throws NotDefinedException
329 * @throws NotEnabledException
330 * @throws NotHandledException
331 */
a26d90be
BH
332 public void startSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
333 executeCommand(session, "start");
334 }
cfdb727a 335
a26d90be
BH
336 /**
337 * Stops a given session
338 * @param session - session to stop
339 * @throws ExecutionException
340 * @throws NotDefinedException
341 * @throws NotEnabledException
342 * @throws NotHandledException
343 */
a26d90be
BH
344 public void stopSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
345 executeCommand(session, "stop");
346 }
eb1bab5b 347}
This page took 0.091703 seconds and 5 git commands to generate.