Commit | Line | Data |
---|---|---|
eb1bab5b BH |
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 | *******************************************************************************/ | |
115b4a01 | 12 | package org.eclipse.linuxtools.lttng2.ui.tests.control.model.component; |
eb1bab5b | 13 | |
d132bcc7 BH |
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; | |
eb1bab5b | 18 | import org.eclipse.core.runtime.jobs.Job; |
115b4a01 | 19 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; |
a26d90be BH |
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; | |
eb1bab5b BH |
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; | |
d132bcc7 | 26 | import org.eclipse.ui.handlers.IHandlerService; |
eb1bab5b BH |
27 | |
28 | /** | |
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. | |
31 | */ | |
32 | public class TraceControlTestFacility { | |
33 | ||
34 | // ------------------------------------------------------------------------ | |
35 | // Constants | |
36 | // ------------------------------------------------------------------------ | |
a26d90be BH |
37 | public final static int WAIT_FOR_JOBS_DELAY = 1000; |
38 | public final static int GUI_REFESH_DELAY = 500; | |
39 | ||
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$ | |
d132bcc7 | 45 | |
eb1bab5b BH |
46 | // ------------------------------------------------------------------------ |
47 | // Attributes | |
48 | // ------------------------------------------------------------------------ | |
49 | private static TraceControlTestFacility fInstance = null; | |
50 | private ControlView fControlView = null; | |
51 | private boolean fIsInitialized = false; | |
52 | ||
53 | // ------------------------------------------------------------------------ | |
54 | // Constructors | |
55 | // ------------------------------------------------------------------------ | |
56 | private TraceControlTestFacility() { | |
57 | } | |
58 | ||
59 | // ------------------------------------------------------------------------ | |
60 | // Operations | |
61 | // ------------------------------------------------------------------------ | |
62 | public static TraceControlTestFacility getInstance() { | |
63 | if (fInstance == null) { | |
64 | fInstance = new TraceControlTestFacility(); | |
65 | } | |
66 | return fInstance; | |
67 | } | |
68 | ||
69 | /** | |
70 | * Initial the test facility. | |
71 | */ | |
72 | public void init() { | |
73 | ||
74 | if (!fIsInitialized) { | |
75 | ||
76 | IViewPart view; | |
77 | try { | |
78 | view = PlatformUI.getWorkbench() | |
79 | .getActiveWorkbenchWindow() | |
80 | .getActivePage() | |
81 | .showView(ControlView.ID); | |
82 | ||
83 | } catch (PartInitException e) { | |
84 | throw new RuntimeException(e); | |
85 | } | |
86 | ||
87 | fControlView = (ControlView) view; | |
88 | ||
89 | delay(3000); | |
90 | fIsInitialized = true; | |
91 | } | |
92 | } | |
93 | ||
94 | ||
a26d90be BH |
95 | /** |
96 | * Disposes the facility (and GUI) | |
97 | */ | |
eb1bab5b BH |
98 | public void dispose() { |
99 | if (fIsInitialized) { | |
100 | waitForJobs(); | |
101 | ||
102 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fControlView); | |
103 | fIsInitialized = false; | |
104 | } | |
105 | } | |
a26d90be BH |
106 | |
107 | /** | |
108 | * Creates a delay for given time. | |
109 | * @param waitTimeMillis - time in milli seconds | |
110 | */ | |
eb1bab5b BH |
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()) { | |
117 | display.sleep(); | |
118 | } | |
119 | display.update(); | |
120 | } | |
121 | } else { | |
122 | try { | |
123 | Thread.sleep(waitTimeMillis); | |
124 | } catch (InterruptedException e) { | |
125 | // Ignored | |
126 | } | |
127 | } | |
128 | } | |
129 | ||
130 | /** | |
131 | * Waits for all Eclipse jobs to finish | |
132 | */ | |
133 | public void waitForJobs() { | |
134 | while (!Job.getJobManager().isIdle()) { | |
135 | delay(WAIT_FOR_JOBS_DELAY); | |
136 | } | |
137 | } | |
138 | ||
eb1bab5b BH |
139 | /** |
140 | * @return current control view | |
141 | */ | |
142 | public ControlView getControlView() { | |
143 | return fControlView; | |
144 | } | |
d132bcc7 | 145 | |
a26d90be BH |
146 | /** |
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 | |
154 | */ | |
155 | public void executeCommand(ITraceControlComponent component, String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException { | |
156 | setSelection(component); | |
157 | executeCommand(commandId); | |
158 | } | |
159 | ||
160 | /** | |
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 | |
168 | */ | |
169 | public void executeCommand(ITraceControlComponent[] components, String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException { | |
170 | setSelection(components); | |
171 | executeCommand(commandId); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Executes an Eclipse command with command ID | |
176 | * @param commandId | |
177 | * @throws ExecutionException | |
178 | * @throws NotDefinedException | |
179 | * @throws NotEnabledException | |
180 | * @throws NotHandledException | |
181 | */ | |
d132bcc7 | 182 | public void executeCommand(String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException { |
d132bcc7 BH |
183 | IHandlerService handlerService = (IHandlerService) fControlView.getSite().getService(IHandlerService.class); |
184 | handlerService.executeCommand(COMMAND_CATEGORY_PREFIX + commandId, null); | |
a26d90be BH |
185 | waitForJobs(); |
186 | } | |
187 | ||
188 | /** | |
189 | * Selects passed component | |
190 | * @param component - component to select in the tree | |
191 | * @param commandId - command ID | |
192 | */ | |
193 | public void setSelection(ITraceControlComponent component) { | |
194 | fControlView.setSelection(component); | |
195 | ||
196 | // Give GUI time to actually execute refresh | |
197 | delay(TraceControlTestFacility.GUI_REFESH_DELAY); | |
d132bcc7 BH |
198 | } |
199 | ||
a26d90be BH |
200 | |
201 | /** | |
202 | * Selects passed components | |
203 | * @param components - array of component to select in the tree | |
204 | * @param commandId - command ID | |
205 | */ | |
206 | public void setSelection(ITraceControlComponent[] components) { | |
207 | fControlView.setSelection(components); | |
208 | ||
209 | // Give GUI time to actually execute refresh | |
210 | delay(TraceControlTestFacility.GUI_REFESH_DELAY); | |
211 | } | |
212 | ||
213 | /** | |
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 | |
221 | */ | |
222 | @SuppressWarnings("nls") | |
223 | public TraceSessionComponent createSession(ITraceControlComponent group) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException { | |
224 | executeCommand(group, "createSession"); | |
225 | ||
226 | ITraceControlComponent[] sessions = group.getChildren(); | |
227 | if ((sessions == null) || (sessions.length == 0)) { | |
228 | return null; | |
229 | } | |
230 | return (TraceSessionComponent)sessions[0]; | |
231 | } | |
232 | ||
233 | /** | |
234 | * Destroys a given session. | |
235 | * @param session - session to destroy | |
236 | * @throws ExecutionException | |
237 | * @throws NotDefinedException | |
238 | * @throws NotEnabledException | |
239 | * @throws NotHandledException | |
240 | */ | |
241 | @SuppressWarnings("nls") | |
242 | public void destroySession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException { | |
243 | executeCommand(session, "destroySession"); | |
244 | } | |
245 | ||
246 | /** | |
247 | * Starts a given session | |
248 | * @param session - session to start | |
249 | * @throws ExecutionException | |
250 | * @throws NotDefinedException | |
251 | * @throws NotEnabledException | |
252 | * @throws NotHandledException | |
253 | */ | |
254 | @SuppressWarnings("nls") | |
255 | public void startSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException { | |
256 | executeCommand(session, "start"); | |
257 | } | |
258 | ||
259 | /** | |
260 | * Stops a given session | |
261 | * @param session - session to stop | |
262 | * @throws ExecutionException | |
263 | * @throws NotDefinedException | |
264 | * @throws NotEnabledException | |
265 | * @throws NotHandledException | |
266 | */ | |
267 | @SuppressWarnings("nls") | |
268 | public void stopSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException { | |
269 | executeCommand(session, "stop"); | |
270 | } | |
eb1bab5b | 271 | } |