1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
;
15 import static org
.junit
.Assert
.assertNotNull
;
16 import static org
.junit
.Assert
.assertTrue
;
17 import static org
.junit
.Assert
.fail
;
19 import java
.util
.List
;
21 import org
.eclipse
.core
.resources
.IProject
;
22 import org
.eclipse
.core
.resources
.IResource
;
23 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
24 import org
.eclipse
.core
.runtime
.CoreException
;
25 import org
.eclipse
.core
.runtime
.IPath
;
26 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
27 import org
.eclipse
.core
.runtime
.jobs
.Job
;
28 import org
.eclipse
.jface
.bindings
.keys
.IKeyLookup
;
29 import org
.eclipse
.jface
.bindings
.keys
.KeyStroke
;
30 import org
.eclipse
.jface
.bindings
.keys
.ParseException
;
31 import org
.eclipse
.swt
.graphics
.Point
;
32 import org
.eclipse
.swt
.graphics
.Rectangle
;
33 import org
.eclipse
.swt
.widgets
.Display
;
34 import org
.eclipse
.swt
.widgets
.Shell
;
35 import org
.eclipse
.swt
.widgets
.Table
;
36 import org
.eclipse
.swt
.widgets
.TableItem
;
37 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
38 import org
.eclipse
.swtbot
.eclipse
.finder
.matchers
.WidgetMatcherFactory
;
39 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
40 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
41 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
42 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
43 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
44 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
45 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTUtils
;
46 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
47 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
48 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
49 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
50 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
51 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
52 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
53 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
54 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
57 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
60 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
.ProjectElementHasChild
;
61 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TracingPerspectiveFactory
;
62 import org
.eclipse
.ui
.IEditorPart
;
63 import org
.eclipse
.ui
.IEditorReference
;
64 import org
.eclipse
.ui
.IPageLayout
;
65 import org
.eclipse
.ui
.PartInitException
;
66 import org
.eclipse
.ui
.PlatformUI
;
67 import org
.eclipse
.ui
.WorkbenchException
;
68 import org
.hamcrest
.Matcher
;
71 * SWTBot Helper functions
73 * @author Matthew Khouzam
75 public final class SWTBotUtils
{
77 private static final String WINDOW_MENU
= "Window";
78 private static final String PREFERENCES_MENU_ITEM
= "Preferences";
80 private SWTBotUtils() {
83 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
86 * Waits for all Eclipse jobs to finish
88 public static void waitForJobs() {
89 while (!Job
.getJobManager().isIdle()) {
95 * Sleeps current thread for a given time.
97 * @param waitTimeMillis
98 * time in milliseconds to wait
100 public static void delay(final long waitTimeMillis
) {
102 Thread
.sleep(waitTimeMillis
);
103 } catch (final InterruptedException e
) {
109 * Create a tracing project
112 * the name of the tracing project
114 public static void createProject(final String projectName
) {
118 UIThreadRunnable
.syncExec(new VoidResult() {
121 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
122 assertNotNull(project
);
126 SWTBotUtils
.waitForJobs();
133 * the name of the tracing project
134 * @param deleteResources
135 * whether or not to deleted resources under the project
139 public static void deleteProject(final String projectName
, boolean deleteResources
, SWTWorkbenchBot bot
) {
140 // Wait for any analysis to complete because it might create
141 // supplementary files
142 SWTBotUtils
.waitForJobs();
144 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
145 } catch (CoreException e
) {
148 SWTBotUtils
.waitForJobs();
150 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
151 projectViewBot
.setFocus();
153 SWTBotTree treeBot
= projectViewBot
.bot().tree();
154 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
155 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
158 if (deleteResources
) {
159 bot
.shell("Delete Resources").setFocus();
160 final SWTBotCheckBox checkBox
= bot
.checkBox();
161 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
165 final SWTBotButton okButton
= bot
.button("OK");
166 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
169 SWTBotUtils
.waitForJobs();
173 * Deletes a project and its resources
176 * the name of the tracing project
180 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
181 deleteProject(projectName
, true, bot
);
185 * Focus on the main window
188 * swtbotshells for all the shells
190 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
191 for (SWTBotShell shellBot
: shellBots
) {
192 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
199 * Close a view with a title
202 * the title, like "welcome"
206 public static void closeView(String title
, SWTWorkbenchBot bot
) {
207 final List
<SWTBotView
> openViews
= bot
.views();
208 for (SWTBotView view
: openViews
) {
209 if (view
.getTitle().equalsIgnoreCase(title
)) {
211 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
217 * Close a view with an id
220 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
224 public static void closeViewById(String viewId
, SWTWorkbenchBot bot
) {
225 final SWTBotView view
= bot
.viewById(viewId
);
227 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
231 * Switch to the tracing perspective
233 public static void switchToTracingPerspective() {
234 switchToPerspective(TRACING_PERSPECTIVE_ID
);
238 * Switch to a given perspective
241 * the perspective id (like
242 * "org.eclipse.linuxtools.tmf.ui.perspective"
244 public static void switchToPerspective(final String id
) {
245 UIThreadRunnable
.syncExec(new VoidResult() {
249 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
250 } catch (WorkbenchException e
) {
251 fail(e
.getMessage());
258 * Initialize the environment for SWTBot
260 public static void initialize() {
263 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
264 UIThreadRunnable
.syncExec(() -> {
266 // There seems to be problems on some system where the main shell is
267 // not in focus initially. This was seen using Xvfb and Xephyr on some occasions.
268 focusMainWindow(bot
.shells());
270 Shell shell
= bot
.activeShell().widget
;
272 // Only adjust shell if it appears to be the top-most
273 if (shell
.getParent() == null) {
274 makeShellFullyVisible(shell
);
280 * If the test is running in the UI thread then fail
282 private static void failIfUIThread() {
283 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
284 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"
285 + " that useUIThread is set to false in the pom.xml");
290 * Try to make the shell fully visible in the display. If the shell cannot
291 * fit the display, it will be positioned so that top-left corner is at
292 * <code>(0, 0)</code> in display-relative coordinates.
295 * the shell to make fully visible
297 private static void makeShellFullyVisible(Shell shell
) {
298 Rectangle displayBounds
= shell
.getDisplay().getBounds();
299 Point absCoord
= shell
.toDisplay(0, 0);
300 Point shellSize
= shell
.getSize();
302 Point newLocation
= new Point(absCoord
.x
, absCoord
.y
);
303 newLocation
.x
= Math
.max(0, Math
.min(absCoord
.x
, displayBounds
.width
- shellSize
.x
));
304 newLocation
.y
= Math
.max(0, Math
.min(absCoord
.y
, displayBounds
.height
- shellSize
.y
));
305 if (!newLocation
.equals(absCoord
)) {
306 shell
.setLocation(newLocation
);
311 * Open a trace, this does not perform any validation though
316 * the path of the trace file (absolute or relative)
318 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
320 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
321 openTrace(projectName
, tracePath
, traceType
, true);
325 * Open a trace, this does not perform any validation though
330 * the path of the trace file (absolute or relative)
332 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
334 * delay and wait for jobs
336 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
337 final Exception exception
[] = new Exception
[1];
339 UIThreadRunnable
.syncExec(new VoidResult() {
343 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
344 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
345 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
346 } catch (CoreException e
) {
351 if (exception
[0] != null) {
352 fail(exception
[0].getMessage());
362 * Finds an editor and sets focus to the editor
368 * @return the corresponding SWTBotEditor
370 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
371 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
372 final SWTBotEditor editorBot
= bot
.editor(matcher
);
373 IEditorPart iep
= editorBot
.getReference().getEditor(true);
374 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
376 UIThreadRunnable
.syncExec(new VoidResult() {
383 SWTBotUtils
.waitForJobs();
384 SWTBotUtils
.delay(1000);
385 assertNotNull(tmfEd
);
390 * Opens a trace in an editor and get the TmfEventsEditor
395 * the name of the project that contains the trace
397 * the trace element path (relative to Traces folder)
398 * @return TmfEventsEditor the opened editor
400 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
401 final SWTBotView projectExplorerView
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
402 projectExplorerView
.setFocus();
403 SWTBot projectExplorerBot
= projectExplorerView
.bot();
405 final SWTBotTree tree
= projectExplorerBot
.tree();
406 projectExplorerBot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(projectName
, tree
));
407 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
410 SWTBotTreeItem tracesNode
= getTraceProjectItem(projectExplorerBot
, treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
413 SWTBotTreeItem currentItem
= tracesNode
;
414 for (String segment
: elementPath
.segments()) {
415 currentItem
= getTraceProjectItem(projectExplorerBot
, currentItem
, segment
);
416 currentItem
.select();
417 currentItem
.doubleClick();
420 SWTBotEditor editor
= bot
.editorByTitle(elementPath
.toString());
421 IEditorPart editorPart
= editor
.getReference().getEditor(false);
422 assertTrue(editorPart
instanceof TmfEventsEditor
);
423 return (TmfEventsEditor
) editorPart
;
427 * Returns the child tree item of the specified item with the given name.
428 * The project element label may have a count suffix in the format ' [n]'.
431 * a given workbench bot
433 * the parent tree item
435 * the desired child element name (without suffix)
436 * @return the a {@link SWTBotTreeItem} with the specified name
438 public static SWTBotTreeItem
getTraceProjectItem(SWTBot bot
, final SWTBotTreeItem parentItem
, final String name
) {
439 ProjectElementHasChild condition
= new ProjectElementHasChild(parentItem
, name
);
440 bot
.waitUntil(condition
);
441 return condition
.getItem();
445 * Select the traces folder
448 * a given workbench bot
450 * the name of the project (it needs to exist or else it would
452 * @return a {@link SWTBotTreeItem} of the "Traces" folder
454 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
455 SWTBotTreeItem projectTreeItem
= selectProject(bot
, projectName
);
456 projectTreeItem
.select();
457 SWTBotTreeItem tracesFolderItem
= getTraceProjectItem(bot
, projectTreeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
458 tracesFolderItem
.select();
459 return tracesFolderItem
;
463 * Select the project in Project Explorer
466 * a given workbench bot
468 * the name of the project (it needs to exist or else it would time out)
469 * @return a {@link SWTBotTreeItem} of the project
471 public static SWTBotTreeItem
selectProject(SWTWorkbenchBot bot
, String projectName
) {
472 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
473 projectExplorerBot
.show();
474 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
485 public static void openView(final String id
) {
486 final PartInitException res
[] = new PartInitException
[1];
487 UIThreadRunnable
.syncExec(new VoidResult() {
491 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
492 } catch (PartInitException e
) {
497 if (res
[0] != null) {
498 fail(res
[0].getMessage());
507 * the {@link SWTBotTable} table
509 public static void maximizeTable(SWTBotTable tableBot
) {
511 tableBot
.pressShortcut(KeyStroke
.getInstance(IKeyLookup
.CTRL_NAME
+ "+"), KeyStroke
.getInstance("M"));
512 } catch (ParseException e
) {
518 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
524 * the row of the table to look up
526 * the column of the table to look up
527 * @return the bounds in display relative coordinates
529 public static Rectangle
getCellBounds(final Table table
, final int row
, final int col
) {
530 return UIThreadRunnable
.syncExec(new Result
<Rectangle
>() {
532 public Rectangle
run() {
533 TableItem item
= table
.getItem(row
);
534 Rectangle bounds
= item
.getBounds(col
);
535 Point p
= table
.toDisplay(bounds
.x
, bounds
.y
);
536 Rectangle rect
= new Rectangle(p
.x
, p
.y
, bounds
.width
, bounds
.height
);
543 * Get the tree item from a tree at the specified location
548 * the tree to find the tree item in
550 * the path to the tree item, in the form of node names (from
552 * @return the tree item
554 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTree tree
, String
... nodeNames
) {
555 if (nodeNames
.length
== 0) {
559 bot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(nodeNames
[0], tree
));
560 SWTBotTreeItem currentNode
= tree
.getTreeItem(nodeNames
[0]);
561 for (int i
= 1; i
< nodeNames
.length
; i
++) {
562 currentNode
.expand();
564 String nodeName
= nodeNames
[i
];
565 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
566 SWTBotTreeItem newNode
= currentNode
.getNode(nodeName
);
567 currentNode
= newNode
;
574 * Get the active events editor. Note that this will wait until such editor
577 * @param workbenchBot
578 * a given workbench bot
579 * @return the active events editor
581 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
) {
582 final SWTBotEditor editor
[] = new SWTBotEditor
[1];
583 workbenchBot
.waitUntil(new DefaultCondition() {
585 public boolean test() throws Exception
{
586 List
<SWTBotEditor
> editors
= workbenchBot
.editors(WidgetMatcherFactory
.withPartId(TmfEventsEditor
.ID
));
587 for (SWTBotEditor e
: editors
) {
588 if (e
.isActive() && !e
.getWidget().isDisposed()) {
597 public String
getFailureMessage() {
598 return "Active events editor not found";
605 * Open the preferences dialog and return the corresponding shell.
608 * a given workbench bot
609 * @return the preferences shell
611 public static SWTBotShell
openPreferences(SWTBot bot
) {
612 if (SWTUtils
.isMac()) {
613 // On Mac, the Preferences menu item is under the application name.
614 // For some reason, we can't access the application menu anymore so
615 // we use the keyboard shortcut.
617 bot
.activeShell().pressShortcut(KeyStroke
.getInstance(IKeyLookup
.COMMAND_NAME
+ "+"), KeyStroke
.getInstance(","));
618 } catch (ParseException e
) {
622 bot
.menu(WINDOW_MENU
).menu(PREFERENCES_MENU_ITEM
).click();
625 bot
.waitUntil(Conditions
.shellIsActive(PREFERENCES_MENU_ITEM
));
626 return bot
.activeShell();