Commit | Line | Data |
---|---|---|
eb1bab5b BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011 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 | |
115b4a01 | 13 | package org.eclipse.linuxtools.lttng2.ui.tests.control.model.component; |
eb1bab5b | 14 | |
d132bcc7 BH |
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; | |
eb1bab5b | 19 | import org.eclipse.core.runtime.jobs.Job; |
115b4a01 | 20 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; |
a26d90be BH |
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; | |
eb1bab5b BH |
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; | |
d132bcc7 | 27 | import org.eclipse.ui.handlers.IHandlerService; |
eb1bab5b BH |
28 | |
29 | /** | |
30 | * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides | |
cfdb727a | 31 | * utility methods for interacting with the loader/view. |
eb1bab5b | 32 | */ |
cfdb727a | 33 | @SuppressWarnings("javadoc") |
eb1bab5b BH |
34 | public class TraceControlTestFacility { |
35 | ||
36 | // ------------------------------------------------------------------------ | |
37 | // Constants | |
38 | // ------------------------------------------------------------------------ | |
cfdb727a | 39 | public final static int WAIT_FOR_JOBS_DELAY = 1000; |
a26d90be | 40 | public final static int GUI_REFESH_DELAY = 500; |
cfdb727a | 41 | |
a26d90be BH |
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$ | |
cfdb727a | 47 | |
eb1bab5b BH |
48 | // ------------------------------------------------------------------------ |
49 | // Attributes | |
50 | // ------------------------------------------------------------------------ | |
51 | private static TraceControlTestFacility fInstance = null; | |
52 | private ControlView fControlView = null; | |
53 | private boolean fIsInitialized = false; | |
cfdb727a | 54 | |
eb1bab5b BH |
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() { | |
cfdb727a | 75 | |
eb1bab5b BH |
76 | if (!fIsInitialized) { |
77 | ||
78 | IViewPart view; | |
79 | try { | |
cfdb727a | 80 | |
b957fb8c BH |
81 | view = PlatformUI.getWorkbench() |
82 | .getActiveWorkbenchWindow() | |
83 | .getActivePage() | |
84 | .findView("org.eclipse.ui.internal.introview"); //$NON-NLS-1$ | |
cfdb727a | 85 | |
b957fb8c BH |
86 | if (view != null) { |
87 | PlatformUI.getWorkbench() | |
88 | .getActiveWorkbenchWindow() | |
cfdb727a | 89 | .getActivePage().hideView(view); |
b957fb8c BH |
90 | } |
91 | ||
eb1bab5b BH |
92 | view = PlatformUI.getWorkbench() |
93 | .getActiveWorkbenchWindow() | |
94 | .getActivePage() | |
cfdb727a AM |
95 | .showView(ControlView.ID); |
96 | ||
eb1bab5b BH |
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 | ||
cfdb727a | 108 | |
a26d90be BH |
109 | /** |
110 | * Disposes the facility (and GUI) | |
111 | */ | |
eb1bab5b BH |
112 | public void dispose() { |
113 | if (fIsInitialized) { | |
114 | waitForJobs(); | |
115 | ||
116 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fControlView); | |
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()) { | |
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 | ||
eb1bab5b BH |
153 | /** |
154 | * @return current control view | |
155 | */ | |
156 | public ControlView getControlView() { | |
157 | return fControlView; | |
158 | } | |
d132bcc7 | 159 | |
a26d90be BH |
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 | } | |
cfdb727a | 173 | |
a26d90be BH |
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 | */ | |
d132bcc7 | 196 | public void executeCommand(String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException { |
d132bcc7 BH |
197 | IHandlerService handlerService = (IHandlerService) fControlView.getSite().getService(IHandlerService.class); |
198 | handlerService.executeCommand(COMMAND_CATEGORY_PREFIX + commandId, null); | |
a26d90be BH |
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); | |
b957fb8c BH |
209 | // Selection is done in own job |
210 | waitForJobs(); | |
d132bcc7 BH |
211 | } |
212 | ||
cfdb727a | 213 | |
a26d90be BH |
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 | ||
b957fb8c BH |
222 | // Selection is done in own job |
223 | waitForJobs(); | |
a26d90be | 224 | } |
cfdb727a | 225 | |
a26d90be BH |
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"); | |
cfdb727a | 238 | |
a26d90be BH |
239 | ITraceControlComponent[] sessions = group.getChildren(); |
240 | if ((sessions == null) || (sessions.length == 0)) { | |
241 | return null; | |
242 | } | |
243 | return (TraceSessionComponent)sessions[0]; | |
244 | } | |
cfdb727a | 245 | |
a26d90be BH |
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 | } | |
cfdb727a | 258 | |
a26d90be BH |
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 | } | |
cfdb727a | 271 | |
a26d90be BH |
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 | } | |
eb1bab5b | 284 | } |