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