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
;
20 import java
.util
.TimeZone
;
21 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
23 import org
.eclipse
.core
.resources
.IFolder
;
24 import org
.eclipse
.core
.resources
.IProject
;
25 import org
.eclipse
.core
.resources
.IResource
;
26 import org
.eclipse
.core
.resources
.IResourceVisitor
;
27 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
28 import org
.eclipse
.core
.runtime
.CoreException
;
29 import org
.eclipse
.core
.runtime
.IPath
;
30 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
31 import org
.eclipse
.core
.runtime
.jobs
.Job
;
32 import org
.eclipse
.jdt
.annotation
.NonNull
;
33 import org
.eclipse
.jface
.bindings
.keys
.IKeyLookup
;
34 import org
.eclipse
.jface
.bindings
.keys
.KeyStroke
;
35 import org
.eclipse
.jface
.bindings
.keys
.ParseException
;
36 import org
.eclipse
.swt
.events
.ControlAdapter
;
37 import org
.eclipse
.swt
.events
.ControlEvent
;
38 import org
.eclipse
.swt
.graphics
.Point
;
39 import org
.eclipse
.swt
.graphics
.Rectangle
;
40 import org
.eclipse
.swt
.widgets
.Display
;
41 import org
.eclipse
.swt
.widgets
.Shell
;
42 import org
.eclipse
.swt
.widgets
.Table
;
43 import org
.eclipse
.swt
.widgets
.TableItem
;
44 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
45 import org
.eclipse
.swtbot
.eclipse
.finder
.matchers
.WidgetMatcherFactory
;
46 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
47 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
48 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
49 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
50 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keyboard
;
51 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keystrokes
;
52 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
53 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
54 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTUtils
;
55 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
56 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
57 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
58 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
59 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
60 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
61 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
62 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
63 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
64 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.TimeoutException
;
65 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.operations
.NewExperimentOperation
;
66 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
67 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentElement
;
68 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
69 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectElement
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
.ProjectElementHasChild
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TracingPerspectiveFactory
;
77 import org
.eclipse
.ui
.IEditorPart
;
78 import org
.eclipse
.ui
.IEditorReference
;
79 import org
.eclipse
.ui
.IPageLayout
;
80 import org
.eclipse
.ui
.PartInitException
;
81 import org
.eclipse
.ui
.PlatformUI
;
82 import org
.eclipse
.ui
.WorkbenchException
;
83 import org
.hamcrest
.Matcher
;
86 * SWTBot Helper functions
88 * @author Matthew Khouzam
90 @SuppressWarnings("restriction")
91 public final class SWTBotUtils
{
93 private static final long MAX_JOBS_WAIT_TIME
= 300000;
94 private static final String WINDOW_MENU
= "Window";
95 private static final String PREFERENCES_MENU_ITEM
= "Preferences";
96 private static boolean fPrintedEnvironment
= false;
98 private SWTBotUtils() {
101 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
104 * Waits for all Eclipse jobs to finish. Times out after
105 * SWTBotUtils#MAX_JOBS_WAIT_TIME by default.
107 * @throws TimeoutException
108 * once the waiting time passes the default maximum value
110 public static void waitForJobs() {
111 waitForJobs(MAX_JOBS_WAIT_TIME
);
115 * Waits for all Eclipse jobs to finish
118 * the maximum time to wait, in milliseconds. Once the waiting
119 * time passes the maximum value, a TimeoutException is thrown
120 * @throws TimeoutException
121 * once the waiting time passes the maximum value
123 public static void waitForJobs(long maxWait
) {
124 long waitStart
= System
.currentTimeMillis();
125 while (!Job
.getJobManager().isIdle()) {
126 if (System
.currentTimeMillis() - waitStart
> maxWait
) {
128 throw new TimeoutException("Timed out waiting for jobs to finish.");
135 private static void printJobs() {
136 Job
[] jobs
= Job
.getJobManager().find(null);
137 for (Job job
: jobs
) {
138 System
.err
.println(job
.toString() + " state: " + jobStateToString(job
.getState()));
139 Thread thread
= job
.getThread();
140 if (thread
!= null) {
141 for (StackTraceElement stractTraceElement
: thread
.getStackTrace()) {
142 System
.err
.println(" " + stractTraceElement
);
145 System
.err
.println();
149 private static String
jobStateToString(int jobState
) {
165 * Sleeps current thread for a given time.
167 * @param waitTimeMillis
168 * time in milliseconds to wait
170 public static void delay(final long waitTimeMillis
) {
172 Thread
.sleep(waitTimeMillis
);
173 } catch (final InterruptedException e
) {
179 * Create a tracing project
182 * the name of the tracing project
184 public static void createProject(final String projectName
) {
188 UIThreadRunnable
.syncExec(new VoidResult() {
191 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
192 assertNotNull(project
);
196 SWTBotUtils
.waitForJobs();
203 * the name of the tracing project
204 * @param deleteResources
205 * whether or not to deleted resources under the project
209 public static void deleteProject(final String projectName
, boolean deleteResources
, SWTWorkbenchBot bot
) {
210 // Wait for any analysis to complete because it might create
211 // supplementary files
212 SWTBotUtils
.waitForJobs();
214 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
215 } catch (CoreException e
) {
218 SWTBotUtils
.waitForJobs();
220 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
221 projectViewBot
.setFocus();
223 SWTBotTree treeBot
= projectViewBot
.bot().tree();
224 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
225 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
228 if (deleteResources
) {
229 bot
.shell("Delete Resources").setFocus();
230 final SWTBotCheckBox checkBox
= bot
.checkBox();
231 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
235 final SWTBotButton okButton
= bot
.button("OK");
236 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
239 SWTBotUtils
.waitForJobs();
243 * Deletes a project and its resources
246 * the name of the tracing project
250 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
251 deleteProject(projectName
, true, bot
);
255 * Creates an experiment
258 * a given workbench bot
260 * the name of the project, creates the project if needed
262 * the experiment name
264 public static void createExperiment(SWTWorkbenchBot bot
, String projectName
, final @NonNull String expName
) {
265 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
266 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, true);
267 TmfExperimentFolder expFolder
= tmfProject
.getExperimentsFolder();
268 assertNotNull(expFolder
);
269 NewExperimentOperation operation
= new NewExperimentOperation(expFolder
, expName
);
270 operation
.run(new NullProgressMonitor());
272 bot
.waitUntil(new DefaultCondition() {
274 public boolean test() throws Exception
{
275 TmfExperimentElement experiment
= expFolder
.getExperiment(expName
);
276 return experiment
!= null;
280 public String
getFailureMessage() {
281 return "Experiment (" + expName
+ ") couldn't be created";
288 * Focus on the main window
291 * swtbotshells for all the shells
293 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
294 for (SWTBotShell shellBot
: shellBots
) {
295 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
302 * Close a view with a title
305 * the title, like "welcome"
309 public static void closeView(String title
, SWTWorkbenchBot bot
) {
310 final List
<SWTBotView
> openViews
= bot
.views();
311 for (SWTBotView view
: openViews
) {
312 if (view
.getTitle().equalsIgnoreCase(title
)) {
314 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
320 * Close a view with an id
323 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
327 public static void closeViewById(String viewId
, SWTWorkbenchBot bot
) {
328 final SWTBotView view
= bot
.viewById(viewId
);
330 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
334 * Switch to the tracing perspective
336 public static void switchToTracingPerspective() {
337 switchToPerspective(TRACING_PERSPECTIVE_ID
);
341 * Switch to a given perspective
344 * the perspective id (like
345 * "org.eclipse.linuxtools.tmf.ui.perspective"
347 public static void switchToPerspective(final String id
) {
348 UIThreadRunnable
.syncExec(new VoidResult() {
352 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
353 } catch (WorkbenchException e
) {
354 fail(e
.getMessage());
361 * Initialize the environment for SWTBot
363 public static void initialize() {
366 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
367 UIThreadRunnable
.syncExec(() -> {
370 // There seems to be problems on some system where the main shell is
371 // not in focus initially. This was seen using Xvfb and Xephyr on some occasions.
372 focusMainWindow(bot
.shells());
374 Shell shell
= bot
.activeShell().widget
;
376 // Only adjust shell if it appears to be the top-most
377 if (shell
.getParent() == null) {
378 makeShellFullyVisible(shell
);
383 private static void printEnvironment() {
384 if (fPrintedEnvironment
) {
388 // Print some information about the environment that could affect test outcome
389 Rectangle bounds
= Display
.getDefault().getBounds();
390 System
.out
.println("Display size: " + bounds
.width
+ "x" + bounds
.height
);
392 String osVersion
= System
.getProperty("os.version");
393 if (osVersion
!= null) {
394 System
.out
.println("OS version=" + osVersion
);
396 String gtkVersion
= System
.getProperty("org.eclipse.swt.internal.gtk.version");
397 if (gtkVersion
!= null) {
398 System
.out
.println("GTK version=" + gtkVersion
);
399 String overlayScrollbar
= System
.getenv("LIBOVERLAY_SCROLLBAR");
400 if (overlayScrollbar
!= null) {
401 System
.out
.println("LIBOVERLAY_SCROLLBAR=" + overlayScrollbar
);
403 String ubuntuMenuProxy
= System
.getenv("UBUNTU_MENUPROXY");
404 if (ubuntuMenuProxy
!= null) {
405 System
.out
.println("UBUNTU_MENUPROXY=" + ubuntuMenuProxy
);
409 System
.out
.println("Time zone: " + TimeZone
.getDefault().getDisplayName());
411 fPrintedEnvironment
= true;
415 * If the test is running in the UI thread then fail
417 private static void failIfUIThread() {
418 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
419 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"
420 + " that useUIThread is set to false in the pom.xml");
425 * Try to make the shell fully visible in the display. If the shell cannot
426 * fit the display, it will be positioned so that top-left corner is at
427 * <code>(0, 0)</code> in display-relative coordinates.
430 * the shell to make fully visible
432 private static void makeShellFullyVisible(Shell shell
) {
433 Rectangle displayBounds
= shell
.getDisplay().getBounds();
434 Point absCoord
= shell
.toDisplay(0, 0);
435 Point shellSize
= shell
.getSize();
437 Point newLocation
= new Point(absCoord
.x
, absCoord
.y
);
438 newLocation
.x
= Math
.max(0, Math
.min(absCoord
.x
, displayBounds
.width
- shellSize
.x
));
439 newLocation
.y
= Math
.max(0, Math
.min(absCoord
.y
, displayBounds
.height
- shellSize
.y
));
440 if (!newLocation
.equals(absCoord
)) {
441 shell
.setLocation(newLocation
);
446 * Open a trace, this does not perform any validation though
451 * the path of the trace file (absolute or relative)
453 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
455 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
456 openTrace(projectName
, tracePath
, traceType
, true);
460 * Open a trace, this does not perform any validation though
465 * the path of the trace file (absolute or relative)
467 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
469 * delay and wait for jobs
471 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
472 final Exception exception
[] = new Exception
[1];
474 UIThreadRunnable
.syncExec(new VoidResult() {
478 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
479 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
480 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
481 } catch (CoreException e
) {
486 if (exception
[0] != null) {
487 fail(exception
[0].getMessage());
497 * Finds an editor and sets focus to the editor
503 * @return the corresponding SWTBotEditor
505 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
506 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
507 final SWTBotEditor editorBot
= bot
.editor(matcher
);
508 IEditorPart iep
= editorBot
.getReference().getEditor(true);
509 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
511 UIThreadRunnable
.syncExec(new VoidResult() {
518 SWTBotUtils
.waitForJobs();
519 SWTBotUtils
.delay(1000);
520 assertNotNull(tmfEd
);
525 * Opens a trace in an editor and get the TmfEventsEditor
530 * the name of the project that contains the trace
532 * the trace element path (relative to Traces folder)
533 * @return TmfEventsEditor the opened editor
535 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
536 final SWTBotView projectExplorerView
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
537 projectExplorerView
.setFocus();
538 SWTBot projectExplorerBot
= projectExplorerView
.bot();
540 final SWTBotTree tree
= projectExplorerBot
.tree();
541 projectExplorerBot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(projectName
, tree
));
542 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
545 SWTBotTreeItem tracesNode
= getTraceProjectItem(projectExplorerBot
, treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
548 SWTBotTreeItem currentItem
= tracesNode
;
549 for (String segment
: elementPath
.segments()) {
550 currentItem
= getTraceProjectItem(projectExplorerBot
, currentItem
, segment
);
551 currentItem
.select();
552 currentItem
.doubleClick();
555 SWTBotEditor editor
= bot
.editorByTitle(elementPath
.toString());
556 IEditorPart editorPart
= editor
.getReference().getEditor(false);
557 assertTrue(editorPart
instanceof TmfEventsEditor
);
558 return (TmfEventsEditor
) editorPart
;
562 * Returns the child tree item of the specified item with the given name.
563 * The project element label may have a count suffix in the format ' [n]'.
566 * a given workbench bot
568 * the parent tree item
570 * the desired child element name (without suffix)
571 * @return the a {@link SWTBotTreeItem} with the specified name
573 public static SWTBotTreeItem
getTraceProjectItem(SWTBot bot
, final SWTBotTreeItem parentItem
, final String name
) {
574 ProjectElementHasChild condition
= new ProjectElementHasChild(parentItem
, name
);
575 bot
.waitUntil(condition
);
576 return condition
.getItem();
580 * Select the traces folder
583 * a given workbench bot
585 * the name of the project (it needs to exist or else it would
587 * @return a {@link SWTBotTreeItem} of the "Traces" folder
589 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
590 SWTBotTreeItem projectTreeItem
= selectProject(bot
, projectName
);
591 projectTreeItem
.select();
592 SWTBotTreeItem tracesFolderItem
= getTraceProjectItem(bot
, projectTreeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
593 tracesFolderItem
.select();
594 return tracesFolderItem
;
598 * Clear the traces folder
601 * a given workbench bot
603 * the name of the project (needs to exist)
605 public static void clearTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
606 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
607 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
608 TmfTraceFolder tracesFolder
= tmfProject
.getTracesFolder();
610 for (TmfTraceElement traceElement
: tracesFolder
.getTraces()) {
611 traceElement
.delete(null);
614 final IFolder resource
= tracesFolder
.getResource();
615 resource
.accept(new IResourceVisitor() {
617 public boolean visit(IResource visitedResource
) throws CoreException
{
618 if (visitedResource
!= resource
) {
619 visitedResource
.delete(true, null);
623 }, IResource
.DEPTH_ONE
, 0);
624 } catch (CoreException e
) {
625 fail(e
.getMessage());
628 bot
.waitUntil(new DefaultCondition() {
629 private int fTraceNb
= 0;
632 public boolean test() throws Exception
{
633 List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
634 fTraceNb
= traces
.size();
635 return fTraceNb
== 0;
639 public String
getFailureMessage() {
640 return "Traces Folder not empty (" + fTraceNb
+ ")";
646 * Clear the experiment folder
649 * a given workbench bot
651 * the name of the project (needs to exist)
653 public static void clearExperimentFolder(SWTWorkbenchBot bot
, String projectName
) {
654 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
655 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
656 TmfExperimentFolder expFolder
= tmfProject
.getExperimentsFolder();
657 expFolder
.getExperiments().forEach(experiment
-> {
658 IResource resource
= experiment
.getResource();
660 // Close the experiment if open
661 experiment
.closeEditors();
663 IPath path
= resource
.getLocation();
665 // Delete supplementary files
666 experiment
.deleteSupplementaryFolder();
668 // Finally, delete the experiment
669 resource
.delete(true, null);
670 } catch (CoreException e
) {
671 fail(e
.getMessage());
675 bot
.waitUntil(new DefaultCondition() {
676 private int fExperimentNb
= 0;
679 public boolean test() throws Exception
{
680 List
<TmfExperimentElement
> experiments
= expFolder
.getExperiments();
681 fExperimentNb
= experiments
.size();
682 return fExperimentNb
== 0;
686 public String
getFailureMessage() {
687 return "Experiment Folder not empty (" + fExperimentNb
+ ")";
693 * Select the project in Project Explorer
696 * a given workbench bot
698 * the name of the project (it needs to exist or else it would time out)
699 * @return a {@link SWTBotTreeItem} of the project
701 public static SWTBotTreeItem
selectProject(SWTWorkbenchBot bot
, String projectName
) {
702 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
703 projectExplorerBot
.show();
704 // FIXME: Bug 496519. Sometimes, the tree becomes disabled for a certain
705 // amount of time. This can happen during a long running operation
706 // (BusyIndicator.showWhile) which brings up the modal dialog "operation
707 // in progress" and this disables all shells
708 projectExplorerBot
.bot().waitUntil(Conditions
.widgetIsEnabled(projectExplorerBot
.bot().tree()));
709 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
720 public static void openView(final String id
) {
721 final PartInitException res
[] = new PartInitException
[1];
722 UIThreadRunnable
.syncExec(new VoidResult() {
726 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
727 } catch (PartInitException e
) {
732 if (res
[0] != null) {
733 fail(res
[0].getMessage());
742 * the {@link SWTBotTable} table
744 public static void maximizeTable(SWTBotTable tableBot
) {
745 final AtomicBoolean controlResized
= new AtomicBoolean();
746 UIThreadRunnable
.syncExec(new VoidResult() {
749 tableBot
.widget
.addControlListener(new ControlAdapter() {
751 public void controlResized(ControlEvent e
) {
752 tableBot
.widget
.removeControlListener(this);
753 controlResized
.set(true);
759 tableBot
.pressShortcut(KeyStroke
.getInstance(IKeyLookup
.CTRL_NAME
+ "+"), KeyStroke
.getInstance("M"));
760 } catch (ParseException e
) {
763 new SWTBot().waitUntil(new DefaultCondition() {
765 public boolean test() throws Exception
{
766 return controlResized
.get();
770 public String
getFailureMessage() {
771 return "Control was not resized";
777 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
783 * the row of the table to look up
785 * the column of the table to look up
786 * @return the bounds in display relative coordinates
788 public static Rectangle
getCellBounds(final Table table
, final int row
, final int col
) {
789 return UIThreadRunnable
.syncExec(new Result
<Rectangle
>() {
791 public Rectangle
run() {
792 TableItem item
= table
.getItem(row
);
793 Rectangle bounds
= item
.getBounds(col
);
794 Point p
= table
.toDisplay(bounds
.x
, bounds
.y
);
795 Rectangle rect
= new Rectangle(p
.x
, p
.y
, bounds
.width
, bounds
.height
);
802 * Get the tree item from a tree at the specified location
807 * the tree to find the tree item in
809 * the path to the tree item, in the form of node names (from
811 * @return the tree item
813 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTree tree
, String
... nodeNames
) {
814 if (nodeNames
.length
== 0) {
818 bot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(nodeNames
[0], tree
));
819 SWTBotTreeItem currentNode
= tree
.getTreeItem(nodeNames
[0]);
820 for (int i
= 1; i
< nodeNames
.length
; i
++) {
821 currentNode
.expand();
823 String nodeName
= nodeNames
[i
];
825 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
826 } catch (TimeoutException e
) {
827 //FIXME: Sometimes in a JFace TreeViewer, it expands to nothing. Need to find out why.
828 currentNode
.collapse();
829 currentNode
.expand();
830 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
833 SWTBotTreeItem newNode
= currentNode
.getNode(nodeName
);
834 currentNode
= newNode
;
841 * Press the keyboard shortcut that goes to the top of a tree widget. The
842 * key combination can differ on different platforms.
845 * the keyboard to use
847 public static void pressShortcutGoToTreeTop(Keyboard keyboard
) {
848 if (SWTUtils
.isMac()) {
849 keyboard
.pressShortcut(Keystrokes
.ALT
, Keystrokes
.UP
);
851 keyboard
.pressShortcut(Keystrokes
.HOME
);
856 * Get the active events editor. Note that this will wait until such editor
859 * @param workbenchBot
860 * a given workbench bot
861 * @return the active events editor
863 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
) {
864 ConditionHelpers
.ActiveEventsEditor condition
= new ConditionHelpers
.ActiveEventsEditor(workbenchBot
, null);
865 workbenchBot
.waitUntil(condition
);
866 return condition
.getActiveEditor();
870 * Get the active events editor. Note that this will wait until such editor
873 * @param workbenchBot
874 * a given workbench bot
876 * the desired editor title. If null, any active events editor
877 * will be considered valid.
878 * @return the active events editor
880 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
, String editorTitle
) {
881 ConditionHelpers
.ActiveEventsEditor condition
= new ConditionHelpers
.ActiveEventsEditor(workbenchBot
, editorTitle
);
882 workbenchBot
.waitUntil(condition
);
883 return condition
.getActiveEditor();
887 * Open the preferences dialog and return the corresponding shell.
890 * a given workbench bot
891 * @return the preferences shell
893 public static SWTBotShell
openPreferences(SWTBot bot
) {
894 if (SWTUtils
.isMac()) {
895 // On Mac, the Preferences menu item is under the application name.
896 // For some reason, we can't access the application menu anymore so
897 // we use the keyboard shortcut.
899 bot
.activeShell().pressShortcut(KeyStroke
.getInstance(IKeyLookup
.COMMAND_NAME
+ "+"), KeyStroke
.getInstance(","));
900 } catch (ParseException e
) {
904 bot
.menu(WINDOW_MENU
).menu(PREFERENCES_MENU_ITEM
).click();
907 bot
.waitUntil(Conditions
.shellIsActive(PREFERENCES_MENU_ITEM
));
908 return bot
.activeShell();