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
.IFolder
;
22 import org
.eclipse
.core
.resources
.IProject
;
23 import org
.eclipse
.core
.resources
.IResource
;
24 import org
.eclipse
.core
.resources
.IResourceVisitor
;
25 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
26 import org
.eclipse
.core
.runtime
.CoreException
;
27 import org
.eclipse
.core
.runtime
.IPath
;
28 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
29 import org
.eclipse
.core
.runtime
.jobs
.Job
;
30 import org
.eclipse
.jface
.bindings
.keys
.IKeyLookup
;
31 import org
.eclipse
.jface
.bindings
.keys
.KeyStroke
;
32 import org
.eclipse
.jface
.bindings
.keys
.ParseException
;
33 import org
.eclipse
.swt
.graphics
.Point
;
34 import org
.eclipse
.swt
.graphics
.Rectangle
;
35 import org
.eclipse
.swt
.widgets
.Display
;
36 import org
.eclipse
.swt
.widgets
.Shell
;
37 import org
.eclipse
.swt
.widgets
.Table
;
38 import org
.eclipse
.swt
.widgets
.TableItem
;
39 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
40 import org
.eclipse
.swtbot
.eclipse
.finder
.matchers
.WidgetMatcherFactory
;
41 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
42 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
43 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
44 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
45 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
46 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
47 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTUtils
;
48 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
49 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
50 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
51 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
52 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
53 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
54 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
55 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
56 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
57 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.TimeoutException
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
60 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectElement
;
61 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
62 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
63 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
64 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
65 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
.ProjectElementHasChild
;
66 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TracingPerspectiveFactory
;
67 import org
.eclipse
.ui
.IEditorPart
;
68 import org
.eclipse
.ui
.IEditorReference
;
69 import org
.eclipse
.ui
.IPageLayout
;
70 import org
.eclipse
.ui
.PartInitException
;
71 import org
.eclipse
.ui
.PlatformUI
;
72 import org
.eclipse
.ui
.WorkbenchException
;
73 import org
.hamcrest
.Matcher
;
76 * SWTBot Helper functions
78 * @author Matthew Khouzam
80 public final class SWTBotUtils
{
82 private static final String WINDOW_MENU
= "Window";
83 private static final String PREFERENCES_MENU_ITEM
= "Preferences";
84 private static boolean fPrintedEnvironment
= false;
86 private SWTBotUtils() {
89 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
92 * Waits for all Eclipse jobs to finish
94 public static void waitForJobs() {
95 while (!Job
.getJobManager().isIdle()) {
101 * Sleeps current thread for a given time.
103 * @param waitTimeMillis
104 * time in milliseconds to wait
106 public static void delay(final long waitTimeMillis
) {
108 Thread
.sleep(waitTimeMillis
);
109 } catch (final InterruptedException e
) {
115 * Create a tracing project
118 * the name of the tracing project
120 public static void createProject(final String projectName
) {
124 UIThreadRunnable
.syncExec(new VoidResult() {
127 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
128 assertNotNull(project
);
132 SWTBotUtils
.waitForJobs();
139 * the name of the tracing project
140 * @param deleteResources
141 * whether or not to deleted resources under the project
145 public static void deleteProject(final String projectName
, boolean deleteResources
, SWTWorkbenchBot bot
) {
146 // Wait for any analysis to complete because it might create
147 // supplementary files
148 SWTBotUtils
.waitForJobs();
150 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
151 } catch (CoreException e
) {
154 SWTBotUtils
.waitForJobs();
156 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
157 projectViewBot
.setFocus();
159 SWTBotTree treeBot
= projectViewBot
.bot().tree();
160 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
161 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
164 if (deleteResources
) {
165 bot
.shell("Delete Resources").setFocus();
166 final SWTBotCheckBox checkBox
= bot
.checkBox();
167 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
171 final SWTBotButton okButton
= bot
.button("OK");
172 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
175 SWTBotUtils
.waitForJobs();
179 * Deletes a project and its resources
182 * the name of the tracing project
186 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
187 deleteProject(projectName
, true, bot
);
191 * Focus on the main window
194 * swtbotshells for all the shells
196 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
197 for (SWTBotShell shellBot
: shellBots
) {
198 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
205 * Close a view with a title
208 * the title, like "welcome"
212 public static void closeView(String title
, SWTWorkbenchBot bot
) {
213 final List
<SWTBotView
> openViews
= bot
.views();
214 for (SWTBotView view
: openViews
) {
215 if (view
.getTitle().equalsIgnoreCase(title
)) {
217 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
223 * Close a view with an id
226 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
230 public static void closeViewById(String viewId
, SWTWorkbenchBot bot
) {
231 final SWTBotView view
= bot
.viewById(viewId
);
233 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
237 * Switch to the tracing perspective
239 public static void switchToTracingPerspective() {
240 switchToPerspective(TRACING_PERSPECTIVE_ID
);
244 * Switch to a given perspective
247 * the perspective id (like
248 * "org.eclipse.linuxtools.tmf.ui.perspective"
250 public static void switchToPerspective(final String id
) {
251 UIThreadRunnable
.syncExec(new VoidResult() {
255 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
256 } catch (WorkbenchException e
) {
257 fail(e
.getMessage());
264 * Initialize the environment for SWTBot
266 public static void initialize() {
269 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
270 UIThreadRunnable
.syncExec(() -> {
273 // There seems to be problems on some system where the main shell is
274 // not in focus initially. This was seen using Xvfb and Xephyr on some occasions.
275 focusMainWindow(bot
.shells());
277 Shell shell
= bot
.activeShell().widget
;
279 // Only adjust shell if it appears to be the top-most
280 if (shell
.getParent() == null) {
281 makeShellFullyVisible(shell
);
286 private static void printEnvironment() {
287 if (fPrintedEnvironment
) {
291 // Print some information about the environment that could affect test outcome
292 Rectangle bounds
= Display
.getDefault().getBounds();
293 System
.out
.println("Display size: " + bounds
.width
+ "x" + bounds
.height
);
295 String osVersion
= System
.getProperty("os.version");
296 if (osVersion
!= null) {
297 System
.out
.println("OS version=" + osVersion
);
299 String gtkVersion
= System
.getProperty("org.eclipse.swt.internal.gtk.version");
300 if (gtkVersion
!= null) {
301 System
.out
.println("GTK version=" + gtkVersion
);
302 String overlayScrollbar
= System
.getenv("LIBOVERLAY_SCROLLBAR");
303 if (overlayScrollbar
!= null) {
304 System
.out
.println("LIBOVERLAY_SCROLLBAR=" + overlayScrollbar
);
306 String ubuntuMenuProxy
= System
.getenv("UBUNTU_MENUPROXY");
307 if (ubuntuMenuProxy
!= null) {
308 System
.out
.println("UBUNTU_MENUPROXY=" + ubuntuMenuProxy
);
312 fPrintedEnvironment
= true;
316 * If the test is running in the UI thread then fail
318 private static void failIfUIThread() {
319 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
320 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"
321 + " that useUIThread is set to false in the pom.xml");
326 * Try to make the shell fully visible in the display. If the shell cannot
327 * fit the display, it will be positioned so that top-left corner is at
328 * <code>(0, 0)</code> in display-relative coordinates.
331 * the shell to make fully visible
333 private static void makeShellFullyVisible(Shell shell
) {
334 Rectangle displayBounds
= shell
.getDisplay().getBounds();
335 Point absCoord
= shell
.toDisplay(0, 0);
336 Point shellSize
= shell
.getSize();
338 Point newLocation
= new Point(absCoord
.x
, absCoord
.y
);
339 newLocation
.x
= Math
.max(0, Math
.min(absCoord
.x
, displayBounds
.width
- shellSize
.x
));
340 newLocation
.y
= Math
.max(0, Math
.min(absCoord
.y
, displayBounds
.height
- shellSize
.y
));
341 if (!newLocation
.equals(absCoord
)) {
342 shell
.setLocation(newLocation
);
347 * Open a trace, this does not perform any validation though
352 * the path of the trace file (absolute or relative)
354 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
356 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
357 openTrace(projectName
, tracePath
, traceType
, true);
361 * Open a trace, this does not perform any validation though
366 * the path of the trace file (absolute or relative)
368 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
370 * delay and wait for jobs
372 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
373 final Exception exception
[] = new Exception
[1];
375 UIThreadRunnable
.syncExec(new VoidResult() {
379 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
380 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
381 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
382 } catch (CoreException e
) {
387 if (exception
[0] != null) {
388 fail(exception
[0].getMessage());
398 * Finds an editor and sets focus to the editor
404 * @return the corresponding SWTBotEditor
406 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
407 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
408 final SWTBotEditor editorBot
= bot
.editor(matcher
);
409 IEditorPart iep
= editorBot
.getReference().getEditor(true);
410 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
412 UIThreadRunnable
.syncExec(new VoidResult() {
419 SWTBotUtils
.waitForJobs();
420 SWTBotUtils
.delay(1000);
421 assertNotNull(tmfEd
);
426 * Opens a trace in an editor and get the TmfEventsEditor
431 * the name of the project that contains the trace
433 * the trace element path (relative to Traces folder)
434 * @return TmfEventsEditor the opened editor
436 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
437 final SWTBotView projectExplorerView
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
438 projectExplorerView
.setFocus();
439 SWTBot projectExplorerBot
= projectExplorerView
.bot();
441 final SWTBotTree tree
= projectExplorerBot
.tree();
442 projectExplorerBot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(projectName
, tree
));
443 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
446 SWTBotTreeItem tracesNode
= getTraceProjectItem(projectExplorerBot
, treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
449 SWTBotTreeItem currentItem
= tracesNode
;
450 for (String segment
: elementPath
.segments()) {
451 currentItem
= getTraceProjectItem(projectExplorerBot
, currentItem
, segment
);
452 currentItem
.select();
453 currentItem
.doubleClick();
456 SWTBotEditor editor
= bot
.editorByTitle(elementPath
.toString());
457 IEditorPart editorPart
= editor
.getReference().getEditor(false);
458 assertTrue(editorPart
instanceof TmfEventsEditor
);
459 return (TmfEventsEditor
) editorPart
;
463 * Returns the child tree item of the specified item with the given name.
464 * The project element label may have a count suffix in the format ' [n]'.
467 * a given workbench bot
469 * the parent tree item
471 * the desired child element name (without suffix)
472 * @return the a {@link SWTBotTreeItem} with the specified name
474 public static SWTBotTreeItem
getTraceProjectItem(SWTBot bot
, final SWTBotTreeItem parentItem
, final String name
) {
475 ProjectElementHasChild condition
= new ProjectElementHasChild(parentItem
, name
);
476 bot
.waitUntil(condition
);
477 return condition
.getItem();
481 * Select the traces folder
484 * a given workbench bot
486 * the name of the project (it needs to exist or else it would
488 * @return a {@link SWTBotTreeItem} of the "Traces" folder
490 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
491 SWTBotTreeItem projectTreeItem
= selectProject(bot
, projectName
);
492 projectTreeItem
.select();
493 SWTBotTreeItem tracesFolderItem
= getTraceProjectItem(bot
, projectTreeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
494 tracesFolderItem
.select();
495 return tracesFolderItem
;
499 * Clear the traces folder
502 * a given workbench bot
504 * the name of the project (needs to exist)
506 public static void clearTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
507 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
508 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
509 TmfTraceFolder tracesFolder
= tmfProject
.getTracesFolder();
511 for (TmfTraceElement traceElement
: tracesFolder
.getTraces()) {
512 traceElement
.delete(null);
515 final IFolder resource
= tracesFolder
.getResource();
516 resource
.accept(new IResourceVisitor() {
518 public boolean visit(IResource visitedResource
) throws CoreException
{
519 if (visitedResource
!= resource
) {
520 visitedResource
.delete(true, null);
524 }, IResource
.DEPTH_ONE
, 0);
525 } catch (CoreException e
) {
526 fail(e
.getMessage());
529 bot
.waitUntil(new DefaultCondition() {
530 private int fTraceNb
= 0;
533 public boolean test() throws Exception
{
534 List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
535 fTraceNb
= traces
.size();
536 return fTraceNb
== 0;
540 public String
getFailureMessage() {
541 return "Traces Folder not empty (" + fTraceNb
+ ")";
548 * Select the project in Project Explorer
551 * a given workbench bot
553 * the name of the project (it needs to exist or else it would time out)
554 * @return a {@link SWTBotTreeItem} of the project
556 public static SWTBotTreeItem
selectProject(SWTWorkbenchBot bot
, String projectName
) {
557 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
558 projectExplorerBot
.show();
559 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
570 public static void openView(final String id
) {
571 final PartInitException res
[] = new PartInitException
[1];
572 UIThreadRunnable
.syncExec(new VoidResult() {
576 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
577 } catch (PartInitException e
) {
582 if (res
[0] != null) {
583 fail(res
[0].getMessage());
592 * the {@link SWTBotTable} table
594 public static void maximizeTable(SWTBotTable tableBot
) {
596 tableBot
.pressShortcut(KeyStroke
.getInstance(IKeyLookup
.CTRL_NAME
+ "+"), KeyStroke
.getInstance("M"));
597 } catch (ParseException e
) {
603 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
609 * the row of the table to look up
611 * the column of the table to look up
612 * @return the bounds in display relative coordinates
614 public static Rectangle
getCellBounds(final Table table
, final int row
, final int col
) {
615 return UIThreadRunnable
.syncExec(new Result
<Rectangle
>() {
617 public Rectangle
run() {
618 TableItem item
= table
.getItem(row
);
619 Rectangle bounds
= item
.getBounds(col
);
620 Point p
= table
.toDisplay(bounds
.x
, bounds
.y
);
621 Rectangle rect
= new Rectangle(p
.x
, p
.y
, bounds
.width
, bounds
.height
);
628 * Get the tree item from a tree at the specified location
633 * the tree to find the tree item in
635 * the path to the tree item, in the form of node names (from
637 * @return the tree item
639 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTree tree
, String
... nodeNames
) {
640 if (nodeNames
.length
== 0) {
644 bot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(nodeNames
[0], tree
));
645 SWTBotTreeItem currentNode
= tree
.getTreeItem(nodeNames
[0]);
646 for (int i
= 1; i
< nodeNames
.length
; i
++) {
647 currentNode
.expand();
649 String nodeName
= nodeNames
[i
];
651 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
652 } catch (TimeoutException e
) {
653 //FIXME: Sometimes in a JFace TreeViewer, it expands to nothing. Need to find out why.
654 currentNode
.collapse();
655 currentNode
.expand();
656 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
659 SWTBotTreeItem newNode
= currentNode
.getNode(nodeName
);
660 currentNode
= newNode
;
667 * Get the active events editor. Note that this will wait until such editor
670 * @param workbenchBot
671 * a given workbench bot
672 * @return the active events editor
674 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
) {
675 final SWTBotEditor editor
[] = new SWTBotEditor
[1];
676 workbenchBot
.waitUntil(new DefaultCondition() {
678 public boolean test() throws Exception
{
679 List
<SWTBotEditor
> editors
= workbenchBot
.editors(WidgetMatcherFactory
.withPartId(TmfEventsEditor
.ID
));
680 for (SWTBotEditor e
: editors
) {
681 if (e
.isActive() && !e
.getWidget().isDisposed()) {
690 public String
getFailureMessage() {
691 return "Active events editor not found";
698 * Open the preferences dialog and return the corresponding shell.
701 * a given workbench bot
702 * @return the preferences shell
704 public static SWTBotShell
openPreferences(SWTBot bot
) {
705 if (SWTUtils
.isMac()) {
706 // On Mac, the Preferences menu item is under the application name.
707 // For some reason, we can't access the application menu anymore so
708 // we use the keyboard shortcut.
710 bot
.activeShell().pressShortcut(KeyStroke
.getInstance(IKeyLookup
.COMMAND_NAME
+ "+"), KeyStroke
.getInstance(","));
711 } catch (ParseException e
) {
715 bot
.menu(WINDOW_MENU
).menu(PREFERENCES_MENU_ITEM
).click();
718 bot
.waitUntil(Conditions
.shellIsActive(PREFERENCES_MENU_ITEM
));
719 return bot
.activeShell();