0eaef20b5e81a6075f5775102e5b68435bc3af2a
[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.core.runtime.jobs.Job;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.tracecompass.internal.lttng2.control.core.model.TargetNodeState;
24 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
25 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
26 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent;
27 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
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 while (!Job.getJobManager().isIdle()) {
186 delay(WAIT_FOR_JOBS_DELAY);
187 }
188 }
189
190 private IViewPart showView(String viewId) throws PartInitException {
191 IViewPart view = getViewPart(viewId);
192
193 if (view == null) {
194 view = PlatformUI.getWorkbench()
195 .getActiveWorkbenchWindow()
196 .getActivePage().showView(viewId);
197
198 waitForViewOpend(viewId);
199 }
200 assertNotNull(view);
201 return view;
202 }
203
204 private void hideView(String viewId) {
205 IViewPart view = getViewPart(viewId);
206 if (view != null) {
207 PlatformUI.getWorkbench()
208 .getActiveWorkbenchWindow()
209 .getActivePage().hideView(view);
210 }
211 waitForViewClosed(viewId);
212 }
213
214 private static IViewPart getViewPart(String viewId) {
215 return PlatformUI.getWorkbench()
216 .getActiveWorkbenchWindow()
217 .getActivePage()
218 .findView(viewId);
219 }
220
221 /**
222 * @return current control view
223 */
224 public ControlView getControlView() {
225 return fControlView;
226 }
227
228 /**
229 * Executes an Eclipse command with command ID after selecting passed component
230 * @param component - component to select in the tree
231 * @param commandId - command ID
232 * @throws ExecutionException
233 * @throws NotDefinedException
234 * @throws NotEnabledException
235 * @throws NotHandledException
236 */
237 public void executeCommand(ITraceControlComponent component, String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
238 setSelection(component);
239 executeCommand(commandId);
240 }
241
242 /**
243 * Executes an Eclipse command with command ID after selecting passed components
244 * @param components - array of components to select in the tree
245 * @param commandId - command ID
246 * @throws ExecutionException
247 * @throws NotDefinedException
248 * @throws NotEnabledException
249 * @throws NotHandledException
250 */
251 public void executeCommand(ITraceControlComponent[] components, String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
252 setSelection(components);
253 executeCommand(commandId);
254 }
255
256 /**
257 * Executes an Eclipse command with command ID
258 * @param commandId
259 * @throws ExecutionException
260 * @throws NotDefinedException
261 * @throws NotEnabledException
262 * @throws NotHandledException
263 */
264 public void executeCommand(String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
265 Object handlerServiceObject = fControlView.getSite().getService(IHandlerService.class);
266 IHandlerService handlerService = (IHandlerService) handlerServiceObject;
267 handlerService.executeCommand(COMMAND_CATEGORY_PREFIX + commandId, null);
268 waitForJobs();
269 }
270
271 /**
272 * Selects passed component
273 * @param component - component to select in the tree
274 * @param commandId - command ID
275 */
276 public void setSelection(ITraceControlComponent component) {
277 fControlView.setSelection(component);
278 // Selection is done in own job
279 waitForJobs();
280 }
281
282
283 /**
284 * Selects passed components
285 * @param components - array of component to select in the tree
286 * @param commandId - command ID
287 */
288 public void setSelection(ITraceControlComponent[] components) {
289 fControlView.setSelection(components);
290
291 // Selection is done in own job
292 waitForJobs();
293 }
294
295 /**
296 * Creates session on passed session group.
297 * @param group - session group
298 * @return - trace session group if it's successful else null
299 * @throws ExecutionException
300 * @throws NotDefinedException
301 * @throws NotEnabledException
302 * @throws NotHandledException
303 */
304 public TraceSessionComponent createSession(ITraceControlComponent group) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
305 executeCommand(group, "createSession");
306
307 ITraceControlComponent[] sessions = group.getChildren();
308 if ((sessions == null) || (sessions.length == 0)) {
309 return null;
310 }
311 return (TraceSessionComponent)sessions[0];
312 }
313
314 /**
315 * Destroys a given session.
316 * @param session - session to destroy
317 * @throws ExecutionException
318 * @throws NotDefinedException
319 * @throws NotEnabledException
320 * @throws NotHandledException
321 */
322 public void destroySession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
323 executeCommand(session, "destroySession");
324 }
325
326 /**
327 * Starts a given session
328 * @param session - session to start
329 * @throws ExecutionException
330 * @throws NotDefinedException
331 * @throws NotEnabledException
332 * @throws NotHandledException
333 */
334 public void startSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
335 executeCommand(session, "start");
336 }
337
338 /**
339 * Stops a given session
340 * @param session - session to stop
341 * @throws ExecutionException
342 * @throws NotDefinedException
343 * @throws NotEnabledException
344 * @throws NotHandledException
345 */
346 public void stopSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
347 executeCommand(session, "stop");
348 }
349 }
This page took 0.038107 seconds and 4 git commands to generate.