Fix references to linuxtools in comments, examples and unused code
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / SWTBotUtils.java
CommitLineData
306e18d0 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2014, 2015 Ericsson
306e18d0
MK
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
fa24d78b 13package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared;
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;
b4290931 24import org.eclipse.core.runtime.IPath;
306e18d0
MK
25import org.eclipse.core.runtime.NullProgressMonitor;
26import org.eclipse.core.runtime.jobs.Job;
306e18d0
MK
27import org.eclipse.swt.widgets.Display;
28import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
693ec829
BH
29import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory;
30import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
306e18d0
MK
31import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
32import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
33import org.eclipse.swtbot.swt.finder.results.VoidResult;
93c91230
MAL
34import org.eclipse.swtbot.swt.finder.waits.Conditions;
35import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
36import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
37import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
306e18d0 38import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
93c91230
MAL
39import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
40import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
2bdf0193
AM
41import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
42import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
43import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
44import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
b4290931 45import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
2bdf0193 46import org.eclipse.tracecompass.tmf.ui.views.TracingPerspectiveFactory;
693ec829
BH
47import org.eclipse.ui.IEditorPart;
48import org.eclipse.ui.IEditorReference;
93c91230 49import org.eclipse.ui.IPageLayout;
2470d687 50import org.eclipse.ui.PartInitException;
306e18d0
MK
51import org.eclipse.ui.PlatformUI;
52import org.eclipse.ui.WorkbenchException;
693ec829 53import org.hamcrest.Matcher;
306e18d0 54
b4290931 55
306e18d0
MK
56/**
57 * SWTBot Helper functions
58 *
59 * @author Matthew Khouzam
60 */
fa24d78b
AM
61public final class SWTBotUtils {
62
63 private SWTBotUtils() {}
64
306e18d0
MK
65 private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID;
66
67 /**
68 * Waits for all Eclipse jobs to finish
69 */
70 public static void waitForJobs() {
71 while (!Job.getJobManager().isIdle()) {
72 delay(100);
73 }
74 }
75
76 /**
77 * Sleeps current thread for a given time.
78 *
79 * @param waitTimeMillis
80 * time in milliseconds to wait
81 */
82 public static void delay(final long waitTimeMillis) {
83 try {
84 Thread.sleep(waitTimeMillis);
85 } catch (final InterruptedException e) {
86 // Ignored
87 }
88 }
89
90 /**
91 * Create a tracing project
92 *
93 * @param projectName
94 * the name of the tracing project
95 */
96 public static void createProject(final String projectName) {
97 /*
98 * Make a new test
99 */
100 UIThreadRunnable.syncExec(new VoidResult() {
101 @Override
102 public void run() {
103 IProject project = TmfProjectRegistry.createProject(projectName, null, new NullProgressMonitor());
104 assertNotNull(project);
105 }
106 });
107
fa24d78b 108 SWTBotUtils.waitForJobs();
306e18d0
MK
109 }
110
93c91230
MAL
111 /**
112 * Deletes a tracing project
113 *
114 * @param projectName
115 * the name of the tracing project
116 * @param bot
117 * the workbench bot
118 */
119 public static void deleteProject(String projectName, SWTWorkbenchBot bot) {
7777d5f0
MK
120 // Wait for any analysis to complete because it might create
121 // supplementary files
fa24d78b 122 SWTBotUtils.waitForJobs();
93c91230
MAL
123 try {
124 ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).refreshLocal(IResource.DEPTH_INFINITE, null);
125 } catch (CoreException e) {
126 }
127
fa24d78b 128 SWTBotUtils.waitForJobs();
93c91230
MAL
129
130 final SWTBotView projectViewBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
131 projectViewBot.setFocus();
132
693ec829 133 SWTBotTree treeBot = projectViewBot.bot().tree();
93c91230
MAL
134 SWTBotTreeItem treeItem = treeBot.getTreeItem(projectName);
135 SWTBotMenu contextMenu = treeItem.contextMenu("Delete");
136 contextMenu.click();
137
138 bot.shell("Delete Resources").setFocus();
139 final SWTBotCheckBox checkBox = bot.checkBox();
140 bot.waitUntil(Conditions.widgetIsEnabled(checkBox));
141 checkBox.click();
142
143 final SWTBotButton okButton = bot.button("OK");
144 bot.waitUntil(Conditions.widgetIsEnabled(okButton));
145 okButton.click();
146
fa24d78b 147 SWTBotUtils.waitForJobs();
93c91230
MAL
148 }
149
306e18d0
MK
150 /**
151 * Focus on the main window
152 *
153 * @param shellBots
154 * swtbotshells for all the shells
155 */
156 public static void focusMainWindow(SWTBotShell[] shellBots) {
157 for (SWTBotShell shellBot : shellBots) {
158 if (shellBot.getText().toLowerCase().contains("eclipse")) {
159 shellBot.activate();
160 }
161 }
162 }
163
164 /**
165 * Close a view with a title
166 *
167 * @param title
168 * the title, like "welcome"
169 * @param bot
170 * the workbench bot
171 */
172 public static void closeView(String title, SWTWorkbenchBot bot) {
173 final List<SWTBotView> openViews = bot.views();
174 for (SWTBotView view : openViews) {
175 if (view.getTitle().equalsIgnoreCase(title)) {
176 view.close();
177 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
178 }
179 }
180 }
181
182 /**
183 * Switch to the tracing perspective
184 */
185 public static void switchToTracingPerspective() {
664fa59c
MK
186 switchToPerspective(TRACING_PERSPECTIVE_ID);
187 }
188
189 /**
190 * Switch to a given perspective
191 *
192 * @param id
193 * the perspective id (like
194 * "org.eclipse.linuxtools.tmf.ui.perspective"
195 */
196 public static void switchToPerspective(final String id) {
306e18d0
MK
197 UIThreadRunnable.syncExec(new VoidResult() {
198 @Override
199 public void run() {
200 try {
664fa59c 201 PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
306e18d0
MK
202 } catch (WorkbenchException e) {
203 fail(e.getMessage());
204 }
205 }
206 });
207 }
208
306e18d0
MK
209 /**
210 * If the test is running in the UI thread then fail
211 */
212 public static void failIfUIThread() {
213 if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) {
214 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"
215 + " that useUIThread is set to false in the pom.xml");
216 }
217
306e18d0 218 }
7777d5f0
MK
219
220 /**
221 * Open a trace, this does not perform any validation though
222 *
223 * @param projectName
224 * The project name
225 * @param tracePath
226 * the path of the trace file (absolute or relative)
227 * @param traceType
2c7fb5af 228 * the trace type id (eg:
7777d5f0
MK
229 * org.eclipse.linuxtools.btf.trace)
230 */
231 public static void openTrace(final String projectName, final String tracePath, final String traceType) {
a3d7df19
BH
232 openTrace(projectName, tracePath, traceType, true);
233 }
234
235 /**
236 * Open a trace, this does not perform any validation though
237 *
238 * @param projectName
239 * The project name
240 * @param tracePath
241 * the path of the trace file (absolute or relative)
242 * @param traceType
2c7fb5af 243 * the trace type id (eg:
a3d7df19
BH
244 * org.eclipse.linuxtools.btf.trace)
245 * @param delay
246 * delay and wait for jobs
247 */
248 public static void openTrace(final String projectName, final String tracePath, final String traceType, boolean delay) {
7777d5f0
MK
249 final Exception exception[] = new Exception[1];
250 exception[0] = null;
251 UIThreadRunnable.syncExec(new VoidResult() {
252 @Override
253 public void run() {
254 try {
255 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
256 TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder();
257 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, tracePath, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType);
258 } catch (CoreException e) {
259 exception[0] = e;
260 }
261 }
262 });
263 if (exception[0] != null) {
264 fail(exception[0].getMessage());
265 }
266
a3d7df19
BH
267 if (delay) {
268 delay(1000);
269 waitForJobs();
270 }
7777d5f0 271 }
693ec829
BH
272
273 /**
b4290931 274 * Finds an editor and sets focus to the editor
693ec829
BH
275 *
276 * @param bot
277 * the workbench bot
278 * @param editorName
279 * the editor name
280 * @return the corresponding SWTBotEditor
281 */
b4290931 282 public static SWTBotEditor activateEditor(SWTWorkbenchBot bot, String editorName) {
693ec829
BH
283 Matcher<IEditorReference> matcher = WidgetMatcherFactory.withPartName(editorName);
284 final SWTBotEditor editorBot = bot.editor(matcher);
285 IEditorPart iep = editorBot.getReference().getEditor(true);
286 final TmfEventsEditor tmfEd = (TmfEventsEditor) iep;
287 editorBot.show();
288 UIThreadRunnable.syncExec(new VoidResult() {
289 @Override
290 public void run() {
291 tmfEd.setFocus();
292 }
293 });
294
fa24d78b
AM
295 SWTBotUtils.waitForJobs();
296 SWTBotUtils.delay(1000);
693ec829
BH
297 assertNotNull(tmfEd);
298 return editorBot;
299 }
b4290931
MAL
300
301 /**
302 * Opens a trace in an editor and get the TmfEventsEditor
303 *
304 * @param bot
305 * the workbench bot
306 * @param projectName
307 * the name of the project that contains the trace
308 * @param elementPath
309 * the trace element path (relative to Traces folder)
310 * @return TmfEventsEditor the opened editor
311 */
312 public static TmfEventsEditor openEditor(SWTWorkbenchBot bot, String projectName, IPath elementPath) {
313 final SWTBotView projectExplorerBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
314 projectExplorerBot.setFocus();
315
316 final SWTBotTree tree = bot.tree();
317 final SWTBotTreeItem treeItem = tree.getTreeItem(projectName);
318 treeItem.expand();
319
320 String nodeName = getFullNodeName(treeItem, TmfTracesFolder.TRACES_FOLDER_NAME);
321 bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, treeItem));
322 SWTBotTreeItem tracesNode = treeItem.getNode(nodeName);
323 tracesNode.expand();
324
325 SWTBotTreeItem currentNode = tracesNode;
326 for (String segment : elementPath.segments()) {
327 String fullNodeName = getFullNodeName(currentNode, segment);
328 bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(fullNodeName, currentNode));
329 SWTBotTreeItem newNode = currentNode.getNode(fullNodeName);
330 newNode.select();
331 newNode.doubleClick();
332 currentNode = newNode;
333 }
334
fa24d78b
AM
335 SWTBotUtils.delay(1000);
336 SWTBotUtils.waitForJobs();
b4290931
MAL
337 final String expectedTitle = elementPath.toString();
338
339 final IEditorPart iep[] = new IEditorPart[1];
340 UIThreadRunnable.syncExec(new VoidResult() {
341 @Override
342 public void run() {
343 IEditorReference[] ieds = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
344 assertNotNull(ieds);
345 iep[0] = null;
346 for (IEditorReference ied : ieds) {
347 if (ied.getTitle().equals(expectedTitle)) {
348 iep[0] = ied.getEditor(true);
349 break;
350 }
351 }
352 }
353 });
354 assertNotNull(iep[0]);
355 return (TmfEventsEditor) iep[0];
356 }
357
358 private static String getFullNodeName(final SWTBotTreeItem treeItem, String prefix) {
359 List<String> nodes = treeItem.getNodes();
360 String nodeName = "";
361 for (String node : nodes) {
362 if (node.startsWith(prefix)) {
363 nodeName = node;
364 }
365 }
366 return nodeName;
367 }
34c0fc10
MK
368
369 /**
370 * Select the traces folder
371 *
372 * @param bot
373 * a given workbench bot
374 * @param projectName
375 * the name of the project (it needs to exist or else it would time out)
376 * @return a {@link SWTBotTreeItem} of the "Traces" directory
377 */
378 public static SWTBotTreeItem selectTracesFolder(SWTWorkbenchBot bot, String projectName) {
379 SWTBotView projectExplorerBot = bot.viewByTitle("Project Explorer");
380 projectExplorerBot.show();
381 SWTBotTreeItem treeItem = projectExplorerBot.bot().tree().getTreeItem(projectName);
382 treeItem.select();
383 treeItem.expand();
384 SWTBotTreeItem treeNode = null;
385 for (String node : treeItem.getNodes()) {
386 if (node.matches("Traces\\s\\[(\\d)*\\]")) {
387 treeNode = treeItem.getNode(node);
388 break;
389 }
390 }
391 assertNotNull(treeNode);
392 return treeNode;
393 }
2470d687
MK
394
395 /**
396 * Open a view by id.
397 *
398 * @param id
399 * view id.
400 */
401 public static void openView(final String id) {
402 final PartInitException res[] = new PartInitException[1];
403 UIThreadRunnable.syncExec(new VoidResult() {
404 @Override
405 public void run() {
406 try {
407 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id);
408 } catch (PartInitException e) {
409 res[0] = e;
410 }
411 }
412 });
413 if (res[0] != null) {
414 fail(res[0].getMessage());
415 }
416 waitForJobs();
417 }
306e18d0 418}
This page took 0.065198 seconds and 5 git commands to generate.