Improved GUI refresh and JUnit tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui.tests / src / org / eclipse / linuxtools / lttng2 / ui / tests / control / model / component / TraceControlTestFacility.java
CommitLineData
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 12package org.eclipse.linuxtools.lttng2.ui.tests.control.model.component;
eb1bab5b 13
d132bcc7
BH
14import org.eclipse.core.commands.ExecutionException;
15import org.eclipse.core.commands.NotEnabledException;
16import org.eclipse.core.commands.NotHandledException;
17import org.eclipse.core.commands.common.NotDefinedException;
eb1bab5b 18import org.eclipse.core.runtime.jobs.Job;
115b4a01 19import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
a26d90be
BH
20import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent;
21import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent;
eb1bab5b
BH
22import org.eclipse.swt.widgets.Display;
23import org.eclipse.ui.IViewPart;
24import org.eclipse.ui.PartInitException;
25import org.eclipse.ui.PlatformUI;
d132bcc7 26import 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 */
32public 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 {
b957fb8c
BH
78
79 view = PlatformUI.getWorkbench()
80 .getActiveWorkbenchWindow()
81 .getActivePage()
82 .findView("org.eclipse.ui.internal.introview"); //$NON-NLS-1$
83
84 if (view != null) {
85 PlatformUI.getWorkbench()
86 .getActiveWorkbenchWindow()
87 .getActivePage().hideView(view);
88 }
89
eb1bab5b
BH
90 view = PlatformUI.getWorkbench()
91 .getActiveWorkbenchWindow()
92 .getActivePage()
93 .showView(ControlView.ID);
94
95 } catch (PartInitException e) {
96 throw new RuntimeException(e);
97 }
98
99 fControlView = (ControlView) view;
100
101 delay(3000);
102 fIsInitialized = true;
103 }
104 }
105
106
a26d90be
BH
107 /**
108 * Disposes the facility (and GUI)
109 */
eb1bab5b
BH
110 public void dispose() {
111 if (fIsInitialized) {
112 waitForJobs();
113
114 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fControlView);
115 fIsInitialized = false;
116 }
117 }
a26d90be
BH
118
119 /**
120 * Creates a delay for given time.
121 * @param waitTimeMillis - time in milli seconds
122 */
eb1bab5b
BH
123 public void delay(long waitTimeMillis) {
124 Display display = Display.getCurrent();
125 if (display != null) {
126 long endTimeMillis = System.currentTimeMillis() + waitTimeMillis;
127 while(System.currentTimeMillis() < endTimeMillis) {
128 if (!display.readAndDispatch()) {
129 display.sleep();
130 }
131 display.update();
132 }
133 } else {
134 try {
135 Thread.sleep(waitTimeMillis);
136 } catch (InterruptedException e) {
137 // Ignored
138 }
139 }
140 }
141
142 /**
143 * Waits for all Eclipse jobs to finish
144 */
145 public void waitForJobs() {
146 while (!Job.getJobManager().isIdle()) {
147 delay(WAIT_FOR_JOBS_DELAY);
148 }
149 }
150
eb1bab5b
BH
151 /**
152 * @return current control view
153 */
154 public ControlView getControlView() {
155 return fControlView;
156 }
d132bcc7 157
a26d90be
BH
158 /**
159 * Executes an Eclipse command with command ID after selecting passed component
160 * @param component - component to select in the tree
161 * @param commandId - command ID
162 * @throws ExecutionException
163 * @throws NotDefinedException
164 * @throws NotEnabledException
165 * @throws NotHandledException
166 */
167 public void executeCommand(ITraceControlComponent component, String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
168 setSelection(component);
169 executeCommand(commandId);
170 }
171
172 /**
173 * Executes an Eclipse command with command ID after selecting passed components
174 * @param components - array of components to select in the tree
175 * @param commandId - command ID
176 * @throws ExecutionException
177 * @throws NotDefinedException
178 * @throws NotEnabledException
179 * @throws NotHandledException
180 */
181 public void executeCommand(ITraceControlComponent[] components, String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
182 setSelection(components);
183 executeCommand(commandId);
184 }
185
186 /**
187 * Executes an Eclipse command with command ID
188 * @param commandId
189 * @throws ExecutionException
190 * @throws NotDefinedException
191 * @throws NotEnabledException
192 * @throws NotHandledException
193 */
d132bcc7 194 public void executeCommand(String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
d132bcc7
BH
195 IHandlerService handlerService = (IHandlerService) fControlView.getSite().getService(IHandlerService.class);
196 handlerService.executeCommand(COMMAND_CATEGORY_PREFIX + commandId, null);
a26d90be
BH
197 waitForJobs();
198 }
199
200 /**
201 * Selects passed component
202 * @param component - component to select in the tree
203 * @param commandId - command ID
204 */
205 public void setSelection(ITraceControlComponent component) {
206 fControlView.setSelection(component);
b957fb8c
BH
207 // Selection is done in own job
208 waitForJobs();
d132bcc7
BH
209 }
210
a26d90be
BH
211
212 /**
213 * Selects passed components
214 * @param components - array of component to select in the tree
215 * @param commandId - command ID
216 */
217 public void setSelection(ITraceControlComponent[] components) {
218 fControlView.setSelection(components);
219
b957fb8c
BH
220 // Selection is done in own job
221 waitForJobs();
a26d90be
BH
222 }
223
224 /**
225 * Creates session on passed session group.
226 * @param group - session group
227 * @return - trace session group if it's successful else null
228 * @throws ExecutionException
229 * @throws NotDefinedException
230 * @throws NotEnabledException
231 * @throws NotHandledException
232 */
233 @SuppressWarnings("nls")
234 public TraceSessionComponent createSession(ITraceControlComponent group) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
235 executeCommand(group, "createSession");
236
237 ITraceControlComponent[] sessions = group.getChildren();
238 if ((sessions == null) || (sessions.length == 0)) {
239 return null;
240 }
241 return (TraceSessionComponent)sessions[0];
242 }
243
244 /**
245 * Destroys a given session.
246 * @param session - session to destroy
247 * @throws ExecutionException
248 * @throws NotDefinedException
249 * @throws NotEnabledException
250 * @throws NotHandledException
251 */
252 @SuppressWarnings("nls")
253 public void destroySession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
254 executeCommand(session, "destroySession");
255 }
256
257 /**
258 * Starts a given session
259 * @param session - session to start
260 * @throws ExecutionException
261 * @throws NotDefinedException
262 * @throws NotEnabledException
263 * @throws NotHandledException
264 */
265 @SuppressWarnings("nls")
266 public void startSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
267 executeCommand(session, "start");
268 }
269
270 /**
271 * Stops a given session
272 * @param session - session to stop
273 * @throws ExecutionException
274 * @throws NotDefinedException
275 * @throws NotEnabledException
276 * @throws NotHandledException
277 */
278 @SuppressWarnings("nls")
279 public void stopSession(TraceSessionComponent session) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
280 executeCommand(session, "stop");
281 }
eb1bab5b 282}
This page took 0.044287 seconds and 5 git commands to generate.