Delete extra "linuxtools" image
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / SWTBotUtil.java
CommitLineData
306e18d0
MK
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
2bdf0193 13package org.eclipse.tracecompass.tmf.ui.swtbot.tests;
306e18d0
MK
14
15import static org.junit.Assert.assertNotNull;
16import static org.junit.Assert.fail;
17
18import java.util.List;
19
20import org.eclipse.core.resources.IProject;
93c91230
MAL
21import org.eclipse.core.resources.IResource;
22import org.eclipse.core.resources.ResourcesPlugin;
23import org.eclipse.core.runtime.CoreException;
306e18d0
MK
24import org.eclipse.core.runtime.NullProgressMonitor;
25import org.eclipse.core.runtime.jobs.Job;
306e18d0
MK
26import org.eclipse.swt.widgets.Display;
27import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
693ec829
BH
28import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory;
29import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
306e18d0
MK
30import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
31import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
32import org.eclipse.swtbot.swt.finder.results.VoidResult;
93c91230
MAL
33import org.eclipse.swtbot.swt.finder.waits.Conditions;
34import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
35import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
36import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
306e18d0 37import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
93c91230
MAL
38import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
39import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
2bdf0193
AM
40import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
41import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
42import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
43import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
44import org.eclipse.tracecompass.tmf.ui.swtbot.tests.conditions.ConditionHelpers;
45import org.eclipse.tracecompass.tmf.ui.views.TracingPerspectiveFactory;
693ec829
BH
46import org.eclipse.ui.IEditorPart;
47import org.eclipse.ui.IEditorReference;
93c91230 48import org.eclipse.ui.IPageLayout;
306e18d0
MK
49import org.eclipse.ui.PlatformUI;
50import org.eclipse.ui.WorkbenchException;
693ec829 51import org.hamcrest.Matcher;
306e18d0
MK
52
53/**
54 * SWTBot Helper functions
55 *
56 * @author Matthew Khouzam
57 */
58public 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
93c91230
MAL
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) {
7777d5f0
MK
114 // Wait for any analysis to complete because it might create
115 // supplementary files
93c91230
MAL
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
693ec829 127 SWTBotTree treeBot = projectViewBot.bot().tree();
93c91230
MAL
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
306e18d0
MK
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() {
664fa59c
MK
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) {
306e18d0
MK
191 UIThreadRunnable.syncExec(new VoidResult() {
192 @Override
193 public void run() {
194 try {
664fa59c 195 PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
306e18d0
MK
196 } catch (WorkbenchException e) {
197 fail(e.getMessage());
198 }
199 }
200 });
201 }
202
306e18d0
MK
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
306e18d0 212 }
7777d5f0
MK
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) {
a3d7df19
BH
226 openTrace(projectName, tracePath, traceType, true);
227 }
228
229 /**
230 * Open a trace, this does not perform any validation though
231 *
232 * @param projectName
233 * The project name
234 * @param tracePath
235 * the path of the trace file (absolute or relative)
236 * @param traceType
237 * the trace canonical string (eg:
238 * org.eclipse.linuxtools.btf.trace)
239 * @param delay
240 * delay and wait for jobs
241 */
242 public static void openTrace(final String projectName, final String tracePath, final String traceType, boolean delay) {
7777d5f0
MK
243 final Exception exception[] = new Exception[1];
244 exception[0] = null;
245 UIThreadRunnable.syncExec(new VoidResult() {
246 @Override
247 public void run() {
248 try {
249 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
250 TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder();
251 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, tracePath, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType);
252 } catch (CoreException e) {
253 exception[0] = e;
254 }
255 }
256 });
257 if (exception[0] != null) {
258 fail(exception[0].getMessage());
259 }
260
a3d7df19
BH
261 if (delay) {
262 delay(1000);
263 waitForJobs();
264 }
7777d5f0 265 }
693ec829
BH
266
267 /**
268 * Opens an editor and sets focus to the editor
269 *
270 * @param bot
271 * the workbench bot
272 * @param editorName
273 * the editor name
274 * @return the corresponding SWTBotEditor
275 */
276 public static SWTBotEditor openEditor(SWTWorkbenchBot bot, String editorName) {
277 Matcher<IEditorReference> matcher = WidgetMatcherFactory.withPartName(editorName);
278 final SWTBotEditor editorBot = bot.editor(matcher);
279 IEditorPart iep = editorBot.getReference().getEditor(true);
280 final TmfEventsEditor tmfEd = (TmfEventsEditor) iep;
281 editorBot.show();
282 UIThreadRunnable.syncExec(new VoidResult() {
283 @Override
284 public void run() {
285 tmfEd.setFocus();
286 }
287 });
288
289 SWTBotUtil.waitForJobs();
290 SWTBotUtil.delay(1000);
291 assertNotNull(tmfEd);
292 return editorBot;
293 }
306e18d0 294}
This page took 0.057754 seconds and 5 git commands to generate.