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
.Arrays
;
20 import java
.util
.List
;
21 import java
.util
.TimeZone
;
22 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
24 import org
.eclipse
.core
.resources
.IFolder
;
25 import org
.eclipse
.core
.resources
.IProject
;
26 import org
.eclipse
.core
.resources
.IResource
;
27 import org
.eclipse
.core
.resources
.IResourceVisitor
;
28 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
29 import org
.eclipse
.core
.runtime
.CoreException
;
30 import org
.eclipse
.core
.runtime
.IPath
;
31 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
32 import org
.eclipse
.core
.runtime
.jobs
.Job
;
33 import org
.eclipse
.jdt
.annotation
.NonNull
;
34 import org
.eclipse
.jface
.bindings
.keys
.IKeyLookup
;
35 import org
.eclipse
.jface
.bindings
.keys
.KeyStroke
;
36 import org
.eclipse
.jface
.bindings
.keys
.ParseException
;
37 import org
.eclipse
.swt
.events
.ControlAdapter
;
38 import org
.eclipse
.swt
.events
.ControlEvent
;
39 import org
.eclipse
.swt
.graphics
.Point
;
40 import org
.eclipse
.swt
.graphics
.Rectangle
;
41 import org
.eclipse
.swt
.widgets
.Display
;
42 import org
.eclipse
.swt
.widgets
.Shell
;
43 import org
.eclipse
.swt
.widgets
.Table
;
44 import org
.eclipse
.swt
.widgets
.TableItem
;
45 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
46 import org
.eclipse
.swtbot
.eclipse
.finder
.matchers
.WidgetMatcherFactory
;
47 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
48 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
49 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
50 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
51 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keyboard
;
52 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keystrokes
;
53 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
54 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
55 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTUtils
;
56 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
57 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
58 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
59 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
60 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
61 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
62 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
63 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
64 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
65 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.TimeoutException
;
66 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.operations
.NewExperimentOperation
;
67 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
68 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentElement
;
69 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectElement
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
.ProjectElementHasChild
;
77 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TracingPerspectiveFactory
;
78 import org
.eclipse
.ui
.IEditorPart
;
79 import org
.eclipse
.ui
.IEditorReference
;
80 import org
.eclipse
.ui
.IPageLayout
;
81 import org
.eclipse
.ui
.PartInitException
;
82 import org
.eclipse
.ui
.PlatformUI
;
83 import org
.eclipse
.ui
.WorkbenchException
;
84 import org
.hamcrest
.Matcher
;
87 * SWTBot Helper functions
89 * @author Matthew Khouzam
91 @SuppressWarnings("restriction")
92 public final class SWTBotUtils
{
94 private static final long MAX_JOBS_WAIT_TIME
= 300000;
95 private static final String WINDOW_MENU
= "Window";
96 private static final String PREFERENCES_MENU_ITEM
= "Preferences";
97 private static boolean fPrintedEnvironment
= false;
99 private SWTBotUtils() {
102 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
105 * Waits for all Eclipse jobs to finish. Times out after
106 * SWTBotUtils#MAX_JOBS_WAIT_TIME by default.
108 * @throws TimeoutException
109 * once the waiting time passes the default maximum value
111 public static void waitForJobs() {
112 waitForJobs(MAX_JOBS_WAIT_TIME
);
116 * Waits for all Eclipse jobs to finish
119 * the maximum time to wait, in milliseconds. Once the waiting
120 * time passes the maximum value, a TimeoutException is thrown
121 * @throws TimeoutException
122 * once the waiting time passes the maximum value
124 public static void waitForJobs(long maxWait
) {
125 long waitStart
= System
.currentTimeMillis();
126 while (!Job
.getJobManager().isIdle()) {
127 if (System
.currentTimeMillis() - waitStart
> maxWait
) {
129 throw new TimeoutException("Timed out waiting for jobs to finish.");
136 private static void printJobs() {
137 Job
[] jobs
= Job
.getJobManager().find(null);
138 for (Job job
: jobs
) {
139 System
.err
.println(job
.toString() + " state: " + jobStateToString(job
.getState()));
140 Thread thread
= job
.getThread();
141 if (thread
!= null) {
142 for (StackTraceElement stractTraceElement
: thread
.getStackTrace()) {
143 System
.err
.println(" " + stractTraceElement
);
146 System
.err
.println();
150 private static String
jobStateToString(int jobState
) {
166 * Sleeps current thread for a given time.
168 * @param waitTimeMillis
169 * time in milliseconds to wait
171 public static void delay(final long waitTimeMillis
) {
173 Thread
.sleep(waitTimeMillis
);
174 } catch (final InterruptedException e
) {
180 * Create a tracing project
183 * the name of the tracing project
185 public static void createProject(final String projectName
) {
189 UIThreadRunnable
.syncExec(new VoidResult() {
192 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
193 assertNotNull(project
);
197 SWTBotUtils
.waitForJobs();
204 * the name of the tracing project
205 * @param deleteResources
206 * whether or not to deleted resources under the project
210 public static void deleteProject(final String projectName
, boolean deleteResources
, SWTWorkbenchBot bot
) {
211 // Wait for any analysis to complete because it might create
212 // supplementary files
213 SWTBotUtils
.waitForJobs();
215 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
216 } catch (CoreException e
) {
219 SWTBotUtils
.waitForJobs();
221 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
222 projectViewBot
.setFocus();
224 SWTBotTree treeBot
= projectViewBot
.bot().tree();
225 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
226 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
229 if (deleteResources
) {
230 bot
.shell("Delete Resources").setFocus();
231 final SWTBotCheckBox checkBox
= bot
.checkBox();
232 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
236 final SWTBotButton okButton
= bot
.button("OK");
237 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
240 SWTBotUtils
.waitForJobs();
244 * Deletes a project and its resources
247 * the name of the tracing project
251 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
252 deleteProject(projectName
, true, bot
);
256 * Creates an experiment
259 * a given workbench bot
261 * the name of the project, creates the project if needed
263 * the experiment name
265 public static void createExperiment(SWTWorkbenchBot bot
, String projectName
, final @NonNull String expName
) {
266 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
267 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, true);
268 TmfExperimentFolder expFolder
= tmfProject
.getExperimentsFolder();
269 assertNotNull(expFolder
);
270 NewExperimentOperation operation
= new NewExperimentOperation(expFolder
, expName
);
271 operation
.run(new NullProgressMonitor());
273 bot
.waitUntil(new DefaultCondition() {
275 public boolean test() throws Exception
{
276 TmfExperimentElement experiment
= expFolder
.getExperiment(expName
);
277 return experiment
!= null;
281 public String
getFailureMessage() {
282 return "Experiment (" + expName
+ ") couldn't be created";
289 * Focus on the main window
292 * swtbotshells for all the shells
294 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
295 for (SWTBotShell shellBot
: shellBots
) {
296 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
303 * Close a view with a title
306 * the title, like "welcome"
310 public static void closeView(String title
, SWTWorkbenchBot bot
) {
311 final List
<SWTBotView
> openViews
= bot
.views();
312 for (SWTBotView view
: openViews
) {
313 if (view
.getTitle().equalsIgnoreCase(title
)) {
315 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
321 * Close a view with an id
324 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
328 public static void closeViewById(String viewId
, SWTWorkbenchBot bot
) {
329 final SWTBotView view
= bot
.viewById(viewId
);
331 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
335 * Switch to the tracing perspective
337 public static void switchToTracingPerspective() {
338 switchToPerspective(TRACING_PERSPECTIVE_ID
);
342 * Switch to a given perspective
345 * the perspective id (like
346 * "org.eclipse.linuxtools.tmf.ui.perspective"
348 public static void switchToPerspective(final String id
) {
349 UIThreadRunnable
.syncExec(new VoidResult() {
353 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
354 } catch (WorkbenchException e
) {
355 fail(e
.getMessage());
362 * Initialize the environment for SWTBot
364 public static void initialize() {
367 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
368 UIThreadRunnable
.syncExec(() -> {
371 // There seems to be problems on some system where the main shell is
372 // not in focus initially. This was seen using Xvfb and Xephyr on some occasions.
373 focusMainWindow(bot
.shells());
375 Shell shell
= bot
.activeShell().widget
;
377 // Only adjust shell if it appears to be the top-most
378 if (shell
.getParent() == null) {
379 makeShellFullyVisible(shell
);
384 private static void printEnvironment() {
385 if (fPrintedEnvironment
) {
389 // Print some information about the environment that could affect test outcome
390 Rectangle bounds
= Display
.getDefault().getBounds();
391 System
.out
.println("Display size: " + bounds
.width
+ "x" + bounds
.height
);
393 String osVersion
= System
.getProperty("os.version");
394 if (osVersion
!= null) {
395 System
.out
.println("OS version=" + osVersion
);
397 String gtkVersion
= System
.getProperty("org.eclipse.swt.internal.gtk.version");
398 if (gtkVersion
!= null) {
399 System
.out
.println("GTK version=" + gtkVersion
);
400 // Try to print the GTK theme information as behavior can change depending on the theme
401 String gtkTheme
= System
.getProperty("org.eclipse.swt.internal.gtk.theme");
402 System
.out
.println("GTK theme=" + (gtkTheme
== null ?
"unknown" : gtkTheme
));
404 String overlayScrollbar
= System
.getenv("LIBOVERLAY_SCROLLBAR");
405 if (overlayScrollbar
!= null) {
406 System
.out
.println("LIBOVERLAY_SCROLLBAR=" + overlayScrollbar
);
408 String ubuntuMenuProxy
= System
.getenv("UBUNTU_MENUPROXY");
409 if (ubuntuMenuProxy
!= null) {
410 System
.out
.println("UBUNTU_MENUPROXY=" + ubuntuMenuProxy
);
414 System
.out
.println("Time zone: " + TimeZone
.getDefault().getDisplayName());
416 fPrintedEnvironment
= true;
420 * If the test is running in the UI thread then fail
422 private static void failIfUIThread() {
423 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
424 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"
425 + " that useUIThread is set to false in the pom.xml");
430 * Try to make the shell fully visible in the display. If the shell cannot
431 * fit the display, it will be positioned so that top-left corner is at
432 * <code>(0, 0)</code> in display-relative coordinates.
435 * the shell to make fully visible
437 private static void makeShellFullyVisible(Shell shell
) {
438 Rectangle displayBounds
= shell
.getDisplay().getBounds();
439 Point absCoord
= shell
.toDisplay(0, 0);
440 Point shellSize
= shell
.getSize();
442 Point newLocation
= new Point(absCoord
.x
, absCoord
.y
);
443 newLocation
.x
= Math
.max(0, Math
.min(absCoord
.x
, displayBounds
.width
- shellSize
.x
));
444 newLocation
.y
= Math
.max(0, Math
.min(absCoord
.y
, displayBounds
.height
- shellSize
.y
));
445 if (!newLocation
.equals(absCoord
)) {
446 shell
.setLocation(newLocation
);
451 * Open a trace, this does not perform any validation though
456 * the path of the trace file (absolute or relative)
458 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
460 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
461 openTrace(projectName
, tracePath
, traceType
, true);
465 * Open a trace, this does not perform any validation though
470 * the path of the trace file (absolute or relative)
472 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
474 * delay and wait for jobs
476 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
477 final Exception exception
[] = new Exception
[1];
479 UIThreadRunnable
.syncExec(new VoidResult() {
483 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
484 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
485 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
486 } catch (CoreException e
) {
491 if (exception
[0] != null) {
492 fail(exception
[0].getMessage());
502 * Finds an editor and sets focus to the editor
508 * @return the corresponding SWTBotEditor
510 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
511 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
512 final SWTBotEditor editorBot
= bot
.editor(matcher
);
513 IEditorPart iep
= editorBot
.getReference().getEditor(true);
514 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
516 UIThreadRunnable
.syncExec(new VoidResult() {
523 SWTBotUtils
.waitForJobs();
524 SWTBotUtils
.delay(1000);
525 assertNotNull(tmfEd
);
530 * Opens a trace in an editor and get the TmfEventsEditor
535 * the name of the project that contains the trace
537 * the trace element path (relative to Traces folder)
538 * @return TmfEventsEditor the opened editor
540 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
541 final SWTBotView projectExplorerView
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
542 projectExplorerView
.setFocus();
543 SWTBot projectExplorerBot
= projectExplorerView
.bot();
545 final SWTBotTree tree
= projectExplorerBot
.tree();
546 projectExplorerBot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(projectName
, tree
));
547 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
550 SWTBotTreeItem tracesNode
= getTraceProjectItem(projectExplorerBot
, treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
553 SWTBotTreeItem currentItem
= tracesNode
;
554 for (String segment
: elementPath
.segments()) {
555 currentItem
= getTraceProjectItem(projectExplorerBot
, currentItem
, segment
);
556 currentItem
.doubleClick();
559 SWTBotEditor editor
= bot
.editorByTitle(elementPath
.toString());
560 IEditorPart editorPart
= editor
.getReference().getEditor(false);
561 assertTrue(editorPart
instanceof TmfEventsEditor
);
562 return (TmfEventsEditor
) editorPart
;
566 * Returns the child tree item of the specified item with the given name.
567 * The project element label may have a count suffix in the format ' [n]'.
570 * a given workbench bot
572 * the parent tree item
574 * the desired child element name (without suffix)
575 * @return the a {@link SWTBotTreeItem} with the specified name
577 public static SWTBotTreeItem
getTraceProjectItem(SWTBot bot
, final SWTBotTreeItem parentItem
, final String name
) {
578 ProjectElementHasChild condition
= new ProjectElementHasChild(parentItem
, name
);
579 bot
.waitUntil(condition
);
580 return condition
.getItem();
584 * Select the traces folder
587 * a given workbench bot
589 * the name of the project (it needs to exist or else it would
591 * @return a {@link SWTBotTreeItem} of the "Traces" folder
593 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
594 SWTBotTreeItem projectTreeItem
= selectProject(bot
, projectName
);
595 projectTreeItem
.select();
596 SWTBotTreeItem tracesFolderItem
= getTraceProjectItem(bot
, projectTreeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
597 tracesFolderItem
.select();
598 return tracesFolderItem
;
602 * Clear the traces folder
605 * a given workbench bot
607 * the name of the project (needs to exist)
609 public static void clearTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
610 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
611 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
612 TmfTraceFolder tracesFolder
= tmfProject
.getTracesFolder();
614 for (TmfTraceElement traceElement
: tracesFolder
.getTraces()) {
615 traceElement
.delete(null);
618 final IFolder resource
= tracesFolder
.getResource();
619 resource
.accept(new IResourceVisitor() {
621 public boolean visit(IResource visitedResource
) throws CoreException
{
622 if (visitedResource
!= resource
) {
623 visitedResource
.delete(true, null);
627 }, IResource
.DEPTH_ONE
, 0);
628 } catch (CoreException e
) {
629 fail(e
.getMessage());
632 bot
.waitUntil(new DefaultCondition() {
633 private int fTraceNb
= 0;
636 public boolean test() throws Exception
{
637 List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
638 fTraceNb
= traces
.size();
639 return fTraceNb
== 0;
643 public String
getFailureMessage() {
644 return "Traces Folder not empty (" + fTraceNb
+ ")";
650 * Clear the trace folder (using the UI)
653 * a given workbench bot
655 * the name of the project (needs to exist)
657 public static void clearTracesFolderUI(SWTWorkbenchBot bot
, String projectName
) {
658 SWTBotTreeItem tracesFolder
= selectTracesFolder(bot
, projectName
);
659 tracesFolder
.contextMenu().menu("Clear").click();
660 String CONFIRM_CLEAR_DIALOG_TITLE
= "Confirm Clear";
661 bot
.waitUntil(Conditions
.shellIsActive(CONFIRM_CLEAR_DIALOG_TITLE
));
663 SWTBotShell shell
= bot
.shell(CONFIRM_CLEAR_DIALOG_TITLE
);
664 shell
.bot().button("Yes").click();
665 bot
.waitUntil(Conditions
.shellCloses(shell
));
666 bot
.waitWhile(ConditionHelpers
.treeItemHasChildren(tracesFolder
));
670 * Clear the experiment folder
673 * a given workbench bot
675 * the name of the project (needs to exist)
677 public static void clearExperimentFolder(SWTWorkbenchBot bot
, String projectName
) {
678 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
679 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
680 TmfExperimentFolder expFolder
= tmfProject
.getExperimentsFolder();
681 expFolder
.getExperiments().forEach(experiment
-> {
682 IResource resource
= experiment
.getResource();
684 // Close the experiment if open
685 experiment
.closeEditors();
687 IPath path
= resource
.getLocation();
689 // Delete supplementary files
690 experiment
.deleteSupplementaryFolder();
692 // Finally, delete the experiment
693 resource
.delete(true, null);
694 } catch (CoreException e
) {
695 fail(e
.getMessage());
699 bot
.waitUntil(new DefaultCondition() {
700 private int fExperimentNb
= 0;
703 public boolean test() throws Exception
{
704 List
<TmfExperimentElement
> experiments
= expFolder
.getExperiments();
705 fExperimentNb
= experiments
.size();
706 return fExperimentNb
== 0;
710 public String
getFailureMessage() {
711 return "Experiment Folder not empty (" + fExperimentNb
+ ")";
717 * Select the project in Project Explorer
720 * a given workbench bot
722 * the name of the project (it needs to exist or else it would time out)
723 * @return a {@link SWTBotTreeItem} of the project
725 public static SWTBotTreeItem
selectProject(SWTWorkbenchBot bot
, String projectName
) {
726 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
727 projectExplorerBot
.show();
728 // FIXME: Bug 496519. Sometimes, the tree becomes disabled for a certain
729 // amount of time. This can happen during a long running operation
730 // (BusyIndicator.showWhile) which brings up the modal dialog "operation
731 // in progress" and this disables all shells
732 projectExplorerBot
.bot().waitUntil(Conditions
.widgetIsEnabled(projectExplorerBot
.bot().tree()));
733 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
744 public static void openView(final String id
) {
745 final PartInitException res
[] = new PartInitException
[1];
746 UIThreadRunnable
.syncExec(new VoidResult() {
750 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
751 } catch (PartInitException e
) {
756 if (res
[0] != null) {
757 fail(res
[0].getMessage());
766 * the {@link SWTBotTable} table
768 public static void maximizeTable(SWTBotTable tableBot
) {
769 final AtomicBoolean controlResized
= new AtomicBoolean();
770 UIThreadRunnable
.syncExec(new VoidResult() {
773 tableBot
.widget
.addControlListener(new ControlAdapter() {
775 public void controlResized(ControlEvent e
) {
776 tableBot
.widget
.removeControlListener(this);
777 controlResized
.set(true);
783 tableBot
.pressShortcut(KeyStroke
.getInstance(IKeyLookup
.CTRL_NAME
+ "+"), KeyStroke
.getInstance("M"));
784 } catch (ParseException e
) {
787 new SWTBot().waitUntil(new DefaultCondition() {
789 public boolean test() throws Exception
{
790 return controlResized
.get();
794 public String
getFailureMessage() {
795 return "Control was not resized";
801 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
807 * the row of the table to look up
809 * the column of the table to look up
810 * @return the bounds in display relative coordinates
812 public static Rectangle
getCellBounds(final Table table
, final int row
, final int col
) {
813 return UIThreadRunnable
.syncExec(new Result
<Rectangle
>() {
815 public Rectangle
run() {
816 TableItem item
= table
.getItem(row
);
817 Rectangle bounds
= item
.getBounds(col
);
818 Point p
= table
.toDisplay(bounds
.x
, bounds
.y
);
819 Rectangle rect
= new Rectangle(p
.x
, p
.y
, bounds
.width
, bounds
.height
);
826 * Get the tree item from a tree at the specified location
831 * the tree to find the tree item in
833 * the path to the tree item, in the form of node names (from
835 * @return the tree item
837 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTree tree
, String
... nodeNames
) {
838 if (nodeNames
.length
== 0) {
842 bot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(nodeNames
[0], tree
));
843 SWTBotTreeItem currentNode
= tree
.getTreeItem(nodeNames
[0]);
844 return getTreeItem(bot
, currentNode
, Arrays
.copyOfRange(nodeNames
, 1, nodeNames
.length
));
848 * Get the tree item from a parent tree item at the specified location
853 * the treeItem to find the tree item under
855 * the path to the tree item, in the form of node names (from
857 * @return the tree item
859 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTreeItem treeItem
, String
... nodeNames
) {
860 if (nodeNames
.length
== 0) {
864 SWTBotTreeItem currentNode
= treeItem
;
865 for (int i
= 0; i
< nodeNames
.length
; i
++) {
866 bot
.waitUntil(ConditionHelpers
.treeItemHasChildren(treeItem
));
867 currentNode
.expand();
869 String nodeName
= nodeNames
[i
];
871 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
872 } catch (TimeoutException e
) {
873 //FIXME: Sometimes in a JFace TreeViewer, it expands to nothing. Need to find out why.
874 currentNode
.collapse();
875 currentNode
.expand();
876 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
879 SWTBotTreeItem newNode
= currentNode
.getNode(nodeName
);
880 currentNode
= newNode
;
887 * Press the keyboard shortcut that goes to the top of a tree widget. The
888 * key combination can differ on different platforms.
891 * the keyboard to use
893 public static void pressShortcutGoToTreeTop(Keyboard keyboard
) {
894 if (SWTUtils
.isMac()) {
895 keyboard
.pressShortcut(Keystrokes
.ALT
, Keystrokes
.UP
);
897 keyboard
.pressShortcut(Keystrokes
.HOME
);
902 * Get the active events editor. Note that this will wait until such editor
905 * @param workbenchBot
906 * a given workbench bot
907 * @return the active events editor
909 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
) {
910 ConditionHelpers
.ActiveEventsEditor condition
= new ConditionHelpers
.ActiveEventsEditor(workbenchBot
, null);
911 workbenchBot
.waitUntil(condition
);
912 return condition
.getActiveEditor();
916 * Get the active events editor. Note that this will wait until such editor
919 * @param workbenchBot
920 * a given workbench bot
922 * the desired editor title. If null, any active events editor
923 * will be considered valid.
924 * @return the active events editor
926 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
, String editorTitle
) {
927 ConditionHelpers
.ActiveEventsEditor condition
= new ConditionHelpers
.ActiveEventsEditor(workbenchBot
, editorTitle
);
928 workbenchBot
.waitUntil(condition
);
929 return condition
.getActiveEditor();
933 * Open the preferences dialog and return the corresponding shell.
936 * a given workbench bot
937 * @return the preferences shell
939 public static SWTBotShell
openPreferences(SWTBot bot
) {
940 if (SWTUtils
.isMac()) {
941 // On Mac, the Preferences menu item is under the application name.
942 // For some reason, we can't access the application menu anymore so
943 // we use the keyboard shortcut.
945 bot
.activeShell().pressShortcut(KeyStroke
.getInstance(IKeyLookup
.COMMAND_NAME
+ "+"), KeyStroke
.getInstance(","));
946 } catch (ParseException e
) {
950 bot
.menu(WINDOW_MENU
).menu(PREFERENCES_MENU_ITEM
).click();
953 bot
.waitUntil(Conditions
.shellIsActive(PREFERENCES_MENU_ITEM
));
954 return bot
.activeShell();