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
.WaitUtils
;
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 String WINDOW_MENU
= "Window";
97 private static final String PREFERENCES_MENU_ITEM
= "Preferences";
98 private static boolean fPrintedEnvironment
= false;
99 private static Logger log
= Logger
.getLogger(SWTBotUtils
.class);
101 private SWTBotUtils() {
105 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
108 * Waits for all Eclipse jobs to finish. Times out after
109 * WaitUtils#MAX_JOBS_WAIT_TIME by default.
111 * @throws RuntimeException
112 * once the waiting time passes the default maximum value
114 * @deprecated Use {@link WaitUtils#waitForJobs()} instead
117 public static void waitForJobs() {
118 WaitUtils
.waitForJobs();
122 * Sleeps current thread for a given time.
124 * @param waitTimeMillis
125 * time in milliseconds to wait
127 public static void delay(final long waitTimeMillis
) {
129 Thread
.sleep(waitTimeMillis
);
130 } catch (final InterruptedException e
) {
136 * Create a tracing project
139 * the name of the tracing project
141 public static void createProject(final String projectName
) {
145 UIThreadRunnable
.syncExec(new VoidResult() {
148 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
149 assertNotNull(project
);
153 WaitUtils
.waitForJobs();
160 * the name of the tracing project
161 * @param deleteResources
162 * whether or not to deleted resources under the project
166 public static void deleteProject(final String projectName
, boolean deleteResources
, SWTWorkbenchBot bot
) {
167 // Wait for any analysis to complete because it might create
168 // supplementary files
169 WaitUtils
.waitForJobs();
171 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
172 } catch (CoreException e
) {
175 WaitUtils
.waitForJobs();
177 closeSecondaryShells(bot
);
178 WaitUtils
.waitForJobs();
180 if (!ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).exists()) {
184 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
185 projectViewBot
.setFocus();
187 SWTBotTree treeBot
= projectViewBot
.bot().tree();
188 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
189 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
192 if (deleteResources
) {
193 bot
.shell("Delete Resources").setFocus();
194 final SWTBotCheckBox checkBox
= bot
.checkBox();
195 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
199 final SWTBotButton okButton
= bot
.button("OK");
200 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
203 WaitUtils
.waitForJobs();
207 * Deletes a project and its resources
210 * the name of the tracing project
214 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
215 deleteProject(projectName
, true, bot
);
219 * Creates an experiment
222 * a given workbench bot
224 * the name of the project, creates the project if needed
226 * the experiment name
228 public static void createExperiment(SWTWorkbenchBot bot
, String projectName
, final @NonNull String expName
) {
229 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
230 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, true);
231 TmfExperimentFolder expFolder
= tmfProject
.getExperimentsFolder();
232 assertNotNull(expFolder
);
233 NewExperimentOperation operation
= new NewExperimentOperation(expFolder
, expName
);
234 operation
.run(new NullProgressMonitor());
236 bot
.waitUntil(new DefaultCondition() {
238 public boolean test() throws Exception
{
239 TmfExperimentElement experiment
= expFolder
.getExperiment(expName
);
240 return experiment
!= null;
244 public String
getFailureMessage() {
245 return "Experiment (" + expName
+ ") couldn't be created";
252 * Focus on the main window
255 * swtbotshells for all the shells
257 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
258 SWTBotShell mainShell
= getMainShell(shellBots
);
259 if (mainShell
!= null) {
260 mainShell
.activate();
264 private static SWTBotShell
getMainShell(SWTBotShell
[] shellBots
) {
265 SWTBotShell mainShell
= null;
266 for (SWTBotShell shellBot
: shellBots
) {
267 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
268 mainShell
= shellBot
;
275 * Close all non-main shells that are visible.
280 public static void closeSecondaryShells(SWTWorkbenchBot bot
) {
281 SWTBotShell
[] shells
= bot
.shells();
282 SWTBotShell mainShell
= getMainShell(shells
);
283 if (mainShell
== null) {
287 // Close all non-main shell but make sure we don't close an invisible
288 // shell such the special "limbo shell" that Eclipse needs to work
289 Arrays
.stream(shells
)
290 .filter(shell
-> shell
!= mainShell
)
291 .filter(s
-> !s
.widget
.isDisposed())
292 .filter(SWTBotShell
::isVisible
)
293 .peek(shell
-> log
.debug(MessageFormat
.format("Closing lingering shell with title {0}", shell
.getText())))
294 .forEach(SWTBotShell
::close
);
298 * Close a view with a title
301 * the title, like "welcome"
305 public static void closeView(String title
, SWTWorkbenchBot bot
) {
306 final List
<SWTBotView
> openViews
= bot
.views();
307 for (SWTBotView view
: openViews
) {
308 if (view
.getTitle().equalsIgnoreCase(title
)) {
310 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
316 * Close a view with an id
319 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
323 public static void closeViewById(String viewId
, SWTWorkbenchBot bot
) {
324 final SWTBotView view
= bot
.viewById(viewId
);
326 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
330 * Switch to the tracing perspective
332 public static void switchToTracingPerspective() {
333 switchToPerspective(TRACING_PERSPECTIVE_ID
);
337 * Switch to a given perspective
340 * the perspective id (like
341 * "org.eclipse.linuxtools.tmf.ui.perspective"
343 public static void switchToPerspective(final String id
) {
344 UIThreadRunnable
.syncExec(new VoidResult() {
348 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
349 } catch (WorkbenchException e
) {
350 fail(e
.getMessage());
357 * Initialize the environment for SWTBot
359 public static void initialize() {
362 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
363 UIThreadRunnable
.syncExec(() -> {
366 // There seems to be problems on some system where the main shell is
367 // not in focus initially. This was seen using Xvfb and Xephyr on some occasions.
368 focusMainWindow(bot
.shells());
370 Shell shell
= bot
.activeShell().widget
;
372 // Only adjust shell if it appears to be the top-most
373 if (shell
.getParent() == null) {
374 makeShellFullyVisible(shell
);
379 private static void printEnvironment() {
380 if (fPrintedEnvironment
) {
384 // Print some information about the environment that could affect test outcome
385 Rectangle bounds
= Display
.getDefault().getBounds();
386 System
.out
.println("Display size: " + bounds
.width
+ "x" + bounds
.height
);
388 String osVersion
= System
.getProperty("os.version");
389 if (osVersion
!= null) {
390 System
.out
.println("OS version=" + osVersion
);
392 String gtkVersion
= System
.getProperty("org.eclipse.swt.internal.gtk.version");
393 if (gtkVersion
!= null) {
394 System
.out
.println("GTK version=" + gtkVersion
);
395 // Try to print the GTK theme information as behavior can change depending on the theme
396 String gtkTheme
= System
.getProperty("org.eclipse.swt.internal.gtk.theme");
397 System
.out
.println("GTK theme=" + (gtkTheme
== null ?
"unknown" : gtkTheme
));
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());
492 WaitUtils
.waitForJobs();
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 WaitUtils
.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
.doubleClick();
554 SWTBotEditor editor
= bot
.editorByTitle(elementPath
.toString());
555 IEditorPart editorPart
= editor
.getReference().getEditor(false);
556 assertTrue(editorPart
instanceof TmfEventsEditor
);
557 return (TmfEventsEditor
) editorPart
;
561 * Returns the child tree item of the specified item at the given sub-path.
562 * The project element labels may have a count suffix in the format ' [n]'.
565 * a given workbench bot
567 * the parent tree item
569 * the desired child element sub-path (without suffix)
570 * @return the a {@link SWTBotTreeItem} with the specified name
572 public static SWTBotTreeItem
getTraceProjectItem(SWTBot bot
, final SWTBotTreeItem parentItem
, final String
... path
) {
573 SWTBotTreeItem item
= parentItem
;
574 for (String name
: path
) {
575 item
= getTraceProjectItem(bot
, item
, name
);
581 * Returns the child tree item of the specified item with the given name.
582 * The project element label may have a count suffix in the format ' [n]'.
585 * a given workbench bot
587 * the parent tree item
589 * the desired child element name (without suffix)
590 * @return the a {@link SWTBotTreeItem} with the specified name
592 public static SWTBotTreeItem
getTraceProjectItem(SWTBot bot
, final SWTBotTreeItem parentItem
, final String name
) {
593 ProjectElementHasChild condition
= new ProjectElementHasChild(parentItem
, name
);
594 bot
.waitUntil(condition
);
595 return condition
.getItem();
599 * Select the traces folder
602 * a given workbench bot
604 * the name of the project (it needs to exist or else it would
606 * @return a {@link SWTBotTreeItem} of the "Traces" folder
608 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
609 SWTBotTreeItem projectTreeItem
= selectProject(bot
, projectName
);
610 projectTreeItem
.select();
611 SWTBotTreeItem tracesFolderItem
= getTraceProjectItem(bot
, projectTreeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
612 tracesFolderItem
.select();
613 return tracesFolderItem
;
617 * Clear the traces folder
620 * a given workbench bot
622 * the name of the project (needs to exist)
624 public static void clearTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
625 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
626 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
627 TmfTraceFolder tracesFolder
= tmfProject
.getTracesFolder();
629 for (TmfTraceElement traceElement
: tracesFolder
.getTraces()) {
630 traceElement
.delete(null);
633 final IFolder resource
= tracesFolder
.getResource();
634 resource
.accept(new IResourceVisitor() {
636 public boolean visit(IResource visitedResource
) throws CoreException
{
637 if (visitedResource
!= resource
) {
638 visitedResource
.delete(true, null);
642 }, IResource
.DEPTH_ONE
, 0);
643 } catch (CoreException e
) {
644 fail(e
.getMessage());
647 bot
.waitUntil(new DefaultCondition() {
648 private int fTraceNb
= 0;
651 public boolean test() throws Exception
{
652 List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
653 fTraceNb
= traces
.size();
654 return fTraceNb
== 0;
658 public String
getFailureMessage() {
659 return "Traces Folder not empty (" + fTraceNb
+ ")";
665 * Clear the trace folder (using the UI)
668 * a given workbench bot
670 * the name of the project (needs to exist)
672 public static void clearTracesFolderUI(SWTWorkbenchBot bot
, String projectName
) {
673 SWTBotTreeItem tracesFolder
= selectTracesFolder(bot
, projectName
);
674 tracesFolder
.contextMenu().menu("Clear").click();
675 String CONFIRM_CLEAR_DIALOG_TITLE
= "Confirm Clear";
676 bot
.waitUntil(Conditions
.shellIsActive(CONFIRM_CLEAR_DIALOG_TITLE
));
678 SWTBotShell shell
= bot
.shell(CONFIRM_CLEAR_DIALOG_TITLE
);
679 shell
.bot().button("Yes").click();
680 bot
.waitUntil(Conditions
.shellCloses(shell
));
681 bot
.waitWhile(ConditionHelpers
.treeItemHasChildren(tracesFolder
));
685 * Clear the experiment folder
688 * a given workbench bot
690 * the name of the project (needs to exist)
692 public static void clearExperimentFolder(SWTWorkbenchBot bot
, String projectName
) {
693 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
694 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
695 TmfExperimentFolder expFolder
= tmfProject
.getExperimentsFolder();
696 expFolder
.getExperiments().forEach(experiment
-> {
697 IResource resource
= experiment
.getResource();
699 // Close the experiment if open
700 experiment
.closeEditors();
702 IPath path
= resource
.getLocation();
704 // Delete supplementary files
705 experiment
.deleteSupplementaryFolder();
707 // Finally, delete the experiment
708 resource
.delete(true, null);
709 } catch (CoreException e
) {
710 fail(e
.getMessage());
714 bot
.waitUntil(new DefaultCondition() {
715 private int fExperimentNb
= 0;
718 public boolean test() throws Exception
{
719 List
<TmfExperimentElement
> experiments
= expFolder
.getExperiments();
720 fExperimentNb
= experiments
.size();
721 return fExperimentNb
== 0;
725 public String
getFailureMessage() {
726 return "Experiment Folder not empty (" + fExperimentNb
+ ")";
732 * Select the project in Project Explorer
735 * a given workbench bot
737 * the name of the project (it needs to exist or else it would time out)
738 * @return a {@link SWTBotTreeItem} of the project
740 public static SWTBotTreeItem
selectProject(SWTWorkbenchBot bot
, String projectName
) {
741 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
742 projectExplorerBot
.show();
743 // FIXME: Bug 496519. Sometimes, the tree becomes disabled for a certain
744 // amount of time. This can happen during a long running operation
745 // (BusyIndicator.showWhile) which brings up the modal dialog "operation
746 // in progress" and this disables all shells
747 projectExplorerBot
.bot().waitUntil(Conditions
.widgetIsEnabled(projectExplorerBot
.bot().tree()));
748 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
759 public static void openView(final String id
) {
760 final PartInitException res
[] = new PartInitException
[1];
761 UIThreadRunnable
.syncExec(new VoidResult() {
765 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
766 } catch (PartInitException e
) {
771 if (res
[0] != null) {
772 fail(res
[0].getMessage());
774 WaitUtils
.waitForJobs();
781 * the {@link SWTBotTable} table
783 public static void maximizeTable(SWTBotTable tableBot
) {
784 final AtomicBoolean controlResized
= new AtomicBoolean();
785 UIThreadRunnable
.syncExec(new VoidResult() {
788 tableBot
.widget
.addControlListener(new ControlAdapter() {
790 public void controlResized(ControlEvent e
) {
791 tableBot
.widget
.removeControlListener(this);
792 controlResized
.set(true);
798 tableBot
.pressShortcut(KeyStroke
.getInstance(IKeyLookup
.CTRL_NAME
+ "+"), KeyStroke
.getInstance("M"));
799 } catch (ParseException e
) {
802 new SWTBot().waitUntil(new DefaultCondition() {
804 public boolean test() throws Exception
{
805 return controlResized
.get();
809 public String
getFailureMessage() {
810 return "Control was not resized";
816 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
822 * the row of the table to look up
824 * the column of the table to look up
825 * @return the bounds in display relative coordinates
827 public static Rectangle
getCellBounds(final Table table
, final int row
, final int col
) {
828 return UIThreadRunnable
.syncExec(new Result
<Rectangle
>() {
830 public Rectangle
run() {
831 TableItem item
= table
.getItem(row
);
832 Rectangle bounds
= item
.getBounds(col
);
833 Point p
= table
.toDisplay(bounds
.x
, bounds
.y
);
834 Rectangle rect
= new Rectangle(p
.x
, p
.y
, bounds
.width
, bounds
.height
);
841 * Get the tree item from a tree at the specified location
846 * the tree to find the tree item in
848 * the path to the tree item, in the form of node names (from
850 * @return the tree item
852 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTree tree
, String
... nodeNames
) {
853 if (nodeNames
.length
== 0) {
857 bot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(nodeNames
[0], tree
));
858 SWTBotTreeItem currentNode
= tree
.getTreeItem(nodeNames
[0]);
859 return getTreeItem(bot
, currentNode
, Arrays
.copyOfRange(nodeNames
, 1, nodeNames
.length
));
863 * Get the tree item from a parent tree item at the specified location
868 * the treeItem to find the tree item under
870 * the path to the tree item, in the form of node names (from
872 * @return the tree item
874 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTreeItem treeItem
, String
... nodeNames
) {
875 if (nodeNames
.length
== 0) {
879 SWTBotTreeItem currentNode
= treeItem
;
880 for (int i
= 0; i
< nodeNames
.length
; i
++) {
881 bot
.waitUntil(ConditionHelpers
.treeItemHasChildren(treeItem
));
882 currentNode
.expand();
884 String nodeName
= nodeNames
[i
];
886 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
887 } catch (TimeoutException e
) {
888 //FIXME: Sometimes in a JFace TreeViewer, it expands to nothing. Need to find out why.
889 currentNode
.collapse();
890 currentNode
.expand();
891 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
894 SWTBotTreeItem newNode
= currentNode
.getNode(nodeName
);
895 currentNode
= newNode
;
902 * Press the keyboard shortcut that goes to the top of a tree widget. The
903 * key combination can differ on different platforms.
906 * the keyboard to use
908 public static void pressShortcutGoToTreeTop(Keyboard keyboard
) {
909 if (SWTUtils
.isMac()) {
910 keyboard
.pressShortcut(Keystrokes
.ALT
, Keystrokes
.UP
);
912 keyboard
.pressShortcut(Keystrokes
.HOME
);
917 * Get the active events editor. Note that this will wait until such editor
920 * @param workbenchBot
921 * a given workbench bot
922 * @return the active events editor
924 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
) {
925 ConditionHelpers
.ActiveEventsEditor condition
= new ConditionHelpers
.ActiveEventsEditor(workbenchBot
, null);
926 workbenchBot
.waitUntil(condition
);
927 return condition
.getActiveEditor();
931 * Get the active events editor. Note that this will wait until such editor
934 * @param workbenchBot
935 * a given workbench bot
937 * the desired editor title. If null, any active events editor
938 * will be considered valid.
939 * @return the active events editor
941 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
, String editorTitle
) {
942 ConditionHelpers
.ActiveEventsEditor condition
= new ConditionHelpers
.ActiveEventsEditor(workbenchBot
, editorTitle
);
943 workbenchBot
.waitUntil(condition
);
944 return condition
.getActiveEditor();
948 * Open the preferences dialog and return the corresponding shell.
951 * a given workbench bot
952 * @return the preferences shell
954 public static SWTBotShell
openPreferences(SWTBot bot
) {
955 if (SWTUtils
.isMac()) {
956 // On Mac, the Preferences menu item is under the application name.
957 // For some reason, we can't access the application menu anymore so
958 // we use the keyboard shortcut.
960 bot
.activeShell().pressShortcut(KeyStroke
.getInstance(IKeyLookup
.COMMAND_NAME
+ "+"), KeyStroke
.getInstance(","));
961 } catch (ParseException e
) {
965 bot
.menu(WINDOW_MENU
).menu(PREFERENCES_MENU_ITEM
).click();
968 bot
.waitUntil(Conditions
.shellIsActive(PREFERENCES_MENU_ITEM
));
969 return bot
.activeShell();