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
.swtbot
.swt
.finder
.widgets
.TimeoutException
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
57 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
60 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
61 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
.ProjectElementHasChild
;
62 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TracingPerspectiveFactory
;
63 import org
.eclipse
.ui
.IEditorPart
;
64 import org
.eclipse
.ui
.IEditorReference
;
65 import org
.eclipse
.ui
.IPageLayout
;
66 import org
.eclipse
.ui
.PartInitException
;
67 import org
.eclipse
.ui
.PlatformUI
;
68 import org
.eclipse
.ui
.WorkbenchException
;
69 import org
.hamcrest
.Matcher
;
72 * SWTBot Helper functions
74 * @author Matthew Khouzam
76 public final class SWTBotUtils
{
78 private static final String WINDOW_MENU
= "Window";
79 private static final String PREFERENCES_MENU_ITEM
= "Preferences";
81 private SWTBotUtils() {
84 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
87 * Waits for all Eclipse jobs to finish
89 public static void waitForJobs() {
90 while (!Job
.getJobManager().isIdle()) {
96 * Sleeps current thread for a given time.
98 * @param waitTimeMillis
99 * time in milliseconds to wait
101 public static void delay(final long waitTimeMillis
) {
103 Thread
.sleep(waitTimeMillis
);
104 } catch (final InterruptedException e
) {
110 * Create a tracing project
113 * the name of the tracing project
115 public static void createProject(final String projectName
) {
119 UIThreadRunnable
.syncExec(new VoidResult() {
122 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
123 assertNotNull(project
);
127 SWTBotUtils
.waitForJobs();
134 * the name of the tracing project
135 * @param deleteResources
136 * whether or not to deleted resources under the project
140 public static void deleteProject(final String projectName
, boolean deleteResources
, SWTWorkbenchBot bot
) {
141 // Wait for any analysis to complete because it might create
142 // supplementary files
143 SWTBotUtils
.waitForJobs();
145 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
146 } catch (CoreException e
) {
149 SWTBotUtils
.waitForJobs();
151 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
152 projectViewBot
.setFocus();
154 SWTBotTree treeBot
= projectViewBot
.bot().tree();
155 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
156 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
159 if (deleteResources
) {
160 bot
.shell("Delete Resources").setFocus();
161 final SWTBotCheckBox checkBox
= bot
.checkBox();
162 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
166 final SWTBotButton okButton
= bot
.button("OK");
167 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
170 SWTBotUtils
.waitForJobs();
174 * Deletes a project and its resources
177 * the name of the tracing project
181 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
182 deleteProject(projectName
, true, bot
);
186 * Focus on the main window
189 * swtbotshells for all the shells
191 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
192 for (SWTBotShell shellBot
: shellBots
) {
193 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
200 * Close a view with a title
203 * the title, like "welcome"
207 public static void closeView(String title
, SWTWorkbenchBot bot
) {
208 final List
<SWTBotView
> openViews
= bot
.views();
209 for (SWTBotView view
: openViews
) {
210 if (view
.getTitle().equalsIgnoreCase(title
)) {
212 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
218 * Close a view with an id
221 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
225 public static void closeViewById(String viewId
, SWTWorkbenchBot bot
) {
226 final SWTBotView view
= bot
.viewById(viewId
);
228 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
232 * Switch to the tracing perspective
234 public static void switchToTracingPerspective() {
235 switchToPerspective(TRACING_PERSPECTIVE_ID
);
239 * Switch to a given perspective
242 * the perspective id (like
243 * "org.eclipse.linuxtools.tmf.ui.perspective"
245 public static void switchToPerspective(final String id
) {
246 UIThreadRunnable
.syncExec(new VoidResult() {
250 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
251 } catch (WorkbenchException e
) {
252 fail(e
.getMessage());
259 * Initialize the environment for SWTBot
261 public static void initialize() {
264 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
265 UIThreadRunnable
.syncExec(() -> {
267 // There seems to be problems on some system where the main shell is
268 // not in focus initially. This was seen using Xvfb and Xephyr on some occasions.
269 focusMainWindow(bot
.shells());
271 Shell shell
= bot
.activeShell().widget
;
273 // Only adjust shell if it appears to be the top-most
274 if (shell
.getParent() == null) {
275 makeShellFullyVisible(shell
);
281 * If the test is running in the UI thread then fail
283 private static void failIfUIThread() {
284 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
285 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"
286 + " that useUIThread is set to false in the pom.xml");
291 * Try to make the shell fully visible in the display. If the shell cannot
292 * fit the display, it will be positioned so that top-left corner is at
293 * <code>(0, 0)</code> in display-relative coordinates.
296 * the shell to make fully visible
298 private static void makeShellFullyVisible(Shell shell
) {
299 Rectangle displayBounds
= shell
.getDisplay().getBounds();
300 Point absCoord
= shell
.toDisplay(0, 0);
301 Point shellSize
= shell
.getSize();
303 Point newLocation
= new Point(absCoord
.x
, absCoord
.y
);
304 newLocation
.x
= Math
.max(0, Math
.min(absCoord
.x
, displayBounds
.width
- shellSize
.x
));
305 newLocation
.y
= Math
.max(0, Math
.min(absCoord
.y
, displayBounds
.height
- shellSize
.y
));
306 if (!newLocation
.equals(absCoord
)) {
307 shell
.setLocation(newLocation
);
312 * Open a trace, this does not perform any validation though
317 * the path of the trace file (absolute or relative)
319 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
321 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
322 openTrace(projectName
, tracePath
, traceType
, true);
326 * Open a trace, this does not perform any validation though
331 * the path of the trace file (absolute or relative)
333 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
335 * delay and wait for jobs
337 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
338 final Exception exception
[] = new Exception
[1];
340 UIThreadRunnable
.syncExec(new VoidResult() {
344 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
345 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
346 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
347 } catch (CoreException e
) {
352 if (exception
[0] != null) {
353 fail(exception
[0].getMessage());
363 * Finds an editor and sets focus to the editor
369 * @return the corresponding SWTBotEditor
371 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
372 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
373 final SWTBotEditor editorBot
= bot
.editor(matcher
);
374 IEditorPart iep
= editorBot
.getReference().getEditor(true);
375 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
377 UIThreadRunnable
.syncExec(new VoidResult() {
384 SWTBotUtils
.waitForJobs();
385 SWTBotUtils
.delay(1000);
386 assertNotNull(tmfEd
);
391 * Opens a trace in an editor and get the TmfEventsEditor
396 * the name of the project that contains the trace
398 * the trace element path (relative to Traces folder)
399 * @return TmfEventsEditor the opened editor
401 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
402 final SWTBotView projectExplorerView
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
403 projectExplorerView
.setFocus();
404 SWTBot projectExplorerBot
= projectExplorerView
.bot();
406 final SWTBotTree tree
= projectExplorerBot
.tree();
407 projectExplorerBot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(projectName
, tree
));
408 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
411 SWTBotTreeItem tracesNode
= getTraceProjectItem(projectExplorerBot
, treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
414 SWTBotTreeItem currentItem
= tracesNode
;
415 for (String segment
: elementPath
.segments()) {
416 currentItem
= getTraceProjectItem(projectExplorerBot
, currentItem
, segment
);
417 currentItem
.select();
418 currentItem
.doubleClick();
421 SWTBotEditor editor
= bot
.editorByTitle(elementPath
.toString());
422 IEditorPart editorPart
= editor
.getReference().getEditor(false);
423 assertTrue(editorPart
instanceof TmfEventsEditor
);
424 return (TmfEventsEditor
) editorPart
;
428 * Returns the child tree item of the specified item with the given name.
429 * The project element label may have a count suffix in the format ' [n]'.
432 * a given workbench bot
434 * the parent tree item
436 * the desired child element name (without suffix)
437 * @return the a {@link SWTBotTreeItem} with the specified name
439 public static SWTBotTreeItem
getTraceProjectItem(SWTBot bot
, final SWTBotTreeItem parentItem
, final String name
) {
440 ProjectElementHasChild condition
= new ProjectElementHasChild(parentItem
, name
);
441 bot
.waitUntil(condition
);
442 return condition
.getItem();
446 * Select the traces folder
449 * a given workbench bot
451 * the name of the project (it needs to exist or else it would
453 * @return a {@link SWTBotTreeItem} of the "Traces" folder
455 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
456 SWTBotTreeItem projectTreeItem
= selectProject(bot
, projectName
);
457 projectTreeItem
.select();
458 SWTBotTreeItem tracesFolderItem
= getTraceProjectItem(bot
, projectTreeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
459 tracesFolderItem
.select();
460 return tracesFolderItem
;
464 * Select the project in Project Explorer
467 * a given workbench bot
469 * the name of the project (it needs to exist or else it would time out)
470 * @return a {@link SWTBotTreeItem} of the project
472 public static SWTBotTreeItem
selectProject(SWTWorkbenchBot bot
, String projectName
) {
473 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
474 projectExplorerBot
.show();
475 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
486 public static void openView(final String id
) {
487 final PartInitException res
[] = new PartInitException
[1];
488 UIThreadRunnable
.syncExec(new VoidResult() {
492 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
493 } catch (PartInitException e
) {
498 if (res
[0] != null) {
499 fail(res
[0].getMessage());
508 * the {@link SWTBotTable} table
510 public static void maximizeTable(SWTBotTable tableBot
) {
512 tableBot
.pressShortcut(KeyStroke
.getInstance(IKeyLookup
.CTRL_NAME
+ "+"), KeyStroke
.getInstance("M"));
513 } catch (ParseException e
) {
519 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
525 * the row of the table to look up
527 * the column of the table to look up
528 * @return the bounds in display relative coordinates
530 public static Rectangle
getCellBounds(final Table table
, final int row
, final int col
) {
531 return UIThreadRunnable
.syncExec(new Result
<Rectangle
>() {
533 public Rectangle
run() {
534 TableItem item
= table
.getItem(row
);
535 Rectangle bounds
= item
.getBounds(col
);
536 Point p
= table
.toDisplay(bounds
.x
, bounds
.y
);
537 Rectangle rect
= new Rectangle(p
.x
, p
.y
, bounds
.width
, bounds
.height
);
544 * Get the tree item from a tree at the specified location
549 * the tree to find the tree item in
551 * the path to the tree item, in the form of node names (from
553 * @return the tree item
555 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTree tree
, String
... nodeNames
) {
556 if (nodeNames
.length
== 0) {
560 bot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(nodeNames
[0], tree
));
561 SWTBotTreeItem currentNode
= tree
.getTreeItem(nodeNames
[0]);
562 for (int i
= 1; i
< nodeNames
.length
; i
++) {
563 currentNode
.expand();
565 String nodeName
= nodeNames
[i
];
567 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
568 } catch (TimeoutException e
) {
569 //FIXME: Sometimes in a JFace TreeViewer, it expands to nothing. Need to find out why.
570 currentNode
.collapse();
571 currentNode
.expand();
572 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
575 SWTBotTreeItem newNode
= currentNode
.getNode(nodeName
);
576 currentNode
= newNode
;
583 * Get the active events editor. Note that this will wait until such editor
586 * @param workbenchBot
587 * a given workbench bot
588 * @return the active events editor
590 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
) {
591 final SWTBotEditor editor
[] = new SWTBotEditor
[1];
592 workbenchBot
.waitUntil(new DefaultCondition() {
594 public boolean test() throws Exception
{
595 List
<SWTBotEditor
> editors
= workbenchBot
.editors(WidgetMatcherFactory
.withPartId(TmfEventsEditor
.ID
));
596 for (SWTBotEditor e
: editors
) {
597 if (e
.isActive() && !e
.getWidget().isDisposed()) {
606 public String
getFailureMessage() {
607 return "Active events editor not found";
614 * Open the preferences dialog and return the corresponding shell.
617 * a given workbench bot
618 * @return the preferences shell
620 public static SWTBotShell
openPreferences(SWTBot bot
) {
621 if (SWTUtils
.isMac()) {
622 // On Mac, the Preferences menu item is under the application name.
623 // For some reason, we can't access the application menu anymore so
624 // we use the keyboard shortcut.
626 bot
.activeShell().pressShortcut(KeyStroke
.getInstance(IKeyLookup
.COMMAND_NAME
+ "+"), KeyStroke
.getInstance(","));
627 } catch (ParseException e
) {
631 bot
.menu(WINDOW_MENU
).menu(PREFERENCES_MENU_ITEM
).click();
634 bot
.waitUntil(Conditions
.shellIsActive(PREFERENCES_MENU_ITEM
));
635 return bot
.activeShell();