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
.jface
.bindings
.keys
.IKeyLookup
;
28 import org
.eclipse
.jface
.bindings
.keys
.KeyStroke
;
29 import org
.eclipse
.jface
.bindings
.keys
.ParseException
;
30 import org
.eclipse
.swt
.graphics
.Point
;
31 import org
.eclipse
.swt
.graphics
.Rectangle
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.eclipse
.swt
.widgets
.Table
;
34 import org
.eclipse
.swt
.widgets
.TableItem
;
35 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
36 import org
.eclipse
.swtbot
.eclipse
.finder
.matchers
.WidgetMatcherFactory
;
37 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
38 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
39 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
40 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
41 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
42 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
43 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotButton
;
44 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCheckBox
;
45 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotMenu
;
46 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
47 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
48 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
49 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfOpenTraceHelper
;
52 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
53 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
54 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TracingPerspectiveFactory
;
56 import org
.eclipse
.ui
.IEditorPart
;
57 import org
.eclipse
.ui
.IEditorReference
;
58 import org
.eclipse
.ui
.IPageLayout
;
59 import org
.eclipse
.ui
.PartInitException
;
60 import org
.eclipse
.ui
.PlatformUI
;
61 import org
.eclipse
.ui
.WorkbenchException
;
62 import org
.hamcrest
.Matcher
;
65 * SWTBot Helper functions
67 * @author Matthew Khouzam
69 public final class SWTBotUtils
{
71 private SWTBotUtils() {
74 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
77 * Waits for all Eclipse jobs to finish
79 public static void waitForJobs() {
80 while (!Job
.getJobManager().isIdle()) {
86 * Sleeps current thread for a given time.
88 * @param waitTimeMillis
89 * time in milliseconds to wait
91 public static void delay(final long waitTimeMillis
) {
93 Thread
.sleep(waitTimeMillis
);
94 } catch (final InterruptedException e
) {
100 * Create a tracing project
103 * the name of the tracing project
105 public static void createProject(final String projectName
) {
109 UIThreadRunnable
.syncExec(new VoidResult() {
112 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
113 assertNotNull(project
);
117 SWTBotUtils
.waitForJobs();
121 * Deletes a tracing project
124 * the name of the tracing project
128 public static void deleteProject(String projectName
, SWTWorkbenchBot bot
) {
129 // Wait for any analysis to complete because it might create
130 // supplementary files
131 SWTBotUtils
.waitForJobs();
133 ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
).refreshLocal(IResource
.DEPTH_INFINITE
, null);
134 } catch (CoreException e
) {
137 SWTBotUtils
.waitForJobs();
139 final SWTBotView projectViewBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
140 projectViewBot
.setFocus();
142 SWTBotTree treeBot
= projectViewBot
.bot().tree();
143 SWTBotTreeItem treeItem
= treeBot
.getTreeItem(projectName
);
144 SWTBotMenu contextMenu
= treeItem
.contextMenu("Delete");
147 bot
.shell("Delete Resources").setFocus();
148 final SWTBotCheckBox checkBox
= bot
.checkBox();
149 bot
.waitUntil(Conditions
.widgetIsEnabled(checkBox
));
152 final SWTBotButton okButton
= bot
.button("OK");
153 bot
.waitUntil(Conditions
.widgetIsEnabled(okButton
));
156 SWTBotUtils
.waitForJobs();
160 * Focus on the main window
163 * swtbotshells for all the shells
165 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
166 for (SWTBotShell shellBot
: shellBots
) {
167 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
174 * Close a view with a title
177 * the title, like "welcome"
181 public static void closeView(String title
, SWTWorkbenchBot bot
) {
182 final List
<SWTBotView
> openViews
= bot
.views();
183 for (SWTBotView view
: openViews
) {
184 if (view
.getTitle().equalsIgnoreCase(title
)) {
186 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
192 * Switch to the tracing perspective
194 public static void switchToTracingPerspective() {
195 switchToPerspective(TRACING_PERSPECTIVE_ID
);
199 * Switch to a given perspective
202 * the perspective id (like
203 * "org.eclipse.linuxtools.tmf.ui.perspective"
205 public static void switchToPerspective(final String id
) {
206 UIThreadRunnable
.syncExec(new VoidResult() {
210 PlatformUI
.getWorkbench().showPerspective(id
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
211 } catch (WorkbenchException e
) {
212 fail(e
.getMessage());
219 * If the test is running in the UI thread then fail
221 public static void failIfUIThread() {
222 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
223 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"
224 + " that useUIThread is set to false in the pom.xml");
230 * Open a trace, this does not perform any validation though
235 * the path of the trace file (absolute or relative)
237 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
239 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
) {
240 openTrace(projectName
, tracePath
, traceType
, true);
244 * Open a trace, this does not perform any validation though
249 * the path of the trace file (absolute or relative)
251 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
253 * delay and wait for jobs
255 public static void openTrace(final String projectName
, final String tracePath
, final String traceType
, boolean delay
) {
256 final Exception exception
[] = new Exception
[1];
258 UIThreadRunnable
.syncExec(new VoidResult() {
262 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(projectName
);
263 TmfTraceFolder destinationFolder
= TmfProjectRegistry
.getProject(project
, true).getTracesFolder();
264 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, tracePath
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType
);
265 } catch (CoreException e
) {
270 if (exception
[0] != null) {
271 fail(exception
[0].getMessage());
281 * Finds an editor and sets focus to the editor
287 * @return the corresponding SWTBotEditor
289 public static SWTBotEditor
activateEditor(SWTWorkbenchBot bot
, String editorName
) {
290 Matcher
<IEditorReference
> matcher
= WidgetMatcherFactory
.withPartName(editorName
);
291 final SWTBotEditor editorBot
= bot
.editor(matcher
);
292 IEditorPart iep
= editorBot
.getReference().getEditor(true);
293 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
295 UIThreadRunnable
.syncExec(new VoidResult() {
302 SWTBotUtils
.waitForJobs();
303 SWTBotUtils
.delay(1000);
304 assertNotNull(tmfEd
);
309 * Opens a trace in an editor and get the TmfEventsEditor
314 * the name of the project that contains the trace
316 * the trace element path (relative to Traces folder)
317 * @return TmfEventsEditor the opened editor
319 public static TmfEventsEditor
openEditor(SWTWorkbenchBot bot
, String projectName
, IPath elementPath
) {
320 final SWTBotView projectExplorerBot
= bot
.viewById(IPageLayout
.ID_PROJECT_EXPLORER
);
321 projectExplorerBot
.setFocus();
323 final SWTBotTree tree
= bot
.tree();
324 final SWTBotTreeItem treeItem
= tree
.getTreeItem(projectName
);
327 String nodeName
= getFullNodeName(treeItem
, TmfTracesFolder
.TRACES_FOLDER_NAME
);
328 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, treeItem
));
329 SWTBotTreeItem tracesNode
= treeItem
.getNode(nodeName
);
332 SWTBotTreeItem currentNode
= tracesNode
;
333 for (String segment
: elementPath
.segments()) {
334 String fullNodeName
= getFullNodeName(currentNode
, segment
);
335 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(fullNodeName
, currentNode
));
336 SWTBotTreeItem newNode
= currentNode
.getNode(fullNodeName
);
338 newNode
.doubleClick();
339 currentNode
= newNode
;
342 SWTBotUtils
.delay(1000);
343 SWTBotUtils
.waitForJobs();
344 final String expectedTitle
= elementPath
.toString();
346 final IEditorPart iep
[] = new IEditorPart
[1];
347 UIThreadRunnable
.syncExec(new VoidResult() {
350 IEditorReference
[] ieds
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
353 for (IEditorReference ied
: ieds
) {
354 if (ied
.getTitle().equals(expectedTitle
)) {
355 iep
[0] = ied
.getEditor(true);
361 assertNotNull(iep
[0]);
362 return (TmfEventsEditor
) iep
[0];
365 private static String
getFullNodeName(final SWTBotTreeItem treeItem
, String prefix
) {
366 List
<String
> nodes
= treeItem
.getNodes();
367 String nodeName
= "";
368 for (String node
: nodes
) {
369 if (node
.startsWith(prefix
)) {
377 * Select the traces folder
380 * a given workbench bot
382 * the name of the project (it needs to exist or else it would
384 * @return a {@link SWTBotTreeItem} of the "Traces" directory
386 public static SWTBotTreeItem
selectTracesFolder(SWTWorkbenchBot bot
, String projectName
) {
387 SWTBotView projectExplorerBot
= bot
.viewByTitle("Project Explorer");
388 projectExplorerBot
.show();
389 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(projectName
);
392 SWTBotTreeItem treeNode
= null;
393 for (String node
: treeItem
.getNodes()) {
394 if (node
.matches("Traces\\s\\[(\\d)*\\]")) {
395 treeNode
= treeItem
.getNode(node
);
399 assertNotNull(treeNode
);
409 public static void openView(final String id
) {
410 final PartInitException res
[] = new PartInitException
[1];
411 UIThreadRunnable
.syncExec(new VoidResult() {
415 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
416 } catch (PartInitException e
) {
421 if (res
[0] != null) {
422 fail(res
[0].getMessage());
431 * the {@link SWTBotTable} table
433 public static void maximizeTable(SWTBotTable tableBot
) {
435 tableBot
.pressShortcut(KeyStroke
.getInstance(IKeyLookup
.CTRL_NAME
+ "+"), KeyStroke
.getInstance("M"));
436 } catch (ParseException e
) {
442 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
448 * the row of the table to look up
450 * the column of the table to look up
451 * @return the bounds in display relative coordinates
453 public static Rectangle
getCellBounds(final Table table
, final int row
, final int col
) {
454 return UIThreadRunnable
.syncExec(new Result
<Rectangle
>() {
456 public Rectangle
run() {
457 TableItem item
= table
.getItem(row
);
458 Rectangle bounds
= item
.getBounds(col
);
459 Point p
= table
.toDisplay(bounds
.x
, bounds
.y
);
460 Rectangle rect
= new Rectangle(p
.x
, p
.y
, bounds
.width
, bounds
.height
);