7b6e5ebeaaec13eefb6ad4b02f384db1df89de42
[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 static org.junit.Assert.assertNotNull;
16
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.commands.NotEnabledException;
19 import org.eclipse.core.commands.NotHandledException;
20 import org.eclipse.core.commands.common.NotDefinedException;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.tracecompass.internal.lttng2.control.core.model.TargetNodeState;
23 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
24 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
25 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent;
26 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
27 import org.eclipse.tracecompass.tmf.ui.tests.shared.JobUtils;
28 import org.eclipse.ui.IViewPart;
29 import org.eclipse.ui.PartInitException;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.handlers.IHandlerService;
32 import org.junit.Assert;
33
34 /**
35 * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides
36 * utility methods for interacting with the loader/view.
37 */
38 @SuppressWarnings("javadoc")
39 public class TraceControlTestFacility {
40
41 // ------------------------------------------------------------------------
42 // Constants
43 // ------------------------------------------------------------------------
44 public final static int WAIT_FOR_JOBS_DELAY = 50;
45 public final static int GUI_REFESH_DELAY = 500;
46
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";
52
53 // ------------------------------------------------------------------------
54 // Attributes
55 // ------------------------------------------------------------------------
56 private static TraceControlTestFacility fInstance = null;
57 private ControlView fControlView = null;
58 private volatile boolean fIsInitialized = false;
59
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() {
80
81 if (!fIsInitialized) {
82 IViewPart view;
83 try {
84 hideView("org.eclipse.ui.internal.introview");
85 view = showView(ControlView.ID);
86 } catch (PartInitException e) {
87 throw new RuntimeException(e);
88 }
89 fControlView = (ControlView) view;
90
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
105 fIsInitialized = true;
106 }
107 }
108
109 /**
110 * Disposes the facility (and GUI)
111 */
112 public void dispose() {
113 if (fIsInitialized) {
114 waitForJobs();
115 hideView(ControlView.ID);
116 delay(200);
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 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
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
181 /**
182 * Waits for all Eclipse jobs to finish
183 */
184 public void waitForJobs() {
185 JobUtils.waitForJobs();
186 }
187
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
219 /**
220 * @return current control view
221 */
222 public ControlView getControlView() {
223 return fControlView;
224 }
225
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 }
239
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 */
262 public void executeCommand(String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
263 Object handlerServiceObject = fControlView.getSite().getService(IHandlerService.class);
264 IHandlerService handlerService = (IHandlerService) handlerServiceObject;
265 handlerService.executeCommand(COMMAND_CATEGORY_PREFIX + commandId, null);
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);
276 // Selection is done in own job
277 waitForJobs();
278 }
279
280
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
289 // Selection is done in own job
290 waitForJobs();
291 }
292
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 */
302 public TraceSessionComponent createSession(ITraceControlComponent group) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
303 executeCommand(group, "createSession");
304
305 ITraceControlComponent[] sessions = group.getChildren();
306 if ((sessions == null) || (sessions.length == 0)) {
307 return null;
308 }
309 return (TraceSessionComponent)sessions[0];
310 }
311
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 */
320 public void destroySession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
321 executeCommand(session, "destroySession");
322 }
323
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 */
332 public void startSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
333 executeCommand(session, "start");
334 }
335
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 */
344 public void stopSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
345 executeCommand(session, "stop");
346 }
347 }
This page took 0.043738 seconds and 4 git commands to generate.