Move alltests plugin to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.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.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;
52
53 /**
54 * SWTBot Helper functions
55 *
56 * @author Matthew Khouzam
57 */
58 public abstract class SWTBotUtil {
59 private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID;
60
61 /**
62 * Waits for all Eclipse jobs to finish
63 */
64 public static void waitForJobs() {
65 while (!Job.getJobManager().isIdle()) {
66 delay(100);
67 }
68 }
69
70 /**
71 * Sleeps current thread for a given time.
72 *
73 * @param waitTimeMillis
74 * time in milliseconds to wait
75 */
76 public static void delay(final long waitTimeMillis) {
77 try {
78 Thread.sleep(waitTimeMillis);
79 } catch (final InterruptedException e) {
80 // Ignored
81 }
82 }
83
84 /**
85 * Create a tracing project
86 *
87 * @param projectName
88 * the name of the tracing project
89 */
90 public static void createProject(final String projectName) {
91 /*
92 * Make a new test
93 */
94 UIThreadRunnable.syncExec(new VoidResult() {
95 @Override
96 public void run() {
97 IProject project = TmfProjectRegistry.createProject(projectName, null, new NullProgressMonitor());
98 assertNotNull(project);
99 }
100 });
101
102 SWTBotUtil.waitForJobs();
103 }
104
105 /**
106 * Deletes a tracing project
107 *
108 * @param projectName
109 * the name of the tracing project
110 * @param bot
111 * the workbench bot
112 */
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();
117 try {
118 ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).refreshLocal(IResource.DEPTH_INFINITE, null);
119 } catch (CoreException e) {
120 }
121
122 SWTBotUtil.waitForJobs();
123
124 final SWTBotView projectViewBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
125 projectViewBot.setFocus();
126
127 SWTBotTree treeBot = projectViewBot.bot().tree();
128 SWTBotTreeItem treeItem = treeBot.getTreeItem(projectName);
129 SWTBotMenu contextMenu = treeItem.contextMenu("Delete");
130 contextMenu.click();
131
132 bot.shell("Delete Resources").setFocus();
133 final SWTBotCheckBox checkBox = bot.checkBox();
134 bot.waitUntil(Conditions.widgetIsEnabled(checkBox));
135 checkBox.click();
136
137 final SWTBotButton okButton = bot.button("OK");
138 bot.waitUntil(Conditions.widgetIsEnabled(okButton));
139 okButton.click();
140
141 SWTBotUtil.waitForJobs();
142 }
143
144 /**
145 * Focus on the main window
146 *
147 * @param shellBots
148 * swtbotshells for all the shells
149 */
150 public static void focusMainWindow(SWTBotShell[] shellBots) {
151 for (SWTBotShell shellBot : shellBots) {
152 if (shellBot.getText().toLowerCase().contains("eclipse")) {
153 shellBot.activate();
154 }
155 }
156 }
157
158 /**
159 * Close a view with a title
160 *
161 * @param title
162 * the title, like "welcome"
163 * @param bot
164 * the workbench bot
165 */
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)) {
170 view.close();
171 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
172 }
173 }
174 }
175
176 /**
177 * Switch to the tracing perspective
178 */
179 public static void switchToTracingPerspective() {
180 switchToPerspective(TRACING_PERSPECTIVE_ID);
181 }
182
183 /**
184 * Switch to a given perspective
185 *
186 * @param id
187 * the perspective id (like
188 * "org.eclipse.linuxtools.tmf.ui.perspective"
189 */
190 public static void switchToPerspective(final String id) {
191 UIThreadRunnable.syncExec(new VoidResult() {
192 @Override
193 public void run() {
194 try {
195 PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
196 } catch (WorkbenchException e) {
197 fail(e.getMessage());
198 }
199 }
200 });
201 }
202
203 /**
204 * If the test is running in the UI thread then fail
205 */
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");
210 }
211
212 }
213
214 /**
215 * Open a trace, this does not perform any validation though
216 *
217 * @param projectName
218 * The project name
219 * @param tracePath
220 * the path of the trace file (absolute or relative)
221 * @param traceType
222 * the trace canonical string (eg:
223 * org.eclipse.linuxtools.btf.trace)
224 */
225 public static void openTrace(final String projectName, final String tracePath, final String traceType) {
226 final Exception exception[] = new Exception[1];
227 exception[0] = null;
228 UIThreadRunnable.syncExec(new VoidResult() {
229 @Override
230 public void run() {
231 try {
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) {
236 exception[0] = e;
237 }
238 }
239 });
240 if (exception[0] != null) {
241 fail(exception[0].getMessage());
242 }
243
244 delay(1000);
245 waitForJobs();
246 }
247
248 /**
249 * Opens an editor and sets focus to the editor
250 *
251 * @param bot
252 * the workbench bot
253 * @param editorName
254 * the editor name
255 * @return the corresponding SWTBotEditor
256 */
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;
262 editorBot.show();
263 UIThreadRunnable.syncExec(new VoidResult() {
264 @Override
265 public void run() {
266 tmfEd.setFocus();
267 }
268 });
269
270 SWTBotUtil.waitForJobs();
271 SWTBotUtil.delay(1000);
272 assertNotNull(tmfEd);
273 return editorBot;
274 }
275 }
This page took 0.0368 seconds and 5 git commands to generate.