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
.fail
;
18 import java
.util
.List
;
20 import org
.eclipse
.core
.resources
.IProject
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
23 import org
.eclipse
.core
.runtime
.CoreException
;
24 import org
.eclipse
.core
.runtime
.IPath
;
25 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.jobs
.Job
;
27 import org
.eclipse
.swt
.widgets
.Display
;
28 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
29 import org
.eclipse
.swtbot
.eclipse
.finder
.matchers
.WidgetMatcherFactory
;
30 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
31 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
32 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
33 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
34 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
35 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
36 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
37 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
38 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
39 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
40 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
44 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
45 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TracingPerspectiveFactory
;
47 import org
.eclipse
.ui
.IEditorPart
;
48 import org
.eclipse
.ui
.IEditorReference
;
49 import org
.eclipse
.ui
.IPageLayout
;
50 import org
.eclipse
.ui
.PartInitException
;
51 import org
.eclipse
.ui
.PlatformUI
;
52 import org
.eclipse
.ui
.WorkbenchException
;
53 import org
.hamcrest
.Matcher
;
57 * SWTBot Helper functions
59 * @author Matthew Khouzam
61 public final class SWTBotUtils
{
63 private SWTBotUtils() {}
65 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
68 * Waits for all Eclipse jobs to finish
70 public static void waitForJobs() {
71 while (!Job
.getJobManager().isIdle()) {
77 * Sleeps current thread for a given time.
79 * @param waitTimeMillis
80 * time in milliseconds to wait
82 public static void delay(final long waitTimeMillis
) {
84 Thread
.sleep(waitTimeMillis
);
85 } catch (final InterruptedException e
) {
91 * Create a tracing project
94 * the name of the tracing project
96 public static void createProject(final String projectName
) {
100 UIThreadRunnable
.syncExec(new VoidResult() {
103 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
104 assertNotNull(project
);
108 SWTBotUtils
.waitForJobs();
112 * Deletes a tracing project
115 * the name of the tracing project
119 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
120 // Wait for any analysis to complete because it might create
121 // supplementary files
122 SWTBotUtils
.waitForJobs();
124 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
125 } catch (CoreException e
) {
128 SWTBotUtils
.waitForJobs();
130 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
131 projectViewBot
.setFocus();
133 SWTBotTree treeBot
= projectViewBot
.bot().tree();
134 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
135 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
138 bot
.shell("Delete Resources").setFocus();
139 final SWTBotCheckBox checkBox
= bot
.checkBox();
140 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
143 final SWTBotButton okButton
= bot
.button("OK");
144 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
147 SWTBotUtils
.waitForJobs();
151 * Focus on the main window
154 * swtbotshells for all the shells
156 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
157 for (SWTBotShell shellBot
: shellBots
) {
158 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
165 * Close a view with a title
168 * the title, like "welcome"
172 public static void closeView(String title
, SWTWorkbenchBot bot
) {
173 final List
<SWTBotView
> openViews
= bot
.views();
174 for (SWTBotView view
: openViews
) {
175 if (view
.getTitle().equalsIgnoreCase(title
)) {
177 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
183 * Switch to the tracing perspective
185 public static void switchToTracingPerspective() {
186 switchToPerspective(TRACING_PERSPECTIVE_ID
);
190 * Switch to a given perspective
193 * the perspective id (like
194 * "org.eclipse.linuxtools.tmf.ui.perspective"
196 public static void switchToPerspective(final String id
) {
197 UIThreadRunnable
.syncExec(new VoidResult() {
201 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
202 } catch (WorkbenchException e
) {
203 fail(e
.getMessage());
210 * If the test is running in the UI thread then fail
212 public static void failIfUIThread() {
213 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
214 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"
215 + " that useUIThread is set to false in the pom.xml");
221 * Open a trace, this does not perform any validation though
226 * the path of the trace file (absolute or relative)
228 * the trace type id (eg:
229 * org.eclipse.linuxtools.btf.trace)
231 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
232 openTrace(projectName
, tracePath
, traceType
, true);
236 * Open a trace, this does not perform any validation though
241 * the path of the trace file (absolute or relative)
243 * the trace type id (eg:
244 * org.eclipse.linuxtools.btf.trace)
246 * delay and wait for jobs
248 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
249 final Exception exception
[] = new Exception
[1];
251 UIThreadRunnable
.syncExec(new VoidResult() {
255 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
256 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
257 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
258 } catch (CoreException e
) {
263 if (exception
[0] != null) {
264 fail(exception
[0].getMessage());
274 * Finds an editor and sets focus to the editor
280 * @return the corresponding SWTBotEditor
282 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
283 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
284 final SWTBotEditor editorBot
= bot
.editor(matcher
);
285 IEditorPart iep
= editorBot
.getReference().getEditor(true);
286 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
288 UIThreadRunnable
.syncExec(new VoidResult() {
295 SWTBotUtils
.waitForJobs();
296 SWTBotUtils
.delay(1000);
297 assertNotNull(tmfEd
);
302 * Opens a trace in an editor and get the TmfEventsEditor
307 * the name of the project that contains the trace
309 * the trace element path (relative to Traces folder)
310 * @return TmfEventsEditor the opened editor
312 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
313 final SWTBotView projectExplorerBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
314 projectExplorerBot
.setFocus();
316 final SWTBotTree tree
= bot
.tree();
317 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
320 String nodeName
= getFullNodeName(treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
321 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, treeItem
));
322 SWTBotTreeItem tracesNode
= treeItem
.getNode(nodeName
);
325 SWTBotTreeItem currentNode
= tracesNode
;
326 for (String segment
: elementPath
.segments()) {
327 String fullNodeName
= getFullNodeName(currentNode
, segment
);
328 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(fullNodeName
, currentNode
));
329 SWTBotTreeItem newNode
= currentNode
.getNode(fullNodeName
);
331 newNode
.doubleClick();
332 currentNode
= newNode
;
335 SWTBotUtils
.delay(1000);
336 SWTBotUtils
.waitForJobs();
337 final String expectedTitle
= elementPath
.toString();
339 final IEditorPart iep
[] = new IEditorPart
[1];
340 UIThreadRunnable
.syncExec(new VoidResult() {
343 IEditorReference
[] ieds
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
346 for (IEditorReference ied
: ieds
) {
347 if (ied
.getTitle().equals(expectedTitle
)) {
348 iep
[0] = ied
.getEditor(true);
354 assertNotNull(iep
[0]);
355 return (TmfEventsEditor
) iep
[0];
358 private static String
getFullNodeName(final SWTBotTreeItem treeItem
, String prefix
) {
359 List
<String
> nodes
= treeItem
.getNodes();
360 String nodeName
= "";
361 for (String node
: nodes
) {
362 if (node
.startsWith(prefix
)) {
370 * Select the traces folder
373 * a given workbench bot
375 * the name of the project (it needs to exist or else it would time out)
376 * @return a {@link SWTBotTreeItem} of the "Traces" directory
378 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
379 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
380 projectExplorerBot
.show();
381 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
384 SWTBotTreeItem treeNode
= null;
385 for (String node
: treeItem
.getNodes()) {
386 if (node
.matches("Traces\\s\\[(\\d)*\\]")) {
387 treeNode
= treeItem
.getNode(node
);
391 assertNotNull(treeNode
);
401 public static void openView(final String id
) {
402 final PartInitException res
[] = new PartInitException
[1];
403 UIThreadRunnable
.syncExec(new VoidResult() {
407 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
408 } catch (PartInitException e
) {
413 if (res
[0] != null) {
414 fail(res
[0].getMessage());