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
.results
.Result
;
51 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
52 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTUtils
;
53 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
54 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
55 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
56 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
57 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
58 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
59 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
60 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
61 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
62 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.TimeoutException
;
63 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.operations
.NewExperimentOperation
;
64 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
65 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentElement
;
66 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
67 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
68 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectElement
;
69 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
.ProjectElementHasChild
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TracingPerspectiveFactory
;
75 import org
.eclipse
.ui
.IEditorPart
;
76 import org
.eclipse
.ui
.IEditorReference
;
77 import org
.eclipse
.ui
.IPageLayout
;
78 import org
.eclipse
.ui
.PartInitException
;
79 import org
.eclipse
.ui
.PlatformUI
;
80 import org
.eclipse
.ui
.WorkbenchException
;
81 import org
.hamcrest
.Matcher
;
84 * SWTBot Helper functions
86 * @author Matthew Khouzam
88 @SuppressWarnings("restriction")
89 public final class SWTBotUtils
{
91 private static final long MAX_JOBS_WAIT_TIME
= 300000;
92 private static final String WINDOW_MENU
= "Window";
93 private static final String PREFERENCES_MENU_ITEM
= "Preferences";
94 private static boolean fPrintedEnvironment
= false;
96 private SWTBotUtils() {
99 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
102 * Waits for all Eclipse jobs to finish. Times out after
103 * SWTBotUtils#MAX_JOBS_WAIT_TIME by default.
105 * @throws TimeoutException
106 * once the waiting time passes the default maximum value
108 public static void waitForJobs() {
109 waitForJobs(MAX_JOBS_WAIT_TIME
);
113 * Waits for all Eclipse jobs to finish
116 * the maximum time to wait, in milliseconds. Once the waiting
117 * time passes the maximum value, a TimeoutException is thrown
118 * @throws TimeoutException
119 * once the waiting time passes the maximum value
121 public static void waitForJobs(long maxWait
) {
122 long waitStart
= System
.currentTimeMillis();
123 while (!Job
.getJobManager().isIdle()) {
124 if (System
.currentTimeMillis() - waitStart
> maxWait
) {
126 throw new TimeoutException("Timed out waiting for jobs to finish.");
133 private static void printJobs() {
134 Job
[] jobs
= Job
.getJobManager().find(null);
135 for (Job job
: jobs
) {
136 System
.err
.println(job
.toString() + " state: " + jobStateToString(job
.getState()));
137 Thread thread
= job
.getThread();
138 if (thread
!= null) {
139 for (StackTraceElement stractTraceElement
: thread
.getStackTrace()) {
140 System
.err
.println(" " + stractTraceElement
);
143 System
.err
.println();
147 private static String
jobStateToString(int jobState
) {
163 * Sleeps current thread for a given time.
165 * @param waitTimeMillis
166 * time in milliseconds to wait
168 public static void delay(final long waitTimeMillis
) {
170 Thread
.sleep(waitTimeMillis
);
171 } catch (final InterruptedException e
) {
177 * Create a tracing project
180 * the name of the tracing project
182 public static void createProject(final String projectName
) {
186 UIThreadRunnable
.syncExec(new VoidResult() {
189 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
190 assertNotNull(project
);
194 SWTBotUtils
.waitForJobs();
201 * the name of the tracing project
202 * @param deleteResources
203 * whether or not to deleted resources under the project
207 public static void deleteProject(final String projectName
, boolean deleteResources
, SWTWorkbenchBot bot
) {
208 // Wait for any analysis to complete because it might create
209 // supplementary files
210 SWTBotUtils
.waitForJobs();
212 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
213 } catch (CoreException e
) {
216 SWTBotUtils
.waitForJobs();
218 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
219 projectViewBot
.setFocus();
221 SWTBotTree treeBot
= projectViewBot
.bot().tree();
222 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
223 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
226 if (deleteResources
) {
227 bot
.shell("Delete Resources").setFocus();
228 final SWTBotCheckBox checkBox
= bot
.checkBox();
229 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
233 final SWTBotButton okButton
= bot
.button("OK");
234 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
237 SWTBotUtils
.waitForJobs();
241 * Deletes a project and its resources
244 * the name of the tracing project
248 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
249 deleteProject(projectName
, true, bot
);
253 * Creates an experiment
256 * a given workbench bot
258 * the name of the project, creates the project if needed
260 * the experiment name
262 public static void createExperiment(SWTWorkbenchBot bot
, String projectName
, final @NonNull String expName
) {
263 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
264 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, true);
265 TmfExperimentFolder expFolder
= tmfProject
.getExperimentsFolder();
266 assertNotNull(expFolder
);
267 NewExperimentOperation operation
= new NewExperimentOperation(expFolder
, expName
);
268 operation
.run(new NullProgressMonitor());
270 bot
.waitUntil(new DefaultCondition() {
272 public boolean test() throws Exception
{
273 TmfExperimentElement experiment
= expFolder
.getExperiment(expName
);
274 return experiment
!= null;
278 public String
getFailureMessage() {
279 return "Experiment (" + expName
+ ") couldn't be created";
286 * Focus on the main window
289 * swtbotshells for all the shells
291 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
292 for (SWTBotShell shellBot
: shellBots
) {
293 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
300 * Close a view with a title
303 * the title, like "welcome"
307 public static void closeView(String title
, SWTWorkbenchBot bot
) {
308 final List
<SWTBotView
> openViews
= bot
.views();
309 for (SWTBotView view
: openViews
) {
310 if (view
.getTitle().equalsIgnoreCase(title
)) {
312 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
318 * Close a view with an id
321 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
325 public static void closeViewById(String viewId
, SWTWorkbenchBot bot
) {
326 final SWTBotView view
= bot
.viewById(viewId
);
328 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
332 * Switch to the tracing perspective
334 public static void switchToTracingPerspective() {
335 switchToPerspective(TRACING_PERSPECTIVE_ID
);
339 * Switch to a given perspective
342 * the perspective id (like
343 * "org.eclipse.linuxtools.tmf.ui.perspective"
345 public static void switchToPerspective(final String id
) {
346 UIThreadRunnable
.syncExec(new VoidResult() {
350 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
351 } catch (WorkbenchException e
) {
352 fail(e
.getMessage());
359 * Initialize the environment for SWTBot
361 public static void initialize() {
364 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
365 UIThreadRunnable
.syncExec(() -> {
368 // There seems to be problems on some system where the main shell is
369 // not in focus initially. This was seen using Xvfb and Xephyr on some occasions.
370 focusMainWindow(bot
.shells());
372 Shell shell
= bot
.activeShell().widget
;
374 // Only adjust shell if it appears to be the top-most
375 if (shell
.getParent() == null) {
376 makeShellFullyVisible(shell
);
381 private static void printEnvironment() {
382 if (fPrintedEnvironment
) {
386 // Print some information about the environment that could affect test outcome
387 Rectangle bounds
= Display
.getDefault().getBounds();
388 System
.out
.println("Display size: " + bounds
.width
+ "x" + bounds
.height
);
390 String osVersion
= System
.getProperty("os.version");
391 if (osVersion
!= null) {
392 System
.out
.println("OS version=" + osVersion
);
394 String gtkVersion
= System
.getProperty("org.eclipse.swt.internal.gtk.version");
395 if (gtkVersion
!= null) {
396 System
.out
.println("GTK version=" + gtkVersion
);
397 String overlayScrollbar
= System
.getenv("LIBOVERLAY_SCROLLBAR");
398 if (overlayScrollbar
!= null) {
399 System
.out
.println("LIBOVERLAY_SCROLLBAR=" + overlayScrollbar
);
401 String ubuntuMenuProxy
= System
.getenv("UBUNTU_MENUPROXY");
402 if (ubuntuMenuProxy
!= null) {
403 System
.out
.println("UBUNTU_MENUPROXY=" + ubuntuMenuProxy
);
407 System
.out
.println("Time zone: " + TimeZone
.getDefault().getDisplayName());
409 fPrintedEnvironment
= true;
413 * If the test is running in the UI thread then fail
415 private static void failIfUIThread() {
416 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
417 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"
418 + " that useUIThread is set to false in the pom.xml");
423 * Try to make the shell fully visible in the display. If the shell cannot
424 * fit the display, it will be positioned so that top-left corner is at
425 * <code>(0, 0)</code> in display-relative coordinates.
428 * the shell to make fully visible
430 private static void makeShellFullyVisible(Shell shell
) {
431 Rectangle displayBounds
= shell
.getDisplay().getBounds();
432 Point absCoord
= shell
.toDisplay(0, 0);
433 Point shellSize
= shell
.getSize();
435 Point newLocation
= new Point(absCoord
.x
, absCoord
.y
);
436 newLocation
.x
= Math
.max(0, Math
.min(absCoord
.x
, displayBounds
.width
- shellSize
.x
));
437 newLocation
.y
= Math
.max(0, Math
.min(absCoord
.y
, displayBounds
.height
- shellSize
.y
));
438 if (!newLocation
.equals(absCoord
)) {
439 shell
.setLocation(newLocation
);
444 * Open a trace, this does not perform any validation though
449 * the path of the trace file (absolute or relative)
451 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
453 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
454 openTrace(projectName
, tracePath
, traceType
, true);
458 * Open a trace, this does not perform any validation though
463 * the path of the trace file (absolute or relative)
465 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
467 * delay and wait for jobs
469 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
470 final Exception exception
[] = new Exception
[1];
472 UIThreadRunnable
.syncExec(new VoidResult() {
476 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
477 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
478 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
479 } catch (CoreException e
) {
484 if (exception
[0] != null) {
485 fail(exception
[0].getMessage());
495 * Finds an editor and sets focus to the editor
501 * @return the corresponding SWTBotEditor
503 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
504 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
505 final SWTBotEditor editorBot
= bot
.editor(matcher
);
506 IEditorPart iep
= editorBot
.getReference().getEditor(true);
507 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
509 UIThreadRunnable
.syncExec(new VoidResult() {
516 SWTBotUtils
.waitForJobs();
517 SWTBotUtils
.delay(1000);
518 assertNotNull(tmfEd
);
523 * Opens a trace in an editor and get the TmfEventsEditor
528 * the name of the project that contains the trace
530 * the trace element path (relative to Traces folder)
531 * @return TmfEventsEditor the opened editor
533 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
534 final SWTBotView projectExplorerView
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
535 projectExplorerView
.setFocus();
536 SWTBot projectExplorerBot
= projectExplorerView
.bot();
538 final SWTBotTree tree
= projectExplorerBot
.tree();
539 projectExplorerBot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(projectName
, tree
));
540 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
543 SWTBotTreeItem tracesNode
= getTraceProjectItem(projectExplorerBot
, treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
546 SWTBotTreeItem currentItem
= tracesNode
;
547 for (String segment
: elementPath
.segments()) {
548 currentItem
= getTraceProjectItem(projectExplorerBot
, currentItem
, segment
);
549 currentItem
.select();
550 currentItem
.doubleClick();
553 SWTBotEditor editor
= bot
.editorByTitle(elementPath
.toString());
554 IEditorPart editorPart
= editor
.getReference().getEditor(false);
555 assertTrue(editorPart
instanceof TmfEventsEditor
);
556 return (TmfEventsEditor
) editorPart
;
560 * Returns the child tree item of the specified item with the given name.
561 * The project element label may have a count suffix in the format ' [n]'.
564 * a given workbench bot
566 * the parent tree item
568 * the desired child element name (without suffix)
569 * @return the a {@link SWTBotTreeItem} with the specified name
571 public static SWTBotTreeItem
getTraceProjectItem(SWTBot bot
, final SWTBotTreeItem parentItem
, final String name
) {
572 ProjectElementHasChild condition
= new ProjectElementHasChild(parentItem
, name
);
573 bot
.waitUntil(condition
);
574 return condition
.getItem();
578 * Select the traces folder
581 * a given workbench bot
583 * the name of the project (it needs to exist or else it would
585 * @return a {@link SWTBotTreeItem} of the "Traces" folder
587 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
588 SWTBotTreeItem projectTreeItem
= selectProject(bot
, projectName
);
589 projectTreeItem
.select();
590 SWTBotTreeItem tracesFolderItem
= getTraceProjectItem(bot
, projectTreeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
591 tracesFolderItem
.select();
592 return tracesFolderItem
;
596 * Clear the traces folder
599 * a given workbench bot
601 * the name of the project (needs to exist)
603 public static void clearTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
604 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
605 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
606 TmfTraceFolder tracesFolder
= tmfProject
.getTracesFolder();
608 for (TmfTraceElement traceElement
: tracesFolder
.getTraces()) {
609 traceElement
.delete(null);
612 final IFolder resource
= tracesFolder
.getResource();
613 resource
.accept(new IResourceVisitor() {
615 public boolean visit(IResource visitedResource
) throws CoreException
{
616 if (visitedResource
!= resource
) {
617 visitedResource
.delete(true, null);
621 }, IResource
.DEPTH_ONE
, 0);
622 } catch (CoreException e
) {
623 fail(e
.getMessage());
626 bot
.waitUntil(new DefaultCondition() {
627 private int fTraceNb
= 0;
630 public boolean test() throws Exception
{
631 List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
632 fTraceNb
= traces
.size();
633 return fTraceNb
== 0;
637 public String
getFailureMessage() {
638 return "Traces Folder not empty (" + fTraceNb
+ ")";
644 * Clear the experiment folder
647 * a given workbench bot
649 * the name of the project (needs to exist)
651 public static void clearExperimentFolder(SWTWorkbenchBot bot
, String projectName
) {
652 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
653 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
654 TmfExperimentFolder expFolder
= tmfProject
.getExperimentsFolder();
655 expFolder
.getExperiments().forEach(experiment
-> {
656 IResource resource
= experiment
.getResource();
658 // Close the experiment if open
659 experiment
.closeEditors();
661 IPath path
= resource
.getLocation();
663 // Delete supplementary files
664 experiment
.deleteSupplementaryFolder();
666 // Finally, delete the experiment
667 resource
.delete(true, null);
668 } catch (CoreException e
) {
669 fail(e
.getMessage());
673 bot
.waitUntil(new DefaultCondition() {
674 private int fExperimentNb
= 0;
677 public boolean test() throws Exception
{
678 List
<TmfExperimentElement
> experiments
= expFolder
.getExperiments();
679 fExperimentNb
= experiments
.size();
680 return fExperimentNb
== 0;
684 public String
getFailureMessage() {
685 return "Experiment Folder not empty (" + fExperimentNb
+ ")";
691 * Select the project in Project Explorer
694 * a given workbench bot
696 * the name of the project (it needs to exist or else it would time out)
697 * @return a {@link SWTBotTreeItem} of the project
699 public static SWTBotTreeItem
selectProject(SWTWorkbenchBot bot
, String projectName
) {
700 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
701 projectExplorerBot
.show();
702 // FIXME: Bug 496519. Sometimes, the tree becomes disabled for a certain
703 // amount of time. This can happen during a long running operation
704 // (BusyIndicator.showWhile) which brings up the modal dialog "operation
705 // in progress" and this disables all shells
706 projectExplorerBot
.bot().waitUntil(Conditions
.widgetIsEnabled(projectExplorerBot
.bot().tree()));
707 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
718 public static void openView(final String id
) {
719 final PartInitException res
[] = new PartInitException
[1];
720 UIThreadRunnable
.syncExec(new VoidResult() {
724 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
725 } catch (PartInitException e
) {
730 if (res
[0] != null) {
731 fail(res
[0].getMessage());
740 * the {@link SWTBotTable} table
742 public static void maximizeTable(SWTBotTable tableBot
) {
743 final AtomicBoolean controlResized
= new AtomicBoolean();
744 UIThreadRunnable
.syncExec(new VoidResult() {
747 tableBot
.widget
.addControlListener(new ControlAdapter() {
749 public void controlResized(ControlEvent e
) {
750 tableBot
.widget
.removeControlListener(this);
751 controlResized
.set(true);
757 tableBot
.pressShortcut(KeyStroke
.getInstance(IKeyLookup
.CTRL_NAME
+ "+"), KeyStroke
.getInstance("M"));
758 } catch (ParseException e
) {
761 new SWTBot().waitUntil(new DefaultCondition() {
763 public boolean test() throws Exception
{
764 return controlResized
.get();
768 public String
getFailureMessage() {
769 return "Control was not resized";
775 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
781 * the row of the table to look up
783 * the column of the table to look up
784 * @return the bounds in display relative coordinates
786 public static Rectangle
getCellBounds(final Table table
, final int row
, final int col
) {
787 return UIThreadRunnable
.syncExec(new Result
<Rectangle
>() {
789 public Rectangle
run() {
790 TableItem item
= table
.getItem(row
);
791 Rectangle bounds
= item
.getBounds(col
);
792 Point p
= table
.toDisplay(bounds
.x
, bounds
.y
);
793 Rectangle rect
= new Rectangle(p
.x
, p
.y
, bounds
.width
, bounds
.height
);
800 * Get the tree item from a tree at the specified location
805 * the tree to find the tree item in
807 * the path to the tree item, in the form of node names (from
809 * @return the tree item
811 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTree tree
, String
... nodeNames
) {
812 if (nodeNames
.length
== 0) {
816 bot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(nodeNames
[0], tree
));
817 SWTBotTreeItem currentNode
= tree
.getTreeItem(nodeNames
[0]);
818 for (int i
= 1; i
< nodeNames
.length
; i
++) {
819 currentNode
.expand();
821 String nodeName
= nodeNames
[i
];
823 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
824 } catch (TimeoutException e
) {
825 //FIXME: Sometimes in a JFace TreeViewer, it expands to nothing. Need to find out why.
826 currentNode
.collapse();
827 currentNode
.expand();
828 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
831 SWTBotTreeItem newNode
= currentNode
.getNode(nodeName
);
832 currentNode
= newNode
;
839 * Get the active events editor. Note that this will wait until such editor
842 * @param workbenchBot
843 * a given workbench bot
844 * @return the active events editor
846 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
) {
847 ConditionHelpers
.ActiveEventsEditor condition
= new ConditionHelpers
.ActiveEventsEditor(workbenchBot
, null);
848 workbenchBot
.waitUntil(condition
);
849 return condition
.getActiveEditor();
853 * Get the active events editor. Note that this will wait until such editor
856 * @param workbenchBot
857 * a given workbench bot
859 * the desired editor title. If null, any active events editor
860 * will be considered valid.
861 * @return the active events editor
863 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
, String editorTitle
) {
864 ConditionHelpers
.ActiveEventsEditor condition
= new ConditionHelpers
.ActiveEventsEditor(workbenchBot
, editorTitle
);
865 workbenchBot
.waitUntil(condition
);
866 return condition
.getActiveEditor();
870 * Open the preferences dialog and return the corresponding shell.
873 * a given workbench bot
874 * @return the preferences shell
876 public static SWTBotShell
openPreferences(SWTBot bot
) {
877 if (SWTUtils
.isMac()) {
878 // On Mac, the Preferences menu item is under the application name.
879 // For some reason, we can't access the application menu anymore so
880 // we use the keyboard shortcut.
882 bot
.activeShell().pressShortcut(KeyStroke
.getInstance(IKeyLookup
.COMMAND_NAME
+ "+"), KeyStroke
.getInstance(","));
883 } catch (ParseException e
) {
887 bot
.menu(WINDOW_MENU
).menu(PREFERENCES_MENU_ITEM
).click();
890 bot
.waitUntil(Conditions
.shellIsActive(PREFERENCES_MENU_ITEM
));
891 return bot
.activeShell();