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
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 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
13 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared;
14
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18
19 import java.util.List;
20
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.ResourcesPlugin;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.NullProgressMonitor;
27 import org.eclipse.core.runtime.jobs.Job;
28 import org.eclipse.jface.bindings.keys.IKeyLookup;
29 import org.eclipse.jface.bindings.keys.KeyStroke;
30 import org.eclipse.jface.bindings.keys.ParseException;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.swt.graphics.Rectangle;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Table;
35 import org.eclipse.swt.widgets.TableItem;
36 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
37 import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory;
38 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
39 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
40 import org.eclipse.swtbot.swt.finder.SWTBot;
41 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
42 import org.eclipse.swtbot.swt.finder.results.Result;
43 import org.eclipse.swtbot.swt.finder.results.VoidResult;
44 import org.eclipse.swtbot.swt.finder.waits.Conditions;
45 import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
46 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
47 import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
48 import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
49 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
50 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
51 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
52 import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
53 import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
54 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
55 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
56 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
57 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers.ProjectElementHasChild;
58 import org.eclipse.tracecompass.tmf.ui.views.TracingPerspectiveFactory;
59 import org.eclipse.ui.IEditorPart;
60 import org.eclipse.ui.IEditorReference;
61 import org.eclipse.ui.IPageLayout;
62 import org.eclipse.ui.PartInitException;
63 import org.eclipse.ui.PlatformUI;
64 import org.eclipse.ui.WorkbenchException;
65 import org.hamcrest.Matcher;
66
67 /**
68 * SWTBot Helper functions
69 *
70 * @author Matthew Khouzam
71 */
72 public final class SWTBotUtils {
73
74 private SWTBotUtils() {
75 }
76
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
120 SWTBotUtils.waitForJobs();
121 }
122
123 /**
124 * Deletes a project
125 *
126 * @param projectName
127 * the name of the tracing project
128 * @param deleteResources
129 * whether or not to deleted resources under the project
130 * @param bot
131 * the workbench bot
132 */
133 public static void deleteProject(final String projectName, boolean deleteResources, SWTWorkbenchBot bot) {
134 // Wait for any analysis to complete because it might create
135 // supplementary files
136 SWTBotUtils.waitForJobs();
137 try {
138 ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).refreshLocal(IResource.DEPTH_INFINITE, null);
139 } catch (CoreException e) {
140 }
141
142 SWTBotUtils.waitForJobs();
143
144 final SWTBotView projectViewBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
145 projectViewBot.setFocus();
146
147 SWTBotTree treeBot = projectViewBot.bot().tree();
148 SWTBotTreeItem treeItem = treeBot.getTreeItem(projectName);
149 SWTBotMenu contextMenu = treeItem.contextMenu("Delete");
150 contextMenu.click();
151
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 }
158
159 final SWTBotButton okButton = bot.button("OK");
160 bot.waitUntil(Conditions.widgetIsEnabled(okButton));
161 okButton.click();
162
163 SWTBotUtils.waitForJobs();
164 }
165
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
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 * 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
224 /**
225 * Switch to the tracing perspective
226 */
227 public static void switchToTracingPerspective() {
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) {
239 UIThreadRunnable.syncExec(new VoidResult() {
240 @Override
241 public void run() {
242 try {
243 PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
244 } catch (WorkbenchException e) {
245 fail(e.getMessage());
246 }
247 }
248 });
249 }
250
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
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
270 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
271 */
272 public static void openTrace(final String projectName, final String tracePath, final String traceType) {
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
284 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
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) {
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
307 if (delay) {
308 delay(1000);
309 waitForJobs();
310 }
311 }
312
313 /**
314 * Finds an editor and sets focus to the editor
315 *
316 * @param bot
317 * the workbench bot
318 * @param editorName
319 * the editor name
320 * @return the corresponding SWTBotEditor
321 */
322 public static SWTBotEditor activateEditor(SWTWorkbenchBot bot, String editorName) {
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
335 SWTBotUtils.waitForJobs();
336 SWTBotUtils.delay(1000);
337 assertNotNull(tmfEd);
338 return editorBot;
339 }
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) {
353 final SWTBotView projectExplorerView = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
354 projectExplorerView.setFocus();
355 SWTBot projectExplorerBot = projectExplorerView.bot();
356
357 final SWTBotTree tree = projectExplorerBot.tree();
358 projectExplorerBot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(projectName, tree));
359 final SWTBotTreeItem treeItem = tree.getTreeItem(projectName);
360 treeItem.expand();
361
362 SWTBotTreeItem tracesNode = getTraceProjectItem(projectExplorerBot, treeItem, TmfTracesFolder.TRACES_FOLDER_NAME);
363 tracesNode.expand();
364
365 SWTBotTreeItem currentItem = tracesNode;
366 for (String segment : elementPath.segments()) {
367 currentItem = getTraceProjectItem(projectExplorerBot, currentItem, segment);
368 currentItem.select();
369 currentItem.doubleClick();
370 }
371
372 SWTBotEditor editor = bot.editorByTitle(elementPath.toString());
373 IEditorPart editorPart = editor.getReference().getEditor(false);
374 assertTrue(editorPart instanceof TmfEventsEditor);
375 return (TmfEventsEditor) editorPart;
376 }
377
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 */
390 public static SWTBotTreeItem getTraceProjectItem(SWTBot bot, final SWTBotTreeItem parentItem, final String name) {
391 ProjectElementHasChild condition = new ProjectElementHasChild(parentItem, name);
392 bot.waitUntil(condition);
393 return condition.getItem();
394 }
395
396 /**
397 * Select the traces folder
398 *
399 * @param bot
400 * a given workbench bot
401 * @param projectName
402 * the name of the project (it needs to exist or else it would
403 * time out)
404 * @return a {@link SWTBotTreeItem} of the "Traces" folder
405 */
406 public static SWTBotTreeItem selectTracesFolder(SWTWorkbenchBot bot, String projectName) {
407 SWTBotTreeItem projectTreeItem = selectProject(bot, projectName);
408 projectTreeItem.select();
409 SWTBotTreeItem tracesFolderItem = getTraceProjectItem(bot, projectTreeItem, TmfTracesFolder.TRACES_FOLDER_NAME);
410 tracesFolderItem.select();
411 return tracesFolderItem;
412 }
413
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
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 }
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 }
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 }
524 }
This page took 0.051665 seconds and 6 git commands to generate.