tmf: Ellipse function names that don't fit in CallStackView
[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;
6941941d 20import java.util.TimeZone;
675b1249 21import java.util.concurrent.atomic.AtomicBoolean;
306e18d0 22
29fe7911 23import org.eclipse.core.resources.IFolder;
306e18d0 24import org.eclipse.core.resources.IProject;
93c91230 25import org.eclipse.core.resources.IResource;
29fe7911 26import org.eclipse.core.resources.IResourceVisitor;
93c91230
MAL
27import org.eclipse.core.resources.ResourcesPlugin;
28import org.eclipse.core.runtime.CoreException;
b4290931 29import org.eclipse.core.runtime.IPath;
306e18d0
MK
30import org.eclipse.core.runtime.NullProgressMonitor;
31import org.eclipse.core.runtime.jobs.Job;
40ba27e1 32import org.eclipse.jdt.annotation.NonNull;
328e5fe4
MK
33import org.eclipse.jface.bindings.keys.IKeyLookup;
34import org.eclipse.jface.bindings.keys.KeyStroke;
35import org.eclipse.jface.bindings.keys.ParseException;
675b1249
PT
36import org.eclipse.swt.events.ControlAdapter;
37import org.eclipse.swt.events.ControlEvent;
328e5fe4
MK
38import org.eclipse.swt.graphics.Point;
39import org.eclipse.swt.graphics.Rectangle;
306e18d0 40import org.eclipse.swt.widgets.Display;
5785ab49 41import org.eclipse.swt.widgets.Shell;
328e5fe4
MK
42import org.eclipse.swt.widgets.Table;
43import org.eclipse.swt.widgets.TableItem;
306e18d0 44import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
693ec829
BH
45import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory;
46import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
306e18d0 47import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
bbdb3d6d 48import org.eclipse.swtbot.swt.finder.SWTBot;
306e18d0 49import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
328e5fe4 50import org.eclipse.swtbot.swt.finder.results.Result;
306e18d0 51import org.eclipse.swtbot.swt.finder.results.VoidResult;
747d62e1 52import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
93c91230 53import org.eclipse.swtbot.swt.finder.waits.Conditions;
a345ad25 54import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
93c91230
MAL
55import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
56import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
57import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
306e18d0 58import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
328e5fe4 59import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
93c91230
MAL
60import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
61import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
65416c44 62import org.eclipse.swtbot.swt.finder.widgets.TimeoutException;
40ba27e1 63import org.eclipse.tracecompass.internal.tmf.ui.project.operations.NewExperimentOperation;
2bdf0193 64import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
40ba27e1
BH
65import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
66import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder;
2bdf0193 67import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
29fe7911 68import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
2bdf0193 69import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
29fe7911 70import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
2bdf0193 71import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
b4290931 72import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
21e5206c 73import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers.ProjectElementHasChild;
2bdf0193 74import org.eclipse.tracecompass.tmf.ui.views.TracingPerspectiveFactory;
693ec829
BH
75import org.eclipse.ui.IEditorPart;
76import org.eclipse.ui.IEditorReference;
93c91230 77import org.eclipse.ui.IPageLayout;
2470d687 78import org.eclipse.ui.PartInitException;
306e18d0
MK
79import org.eclipse.ui.PlatformUI;
80import org.eclipse.ui.WorkbenchException;
693ec829 81import org.hamcrest.Matcher;
306e18d0
MK
82
83/**
84 * SWTBot Helper functions
85 *
86 * @author Matthew Khouzam
87 */
40ba27e1 88@SuppressWarnings("restriction")
fa24d78b
AM
89public final class SWTBotUtils {
90
fd5f786c 91 private static final long MAX_JOBS_WAIT_TIME = 300000;
747d62e1
MAL
92 private static final String WINDOW_MENU = "Window";
93 private static final String PREFERENCES_MENU_ITEM = "Preferences";
2ad409b3 94 private static boolean fPrintedEnvironment = false;
747d62e1 95
328e5fe4
MK
96 private SWTBotUtils() {
97 }
fa24d78b 98
306e18d0
MK
99 private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID;
100
101 /**
fd5f786c
MAL
102 * Waits for all Eclipse jobs to finish. Times out after
103 * SWTBotUtils#MAX_JOBS_WAIT_TIME by default.
104 *
105 * @throws TimeoutException
106 * once the waiting time passes the default maximum value
306e18d0
MK
107 */
108 public static void waitForJobs() {
fd5f786c
MAL
109 waitForJobs(MAX_JOBS_WAIT_TIME);
110 }
111
112 /**
113 * Waits for all Eclipse jobs to finish
114 *
115 * @param maxWait
116 * the maximum time to wait, in milliseconds. Once the waiting
117 * time passes the maximum value, a TimeoutException is thrown
118 * @throws TimeoutException
119 * once the waiting time passes the maximum value
120 */
121 public static void waitForJobs(long maxWait) {
122 long waitStart = System.currentTimeMillis();
306e18d0 123 while (!Job.getJobManager().isIdle()) {
fd5f786c
MAL
124 if (System.currentTimeMillis() - waitStart > maxWait) {
125 printJobs();
126 throw new TimeoutException("Timed out waiting for jobs to finish.");
127 }
128
306e18d0
MK
129 delay(100);
130 }
131 }
132
fd5f786c
MAL
133 private static void printJobs() {
134 Job[] jobs = Job.getJobManager().find(null);
135 for (Job job : jobs) {
136 System.err.println(job.toString() + " state: " + jobStateToString(job.getState()));
137 Thread thread = job.getThread();
138 if (thread != null) {
139 for (StackTraceElement stractTraceElement : thread.getStackTrace()) {
140 System.err.println(" " + stractTraceElement);
141 }
142 }
143 System.err.println();
144 }
145 }
146
147 private static String jobStateToString(int jobState) {
148 switch (jobState) {
149 case Job.RUNNING:
150 return "RUNNING";
151 case Job.WAITING:
152 return "WAITING";
153 case Job.SLEEPING:
154 return "SLEEPING";
155 case Job.NONE:
156 return "NONE";
157 default:
158 return "UNKNOWN";
159 }
160 }
161
306e18d0
MK
162 /**
163 * Sleeps current thread for a given time.
164 *
165 * @param waitTimeMillis
166 * time in milliseconds to wait
167 */
168 public static void delay(final long waitTimeMillis) {
169 try {
170 Thread.sleep(waitTimeMillis);
171 } catch (final InterruptedException e) {
172 // Ignored
173 }
174 }
175
176 /**
177 * Create a tracing project
178 *
179 * @param projectName
180 * the name of the tracing project
181 */
182 public static void createProject(final String projectName) {
183 /*
184 * Make a new test
185 */
186 UIThreadRunnable.syncExec(new VoidResult() {
187 @Override
188 public void run() {
189 IProject project = TmfProjectRegistry.createProject(projectName, null, new NullProgressMonitor());
190 assertNotNull(project);
191 }
192 });
193
fa24d78b 194 SWTBotUtils.waitForJobs();
306e18d0
MK
195 }
196
93c91230 197 /**
bbdb3d6d 198 * Deletes a project
93c91230
MAL
199 *
200 * @param projectName
201 * the name of the tracing project
bbdb3d6d
MAL
202 * @param deleteResources
203 * whether or not to deleted resources under the project
93c91230
MAL
204 * @param bot
205 * the workbench bot
206 */
bbdb3d6d 207 public static void deleteProject(final String projectName, boolean deleteResources, SWTWorkbenchBot bot) {
7777d5f0
MK
208 // Wait for any analysis to complete because it might create
209 // supplementary files
fa24d78b 210 SWTBotUtils.waitForJobs();
93c91230
MAL
211 try {
212 ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).refreshLocal(IResource.DEPTH_INFINITE, null);
213 } catch (CoreException e) {
214 }
215
fa24d78b 216 SWTBotUtils.waitForJobs();
93c91230
MAL
217
218 final SWTBotView projectViewBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
219 projectViewBot.setFocus();
220
693ec829 221 SWTBotTree treeBot = projectViewBot.bot().tree();
93c91230
MAL
222 SWTBotTreeItem treeItem = treeBot.getTreeItem(projectName);
223 SWTBotMenu contextMenu = treeItem.contextMenu("Delete");
224 contextMenu.click();
225
bbdb3d6d
MAL
226 if (deleteResources) {
227 bot.shell("Delete Resources").setFocus();
228 final SWTBotCheckBox checkBox = bot.checkBox();
229 bot.waitUntil(Conditions.widgetIsEnabled(checkBox));
230 checkBox.click();
231 }
93c91230
MAL
232
233 final SWTBotButton okButton = bot.button("OK");
234 bot.waitUntil(Conditions.widgetIsEnabled(okButton));
235 okButton.click();
236
fa24d78b 237 SWTBotUtils.waitForJobs();
93c91230
MAL
238 }
239
bbdb3d6d
MAL
240 /**
241 * Deletes a project and its resources
242 *
243 * @param projectName
244 * the name of the tracing project
245 * @param bot
246 * the workbench bot
247 */
248 public static void deleteProject(String projectName, SWTWorkbenchBot bot) {
249 deleteProject(projectName, true, bot);
250 }
251
40ba27e1
BH
252 /**
253 * Creates an experiment
254 *
255 * @param bot
256 * a given workbench bot
257 * @param projectName
258 * the name of the project, creates the project if needed
259 * @param expName
260 * the experiment name
261 */
262 public static void createExperiment(SWTWorkbenchBot bot, String projectName, final @NonNull String expName) {
263 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
264 TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, true);
265 TmfExperimentFolder expFolder = tmfProject.getExperimentsFolder();
266 assertNotNull(expFolder);
267 NewExperimentOperation operation = new NewExperimentOperation(expFolder, expName);
268 operation.run(new NullProgressMonitor());
269
270 bot.waitUntil(new DefaultCondition() {
271 @Override
272 public boolean test() throws Exception {
273 TmfExperimentElement experiment = expFolder.getExperiment(expName);
274 return experiment != null;
275 }
276
277 @Override
278 public String getFailureMessage() {
279 return "Experiment (" + expName + ") couldn't be created";
280 }
281 });
282 }
283
284
306e18d0
MK
285 /**
286 * Focus on the main window
287 *
288 * @param shellBots
289 * swtbotshells for all the shells
290 */
291 public static void focusMainWindow(SWTBotShell[] shellBots) {
292 for (SWTBotShell shellBot : shellBots) {
293 if (shellBot.getText().toLowerCase().contains("eclipse")) {
294 shellBot.activate();
295 }
296 }
297 }
298
299 /**
300 * Close a view with a title
301 *
302 * @param title
303 * the title, like "welcome"
304 * @param bot
305 * the workbench bot
306 */
307 public static void closeView(String title, SWTWorkbenchBot bot) {
308 final List<SWTBotView> openViews = bot.views();
309 for (SWTBotView view : openViews) {
310 if (view.getTitle().equalsIgnoreCase(title)) {
311 view.close();
312 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
313 }
314 }
315 }
316
e4d15418
MAL
317 /**
318 * Close a view with an id
319 *
320 * @param viewId
321 * the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
322 * @param bot
323 * the workbench bot
324 */
325 public static void closeViewById(String viewId, SWTWorkbenchBot bot) {
326 final SWTBotView view = bot.viewById(viewId);
327 view.close();
328 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
329 }
330
306e18d0
MK
331 /**
332 * Switch to the tracing perspective
333 */
334 public static void switchToTracingPerspective() {
664fa59c
MK
335 switchToPerspective(TRACING_PERSPECTIVE_ID);
336 }
337
338 /**
339 * Switch to a given perspective
340 *
341 * @param id
342 * the perspective id (like
343 * "org.eclipse.linuxtools.tmf.ui.perspective"
344 */
345 public static void switchToPerspective(final String id) {
306e18d0
MK
346 UIThreadRunnable.syncExec(new VoidResult() {
347 @Override
348 public void run() {
349 try {
664fa59c 350 PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
306e18d0
MK
351 } catch (WorkbenchException e) {
352 fail(e.getMessage());
353 }
354 }
355 });
356 }
357
5785ab49
MAL
358 /**
359 * Initialize the environment for SWTBot
360 */
361 public static void initialize() {
362 failIfUIThread();
363
364 SWTWorkbenchBot bot = new SWTWorkbenchBot();
365 UIThreadRunnable.syncExec(() -> {
2ad409b3 366 printEnvironment();
5475ad49
MAL
367
368 // There seems to be problems on some system where the main shell is
369 // not in focus initially. This was seen using Xvfb and Xephyr on some occasions.
370 focusMainWindow(bot.shells());
371
5785ab49
MAL
372 Shell shell = bot.activeShell().widget;
373
374 // Only adjust shell if it appears to be the top-most
375 if (shell.getParent() == null) {
376 makeShellFullyVisible(shell);
377 }
378 });
379 }
380
2ad409b3
MAL
381 private static void printEnvironment() {
382 if (fPrintedEnvironment) {
383 return;
384 }
385
386 // Print some information about the environment that could affect test outcome
387 Rectangle bounds = Display.getDefault().getBounds();
388 System.out.println("Display size: " + bounds.width + "x" + bounds.height);
389
390 String osVersion = System.getProperty("os.version");
391 if (osVersion != null) {
392 System.out.println("OS version=" + osVersion);
393 }
394 String gtkVersion = System.getProperty("org.eclipse.swt.internal.gtk.version");
395 if (gtkVersion != null) {
396 System.out.println("GTK version=" + gtkVersion);
397 String overlayScrollbar = System.getenv("LIBOVERLAY_SCROLLBAR");
398 if (overlayScrollbar != null) {
399 System.out.println("LIBOVERLAY_SCROLLBAR=" + overlayScrollbar);
400 }
401 String ubuntuMenuProxy = System.getenv("UBUNTU_MENUPROXY");
402 if (ubuntuMenuProxy != null) {
403 System.out.println("UBUNTU_MENUPROXY=" + ubuntuMenuProxy);
404 }
405 }
406
6941941d
MAL
407 System.out.println("Time zone: " + TimeZone.getDefault().getDisplayName());
408
2ad409b3
MAL
409 fPrintedEnvironment = true;
410 }
411
306e18d0
MK
412 /**
413 * If the test is running in the UI thread then fail
414 */
5785ab49 415 private static void failIfUIThread() {
306e18d0
MK
416 if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) {
417 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"
418 + " that useUIThread is set to false in the pom.xml");
419 }
5785ab49 420 }
306e18d0 421
5785ab49
MAL
422 /**
423 * Try to make the shell fully visible in the display. If the shell cannot
424 * fit the display, it will be positioned so that top-left corner is at
425 * <code>(0, 0)</code> in display-relative coordinates.
426 *
427 * @param shell
428 * the shell to make fully visible
429 */
430 private static void makeShellFullyVisible(Shell shell) {
431 Rectangle displayBounds = shell.getDisplay().getBounds();
432 Point absCoord = shell.toDisplay(0, 0);
433 Point shellSize = shell.getSize();
434
435 Point newLocation = new Point(absCoord.x, absCoord.y);
436 newLocation.x = Math.max(0, Math.min(absCoord.x, displayBounds.width - shellSize.x));
437 newLocation.y = Math.max(0, Math.min(absCoord.y, displayBounds.height - shellSize.y));
438 if (!newLocation.equals(absCoord)) {
439 shell.setLocation(newLocation);
440 }
306e18d0 441 }
7777d5f0
MK
442
443 /**
444 * Open a trace, this does not perform any validation though
445 *
446 * @param projectName
447 * The project name
448 * @param tracePath
449 * the path of the trace file (absolute or relative)
450 * @param traceType
328e5fe4 451 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
7777d5f0
MK
452 */
453 public static void openTrace(final String projectName, final String tracePath, final String traceType) {
a3d7df19
BH
454 openTrace(projectName, tracePath, traceType, true);
455 }
456
457 /**
458 * Open a trace, this does not perform any validation though
459 *
460 * @param projectName
461 * The project name
462 * @param tracePath
463 * the path of the trace file (absolute or relative)
464 * @param traceType
328e5fe4 465 * the trace type id (eg: org.eclipse.linuxtools.btf.trace)
a3d7df19
BH
466 * @param delay
467 * delay and wait for jobs
468 */
469 public static void openTrace(final String projectName, final String tracePath, final String traceType, boolean delay) {
7777d5f0
MK
470 final Exception exception[] = new Exception[1];
471 exception[0] = null;
472 UIThreadRunnable.syncExec(new VoidResult() {
473 @Override
474 public void run() {
475 try {
476 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
477 TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder();
478 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, tracePath, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType);
479 } catch (CoreException e) {
480 exception[0] = e;
481 }
482 }
483 });
484 if (exception[0] != null) {
485 fail(exception[0].getMessage());
486 }
487
a3d7df19
BH
488 if (delay) {
489 delay(1000);
490 waitForJobs();
491 }
7777d5f0 492 }
693ec829
BH
493
494 /**
b4290931 495 * Finds an editor and sets focus to the editor
693ec829
BH
496 *
497 * @param bot
498 * the workbench bot
499 * @param editorName
500 * the editor name
501 * @return the corresponding SWTBotEditor
502 */
b4290931 503 public static SWTBotEditor activateEditor(SWTWorkbenchBot bot, String editorName) {
693ec829
BH
504 Matcher<IEditorReference> matcher = WidgetMatcherFactory.withPartName(editorName);
505 final SWTBotEditor editorBot = bot.editor(matcher);
506 IEditorPart iep = editorBot.getReference().getEditor(true);
507 final TmfEventsEditor tmfEd = (TmfEventsEditor) iep;
508 editorBot.show();
509 UIThreadRunnable.syncExec(new VoidResult() {
510 @Override
511 public void run() {
512 tmfEd.setFocus();
513 }
514 });
515
fa24d78b
AM
516 SWTBotUtils.waitForJobs();
517 SWTBotUtils.delay(1000);
693ec829
BH
518 assertNotNull(tmfEd);
519 return editorBot;
520 }
b4290931
MAL
521
522 /**
523 * Opens a trace in an editor and get the TmfEventsEditor
524 *
525 * @param bot
526 * the workbench bot
527 * @param projectName
528 * the name of the project that contains the trace
529 * @param elementPath
530 * the trace element path (relative to Traces folder)
531 * @return TmfEventsEditor the opened editor
532 */
533 public static TmfEventsEditor openEditor(SWTWorkbenchBot bot, String projectName, IPath elementPath) {
5b6c8456
MAL
534 final SWTBotView projectExplorerView = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
535 projectExplorerView.setFocus();
536 SWTBot projectExplorerBot = projectExplorerView.bot();
b4290931 537
5b6c8456
MAL
538 final SWTBotTree tree = projectExplorerBot.tree();
539 projectExplorerBot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(projectName, tree));
b4290931
MAL
540 final SWTBotTreeItem treeItem = tree.getTreeItem(projectName);
541 treeItem.expand();
542
5b6c8456 543 SWTBotTreeItem tracesNode = getTraceProjectItem(projectExplorerBot, treeItem, TmfTracesFolder.TRACES_FOLDER_NAME);
b4290931
MAL
544 tracesNode.expand();
545
21e5206c 546 SWTBotTreeItem currentItem = tracesNode;
b4290931 547 for (String segment : elementPath.segments()) {
5b6c8456 548 currentItem = getTraceProjectItem(projectExplorerBot, currentItem, segment);
21e5206c
PT
549 currentItem.select();
550 currentItem.doubleClick();
b4290931
MAL
551 }
552
21e5206c
PT
553 SWTBotEditor editor = bot.editorByTitle(elementPath.toString());
554 IEditorPart editorPart = editor.getReference().getEditor(false);
555 assertTrue(editorPart instanceof TmfEventsEditor);
556 return (TmfEventsEditor) editorPart;
b4290931
MAL
557 }
558
21e5206c
PT
559 /**
560 * Returns the child tree item of the specified item with the given name.
561 * The project element label may have a count suffix in the format ' [n]'.
562 *
563 * @param bot
564 * a given workbench bot
565 * @param parentItem
566 * the parent tree item
567 * @param name
568 * the desired child element name (without suffix)
569 * @return the a {@link SWTBotTreeItem} with the specified name
570 */
5b6c8456 571 public static SWTBotTreeItem getTraceProjectItem(SWTBot bot, final SWTBotTreeItem parentItem, final String name) {
21e5206c
PT
572 ProjectElementHasChild condition = new ProjectElementHasChild(parentItem, name);
573 bot.waitUntil(condition);
574 return condition.getItem();
b4290931 575 }
34c0fc10
MK
576
577 /**
578 * Select the traces folder
579 *
580 * @param bot
581 * a given workbench bot
582 * @param projectName
328e5fe4
MK
583 * the name of the project (it needs to exist or else it would
584 * time out)
21e5206c 585 * @return a {@link SWTBotTreeItem} of the "Traces" folder
34c0fc10
MK
586 */
587 public static SWTBotTreeItem selectTracesFolder(SWTWorkbenchBot bot, String projectName) {
bbdb3d6d 588 SWTBotTreeItem projectTreeItem = selectProject(bot, projectName);
21e5206c
PT
589 projectTreeItem.select();
590 SWTBotTreeItem tracesFolderItem = getTraceProjectItem(bot, projectTreeItem, TmfTracesFolder.TRACES_FOLDER_NAME);
591 tracesFolderItem.select();
592 return tracesFolderItem;
34c0fc10 593 }
2470d687 594
29fe7911
MAL
595 /**
596 * Clear the traces folder
597 *
598 * @param bot
599 * a given workbench bot
600 * @param projectName
601 * the name of the project (needs to exist)
602 */
603 public static void clearTracesFolder(SWTWorkbenchBot bot, String projectName) {
604 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
605 TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, false);
606 TmfTraceFolder tracesFolder = tmfProject.getTracesFolder();
607 try {
608 for (TmfTraceElement traceElement : tracesFolder.getTraces()) {
609 traceElement.delete(null);
610 }
611
612 final IFolder resource = tracesFolder.getResource();
613 resource.accept(new IResourceVisitor() {
614 @Override
615 public boolean visit(IResource visitedResource) throws CoreException {
616 if (visitedResource != resource) {
617 visitedResource.delete(true, null);
618 }
619 return true;
620 }
621 }, IResource.DEPTH_ONE, 0);
622 } catch (CoreException e) {
623 fail(e.getMessage());
624 }
625
626 bot.waitUntil(new DefaultCondition() {
627 private int fTraceNb = 0;
628
629 @Override
630 public boolean test() throws Exception {
631 List<TmfTraceElement> traces = tracesFolder.getTraces();
632 fTraceNb = traces.size();
633 return fTraceNb == 0;
634 }
635
636 @Override
637 public String getFailureMessage() {
638 return "Traces Folder not empty (" + fTraceNb + ")";
639 }
640 });
40ba27e1
BH
641 }
642
643 /**
644 * Clear the experiment folder
645 *
646 * @param bot
647 * a given workbench bot
648 * @param projectName
649 * the name of the project (needs to exist)
650 */
651 public static void clearExperimentFolder(SWTWorkbenchBot bot, String projectName) {
652 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
653 TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, false);
654 TmfExperimentFolder expFolder = tmfProject.getExperimentsFolder();
655 expFolder.getExperiments().forEach(experiment -> {
656 IResource resource = experiment.getResource();
657 try {
658 // Close the experiment if open
659 experiment.closeEditors();
660
661 IPath path = resource.getLocation();
662 if (path != null) {
663 // Delete supplementary files
664 experiment.deleteSupplementaryFolder();
665 }
666 // Finally, delete the experiment
667 resource.delete(true, null);
668 } catch (CoreException e) {
669 fail(e.getMessage());
670 }
671 });
672
673 bot.waitUntil(new DefaultCondition() {
674 private int fExperimentNb = 0;
29fe7911 675
40ba27e1
BH
676 @Override
677 public boolean test() throws Exception {
678 List<TmfExperimentElement> experiments = expFolder.getExperiments();
679 fExperimentNb = experiments.size();
680 return fExperimentNb == 0;
681 }
682
683 @Override
684 public String getFailureMessage() {
685 return "Experiment Folder not empty (" + fExperimentNb + ")";
686 }
687 });
29fe7911
MAL
688 }
689
bbdb3d6d
MAL
690 /**
691 * Select the project in Project Explorer
692 *
693 * @param bot
694 * a given workbench bot
695 * @param projectName
696 * the name of the project (it needs to exist or else it would time out)
697 * @return a {@link SWTBotTreeItem} of the project
698 */
699 public static SWTBotTreeItem selectProject(SWTWorkbenchBot bot, String projectName) {
700 SWTBotView projectExplorerBot = bot.viewByTitle("Project Explorer");
701 projectExplorerBot.show();
e834a6b4
MAL
702 // FIXME: Bug 496519. Sometimes, the tree becomes disabled for a certain
703 // amount of time. This can happen during a long running operation
704 // (BusyIndicator.showWhile) which brings up the modal dialog "operation
705 // in progress" and this disables all shells
706 projectExplorerBot.bot().waitUntil(Conditions.widgetIsEnabled(projectExplorerBot.bot().tree()));
bbdb3d6d
MAL
707 SWTBotTreeItem treeItem = projectExplorerBot.bot().tree().getTreeItem(projectName);
708 treeItem.select();
709 return treeItem;
710 }
711
2470d687
MK
712 /**
713 * Open a view by id.
714 *
715 * @param id
716 * view id.
717 */
718 public static void openView(final String id) {
719 final PartInitException res[] = new PartInitException[1];
720 UIThreadRunnable.syncExec(new VoidResult() {
721 @Override
722 public void run() {
723 try {
724 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id);
725 } catch (PartInitException e) {
726 res[0] = e;
727 }
728 }
729 });
730 if (res[0] != null) {
731 fail(res[0].getMessage());
732 }
733 waitForJobs();
734 }
328e5fe4
MK
735
736 /**
737 * Maximize a table
738 *
739 * @param tableBot
740 * the {@link SWTBotTable} table
741 */
742 public static void maximizeTable(SWTBotTable tableBot) {
675b1249
PT
743 final AtomicBoolean controlResized = new AtomicBoolean();
744 UIThreadRunnable.syncExec(new VoidResult() {
745 @Override
746 public void run() {
747 tableBot.widget.addControlListener(new ControlAdapter() {
748 @Override
749 public void controlResized(ControlEvent e) {
750 tableBot.widget.removeControlListener(this);
751 controlResized.set(true);
752 }
753 });
754 }
755 });
328e5fe4
MK
756 try {
757 tableBot.pressShortcut(KeyStroke.getInstance(IKeyLookup.CTRL_NAME + "+"), KeyStroke.getInstance("M"));
758 } catch (ParseException e) {
759 fail();
760 }
675b1249
PT
761 new SWTBot().waitUntil(new DefaultCondition() {
762 @Override
763 public boolean test() throws Exception {
764 return controlResized.get();
765 }
766
767 @Override
768 public String getFailureMessage() {
769 return "Control was not resized";
770 }
771 });
328e5fe4
MK
772 }
773
774 /**
775 * Get the bounds of a cell (SWT.Rectangle) for the specified row and column
776 * index in a table
777 *
778 * @param table
779 * the table
780 * @param row
781 * the row of the table to look up
782 * @param col
783 * the column of the table to look up
784 * @return the bounds in display relative coordinates
785 */
786 public static Rectangle getCellBounds(final Table table, final int row, final int col) {
787 return UIThreadRunnable.syncExec(new Result<Rectangle>() {
788 @Override
789 public Rectangle run() {
790 TableItem item = table.getItem(row);
791 Rectangle bounds = item.getBounds(col);
792 Point p = table.toDisplay(bounds.x, bounds.y);
793 Rectangle rect = new Rectangle(p.x, p.y, bounds.width, bounds.height);
794 return rect;
795 }
796 });
797 }
bbdb3d6d
MAL
798
799 /**
800 * Get the tree item from a tree at the specified location
801 *
802 * @param bot
803 * the SWTBot
804 * @param tree
805 * the tree to find the tree item in
806 * @param nodeNames
807 * the path to the tree item, in the form of node names (from
808 * parent to child).
809 * @return the tree item
810 */
811 public static SWTBotTreeItem getTreeItem(SWTBot bot, SWTBotTree tree, String... nodeNames) {
812 if (nodeNames.length == 0) {
813 return null;
814 }
815
816 bot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(nodeNames[0], tree));
817 SWTBotTreeItem currentNode = tree.getTreeItem(nodeNames[0]);
818 for (int i = 1; i < nodeNames.length; i++) {
819 currentNode.expand();
820
821 String nodeName = nodeNames[i];
65416c44
MAL
822 try {
823 bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, currentNode));
824 } catch (TimeoutException e) {
825 //FIXME: Sometimes in a JFace TreeViewer, it expands to nothing. Need to find out why.
826 currentNode.collapse();
827 currentNode.expand();
828 bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, currentNode));
829 }
830
bbdb3d6d
MAL
831 SWTBotTreeItem newNode = currentNode.getNode(nodeName);
832 currentNode = newNode;
833 }
834
835 return currentNode;
836 }
a345ad25
MAL
837
838 /**
839 * Get the active events editor. Note that this will wait until such editor
840 * is available.
841 *
842 * @param workbenchBot
843 * a given workbench bot
844 * @return the active events editor
845 */
846 public static SWTBotEditor activeEventsEditor(final SWTWorkbenchBot workbenchBot) {
e834a6b4
MAL
847 ConditionHelpers.ActiveEventsEditor condition = new ConditionHelpers.ActiveEventsEditor(workbenchBot, null);
848 workbenchBot.waitUntil(condition);
849 return condition.getActiveEditor();
850 }
a345ad25 851
e834a6b4
MAL
852 /**
853 * Get the active events editor. Note that this will wait until such editor
854 * is available.
855 *
856 * @param workbenchBot
857 * a given workbench bot
858 * @param editorTitle
859 * the desired editor title. If null, any active events editor
860 * will be considered valid.
861 * @return the active events editor
862 */
863 public static SWTBotEditor activeEventsEditor(final SWTWorkbenchBot workbenchBot, String editorTitle) {
864 ConditionHelpers.ActiveEventsEditor condition = new ConditionHelpers.ActiveEventsEditor(workbenchBot, editorTitle);
865 workbenchBot.waitUntil(condition);
866 return condition.getActiveEditor();
a345ad25 867 }
747d62e1
MAL
868
869 /**
870 * Open the preferences dialog and return the corresponding shell.
871 *
872 * @param bot
873 * a given workbench bot
874 * @return the preferences shell
875 */
876 public static SWTBotShell openPreferences(SWTBot bot) {
877 if (SWTUtils.isMac()) {
878 // On Mac, the Preferences menu item is under the application name.
879 // For some reason, we can't access the application menu anymore so
880 // we use the keyboard shortcut.
881 try {
882 bot.activeShell().pressShortcut(KeyStroke.getInstance(IKeyLookup.COMMAND_NAME + "+"), KeyStroke.getInstance(","));
883 } catch (ParseException e) {
884 fail();
885 }
886 } else {
887 bot.menu(WINDOW_MENU).menu(PREFERENCES_MENU_ITEM).click();
888 }
889
890 bot.waitUntil(Conditions.shellIsActive(PREFERENCES_MENU_ITEM));
891 return bot.activeShell();
892 }
306e18d0 893}
This page took 0.116537 seconds and 5 git commands to generate.