tmf/lttng: add swtbot helper
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.swtbot.tests / src / org / eclipse / linuxtools / tmf / ui / swtbot / tests / SWTBotUtil.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.swtbot.tests;
14
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.fail;
17
18 import java.util.List;
19
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;
34
35 /**
36 * SWTBot Helper functions
37 *
38 * @author Matthew Khouzam
39 */
40 public abstract class SWTBotUtil {
41 private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID;
42
43 /**
44 * Waits for all Eclipse jobs to finish
45 */
46 public static void waitForJobs() {
47 while (!Job.getJobManager().isIdle()) {
48 delay(100);
49 }
50 }
51
52 /**
53 * Sleeps current thread for a given time.
54 *
55 * @param waitTimeMillis
56 * time in milliseconds to wait
57 */
58 public static void delay(final long waitTimeMillis) {
59 try {
60 Thread.sleep(waitTimeMillis);
61 } catch (final InterruptedException e) {
62 // Ignored
63 }
64 }
65
66 /**
67 * Create a tracing project
68 *
69 * @param projectName
70 * the name of the tracing project
71 */
72 public static void createProject(final String projectName) {
73 /*
74 * Make a new test
75 */
76 UIThreadRunnable.syncExec(new VoidResult() {
77 @Override
78 public void run() {
79 IProject project = TmfProjectRegistry.createProject(projectName, null, new NullProgressMonitor());
80 assertNotNull(project);
81 }
82 });
83
84 SWTBotUtil.waitForJobs();
85 }
86
87 /**
88 * Focus on the main window
89 *
90 * @param shellBots
91 * swtbotshells for all the shells
92 */
93 public static void focusMainWindow(SWTBotShell[] shellBots) {
94 for (SWTBotShell shellBot : shellBots) {
95 if (shellBot.getText().toLowerCase().contains("eclipse")) {
96 shellBot.activate();
97 }
98 }
99 }
100
101 /**
102 * Close a view with a title
103 *
104 * @param title
105 * the title, like "welcome"
106 * @param bot
107 * the workbench bot
108 */
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)) {
113 view.close();
114 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
115 }
116 }
117 }
118
119 /**
120 * Switch to the tracing perspective
121 */
122 public static void switchToTracingPerspective() {
123 UIThreadRunnable.syncExec(new VoidResult() {
124 @Override
125 public void run() {
126 try {
127 PlatformUI.getWorkbench().showPerspective(TRACING_PERSPECTIVE_ID, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
128 } catch (WorkbenchException e) {
129 fail(e.getMessage());
130 }
131 }
132 });
133 }
134
135
136 /**
137 * If the test is running in the UI thread then fail
138 */
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");
143 }
144
145
146 }
147 }
This page took 0.060272 seconds and 6 git commands to generate.