Commit | Line | Data |
---|---|---|
306e18d0 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Matthew Khouzam - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.swtbot.tests; | |
14 | ||
15 | import static org.junit.Assert.assertNotNull; | |
16 | import static org.junit.Assert.fail; | |
17 | ||
18 | import java.util.List; | |
19 | ||
20 | import org.eclipse.core.resources.IProject; | |
93c91230 MAL |
21 | import org.eclipse.core.resources.IResource; |
22 | import org.eclipse.core.resources.ResourcesPlugin; | |
23 | import org.eclipse.core.runtime.CoreException; | |
306e18d0 MK |
24 | import org.eclipse.core.runtime.NullProgressMonitor; |
25 | import org.eclipse.core.runtime.jobs.Job; | |
7777d5f0 | 26 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfOpenTraceHelper; |
306e18d0 | 27 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry; |
7777d5f0 | 28 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
306e18d0 MK |
29 | import org.eclipse.linuxtools.tmf.ui.swtbot.tests.conditions.ConditionHelpers; |
30 | import org.eclipse.linuxtools.tmf.ui.views.TracingPerspectiveFactory; | |
31 | import org.eclipse.swt.widgets.Display; | |
32 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; | |
33 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; | |
34 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; | |
35 | import org.eclipse.swtbot.swt.finder.results.VoidResult; | |
93c91230 MAL |
36 | import org.eclipse.swtbot.swt.finder.waits.Conditions; |
37 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton; | |
38 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox; | |
39 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu; | |
306e18d0 | 40 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; |
93c91230 MAL |
41 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; |
42 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; | |
43 | import org.eclipse.ui.IPageLayout; | |
306e18d0 MK |
44 | import org.eclipse.ui.PlatformUI; |
45 | import org.eclipse.ui.WorkbenchException; | |
46 | ||
47 | /** | |
48 | * SWTBot Helper functions | |
49 | * | |
50 | * @author Matthew Khouzam | |
51 | */ | |
52 | public abstract class SWTBotUtil { | |
53 | private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID; | |
54 | ||
55 | /** | |
56 | * Waits for all Eclipse jobs to finish | |
57 | */ | |
58 | public static void waitForJobs() { | |
59 | while (!Job.getJobManager().isIdle()) { | |
60 | delay(100); | |
61 | } | |
62 | } | |
63 | ||
64 | /** | |
65 | * Sleeps current thread for a given time. | |
66 | * | |
67 | * @param waitTimeMillis | |
68 | * time in milliseconds to wait | |
69 | */ | |
70 | public static void delay(final long waitTimeMillis) { | |
71 | try { | |
72 | Thread.sleep(waitTimeMillis); | |
73 | } catch (final InterruptedException e) { | |
74 | // Ignored | |
75 | } | |
76 | } | |
77 | ||
78 | /** | |
79 | * Create a tracing project | |
80 | * | |
81 | * @param projectName | |
82 | * the name of the tracing project | |
83 | */ | |
84 | public static void createProject(final String projectName) { | |
85 | /* | |
86 | * Make a new test | |
87 | */ | |
88 | UIThreadRunnable.syncExec(new VoidResult() { | |
89 | @Override | |
90 | public void run() { | |
91 | IProject project = TmfProjectRegistry.createProject(projectName, null, new NullProgressMonitor()); | |
92 | assertNotNull(project); | |
93 | } | |
94 | }); | |
95 | ||
96 | SWTBotUtil.waitForJobs(); | |
97 | } | |
98 | ||
93c91230 MAL |
99 | /** |
100 | * Deletes a tracing project | |
101 | * | |
102 | * @param projectName | |
103 | * the name of the tracing project | |
104 | * @param bot | |
105 | * the workbench bot | |
106 | */ | |
107 | public static void deleteProject(String projectName, SWTWorkbenchBot bot) { | |
7777d5f0 MK |
108 | // Wait for any analysis to complete because it might create |
109 | // supplementary files | |
93c91230 MAL |
110 | SWTBotUtil.waitForJobs(); |
111 | try { | |
112 | ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).refreshLocal(IResource.DEPTH_INFINITE, null); | |
113 | } catch (CoreException e) { | |
114 | } | |
115 | ||
116 | SWTBotUtil.waitForJobs(); | |
117 | ||
118 | final SWTBotView projectViewBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER); | |
119 | projectViewBot.setFocus(); | |
120 | ||
121 | SWTBotTree treeBot = bot.tree(); | |
122 | SWTBotTreeItem treeItem = treeBot.getTreeItem(projectName); | |
123 | SWTBotMenu contextMenu = treeItem.contextMenu("Delete"); | |
124 | contextMenu.click(); | |
125 | ||
126 | bot.shell("Delete Resources").setFocus(); | |
127 | final SWTBotCheckBox checkBox = bot.checkBox(); | |
128 | bot.waitUntil(Conditions.widgetIsEnabled(checkBox)); | |
129 | checkBox.click(); | |
130 | ||
131 | final SWTBotButton okButton = bot.button("OK"); | |
132 | bot.waitUntil(Conditions.widgetIsEnabled(okButton)); | |
133 | okButton.click(); | |
134 | ||
135 | SWTBotUtil.waitForJobs(); | |
136 | } | |
137 | ||
306e18d0 MK |
138 | /** |
139 | * Focus on the main window | |
140 | * | |
141 | * @param shellBots | |
142 | * swtbotshells for all the shells | |
143 | */ | |
144 | public static void focusMainWindow(SWTBotShell[] shellBots) { | |
145 | for (SWTBotShell shellBot : shellBots) { | |
146 | if (shellBot.getText().toLowerCase().contains("eclipse")) { | |
147 | shellBot.activate(); | |
148 | } | |
149 | } | |
150 | } | |
151 | ||
152 | /** | |
153 | * Close a view with a title | |
154 | * | |
155 | * @param title | |
156 | * the title, like "welcome" | |
157 | * @param bot | |
158 | * the workbench bot | |
159 | */ | |
160 | public static void closeView(String title, SWTWorkbenchBot bot) { | |
161 | final List<SWTBotView> openViews = bot.views(); | |
162 | for (SWTBotView view : openViews) { | |
163 | if (view.getTitle().equalsIgnoreCase(title)) { | |
164 | view.close(); | |
165 | bot.waitUntil(ConditionHelpers.ViewIsClosed(view)); | |
166 | } | |
167 | } | |
168 | } | |
169 | ||
170 | /** | |
171 | * Switch to the tracing perspective | |
172 | */ | |
173 | public static void switchToTracingPerspective() { | |
664fa59c MK |
174 | switchToPerspective(TRACING_PERSPECTIVE_ID); |
175 | } | |
176 | ||
177 | /** | |
178 | * Switch to a given perspective | |
179 | * | |
180 | * @param id | |
181 | * the perspective id (like | |
182 | * "org.eclipse.linuxtools.tmf.ui.perspective" | |
183 | */ | |
184 | public static void switchToPerspective(final String id) { | |
306e18d0 MK |
185 | UIThreadRunnable.syncExec(new VoidResult() { |
186 | @Override | |
187 | public void run() { | |
188 | try { | |
664fa59c | 189 | PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow()); |
306e18d0 MK |
190 | } catch (WorkbenchException e) { |
191 | fail(e.getMessage()); | |
192 | } | |
193 | } | |
194 | }); | |
195 | } | |
196 | ||
306e18d0 MK |
197 | /** |
198 | * If the test is running in the UI thread then fail | |
199 | */ | |
200 | public static void failIfUIThread() { | |
201 | if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) { | |
202 | 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" | |
203 | + " that useUIThread is set to false in the pom.xml"); | |
204 | } | |
205 | ||
306e18d0 | 206 | } |
7777d5f0 MK |
207 | |
208 | /** | |
209 | * Open a trace, this does not perform any validation though | |
210 | * | |
211 | * @param projectName | |
212 | * The project name | |
213 | * @param tracePath | |
214 | * the path of the trace file (absolute or relative) | |
215 | * @param traceType | |
216 | * the trace canonical string (eg: | |
217 | * org.eclipse.linuxtools.btf.trace) | |
218 | */ | |
219 | public static void openTrace(final String projectName, final String tracePath, final String traceType) { | |
220 | final Exception exception[] = new Exception[1]; | |
221 | exception[0] = null; | |
222 | UIThreadRunnable.syncExec(new VoidResult() { | |
223 | @Override | |
224 | public void run() { | |
225 | try { | |
226 | IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); | |
227 | TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder(); | |
228 | TmfOpenTraceHelper.openTraceFromPath(destinationFolder, tracePath, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType); | |
229 | } catch (CoreException e) { | |
230 | exception[0] = e; | |
231 | } | |
232 | } | |
233 | }); | |
234 | if (exception[0] != null) { | |
235 | fail(exception[0].getMessage()); | |
236 | } | |
237 | ||
238 | delay(1000); | |
239 | waitForJobs(); | |
240 | } | |
306e18d0 | 241 | } |