ceccb7a22c0136f6342b3599d0e26548a13a4696
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / SWTBotUtils.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 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 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared;
14
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.fail;
17
18 import java.util.List;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.NullProgressMonitor;
26 import org.eclipse.core.runtime.jobs.Job;
27 import org.eclipse.jface.bindings.keys.IKeyLookup;
28 import org.eclipse.jface.bindings.keys.KeyStroke;
29 import org.eclipse.jface.bindings.keys.ParseException;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.graphics.Rectangle;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.Table;
34 import org.eclipse.swt.widgets.TableItem;
35 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
36 import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory;
37 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
38 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
39 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
40 import org.eclipse.swtbot.swt.finder.results.Result;
41 import org.eclipse.swtbot.swt.finder.results.VoidResult;
42 import org.eclipse.swtbot.swt.finder.waits.Conditions;
43 import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
44 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
45 import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
46 import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
47 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
48 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
49 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
50 import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
51 import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
52 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
53 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
54 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
55 import org.eclipse.tracecompass.tmf.ui.views.TracingPerspectiveFactory;
56 import org.eclipse.ui.IEditorPart;
57 import org.eclipse.ui.IEditorReference;
58 import org.eclipse.ui.IPageLayout;
59 import org.eclipse.ui.PartInitException;
60 import org.eclipse.ui.PlatformUI;
61 import org.eclipse.ui.WorkbenchException;
62 import org.hamcrest.Matcher;
63
64 /**
65 * SWTBot Helper functions
66 *
67 * @author Matthew Khouzam
68 */
69 public final class SWTBotUtils {
70
71 private SWTBotUtils() {
72 }
73
74 private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID;
75
76 /**
77 * Waits for all Eclipse jobs to finish
78 */
79 public static void waitForJobs() {
80 while (!Job.getJobManager().isIdle()) {
81 delay(100);
82 }
83 }
84
85 /**
86 * Sleeps current thread for a given time.
87 *
88 * @param waitTimeMillis
89 * time in milliseconds to wait
90 */
91 public static void delay(final long waitTimeMillis) {
92 try {
93 Thread.sleep(waitTimeMillis);
94 } catch (final InterruptedException e) {
95 // Ignored
96 }
97 }
98
99 /**
100 * Create a tracing project
101 *
102 * @param projectName
103 * the name of the tracing project
104 */
105 public static void createProject(final String projectName) {
106 /*
107 * Make a new test
108 */
109 UIThreadRunnable.syncExec(new VoidResult() {
110 @Override
111 public void run() {
112 IProject project = TmfProjectRegistry.createProject(projectName, null, new NullProgressMonitor());
113 assertNotNull(project);
114 }
115 });
116
117 SWTBotUtils.waitForJobs();
118 }
119
120 /**
121 * Deletes a tracing project
122 *
123 * @param projectName
124 * the name of the tracing project
125 * @param bot
126 * the workbench bot
127 */
128 public static void deleteProject(String projectName, SWTWorkbenchBot bot) {
129 // Wait for any analysis to complete because it might create
130 // supplementary files
131 SWTBotUtils.waitForJobs();
132 try {
133 ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).refreshLocal(IResource.DEPTH_INFINITE, null);
134 } catch (CoreException e) {
135 }
136
137 SWTBotUtils.waitForJobs();
138
139 final SWTBotView projectViewBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
140 projectViewBot.setFocus();
141
142 SWTBotTree treeBot = projectViewBot.bot().tree();
143 SWTBotTreeItem treeItem = treeBot.getTreeItem(projectName);
144 SWTBotMenu contextMenu = treeItem.contextMenu("Delete");
145 contextMenu.click();
146
147 bot.shell("Delete Resources").setFocus();
148 final SWTBotCheckBox checkBox = bot.checkBox();
149 bot.waitUntil(Conditions.widgetIsEnabled(checkBox));
150 checkBox.click();
151
152 final SWTBotButton okButton = bot.button("OK");
153 bot.waitUntil(Conditions.widgetIsEnabled(okButton));
154 okButton.click();
155
156 SWTBotUtils.waitForJobs();
157 }
158
159 /**
160 * Focus on the main window
161 *
162 * @param shellBots
163 * swtbotshells for all the shells
164 */
165 public static void focusMainWindow(SWTBotShell[] shellBots) {
166 for (SWTBotShell shellBot : shellBots) {
167 if (shellBot.getText().toLowerCase().contains("eclipse")) {
168 shellBot.activate();
169 }
170 }
171 }
172
173 /**
174 * Close a view with a title
175 *
176 * @param title
177 * the title, like "welcome"
178 * @param bot
179 * the workbench bot
180 */
181 public static void closeView(String title, SWTWorkbenchBot bot) {
182 final List<SWTBotView> openViews = bot.views();
183 for (SWTBotView view : openViews) {
184 if (view.getTitle().equalsIgnoreCase(title)) {
185 view.close();
186 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
187 }
188 }
189 }
190
191 /**
192 * Switch to the tracing perspective
193 */
194 public static void switchToTracingPerspective() {
195 switchToPerspective(TRACING_PERSPECTIVE_ID);
196 }
197
198 /**
199 * Switch to a given perspective
200 *
201 * @param id
202 * the perspective id (like
203 * "org.eclipse.linuxtools.tmf.ui.perspective"
204 */
205 public static void switchToPerspective(final String id) {
206 UIThreadRunnable.syncExec(new VoidResult() {
207 @Override
208 public void run() {
209 try {
210 PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
211 } catch (WorkbenchException e) {
212 fail(e.getMessage());
213 }
214 }
215 });
216 }
217
218 /**
219 * If the test is running in the UI thread then fail
220 */
221 public static void failIfUIThread() {
222 if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) {
223 fail("SWTBot test needs to run in a non-UI thread. Make sure that \"Run in UI thread\" is unchecked in your launch configuration or"
224 + " that useUIThread is set to false in the pom.xml");
225 }
226
227 }
228
229 /**
230 * Open a trace, this does not perform any validation though
231 *
232 * @param projectName
233 * The project name
234 * @param tracePath
235 * the path of the trace file (absolute or relative)
236 * @param traceType
237 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
238 */
239 public static void openTrace(final String projectName, final String tracePath, final String traceType) {
240 openTrace(projectName, tracePath, traceType, true);
241 }
242
243 /**
244 * Open a trace, this does not perform any validation though
245 *
246 * @param projectName
247 * The project name
248 * @param tracePath
249 * the path of the trace file (absolute or relative)
250 * @param traceType
251 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
252 * @param delay
253 * delay and wait for jobs
254 */
255 public static void openTrace(final String projectName, final String tracePath, final String traceType, boolean delay) {
256 final Exception exception[] = new Exception[1];
257 exception[0] = null;
258 UIThreadRunnable.syncExec(new VoidResult() {
259 @Override
260 public void run() {
261 try {
262 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
263 TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder();
264 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, tracePath, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType);
265 } catch (CoreException e) {
266 exception[0] = e;
267 }
268 }
269 });
270 if (exception[0] != null) {
271 fail(exception[0].getMessage());
272 }
273
274 if (delay) {
275 delay(1000);
276 waitForJobs();
277 }
278 }
279
280 /**
281 * Finds an editor and sets focus to the editor
282 *
283 * @param bot
284 * the workbench bot
285 * @param editorName
286 * the editor name
287 * @return the corresponding SWTBotEditor
288 */
289 public static SWTBotEditor activateEditor(SWTWorkbenchBot bot, String editorName) {
290 Matcher<IEditorReference> matcher = WidgetMatcherFactory.withPartName(editorName);
291 final SWTBotEditor editorBot = bot.editor(matcher);
292 IEditorPart iep = editorBot.getReference().getEditor(true);
293 final TmfEventsEditor tmfEd = (TmfEventsEditor) iep;
294 editorBot.show();
295 UIThreadRunnable.syncExec(new VoidResult() {
296 @Override
297 public void run() {
298 tmfEd.setFocus();
299 }
300 });
301
302 SWTBotUtils.waitForJobs();
303 SWTBotUtils.delay(1000);
304 assertNotNull(tmfEd);
305 return editorBot;
306 }
307
308 /**
309 * Opens a trace in an editor and get the TmfEventsEditor
310 *
311 * @param bot
312 * the workbench bot
313 * @param projectName
314 * the name of the project that contains the trace
315 * @param elementPath
316 * the trace element path (relative to Traces folder)
317 * @return TmfEventsEditor the opened editor
318 */
319 public static TmfEventsEditor openEditor(SWTWorkbenchBot bot, String projectName, IPath elementPath) {
320 final SWTBotView projectExplorerBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
321 projectExplorerBot.setFocus();
322
323 final SWTBotTree tree = bot.tree();
324 final SWTBotTreeItem treeItem = tree.getTreeItem(projectName);
325 treeItem.expand();
326
327 String nodeName = getFullNodeName(treeItem, TmfTracesFolder.TRACES_FOLDER_NAME);
328 bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, treeItem));
329 SWTBotTreeItem tracesNode = treeItem.getNode(nodeName);
330 tracesNode.expand();
331
332 SWTBotTreeItem currentNode = tracesNode;
333 for (String segment : elementPath.segments()) {
334 String fullNodeName = getFullNodeName(currentNode, segment);
335 bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(fullNodeName, currentNode));
336 SWTBotTreeItem newNode = currentNode.getNode(fullNodeName);
337 newNode.select();
338 newNode.doubleClick();
339 currentNode = newNode;
340 }
341
342 SWTBotUtils.delay(1000);
343 SWTBotUtils.waitForJobs();
344 final String expectedTitle = elementPath.toString();
345
346 final IEditorPart iep[] = new IEditorPart[1];
347 UIThreadRunnable.syncExec(new VoidResult() {
348 @Override
349 public void run() {
350 IEditorReference[] ieds = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
351 assertNotNull(ieds);
352 iep[0] = null;
353 for (IEditorReference ied : ieds) {
354 if (ied.getTitle().equals(expectedTitle)) {
355 iep[0] = ied.getEditor(true);
356 break;
357 }
358 }
359 }
360 });
361 assertNotNull(iep[0]);
362 return (TmfEventsEditor) iep[0];
363 }
364
365 private static String getFullNodeName(final SWTBotTreeItem treeItem, String prefix) {
366 List<String> nodes = treeItem.getNodes();
367 String nodeName = "";
368 for (String node : nodes) {
369 if (node.startsWith(prefix)) {
370 nodeName = node;
371 }
372 }
373 return nodeName;
374 }
375
376 /**
377 * Select the traces folder
378 *
379 * @param bot
380 * a given workbench bot
381 * @param projectName
382 * the name of the project (it needs to exist or else it would
383 * time out)
384 * @return a {@link SWTBotTreeItem} of the "Traces" directory
385 */
386 public static SWTBotTreeItem selectTracesFolder(SWTWorkbenchBot bot, String projectName) {
387 SWTBotView projectExplorerBot = bot.viewByTitle("Project Explorer");
388 projectExplorerBot.show();
389 SWTBotTreeItem treeItem = projectExplorerBot.bot().tree().getTreeItem(projectName);
390 treeItem.select();
391 treeItem.expand();
392 SWTBotTreeItem treeNode = null;
393 for (String node : treeItem.getNodes()) {
394 if (node.matches("Traces\\s\\[(\\d)*\\]")) {
395 treeNode = treeItem.getNode(node);
396 break;
397 }
398 }
399 assertNotNull(treeNode);
400 return treeNode;
401 }
402
403 /**
404 * Open a view by id.
405 *
406 * @param id
407 * view id.
408 */
409 public static void openView(final String id) {
410 final PartInitException res[] = new PartInitException[1];
411 UIThreadRunnable.syncExec(new VoidResult() {
412 @Override
413 public void run() {
414 try {
415 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id);
416 } catch (PartInitException e) {
417 res[0] = e;
418 }
419 }
420 });
421 if (res[0] != null) {
422 fail(res[0].getMessage());
423 }
424 waitForJobs();
425 }
426
427 /**
428 * Maximize a table
429 *
430 * @param tableBot
431 * the {@link SWTBotTable} table
432 */
433 public static void maximizeTable(SWTBotTable tableBot) {
434 try {
435 tableBot.pressShortcut(KeyStroke.getInstance(IKeyLookup.CTRL_NAME + "+"), KeyStroke.getInstance("M"));
436 } catch (ParseException e) {
437 fail();
438 }
439 }
440
441 /**
442 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
443 * index in a table
444 *
445 * @param table
446 * the table
447 * @param row
448 * the row of the table to look up
449 * @param col
450 * the column of the table to look up
451 * @return the bounds in display relative coordinates
452 */
453 public static Rectangle getCellBounds(final Table table, final int row, final int col) {
454 return UIThreadRunnable.syncExec(new Result<Rectangle>() {
455 @Override
456 public Rectangle run() {
457 TableItem item = table.getItem(row);
458 Rectangle bounds = item.getBounds(col);
459 Point p = table.toDisplay(bounds.x, bounds.y);
460 Rectangle rect = new Rectangle(p.x, p.y, bounds.width, bounds.height);
461 return rect;
462 }
463 });
464 }
465 }
This page took 0.042071 seconds and 4 git commands to generate.