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