1 | 12 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.eclipse.swtbot.eclipse.finder.widgets; |
12 | |
|
13 | |
import static org.hamcrest.Matchers.any; |
14 | |
|
15 | |
import java.util.ArrayList; |
16 | |
import java.util.List; |
17 | |
|
18 | |
import org.apache.log4j.Logger; |
19 | |
import org.eclipse.swt.SWT; |
20 | |
import org.eclipse.swt.widgets.Control; |
21 | |
import org.eclipse.swt.widgets.ToolBar; |
22 | |
import org.eclipse.swt.widgets.ToolItem; |
23 | |
import org.eclipse.swt.widgets.Widget; |
24 | |
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; |
25 | |
import org.eclipse.swtbot.eclipse.finder.widgets.utils.PartLabelDescription; |
26 | |
import org.eclipse.swtbot.swt.finder.SWTBot; |
27 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
28 | |
import org.eclipse.swtbot.swt.finder.finders.Finder; |
29 | |
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; |
30 | |
import org.eclipse.swtbot.swt.finder.results.ListResult; |
31 | |
import org.eclipse.swtbot.swt.finder.results.VoidResult; |
32 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
33 | |
import org.eclipse.swtbot.swt.finder.utils.SWTUtils; |
34 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
35 | |
import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton; |
36 | |
import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarDropDownButton; |
37 | |
import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarPushButton; |
38 | |
import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarRadioButton; |
39 | |
import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarSeparatorButton; |
40 | |
import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarToggleButton; |
41 | |
import org.eclipse.ui.IWorkbenchPartReference; |
42 | |
import org.eclipse.ui.PartInitException; |
43 | |
import org.eclipse.ui.internal.PartPane; |
44 | |
import org.eclipse.ui.internal.WorkbenchPartReference; |
45 | |
import org.hamcrest.Matcher; |
46 | |
import org.hamcrest.SelfDescribing; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public abstract class SWTBotWorkbenchPart<T extends IWorkbenchPartReference> { |
58 | |
|
59 | |
|
60 | |
protected final T partReference; |
61 | |
|
62 | |
protected final Logger log; |
63 | |
|
64 | |
protected final SWTWorkbenchBot bot; |
65 | |
private final SelfDescribing description; |
66 | |
private Widget widget; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public SWTBotWorkbenchPart(T partReference, SWTWorkbenchBot bot) { |
75 | 42 | this(partReference, bot, new PartLabelDescription<T>(partReference)); |
76 | 42 | } |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | 60 | public SWTBotWorkbenchPart(T partReference, SWTWorkbenchBot bot, SelfDescribing description) { |
86 | 60 | this.bot = bot; |
87 | 60 | if (description == null ) |
88 | 0 | description = new PartLabelDescription<T>(partReference); |
89 | 60 | this.description = description; |
90 | 60 | Assert.isNotNull(partReference, "The part reference cannot be null"); |
91 | 60 | this.partReference = partReference; |
92 | 60 | log = Logger.getLogger(getClass()); |
93 | 60 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public T getReference() { |
99 | 7 | return partReference; |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public abstract void close(); |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public void show() { |
111 | 2 | UIThreadRunnable.syncExec(new VoidResult() { |
112 | |
public void run() { |
113 | |
try { |
114 | 2 | partReference.getPage().activate(partReference.getPart(true)); |
115 | 2 | partReference.getPage().showView(partReference.getId()); |
116 | 0 | } catch (PartInitException e) { |
117 | 0 | throw new RuntimeException("Could not show partReference - " + partReference.getPartName(), e); |
118 | |
} |
119 | 2 | } |
120 | |
}); |
121 | 2 | } |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public String getTitle() { |
129 | 1 | return partReference.getPartName(); |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public List<SWTBotToolbarButton> getToolbarButtons() { |
138 | 6 | return UIThreadRunnable.syncExec(new ListResult<SWTBotToolbarButton>() { |
139 | |
|
140 | |
public List<SWTBotToolbarButton> run() { |
141 | 6 | PartPane obj = ((WorkbenchPartReference) partReference).getPane(); |
142 | 6 | ToolBar toolbar = (ToolBar) obj.getToolBar(); |
143 | 6 | final List<SWTBotToolbarButton> l = new ArrayList<SWTBotToolbarButton>(); |
144 | |
|
145 | 6 | if (toolbar == null) |
146 | 0 | return l; |
147 | |
|
148 | 6 | ToolItem[] items = toolbar.getItems(); |
149 | 6 | log.debug("number of items : " + items.length); |
150 | 30 | for (int i = 0; i < items.length; i++) { |
151 | |
try { |
152 | 24 | if (SWTUtils.hasStyle(items[i], SWT.PUSH)) |
153 | 6 | l.add(new SWTBotToolbarPushButton(items[i])); |
154 | 18 | else if(SWTUtils.hasStyle(items[i], SWT.CHECK)) |
155 | 6 | l.add(new SWTBotToolbarToggleButton(items[i])); |
156 | 12 | else if(SWTUtils.hasStyle(items[i], SWT.RADIO)) |
157 | 6 | l.add(new SWTBotToolbarRadioButton(items[i])); |
158 | 6 | else if(SWTUtils.hasStyle(items[i], SWT.DROP_DOWN)) |
159 | 6 | l.add(new SWTBotToolbarDropDownButton(items[i])); |
160 | 0 | else if(SWTUtils.hasStyle(items[i], SWT.SEPARATOR)) |
161 | 0 | l.add(new SWTBotToolbarSeparatorButton(items[i])); |
162 | 0 | } catch (WidgetNotFoundException e) { |
163 | 0 | log.warn("Failed to find widget " + items[i].getText(), e); |
164 | |
} |
165 | |
} |
166 | |
|
167 | 6 | return l; |
168 | |
|
169 | |
} |
170 | |
}); |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public SWTBotToolbarDropDownButton toolbarDropDownButton(String tooltip) throws WidgetNotFoundException { |
181 | 1 | SWTBotToolbarButton abstractButton = toolbarButton(tooltip); |
182 | 1 | if (abstractButton instanceof SWTBotToolbarDropDownButton) |
183 | 1 | return (SWTBotToolbarDropDownButton) abstractButton; |
184 | |
|
185 | 0 | throw new WidgetNotFoundException("Unable to find toolitem with the given tooltip '" + tooltip + "'"); |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public SWTBotToolbarRadioButton toolbarRadioButton(String tooltip) throws WidgetNotFoundException { |
196 | 1 | SWTBotToolbarButton abstractButton = toolbarButton(tooltip); |
197 | 1 | if (abstractButton instanceof SWTBotToolbarRadioButton) |
198 | 1 | return (SWTBotToolbarRadioButton) abstractButton; |
199 | |
|
200 | 0 | throw new WidgetNotFoundException("Unable to find toolitem with the given tooltip '" + tooltip + "'"); |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
public SWTBotToolbarPushButton toolbarPushButton(String tooltip) throws WidgetNotFoundException { |
211 | 0 | SWTBotToolbarButton abstractButton = toolbarButton(tooltip); |
212 | 0 | if (abstractButton instanceof SWTBotToolbarPushButton) |
213 | 0 | return (SWTBotToolbarPushButton) abstractButton; |
214 | |
|
215 | 0 | throw new WidgetNotFoundException("Unable to find toolitem with the given tooltip '" + tooltip + "'"); |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public SWTBotToolbarToggleButton toolbarToggleButton(String tooltip) throws WidgetNotFoundException { |
226 | 1 | SWTBotToolbarButton abstractButton = toolbarButton(tooltip); |
227 | 1 | if (abstractButton instanceof SWTBotToolbarToggleButton) |
228 | 1 | return (SWTBotToolbarToggleButton) abstractButton; |
229 | |
|
230 | 0 | throw new WidgetNotFoundException("Unable to find toolitem with the given tooltip '" + tooltip + "'"); |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
public SWTBotToolbarButton toolbarButton(String tooltip) throws WidgetNotFoundException { |
241 | 5 | List<SWTBotToolbarButton> l = getToolbarButtons(); |
242 | |
|
243 | 15 | for (int i = 0; i < l.size(); i++) { |
244 | 14 | SWTBotToolbarButton item = l.get(i); |
245 | 14 | if (item.getToolTipText().equals(tooltip)) { |
246 | 4 | return item; |
247 | |
} |
248 | |
} |
249 | |
|
250 | 1 | throw new WidgetNotFoundException("Unable to find toolitem with the given tooltip '" + tooltip + "'"); |
251 | |
} |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
protected <S extends Widget> S findWidget(Matcher<S> matcher) { |
258 | 16 | return findWidgets(matcher).get(0); |
259 | |
} |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
protected <S extends Widget> List<? extends S> findWidgets(Matcher<S> matcher) { |
266 | 19 | Finder finder = bot.getFinder(); |
267 | 19 | Control control = getControl(); |
268 | 19 | boolean shouldFindInvisibleControls = finder.shouldFindInvisibleControls(); |
269 | 19 | finder.setShouldFindInvisibleControls(true); |
270 | |
try { |
271 | 38 | return bot.widgets(matcher, control); |
272 | 0 | } catch (Exception e) { |
273 | 0 | throw new WidgetNotFoundException("Could not find any control inside the view " + partReference.getPartName(), e); |
274 | 0 | } finally { |
275 | 19 | finder.setShouldFindInvisibleControls(shouldFindInvisibleControls); |
276 | 0 | } |
277 | |
} |
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
private Control getControl() { |
285 | 26 | return ((WorkbenchPartReference) partReference).getPane().getControl(); |
286 | |
} |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
public SWTBot bot() { |
294 | 7 | return new SWTBot(getControl()); |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
protected void assertActive() { |
302 | 0 | Assert.isLegal(isActive(), MessageFormat.format("The workbench part {0}is not active", description)); |
303 | 0 | } |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
public abstract boolean isActive(); |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
public abstract void setFocus(); |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
public Widget getWidget() { |
329 | 0 | show(); |
330 | 0 | if (widget == null) |
331 | 0 | widget = findWidget(any(Widget.class)); |
332 | 0 | return widget; |
333 | |
} |
334 | |
|
335 | |
} |