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
.apache
.log4j
.Logger
;
25 import org
.eclipse
.core
.resources
.IFolder
;
26 import org
.eclipse
.core
.resources
.IProject
;
27 import org
.eclipse
.core
.resources
.IResource
;
28 import org
.eclipse
.core
.resources
.IResourceVisitor
;
29 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
30 import org
.eclipse
.core
.runtime
.CoreException
;
31 import org
.eclipse
.core
.runtime
.IPath
;
32 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
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
.MessageFormat
;
56 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTUtils
;
57 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
58 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
59 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
60 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
61 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
62 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
63 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
64 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
65 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
66 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.TimeoutException
;
67 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.operations
.NewExperimentOperation
;
68 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
69 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentElement
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectElement
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
77 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
.ProjectElementHasChild
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
.JobUtils
;
79 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TracingPerspectiveFactory
;
80 import org
.eclipse
.ui
.IEditorPart
;
81 import org
.eclipse
.ui
.IEditorReference
;
82 import org
.eclipse
.ui
.IPageLayout
;
83 import org
.eclipse
.ui
.PartInitException
;
84 import org
.eclipse
.ui
.PlatformUI
;
85 import org
.eclipse
.ui
.WorkbenchException
;
86 import org
.hamcrest
.Matcher
;
89 * SWTBot Helper functions
91 * @author Matthew Khouzam
93 @SuppressWarnings("restriction")
94 public final class SWTBotUtils
{
96 private static final long MAX_JOBS_WAIT_TIME
= 300000;
97 private static final String WINDOW_MENU
= "Window";
98 private static final String PREFERENCES_MENU_ITEM
= "Preferences";
99 private static boolean fPrintedEnvironment
= false;
100 private static Logger log
= Logger
.getLogger(SWTBotUtils
.class);
102 private SWTBotUtils() {
106 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
109 * Waits for all Eclipse jobs to finish. Times out after
110 * SWTBotUtils#MAX_JOBS_WAIT_TIME by default.
112 * @throws RuntimeException
113 * once the waiting time passes the default maximum value
115 public static void waitForJobs() {
116 JobUtils
.waitForJobs(MAX_JOBS_WAIT_TIME
);
120 * Sleeps current thread for a given time.
122 * @param waitTimeMillis
123 * time in milliseconds to wait
125 public static void delay(final long waitTimeMillis
) {
127 Thread
.sleep(waitTimeMillis
);
128 } catch (final InterruptedException e
) {
134 * Create a tracing project
137 * the name of the tracing project
139 public static void createProject(final String projectName
) {
143 UIThreadRunnable
.syncExec(new VoidResult() {
146 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
147 assertNotNull(project
);
151 SWTBotUtils
.waitForJobs();
158 * the name of the tracing project
159 * @param deleteResources
160 * whether or not to deleted resources under the project
164 public static void deleteProject(final String projectName
, boolean deleteResources
, SWTWorkbenchBot bot
) {
165 // Wait for any analysis to complete because it might create
166 // supplementary files
167 SWTBotUtils
.waitForJobs();
169 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
170 } catch (CoreException e
) {
173 SWTBotUtils
.waitForJobs();
175 closeSecondaryShells(bot
);
176 SWTBotUtils
.waitForJobs();
178 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
179 projectViewBot
.setFocus();
181 SWTBotTree treeBot
= projectViewBot
.bot().tree();
182 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
183 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
186 if (deleteResources
) {
187 bot
.shell("Delete Resources").setFocus();
188 final SWTBotCheckBox checkBox
= bot
.checkBox();
189 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
193 final SWTBotButton okButton
= bot
.button("OK");
194 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
197 SWTBotUtils
.waitForJobs();
201 * Deletes a project and its resources
204 * the name of the tracing project
208 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
209 deleteProject(projectName
, true, bot
);
213 * Creates an experiment
216 * a given workbench bot
218 * the name of the project, creates the project if needed
220 * the experiment name
222 public static void createExperiment(SWTWorkbenchBot bot
, String projectName
, final @NonNull String expName
) {
223 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
224 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, true);
225 TmfExperimentFolder expFolder
= tmfProject
.getExperimentsFolder();
226 assertNotNull(expFolder
);
227 NewExperimentOperation operation
= new NewExperimentOperation(expFolder
, expName
);
228 operation
.run(new NullProgressMonitor());
230 bot
.waitUntil(new DefaultCondition() {
232 public boolean test() throws Exception
{
233 TmfExperimentElement experiment
= expFolder
.getExperiment(expName
);
234 return experiment
!= null;
238 public String
getFailureMessage() {
239 return "Experiment (" + expName
+ ") couldn't be created";
246 * Focus on the main window
249 * swtbotshells for all the shells
251 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
252 SWTBotShell mainShell
= getMainShell(shellBots
);
253 if (mainShell
!= null) {
254 mainShell
.activate();
258 private static SWTBotShell
getMainShell(SWTBotShell
[] shellBots
) {
259 SWTBotShell mainShell
= null;
260 for (SWTBotShell shellBot
: shellBots
) {
261 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
262 mainShell
= shellBot
;
269 * Close all non-main shells that are visible.
274 public static void closeSecondaryShells(SWTWorkbenchBot bot
) {
275 SWTBotShell
[] shells
= bot
.shells();
276 SWTBotShell mainShell
= getMainShell(shells
);
277 if (mainShell
== null) {
281 // Close all non-main shell but make sure we don't close an invisible
282 // shell such the special "limbo shell" that Eclipse needs to work
283 Arrays
.stream(shells
)
284 .filter(shell
-> shell
!= mainShell
)
285 .filter(SWTBotShell
::isVisible
)
286 .peek(shell
-> log
.debug(MessageFormat
.format("Closing lingering shell with title {0}", shell
.getText())))
287 .forEach(SWTBotShell
::close
);
291 * Close a view with a title
294 * the title, like "welcome"
298 public static void closeView(String title
, SWTWorkbenchBot bot
) {
299 final List
<SWTBotView
> openViews
= bot
.views();
300 for (SWTBotView view
: openViews
) {
301 if (view
.getTitle().equalsIgnoreCase(title
)) {
303 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
309 * Close a view with an id
312 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
316 public static void closeViewById(String viewId
, SWTWorkbenchBot bot
) {
317 final SWTBotView view
= bot
.viewById(viewId
);
319 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
323 * Switch to the tracing perspective
325 public static void switchToTracingPerspective() {
326 switchToPerspective(TRACING_PERSPECTIVE_ID
);
330 * Switch to a given perspective
333 * the perspective id (like
334 * "org.eclipse.linuxtools.tmf.ui.perspective"
336 public static void switchToPerspective(final String id
) {
337 UIThreadRunnable
.syncExec(new VoidResult() {
341 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
342 } catch (WorkbenchException e
) {
343 fail(e
.getMessage());
350 * Initialize the environment for SWTBot
352 public static void initialize() {
355 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
356 UIThreadRunnable
.syncExec(() -> {
359 // There seems to be problems on some system where the main shell is
360 // not in focus initially. This was seen using Xvfb and Xephyr on some occasions.
361 focusMainWindow(bot
.shells());
363 Shell shell
= bot
.activeShell().widget
;
365 // Only adjust shell if it appears to be the top-most
366 if (shell
.getParent() == null) {
367 makeShellFullyVisible(shell
);
372 private static void printEnvironment() {
373 if (fPrintedEnvironment
) {
377 // Print some information about the environment that could affect test outcome
378 Rectangle bounds
= Display
.getDefault().getBounds();
379 System
.out
.println("Display size: " + bounds
.width
+ "x" + bounds
.height
);
381 String osVersion
= System
.getProperty("os.version");
382 if (osVersion
!= null) {
383 System
.out
.println("OS version=" + osVersion
);
385 String gtkVersion
= System
.getProperty("org.eclipse.swt.internal.gtk.version");
386 if (gtkVersion
!= null) {
387 System
.out
.println("GTK version=" + gtkVersion
);
388 // Try to print the GTK theme information as behavior can change depending on the theme
389 String gtkTheme
= System
.getProperty("org.eclipse.swt.internal.gtk.theme");
390 System
.out
.println("GTK theme=" + (gtkTheme
== null ?
"unknown" : gtkTheme
));
392 String overlayScrollbar
= System
.getenv("LIBOVERLAY_SCROLLBAR");
393 if (overlayScrollbar
!= null) {
394 System
.out
.println("LIBOVERLAY_SCROLLBAR=" + overlayScrollbar
);
396 String ubuntuMenuProxy
= System
.getenv("UBUNTU_MENUPROXY");
397 if (ubuntuMenuProxy
!= null) {
398 System
.out
.println("UBUNTU_MENUPROXY=" + ubuntuMenuProxy
);
402 System
.out
.println("Time zone: " + TimeZone
.getDefault().getDisplayName());
404 fPrintedEnvironment
= true;
408 * If the test is running in the UI thread then fail
410 private static void failIfUIThread() {
411 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
412 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"
413 + " that useUIThread is set to false in the pom.xml");
418 * Try to make the shell fully visible in the display. If the shell cannot
419 * fit the display, it will be positioned so that top-left corner is at
420 * <code>(0, 0)</code> in display-relative coordinates.
423 * the shell to make fully visible
425 private static void makeShellFullyVisible(Shell shell
) {
426 Rectangle displayBounds
= shell
.getDisplay().getBounds();
427 Point absCoord
= shell
.toDisplay(0, 0);
428 Point shellSize
= shell
.getSize();
430 Point newLocation
= new Point(absCoord
.x
, absCoord
.y
);
431 newLocation
.x
= Math
.max(0, Math
.min(absCoord
.x
, displayBounds
.width
- shellSize
.x
));
432 newLocation
.y
= Math
.max(0, Math
.min(absCoord
.y
, displayBounds
.height
- shellSize
.y
));
433 if (!newLocation
.equals(absCoord
)) {
434 shell
.setLocation(newLocation
);
439 * Open a trace, this does not perform any validation though
444 * the path of the trace file (absolute or relative)
446 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
448 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
449 openTrace(projectName
, tracePath
, traceType
, true);
453 * Open a trace, this does not perform any validation though
458 * the path of the trace file (absolute or relative)
460 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
462 * delay and wait for jobs
464 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
465 final Exception exception
[] = new Exception
[1];
467 UIThreadRunnable
.syncExec(new VoidResult() {
471 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
472 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
473 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
474 } catch (CoreException e
) {
479 if (exception
[0] != null) {
480 fail(exception
[0].getMessage());
490 * Finds an editor and sets focus to the editor
496 * @return the corresponding SWTBotEditor
498 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
499 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
500 final SWTBotEditor editorBot
= bot
.editor(matcher
);
501 IEditorPart iep
= editorBot
.getReference().getEditor(true);
502 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
504 UIThreadRunnable
.syncExec(new VoidResult() {
511 SWTBotUtils
.waitForJobs();
512 SWTBotUtils
.delay(1000);
513 assertNotNull(tmfEd
);
518 * Opens a trace in an editor and get the TmfEventsEditor
523 * the name of the project that contains the trace
525 * the trace element path (relative to Traces folder)
526 * @return TmfEventsEditor the opened editor
528 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
529 final SWTBotView projectExplorerView
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
530 projectExplorerView
.setFocus();
531 SWTBot projectExplorerBot
= projectExplorerView
.bot();
533 final SWTBotTree tree
= projectExplorerBot
.tree();
534 projectExplorerBot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(projectName
, tree
));
535 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
538 SWTBotTreeItem tracesNode
= getTraceProjectItem(projectExplorerBot
, treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
541 SWTBotTreeItem currentItem
= tracesNode
;
542 for (String segment
: elementPath
.segments()) {
543 currentItem
= getTraceProjectItem(projectExplorerBot
, currentItem
, segment
);
544 currentItem
.doubleClick();
547 SWTBotEditor editor
= bot
.editorByTitle(elementPath
.toString());
548 IEditorPart editorPart
= editor
.getReference().getEditor(false);
549 assertTrue(editorPart
instanceof TmfEventsEditor
);
550 return (TmfEventsEditor
) editorPart
;
554 * Returns the child tree item of the specified item with the given name.
555 * The project element label may have a count suffix in the format ' [n]'.
558 * a given workbench bot
560 * the parent tree item
562 * the desired child element name (without suffix)
563 * @return the a {@link SWTBotTreeItem} with the specified name
565 public static SWTBotTreeItem
getTraceProjectItem(SWTBot bot
, final SWTBotTreeItem parentItem
, final String name
) {
566 ProjectElementHasChild condition
= new ProjectElementHasChild(parentItem
, name
);
567 bot
.waitUntil(condition
);
568 return condition
.getItem();
572 * Select the traces folder
575 * a given workbench bot
577 * the name of the project (it needs to exist or else it would
579 * @return a {@link SWTBotTreeItem} of the "Traces" folder
581 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
582 SWTBotTreeItem projectTreeItem
= selectProject(bot
, projectName
);
583 projectTreeItem
.select();
584 SWTBotTreeItem tracesFolderItem
= getTraceProjectItem(bot
, projectTreeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
585 tracesFolderItem
.select();
586 return tracesFolderItem
;
590 * Clear the traces folder
593 * a given workbench bot
595 * the name of the project (needs to exist)
597 public static void clearTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
598 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
599 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
600 TmfTraceFolder tracesFolder
= tmfProject
.getTracesFolder();
602 for (TmfTraceElement traceElement
: tracesFolder
.getTraces()) {
603 traceElement
.delete(null);
606 final IFolder resource
= tracesFolder
.getResource();
607 resource
.accept(new IResourceVisitor() {
609 public boolean visit(IResource visitedResource
) throws CoreException
{
610 if (visitedResource
!= resource
) {
611 visitedResource
.delete(true, null);
615 }, IResource
.DEPTH_ONE
, 0);
616 } catch (CoreException e
) {
617 fail(e
.getMessage());
620 bot
.waitUntil(new DefaultCondition() {
621 private int fTraceNb
= 0;
624 public boolean test() throws Exception
{
625 List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
626 fTraceNb
= traces
.size();
627 return fTraceNb
== 0;
631 public String
getFailureMessage() {
632 return "Traces Folder not empty (" + fTraceNb
+ ")";
638 * Clear the trace folder (using the UI)
641 * a given workbench bot
643 * the name of the project (needs to exist)
645 public static void clearTracesFolderUI(SWTWorkbenchBot bot
, String projectName
) {
646 SWTBotTreeItem tracesFolder
= selectTracesFolder(bot
, projectName
);
647 tracesFolder
.contextMenu().menu("Clear").click();
648 String CONFIRM_CLEAR_DIALOG_TITLE
= "Confirm Clear";
649 bot
.waitUntil(Conditions
.shellIsActive(CONFIRM_CLEAR_DIALOG_TITLE
));
651 SWTBotShell shell
= bot
.shell(CONFIRM_CLEAR_DIALOG_TITLE
);
652 shell
.bot().button("Yes").click();
653 bot
.waitUntil(Conditions
.shellCloses(shell
));
654 bot
.waitWhile(ConditionHelpers
.treeItemHasChildren(tracesFolder
));
658 * Clear the experiment folder
661 * a given workbench bot
663 * the name of the project (needs to exist)
665 public static void clearExperimentFolder(SWTWorkbenchBot bot
, String projectName
) {
666 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
667 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
668 TmfExperimentFolder expFolder
= tmfProject
.getExperimentsFolder();
669 expFolder
.getExperiments().forEach(experiment
-> {
670 IResource resource
= experiment
.getResource();
672 // Close the experiment if open
673 experiment
.closeEditors();
675 IPath path
= resource
.getLocation();
677 // Delete supplementary files
678 experiment
.deleteSupplementaryFolder();
680 // Finally, delete the experiment
681 resource
.delete(true, null);
682 } catch (CoreException e
) {
683 fail(e
.getMessage());
687 bot
.waitUntil(new DefaultCondition() {
688 private int fExperimentNb
= 0;
691 public boolean test() throws Exception
{
692 List
<TmfExperimentElement
> experiments
= expFolder
.getExperiments();
693 fExperimentNb
= experiments
.size();
694 return fExperimentNb
== 0;
698 public String
getFailureMessage() {
699 return "Experiment Folder not empty (" + fExperimentNb
+ ")";
705 * Select the project in Project Explorer
708 * a given workbench bot
710 * the name of the project (it needs to exist or else it would time out)
711 * @return a {@link SWTBotTreeItem} of the project
713 public static SWTBotTreeItem
selectProject(SWTWorkbenchBot bot
, String projectName
) {
714 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
715 projectExplorerBot
.show();
716 // FIXME: Bug 496519. Sometimes, the tree becomes disabled for a certain
717 // amount of time. This can happen during a long running operation
718 // (BusyIndicator.showWhile) which brings up the modal dialog "operation
719 // in progress" and this disables all shells
720 projectExplorerBot
.bot().waitUntil(Conditions
.widgetIsEnabled(projectExplorerBot
.bot().tree()));
721 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
732 public static void openView(final String id
) {
733 final PartInitException res
[] = new PartInitException
[1];
734 UIThreadRunnable
.syncExec(new VoidResult() {
738 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
739 } catch (PartInitException e
) {
744 if (res
[0] != null) {
745 fail(res
[0].getMessage());
754 * the {@link SWTBotTable} table
756 public static void maximizeTable(SWTBotTable tableBot
) {
757 final AtomicBoolean controlResized
= new AtomicBoolean();
758 UIThreadRunnable
.syncExec(new VoidResult() {
761 tableBot
.widget
.addControlListener(new ControlAdapter() {
763 public void controlResized(ControlEvent e
) {
764 tableBot
.widget
.removeControlListener(this);
765 controlResized
.set(true);
771 tableBot
.pressShortcut(KeyStroke
.getInstance(IKeyLookup
.CTRL_NAME
+ "+"), KeyStroke
.getInstance("M"));
772 } catch (ParseException e
) {
775 new SWTBot().waitUntil(new DefaultCondition() {
777 public boolean test() throws Exception
{
778 return controlResized
.get();
782 public String
getFailureMessage() {
783 return "Control was not resized";
789 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
795 * the row of the table to look up
797 * the column of the table to look up
798 * @return the bounds in display relative coordinates
800 public static Rectangle
getCellBounds(final Table table
, final int row
, final int col
) {
801 return UIThreadRunnable
.syncExec(new Result
<Rectangle
>() {
803 public Rectangle
run() {
804 TableItem item
= table
.getItem(row
);
805 Rectangle bounds
= item
.getBounds(col
);
806 Point p
= table
.toDisplay(bounds
.x
, bounds
.y
);
807 Rectangle rect
= new Rectangle(p
.x
, p
.y
, bounds
.width
, bounds
.height
);
814 * Get the tree item from a tree at the specified location
819 * the tree to find the tree item in
821 * the path to the tree item, in the form of node names (from
823 * @return the tree item
825 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTree tree
, String
... nodeNames
) {
826 if (nodeNames
.length
== 0) {
830 bot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(nodeNames
[0], tree
));
831 SWTBotTreeItem currentNode
= tree
.getTreeItem(nodeNames
[0]);
832 return getTreeItem(bot
, currentNode
, Arrays
.copyOfRange(nodeNames
, 1, nodeNames
.length
));
836 * Get the tree item from a parent tree item at the specified location
841 * the treeItem to find the tree item under
843 * the path to the tree item, in the form of node names (from
845 * @return the tree item
847 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTreeItem treeItem
, String
... nodeNames
) {
848 if (nodeNames
.length
== 0) {
852 SWTBotTreeItem currentNode
= treeItem
;
853 for (int i
= 0; i
< nodeNames
.length
; i
++) {
854 bot
.waitUntil(ConditionHelpers
.treeItemHasChildren(treeItem
));
855 currentNode
.expand();
857 String nodeName
= nodeNames
[i
];
859 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
860 } catch (TimeoutException e
) {
861 //FIXME: Sometimes in a JFace TreeViewer, it expands to nothing. Need to find out why.
862 currentNode
.collapse();
863 currentNode
.expand();
864 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
867 SWTBotTreeItem newNode
= currentNode
.getNode(nodeName
);
868 currentNode
= newNode
;
875 * Press the keyboard shortcut that goes to the top of a tree widget. The
876 * key combination can differ on different platforms.
879 * the keyboard to use
881 public static void pressShortcutGoToTreeTop(Keyboard keyboard
) {
882 if (SWTUtils
.isMac()) {
883 keyboard
.pressShortcut(Keystrokes
.ALT
, Keystrokes
.UP
);
885 keyboard
.pressShortcut(Keystrokes
.HOME
);
890 * Get the active events editor. Note that this will wait until such editor
893 * @param workbenchBot
894 * a given workbench bot
895 * @return the active events editor
897 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
) {
898 ConditionHelpers
.ActiveEventsEditor condition
= new ConditionHelpers
.ActiveEventsEditor(workbenchBot
, null);
899 workbenchBot
.waitUntil(condition
);
900 return condition
.getActiveEditor();
904 * Get the active events editor. Note that this will wait until such editor
907 * @param workbenchBot
908 * a given workbench bot
910 * the desired editor title. If null, any active events editor
911 * will be considered valid.
912 * @return the active events editor
914 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
, String editorTitle
) {
915 ConditionHelpers
.ActiveEventsEditor condition
= new ConditionHelpers
.ActiveEventsEditor(workbenchBot
, editorTitle
);
916 workbenchBot
.waitUntil(condition
);
917 return condition
.getActiveEditor();
921 * Open the preferences dialog and return the corresponding shell.
924 * a given workbench bot
925 * @return the preferences shell
927 public static SWTBotShell
openPreferences(SWTBot bot
) {
928 if (SWTUtils
.isMac()) {
929 // On Mac, the Preferences menu item is under the application name.
930 // For some reason, we can't access the application menu anymore so
931 // we use the keyboard shortcut.
933 bot
.activeShell().pressShortcut(KeyStroke
.getInstance(IKeyLookup
.COMMAND_NAME
+ "+"), KeyStroke
.getInstance(","));
934 } catch (ParseException e
) {
938 bot
.menu(WINDOW_MENU
).menu(PREFERENCES_MENU_ITEM
).click();
941 bot
.waitUntil(Conditions
.shellIsActive(PREFERENCES_MENU_ITEM
));
942 return bot
.activeShell();