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