tmf: Make Filter and Colors tests close the views they opened
[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
e4d15418
MAL
210 /**
211 * Close a view with an id
212 *
213 * @param viewId
214 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
215 * @param bot
216 * the workbench bot
217 */
218 public static void closeViewById(String viewId, SWTWorkbenchBot bot) {
219 final SWTBotView view = bot.viewById(viewId);
220 view.close();
221 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
222 }
223
306e18d0
MK
224 /**
225 * Switch to the tracing perspective
226 */
227 public static void switchToTracingPerspective() {
664fa59c
MK
228 switchToPerspective(TRACING_PERSPECTIVE_ID);
229 }
230
231 /**
232 * Switch to a given perspective
233 *
234 * @param id
235 * the perspective id (like
236 * "org.eclipse.linuxtools.tmf.ui.perspective"
237 */
238 public static void switchToPerspective(final String id) {
306e18d0
MK
239 UIThreadRunnable.syncExec(new VoidResult() {
240 @Override
241 public void run() {
242 try {
664fa59c 243 PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
306e18d0
MK
244 } catch (WorkbenchException e) {
245 fail(e.getMessage());
246 }
247 }
248 });
249 }
250
306e18d0
MK
251 /**
252 * If the test is running in the UI thread then fail
253 */
254 public static void failIfUIThread() {
255 if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) {
256 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"
257 + " that useUIThread is set to false in the pom.xml");
258 }
259
306e18d0 260 }
7777d5f0
MK
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)
7777d5f0
MK
271 */
272 public static void openTrace(final String projectName, final String tracePath, final String traceType) {
a3d7df19
BH
273 openTrace(projectName, tracePath, traceType, true);
274 }
275
276 /**
277 * Open a trace, this does not perform any validation though
278 *
279 * @param projectName
280 * The project name
281 * @param tracePath
282 * the path of the trace file (absolute or relative)
283 * @param traceType
328e5fe4 284 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
a3d7df19
BH
285 * @param delay
286 * delay and wait for jobs
287 */
288 public static void openTrace(final String projectName, final String tracePath, final String traceType, boolean delay) {
7777d5f0
MK
289 final Exception exception[] = new Exception[1];
290 exception[0] = null;
291 UIThreadRunnable.syncExec(new VoidResult() {
292 @Override
293 public void run() {
294 try {
295 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
296 TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder();
297 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, tracePath, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType);
298 } catch (CoreException e) {
299 exception[0] = e;
300 }
301 }
302 });
303 if (exception[0] != null) {
304 fail(exception[0].getMessage());
305 }
306
a3d7df19
BH
307 if (delay) {
308 delay(1000);
309 waitForJobs();
310 }
7777d5f0 311 }
693ec829
BH
312
313 /**
b4290931 314 * Finds an editor and sets focus to the editor
693ec829
BH
315 *
316 * @param bot
317 * the workbench bot
318 * @param editorName
319 * the editor name
320 * @return the corresponding SWTBotEditor
321 */
b4290931 322 public static SWTBotEditor activateEditor(SWTWorkbenchBot bot, String editorName) {
693ec829
BH
323 Matcher<IEditorReference> matcher = WidgetMatcherFactory.withPartName(editorName);
324 final SWTBotEditor editorBot = bot.editor(matcher);
325 IEditorPart iep = editorBot.getReference().getEditor(true);
326 final TmfEventsEditor tmfEd = (TmfEventsEditor) iep;
327 editorBot.show();
328 UIThreadRunnable.syncExec(new VoidResult() {
329 @Override
330 public void run() {
331 tmfEd.setFocus();
332 }
333 });
334
fa24d78b
AM
335 SWTBotUtils.waitForJobs();
336 SWTBotUtils.delay(1000);
693ec829
BH
337 assertNotNull(tmfEd);
338 return editorBot;
339 }
b4290931
MAL
340
341 /**
342 * Opens a trace in an editor and get the TmfEventsEditor
343 *
344 * @param bot
345 * the workbench bot
346 * @param projectName
347 * the name of the project that contains the trace
348 * @param elementPath
349 * the trace element path (relative to Traces folder)
350 * @return TmfEventsEditor the opened editor
351 */
352 public static TmfEventsEditor openEditor(SWTWorkbenchBot bot, String projectName, IPath elementPath) {
5b6c8456
MAL
353 final SWTBotView projectExplorerView = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
354 projectExplorerView.setFocus();
355 SWTBot projectExplorerBot = projectExplorerView.bot();
b4290931 356
5b6c8456
MAL
357 final SWTBotTree tree = projectExplorerBot.tree();
358 projectExplorerBot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(projectName, tree));
b4290931
MAL
359 final SWTBotTreeItem treeItem = tree.getTreeItem(projectName);
360 treeItem.expand();
361
5b6c8456 362 SWTBotTreeItem tracesNode = getTraceProjectItem(projectExplorerBot, treeItem, TmfTracesFolder.TRACES_FOLDER_NAME);
b4290931
MAL
363 tracesNode.expand();
364
21e5206c 365 SWTBotTreeItem currentItem = tracesNode;
b4290931 366 for (String segment : elementPath.segments()) {
5b6c8456 367 currentItem = getTraceProjectItem(projectExplorerBot, currentItem, segment);
21e5206c
PT
368 currentItem.select();
369 currentItem.doubleClick();
b4290931
MAL
370 }
371
21e5206c
PT
372 SWTBotEditor editor = bot.editorByTitle(elementPath.toString());
373 IEditorPart editorPart = editor.getReference().getEditor(false);
374 assertTrue(editorPart instanceof TmfEventsEditor);
375 return (TmfEventsEditor) editorPart;
b4290931
MAL
376 }
377
21e5206c
PT
378 /**
379 * Returns the child tree item of the specified item with the given name.
380 * The project element label may have a count suffix in the format ' [n]'.
381 *
382 * @param bot
383 * a given workbench bot
384 * @param parentItem
385 * the parent tree item
386 * @param name
387 * the desired child element name (without suffix)
388 * @return the a {@link SWTBotTreeItem} with the specified name
389 */
5b6c8456 390 public static SWTBotTreeItem getTraceProjectItem(SWTBot bot, final SWTBotTreeItem parentItem, final String name) {
21e5206c
PT
391 ProjectElementHasChild condition = new ProjectElementHasChild(parentItem, name);
392 bot.waitUntil(condition);
393 return condition.getItem();
b4290931 394 }
34c0fc10
MK
395
396 /**
397 * Select the traces folder
398 *
399 * @param bot
400 * a given workbench bot
401 * @param projectName
328e5fe4
MK
402 * the name of the project (it needs to exist or else it would
403 * time out)
21e5206c 404 * @return a {@link SWTBotTreeItem} of the "Traces" folder
34c0fc10
MK
405 */
406 public static SWTBotTreeItem selectTracesFolder(SWTWorkbenchBot bot, String projectName) {
bbdb3d6d 407 SWTBotTreeItem projectTreeItem = selectProject(bot, projectName);
21e5206c
PT
408 projectTreeItem.select();
409 SWTBotTreeItem tracesFolderItem = getTraceProjectItem(bot, projectTreeItem, TmfTracesFolder.TRACES_FOLDER_NAME);
410 tracesFolderItem.select();
411 return tracesFolderItem;
34c0fc10 412 }
2470d687 413
bbdb3d6d
MAL
414 /**
415 * Select the project in Project Explorer
416 *
417 * @param bot
418 * a given workbench bot
419 * @param projectName
420 * the name of the project (it needs to exist or else it would time out)
421 * @return a {@link SWTBotTreeItem} of the project
422 */
423 public static SWTBotTreeItem selectProject(SWTWorkbenchBot bot, String projectName) {
424 SWTBotView projectExplorerBot = bot.viewByTitle("Project Explorer");
425 projectExplorerBot.show();
426 SWTBotTreeItem treeItem = projectExplorerBot.bot().tree().getTreeItem(projectName);
427 treeItem.select();
428 return treeItem;
429 }
430
2470d687
MK
431 /**
432 * Open a view by id.
433 *
434 * @param id
435 * view id.
436 */
437 public static void openView(final String id) {
438 final PartInitException res[] = new PartInitException[1];
439 UIThreadRunnable.syncExec(new VoidResult() {
440 @Override
441 public void run() {
442 try {
443 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id);
444 } catch (PartInitException e) {
445 res[0] = e;
446 }
447 }
448 });
449 if (res[0] != null) {
450 fail(res[0].getMessage());
451 }
452 waitForJobs();
453 }
328e5fe4
MK
454
455 /**
456 * Maximize a table
457 *
458 * @param tableBot
459 * the {@link SWTBotTable} table
460 */
461 public static void maximizeTable(SWTBotTable tableBot) {
462 try {
463 tableBot.pressShortcut(KeyStroke.getInstance(IKeyLookup.CTRL_NAME + "+"), KeyStroke.getInstance("M"));
464 } catch (ParseException e) {
465 fail();
466 }
467 }
468
469 /**
470 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
471 * index in a table
472 *
473 * @param table
474 * the table
475 * @param row
476 * the row of the table to look up
477 * @param col
478 * the column of the table to look up
479 * @return the bounds in display relative coordinates
480 */
481 public static Rectangle getCellBounds(final Table table, final int row, final int col) {
482 return UIThreadRunnable.syncExec(new Result<Rectangle>() {
483 @Override
484 public Rectangle run() {
485 TableItem item = table.getItem(row);
486 Rectangle bounds = item.getBounds(col);
487 Point p = table.toDisplay(bounds.x, bounds.y);
488 Rectangle rect = new Rectangle(p.x, p.y, bounds.width, bounds.height);
489 return rect;
490 }
491 });
492 }
bbdb3d6d
MAL
493
494 /**
495 * Get the tree item from a tree at the specified location
496 *
497 * @param bot
498 * the SWTBot
499 * @param tree
500 * the tree to find the tree item in
501 * @param nodeNames
502 * the path to the tree item, in the form of node names (from
503 * parent to child).
504 * @return the tree item
505 */
506 public static SWTBotTreeItem getTreeItem(SWTBot bot, SWTBotTree tree, String... nodeNames) {
507 if (nodeNames.length == 0) {
508 return null;
509 }
510
511 bot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(nodeNames[0], tree));
512 SWTBotTreeItem currentNode = tree.getTreeItem(nodeNames[0]);
513 for (int i = 1; i < nodeNames.length; i++) {
514 currentNode.expand();
515
516 String nodeName = nodeNames[i];
517 bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, currentNode));
518 SWTBotTreeItem newNode = currentNode.getNode(nodeName);
519 currentNode = newNode;
520 }
521
522 return currentNode;
523 }
306e18d0 524}
This page took 0.088959 seconds and 5 git commands to generate.