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