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
.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
.PlatformUI
;
51 import org
.eclipse
.ui
.WorkbenchException
;
52 import org
.hamcrest
.Matcher
;
56 * SWTBot Helper functions
58 * @author Matthew Khouzam
60 public final class SWTBotUtils
{
62 private SWTBotUtils() {}
64 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
67 * Waits for all Eclipse jobs to finish
69 public static void waitForJobs() {
70 while (!Job
.getJobManager().isIdle()) {
76 * Sleeps current thread for a given time.
78 * @param waitTimeMillis
79 * time in milliseconds to wait
81 public static void delay(final long waitTimeMillis
) {
83 Thread
.sleep(waitTimeMillis
);
84 } catch (final InterruptedException e
) {
90 * Create a tracing project
93 * the name of the tracing project
95 public static void createProject(final String projectName
) {
99 UIThreadRunnable
.syncExec(new VoidResult() {
102 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
103 assertNotNull(project
);
107 SWTBotUtils
.waitForJobs();
111 * Deletes a tracing project
114 * the name of the tracing project
118 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
119 // Wait for any analysis to complete because it might create
120 // supplementary files
121 SWTBotUtils
.waitForJobs();
123 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
124 } catch (CoreException e
) {
127 SWTBotUtils
.waitForJobs();
129 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
130 projectViewBot
.setFocus();
132 SWTBotTree treeBot
= projectViewBot
.bot().tree();
133 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
134 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
137 bot
.shell("Delete Resources").setFocus();
138 final SWTBotCheckBox checkBox
= bot
.checkBox();
139 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
142 final SWTBotButton okButton
= bot
.button("OK");
143 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
146 SWTBotUtils
.waitForJobs();
150 * Focus on the main window
153 * swtbotshells for all the shells
155 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
156 for (SWTBotShell shellBot
: shellBots
) {
157 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
164 * Close a view with a title
167 * the title, like "welcome"
171 public static void closeView(String title
, SWTWorkbenchBot bot
) {
172 final List
<SWTBotView
> openViews
= bot
.views();
173 for (SWTBotView view
: openViews
) {
174 if (view
.getTitle().equalsIgnoreCase(title
)) {
176 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
182 * Switch to the tracing perspective
184 public static void switchToTracingPerspective() {
185 switchToPerspective(TRACING_PERSPECTIVE_ID
);
189 * Switch to a given perspective
192 * the perspective id (like
193 * "org.eclipse.linuxtools.tmf.ui.perspective"
195 public static void switchToPerspective(final String id
) {
196 UIThreadRunnable
.syncExec(new VoidResult() {
200 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
201 } catch (WorkbenchException e
) {
202 fail(e
.getMessage());
209 * If the test is running in the UI thread then fail
211 public static void failIfUIThread() {
212 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
213 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"
214 + " that useUIThread is set to false in the pom.xml");
220 * Open a trace, this does not perform any validation though
225 * the path of the trace file (absolute or relative)
227 * the trace canonical string (eg:
228 * org.eclipse.linuxtools.btf.trace)
230 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
231 openTrace(projectName
, tracePath
, traceType
, true);
235 * Open a trace, this does not perform any validation though
240 * the path of the trace file (absolute or relative)
242 * the trace canonical string (eg:
243 * org.eclipse.linuxtools.btf.trace)
245 * delay and wait for jobs
247 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
248 final Exception exception
[] = new Exception
[1];
250 UIThreadRunnable
.syncExec(new VoidResult() {
254 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
255 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
256 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
257 } catch (CoreException e
) {
262 if (exception
[0] != null) {
263 fail(exception
[0].getMessage());
273 * Finds an editor and sets focus to the editor
279 * @return the corresponding SWTBotEditor
281 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
282 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
283 final SWTBotEditor editorBot
= bot
.editor(matcher
);
284 IEditorPart iep
= editorBot
.getReference().getEditor(true);
285 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
287 UIThreadRunnable
.syncExec(new VoidResult() {
294 SWTBotUtils
.waitForJobs();
295 SWTBotUtils
.delay(1000);
296 assertNotNull(tmfEd
);
301 * Opens a trace in an editor and get the TmfEventsEditor
306 * the name of the project that contains the trace
308 * the trace element path (relative to Traces folder)
309 * @return TmfEventsEditor the opened editor
311 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
312 final SWTBotView projectExplorerBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
313 projectExplorerBot
.setFocus();
315 final SWTBotTree tree
= bot
.tree();
316 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
319 String nodeName
= getFullNodeName(treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
320 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, treeItem
));
321 SWTBotTreeItem tracesNode
= treeItem
.getNode(nodeName
);
324 SWTBotTreeItem currentNode
= tracesNode
;
325 for (String segment
: elementPath
.segments()) {
326 String fullNodeName
= getFullNodeName(currentNode
, segment
);
327 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(fullNodeName
, currentNode
));
328 SWTBotTreeItem newNode
= currentNode
.getNode(fullNodeName
);
330 newNode
.doubleClick();
331 currentNode
= newNode
;
334 SWTBotUtils
.delay(1000);
335 SWTBotUtils
.waitForJobs();
336 final String expectedTitle
= elementPath
.toString();
338 final IEditorPart iep
[] = new IEditorPart
[1];
339 UIThreadRunnable
.syncExec(new VoidResult() {
342 IEditorReference
[] ieds
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
345 for (IEditorReference ied
: ieds
) {
346 if (ied
.getTitle().equals(expectedTitle
)) {
347 iep
[0] = ied
.getEditor(true);
353 assertNotNull(iep
[0]);
354 return (TmfEventsEditor
) iep
[0];
357 private static String
getFullNodeName(final SWTBotTreeItem treeItem
, String prefix
) {
358 List
<String
> nodes
= treeItem
.getNodes();
359 String nodeName
= "";
360 for (String node
: nodes
) {
361 if (node
.startsWith(prefix
)) {