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
.Table
;
35 import org
.eclipse
.swt
.widgets
.TableItem
;
36 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
37 import org
.eclipse
.swtbot
.eclipse
.finder
.matchers
.WidgetMatcherFactory
;
38 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
39 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
40 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
41 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
42 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
43 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
44 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
45 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
46 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
47 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
48 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
49 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
50 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
51 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
52 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
53 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
54 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
57 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
.ProjectElementHasChild
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TracingPerspectiveFactory
;
59 import org
.eclipse
.ui
.IEditorPart
;
60 import org
.eclipse
.ui
.IEditorReference
;
61 import org
.eclipse
.ui
.IPageLayout
;
62 import org
.eclipse
.ui
.PartInitException
;
63 import org
.eclipse
.ui
.PlatformUI
;
64 import org
.eclipse
.ui
.WorkbenchException
;
65 import org
.hamcrest
.Matcher
;
68 * SWTBot Helper functions
70 * @author Matthew Khouzam
72 public final class SWTBotUtils
{
74 private SWTBotUtils() {
77 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
80 * Waits for all Eclipse jobs to finish
82 public static void waitForJobs() {
83 while (!Job
.getJobManager().isIdle()) {
89 * Sleeps current thread for a given time.
91 * @param waitTimeMillis
92 * time in milliseconds to wait
94 public static void delay(final long waitTimeMillis
) {
96 Thread
.sleep(waitTimeMillis
);
97 } catch (final InterruptedException e
) {
103 * Create a tracing project
106 * the name of the tracing project
108 public static void createProject(final String projectName
) {
112 UIThreadRunnable
.syncExec(new VoidResult() {
115 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
116 assertNotNull(project
);
120 SWTBotUtils
.waitForJobs();
127 * the name of the tracing project
128 * @param deleteResources
129 * whether or not to deleted resources under the project
133 public static void deleteProject(final String projectName
, boolean deleteResources
, SWTWorkbenchBot bot
) {
134 // Wait for any analysis to complete because it might create
135 // supplementary files
136 SWTBotUtils
.waitForJobs();
138 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
139 } catch (CoreException e
) {
142 SWTBotUtils
.waitForJobs();
144 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
145 projectViewBot
.setFocus();
147 SWTBotTree treeBot
= projectViewBot
.bot().tree();
148 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
149 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
152 if (deleteResources
) {
153 bot
.shell("Delete Resources").setFocus();
154 final SWTBotCheckBox checkBox
= bot
.checkBox();
155 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
159 final SWTBotButton okButton
= bot
.button("OK");
160 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
163 SWTBotUtils
.waitForJobs();
167 * Deletes a project and its resources
170 * the name of the tracing project
174 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
175 deleteProject(projectName
, true, bot
);
179 * Focus on the main window
182 * swtbotshells for all the shells
184 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
185 for (SWTBotShell shellBot
: shellBots
) {
186 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
193 * Close a view with a title
196 * the title, like "welcome"
200 public static void closeView(String title
, SWTWorkbenchBot bot
) {
201 final List
<SWTBotView
> openViews
= bot
.views();
202 for (SWTBotView view
: openViews
) {
203 if (view
.getTitle().equalsIgnoreCase(title
)) {
205 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
211 * Close a view with an id
214 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
218 public static void closeViewById(String viewId
, SWTWorkbenchBot bot
) {
219 final SWTBotView view
= bot
.viewById(viewId
);
221 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
225 * Switch to the tracing perspective
227 public static void switchToTracingPerspective() {
228 switchToPerspective(TRACING_PERSPECTIVE_ID
);
232 * Switch to a given perspective
235 * the perspective id (like
236 * "org.eclipse.linuxtools.tmf.ui.perspective"
238 public static void switchToPerspective(final String id
) {
239 UIThreadRunnable
.syncExec(new VoidResult() {
243 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
244 } catch (WorkbenchException e
) {
245 fail(e
.getMessage());
252 * If the test is running in the UI thread then fail
254 public static void failIfUIThread() {
255 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
256 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"
257 + " that useUIThread is set to false in the pom.xml");
263 * Open a trace, this does not perform any validation though
268 * the path of the trace file (absolute or relative)
270 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
272 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
273 openTrace(projectName
, tracePath
, traceType
, true);
277 * Open a trace, this does not perform any validation though
282 * the path of the trace file (absolute or relative)
284 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
286 * delay and wait for jobs
288 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
289 final Exception exception
[] = new Exception
[1];
291 UIThreadRunnable
.syncExec(new VoidResult() {
295 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
296 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
297 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
298 } catch (CoreException e
) {
303 if (exception
[0] != null) {
304 fail(exception
[0].getMessage());
314 * Finds an editor and sets focus to the editor
320 * @return the corresponding SWTBotEditor
322 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
323 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
324 final SWTBotEditor editorBot
= bot
.editor(matcher
);
325 IEditorPart iep
= editorBot
.getReference().getEditor(true);
326 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
328 UIThreadRunnable
.syncExec(new VoidResult() {
335 SWTBotUtils
.waitForJobs();
336 SWTBotUtils
.delay(1000);
337 assertNotNull(tmfEd
);
342 * Opens a trace in an editor and get the TmfEventsEditor
347 * the name of the project that contains the trace
349 * the trace element path (relative to Traces folder)
350 * @return TmfEventsEditor the opened editor
352 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
353 final SWTBotView projectExplorerView
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
354 projectExplorerView
.setFocus();
355 SWTBot projectExplorerBot
= projectExplorerView
.bot();
357 final SWTBotTree tree
= projectExplorerBot
.tree();
358 projectExplorerBot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(projectName
, tree
));
359 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
362 SWTBotTreeItem tracesNode
= getTraceProjectItem(projectExplorerBot
, treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
365 SWTBotTreeItem currentItem
= tracesNode
;
366 for (String segment
: elementPath
.segments()) {
367 currentItem
= getTraceProjectItem(projectExplorerBot
, currentItem
, segment
);
368 currentItem
.select();
369 currentItem
.doubleClick();
372 SWTBotEditor editor
= bot
.editorByTitle(elementPath
.toString());
373 IEditorPart editorPart
= editor
.getReference().getEditor(false);
374 assertTrue(editorPart
instanceof TmfEventsEditor
);
375 return (TmfEventsEditor
) editorPart
;
379 * Returns the child tree item of the specified item with the given name.
380 * The project element label may have a count suffix in the format ' [n]'.
383 * a given workbench bot
385 * the parent tree item
387 * the desired child element name (without suffix)
388 * @return the a {@link SWTBotTreeItem} with the specified name
390 public static SWTBotTreeItem
getTraceProjectItem(SWTBot bot
, final SWTBotTreeItem parentItem
, final String name
) {
391 ProjectElementHasChild condition
= new ProjectElementHasChild(parentItem
, name
);
392 bot
.waitUntil(condition
);
393 return condition
.getItem();
397 * Select the traces folder
400 * a given workbench bot
402 * the name of the project (it needs to exist or else it would
404 * @return a {@link SWTBotTreeItem} of the "Traces" folder
406 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
407 SWTBotTreeItem projectTreeItem
= selectProject(bot
, projectName
);
408 projectTreeItem
.select();
409 SWTBotTreeItem tracesFolderItem
= getTraceProjectItem(bot
, projectTreeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
410 tracesFolderItem
.select();
411 return tracesFolderItem
;
415 * Select the project in Project Explorer
418 * a given workbench bot
420 * the name of the project (it needs to exist or else it would time out)
421 * @return a {@link SWTBotTreeItem} of the project
423 public static SWTBotTreeItem
selectProject(SWTWorkbenchBot bot
, String projectName
) {
424 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
425 projectExplorerBot
.show();
426 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
437 public static void openView(final String id
) {
438 final PartInitException res
[] = new PartInitException
[1];
439 UIThreadRunnable
.syncExec(new VoidResult() {
443 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
444 } catch (PartInitException e
) {
449 if (res
[0] != null) {
450 fail(res
[0].getMessage());
459 * the {@link SWTBotTable} table
461 public static void maximizeTable(SWTBotTable tableBot
) {
463 tableBot
.pressShortcut(KeyStroke
.getInstance(IKeyLookup
.CTRL_NAME
+ "+"), KeyStroke
.getInstance("M"));
464 } catch (ParseException e
) {
470 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
476 * the row of the table to look up
478 * the column of the table to look up
479 * @return the bounds in display relative coordinates
481 public static Rectangle
getCellBounds(final Table table
, final int row
, final int col
) {
482 return UIThreadRunnable
.syncExec(new Result
<Rectangle
>() {
484 public Rectangle
run() {
485 TableItem item
= table
.getItem(row
);
486 Rectangle bounds
= item
.getBounds(col
);
487 Point p
= table
.toDisplay(bounds
.x
, bounds
.y
);
488 Rectangle rect
= new Rectangle(p
.x
, p
.y
, bounds
.width
, bounds
.height
);
495 * Get the tree item from a tree at the specified location
500 * the tree to find the tree item in
502 * the path to the tree item, in the form of node names (from
504 * @return the tree item
506 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTree tree
, String
... nodeNames
) {
507 if (nodeNames
.length
== 0) {
511 bot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(nodeNames
[0], tree
));
512 SWTBotTreeItem currentNode
= tree
.getTreeItem(nodeNames
[0]);
513 for (int i
= 1; i
< nodeNames
.length
; i
++) {
514 currentNode
.expand();
516 String nodeName
= nodeNames
[i
];
517 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
518 SWTBotTreeItem newNode
= currentNode
.getNode(nodeName
);
519 currentNode
= newNode
;