lttng: Port unit tests to JUnit4
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui.tests / src / org / eclipse / linuxtools / lttng2 / ui / tests / control / model / component / TraceControlTestFacility.java
1 /*******************************************************************************
2 * Copyright (c) 2011 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.linuxtools.lttng2.ui.tests.control.model.component;
14
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.linuxtools.internal.lttng2.ui.views.control.ControlView;
21 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent;
22 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.ui.IViewPart;
25 import org.eclipse.ui.PartInitException;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.handlers.IHandlerService;
28
29 /**
30 * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides
31 * utility methods for interacting with the loader/view.
32 */
33 @SuppressWarnings("javadoc")
34 public class TraceControlTestFacility {
35
36 // ------------------------------------------------------------------------
37 // Constants
38 // ------------------------------------------------------------------------
39 public final static int WAIT_FOR_JOBS_DELAY = 1000;
40 public final static int GUI_REFESH_DELAY = 500;
41
42 public final static String DIRECTORY = "testfiles"; //$NON-NLS-1$
43 public final static String COMMAND_CATEGORY_PREFIX = "org.eclipse.linuxtools.internal.lttng2.ui.commands.control."; //$NON-NLS-1$
44 public final static String SCEN_INIT_TEST = "Initialize"; //$NON-NLS-1$
45 public final static String SCEN_SCENARIO_SESSION_HANDLING = "SessionHandling"; //$NON-NLS-1$
46 public final static String SCEN_SCENARIO_SESSION_HANDLING_WITH_PATH = "SessionHandlingWithPath"; //$NON-NLS-1$
47
48 // ------------------------------------------------------------------------
49 // Attributes
50 // ------------------------------------------------------------------------
51 private static TraceControlTestFacility fInstance = null;
52 private ControlView fControlView = null;
53 private boolean fIsInitialized = false;
54
55 // ------------------------------------------------------------------------
56 // Constructors
57 // ------------------------------------------------------------------------
58 private TraceControlTestFacility() {
59 }
60
61 // ------------------------------------------------------------------------
62 // Operations
63 // ------------------------------------------------------------------------
64 public static TraceControlTestFacility getInstance() {
65 if (fInstance == null) {
66 fInstance = new TraceControlTestFacility();
67 }
68 return fInstance;
69 }
70
71 /**
72 * Initial the test facility.
73 */
74 public void init() {
75
76 if (!fIsInitialized) {
77
78 IViewPart view;
79 try {
80
81 view = PlatformUI.getWorkbench()
82 .getActiveWorkbenchWindow()
83 .getActivePage()
84 .findView("org.eclipse.ui.internal.introview"); //$NON-NLS-1$
85
86 if (view != null) {
87 PlatformUI.getWorkbench()
88 .getActiveWorkbenchWindow()
89 .getActivePage().hideView(view);
90 }
91
92 view = PlatformUI.getWorkbench()
93 .getActiveWorkbenchWindow()
94 .getActivePage()
95 .showView(ControlView.ID);
96
97 } catch (PartInitException e) {
98 throw new RuntimeException(e);
99 }
100
101 fControlView = (ControlView) view;
102
103 delay(3000);
104 fIsInitialized = true;
105 }
106 }
107
108
109 /**
110 * Disposes the facility (and GUI)
111 */
112 public void dispose() {
113 if (fIsInitialized) {
114 waitForJobs();
115
116 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fControlView);
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 display.sleep();
132 }
133 display.update();
134 }
135 } else {
136 try {
137 Thread.sleep(waitTimeMillis);
138 } catch (InterruptedException e) {
139 // Ignored
140 }
141 }
142 }
143
144 /**
145 * Waits for all Eclipse jobs to finish
146 */
147 public void waitForJobs() {
148 while (!Job.getJobManager().isIdle()) {
149 delay(WAIT_FOR_JOBS_DELAY);
150 }
151 }
152
153 /**
154 * @return current control view
155 */
156 public ControlView getControlView() {
157 return fControlView;
158 }
159
160 /**
161 * Executes an Eclipse command with command ID after selecting passed component
162 * @param component - component to select in the tree
163 * @param commandId - command ID
164 * @throws ExecutionException
165 * @throws NotDefinedException
166 * @throws NotEnabledException
167 * @throws NotHandledException
168 */
169 public void executeCommand(ITraceControlComponent component, String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
170 setSelection(component);
171 executeCommand(commandId);
172 }
173
174 /**
175 * Executes an Eclipse command with command ID after selecting passed components
176 * @param components - array of components to select in the tree
177 * @param commandId - command ID
178 * @throws ExecutionException
179 * @throws NotDefinedException
180 * @throws NotEnabledException
181 * @throws NotHandledException
182 */
183 public void executeCommand(ITraceControlComponent[] components, String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
184 setSelection(components);
185 executeCommand(commandId);
186 }
187
188 /**
189 * Executes an Eclipse command with command ID
190 * @param commandId
191 * @throws ExecutionException
192 * @throws NotDefinedException
193 * @throws NotEnabledException
194 * @throws NotHandledException
195 */
196 public void executeCommand(String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
197 IHandlerService handlerService = (IHandlerService) fControlView.getSite().getService(IHandlerService.class);
198 handlerService.executeCommand(COMMAND_CATEGORY_PREFIX + commandId, null);
199 waitForJobs();
200 }
201
202 /**
203 * Selects passed component
204 * @param component - component to select in the tree
205 * @param commandId - command ID
206 */
207 public void setSelection(ITraceControlComponent component) {
208 fControlView.setSelection(component);
209 // Selection is done in own job
210 waitForJobs();
211 }
212
213
214 /**
215 * Selects passed components
216 * @param components - array of component to select in the tree
217 * @param commandId - command ID
218 */
219 public void setSelection(ITraceControlComponent[] components) {
220 fControlView.setSelection(components);
221
222 // Selection is done in own job
223 waitForJobs();
224 }
225
226 /**
227 * Creates session on passed session group.
228 * @param group - session group
229 * @return - trace session group if it's successful else null
230 * @throws ExecutionException
231 * @throws NotDefinedException
232 * @throws NotEnabledException
233 * @throws NotHandledException
234 */
235 @SuppressWarnings("nls")
236 public TraceSessionComponent createSession(ITraceControlComponent group) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
237 executeCommand(group, "createSession");
238
239 ITraceControlComponent[] sessions = group.getChildren();
240 if ((sessions == null) || (sessions.length == 0)) {
241 return null;
242 }
243 return (TraceSessionComponent)sessions[0];
244 }
245
246 /**
247 * Destroys a given session.
248 * @param session - session to destroy
249 * @throws ExecutionException
250 * @throws NotDefinedException
251 * @throws NotEnabledException
252 * @throws NotHandledException
253 */
254 @SuppressWarnings("nls")
255 public void destroySession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
256 executeCommand(session, "destroySession");
257 }
258
259 /**
260 * Starts a given session
261 * @param session - session to start
262 * @throws ExecutionException
263 * @throws NotDefinedException
264 * @throws NotEnabledException
265 * @throws NotHandledException
266 */
267 @SuppressWarnings("nls")
268 public void startSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
269 executeCommand(session, "start");
270 }
271
272 /**
273 * Stops a given session
274 * @param session - session to stop
275 * @throws ExecutionException
276 * @throws NotDefinedException
277 * @throws NotEnabledException
278 * @throws NotHandledException
279 */
280 @SuppressWarnings("nls")
281 public void stopSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
282 executeCommand(session, "stop");
283 }
284 }
This page took 0.049665 seconds and 5 git commands to generate.