control: add test for correct command line for adding context for UST
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui.tests / src / org / eclipse / tracecompass / lttng2 / control / ui / tests / model / component / TraceControlTestFacility.java
CommitLineData
eb1bab5b 1/*******************************************************************************
b732adaa 2 * Copyright (c) 2011, 2014 Ericsson
cfdb727a 3 *
eb1bab5b
BH
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
cfdb727a 8 *
eb1bab5b
BH
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
2ba3d0a1 12
9bc60be7 13package org.eclipse.tracecompass.lttng2.control.ui.tests.model.component;
eb1bab5b 14
13f36900
BH
15import static org.junit.Assert.assertNotNull;
16
d132bcc7
BH
17import org.eclipse.core.commands.ExecutionException;
18import org.eclipse.core.commands.NotEnabledException;
19import org.eclipse.core.commands.NotHandledException;
20import org.eclipse.core.commands.common.NotDefinedException;
eb1bab5b 21import org.eclipse.core.runtime.jobs.Job;
eb1bab5b 22import org.eclipse.swt.widgets.Display;
b732adaa 23import org.eclipse.tracecompass.internal.lttng2.control.core.model.TargetNodeState;
9bc60be7
AM
24import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
25import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
b732adaa 26import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent;
9bc60be7 27import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
eb1bab5b
BH
28import org.eclipse.ui.IViewPart;
29import org.eclipse.ui.PartInitException;
30import org.eclipse.ui.PlatformUI;
d132bcc7 31import org.eclipse.ui.handlers.IHandlerService;
b732adaa 32import org.junit.Assert;
eb1bab5b
BH
33
34/**
35 * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides
cfdb727a 36 * utility methods for interacting with the loader/view.
eb1bab5b 37 */
cfdb727a 38@SuppressWarnings("javadoc")
eb1bab5b
BH
39public class TraceControlTestFacility {
40
41 // ------------------------------------------------------------------------
42 // Constants
43 // ------------------------------------------------------------------------
22a0f8f8 44 public final static int WAIT_FOR_JOBS_DELAY = 50;
a26d90be 45 public final static int GUI_REFESH_DELAY = 500;
cfdb727a 46
4e0b52e0
AM
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";
cfdb727a 52
eb1bab5b
BH
53 // ------------------------------------------------------------------------
54 // Attributes
55 // ------------------------------------------------------------------------
56 private static TraceControlTestFacility fInstance = null;
57 private ControlView fControlView = null;
13f36900 58 private volatile boolean fIsInitialized = false;
cfdb727a 59
eb1bab5b
BH
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() {
cfdb727a 80
eb1bab5b 81 if (!fIsInitialized) {
eb1bab5b
BH
82 IViewPart view;
83 try {
13f36900
BH
84 hideView("org.eclipse.ui.internal.introview");
85 view = showView(ControlView.ID);
eb1bab5b
BH
86 } catch (PartInitException e) {
87 throw new RuntimeException(e);
88 }
eb1bab5b
BH
89 fControlView = (ControlView) view;
90
705e682f
BH
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
eb1bab5b
BH
105 fIsInitialized = true;
106 }
107 }
108
a26d90be
BH
109 /**
110 * Disposes the facility (and GUI)
111 */
eb1bab5b
BH
112 public void dispose() {
113 if (fIsInitialized) {
114 waitForJobs();
13f36900
BH
115 hideView(ControlView.ID);
116 delay(200);
eb1bab5b
BH
117 fIsInitialized = false;
118 }
119 }
a26d90be
BH
120
121 /**
122 * Creates a delay for given time.
123 * @param waitTimeMillis - time in milli seconds
124 */
eb1bab5b
BH
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()) {
99c3a878
MAL
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
b07022a9 137 }
eb1bab5b
BH
138 }
139 display.update();
140 }
141 } else {
142 try {
143 Thread.sleep(waitTimeMillis);
144 } catch (InterruptedException e) {
145 // Ignored
146 }
147 }
148 }
149
b732adaa
MS
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
13f36900
BH
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
eb1bab5b
BH
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
13f36900
BH
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
eb1bab5b
BH
221 /**
222 * @return current control view
223 */
224 public ControlView getControlView() {
225 return fControlView;
226 }
d132bcc7 227
a26d90be
BH
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 }
cfdb727a 241
a26d90be
BH
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 */
d132bcc7 264 public void executeCommand(String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
346fa221
MAL
265 Object handlerServiceObject = fControlView.getSite().getService(IHandlerService.class);
266 IHandlerService handlerService = (IHandlerService) handlerServiceObject;
d132bcc7 267 handlerService.executeCommand(COMMAND_CATEGORY_PREFIX + commandId, null);
a26d90be
BH
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);
b957fb8c
BH
278 // Selection is done in own job
279 waitForJobs();
d132bcc7
BH
280 }
281
cfdb727a 282
a26d90be
BH
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
b957fb8c
BH
291 // Selection is done in own job
292 waitForJobs();
a26d90be 293 }
cfdb727a 294
a26d90be
BH
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 */
a26d90be
BH
304 public TraceSessionComponent createSession(ITraceControlComponent group) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
305 executeCommand(group, "createSession");
cfdb727a 306
a26d90be
BH
307 ITraceControlComponent[] sessions = group.getChildren();
308 if ((sessions == null) || (sessions.length == 0)) {
309 return null;
310 }
311 return (TraceSessionComponent)sessions[0];
312 }
cfdb727a 313
a26d90be
BH
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 */
a26d90be
BH
322 public void destroySession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
323 executeCommand(session, "destroySession");
324 }
cfdb727a 325
a26d90be
BH
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 */
a26d90be
BH
334 public void startSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
335 executeCommand(session, "start");
336 }
cfdb727a 337
a26d90be
BH
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 */
a26d90be
BH
346 public void stopSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
347 executeCommand(session, "stop");
348 }
eb1bab5b 349}
This page took 0.082206 seconds and 5 git commands to generate.