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
.TmfProjectRegistry
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.swtbot
.tests
.conditions
.ConditionHelpers
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TracingPerspectiveFactory
;
29 import org
.eclipse
.swt
.widgets
.Display
;
30 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
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
.ui
.IPageLayout
;
42 import org
.eclipse
.ui
.PlatformUI
;
43 import org
.eclipse
.ui
.WorkbenchException
;
46 * SWTBot Helper functions
48 * @author Matthew Khouzam
50 public abstract class SWTBotUtil
{
51 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
54 * Waits for all Eclipse jobs to finish
56 public static void waitForJobs() {
57 while (!Job
.getJobManager().isIdle()) {
63 * Sleeps current thread for a given time.
65 * @param waitTimeMillis
66 * time in milliseconds to wait
68 public static void delay(final long waitTimeMillis
) {
70 Thread
.sleep(waitTimeMillis
);
71 } catch (final InterruptedException e
) {
77 * Create a tracing project
80 * the name of the tracing project
82 public static void createProject(final String projectName
) {
86 UIThreadRunnable
.syncExec(new VoidResult() {
89 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
90 assertNotNull(project
);
94 SWTBotUtil
.waitForJobs();
98 * Deletes a tracing project
101 * the name of the tracing project
105 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
106 // Wait for any analysis to complete because it might create supplementary files
107 SWTBotUtil
.waitForJobs();
109 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
110 } catch (CoreException e
) {
113 SWTBotUtil
.waitForJobs();
115 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
116 projectViewBot
.setFocus();
118 SWTBotTree treeBot
= bot
.tree();
119 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
120 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
123 bot
.shell("Delete Resources").setFocus();
124 final SWTBotCheckBox checkBox
= bot
.checkBox();
125 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
128 final SWTBotButton okButton
= bot
.button("OK");
129 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
132 SWTBotUtil
.waitForJobs();
136 * Focus on the main window
139 * swtbotshells for all the shells
141 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
142 for (SWTBotShell shellBot
: shellBots
) {
143 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
150 * Close a view with a title
153 * the title, like "welcome"
157 public static void closeView(String title
, SWTWorkbenchBot bot
) {
158 final List
<SWTBotView
> openViews
= bot
.views();
159 for (SWTBotView view
: openViews
) {
160 if (view
.getTitle().equalsIgnoreCase(title
)) {
162 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
168 * Switch to the tracing perspective
170 public static void switchToTracingPerspective() {
171 switchToPerspective(TRACING_PERSPECTIVE_ID
);
175 * Switch to a given perspective
178 * the perspective id (like
179 * "org.eclipse.linuxtools.tmf.ui.perspective"
181 public static void switchToPerspective(final String id
) {
182 UIThreadRunnable
.syncExec(new VoidResult() {
186 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
187 } catch (WorkbenchException e
) {
188 fail(e
.getMessage());
195 * If the test is running in the UI thread then fail
197 public static void failIfUIThread() {
198 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
199 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"
200 + " that useUIThread is set to false in the pom.xml");