1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.lttng2
.control
.ui
.tests
.model
.component
;
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
;
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.
36 @SuppressWarnings("javadoc")
37 public class TraceControlTestFacility
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
42 public final static int WAIT_FOR_JOBS_DELAY
= 50;
43 public final static int GUI_REFESH_DELAY
= 500;
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";
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
54 private static TraceControlTestFacility fInstance
= null;
55 private ControlView fControlView
= null;
56 private boolean fIsInitialized
= false;
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
61 private TraceControlTestFacility() {
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
67 public static TraceControlTestFacility
getInstance() {
68 if (fInstance
== null) {
69 fInstance
= new TraceControlTestFacility();
75 * Initial the test facility.
79 if (!fIsInitialized
) {
84 view
= PlatformUI
.getWorkbench()
85 .getActiveWorkbenchWindow()
87 .findView("org.eclipse.ui.internal.introview");
90 PlatformUI
.getWorkbench()
91 .getActiveWorkbenchWindow()
92 .getActivePage().hideView(view
);
95 view
= PlatformUI
.getWorkbench()
96 .getActiveWorkbenchWindow()
98 .showView(ControlView
.ID
);
100 } catch (PartInitException e
) {
101 throw new RuntimeException(e
);
104 fControlView
= (ControlView
) view
;
107 fIsInitialized
= true;
113 * Disposes the facility (and GUI)
115 public void dispose() {
116 if (fIsInitialized
) {
119 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fControlView
);
120 fIsInitialized
= false;
125 * Creates a delay for given time.
126 * @param waitTimeMillis - time in milli seconds
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
137 Thread
.sleep(Math
.min(waitTimeMillis
, 10));
138 } catch (final InterruptedException e
) {
146 Thread
.sleep(waitTimeMillis
);
147 } catch (InterruptedException e
) {
154 * Waits for a connection to be connected
156 public void waitForConnect(TargetNodeComponent node
) {
157 for (int i
= 1; i
< 5000 && node
.getTargetNodeState() == TargetNodeState
.CONNECTING
; i
*= 2) {
160 } catch (InterruptedException e
) {
167 * Waits for all Eclipse jobs to finish
169 public void waitForJobs() {
170 while (!Job
.getJobManager().isIdle()) {
171 delay(WAIT_FOR_JOBS_DELAY
);
176 * @return current control view
178 public ControlView
getControlView() {
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
191 public void executeCommand(ITraceControlComponent component
, String commandId
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
192 setSelection(component
);
193 executeCommand(commandId
);
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
205 public void executeCommand(ITraceControlComponent
[] components
, String commandId
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
206 setSelection(components
);
207 executeCommand(commandId
);
211 * Executes an Eclipse command with command ID
213 * @throws ExecutionException
214 * @throws NotDefinedException
215 * @throws NotEnabledException
216 * @throws NotHandledException
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);
226 * Selects passed component
227 * @param component - component to select in the tree
228 * @param commandId - command ID
230 public void setSelection(ITraceControlComponent component
) {
231 fControlView
.setSelection(component
);
232 // Selection is done in own job
238 * Selects passed components
239 * @param components - array of component to select in the tree
240 * @param commandId - command ID
242 public void setSelection(ITraceControlComponent
[] components
) {
243 fControlView
.setSelection(components
);
245 // Selection is done in own job
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
258 public TraceSessionComponent
createSession(ITraceControlComponent group
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
259 executeCommand(group
, "createSession");
261 ITraceControlComponent
[] sessions
= group
.getChildren();
262 if ((sessions
== null) || (sessions
.length
== 0)) {
265 return (TraceSessionComponent
)sessions
[0];
269 * Destroys a given session.
270 * @param session - session to destroy
271 * @throws ExecutionException
272 * @throws NotDefinedException
273 * @throws NotEnabledException
274 * @throws NotHandledException
276 public void destroySession(TraceSessionComponent session
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
277 executeCommand(session
, "destroySession");
281 * Starts a given session
282 * @param session - session to start
283 * @throws ExecutionException
284 * @throws NotDefinedException
285 * @throws NotEnabledException
286 * @throws NotHandledException
288 public void startSession(TraceSessionComponent session
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
289 executeCommand(session
, "start");
293 * Stops a given session
294 * @param session - session to stop
295 * @throws ExecutionException
296 * @throws NotDefinedException
297 * @throws NotEnabledException
298 * @throws NotHandledException
300 public void stopSession(TraceSessionComponent session
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
301 executeCommand(session
, "stop");