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
.waits
.Conditions
;
46 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
47 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
48 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
49 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
50 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
51 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
52 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
53 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
54 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
57 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
.ProjectElementHasChild
;
60 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TracingPerspectiveFactory
;
61 import org
.eclipse
.ui
.IEditorPart
;
62 import org
.eclipse
.ui
.IEditorReference
;
63 import org
.eclipse
.ui
.IPageLayout
;
64 import org
.eclipse
.ui
.PartInitException
;
65 import org
.eclipse
.ui
.PlatformUI
;
66 import org
.eclipse
.ui
.WorkbenchException
;
67 import org
.hamcrest
.Matcher
;
70 * SWTBot Helper functions
72 * @author Matthew Khouzam
74 public final class SWTBotUtils
{
76 private SWTBotUtils() {
79 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
82 * Waits for all Eclipse jobs to finish
84 public static void waitForJobs() {
85 while (!Job
.getJobManager().isIdle()) {
91 * Sleeps current thread for a given time.
93 * @param waitTimeMillis
94 * time in milliseconds to wait
96 public static void delay(final long waitTimeMillis
) {
98 Thread
.sleep(waitTimeMillis
);
99 } catch (final InterruptedException e
) {
105 * Create a tracing project
108 * the name of the tracing project
110 public static void createProject(final String projectName
) {
114 UIThreadRunnable
.syncExec(new VoidResult() {
117 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
118 assertNotNull(project
);
122 SWTBotUtils
.waitForJobs();
129 * the name of the tracing project
130 * @param deleteResources
131 * whether or not to deleted resources under the project
135 public static void deleteProject(final String projectName
, boolean deleteResources
, SWTWorkbenchBot bot
) {
136 // Wait for any analysis to complete because it might create
137 // supplementary files
138 SWTBotUtils
.waitForJobs();
140 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
141 } catch (CoreException e
) {
144 SWTBotUtils
.waitForJobs();
146 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
147 projectViewBot
.setFocus();
149 SWTBotTree treeBot
= projectViewBot
.bot().tree();
150 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
151 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
154 if (deleteResources
) {
155 bot
.shell("Delete Resources").setFocus();
156 final SWTBotCheckBox checkBox
= bot
.checkBox();
157 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
161 final SWTBotButton okButton
= bot
.button("OK");
162 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
165 SWTBotUtils
.waitForJobs();
169 * Deletes a project and its resources
172 * the name of the tracing project
176 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
177 deleteProject(projectName
, true, bot
);
181 * Focus on the main window
184 * swtbotshells for all the shells
186 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
187 for (SWTBotShell shellBot
: shellBots
) {
188 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
195 * Close a view with a title
198 * the title, like "welcome"
202 public static void closeView(String title
, SWTWorkbenchBot bot
) {
203 final List
<SWTBotView
> openViews
= bot
.views();
204 for (SWTBotView view
: openViews
) {
205 if (view
.getTitle().equalsIgnoreCase(title
)) {
207 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
213 * Close a view with an id
216 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
220 public static void closeViewById(String viewId
, SWTWorkbenchBot bot
) {
221 final SWTBotView view
= bot
.viewById(viewId
);
223 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
227 * Switch to the tracing perspective
229 public static void switchToTracingPerspective() {
230 switchToPerspective(TRACING_PERSPECTIVE_ID
);
234 * Switch to a given perspective
237 * the perspective id (like
238 * "org.eclipse.linuxtools.tmf.ui.perspective"
240 public static void switchToPerspective(final String id
) {
241 UIThreadRunnable
.syncExec(new VoidResult() {
245 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
246 } catch (WorkbenchException e
) {
247 fail(e
.getMessage());
254 * Initialize the environment for SWTBot
256 public static void initialize() {
259 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
260 UIThreadRunnable
.syncExec(() -> {
261 Shell shell
= bot
.activeShell().widget
;
263 // Only adjust shell if it appears to be the top-most
264 if (shell
.getParent() == null) {
265 makeShellFullyVisible(shell
);
271 * If the test is running in the UI thread then fail
273 private static void failIfUIThread() {
274 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
275 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"
276 + " that useUIThread is set to false in the pom.xml");
281 * Try to make the shell fully visible in the display. If the shell cannot
282 * fit the display, it will be positioned so that top-left corner is at
283 * <code>(0, 0)</code> in display-relative coordinates.
286 * the shell to make fully visible
288 private static void makeShellFullyVisible(Shell shell
) {
289 Rectangle displayBounds
= shell
.getDisplay().getBounds();
290 Point absCoord
= shell
.toDisplay(0, 0);
291 Point shellSize
= shell
.getSize();
293 Point newLocation
= new Point(absCoord
.x
, absCoord
.y
);
294 newLocation
.x
= Math
.max(0, Math
.min(absCoord
.x
, displayBounds
.width
- shellSize
.x
));
295 newLocation
.y
= Math
.max(0, Math
.min(absCoord
.y
, displayBounds
.height
- shellSize
.y
));
296 if (!newLocation
.equals(absCoord
)) {
297 shell
.setLocation(newLocation
);
302 * Open a trace, this does not perform any validation though
307 * the path of the trace file (absolute or relative)
309 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
311 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
312 openTrace(projectName
, tracePath
, traceType
, true);
316 * Open a trace, this does not perform any validation though
321 * the path of the trace file (absolute or relative)
323 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
325 * delay and wait for jobs
327 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
328 final Exception exception
[] = new Exception
[1];
330 UIThreadRunnable
.syncExec(new VoidResult() {
334 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
335 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
336 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
337 } catch (CoreException e
) {
342 if (exception
[0] != null) {
343 fail(exception
[0].getMessage());
353 * Finds an editor and sets focus to the editor
359 * @return the corresponding SWTBotEditor
361 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
362 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
363 final SWTBotEditor editorBot
= bot
.editor(matcher
);
364 IEditorPart iep
= editorBot
.getReference().getEditor(true);
365 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
367 UIThreadRunnable
.syncExec(new VoidResult() {
374 SWTBotUtils
.waitForJobs();
375 SWTBotUtils
.delay(1000);
376 assertNotNull(tmfEd
);
381 * Opens a trace in an editor and get the TmfEventsEditor
386 * the name of the project that contains the trace
388 * the trace element path (relative to Traces folder)
389 * @return TmfEventsEditor the opened editor
391 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
392 final SWTBotView projectExplorerView
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
393 projectExplorerView
.setFocus();
394 SWTBot projectExplorerBot
= projectExplorerView
.bot();
396 final SWTBotTree tree
= projectExplorerBot
.tree();
397 projectExplorerBot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(projectName
, tree
));
398 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
401 SWTBotTreeItem tracesNode
= getTraceProjectItem(projectExplorerBot
, treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
404 SWTBotTreeItem currentItem
= tracesNode
;
405 for (String segment
: elementPath
.segments()) {
406 currentItem
= getTraceProjectItem(projectExplorerBot
, currentItem
, segment
);
407 currentItem
.select();
408 currentItem
.doubleClick();
411 SWTBotEditor editor
= bot
.editorByTitle(elementPath
.toString());
412 IEditorPart editorPart
= editor
.getReference().getEditor(false);
413 assertTrue(editorPart
instanceof TmfEventsEditor
);
414 return (TmfEventsEditor
) editorPart
;
418 * Returns the child tree item of the specified item with the given name.
419 * The project element label may have a count suffix in the format ' [n]'.
422 * a given workbench bot
424 * the parent tree item
426 * the desired child element name (without suffix)
427 * @return the a {@link SWTBotTreeItem} with the specified name
429 public static SWTBotTreeItem
getTraceProjectItem(SWTBot bot
, final SWTBotTreeItem parentItem
, final String name
) {
430 ProjectElementHasChild condition
= new ProjectElementHasChild(parentItem
, name
);
431 bot
.waitUntil(condition
);
432 return condition
.getItem();
436 * Select the traces folder
439 * a given workbench bot
441 * the name of the project (it needs to exist or else it would
443 * @return a {@link SWTBotTreeItem} of the "Traces" folder
445 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
446 SWTBotTreeItem projectTreeItem
= selectProject(bot
, projectName
);
447 projectTreeItem
.select();
448 SWTBotTreeItem tracesFolderItem
= getTraceProjectItem(bot
, projectTreeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
449 tracesFolderItem
.select();
450 return tracesFolderItem
;
454 * Select the project in Project Explorer
457 * a given workbench bot
459 * the name of the project (it needs to exist or else it would time out)
460 * @return a {@link SWTBotTreeItem} of the project
462 public static SWTBotTreeItem
selectProject(SWTWorkbenchBot bot
, String projectName
) {
463 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
464 projectExplorerBot
.show();
465 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
476 public static void openView(final String id
) {
477 final PartInitException res
[] = new PartInitException
[1];
478 UIThreadRunnable
.syncExec(new VoidResult() {
482 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
483 } catch (PartInitException e
) {
488 if (res
[0] != null) {
489 fail(res
[0].getMessage());
498 * the {@link SWTBotTable} table
500 public static void maximizeTable(SWTBotTable tableBot
) {
502 tableBot
.pressShortcut(KeyStroke
.getInstance(IKeyLookup
.CTRL_NAME
+ "+"), KeyStroke
.getInstance("M"));
503 } catch (ParseException e
) {
509 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
515 * the row of the table to look up
517 * the column of the table to look up
518 * @return the bounds in display relative coordinates
520 public static Rectangle
getCellBounds(final Table table
, final int row
, final int col
) {
521 return UIThreadRunnable
.syncExec(new Result
<Rectangle
>() {
523 public Rectangle
run() {
524 TableItem item
= table
.getItem(row
);
525 Rectangle bounds
= item
.getBounds(col
);
526 Point p
= table
.toDisplay(bounds
.x
, bounds
.y
);
527 Rectangle rect
= new Rectangle(p
.x
, p
.y
, bounds
.width
, bounds
.height
);
534 * Get the tree item from a tree at the specified location
539 * the tree to find the tree item in
541 * the path to the tree item, in the form of node names (from
543 * @return the tree item
545 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTree tree
, String
... nodeNames
) {
546 if (nodeNames
.length
== 0) {
550 bot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(nodeNames
[0], tree
));
551 SWTBotTreeItem currentNode
= tree
.getTreeItem(nodeNames
[0]);
552 for (int i
= 1; i
< nodeNames
.length
; i
++) {
553 currentNode
.expand();
555 String nodeName
= nodeNames
[i
];
556 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
557 SWTBotTreeItem newNode
= currentNode
.getNode(nodeName
);
558 currentNode
= newNode
;
565 * Get the active events editor. Note that this will wait until such editor
568 * @param workbenchBot
569 * a given workbench bot
570 * @return the active events editor
572 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
) {
573 final SWTBotEditor editor
[] = new SWTBotEditor
[1];
574 workbenchBot
.waitUntil(new DefaultCondition() {
576 public boolean test() throws Exception
{
577 List
<SWTBotEditor
> editors
= workbenchBot
.editors(WidgetMatcherFactory
.withPartId(TmfEventsEditor
.ID
));
578 for (SWTBotEditor e
: editors
) {
579 if (e
.isActive() && !e
.getWidget().isDisposed()) {
588 public String
getFailureMessage() {
589 return "Active events editor not found";