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
.editors
.TmfEventsEditor
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.swtbot
.tests
.conditions
.ConditionHelpers
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TracingPerspectiveFactory
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
34 import org
.eclipse
.swtbot
.eclipse
.finder
.matchers
.WidgetMatcherFactory
;
35 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
36 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
37 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
38 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
39 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
40 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
41 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
42 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
43 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
44 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
45 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
46 import org
.eclipse
.ui
.IEditorPart
;
47 import org
.eclipse
.ui
.IEditorReference
;
48 import org
.eclipse
.ui
.IPageLayout
;
49 import org
.eclipse
.ui
.PlatformUI
;
50 import org
.eclipse
.ui
.WorkbenchException
;
51 import org
.hamcrest
.Matcher
;
54 * SWTBot Helper functions
56 * @author Matthew Khouzam
58 public abstract class SWTBotUtil
{
59 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
62 * Waits for all Eclipse jobs to finish
64 public static void waitForJobs() {
65 while (!Job
.getJobManager().isIdle()) {
71 * Sleeps current thread for a given time.
73 * @param waitTimeMillis
74 * time in milliseconds to wait
76 public static void delay(final long waitTimeMillis
) {
78 Thread
.sleep(waitTimeMillis
);
79 } catch (final InterruptedException e
) {
85 * Create a tracing project
88 * the name of the tracing project
90 public static void createProject(final String projectName
) {
94 UIThreadRunnable
.syncExec(new VoidResult() {
97 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
98 assertNotNull(project
);
102 SWTBotUtil
.waitForJobs();
106 * Deletes a tracing project
109 * the name of the tracing project
113 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
114 // Wait for any analysis to complete because it might create
115 // supplementary files
116 SWTBotUtil
.waitForJobs();
118 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
119 } catch (CoreException e
) {
122 SWTBotUtil
.waitForJobs();
124 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
125 projectViewBot
.setFocus();
127 SWTBotTree treeBot
= projectViewBot
.bot().tree();
128 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
129 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
132 bot
.shell("Delete Resources").setFocus();
133 final SWTBotCheckBox checkBox
= bot
.checkBox();
134 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
137 final SWTBotButton okButton
= bot
.button("OK");
138 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
141 SWTBotUtil
.waitForJobs();
145 * Focus on the main window
148 * swtbotshells for all the shells
150 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
151 for (SWTBotShell shellBot
: shellBots
) {
152 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
159 * Close a view with a title
162 * the title, like "welcome"
166 public static void closeView(String title
, SWTWorkbenchBot bot
) {
167 final List
<SWTBotView
> openViews
= bot
.views();
168 for (SWTBotView view
: openViews
) {
169 if (view
.getTitle().equalsIgnoreCase(title
)) {
171 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
177 * Switch to the tracing perspective
179 public static void switchToTracingPerspective() {
180 switchToPerspective(TRACING_PERSPECTIVE_ID
);
184 * Switch to a given perspective
187 * the perspective id (like
188 * "org.eclipse.linuxtools.tmf.ui.perspective"
190 public static void switchToPerspective(final String id
) {
191 UIThreadRunnable
.syncExec(new VoidResult() {
195 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
196 } catch (WorkbenchException e
) {
197 fail(e
.getMessage());
204 * If the test is running in the UI thread then fail
206 public static void failIfUIThread() {
207 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
208 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"
209 + " that useUIThread is set to false in the pom.xml");
215 * Open a trace, this does not perform any validation though
220 * the path of the trace file (absolute or relative)
222 * the trace canonical string (eg:
223 * org.eclipse.linuxtools.btf.trace)
225 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
226 final Exception exception
[] = new Exception
[1];
228 UIThreadRunnable
.syncExec(new VoidResult() {
232 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
233 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
234 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
235 } catch (CoreException e
) {
240 if (exception
[0] != null) {
241 fail(exception
[0].getMessage());
249 * Opens an editor and sets focus to the editor
255 * @return the corresponding SWTBotEditor
257 public static SWTBotEditor
openEditor(SWTWorkbenchBot bot
, String editorName
) {
258 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
259 final SWTBotEditor editorBot
= bot
.editor(matcher
);
260 IEditorPart iep
= editorBot
.getReference().getEditor(true);
261 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
263 UIThreadRunnable
.syncExec(new VoidResult() {
270 SWTBotUtil
.waitForJobs();
271 SWTBotUtil
.delay(1000);
272 assertNotNull(tmfEd
);