tmf: Fix StandardImportGzipTraceTest failing when running from RunAllSWTBotTests
[deliverable/tracecompass.git] / tmf / 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;
21e5206c 16import static org.junit.Assert.assertTrue;
306e18d0
MK
17import static org.junit.Assert.fail;
18
19import java.util.List;
20
21import org.eclipse.core.resources.IProject;
93c91230
MAL
22import org.eclipse.core.resources.IResource;
23import org.eclipse.core.resources.ResourcesPlugin;
24import org.eclipse.core.runtime.CoreException;
b4290931 25import org.eclipse.core.runtime.IPath;
306e18d0
MK
26import org.eclipse.core.runtime.NullProgressMonitor;
27import org.eclipse.core.runtime.jobs.Job;
328e5fe4
MK
28import org.eclipse.jface.bindings.keys.IKeyLookup;
29import org.eclipse.jface.bindings.keys.KeyStroke;
30import org.eclipse.jface.bindings.keys.ParseException;
31import org.eclipse.swt.graphics.Point;
32import org.eclipse.swt.graphics.Rectangle;
306e18d0 33import org.eclipse.swt.widgets.Display;
328e5fe4
MK
34import org.eclipse.swt.widgets.Table;
35import org.eclipse.swt.widgets.TableItem;
306e18d0 36import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
693ec829
BH
37import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory;
38import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
306e18d0 39import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
bbdb3d6d 40import org.eclipse.swtbot.swt.finder.SWTBot;
306e18d0 41import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
328e5fe4 42import org.eclipse.swtbot.swt.finder.results.Result;
306e18d0 43import org.eclipse.swtbot.swt.finder.results.VoidResult;
93c91230
MAL
44import org.eclipse.swtbot.swt.finder.waits.Conditions;
45import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
46import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
47import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
306e18d0 48import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
328e5fe4 49import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
93c91230
MAL
50import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
51import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
2bdf0193
AM
52import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
53import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
54import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
55import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
b4290931 56import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
21e5206c 57import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers.ProjectElementHasChild;
2bdf0193 58import org.eclipse.tracecompass.tmf.ui.views.TracingPerspectiveFactory;
693ec829
BH
59import org.eclipse.ui.IEditorPart;
60import org.eclipse.ui.IEditorReference;
93c91230 61import org.eclipse.ui.IPageLayout;
2470d687 62import org.eclipse.ui.PartInitException;
306e18d0
MK
63import org.eclipse.ui.PlatformUI;
64import org.eclipse.ui.WorkbenchException;
693ec829 65import org.hamcrest.Matcher;
306e18d0
MK
66
67/**
68 * SWTBot Helper functions
69 *
70 * @author Matthew Khouzam
71 */
fa24d78b
AM
72public final class SWTBotUtils {
73
328e5fe4
MK
74 private SWTBotUtils() {
75 }
fa24d78b 76
306e18d0
MK
77 private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID;
78
79 /**
80 * Waits for all Eclipse jobs to finish
81 */
82 public static void waitForJobs() {
83 while (!Job.getJobManager().isIdle()) {
84 delay(100);
85 }
86 }
87
88 /**
89 * Sleeps current thread for a given time.
90 *
91 * @param waitTimeMillis
92 * time in milliseconds to wait
93 */
94 public static void delay(final long waitTimeMillis) {
95 try {
96 Thread.sleep(waitTimeMillis);
97 } catch (final InterruptedException e) {
98 // Ignored
99 }
100 }
101
102 /**
103 * Create a tracing project
104 *
105 * @param projectName
106 * the name of the tracing project
107 */
108 public static void createProject(final String projectName) {
109 /*
110 * Make a new test
111 */
112 UIThreadRunnable.syncExec(new VoidResult() {
113 @Override
114 public void run() {
115 IProject project = TmfProjectRegistry.createProject(projectName, null, new NullProgressMonitor());
116 assertNotNull(project);
117 }
118 });
119
fa24d78b 120 SWTBotUtils.waitForJobs();
306e18d0
MK
121 }
122
93c91230 123 /**
bbdb3d6d 124 * Deletes a project
93c91230
MAL
125 *
126 * @param projectName
127 * the name of the tracing project
bbdb3d6d
MAL
128 * @param deleteResources
129 * whether or not to deleted resources under the project
93c91230
MAL
130 * @param bot
131 * the workbench bot
132 */
bbdb3d6d 133 public static void deleteProject(final String projectName, boolean deleteResources, SWTWorkbenchBot bot) {
7777d5f0
MK
134 // Wait for any analysis to complete because it might create
135 // supplementary files
fa24d78b 136 SWTBotUtils.waitForJobs();
93c91230
MAL
137 try {
138 ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).refreshLocal(IResource.DEPTH_INFINITE, null);
139 } catch (CoreException e) {
140 }
141
fa24d78b 142 SWTBotUtils.waitForJobs();
93c91230
MAL
143
144 final SWTBotView projectViewBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
145 projectViewBot.setFocus();
146
693ec829 147 SWTBotTree treeBot = projectViewBot.bot().tree();
93c91230
MAL
148 SWTBotTreeItem treeItem = treeBot.getTreeItem(projectName);
149 SWTBotMenu contextMenu = treeItem.contextMenu("Delete");
150 contextMenu.click();
151
bbdb3d6d
MAL
152 if (deleteResources) {
153 bot.shell("Delete Resources").setFocus();
154 final SWTBotCheckBox checkBox = bot.checkBox();
155 bot.waitUntil(Conditions.widgetIsEnabled(checkBox));
156 checkBox.click();
157 }
93c91230
MAL
158
159 final SWTBotButton okButton = bot.button("OK");
160 bot.waitUntil(Conditions.widgetIsEnabled(okButton));
161 okButton.click();
162
fa24d78b 163 SWTBotUtils.waitForJobs();
93c91230
MAL
164 }
165
bbdb3d6d
MAL
166 /**
167 * Deletes a project and its resources
168 *
169 * @param projectName
170 * the name of the tracing project
171 * @param bot
172 * the workbench bot
173 */
174 public static void deleteProject(String projectName, SWTWorkbenchBot bot) {
175 deleteProject(projectName, true, bot);
176 }
177
306e18d0
MK
178 /**
179 * Focus on the main window
180 *
181 * @param shellBots
182 * swtbotshells for all the shells
183 */
184 public static void focusMainWindow(SWTBotShell[] shellBots) {
185 for (SWTBotShell shellBot : shellBots) {
186 if (shellBot.getText().toLowerCase().contains("eclipse")) {
187 shellBot.activate();
188 }
189 }
190 }
191
192 /**
193 * Close a view with a title
194 *
195 * @param title
196 * the title, like "welcome"
197 * @param bot
198 * the workbench bot
199 */
200 public static void closeView(String title, SWTWorkbenchBot bot) {
201 final List<SWTBotView> openViews = bot.views();
202 for (SWTBotView view : openViews) {
203 if (view.getTitle().equalsIgnoreCase(title)) {
204 view.close();
205 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
206 }
207 }
208 }
209
210 /**
211 * Switch to the tracing perspective
212 */
213 public static void switchToTracingPerspective() {
664fa59c
MK
214 switchToPerspective(TRACING_PERSPECTIVE_ID);
215 }
216
217 /**
218 * Switch to a given perspective
219 *
220 * @param id
221 * the perspective id (like
222 * "org.eclipse.linuxtools.tmf.ui.perspective"
223 */
224 public static void switchToPerspective(final String id) {
306e18d0
MK
225 UIThreadRunnable.syncExec(new VoidResult() {
226 @Override
227 public void run() {
228 try {
664fa59c 229 PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
306e18d0
MK
230 } catch (WorkbenchException e) {
231 fail(e.getMessage());
232 }
233 }
234 });
235 }
236
306e18d0
MK
237 /**
238 * If the test is running in the UI thread then fail
239 */
240 public static void failIfUIThread() {
241 if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) {
242 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"
243 + " that useUIThread is set to false in the pom.xml");
244 }
245
306e18d0 246 }
7777d5f0
MK
247
248 /**
249 * Open a trace, this does not perform any validation though
250 *
251 * @param projectName
252 * The project name
253 * @param tracePath
254 * the path of the trace file (absolute or relative)
255 * @param traceType
328e5fe4 256 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
7777d5f0
MK
257 */
258 public static void openTrace(final String projectName, final String tracePath, final String traceType) {
a3d7df19
BH
259 openTrace(projectName, tracePath, traceType, true);
260 }
261
262 /**
263 * Open a trace, this does not perform any validation though
264 *
265 * @param projectName
266 * The project name
267 * @param tracePath
268 * the path of the trace file (absolute or relative)
269 * @param traceType
328e5fe4 270 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
a3d7df19
BH
271 * @param delay
272 * delay and wait for jobs
273 */
274 public static void openTrace(final String projectName, final String tracePath, final String traceType, boolean delay) {
7777d5f0
MK
275 final Exception exception[] = new Exception[1];
276 exception[0] = null;
277 UIThreadRunnable.syncExec(new VoidResult() {
278 @Override
279 public void run() {
280 try {
281 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
282 TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder();
283 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, tracePath, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType);
284 } catch (CoreException e) {
285 exception[0] = e;
286 }
287 }
288 });
289 if (exception[0] != null) {
290 fail(exception[0].getMessage());
291 }
292
a3d7df19
BH
293 if (delay) {
294 delay(1000);
295 waitForJobs();
296 }
7777d5f0 297 }
693ec829
BH
298
299 /**
b4290931 300 * Finds an editor and sets focus to the editor
693ec829
BH
301 *
302 * @param bot
303 * the workbench bot
304 * @param editorName
305 * the editor name
306 * @return the corresponding SWTBotEditor
307 */
b4290931 308 public static SWTBotEditor activateEditor(SWTWorkbenchBot bot, String editorName) {
693ec829
BH
309 Matcher<IEditorReference> matcher = WidgetMatcherFactory.withPartName(editorName);
310 final SWTBotEditor editorBot = bot.editor(matcher);
311 IEditorPart iep = editorBot.getReference().getEditor(true);
312 final TmfEventsEditor tmfEd = (TmfEventsEditor) iep;
313 editorBot.show();
314 UIThreadRunnable.syncExec(new VoidResult() {
315 @Override
316 public void run() {
317 tmfEd.setFocus();
318 }
319 });
320
fa24d78b
AM
321 SWTBotUtils.waitForJobs();
322 SWTBotUtils.delay(1000);
693ec829
BH
323 assertNotNull(tmfEd);
324 return editorBot;
325 }
b4290931
MAL
326
327 /**
328 * Opens a trace in an editor and get the TmfEventsEditor
329 *
330 * @param bot
331 * the workbench bot
332 * @param projectName
333 * the name of the project that contains the trace
334 * @param elementPath
335 * the trace element path (relative to Traces folder)
336 * @return TmfEventsEditor the opened editor
337 */
338 public static TmfEventsEditor openEditor(SWTWorkbenchBot bot, String projectName, IPath elementPath) {
339 final SWTBotView projectExplorerBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
340 projectExplorerBot.setFocus();
341
342 final SWTBotTree tree = bot.tree();
21e5206c 343 bot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(projectName, tree));
b4290931
MAL
344 final SWTBotTreeItem treeItem = tree.getTreeItem(projectName);
345 treeItem.expand();
346
21e5206c 347 SWTBotTreeItem tracesNode = getTraceProjectItem(bot, treeItem, TmfTracesFolder.TRACES_FOLDER_NAME);
b4290931
MAL
348 tracesNode.expand();
349
21e5206c 350 SWTBotTreeItem currentItem = tracesNode;
b4290931 351 for (String segment : elementPath.segments()) {
21e5206c
PT
352 currentItem = getTraceProjectItem(bot, currentItem, segment);
353 currentItem.select();
354 currentItem.doubleClick();
b4290931
MAL
355 }
356
21e5206c
PT
357 SWTBotEditor editor = bot.editorByTitle(elementPath.toString());
358 IEditorPart editorPart = editor.getReference().getEditor(false);
359 assertTrue(editorPart instanceof TmfEventsEditor);
360 return (TmfEventsEditor) editorPart;
b4290931
MAL
361 }
362
21e5206c
PT
363 /**
364 * Returns the child tree item of the specified item with the given name.
365 * The project element label may have a count suffix in the format ' [n]'.
366 *
367 * @param bot
368 * a given workbench bot
369 * @param parentItem
370 * the parent tree item
371 * @param name
372 * the desired child element name (without suffix)
373 * @return the a {@link SWTBotTreeItem} with the specified name
374 */
375 public static SWTBotTreeItem getTraceProjectItem(SWTWorkbenchBot bot, final SWTBotTreeItem parentItem, final String name) {
376 ProjectElementHasChild condition = new ProjectElementHasChild(parentItem, name);
377 bot.waitUntil(condition);
378 return condition.getItem();
b4290931 379 }
34c0fc10
MK
380
381 /**
382 * Select the traces folder
383 *
384 * @param bot
385 * a given workbench bot
386 * @param projectName
328e5fe4
MK
387 * the name of the project (it needs to exist or else it would
388 * time out)
21e5206c 389 * @return a {@link SWTBotTreeItem} of the "Traces" folder
34c0fc10
MK
390 */
391 public static SWTBotTreeItem selectTracesFolder(SWTWorkbenchBot bot, String projectName) {
bbdb3d6d 392 SWTBotTreeItem projectTreeItem = selectProject(bot, projectName);
21e5206c
PT
393 projectTreeItem.select();
394 SWTBotTreeItem tracesFolderItem = getTraceProjectItem(bot, projectTreeItem, TmfTracesFolder.TRACES_FOLDER_NAME);
395 tracesFolderItem.select();
396 return tracesFolderItem;
34c0fc10 397 }
2470d687 398
bbdb3d6d
MAL
399 /**
400 * Select the project in Project Explorer
401 *
402 * @param bot
403 * a given workbench bot
404 * @param projectName
405 * the name of the project (it needs to exist or else it would time out)
406 * @return a {@link SWTBotTreeItem} of the project
407 */
408 public static SWTBotTreeItem selectProject(SWTWorkbenchBot bot, String projectName) {
409 SWTBotView projectExplorerBot = bot.viewByTitle("Project Explorer");
410 projectExplorerBot.show();
411 SWTBotTreeItem treeItem = projectExplorerBot.bot().tree().getTreeItem(projectName);
412 treeItem.select();
413 return treeItem;
414 }
415
2470d687
MK
416 /**
417 * Open a view by id.
418 *
419 * @param id
420 * view id.
421 */
422 public static void openView(final String id) {
423 final PartInitException res[] = new PartInitException[1];
424 UIThreadRunnable.syncExec(new VoidResult() {
425 @Override
426 public void run() {
427 try {
428 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id);
429 } catch (PartInitException e) {
430 res[0] = e;
431 }
432 }
433 });
434 if (res[0] != null) {
435 fail(res[0].getMessage());
436 }
437 waitForJobs();
438 }
328e5fe4
MK
439
440 /**
441 * Maximize a table
442 *
443 * @param tableBot
444 * the {@link SWTBotTable} table
445 */
446 public static void maximizeTable(SWTBotTable tableBot) {
447 try {
448 tableBot.pressShortcut(KeyStroke.getInstance(IKeyLookup.CTRL_NAME + "+"), KeyStroke.getInstance("M"));
449 } catch (ParseException e) {
450 fail();
451 }
452 }
453
454 /**
455 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
456 * index in a table
457 *
458 * @param table
459 * the table
460 * @param row
461 * the row of the table to look up
462 * @param col
463 * the column of the table to look up
464 * @return the bounds in display relative coordinates
465 */
466 public static Rectangle getCellBounds(final Table table, final int row, final int col) {
467 return UIThreadRunnable.syncExec(new Result<Rectangle>() {
468 @Override
469 public Rectangle run() {
470 TableItem item = table.getItem(row);
471 Rectangle bounds = item.getBounds(col);
472 Point p = table.toDisplay(bounds.x, bounds.y);
473 Rectangle rect = new Rectangle(p.x, p.y, bounds.width, bounds.height);
474 return rect;
475 }
476 });
477 }
bbdb3d6d
MAL
478
479 /**
480 * Get the tree item from a tree at the specified location
481 *
482 * @param bot
483 * the SWTBot
484 * @param tree
485 * the tree to find the tree item in
486 * @param nodeNames
487 * the path to the tree item, in the form of node names (from
488 * parent to child).
489 * @return the tree item
490 */
491 public static SWTBotTreeItem getTreeItem(SWTBot bot, SWTBotTree tree, String... nodeNames) {
492 if (nodeNames.length == 0) {
493 return null;
494 }
495
496 bot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(nodeNames[0], tree));
497 SWTBotTreeItem currentNode = tree.getTreeItem(nodeNames[0]);
498 for (int i = 1; i < nodeNames.length; i++) {
499 currentNode.expand();
500
501 String nodeName = nodeNames[i];
502 bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, currentNode));
503 SWTBotTreeItem newNode = currentNode.getNode(nodeName);
504 currentNode = newNode;
505 }
506
507 return currentNode;
508 }
306e18d0 509}
This page took 0.087923 seconds and 5 git commands to generate.