1 /*******************************************************************************
2 * Copyright (c) 2014 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
.linuxtools
.tmf
.ui
.swtbot
.tests
;
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
.NullProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.jobs
.Job
;
26 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.swtbot
.tests
.conditions
.ConditionHelpers
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TracingPerspectiveFactory
;
31 import org
.eclipse
.swt
.widgets
.Display
;
32 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
33 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
34 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
35 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
36 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
37 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
38 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
39 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
40 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
41 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
42 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
43 import org
.eclipse
.ui
.IPageLayout
;
44 import org
.eclipse
.ui
.PlatformUI
;
45 import org
.eclipse
.ui
.WorkbenchException
;
48 * SWTBot Helper functions
50 * @author Matthew Khouzam
52 public abstract class SWTBotUtil
{
53 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
56 * Waits for all Eclipse jobs to finish
58 public static void waitForJobs() {
59 while (!Job
.getJobManager().isIdle()) {
65 * Sleeps current thread for a given time.
67 * @param waitTimeMillis
68 * time in milliseconds to wait
70 public static void delay(final long waitTimeMillis
) {
72 Thread
.sleep(waitTimeMillis
);
73 } catch (final InterruptedException e
) {
79 * Create a tracing project
82 * the name of the tracing project
84 public static void createProject(final String projectName
) {
88 UIThreadRunnable
.syncExec(new VoidResult() {
91 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
92 assertNotNull(project
);
96 SWTBotUtil
.waitForJobs();
100 * Deletes a tracing project
103 * the name of the tracing project
107 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
108 // Wait for any analysis to complete because it might create
109 // supplementary files
110 SWTBotUtil
.waitForJobs();
112 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
113 } catch (CoreException e
) {
116 SWTBotUtil
.waitForJobs();
118 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
119 projectViewBot
.setFocus();
121 SWTBotTree treeBot
= bot
.tree();
122 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
123 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
126 bot
.shell("Delete Resources").setFocus();
127 final SWTBotCheckBox checkBox
= bot
.checkBox();
128 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
131 final SWTBotButton okButton
= bot
.button("OK");
132 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
135 SWTBotUtil
.waitForJobs();
139 * Focus on the main window
142 * swtbotshells for all the shells
144 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
145 for (SWTBotShell shellBot
: shellBots
) {
146 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
153 * Close a view with a title
156 * the title, like "welcome"
160 public static void closeView(String title
, SWTWorkbenchBot bot
) {
161 final List
<SWTBotView
> openViews
= bot
.views();
162 for (SWTBotView view
: openViews
) {
163 if (view
.getTitle().equalsIgnoreCase(title
)) {
165 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
171 * Switch to the tracing perspective
173 public static void switchToTracingPerspective() {
174 switchToPerspective(TRACING_PERSPECTIVE_ID
);
178 * Switch to a given perspective
181 * the perspective id (like
182 * "org.eclipse.linuxtools.tmf.ui.perspective"
184 public static void switchToPerspective(final String id
) {
185 UIThreadRunnable
.syncExec(new VoidResult() {
189 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
190 } catch (WorkbenchException e
) {
191 fail(e
.getMessage());
198 * If the test is running in the UI thread then fail
200 public static void failIfUIThread() {
201 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
202 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"
203 + " that useUIThread is set to false in the pom.xml");
209 * Open a trace, this does not perform any validation though
214 * the path of the trace file (absolute or relative)
216 * the trace canonical string (eg:
217 * org.eclipse.linuxtools.btf.trace)
219 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
220 final Exception exception
[] = new Exception
[1];
222 UIThreadRunnable
.syncExec(new VoidResult() {
226 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
227 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
228 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
229 } catch (CoreException e
) {
234 if (exception
[0] != null) {
235 fail(exception
[0].getMessage());