1 /*******************************************************************************
2 * Copyright (c) 2011 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 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.lttng2
.ui
.tests
.control
.model
.component
;
14 import org
.eclipse
.core
.commands
.ExecutionException
;
15 import org
.eclipse
.core
.commands
.NotEnabledException
;
16 import org
.eclipse
.core
.commands
.NotHandledException
;
17 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
18 import org
.eclipse
.core
.runtime
.jobs
.Job
;
19 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.ControlView
;
20 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.model
.ITraceControlComponent
;
21 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.model
.impl
.TraceSessionComponent
;
22 import org
.eclipse
.swt
.widgets
.Display
;
23 import org
.eclipse
.ui
.IViewPart
;
24 import org
.eclipse
.ui
.PartInitException
;
25 import org
.eclipse
.ui
.PlatformUI
;
26 import org
.eclipse
.ui
.handlers
.IHandlerService
;
29 * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides
30 * utility methods for interacting with the loader/view.
32 public class TraceControlTestFacility
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
37 public final static int WAIT_FOR_JOBS_DELAY
= 1000;
38 public final static int GUI_REFESH_DELAY
= 500;
40 public final static String DIRECTORY
= "testfiles"; //$NON-NLS-1$
41 public final static String COMMAND_CATEGORY_PREFIX
= "org.eclipse.linuxtools.internal.lttng2.ui.commands.control."; //$NON-NLS-1$
42 public final static String SCEN_INIT_TEST
= "Initialize"; //$NON-NLS-1$
43 public final static String SCEN_SCENARIO_SESSION_HANDLING
= "SessionHandling"; //$NON-NLS-1$
44 public final static String SCEN_SCENARIO_SESSION_HANDLING_WITH_PATH
= "SessionHandlingWithPath"; //$NON-NLS-1$
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
49 private static TraceControlTestFacility fInstance
= null;
50 private ControlView fControlView
= null;
51 private boolean fIsInitialized
= false;
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
56 private TraceControlTestFacility() {
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
62 public static TraceControlTestFacility
getInstance() {
63 if (fInstance
== null) {
64 fInstance
= new TraceControlTestFacility();
70 * Initial the test facility.
74 if (!fIsInitialized
) {
78 view
= PlatformUI
.getWorkbench()
79 .getActiveWorkbenchWindow()
81 .showView(ControlView
.ID
);
83 } catch (PartInitException e
) {
84 throw new RuntimeException(e
);
87 fControlView
= (ControlView
) view
;
90 fIsInitialized
= true;
96 * Disposes the facility (and GUI)
98 public void dispose() {
102 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fControlView
);
103 fIsInitialized
= false;
108 * Creates a delay for given time.
109 * @param waitTimeMillis - time in milli seconds
111 public void delay(long waitTimeMillis
) {
112 Display display
= Display
.getCurrent();
113 if (display
!= null) {
114 long endTimeMillis
= System
.currentTimeMillis() + waitTimeMillis
;
115 while(System
.currentTimeMillis() < endTimeMillis
) {
116 if (!display
.readAndDispatch()) {
123 Thread
.sleep(waitTimeMillis
);
124 } catch (InterruptedException e
) {
131 * Waits for all Eclipse jobs to finish
133 public void waitForJobs() {
134 while (!Job
.getJobManager().isIdle()) {
135 delay(WAIT_FOR_JOBS_DELAY
);
140 * @return current control view
142 public ControlView
getControlView() {
147 * Executes an Eclipse command with command ID after selecting passed component
148 * @param component - component to select in the tree
149 * @param commandId - command ID
150 * @throws ExecutionException
151 * @throws NotDefinedException
152 * @throws NotEnabledException
153 * @throws NotHandledException
155 public void executeCommand(ITraceControlComponent component
, String commandId
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
156 setSelection(component
);
157 executeCommand(commandId
);
161 * Executes an Eclipse command with command ID after selecting passed components
162 * @param components - array of components to select in the tree
163 * @param commandId - command ID
164 * @throws ExecutionException
165 * @throws NotDefinedException
166 * @throws NotEnabledException
167 * @throws NotHandledException
169 public void executeCommand(ITraceControlComponent
[] components
, String commandId
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
170 setSelection(components
);
171 executeCommand(commandId
);
175 * Executes an Eclipse command with command ID
177 * @throws ExecutionException
178 * @throws NotDefinedException
179 * @throws NotEnabledException
180 * @throws NotHandledException
182 public void executeCommand(String commandId
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
183 IHandlerService handlerService
= (IHandlerService
) fControlView
.getSite().getService(IHandlerService
.class);
184 handlerService
.executeCommand(COMMAND_CATEGORY_PREFIX
+ commandId
, null);
189 * Selects passed component
190 * @param component - component to select in the tree
191 * @param commandId - command ID
193 public void setSelection(ITraceControlComponent component
) {
194 fControlView
.setSelection(component
);
196 // Give GUI time to actually execute refresh
197 delay(TraceControlTestFacility
.GUI_REFESH_DELAY
);
202 * Selects passed components
203 * @param components - array of component to select in the tree
204 * @param commandId - command ID
206 public void setSelection(ITraceControlComponent
[] components
) {
207 fControlView
.setSelection(components
);
209 // Give GUI time to actually execute refresh
210 delay(TraceControlTestFacility
.GUI_REFESH_DELAY
);
214 * Creates session on passed session group.
215 * @param group - session group
216 * @return - trace session group if it's successful else null
217 * @throws ExecutionException
218 * @throws NotDefinedException
219 * @throws NotEnabledException
220 * @throws NotHandledException
222 @SuppressWarnings("nls")
223 public TraceSessionComponent
createSession(ITraceControlComponent group
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
224 executeCommand(group
, "createSession");
226 ITraceControlComponent
[] sessions
= group
.getChildren();
227 if ((sessions
== null) || (sessions
.length
== 0)) {
230 return (TraceSessionComponent
)sessions
[0];
234 * Destroys a given session.
235 * @param session - session to destroy
236 * @throws ExecutionException
237 * @throws NotDefinedException
238 * @throws NotEnabledException
239 * @throws NotHandledException
241 @SuppressWarnings("nls")
242 public void destroySession(TraceSessionComponent session
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
243 executeCommand(session
, "destroySession");
247 * Starts a given session
248 * @param session - session to start
249 * @throws ExecutionException
250 * @throws NotDefinedException
251 * @throws NotEnabledException
252 * @throws NotHandledException
254 @SuppressWarnings("nls")
255 public void startSession(TraceSessionComponent session
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
256 executeCommand(session
, "start");
260 * Stops a given session
261 * @param session - session to stop
262 * @throws ExecutionException
263 * @throws NotDefinedException
264 * @throws NotEnabledException
265 * @throws NotHandledException
267 @SuppressWarnings("nls")
268 public void stopSession(TraceSessionComponent session
) throws ExecutionException
, NotDefinedException
, NotEnabledException
, NotHandledException
{
269 executeCommand(session
, "stop");