1 | 2 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
package org.eclipse.swtbot.eclipse.finder; |
13 | |
|
14 | |
import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartId; |
15 | |
import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartName; |
16 | |
import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPerspectiveId; |
17 | |
import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPerspectiveLabel; |
18 | |
import static org.eclipse.swtbot.eclipse.finder.waits.Conditions.waitForEditor; |
19 | |
import static org.eclipse.swtbot.eclipse.finder.waits.Conditions.waitForView; |
20 | |
import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec; |
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.List; |
24 | |
|
25 | |
import org.eclipse.swtbot.eclipse.finder.finders.WorkbenchContentsFinder; |
26 | |
import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory; |
27 | |
import org.eclipse.swtbot.eclipse.finder.waits.WaitForEditor; |
28 | |
import org.eclipse.swtbot.eclipse.finder.waits.WaitForView; |
29 | |
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor; |
30 | |
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotMultiPageEditor; |
31 | |
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotPerspective; |
32 | |
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; |
33 | |
import org.eclipse.swtbot.swt.finder.SWTBot; |
34 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
35 | |
import org.eclipse.swtbot.swt.finder.results.Result; |
36 | |
import org.eclipse.ui.IEditorReference; |
37 | |
import org.eclipse.ui.IPerspectiveDescriptor; |
38 | |
import org.eclipse.ui.IViewReference; |
39 | |
import org.eclipse.ui.IWorkbench; |
40 | |
import org.eclipse.ui.PlatformUI; |
41 | |
import org.hamcrest.Matcher; |
42 | |
import org.hamcrest.Matchers; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public class SWTWorkbenchBot extends SWTBot { |
52 | |
|
53 | |
private final WorkbenchContentsFinder workbenchContentsFinder; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 25 | public SWTWorkbenchBot() { |
59 | 25 | workbenchContentsFinder = new WorkbenchContentsFinder(); |
60 | 25 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public SWTBotPerspective perspective(Matcher<?> matcher) { |
70 | 2 | List<IPerspectiveDescriptor> perspectives = workbenchContentsFinder.findPerspectives(matcher); |
71 | 2 | return new SWTBotPerspective(perspectives.get(0), this); |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public SWTBotPerspective perspectiveByLabel(String label) { |
83 | 0 | return perspective(withPerspectiveLabel(label)); |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public SWTBotPerspective perspectiveById(String id) { |
95 | 2 | return perspective(withPerspectiveId(id)); |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public List<SWTBotPerspective> perspectives(Matcher<?> matcher) { |
103 | 0 | List<IPerspectiveDescriptor> perspectives = workbenchContentsFinder.findPerspectives(matcher); |
104 | |
|
105 | 0 | List<SWTBotPerspective> perspectiveBots = new ArrayList<SWTBotPerspective>(); |
106 | 0 | for (IPerspectiveDescriptor perspectiveDescriptor : perspectives) |
107 | 0 | perspectiveBots.add(new SWTBotPerspective(perspectiveDescriptor, this)); |
108 | |
|
109 | 0 | return perspectiveBots; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public List<SWTBotPerspective> perspectives() { |
116 | 0 | return perspectives(Matchers.anything()); |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public SWTBotView view(Matcher<IViewReference> matcher) { |
127 | 22 | WaitForView waitForView = waitForView(matcher); |
128 | 22 | waitUntilWidgetAppears(waitForView); |
129 | 18 | return new SWTBotView(waitForView.get(0), this); |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public SWTBotView viewByTitle(String title) { |
140 | 22 | Matcher<IViewReference> withPartName = withPartName(title); |
141 | 22 | return view(withPartName); |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public SWTBotView viewById(String id) { |
152 | 0 | Matcher<IViewReference> withPartId = withPartId(id); |
153 | 0 | return view(withPartId); |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public List<SWTBotView> views(Matcher<?> matcher) { |
163 | 0 | List<IViewReference> views = workbenchContentsFinder.findViews(matcher); |
164 | |
|
165 | 0 | List<SWTBotView> viewBots = new ArrayList<SWTBotView>(); |
166 | 0 | for (IViewReference viewReference : views) |
167 | 0 | viewBots.add(new SWTBotView(viewReference, this)); |
168 | 0 | return viewBots; |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
public List<SWTBotView> views() { |
175 | 0 | return views(Matchers.anything()); |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public SWTBotView activeView() { |
185 | 0 | IViewReference view = workbenchContentsFinder.findActiveView(); |
186 | 0 | if (view == null) |
187 | 0 | throw new WidgetNotFoundException("There is no active view"); |
188 | 0 | return new SWTBotView(view, this); |
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
public SWTBotEditor editor(Matcher<IEditorReference> matcher) { |
199 | 6 | WaitForEditor waitForEditor = waitForEditor(matcher); |
200 | 6 | waitUntilWidgetAppears(waitForEditor); |
201 | 6 | return new SWTBotEditor(waitForEditor.get(0), this); |
202 | |
} |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
public List<SWTBotEditor> editors(Matcher<?> matcher) { |
209 | 12 | List<IEditorReference> editors = workbenchContentsFinder.findEditors(matcher); |
210 | |
|
211 | 12 | List<SWTBotEditor> editorBots = new ArrayList<SWTBotEditor>(); |
212 | 37 | for (IEditorReference editorReference : editors) |
213 | 13 | editorBots.add(new SWTBotEditor(editorReference, this)); |
214 | |
|
215 | 12 | return editorBots; |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
public List<? extends SWTBotEditor> editors() { |
222 | 12 | return editors(Matchers.anything()); |
223 | |
} |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
public SWTBotEditor editorByTitle(String fileName) { |
233 | 6 | Matcher<IEditorReference> withPartName = withPartName(fileName); |
234 | 6 | return editor(withPartName); |
235 | |
} |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
public SWTBotEditor editorById(String id) { |
245 | 0 | Matcher<IEditorReference> withPartId = withPartId(id); |
246 | 0 | return editor(withPartId); |
247 | |
} |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
public SWTBotEditor activeEditor() { |
256 | 6 | IEditorReference editor = workbenchContentsFinder.findActiveEditor(); |
257 | 6 | if (editor == null) |
258 | 0 | throw new WidgetNotFoundException("There is no active editor"); |
259 | 6 | return new SWTBotEditor(editor, this); |
260 | |
} |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
public SWTBotMultiPageEditor multipageEditor(Matcher<IEditorReference> matcher) { |
270 | 7 | WaitForEditor waitForEditor = waitForEditor(matcher); |
271 | 7 | waitUntilWidgetAppears(waitForEditor); |
272 | 7 | return new SWTBotMultiPageEditor(waitForEditor.get(0), this); |
273 | |
} |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
public List<SWTBotMultiPageEditor> multipageEditors(Matcher<?> matcher) { |
280 | 0 | List<IEditorReference> editors = workbenchContentsFinder.findEditors(matcher); |
281 | |
|
282 | 0 | List<SWTBotMultiPageEditor> editorBots = new ArrayList<SWTBotMultiPageEditor>(); |
283 | 0 | for (IEditorReference editorReference : editors) |
284 | 0 | editorBots.add(new SWTBotMultiPageEditor(editorReference, this)); |
285 | |
|
286 | 0 | return editorBots; |
287 | |
} |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
public List<? extends SWTBotMultiPageEditor> multipageEditors() { |
293 | 0 | return multipageEditors(Matchers.anything()); |
294 | |
} |
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
public SWTBotMultiPageEditor multipageEditorByTitle(String fileName) { |
304 | 7 | Matcher<IEditorReference> withPartName = withPartName(fileName); |
305 | 7 | return multipageEditor(withPartName); |
306 | |
} |
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
public SWTBotMultiPageEditor multipageEditorById(String id) { |
316 | 0 | Matcher<IEditorReference> withPartId = withPartId(id); |
317 | 0 | return multipageEditor(withPartId); |
318 | |
} |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
public SWTBotPerspective activePerspective() { |
324 | 2 | IPerspectiveDescriptor perspective = workbenchContentsFinder.findActivePerspective(); |
325 | 2 | if (perspective == null) |
326 | 0 | throw new WidgetNotFoundException("There is no active perspective"); |
327 | 2 | return new SWTBotPerspective(perspective, this); |
328 | |
} |
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
public void resetWorkbench() { |
341 | 1 | closeAllShells(); |
342 | 1 | saveAllEditors(); |
343 | 1 | closeAllEditors(); |
344 | 1 | resetActivePerspective(); |
345 | |
|
346 | 1 | defaultPerspective().activate(); |
347 | 1 | resetActivePerspective(); |
348 | 1 | } |
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
public SWTBotPerspective defaultPerspective() { |
354 | 1 | return syncExec(new Result<SWTBotPerspective>() { |
355 | |
|
356 | |
public SWTBotPerspective run() { |
357 | 1 | IWorkbench workbench = PlatformUI.getWorkbench(); |
358 | 1 | return perspectiveById(workbench.getPerspectiveRegistry().getDefaultPerspective()); |
359 | |
} |
360 | |
}); |
361 | |
} |
362 | |
|
363 | |
public void closeAllEditors() { |
364 | 1 | new DefaultWorkbench(this).closeAllEditors(); |
365 | 1 | } |
366 | |
|
367 | |
public void saveAllEditors() { |
368 | 1 | new DefaultWorkbench(this).saveAllEditors(); |
369 | 1 | } |
370 | |
|
371 | |
public SWTBotPerspective resetActivePerspective() { |
372 | 2 | new DefaultWorkbench(this).resetActivePerspective(); |
373 | 2 | return activePerspective(); |
374 | |
} |
375 | |
|
376 | |
public void closeAllShells() { |
377 | 1 | new DefaultWorkbench(this).closeAllShells(); |
378 | 1 | } |
379 | |
|
380 | |
} |