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 static org
.junit
.Assert
.assertNotNull
;
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
;
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.
38 @SuppressWarnings("javadoc")
39 public class TraceControlTestFacility
{
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
44 public final static int WAIT_FOR_JOBS_DELAY
= 50;
45 public final static int GUI_REFESH_DELAY
= 500;
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";
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
56 private static TraceControlTestFacility fInstance
= null;
57 private ControlView fControlView
= null;
58 private volatile boolean fIsInitialized
= false;
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
63 private TraceControlTestFacility() {
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
69 public static TraceControlTestFacility
getInstance() {
70 if (fInstance
== null) {
71 fInstance
= new TraceControlTestFacility();
77 * Initial the test facility.
81 if (!fIsInitialized
) {
84 hideView("org.eclipse.ui.internal.introview");
85 view
= showView(ControlView
.ID
);
86 } catch (PartInitException e
) {
87 throw new RuntimeException(e
);
89 fControlView
= (ControlView
) view
;
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()).
97 * When showing the view above ControlView.init(IMemento) is
98 * called which restores saved connections.
100 * The tests require that the ControlView is empty. So
101 * we remove all the connection nodes from the root.
103 fControlView
.getTraceControlRoot().removeAllChildren();
105 fIsInitialized
= true;
110 * Disposes the facility (and GUI)
112 public void dispose() {
113 if (fIsInitialized
) {
115 hideView(ControlView
.ID
);
117 fIsInitialized
= false;
122 * Creates a delay for given time.
123 * @param waitTimeMillis - time in milli seconds
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
134 Thread
.sleep(Math
.min(waitTimeMillis
, 10));
135 } catch (final InterruptedException e
) {
143 Thread
.sleep(waitTimeMillis
);
144 } catch (InterruptedException e
) {
151 * Waits for a connection to be connected
153 public void waitForConnect(TargetNodeComponent node
) {
154 for (int i
= 1; i
< 5000 && node
.getTargetNodeState() == TargetNodeState
.CONNECTING
; i
*= 2) {
157 } catch (InterruptedException e
) {
164 * Waits for a view to be closed
166 public void waitForViewClosed(String viewId
) {
167 for (int i
= 1; i
< 5000 && (getViewPart(viewId
) != null); i
*= 2) {
173 * Waits for a view to be closed
175 public void waitForViewOpend(String viewId
) {
176 for (int i
= 1; i
< 5000 && (getViewPart(viewId
) == null); i
*= 2) {
182 * Waits for all Eclipse jobs to finish
184 public void waitForJobs() {
185 while (!Job
.getJobManager().isIdle()) {
186 delay(WAIT_FOR_JOBS_DELAY
);
190 private IViewPart
showView(String viewId
) throws PartInitException
{
191 IViewPart view
= getViewPart(viewId
);
194 view
= PlatformUI
.getWorkbench()
195 .getActiveWorkbenchWindow()
196 .getActivePage().showView(viewId
);
198 waitForViewOpend(viewId
);
204 private void hideView(String viewId
) {
205 IViewPart view
= getViewPart(viewId
);
207 PlatformUI
.getWorkbench()
208 .getActiveWorkbenchWindow()
209 .getActivePage().hideView(view
);
211 waitForViewClosed(viewId
);
214 private static IViewPart
getViewPart(String viewId
) {
215 return PlatformUI
.getWorkbench()
216 .getActiveWorkbenchWindow()
222 * @return current control view
224 public ControlView
getControlView() {
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
237 public void executeCommand(ITraceControlComponent component
, String commandId
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
238 setSelection(component
);
239 executeCommand(commandId
);
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
251 public void executeCommand(ITraceControlComponent
[] components
, String commandId
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
252 setSelection(components
);
253 executeCommand(commandId
);
257 * Executes an Eclipse command with command ID
259 * @throws ExecutionException
260 * @throws NotDefinedException
261 * @throws NotEnabledException
262 * @throws NotHandledException
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);
272 * Selects passed component
273 * @param component - component to select in the tree
274 * @param commandId - command ID
276 public void setSelection(ITraceControlComponent component
) {
277 fControlView
.setSelection(component
);
278 // Selection is done in own job
284 * Selects passed components
285 * @param components - array of component to select in the tree
286 * @param commandId - command ID
288 public void setSelection(ITraceControlComponent
[] components
) {
289 fControlView
.setSelection(components
);
291 // Selection is done in own job
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
304 public TraceSessionComponent
createSession(ITraceControlComponent group
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
305 executeCommand(group
, "createSession");
307 ITraceControlComponent
[] sessions
= group
.getChildren();
308 if ((sessions
== null) || (sessions
.length
== 0)) {
311 return (TraceSessionComponent
)sessions
[0];
315 * Destroys a given session.
316 * @param session - session to destroy
317 * @throws ExecutionException
318 * @throws NotDefinedException
319 * @throws NotEnabledException
320 * @throws NotHandledException
322 public void destroySession(TraceSessionComponent session
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
323 executeCommand(session
, "destroySession");
327 * Starts a given session
328 * @param session - session to start
329 * @throws ExecutionException
330 * @throws NotDefinedException
331 * @throws NotEnabledException
332 * @throws NotHandledException
334 public void startSession(TraceSessionComponent session
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
335 executeCommand(session
, "start");
339 * Stops a given session
340 * @param session - session to stop
341 * @throws ExecutionException
342 * @throws NotDefinedException
343 * @throws NotEnabledException
344 * @throws NotHandledException
346 public void stopSession(TraceSessionComponent session
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
347 executeCommand(session
, "stop");