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
.core
.runtime
.jobs
.Job
;
34 import org
.eclipse
.jdt
.annotation
.NonNull
;
35 import org
.eclipse
.jface
.bindings
.keys
.IKeyLookup
;
36 import org
.eclipse
.jface
.bindings
.keys
.KeyStroke
;
37 import org
.eclipse
.jface
.bindings
.keys
.ParseException
;
38 import org
.eclipse
.swt
.events
.ControlAdapter
;
39 import org
.eclipse
.swt
.events
.ControlEvent
;
40 import org
.eclipse
.swt
.graphics
.Point
;
41 import org
.eclipse
.swt
.graphics
.Rectangle
;
42 import org
.eclipse
.swt
.widgets
.Display
;
43 import org
.eclipse
.swt
.widgets
.Shell
;
44 import org
.eclipse
.swt
.widgets
.Table
;
45 import org
.eclipse
.swt
.widgets
.TableItem
;
46 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
47 import org
.eclipse
.swtbot
.eclipse
.finder
.matchers
.WidgetMatcherFactory
;
48 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
49 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
50 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
51 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
52 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keyboard
;
53 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keystrokes
;
54 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
55 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
56 import org
.eclipse
.swtbot
.swt
.finder
.utils
.MessageFormat
;
57 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTUtils
;
58 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
59 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
60 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
61 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
62 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
63 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
64 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
65 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
66 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
67 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.TimeoutException
;
68 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.operations
.NewExperimentOperation
;
69 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentElement
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectElement
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
77 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
.ProjectElementHasChild
;
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 TimeoutException
113 * once the waiting time passes the default maximum value
115 public static void waitForJobs() {
116 waitForJobs(MAX_JOBS_WAIT_TIME
);
120 * Waits for all Eclipse jobs to finish
123 * the maximum time to wait, in milliseconds. Once the waiting
124 * time passes the maximum value, a TimeoutException is thrown
125 * @throws TimeoutException
126 * once the waiting time passes the maximum value
128 public static void waitForJobs(long maxWait
) {
129 long waitStart
= System
.currentTimeMillis();
130 while (!Job
.getJobManager().isIdle()) {
131 if (System
.currentTimeMillis() - waitStart
> maxWait
) {
133 throw new TimeoutException("Timed out waiting for jobs to finish.");
140 private static void printJobs() {
141 Job
[] jobs
= Job
.getJobManager().find(null);
142 for (Job job
: jobs
) {
143 System
.err
.println(job
.toString() + " state: " + jobStateToString(job
.getState()));
144 Thread thread
= job
.getThread();
145 if (thread
!= null) {
146 for (StackTraceElement stractTraceElement
: thread
.getStackTrace()) {
147 System
.err
.println(" " + stractTraceElement
);
150 System
.err
.println();
154 private static String
jobStateToString(int jobState
) {
170 * Sleeps current thread for a given time.
172 * @param waitTimeMillis
173 * time in milliseconds to wait
175 public static void delay(final long waitTimeMillis
) {
177 Thread
.sleep(waitTimeMillis
);
178 } catch (final InterruptedException e
) {
184 * Create a tracing project
187 * the name of the tracing project
189 public static void createProject(final String projectName
) {
193 UIThreadRunnable
.syncExec(new VoidResult() {
196 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
197 assertNotNull(project
);
201 SWTBotUtils
.waitForJobs();
208 * the name of the tracing project
209 * @param deleteResources
210 * whether or not to deleted resources under the project
214 public static void deleteProject(final String projectName
, boolean deleteResources
, SWTWorkbenchBot bot
) {
215 // Wait for any analysis to complete because it might create
216 // supplementary files
217 SWTBotUtils
.waitForJobs();
219 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
220 } catch (CoreException e
) {
223 SWTBotUtils
.waitForJobs();
225 closeSecondaryShells(bot
);
226 SWTBotUtils
.waitForJobs();
228 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
229 projectViewBot
.setFocus();
231 SWTBotTree treeBot
= projectViewBot
.bot().tree();
232 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
233 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
236 if (deleteResources
) {
237 bot
.shell("Delete Resources").setFocus();
238 final SWTBotCheckBox checkBox
= bot
.checkBox();
239 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
243 final SWTBotButton okButton
= bot
.button("OK");
244 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
247 SWTBotUtils
.waitForJobs();
251 * Deletes a project and its resources
254 * the name of the tracing project
258 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
259 deleteProject(projectName
, true, bot
);
263 * Creates an experiment
266 * a given workbench bot
268 * the name of the project, creates the project if needed
270 * the experiment name
272 public static void createExperiment(SWTWorkbenchBot bot
, String projectName
, final @NonNull String expName
) {
273 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
274 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, true);
275 TmfExperimentFolder expFolder
= tmfProject
.getExperimentsFolder();
276 assertNotNull(expFolder
);
277 NewExperimentOperation operation
= new NewExperimentOperation(expFolder
, expName
);
278 operation
.run(new NullProgressMonitor());
280 bot
.waitUntil(new DefaultCondition() {
282 public boolean test() throws Exception
{
283 TmfExperimentElement experiment
= expFolder
.getExperiment(expName
);
284 return experiment
!= null;
288 public String
getFailureMessage() {
289 return "Experiment (" + expName
+ ") couldn't be created";
296 * Focus on the main window
299 * swtbotshells for all the shells
301 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
302 SWTBotShell mainShell
= getMainShell(shellBots
);
303 if (mainShell
!= null) {
304 mainShell
.activate();
308 private static SWTBotShell
getMainShell(SWTBotShell
[] shellBots
) {
309 SWTBotShell mainShell
= null;
310 for (SWTBotShell shellBot
: shellBots
) {
311 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
312 mainShell
= shellBot
;
319 * Close all non-main shells that are visible.
324 public static void closeSecondaryShells(SWTWorkbenchBot bot
) {
325 SWTBotShell
[] shells
= bot
.shells();
326 SWTBotShell mainShell
= getMainShell(shells
);
327 if (mainShell
== null) {
331 // Close all non-main shell but make sure we don't close an invisible
332 // shell such the special "limbo shell" that Eclipse needs to work
333 Arrays
.stream(shells
)
334 .filter(shell
-> shell
!= mainShell
)
335 .filter(SWTBotShell
::isVisible
)
336 .peek(shell
-> log
.debug(MessageFormat
.format("Closing ligering shell with title {0}", shell
.getText())))
337 .forEach(SWTBotShell
::close
);
341 * Close a view with a title
344 * the title, like "welcome"
348 public static void closeView(String title
, SWTWorkbenchBot bot
) {
349 final List
<SWTBotView
> openViews
= bot
.views();
350 for (SWTBotView view
: openViews
) {
351 if (view
.getTitle().equalsIgnoreCase(title
)) {
353 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
359 * Close a view with an id
362 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
366 public static void closeViewById(String viewId
, SWTWorkbenchBot bot
) {
367 final SWTBotView view
= bot
.viewById(viewId
);
369 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
373 * Switch to the tracing perspective
375 public static void switchToTracingPerspective() {
376 switchToPerspective(TRACING_PERSPECTIVE_ID
);
380 * Switch to a given perspective
383 * the perspective id (like
384 * "org.eclipse.linuxtools.tmf.ui.perspective"
386 public static void switchToPerspective(final String id
) {
387 UIThreadRunnable
.syncExec(new VoidResult() {
391 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
392 } catch (WorkbenchException e
) {
393 fail(e
.getMessage());
400 * Initialize the environment for SWTBot
402 public static void initialize() {
405 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
406 UIThreadRunnable
.syncExec(() -> {
409 // There seems to be problems on some system where the main shell is
410 // not in focus initially. This was seen using Xvfb and Xephyr on some occasions.
411 focusMainWindow(bot
.shells());
413 Shell shell
= bot
.activeShell().widget
;
415 // Only adjust shell if it appears to be the top-most
416 if (shell
.getParent() == null) {
417 makeShellFullyVisible(shell
);
422 private static void printEnvironment() {
423 if (fPrintedEnvironment
) {
427 // Print some information about the environment that could affect test outcome
428 Rectangle bounds
= Display
.getDefault().getBounds();
429 System
.out
.println("Display size: " + bounds
.width
+ "x" + bounds
.height
);
431 String osVersion
= System
.getProperty("os.version");
432 if (osVersion
!= null) {
433 System
.out
.println("OS version=" + osVersion
);
435 String gtkVersion
= System
.getProperty("org.eclipse.swt.internal.gtk.version");
436 if (gtkVersion
!= null) {
437 System
.out
.println("GTK version=" + gtkVersion
);
438 // Try to print the GTK theme information as behavior can change depending on the theme
439 String gtkTheme
= System
.getProperty("org.eclipse.swt.internal.gtk.theme");
440 System
.out
.println("GTK theme=" + (gtkTheme
== null ?
"unknown" : gtkTheme
));
442 String overlayScrollbar
= System
.getenv("LIBOVERLAY_SCROLLBAR");
443 if (overlayScrollbar
!= null) {
444 System
.out
.println("LIBOVERLAY_SCROLLBAR=" + overlayScrollbar
);
446 String ubuntuMenuProxy
= System
.getenv("UBUNTU_MENUPROXY");
447 if (ubuntuMenuProxy
!= null) {
448 System
.out
.println("UBUNTU_MENUPROXY=" + ubuntuMenuProxy
);
452 System
.out
.println("Time zone: " + TimeZone
.getDefault().getDisplayName());
454 fPrintedEnvironment
= true;
458 * If the test is running in the UI thread then fail
460 private static void failIfUIThread() {
461 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
462 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"
463 + " that useUIThread is set to false in the pom.xml");
468 * Try to make the shell fully visible in the display. If the shell cannot
469 * fit the display, it will be positioned so that top-left corner is at
470 * <code>(0, 0)</code> in display-relative coordinates.
473 * the shell to make fully visible
475 private static void makeShellFullyVisible(Shell shell
) {
476 Rectangle displayBounds
= shell
.getDisplay().getBounds();
477 Point absCoord
= shell
.toDisplay(0, 0);
478 Point shellSize
= shell
.getSize();
480 Point newLocation
= new Point(absCoord
.x
, absCoord
.y
);
481 newLocation
.x
= Math
.max(0, Math
.min(absCoord
.x
, displayBounds
.width
- shellSize
.x
));
482 newLocation
.y
= Math
.max(0, Math
.min(absCoord
.y
, displayBounds
.height
- shellSize
.y
));
483 if (!newLocation
.equals(absCoord
)) {
484 shell
.setLocation(newLocation
);
489 * Open a trace, this does not perform any validation though
494 * the path of the trace file (absolute or relative)
496 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
498 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
499 openTrace(projectName
, tracePath
, traceType
, true);
503 * Open a trace, this does not perform any validation though
508 * the path of the trace file (absolute or relative)
510 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
512 * delay and wait for jobs
514 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
515 final Exception exception
[] = new Exception
[1];
517 UIThreadRunnable
.syncExec(new VoidResult() {
521 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
522 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
523 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
524 } catch (CoreException e
) {
529 if (exception
[0] != null) {
530 fail(exception
[0].getMessage());
540 * Finds an editor and sets focus to the editor
546 * @return the corresponding SWTBotEditor
548 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
549 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
550 final SWTBotEditor editorBot
= bot
.editor(matcher
);
551 IEditorPart iep
= editorBot
.getReference().getEditor(true);
552 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
554 UIThreadRunnable
.syncExec(new VoidResult() {
561 SWTBotUtils
.waitForJobs();
562 SWTBotUtils
.delay(1000);
563 assertNotNull(tmfEd
);
568 * Opens a trace in an editor and get the TmfEventsEditor
573 * the name of the project that contains the trace
575 * the trace element path (relative to Traces folder)
576 * @return TmfEventsEditor the opened editor
578 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
579 final SWTBotView projectExplorerView
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
580 projectExplorerView
.setFocus();
581 SWTBot projectExplorerBot
= projectExplorerView
.bot();
583 final SWTBotTree tree
= projectExplorerBot
.tree();
584 projectExplorerBot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(projectName
, tree
));
585 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
588 SWTBotTreeItem tracesNode
= getTraceProjectItem(projectExplorerBot
, treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
591 SWTBotTreeItem currentItem
= tracesNode
;
592 for (String segment
: elementPath
.segments()) {
593 currentItem
= getTraceProjectItem(projectExplorerBot
, currentItem
, segment
);
594 currentItem
.doubleClick();
597 SWTBotEditor editor
= bot
.editorByTitle(elementPath
.toString());
598 IEditorPart editorPart
= editor
.getReference().getEditor(false);
599 assertTrue(editorPart
instanceof TmfEventsEditor
);
600 return (TmfEventsEditor
) editorPart
;
604 * Returns the child tree item of the specified item with the given name.
605 * The project element label may have a count suffix in the format ' [n]'.
608 * a given workbench bot
610 * the parent tree item
612 * the desired child element name (without suffix)
613 * @return the a {@link SWTBotTreeItem} with the specified name
615 public static SWTBotTreeItem
getTraceProjectItem(SWTBot bot
, final SWTBotTreeItem parentItem
, final String name
) {
616 ProjectElementHasChild condition
= new ProjectElementHasChild(parentItem
, name
);
617 bot
.waitUntil(condition
);
618 return condition
.getItem();
622 * Select the traces folder
625 * a given workbench bot
627 * the name of the project (it needs to exist or else it would
629 * @return a {@link SWTBotTreeItem} of the "Traces" folder
631 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
632 SWTBotTreeItem projectTreeItem
= selectProject(bot
, projectName
);
633 projectTreeItem
.select();
634 SWTBotTreeItem tracesFolderItem
= getTraceProjectItem(bot
, projectTreeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
635 tracesFolderItem
.select();
636 return tracesFolderItem
;
640 * Clear the traces folder
643 * a given workbench bot
645 * the name of the project (needs to exist)
647 public static void clearTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
648 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
649 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
650 TmfTraceFolder tracesFolder
= tmfProject
.getTracesFolder();
652 for (TmfTraceElement traceElement
: tracesFolder
.getTraces()) {
653 traceElement
.delete(null);
656 final IFolder resource
= tracesFolder
.getResource();
657 resource
.accept(new IResourceVisitor() {
659 public boolean visit(IResource visitedResource
) throws CoreException
{
660 if (visitedResource
!= resource
) {
661 visitedResource
.delete(true, null);
665 }, IResource
.DEPTH_ONE
, 0);
666 } catch (CoreException e
) {
667 fail(e
.getMessage());
670 bot
.waitUntil(new DefaultCondition() {
671 private int fTraceNb
= 0;
674 public boolean test() throws Exception
{
675 List
<TmfTraceElement
> traces
= tracesFolder
.getTraces();
676 fTraceNb
= traces
.size();
677 return fTraceNb
== 0;
681 public String
getFailureMessage() {
682 return "Traces Folder not empty (" + fTraceNb
+ ")";
688 * Clear the trace folder (using the UI)
691 * a given workbench bot
693 * the name of the project (needs to exist)
695 public static void clearTracesFolderUI(SWTWorkbenchBot bot
, String projectName
) {
696 SWTBotTreeItem tracesFolder
= selectTracesFolder(bot
, projectName
);
697 tracesFolder
.contextMenu().menu("Clear").click();
698 String CONFIRM_CLEAR_DIALOG_TITLE
= "Confirm Clear";
699 bot
.waitUntil(Conditions
.shellIsActive(CONFIRM_CLEAR_DIALOG_TITLE
));
701 SWTBotShell shell
= bot
.shell(CONFIRM_CLEAR_DIALOG_TITLE
);
702 shell
.bot().button("Yes").click();
703 bot
.waitUntil(Conditions
.shellCloses(shell
));
704 bot
.waitWhile(ConditionHelpers
.treeItemHasChildren(tracesFolder
));
708 * Clear the experiment folder
711 * a given workbench bot
713 * the name of the project (needs to exist)
715 public static void clearExperimentFolder(SWTWorkbenchBot bot
, String projectName
) {
716 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
717 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, false);
718 TmfExperimentFolder expFolder
= tmfProject
.getExperimentsFolder();
719 expFolder
.getExperiments().forEach(experiment
-> {
720 IResource resource
= experiment
.getResource();
722 // Close the experiment if open
723 experiment
.closeEditors();
725 IPath path
= resource
.getLocation();
727 // Delete supplementary files
728 experiment
.deleteSupplementaryFolder();
730 // Finally, delete the experiment
731 resource
.delete(true, null);
732 } catch (CoreException e
) {
733 fail(e
.getMessage());
737 bot
.waitUntil(new DefaultCondition() {
738 private int fExperimentNb
= 0;
741 public boolean test() throws Exception
{
742 List
<TmfExperimentElement
> experiments
= expFolder
.getExperiments();
743 fExperimentNb
= experiments
.size();
744 return fExperimentNb
== 0;
748 public String
getFailureMessage() {
749 return "Experiment Folder not empty (" + fExperimentNb
+ ")";
755 * Select the project in Project Explorer
758 * a given workbench bot
760 * the name of the project (it needs to exist or else it would time out)
761 * @return a {@link SWTBotTreeItem} of the project
763 public static SWTBotTreeItem
selectProject(SWTWorkbenchBot bot
, String projectName
) {
764 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
765 projectExplorerBot
.show();
766 // FIXME: Bug 496519. Sometimes, the tree becomes disabled for a certain
767 // amount of time. This can happen during a long running operation
768 // (BusyIndicator.showWhile) which brings up the modal dialog "operation
769 // in progress" and this disables all shells
770 projectExplorerBot
.bot().waitUntil(Conditions
.widgetIsEnabled(projectExplorerBot
.bot().tree()));
771 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
782 public static void openView(final String id
) {
783 final PartInitException res
[] = new PartInitException
[1];
784 UIThreadRunnable
.syncExec(new VoidResult() {
788 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
789 } catch (PartInitException e
) {
794 if (res
[0] != null) {
795 fail(res
[0].getMessage());
804 * the {@link SWTBotTable} table
806 public static void maximizeTable(SWTBotTable tableBot
) {
807 final AtomicBoolean controlResized
= new AtomicBoolean();
808 UIThreadRunnable
.syncExec(new VoidResult() {
811 tableBot
.widget
.addControlListener(new ControlAdapter() {
813 public void controlResized(ControlEvent e
) {
814 tableBot
.widget
.removeControlListener(this);
815 controlResized
.set(true);
821 tableBot
.pressShortcut(KeyStroke
.getInstance(IKeyLookup
.CTRL_NAME
+ "+"), KeyStroke
.getInstance("M"));
822 } catch (ParseException e
) {
825 new SWTBot().waitUntil(new DefaultCondition() {
827 public boolean test() throws Exception
{
828 return controlResized
.get();
832 public String
getFailureMessage() {
833 return "Control was not resized";
839 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
845 * the row of the table to look up
847 * the column of the table to look up
848 * @return the bounds in display relative coordinates
850 public static Rectangle
getCellBounds(final Table table
, final int row
, final int col
) {
851 return UIThreadRunnable
.syncExec(new Result
<Rectangle
>() {
853 public Rectangle
run() {
854 TableItem item
= table
.getItem(row
);
855 Rectangle bounds
= item
.getBounds(col
);
856 Point p
= table
.toDisplay(bounds
.x
, bounds
.y
);
857 Rectangle rect
= new Rectangle(p
.x
, p
.y
, bounds
.width
, bounds
.height
);
864 * Get the tree item from a tree at the specified location
869 * the tree to find the tree item in
871 * the path to the tree item, in the form of node names (from
873 * @return the tree item
875 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTree tree
, String
... nodeNames
) {
876 if (nodeNames
.length
== 0) {
880 bot
.waitUntil(ConditionHelpers
.IsTreeNodeAvailable(nodeNames
[0], tree
));
881 SWTBotTreeItem currentNode
= tree
.getTreeItem(nodeNames
[0]);
882 return getTreeItem(bot
, currentNode
, Arrays
.copyOfRange(nodeNames
, 1, nodeNames
.length
));
886 * Get the tree item from a parent tree item at the specified location
891 * the treeItem to find the tree item under
893 * the path to the tree item, in the form of node names (from
895 * @return the tree item
897 public static SWTBotTreeItem
getTreeItem(SWTBot bot
, SWTBotTreeItem treeItem
, String
... nodeNames
) {
898 if (nodeNames
.length
== 0) {
902 SWTBotTreeItem currentNode
= treeItem
;
903 for (int i
= 0; i
< nodeNames
.length
; i
++) {
904 bot
.waitUntil(ConditionHelpers
.treeItemHasChildren(treeItem
));
905 currentNode
.expand();
907 String nodeName
= nodeNames
[i
];
909 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
910 } catch (TimeoutException e
) {
911 //FIXME: Sometimes in a JFace TreeViewer, it expands to nothing. Need to find out why.
912 currentNode
.collapse();
913 currentNode
.expand();
914 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
917 SWTBotTreeItem newNode
= currentNode
.getNode(nodeName
);
918 currentNode
= newNode
;
925 * Press the keyboard shortcut that goes to the top of a tree widget. The
926 * key combination can differ on different platforms.
929 * the keyboard to use
931 public static void pressShortcutGoToTreeTop(Keyboard keyboard
) {
932 if (SWTUtils
.isMac()) {
933 keyboard
.pressShortcut(Keystrokes
.ALT
, Keystrokes
.UP
);
935 keyboard
.pressShortcut(Keystrokes
.HOME
);
940 * Get the active events editor. Note that this will wait until such editor
943 * @param workbenchBot
944 * a given workbench bot
945 * @return the active events editor
947 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
) {
948 ConditionHelpers
.ActiveEventsEditor condition
= new ConditionHelpers
.ActiveEventsEditor(workbenchBot
, null);
949 workbenchBot
.waitUntil(condition
);
950 return condition
.getActiveEditor();
954 * Get the active events editor. Note that this will wait until such editor
957 * @param workbenchBot
958 * a given workbench bot
960 * the desired editor title. If null, any active events editor
961 * will be considered valid.
962 * @return the active events editor
964 public static SWTBotEditor
activeEventsEditor(final SWTWorkbenchBot workbenchBot
, String editorTitle
) {
965 ConditionHelpers
.ActiveEventsEditor condition
= new ConditionHelpers
.ActiveEventsEditor(workbenchBot
, editorTitle
);
966 workbenchBot
.waitUntil(condition
);
967 return condition
.getActiveEditor();
971 * Open the preferences dialog and return the corresponding shell.
974 * a given workbench bot
975 * @return the preferences shell
977 public static SWTBotShell
openPreferences(SWTBot bot
) {
978 if (SWTUtils
.isMac()) {
979 // On Mac, the Preferences menu item is under the application name.
980 // For some reason, we can't access the application menu anymore so
981 // we use the keyboard shortcut.
983 bot
.activeShell().pressShortcut(KeyStroke
.getInstance(IKeyLookup
.COMMAND_NAME
+ "+"), KeyStroke
.getInstance(","));
984 } catch (ParseException e
) {
988 bot
.menu(WINDOW_MENU
).menu(PREFERENCES_MENU_ITEM
).click();
991 bot
.waitUntil(Conditions
.shellIsActive(PREFERENCES_MENU_ITEM
));
992 return bot
.activeShell();