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