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 | 14 | |
f715581c | 15 | import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic; |
4045095f | 16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
306e18d0 | 17 | import static org.junit.Assert.assertNotNull; |
21e5206c | 18 | import static org.junit.Assert.assertTrue; |
306e18d0 MK |
19 | import static org.junit.Assert.fail; |
20 | ||
1b9a8a8c | 21 | import java.util.Arrays; |
306e18d0 | 22 | import java.util.List; |
6941941d | 23 | import java.util.TimeZone; |
675b1249 | 24 | import java.util.concurrent.atomic.AtomicBoolean; |
4045095f | 25 | import java.util.concurrent.atomic.AtomicInteger; |
306e18d0 | 26 | |
1dfcd42b | 27 | import org.apache.log4j.Logger; |
b1d4e193 MK |
28 | import org.eclipse.core.commands.ExecutionException; |
29 | import org.eclipse.core.commands.NotEnabledException; | |
30 | import org.eclipse.core.commands.NotHandledException; | |
31 | import org.eclipse.core.commands.common.NotDefinedException; | |
29fe7911 | 32 | import org.eclipse.core.resources.IFolder; |
306e18d0 | 33 | import org.eclipse.core.resources.IProject; |
93c91230 | 34 | import org.eclipse.core.resources.IResource; |
29fe7911 | 35 | import org.eclipse.core.resources.IResourceVisitor; |
93c91230 MAL |
36 | import org.eclipse.core.resources.ResourcesPlugin; |
37 | import org.eclipse.core.runtime.CoreException; | |
b4290931 | 38 | import org.eclipse.core.runtime.IPath; |
e0a64bd9 | 39 | import org.eclipse.core.runtime.IStatus; |
306e18d0 | 40 | import org.eclipse.core.runtime.NullProgressMonitor; |
e0a64bd9 | 41 | import org.eclipse.core.runtime.Status; |
40ba27e1 | 42 | import org.eclipse.jdt.annotation.NonNull; |
43439230 | 43 | import org.eclipse.jdt.annotation.Nullable; |
328e5fe4 MK |
44 | import org.eclipse.jface.bindings.keys.IKeyLookup; |
45 | import org.eclipse.jface.bindings.keys.KeyStroke; | |
46 | import org.eclipse.jface.bindings.keys.ParseException; | |
d1255771 PT |
47 | import org.eclipse.jface.dialogs.MessageDialogWithToggle; |
48 | import org.eclipse.jface.preference.IPreferenceStore; | |
675b1249 PT |
49 | import org.eclipse.swt.events.ControlAdapter; |
50 | import org.eclipse.swt.events.ControlEvent; | |
4045095f PT |
51 | import org.eclipse.swt.events.KeyEvent; |
52 | import org.eclipse.swt.events.KeyListener; | |
328e5fe4 MK |
53 | import org.eclipse.swt.graphics.Point; |
54 | import org.eclipse.swt.graphics.Rectangle; | |
4045095f | 55 | import org.eclipse.swt.widgets.Control; |
306e18d0 | 56 | import org.eclipse.swt.widgets.Display; |
5785ab49 | 57 | import org.eclipse.swt.widgets.Shell; |
328e5fe4 MK |
58 | import org.eclipse.swt.widgets.Table; |
59 | import org.eclipse.swt.widgets.TableItem; | |
470aad1b | 60 | import org.eclipse.swt.widgets.TreeItem; |
306e18d0 | 61 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; |
693ec829 BH |
62 | import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory; |
63 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor; | |
306e18d0 | 64 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; |
bbdb3d6d | 65 | import org.eclipse.swtbot.swt.finder.SWTBot; |
306e18d0 | 66 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; |
1c9b6343 MAL |
67 | import org.eclipse.swtbot.swt.finder.keyboard.Keyboard; |
68 | import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes; | |
470aad1b | 69 | import org.eclipse.swtbot.swt.finder.results.IntResult; |
328e5fe4 | 70 | import org.eclipse.swtbot.swt.finder.results.Result; |
306e18d0 | 71 | import org.eclipse.swtbot.swt.finder.results.VoidResult; |
1dfcd42b | 72 | import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
747d62e1 | 73 | import org.eclipse.swtbot.swt.finder.utils.SWTUtils; |
93c91230 | 74 | import org.eclipse.swtbot.swt.finder.waits.Conditions; |
a345ad25 | 75 | import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; |
4045095f PT |
76 | import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot; |
77 | import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl; | |
93c91230 MAL |
78 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton; |
79 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox; | |
80 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu; | |
306e18d0 | 81 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; |
328e5fe4 | 82 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; |
4045095f | 83 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton; |
93c91230 MAL |
84 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; |
85 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; | |
65416c44 | 86 | import org.eclipse.swtbot.swt.finder.widgets.TimeoutException; |
d1255771 PT |
87 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
88 | import org.eclipse.tracecompass.internal.tmf.ui.ITmfUIPreferences; | |
40ba27e1 | 89 | import org.eclipse.tracecompass.internal.tmf.ui.project.operations.NewExperimentOperation; |
2bdf0193 | 90 | import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor; |
40ba27e1 BH |
91 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement; |
92 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder; | |
2bdf0193 | 93 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper; |
29fe7911 | 94 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement; |
2bdf0193 | 95 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry; |
29fe7911 | 96 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement; |
2bdf0193 | 97 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder; |
b4290931 | 98 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder; |
21e5206c | 99 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers.ProjectElementHasChild; |
f0beeb4a | 100 | import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils; |
2bdf0193 | 101 | import org.eclipse.tracecompass.tmf.ui.views.TracingPerspectiveFactory; |
693ec829 BH |
102 | import org.eclipse.ui.IEditorPart; |
103 | import org.eclipse.ui.IEditorReference; | |
93c91230 | 104 | import org.eclipse.ui.IPageLayout; |
b1d4e193 MK |
105 | import org.eclipse.ui.IViewPart; |
106 | import org.eclipse.ui.IWorkbenchCommandConstants; | |
43439230 | 107 | import org.eclipse.ui.IWorkbenchPage; |
b1d4e193 | 108 | import org.eclipse.ui.IWorkbenchPartSite; |
2470d687 | 109 | import org.eclipse.ui.PartInitException; |
306e18d0 MK |
110 | import org.eclipse.ui.PlatformUI; |
111 | import org.eclipse.ui.WorkbenchException; | |
b1d4e193 | 112 | import org.eclipse.ui.handlers.IHandlerService; |
693ec829 | 113 | import org.hamcrest.Matcher; |
306e18d0 MK |
114 | |
115 | /** | |
116 | * SWTBot Helper functions | |
117 | * | |
118 | * @author Matthew Khouzam | |
119 | */ | |
40ba27e1 | 120 | @SuppressWarnings("restriction") |
fa24d78b AM |
121 | public final class SWTBotUtils { |
122 | ||
747d62e1 MAL |
123 | private static final String WINDOW_MENU = "Window"; |
124 | private static final String PREFERENCES_MENU_ITEM = "Preferences"; | |
2ad409b3 | 125 | private static boolean fPrintedEnvironment = false; |
1dfcd42b | 126 | private static Logger log = Logger.getLogger(SWTBotUtils.class); |
747d62e1 | 127 | |
328e5fe4 | 128 | private SWTBotUtils() { |
1dfcd42b | 129 | |
328e5fe4 | 130 | } |
fa24d78b | 131 | |
306e18d0 MK |
132 | private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID; |
133 | ||
306e18d0 MK |
134 | /** |
135 | * Sleeps current thread for a given time. | |
136 | * | |
137 | * @param waitTimeMillis | |
138 | * time in milliseconds to wait | |
139 | */ | |
140 | public static void delay(final long waitTimeMillis) { | |
141 | try { | |
142 | Thread.sleep(waitTimeMillis); | |
143 | } catch (final InterruptedException e) { | |
144 | // Ignored | |
145 | } | |
146 | } | |
147 | ||
148 | /** | |
149 | * Create a tracing project | |
150 | * | |
151 | * @param projectName | |
152 | * the name of the tracing project | |
153 | */ | |
154 | public static void createProject(final String projectName) { | |
155 | /* | |
156 | * Make a new test | |
157 | */ | |
158 | UIThreadRunnable.syncExec(new VoidResult() { | |
159 | @Override | |
160 | public void run() { | |
161 | IProject project = TmfProjectRegistry.createProject(projectName, null, new NullProgressMonitor()); | |
162 | assertNotNull(project); | |
163 | } | |
164 | }); | |
165 | ||
f0beeb4a | 166 | WaitUtils.waitForJobs(); |
306e18d0 MK |
167 | } |
168 | ||
93c91230 | 169 | /** |
bbdb3d6d | 170 | * Deletes a project |
93c91230 MAL |
171 | * |
172 | * @param projectName | |
173 | * the name of the tracing project | |
bbdb3d6d MAL |
174 | * @param deleteResources |
175 | * whether or not to deleted resources under the project | |
93c91230 MAL |
176 | * @param bot |
177 | * the workbench bot | |
178 | */ | |
bbdb3d6d | 179 | public static void deleteProject(final String projectName, boolean deleteResources, SWTWorkbenchBot bot) { |
7777d5f0 MK |
180 | // Wait for any analysis to complete because it might create |
181 | // supplementary files | |
f0beeb4a | 182 | WaitUtils.waitForJobs(); |
93c91230 MAL |
183 | try { |
184 | ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).refreshLocal(IResource.DEPTH_INFINITE, null); | |
185 | } catch (CoreException e) { | |
186 | } | |
187 | ||
f0beeb4a | 188 | WaitUtils.waitForJobs(); |
93c91230 | 189 | |
1dfcd42b | 190 | closeSecondaryShells(bot); |
f0beeb4a | 191 | WaitUtils.waitForJobs(); |
1dfcd42b | 192 | |
d2fbf6b6 PT |
193 | if (!ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).exists()) { |
194 | return; | |
195 | } | |
196 | ||
93c91230 MAL |
197 | final SWTBotView projectViewBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER); |
198 | projectViewBot.setFocus(); | |
199 | ||
693ec829 | 200 | SWTBotTree treeBot = projectViewBot.bot().tree(); |
93c91230 MAL |
201 | SWTBotTreeItem treeItem = treeBot.getTreeItem(projectName); |
202 | SWTBotMenu contextMenu = treeItem.contextMenu("Delete"); | |
203 | contextMenu.click(); | |
204 | ||
bbdb3d6d MAL |
205 | if (deleteResources) { |
206 | bot.shell("Delete Resources").setFocus(); | |
207 | final SWTBotCheckBox checkBox = bot.checkBox(); | |
208 | bot.waitUntil(Conditions.widgetIsEnabled(checkBox)); | |
209 | checkBox.click(); | |
210 | } | |
93c91230 MAL |
211 | |
212 | final SWTBotButton okButton = bot.button("OK"); | |
213 | bot.waitUntil(Conditions.widgetIsEnabled(okButton)); | |
214 | okButton.click(); | |
215 | ||
f0beeb4a | 216 | WaitUtils.waitForJobs(); |
93c91230 MAL |
217 | } |
218 | ||
bbdb3d6d MAL |
219 | /** |
220 | * Deletes a project and its resources | |
221 | * | |
222 | * @param projectName | |
223 | * the name of the tracing project | |
224 | * @param bot | |
225 | * the workbench bot | |
226 | */ | |
227 | public static void deleteProject(String projectName, SWTWorkbenchBot bot) { | |
228 | deleteProject(projectName, true, bot); | |
229 | } | |
230 | ||
40ba27e1 BH |
231 | /** |
232 | * Creates an experiment | |
233 | * | |
234 | * @param bot | |
235 | * a given workbench bot | |
236 | * @param projectName | |
237 | * the name of the project, creates the project if needed | |
238 | * @param expName | |
239 | * the experiment name | |
240 | */ | |
241 | public static void createExperiment(SWTWorkbenchBot bot, String projectName, final @NonNull String expName) { | |
242 | IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); | |
243 | TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, true); | |
244 | TmfExperimentFolder expFolder = tmfProject.getExperimentsFolder(); | |
245 | assertNotNull(expFolder); | |
246 | NewExperimentOperation operation = new NewExperimentOperation(expFolder, expName); | |
247 | operation.run(new NullProgressMonitor()); | |
248 | ||
249 | bot.waitUntil(new DefaultCondition() { | |
250 | @Override | |
251 | public boolean test() throws Exception { | |
252 | TmfExperimentElement experiment = expFolder.getExperiment(expName); | |
253 | return experiment != null; | |
254 | } | |
255 | ||
256 | @Override | |
257 | public String getFailureMessage() { | |
258 | return "Experiment (" + expName + ") couldn't be created"; | |
259 | } | |
260 | }); | |
261 | } | |
262 | ||
306e18d0 MK |
263 | /** |
264 | * Focus on the main window | |
265 | * | |
266 | * @param shellBots | |
267 | * swtbotshells for all the shells | |
268 | */ | |
269 | public static void focusMainWindow(SWTBotShell[] shellBots) { | |
1dfcd42b MAL |
270 | SWTBotShell mainShell = getMainShell(shellBots); |
271 | if (mainShell != null) { | |
272 | mainShell.activate(); | |
273 | } | |
274 | } | |
275 | ||
276 | private static SWTBotShell getMainShell(SWTBotShell[] shellBots) { | |
277 | SWTBotShell mainShell = null; | |
306e18d0 MK |
278 | for (SWTBotShell shellBot : shellBots) { |
279 | if (shellBot.getText().toLowerCase().contains("eclipse")) { | |
1dfcd42b | 280 | mainShell = shellBot; |
306e18d0 MK |
281 | } |
282 | } | |
1dfcd42b MAL |
283 | return mainShell; |
284 | } | |
285 | ||
286 | /** | |
287 | * Close all non-main shells that are visible. | |
288 | * | |
289 | * @param bot | |
290 | * the workbench bot | |
291 | */ | |
292 | public static void closeSecondaryShells(SWTWorkbenchBot bot) { | |
293 | SWTBotShell[] shells = bot.shells(); | |
294 | SWTBotShell mainShell = getMainShell(shells); | |
295 | if (mainShell == null) { | |
296 | return; | |
297 | } | |
298 | ||
299 | // Close all non-main shell but make sure we don't close an invisible | |
300 | // shell such the special "limbo shell" that Eclipse needs to work | |
301 | Arrays.stream(shells) | |
302 | .filter(shell -> shell != mainShell) | |
d7ac6294 | 303 | .filter(s -> !s.widget.isDisposed()) |
1dfcd42b | 304 | .filter(SWTBotShell::isVisible) |
a463a9ba | 305 | .peek(shell -> log.debug(MessageFormat.format("Closing lingering shell with title {0}", shell.getText()))) |
1dfcd42b | 306 | .forEach(SWTBotShell::close); |
306e18d0 MK |
307 | } |
308 | ||
309 | /** | |
310 | * Close a view with a title | |
311 | * | |
312 | * @param title | |
313 | * the title, like "welcome" | |
314 | * @param bot | |
315 | * the workbench bot | |
316 | */ | |
317 | public static void closeView(String title, SWTWorkbenchBot bot) { | |
318 | final List<SWTBotView> openViews = bot.views(); | |
319 | for (SWTBotView view : openViews) { | |
320 | if (view.getTitle().equalsIgnoreCase(title)) { | |
321 | view.close(); | |
322 | bot.waitUntil(ConditionHelpers.ViewIsClosed(view)); | |
323 | } | |
324 | } | |
325 | } | |
326 | ||
e4d15418 MAL |
327 | /** |
328 | * Close a view with an id | |
329 | * | |
330 | * @param viewId | |
b6fddb83 MK |
331 | * the view id, like |
332 | * "org.eclipse.linuxtools.tmf.ui.views.histogram" | |
e4d15418 MAL |
333 | * @param bot |
334 | * the workbench bot | |
335 | */ | |
336 | public static void closeViewById(String viewId, SWTWorkbenchBot bot) { | |
337 | final SWTBotView view = bot.viewById(viewId); | |
338 | view.close(); | |
339 | bot.waitUntil(ConditionHelpers.ViewIsClosed(view)); | |
340 | } | |
341 | ||
306e18d0 MK |
342 | /** |
343 | * Switch to the tracing perspective | |
344 | */ | |
345 | public static void switchToTracingPerspective() { | |
664fa59c MK |
346 | switchToPerspective(TRACING_PERSPECTIVE_ID); |
347 | } | |
348 | ||
349 | /** | |
350 | * Switch to a given perspective | |
351 | * | |
352 | * @param id | |
353 | * the perspective id (like | |
354 | * "org.eclipse.linuxtools.tmf.ui.perspective" | |
355 | */ | |
356 | public static void switchToPerspective(final String id) { | |
306e18d0 MK |
357 | UIThreadRunnable.syncExec(new VoidResult() { |
358 | @Override | |
359 | public void run() { | |
360 | try { | |
664fa59c | 361 | PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow()); |
306e18d0 MK |
362 | } catch (WorkbenchException e) { |
363 | fail(e.getMessage()); | |
364 | } | |
365 | } | |
366 | }); | |
367 | } | |
368 | ||
5785ab49 MAL |
369 | /** |
370 | * Initialize the environment for SWTBot | |
371 | */ | |
372 | public static void initialize() { | |
373 | failIfUIThread(); | |
374 | ||
375 | SWTWorkbenchBot bot = new SWTWorkbenchBot(); | |
376 | UIThreadRunnable.syncExec(() -> { | |
2ad409b3 | 377 | printEnvironment(); |
5475ad49 MAL |
378 | |
379 | // There seems to be problems on some system where the main shell is | |
b6fddb83 MK |
380 | // not in focus initially. This was seen using Xvfb and Xephyr on |
381 | // some occasions. | |
5475ad49 MAL |
382 | focusMainWindow(bot.shells()); |
383 | ||
5785ab49 MAL |
384 | Shell shell = bot.activeShell().widget; |
385 | ||
386 | // Only adjust shell if it appears to be the top-most | |
387 | if (shell.getParent() == null) { | |
388 | makeShellFullyVisible(shell); | |
389 | } | |
390 | }); | |
d1255771 PT |
391 | |
392 | /* Do not switch perspectives on trace open */ | |
393 | IPreferenceStore store = Activator.getDefault().getPreferenceStore(); | |
394 | store.setValue(ITmfUIPreferences.SWITCH_TO_PERSPECTIVE, MessageDialogWithToggle.NEVER); | |
5785ab49 MAL |
395 | } |
396 | ||
2ad409b3 MAL |
397 | private static void printEnvironment() { |
398 | if (fPrintedEnvironment) { | |
399 | return; | |
400 | } | |
401 | ||
b6fddb83 MK |
402 | // Print some information about the environment that could affect test |
403 | // outcome | |
2ad409b3 MAL |
404 | Rectangle bounds = Display.getDefault().getBounds(); |
405 | System.out.println("Display size: " + bounds.width + "x" + bounds.height); | |
406 | ||
407 | String osVersion = System.getProperty("os.version"); | |
408 | if (osVersion != null) { | |
409 | System.out.println("OS version=" + osVersion); | |
410 | } | |
411 | String gtkVersion = System.getProperty("org.eclipse.swt.internal.gtk.version"); | |
412 | if (gtkVersion != null) { | |
413 | System.out.println("GTK version=" + gtkVersion); | |
b6fddb83 MK |
414 | // Try to print the GTK theme information as behavior can change |
415 | // depending on the theme | |
7df209fa MAL |
416 | String gtkTheme = System.getProperty("org.eclipse.swt.internal.gtk.theme"); |
417 | System.out.println("GTK theme=" + (gtkTheme == null ? "unknown" : gtkTheme)); | |
418 | ||
2ad409b3 MAL |
419 | String overlayScrollbar = System.getenv("LIBOVERLAY_SCROLLBAR"); |
420 | if (overlayScrollbar != null) { | |
421 | System.out.println("LIBOVERLAY_SCROLLBAR=" + overlayScrollbar); | |
422 | } | |
423 | String ubuntuMenuProxy = System.getenv("UBUNTU_MENUPROXY"); | |
424 | if (ubuntuMenuProxy != null) { | |
425 | System.out.println("UBUNTU_MENUPROXY=" + ubuntuMenuProxy); | |
426 | } | |
427 | } | |
428 | ||
6941941d MAL |
429 | System.out.println("Time zone: " + TimeZone.getDefault().getDisplayName()); |
430 | ||
2ad409b3 MAL |
431 | fPrintedEnvironment = true; |
432 | } | |
433 | ||
306e18d0 MK |
434 | /** |
435 | * If the test is running in the UI thread then fail | |
436 | */ | |
5785ab49 | 437 | private static void failIfUIThread() { |
306e18d0 MK |
438 | if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) { |
439 | 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" | |
440 | + " that useUIThread is set to false in the pom.xml"); | |
441 | } | |
5785ab49 | 442 | } |
306e18d0 | 443 | |
5785ab49 MAL |
444 | /** |
445 | * Try to make the shell fully visible in the display. If the shell cannot | |
446 | * fit the display, it will be positioned so that top-left corner is at | |
447 | * <code>(0, 0)</code> in display-relative coordinates. | |
448 | * | |
449 | * @param shell | |
450 | * the shell to make fully visible | |
451 | */ | |
452 | private static void makeShellFullyVisible(Shell shell) { | |
453 | Rectangle displayBounds = shell.getDisplay().getBounds(); | |
454 | Point absCoord = shell.toDisplay(0, 0); | |
455 | Point shellSize = shell.getSize(); | |
456 | ||
457 | Point newLocation = new Point(absCoord.x, absCoord.y); | |
458 | newLocation.x = Math.max(0, Math.min(absCoord.x, displayBounds.width - shellSize.x)); | |
459 | newLocation.y = Math.max(0, Math.min(absCoord.y, displayBounds.height - shellSize.y)); | |
460 | if (!newLocation.equals(absCoord)) { | |
461 | shell.setLocation(newLocation); | |
462 | } | |
306e18d0 | 463 | } |
7777d5f0 MK |
464 | |
465 | /** | |
466 | * Open a trace, this does not perform any validation though | |
467 | * | |
468 | * @param projectName | |
469 | * The project name | |
470 | * @param tracePath | |
471 | * the path of the trace file (absolute or relative) | |
472 | * @param traceType | |
328e5fe4 | 473 | * the trace type id (eg: org.eclipse.linuxtools.btf.trace) |
7777d5f0 MK |
474 | */ |
475 | public static void openTrace(final String projectName, final String tracePath, final String traceType) { | |
a3d7df19 BH |
476 | openTrace(projectName, tracePath, traceType, true); |
477 | } | |
478 | ||
479 | /** | |
480 | * Open a trace, this does not perform any validation though | |
481 | * | |
482 | * @param projectName | |
483 | * The project name | |
484 | * @param tracePath | |
485 | * the path of the trace file (absolute or relative) | |
486 | * @param traceType | |
328e5fe4 | 487 | * the trace type id (eg: org.eclipse.linuxtools.btf.trace) |
a3d7df19 BH |
488 | * @param delay |
489 | * delay and wait for jobs | |
490 | */ | |
491 | public static void openTrace(final String projectName, final String tracePath, final String traceType, boolean delay) { | |
e0a64bd9 | 492 | IStatus status = UIThreadRunnable.syncExec(new Result<IStatus>() { |
7777d5f0 | 493 | @Override |
e0a64bd9 | 494 | public IStatus run() { |
7777d5f0 MK |
495 | try { |
496 | IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); | |
497 | TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder(); | |
e0a64bd9 | 498 | return TmfOpenTraceHelper.openTraceFromPath(destinationFolder, tracePath, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType); |
7777d5f0 | 499 | } catch (CoreException e) { |
e0a64bd9 | 500 | return new Status(IStatus.ERROR, "", e.getMessage(), e); |
7777d5f0 MK |
501 | } |
502 | } | |
503 | }); | |
e0a64bd9 MK |
504 | if (!status.isOK()) { |
505 | fail(status.getMessage()); | |
7777d5f0 MK |
506 | } |
507 | ||
a3d7df19 BH |
508 | if (delay) { |
509 | delay(1000); | |
f0beeb4a | 510 | WaitUtils.waitForJobs(); |
a3d7df19 | 511 | } |
7777d5f0 | 512 | } |
693ec829 BH |
513 | |
514 | /** | |
b4290931 | 515 | * Finds an editor and sets focus to the editor |
693ec829 BH |
516 | * |
517 | * @param bot | |
518 | * the workbench bot | |
519 | * @param editorName | |
520 | * the editor name | |
521 | * @return the corresponding SWTBotEditor | |
522 | */ | |
b4290931 | 523 | public static SWTBotEditor activateEditor(SWTWorkbenchBot bot, String editorName) { |
693ec829 BH |
524 | Matcher<IEditorReference> matcher = WidgetMatcherFactory.withPartName(editorName); |
525 | final SWTBotEditor editorBot = bot.editor(matcher); | |
526 | IEditorPart iep = editorBot.getReference().getEditor(true); | |
527 | final TmfEventsEditor tmfEd = (TmfEventsEditor) iep; | |
528 | editorBot.show(); | |
529 | UIThreadRunnable.syncExec(new VoidResult() { | |
530 | @Override | |
531 | public void run() { | |
532 | tmfEd.setFocus(); | |
533 | } | |
534 | }); | |
535 | ||
f0beeb4a | 536 | WaitUtils.waitForJobs(); |
fa24d78b | 537 | SWTBotUtils.delay(1000); |
693ec829 BH |
538 | assertNotNull(tmfEd); |
539 | return editorBot; | |
540 | } | |
b4290931 MAL |
541 | |
542 | /** | |
543 | * Opens a trace in an editor and get the TmfEventsEditor | |
544 | * | |
545 | * @param bot | |
546 | * the workbench bot | |
547 | * @param projectName | |
548 | * the name of the project that contains the trace | |
549 | * @param elementPath | |
550 | * the trace element path (relative to Traces folder) | |
551 | * @return TmfEventsEditor the opened editor | |
552 | */ | |
553 | public static TmfEventsEditor openEditor(SWTWorkbenchBot bot, String projectName, IPath elementPath) { | |
5b6c8456 MAL |
554 | final SWTBotView projectExplorerView = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER); |
555 | projectExplorerView.setFocus(); | |
556 | SWTBot projectExplorerBot = projectExplorerView.bot(); | |
b4290931 | 557 | |
5b6c8456 MAL |
558 | final SWTBotTree tree = projectExplorerBot.tree(); |
559 | projectExplorerBot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(projectName, tree)); | |
b4290931 MAL |
560 | final SWTBotTreeItem treeItem = tree.getTreeItem(projectName); |
561 | treeItem.expand(); | |
562 | ||
5b6c8456 | 563 | SWTBotTreeItem tracesNode = getTraceProjectItem(projectExplorerBot, treeItem, TmfTracesFolder.TRACES_FOLDER_NAME); |
b4290931 MAL |
564 | tracesNode.expand(); |
565 | ||
21e5206c | 566 | SWTBotTreeItem currentItem = tracesNode; |
b4290931 | 567 | for (String segment : elementPath.segments()) { |
5b6c8456 | 568 | currentItem = getTraceProjectItem(projectExplorerBot, currentItem, segment); |
21e5206c | 569 | currentItem.doubleClick(); |
b4290931 MAL |
570 | } |
571 | ||
21e5206c PT |
572 | SWTBotEditor editor = bot.editorByTitle(elementPath.toString()); |
573 | IEditorPart editorPart = editor.getReference().getEditor(false); | |
574 | assertTrue(editorPart instanceof TmfEventsEditor); | |
575 | return (TmfEventsEditor) editorPart; | |
b4290931 MAL |
576 | } |
577 | ||
cdfe10e7 PT |
578 | /** |
579 | * Returns the child tree item of the specified item at the given sub-path. | |
580 | * The project element labels may have a count suffix in the format ' [n]'. | |
581 | * | |
582 | * @param bot | |
583 | * a given workbench bot | |
584 | * @param parentItem | |
585 | * the parent tree item | |
586 | * @param path | |
587 | * the desired child element sub-path (without suffix) | |
588 | * @return the a {@link SWTBotTreeItem} with the specified name | |
589 | */ | |
590 | public static SWTBotTreeItem getTraceProjectItem(SWTBot bot, final SWTBotTreeItem parentItem, final String... path) { | |
591 | SWTBotTreeItem item = parentItem; | |
592 | for (String name : path) { | |
593 | item = getTraceProjectItem(bot, item, name); | |
594 | } | |
595 | return item; | |
596 | } | |
597 | ||
21e5206c PT |
598 | /** |
599 | * Returns the child tree item of the specified item with the given name. | |
600 | * The project element label may have a count suffix in the format ' [n]'. | |
601 | * | |
602 | * @param bot | |
603 | * a given workbench bot | |
604 | * @param parentItem | |
605 | * the parent tree item | |
606 | * @param name | |
607 | * the desired child element name (without suffix) | |
608 | * @return the a {@link SWTBotTreeItem} with the specified name | |
609 | */ | |
5b6c8456 | 610 | public static SWTBotTreeItem getTraceProjectItem(SWTBot bot, final SWTBotTreeItem parentItem, final String name) { |
21e5206c PT |
611 | ProjectElementHasChild condition = new ProjectElementHasChild(parentItem, name); |
612 | bot.waitUntil(condition); | |
613 | return condition.getItem(); | |
b4290931 | 614 | } |
34c0fc10 MK |
615 | |
616 | /** | |
617 | * Select the traces folder | |
618 | * | |
619 | * @param bot | |
620 | * a given workbench bot | |
621 | * @param projectName | |
328e5fe4 MK |
622 | * the name of the project (it needs to exist or else it would |
623 | * time out) | |
21e5206c | 624 | * @return a {@link SWTBotTreeItem} of the "Traces" folder |
34c0fc10 MK |
625 | */ |
626 | public static SWTBotTreeItem selectTracesFolder(SWTWorkbenchBot bot, String projectName) { | |
bbdb3d6d | 627 | SWTBotTreeItem projectTreeItem = selectProject(bot, projectName); |
21e5206c PT |
628 | projectTreeItem.select(); |
629 | SWTBotTreeItem tracesFolderItem = getTraceProjectItem(bot, projectTreeItem, TmfTracesFolder.TRACES_FOLDER_NAME); | |
630 | tracesFolderItem.select(); | |
631 | return tracesFolderItem; | |
34c0fc10 | 632 | } |
2470d687 | 633 | |
29fe7911 MAL |
634 | /** |
635 | * Clear the traces folder | |
636 | * | |
637 | * @param bot | |
638 | * a given workbench bot | |
639 | * @param projectName | |
640 | * the name of the project (needs to exist) | |
641 | */ | |
642 | public static void clearTracesFolder(SWTWorkbenchBot bot, String projectName) { | |
643 | IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); | |
644 | TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, false); | |
645 | TmfTraceFolder tracesFolder = tmfProject.getTracesFolder(); | |
a485bc7f BH |
646 | if (tracesFolder == null) { |
647 | return; | |
648 | } | |
29fe7911 MAL |
649 | try { |
650 | for (TmfTraceElement traceElement : tracesFolder.getTraces()) { | |
651 | traceElement.delete(null); | |
652 | } | |
653 | ||
654 | final IFolder resource = tracesFolder.getResource(); | |
655 | resource.accept(new IResourceVisitor() { | |
656 | @Override | |
657 | public boolean visit(IResource visitedResource) throws CoreException { | |
658 | if (visitedResource != resource) { | |
659 | visitedResource.delete(true, null); | |
660 | } | |
661 | return true; | |
662 | } | |
663 | }, IResource.DEPTH_ONE, 0); | |
664 | } catch (CoreException e) { | |
665 | fail(e.getMessage()); | |
666 | } | |
667 | ||
668 | bot.waitUntil(new DefaultCondition() { | |
669 | private int fTraceNb = 0; | |
670 | ||
671 | @Override | |
672 | public boolean test() throws Exception { | |
673 | List<TmfTraceElement> traces = tracesFolder.getTraces(); | |
674 | fTraceNb = traces.size(); | |
675 | return fTraceNb == 0; | |
676 | } | |
677 | ||
678 | @Override | |
679 | public String getFailureMessage() { | |
680 | return "Traces Folder not empty (" + fTraceNb + ")"; | |
681 | } | |
682 | }); | |
40ba27e1 BH |
683 | } |
684 | ||
450daec8 MAL |
685 | /** |
686 | * Clear the trace folder (using the UI) | |
687 | * | |
688 | * @param bot | |
689 | * a given workbench bot | |
690 | * @param projectName | |
691 | * the name of the project (needs to exist) | |
692 | */ | |
693 | public static void clearTracesFolderUI(SWTWorkbenchBot bot, String projectName) { | |
694 | SWTBotTreeItem tracesFolder = selectTracesFolder(bot, projectName); | |
695 | tracesFolder.contextMenu().menu("Clear").click(); | |
696 | String CONFIRM_CLEAR_DIALOG_TITLE = "Confirm Clear"; | |
697 | bot.waitUntil(Conditions.shellIsActive(CONFIRM_CLEAR_DIALOG_TITLE)); | |
698 | ||
699 | SWTBotShell shell = bot.shell(CONFIRM_CLEAR_DIALOG_TITLE); | |
700 | shell.bot().button("Yes").click(); | |
701 | bot.waitUntil(Conditions.shellCloses(shell)); | |
702 | bot.waitWhile(ConditionHelpers.treeItemHasChildren(tracesFolder)); | |
703 | } | |
704 | ||
40ba27e1 BH |
705 | /** |
706 | * Clear the experiment folder | |
707 | * | |
708 | * @param bot | |
709 | * a given workbench bot | |
710 | * @param projectName | |
711 | * the name of the project (needs to exist) | |
712 | */ | |
713 | public static void clearExperimentFolder(SWTWorkbenchBot bot, String projectName) { | |
714 | IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); | |
715 | TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, false); | |
716 | TmfExperimentFolder expFolder = tmfProject.getExperimentsFolder(); | |
a485bc7f BH |
717 | if (expFolder == null) { |
718 | return; | |
719 | } | |
40ba27e1 BH |
720 | expFolder.getExperiments().forEach(experiment -> { |
721 | IResource resource = experiment.getResource(); | |
722 | try { | |
723 | // Close the experiment if open | |
724 | experiment.closeEditors(); | |
725 | ||
726 | IPath path = resource.getLocation(); | |
727 | if (path != null) { | |
728 | // Delete supplementary files | |
729 | experiment.deleteSupplementaryFolder(); | |
730 | } | |
731 | // Finally, delete the experiment | |
732 | resource.delete(true, null); | |
733 | } catch (CoreException e) { | |
734 | fail(e.getMessage()); | |
735 | } | |
736 | }); | |
737 | ||
738 | bot.waitUntil(new DefaultCondition() { | |
739 | private int fExperimentNb = 0; | |
29fe7911 | 740 | |
40ba27e1 BH |
741 | @Override |
742 | public boolean test() throws Exception { | |
743 | List<TmfExperimentElement> experiments = expFolder.getExperiments(); | |
744 | fExperimentNb = experiments.size(); | |
745 | return fExperimentNb == 0; | |
746 | } | |
747 | ||
748 | @Override | |
749 | public String getFailureMessage() { | |
750 | return "Experiment Folder not empty (" + fExperimentNb + ")"; | |
751 | } | |
752 | }); | |
29fe7911 MAL |
753 | } |
754 | ||
bbdb3d6d MAL |
755 | /** |
756 | * Select the project in Project Explorer | |
757 | * | |
758 | * @param bot | |
759 | * a given workbench bot | |
760 | * @param projectName | |
b6fddb83 MK |
761 | * the name of the project (it needs to exist or else it would |
762 | * time out) | |
bbdb3d6d MAL |
763 | * @return a {@link SWTBotTreeItem} of the project |
764 | */ | |
765 | public static SWTBotTreeItem selectProject(SWTWorkbenchBot bot, String projectName) { | |
766 | SWTBotView projectExplorerBot = bot.viewByTitle("Project Explorer"); | |
767 | projectExplorerBot.show(); | |
e834a6b4 MAL |
768 | // FIXME: Bug 496519. Sometimes, the tree becomes disabled for a certain |
769 | // amount of time. This can happen during a long running operation | |
770 | // (BusyIndicator.showWhile) which brings up the modal dialog "operation | |
771 | // in progress" and this disables all shells | |
772 | projectExplorerBot.bot().waitUntil(Conditions.widgetIsEnabled(projectExplorerBot.bot().tree())); | |
bbdb3d6d MAL |
773 | SWTBotTreeItem treeItem = projectExplorerBot.bot().tree().getTreeItem(projectName); |
774 | treeItem.select(); | |
775 | return treeItem; | |
776 | } | |
777 | ||
2470d687 MK |
778 | /** |
779 | * Open a view by id. | |
780 | * | |
781 | * @param id | |
782 | * view id. | |
783 | */ | |
784 | public static void openView(final String id) { | |
43439230 GB |
785 | openView(id, null); |
786 | } | |
787 | ||
788 | /** | |
789 | * Open a view by id and secondary id | |
790 | * | |
791 | * @param id | |
792 | * view id. | |
793 | * @param secondaryId | |
794 | * The secondary ID | |
795 | */ | |
796 | public static void openView(final String id, final @Nullable String secondaryId) { | |
2470d687 MK |
797 | final PartInitException res[] = new PartInitException[1]; |
798 | UIThreadRunnable.syncExec(new VoidResult() { | |
799 | @Override | |
800 | public void run() { | |
801 | try { | |
43439230 GB |
802 | if (secondaryId == null) { |
803 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id); | |
804 | } else { | |
805 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id, secondaryId, IWorkbenchPage.VIEW_ACTIVATE); | |
806 | } | |
2470d687 MK |
807 | } catch (PartInitException e) { |
808 | res[0] = e; | |
809 | } | |
810 | } | |
811 | }); | |
812 | if (res[0] != null) { | |
813 | fail(res[0].getMessage()); | |
814 | } | |
f0beeb4a | 815 | WaitUtils.waitForJobs(); |
2470d687 | 816 | } |
328e5fe4 MK |
817 | |
818 | /** | |
819 | * Maximize a table | |
820 | * | |
821 | * @param tableBot | |
822 | * the {@link SWTBotTable} table | |
823 | */ | |
824 | public static void maximizeTable(SWTBotTable tableBot) { | |
675b1249 PT |
825 | final AtomicBoolean controlResized = new AtomicBoolean(); |
826 | UIThreadRunnable.syncExec(new VoidResult() { | |
827 | @Override | |
828 | public void run() { | |
829 | tableBot.widget.addControlListener(new ControlAdapter() { | |
830 | @Override | |
831 | public void controlResized(ControlEvent e) { | |
832 | tableBot.widget.removeControlListener(this); | |
833 | controlResized.set(true); | |
834 | } | |
835 | }); | |
836 | } | |
837 | }); | |
328e5fe4 MK |
838 | try { |
839 | tableBot.pressShortcut(KeyStroke.getInstance(IKeyLookup.CTRL_NAME + "+"), KeyStroke.getInstance("M")); | |
840 | } catch (ParseException e) { | |
841 | fail(); | |
842 | } | |
675b1249 PT |
843 | new SWTBot().waitUntil(new DefaultCondition() { |
844 | @Override | |
845 | public boolean test() throws Exception { | |
846 | return controlResized.get(); | |
847 | } | |
848 | ||
849 | @Override | |
850 | public String getFailureMessage() { | |
851 | return "Control was not resized"; | |
852 | } | |
853 | }); | |
328e5fe4 MK |
854 | } |
855 | ||
4045095f PT |
856 | /** |
857 | * Get the bounds of a widget in display coordinates | |
858 | * | |
859 | * @param bot | |
860 | * the widget bot | |
861 | * @return the widget bounds, in display coordinates | |
862 | */ | |
863 | public static Rectangle getBoundsToDisplay(AbstractSWTBot<?> bot) { | |
864 | if (bot.widget instanceof Control) { | |
865 | final Control control = checkNotNull((Control) bot.widget); | |
866 | return UIThreadRunnable.syncExec(new Result<Rectangle>() { | |
867 | @Override | |
868 | public Rectangle run() { | |
869 | Point location = control.toDisplay(0, 0); | |
870 | Point size = control.getSize(); | |
871 | return new Rectangle(location.x, location.y, size.x, size.y); | |
872 | } | |
873 | }); | |
874 | } | |
875 | throw new IllegalArgumentException(bot +" is not a Control widget"); | |
876 | } | |
877 | ||
328e5fe4 MK |
878 | /** |
879 | * Get the bounds of a cell (SWT.Rectangle) for the specified row and column | |
880 | * index in a table | |
881 | * | |
882 | * @param table | |
883 | * the table | |
884 | * @param row | |
885 | * the row of the table to look up | |
886 | * @param col | |
887 | * the column of the table to look up | |
888 | * @return the bounds in display relative coordinates | |
889 | */ | |
890 | public static Rectangle getCellBounds(final Table table, final int row, final int col) { | |
891 | return UIThreadRunnable.syncExec(new Result<Rectangle>() { | |
892 | @Override | |
893 | public Rectangle run() { | |
894 | TableItem item = table.getItem(row); | |
895 | Rectangle bounds = item.getBounds(col); | |
896 | Point p = table.toDisplay(bounds.x, bounds.y); | |
897 | Rectangle rect = new Rectangle(p.x, p.y, bounds.width, bounds.height); | |
898 | return rect; | |
899 | } | |
900 | }); | |
901 | } | |
bbdb3d6d MAL |
902 | |
903 | /** | |
904 | * Get the tree item from a tree at the specified location | |
905 | * | |
906 | * @param bot | |
907 | * the SWTBot | |
908 | * @param tree | |
909 | * the tree to find the tree item in | |
910 | * @param nodeNames | |
911 | * the path to the tree item, in the form of node names (from | |
912 | * parent to child). | |
913 | * @return the tree item | |
914 | */ | |
915 | public static SWTBotTreeItem getTreeItem(SWTBot bot, SWTBotTree tree, String... nodeNames) { | |
916 | if (nodeNames.length == 0) { | |
917 | return null; | |
918 | } | |
919 | ||
920 | bot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(nodeNames[0], tree)); | |
921 | SWTBotTreeItem currentNode = tree.getTreeItem(nodeNames[0]); | |
1b9a8a8c MAL |
922 | return getTreeItem(bot, currentNode, Arrays.copyOfRange(nodeNames, 1, nodeNames.length)); |
923 | } | |
924 | ||
925 | /** | |
926 | * Get the tree item from a parent tree item at the specified location | |
927 | * | |
928 | * @param bot | |
929 | * the SWTBot | |
930 | * @param treeItem | |
931 | * the treeItem to find the tree item under | |
932 | * @param nodeNames | |
933 | * the path to the tree item, in the form of node names (from | |
934 | * parent to child). | |
935 | * @return the tree item | |
936 | */ | |
937 | public static SWTBotTreeItem getTreeItem(SWTBot bot, SWTBotTreeItem treeItem, String... nodeNames) { | |
938 | if (nodeNames.length == 0) { | |
939 | return treeItem; | |
940 | } | |
941 | ||
942 | SWTBotTreeItem currentNode = treeItem; | |
943 | for (int i = 0; i < nodeNames.length; i++) { | |
944 | bot.waitUntil(ConditionHelpers.treeItemHasChildren(treeItem)); | |
bbdb3d6d MAL |
945 | currentNode.expand(); |
946 | ||
947 | String nodeName = nodeNames[i]; | |
65416c44 MAL |
948 | try { |
949 | bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, currentNode)); | |
950 | } catch (TimeoutException e) { | |
b6fddb83 MK |
951 | // FIXME: Sometimes in a JFace TreeViewer, it expands to |
952 | // nothing. Need to find out why. | |
65416c44 MAL |
953 | currentNode.collapse(); |
954 | currentNode.expand(); | |
955 | bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, currentNode)); | |
956 | } | |
957 | ||
bbdb3d6d MAL |
958 | SWTBotTreeItem newNode = currentNode.getNode(nodeName); |
959 | currentNode = newNode; | |
960 | } | |
961 | ||
962 | return currentNode; | |
963 | } | |
a345ad25 | 964 | |
4045095f PT |
965 | /** |
966 | * Press the shortcut specified by the given keys. The method returns when | |
967 | * the key events have been received by the focus control. | |
968 | * | |
969 | * @param keyboard | |
970 | * the keyboard | |
971 | * @param keys | |
972 | * the keys to press | |
973 | */ | |
974 | public static void pressShortcut(Keyboard keyboard, KeyStroke... keys) { | |
975 | Control focusControl = UIThreadRunnable.syncExec(new Result<Control>() { | |
976 | @Override | |
977 | public Control run() { | |
978 | return Display.getCurrent().getFocusControl(); | |
979 | } | |
980 | }); | |
981 | pressShortcut(focusControl, () -> keyboard.pressShortcut(keys), keys); | |
982 | } | |
983 | ||
984 | /** | |
985 | * Press the shortcut specified by the given keys. The method returns when | |
986 | * the key events have been received by the given control. | |
987 | * | |
988 | * @param bot | |
989 | * the control bot | |
990 | * @param keys | |
991 | * the keys to press | |
992 | */ | |
993 | public static void pressShortcut(AbstractSWTBotControl<?> bot, KeyStroke... keys) { | |
994 | pressShortcut(bot.widget, () -> bot.pressShortcut(keys), keys); | |
995 | } | |
996 | ||
997 | private static void pressShortcut(Control control, Runnable pressShortcut, KeyStroke... keys) { | |
998 | AtomicInteger keysPressed = new AtomicInteger(); | |
999 | AtomicInteger keysReleased = new AtomicInteger(); | |
1000 | KeyListener keyListener = new KeyListener() { | |
1001 | @Override | |
1002 | public void keyPressed(KeyEvent e) { | |
1003 | keysPressed.incrementAndGet(); | |
1004 | } | |
1005 | @Override | |
1006 | public void keyReleased(KeyEvent e) { | |
1007 | keysReleased.incrementAndGet(); | |
1008 | } | |
1009 | }; | |
1010 | UIThreadRunnable.syncExec(() -> control.addKeyListener(keyListener)); | |
1011 | pressShortcut.run(); | |
1012 | new SWTBot().waitUntil(new DefaultCondition() { | |
1013 | @Override | |
1014 | public boolean test() throws Exception { | |
1015 | return keysPressed.get() > 0 && keysPressed.get() == keysReleased.get(); | |
1016 | } | |
1017 | @Override | |
1018 | public String getFailureMessage() { | |
1019 | return "key press " + Arrays.toString(keys) + " not detected"; | |
1020 | } | |
1021 | }); | |
1022 | UIThreadRunnable.syncExec(() -> control.removeKeyListener(keyListener)); | |
1023 | } | |
1024 | ||
1c9b6343 MAL |
1025 | /** |
1026 | * Press the keyboard shortcut that goes to the top of a tree widget. The | |
1027 | * key combination can differ on different platforms. | |
1028 | * | |
1029 | * @param keyboard | |
1030 | * the keyboard to use | |
1031 | */ | |
1032 | public static void pressShortcutGoToTreeTop(Keyboard keyboard) { | |
1033 | if (SWTUtils.isMac()) { | |
4045095f | 1034 | pressShortcut(keyboard, Keystrokes.ALT, Keystrokes.UP); |
1c9b6343 | 1035 | } else { |
4045095f | 1036 | pressShortcut(keyboard, Keystrokes.HOME); |
1c9b6343 MAL |
1037 | } |
1038 | } | |
1039 | ||
a345ad25 MAL |
1040 | /** |
1041 | * Get the active events editor. Note that this will wait until such editor | |
1042 | * is available. | |
1043 | * | |
1044 | * @param workbenchBot | |
1045 | * a given workbench bot | |
1046 | * @return the active events editor | |
1047 | */ | |
1048 | public static SWTBotEditor activeEventsEditor(final SWTWorkbenchBot workbenchBot) { | |
e834a6b4 MAL |
1049 | ConditionHelpers.ActiveEventsEditor condition = new ConditionHelpers.ActiveEventsEditor(workbenchBot, null); |
1050 | workbenchBot.waitUntil(condition); | |
1051 | return condition.getActiveEditor(); | |
1052 | } | |
a345ad25 | 1053 | |
e834a6b4 MAL |
1054 | /** |
1055 | * Get the active events editor. Note that this will wait until such editor | |
1056 | * is available. | |
1057 | * | |
1058 | * @param workbenchBot | |
1059 | * a given workbench bot | |
1060 | * @param editorTitle | |
1061 | * the desired editor title. If null, any active events editor | |
1062 | * will be considered valid. | |
1063 | * @return the active events editor | |
1064 | */ | |
1065 | public static SWTBotEditor activeEventsEditor(final SWTWorkbenchBot workbenchBot, String editorTitle) { | |
1066 | ConditionHelpers.ActiveEventsEditor condition = new ConditionHelpers.ActiveEventsEditor(workbenchBot, editorTitle); | |
1067 | workbenchBot.waitUntil(condition); | |
1068 | return condition.getActiveEditor(); | |
a345ad25 | 1069 | } |
747d62e1 MAL |
1070 | |
1071 | /** | |
f715581c MAL |
1072 | * Open the preferences dialog and return the corresponding shell. See also |
1073 | * {@link #pressOKishButtonInPreferences(SWTBot)} to close the dialog. | |
747d62e1 MAL |
1074 | * |
1075 | * @param bot | |
1076 | * a given workbench bot | |
1077 | * @return the preferences shell | |
1078 | */ | |
1079 | public static SWTBotShell openPreferences(SWTBot bot) { | |
1080 | if (SWTUtils.isMac()) { | |
1081 | // On Mac, the Preferences menu item is under the application name. | |
1082 | // For some reason, we can't access the application menu anymore so | |
1083 | // we use the keyboard shortcut. | |
1084 | try { | |
1085 | bot.activeShell().pressShortcut(KeyStroke.getInstance(IKeyLookup.COMMAND_NAME + "+"), KeyStroke.getInstance(",")); | |
1086 | } catch (ParseException e) { | |
1087 | fail(); | |
1088 | } | |
1089 | } else { | |
1090 | bot.menu(WINDOW_MENU).menu(PREFERENCES_MENU_ITEM).click(); | |
1091 | } | |
1092 | ||
1093 | bot.waitUntil(Conditions.shellIsActive(PREFERENCES_MENU_ITEM)); | |
1094 | return bot.activeShell(); | |
1095 | } | |
b1d4e193 | 1096 | |
f715581c MAL |
1097 | /** |
1098 | * Click the OK or "Apply and Close" button the preferences dialog. The | |
1099 | * button label changed from OK to "Apply and Close" in Eclipse version | |
1100 | * 4.7-I20170329-2000. | |
1101 | * | |
1102 | * @param bot | |
1103 | * a given workbench bot | |
1104 | */ | |
1105 | public static void pressOKishButtonInPreferences(SWTBot bot) { | |
1106 | try { | |
1107 | String okIshLabel = "Apply and Close"; | |
1108 | // We do it this more manual way in order to not have to timout and | |
1109 | // wait 30 secs when the button is not there | |
1110 | bot.waitUntil(Conditions.waitForWidget(withMnemonic(okIshLabel)), 100); | |
1111 | bot.button(okIshLabel).click(); | |
1112 | } catch (TimeoutException e) { | |
1113 | // Doesn't exist pre-4.7-I20170329-2000, try old "OK" button | |
1114 | bot.button("OK").click(); | |
1115 | } | |
1116 | } | |
1117 | ||
b1d4e193 MK |
1118 | /** |
1119 | * Maximize a view by reference. Calling this a second time will "un-maximize" a view. | |
1120 | * <p> | |
1121 | * TODO: if this is useful, maybe uplift to SWTViewBot | |
1122 | * | |
1123 | * @param view | |
1124 | * the view reference | |
1125 | */ | |
1126 | public static void maximize(@NonNull IViewPart view) { | |
1127 | assertNotNull(view); | |
1128 | IWorkbenchPartSite site = view.getSite(); | |
1129 | assertNotNull(site); | |
1130 | // The annotation is to make the compiler not complain. | |
1131 | @Nullable | |
1132 | Object handlerServiceObject = site.getService(IHandlerService.class); | |
1133 | assertTrue(handlerServiceObject instanceof IHandlerService); | |
1134 | IHandlerService handlerService = (IHandlerService) handlerServiceObject; | |
1135 | try { | |
1136 | handlerService.executeCommand(IWorkbenchCommandConstants.WINDOW_MAXIMIZE_ACTIVE_VIEW_OR_EDITOR, null); | |
1137 | } catch (ExecutionException | NotDefinedException | NotEnabledException | NotHandledException e) { | |
1138 | fail(e.getMessage()); | |
1139 | } | |
1140 | } | |
470aad1b JCK |
1141 | |
1142 | /** | |
1143 | * Get the number of checked items of a tree | |
1144 | * | |
1145 | * @param tree | |
1146 | * The tree bot | |
1147 | * @return The number of checked items | |
1148 | */ | |
1149 | public static int getTreeCheckedItemCount(SWTBotTree tree) { | |
1150 | return UIThreadRunnable.syncExec(new IntResult() { | |
1151 | ||
1152 | @Override | |
1153 | public Integer run() { | |
1154 | int checked = 0; | |
1155 | for (TreeItem item : tree.widget.getItems()) { | |
1156 | checked += getChecked(item); | |
1157 | } | |
1158 | return checked; | |
1159 | } | |
1160 | ||
1161 | private int getChecked(TreeItem item) { | |
1162 | int total = 0; | |
1163 | if (item.getChecked()) { | |
1164 | total++; | |
1165 | } | |
1166 | for (TreeItem child : item.getItems()) { | |
1167 | total += getChecked(child); | |
1168 | } | |
1169 | return total; | |
1170 | } | |
1171 | }); | |
1172 | } | |
4045095f PT |
1173 | |
1174 | /** | |
1175 | * Filter the specified list of items in a time graph view. | |
1176 | * | |
1177 | * @param viewBot | |
1178 | * the view | |
1179 | * @param filterItems | |
1180 | * the list of filter dialog item tree paths | |
1181 | * @param checkSubTree | |
1182 | * true if the filter items sub-trees should be checked, or false | |
1183 | * to only check the filter items | |
1184 | */ | |
1185 | public static void applyTimeGraphFilter(SWTBotView viewBot, List<String[]> filterItems, boolean checkSubTree) { | |
1186 | final String FILTER_ACTION = "Show View Filters"; | |
1187 | final String FILTER_DIALOG_TITLE = "Filter"; | |
1188 | final String UNCHECK_ALL = "Uncheck all"; | |
1189 | final String CHECK_SUBTREE = "Check subtree"; | |
1190 | final String OK_BUTTON = "OK"; | |
1191 | ||
1192 | SWTBotToolbarButton filterButton = viewBot.toolbarButton(FILTER_ACTION); | |
1193 | filterButton.click(); | |
1194 | SWTBotShell shell = viewBot.bot().shell(FILTER_DIALOG_TITLE).activate(); | |
1195 | ||
1196 | SWTBot bot = shell.bot(); | |
1197 | SWTBotTree treeBot = bot.tree(); | |
1198 | bot.button(UNCHECK_ALL).click(); | |
1199 | ||
1200 | for (String[] filterItem : filterItems) { | |
1201 | SWTBotTreeItem item = SWTBotUtils.getTreeItem(bot, treeBot, filterItem); | |
1202 | if (checkSubTree) { | |
1203 | item.select(); | |
1204 | bot.button(CHECK_SUBTREE).click(); | |
1205 | } else { | |
1206 | item.check(); | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | bot.button(OK_BUTTON).click(); | |
1211 | } | |
306e18d0 | 1212 | } |