lttng: Work around UI thread sleeping forever on Mac in tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui.tests / src / org / eclipse / linuxtools / lttng2 / ui / tests / control / model / component / TraceControlTestFacility.java
CommitLineData
eb1bab5b 1/*******************************************************************************
94cce698 2 * Copyright (c) 2011, 2013 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
115b4a01 13package org.eclipse.linuxtools.lttng2.ui.tests.control.model.component;
eb1bab5b 14
d132bcc7
BH
15import org.eclipse.core.commands.ExecutionException;
16import org.eclipse.core.commands.NotEnabledException;
17import org.eclipse.core.commands.NotHandledException;
18import org.eclipse.core.commands.common.NotDefinedException;
eb1bab5b 19import org.eclipse.core.runtime.jobs.Job;
115b4a01 20import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
a26d90be
BH
21import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent;
22import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent;
b07022a9 23import org.eclipse.swt.SWT;
eb1bab5b
BH
24import org.eclipse.swt.widgets.Display;
25import org.eclipse.ui.IViewPart;
26import org.eclipse.ui.PartInitException;
27import org.eclipse.ui.PlatformUI;
d132bcc7 28import org.eclipse.ui.handlers.IHandlerService;
eb1bab5b
BH
29
30/**
31 * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides
cfdb727a 32 * utility methods for interacting with the loader/view.
eb1bab5b 33 */
cfdb727a 34@SuppressWarnings("javadoc")
eb1bab5b
BH
35public class TraceControlTestFacility {
36
37 // ------------------------------------------------------------------------
38 // Constants
39 // ------------------------------------------------------------------------
22a0f8f8 40 public final static int WAIT_FOR_JOBS_DELAY = 50;
a26d90be 41 public final static int GUI_REFESH_DELAY = 500;
cfdb727a 42
4e0b52e0
AM
43 public final static String DIRECTORY = "testfiles";
44 public final static String COMMAND_CATEGORY_PREFIX = "org.eclipse.linuxtools.internal.lttng2.ui.commands.control.";
45 public final static String SCEN_INIT_TEST = "Initialize";
46 public final static String SCEN_SCENARIO_SESSION_HANDLING = "SessionHandling";
47 public final static String SCEN_SCENARIO_SESSION_HANDLING_WITH_PATH = "SessionHandlingWithPath";
cfdb727a 48
eb1bab5b
BH
49 // ------------------------------------------------------------------------
50 // Attributes
51 // ------------------------------------------------------------------------
52 private static TraceControlTestFacility fInstance = null;
53 private ControlView fControlView = null;
54 private boolean fIsInitialized = false;
cfdb727a 55
eb1bab5b
BH
56 // ------------------------------------------------------------------------
57 // Constructors
58 // ------------------------------------------------------------------------
59 private TraceControlTestFacility() {
60 }
61
62 // ------------------------------------------------------------------------
63 // Operations
64 // ------------------------------------------------------------------------
65 public static TraceControlTestFacility getInstance() {
66 if (fInstance == null) {
67 fInstance = new TraceControlTestFacility();
68 }
69 return fInstance;
70 }
71
72 /**
73 * Initial the test facility.
74 */
75 public void init() {
cfdb727a 76
eb1bab5b
BH
77 if (!fIsInitialized) {
78
79 IViewPart view;
80 try {
cfdb727a 81
b957fb8c
BH
82 view = PlatformUI.getWorkbench()
83 .getActiveWorkbenchWindow()
84 .getActivePage()
4e0b52e0 85 .findView("org.eclipse.ui.internal.introview");
cfdb727a 86
b957fb8c
BH
87 if (view != null) {
88 PlatformUI.getWorkbench()
89 .getActiveWorkbenchWindow()
cfdb727a 90 .getActivePage().hideView(view);
b957fb8c
BH
91 }
92
eb1bab5b
BH
93 view = PlatformUI.getWorkbench()
94 .getActiveWorkbenchWindow()
95 .getActivePage()
cfdb727a
AM
96 .showView(ControlView.ID);
97
eb1bab5b
BH
98 } catch (PartInitException e) {
99 throw new RuntimeException(e);
100 }
101
102 fControlView = (ControlView) view;
103
104 delay(3000);
105 fIsInitialized = true;
106 }
107 }
108
cfdb727a 109
a26d90be
BH
110 /**
111 * Disposes the facility (and GUI)
112 */
eb1bab5b
BH
113 public void dispose() {
114 if (fIsInitialized) {
115 waitForJobs();
116
117 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fControlView);
118 fIsInitialized = false;
119 }
120 }
a26d90be
BH
121
122 /**
123 * Creates a delay for given time.
124 * @param waitTimeMillis - time in milli seconds
125 */
eb1bab5b
BH
126 public void delay(long waitTimeMillis) {
127 Display display = Display.getCurrent();
128 if (display != null) {
129 long endTimeMillis = System.currentTimeMillis() + waitTimeMillis;
130 while(System.currentTimeMillis() < endTimeMillis) {
131 if (!display.readAndDispatch()) {
b07022a9
MAL
132 if ("cocoa".equals (SWT.getPlatform ())) {
133 // The display needs to be woken up because it's possible
134 // to get in a state where nothing will wake up the UI thread
135 display.asyncExec(null);
136 }
eb1bab5b
BH
137 display.sleep();
138 }
139 display.update();
140 }
141 } else {
142 try {
143 Thread.sleep(waitTimeMillis);
144 } catch (InterruptedException e) {
145 // Ignored
146 }
147 }
148 }
149
150 /**
151 * Waits for all Eclipse jobs to finish
152 */
153 public void waitForJobs() {
154 while (!Job.getJobManager().isIdle()) {
155 delay(WAIT_FOR_JOBS_DELAY);
156 }
157 }
158
eb1bab5b
BH
159 /**
160 * @return current control view
161 */
162 public ControlView getControlView() {
163 return fControlView;
164 }
d132bcc7 165
a26d90be
BH
166 /**
167 * Executes an Eclipse command with command ID after selecting passed component
168 * @param component - component to select in the tree
169 * @param commandId - command ID
170 * @throws ExecutionException
171 * @throws NotDefinedException
172 * @throws NotEnabledException
173 * @throws NotHandledException
174 */
175 public void executeCommand(ITraceControlComponent component, String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
176 setSelection(component);
177 executeCommand(commandId);
178 }
cfdb727a 179
a26d90be
BH
180 /**
181 * Executes an Eclipse command with command ID after selecting passed components
182 * @param components - array of components to select in the tree
183 * @param commandId - command ID
184 * @throws ExecutionException
185 * @throws NotDefinedException
186 * @throws NotEnabledException
187 * @throws NotHandledException
188 */
189 public void executeCommand(ITraceControlComponent[] components, String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
190 setSelection(components);
191 executeCommand(commandId);
192 }
193
194 /**
195 * Executes an Eclipse command with command ID
196 * @param commandId
197 * @throws ExecutionException
198 * @throws NotDefinedException
199 * @throws NotEnabledException
200 * @throws NotHandledException
201 */
d132bcc7 202 public void executeCommand(String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
d132bcc7
BH
203 IHandlerService handlerService = (IHandlerService) fControlView.getSite().getService(IHandlerService.class);
204 handlerService.executeCommand(COMMAND_CATEGORY_PREFIX + commandId, null);
a26d90be
BH
205 waitForJobs();
206 }
207
208 /**
209 * Selects passed component
210 * @param component - component to select in the tree
211 * @param commandId - command ID
212 */
213 public void setSelection(ITraceControlComponent component) {
214 fControlView.setSelection(component);
b957fb8c
BH
215 // Selection is done in own job
216 waitForJobs();
d132bcc7
BH
217 }
218
cfdb727a 219
a26d90be
BH
220 /**
221 * Selects passed components
222 * @param components - array of component to select in the tree
223 * @param commandId - command ID
224 */
225 public void setSelection(ITraceControlComponent[] components) {
226 fControlView.setSelection(components);
227
b957fb8c
BH
228 // Selection is done in own job
229 waitForJobs();
a26d90be 230 }
cfdb727a 231
a26d90be
BH
232 /**
233 * Creates session on passed session group.
234 * @param group - session group
235 * @return - trace session group if it's successful else null
236 * @throws ExecutionException
237 * @throws NotDefinedException
238 * @throws NotEnabledException
239 * @throws NotHandledException
240 */
a26d90be
BH
241 public TraceSessionComponent createSession(ITraceControlComponent group) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
242 executeCommand(group, "createSession");
cfdb727a 243
a26d90be
BH
244 ITraceControlComponent[] sessions = group.getChildren();
245 if ((sessions == null) || (sessions.length == 0)) {
246 return null;
247 }
248 return (TraceSessionComponent)sessions[0];
249 }
cfdb727a 250
a26d90be
BH
251 /**
252 * Destroys a given session.
253 * @param session - session to destroy
254 * @throws ExecutionException
255 * @throws NotDefinedException
256 * @throws NotEnabledException
257 * @throws NotHandledException
258 */
a26d90be
BH
259 public void destroySession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
260 executeCommand(session, "destroySession");
261 }
cfdb727a 262
a26d90be
BH
263 /**
264 * Starts a given session
265 * @param session - session to start
266 * @throws ExecutionException
267 * @throws NotDefinedException
268 * @throws NotEnabledException
269 * @throws NotHandledException
270 */
a26d90be
BH
271 public void startSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
272 executeCommand(session, "start");
273 }
cfdb727a 274
a26d90be
BH
275 /**
276 * Stops a given session
277 * @param session - session to stop
278 * @throws ExecutionException
279 * @throws NotDefinedException
280 * @throws NotEnabledException
281 * @throws NotHandledException
282 */
a26d90be
BH
283 public void stopSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
284 executeCommand(session, "stop");
285 }
eb1bab5b 286}
This page took 0.05017 seconds and 5 git commands to generate.