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