gdbtrace: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.control.ui.tests / src / org / eclipse / linuxtools / lttng2 / control / ui / tests / model / component / TraceControlTestFacility.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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
13 package org.eclipse.linuxtools.lttng2.control.ui.tests.model.component;
14
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.NotEnabledException;
17 import org.eclipse.core.commands.NotHandledException;
18 import org.eclipse.core.commands.common.NotDefinedException;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.ControlView;
21 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.ITraceControlComponent;
22 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.ui.IViewPart;
25 import org.eclipse.ui.PartInitException;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.handlers.IHandlerService;
28
29 /**
30 * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides
31 * utility methods for interacting with the loader/view.
32 */
33 @SuppressWarnings("javadoc")
34 public class TraceControlTestFacility {
35
36 // ------------------------------------------------------------------------
37 // Constants
38 // ------------------------------------------------------------------------
39 public final static int WAIT_FOR_JOBS_DELAY = 50;
40 public final static int GUI_REFESH_DELAY = 500;
41
42 public final static String DIRECTORY = "testfiles";
43 public final static String COMMAND_CATEGORY_PREFIX = "org.eclipse.linuxtools.internal.lttng2.ui.commands.control.";
44 public final static String SCEN_INIT_TEST = "Initialize";
45 public final static String SCEN_SCENARIO_SESSION_HANDLING = "SessionHandling";
46 public final static String SCEN_SCENARIO_SESSION_HANDLING_WITH_PATH = "SessionHandlingWithPath";
47
48 // ------------------------------------------------------------------------
49 // Attributes
50 // ------------------------------------------------------------------------
51 private static TraceControlTestFacility fInstance = null;
52 private ControlView fControlView = null;
53 private boolean fIsInitialized = false;
54
55 // ------------------------------------------------------------------------
56 // Constructors
57 // ------------------------------------------------------------------------
58 private TraceControlTestFacility() {
59 }
60
61 // ------------------------------------------------------------------------
62 // Operations
63 // ------------------------------------------------------------------------
64 public static TraceControlTestFacility getInstance() {
65 if (fInstance == null) {
66 fInstance = new TraceControlTestFacility();
67 }
68 return fInstance;
69 }
70
71 /**
72 * Initial the test facility.
73 */
74 public void init() {
75
76 if (!fIsInitialized) {
77
78 IViewPart view;
79 try {
80
81 view = PlatformUI.getWorkbench()
82 .getActiveWorkbenchWindow()
83 .getActivePage()
84 .findView("org.eclipse.ui.internal.introview");
85
86 if (view != null) {
87 PlatformUI.getWorkbench()
88 .getActiveWorkbenchWindow()
89 .getActivePage().hideView(view);
90 }
91
92 view = PlatformUI.getWorkbench()
93 .getActiveWorkbenchWindow()
94 .getActivePage()
95 .showView(ControlView.ID);
96
97 } catch (PartInitException e) {
98 throw new RuntimeException(e);
99 }
100
101 fControlView = (ControlView) view;
102
103 delay(3000);
104 fIsInitialized = true;
105 }
106 }
107
108
109 /**
110 * Disposes the facility (and GUI)
111 */
112 public void dispose() {
113 if (fIsInitialized) {
114 waitForJobs();
115
116 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fControlView);
117 fIsInitialized = false;
118 }
119 }
120
121 /**
122 * Creates a delay for given time.
123 * @param waitTimeMillis - time in milli seconds
124 */
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()) {
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
137 }
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
159 /**
160 * @return current control view
161 */
162 public ControlView getControlView() {
163 return fControlView;
164 }
165
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 }
179
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 */
202 public void executeCommand(String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
203 IHandlerService handlerService = (IHandlerService) fControlView.getSite().getService(IHandlerService.class);
204 handlerService.executeCommand(COMMAND_CATEGORY_PREFIX + commandId, null);
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);
215 // Selection is done in own job
216 waitForJobs();
217 }
218
219
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
228 // Selection is done in own job
229 waitForJobs();
230 }
231
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 */
241 public TraceSessionComponent createSession(ITraceControlComponent group) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
242 executeCommand(group, "createSession");
243
244 ITraceControlComponent[] sessions = group.getChildren();
245 if ((sessions == null) || (sessions.length == 0)) {
246 return null;
247 }
248 return (TraceSessionComponent)sessions[0];
249 }
250
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 */
259 public void destroySession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
260 executeCommand(session, "destroySession");
261 }
262
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 */
271 public void startSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
272 executeCommand(session, "start");
273 }
274
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 */
283 public void stopSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
284 executeCommand(session, "stop");
285 }
286 }
This page took 0.039025 seconds and 5 git commands to generate.