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
.runtime
.NullProgressMonitor
;
22 import org
.eclipse
.core
.runtime
.jobs
.Job
;
23 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
24 import org
.eclipse
.linuxtools
.tmf
.ui
.swtbot
.tests
.conditions
.ConditionHelpers
;
25 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TracingPerspectiveFactory
;
26 import org
.eclipse
.swt
.widgets
.Display
;
27 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
28 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
29 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
30 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
31 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
32 import org
.eclipse
.ui
.PlatformUI
;
33 import org
.eclipse
.ui
.WorkbenchException
;
36 * SWTBot Helper functions
38 * @author Matthew Khouzam
40 public abstract class SWTBotUtil
{
41 private static final String TRACING_PERSPECTIVE_ID
= TracingPerspectiveFactory
.ID
;
44 * Waits for all Eclipse jobs to finish
46 public static void waitForJobs() {
47 while (!Job
.getJobManager().isIdle()) {
53 * Sleeps current thread for a given time.
55 * @param waitTimeMillis
56 * time in milliseconds to wait
58 public static void delay(final long waitTimeMillis
) {
60 Thread
.sleep(waitTimeMillis
);
61 } catch (final InterruptedException e
) {
67 * Create a tracing project
70 * the name of the tracing project
72 public static void createProject(final String projectName
) {
76 UIThreadRunnable
.syncExec(new VoidResult() {
79 IProject project
= TmfProjectRegistry
.createProject(projectName
, null, new NullProgressMonitor());
80 assertNotNull(project
);
84 SWTBotUtil
.waitForJobs();
88 * Focus on the main window
91 * swtbotshells for all the shells
93 public static void focusMainWindow(SWTBotShell
[] shellBots
) {
94 for (SWTBotShell shellBot
: shellBots
) {
95 if (shellBot
.getText().toLowerCase().contains("eclipse")) {
102 * Close a view with a title
105 * the title, like "welcome"
109 public static void closeView(String title
, SWTWorkbenchBot bot
) {
110 final List
<SWTBotView
> openViews
= bot
.views();
111 for (SWTBotView view
: openViews
) {
112 if (view
.getTitle().equalsIgnoreCase(title
)) {
114 bot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
120 * Switch to the tracing perspective
122 public static void switchToTracingPerspective() {
123 UIThreadRunnable
.syncExec(new VoidResult() {
127 PlatformUI
.getWorkbench().showPerspective(TRACING_PERSPECTIVE_ID
, PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
128 } catch (WorkbenchException e
) {
129 fail(e
.getMessage());
137 * If the test is running in the UI thread then fail
139 public static void failIfUIThread() {
140 if (Display
.getCurrent() != null && Display
.getCurrent().getThread() == Thread
.currentThread()) {
141 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"
142 + " that useUIThread is set to false in the pom.xml");