Commit | Line | Data |
---|---|---|
fa24d78b | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2013, 2015 Ericsson |
fa24d78b AM |
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 | * Alexandre Montplaisir - Replaced separate Condition objects by anonymous classes | |
88051e61 | 12 | * Patrick Tasse - Add projectElementHasChild and isEditorOpened conditions |
fa24d78b AM |
13 | *******************************************************************************/ |
14 | ||
15 | package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared; | |
16 | ||
17 | ||
88051e61 PT |
18 | import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartName; |
19 | ||
fa24d78b AM |
20 | import org.eclipse.jface.wizard.IWizardContainer; |
21 | import org.eclipse.jface.wizard.IWizardPage; | |
22 | import org.eclipse.jface.wizard.Wizard; | |
21e5206c | 23 | import org.eclipse.osgi.util.NLS; |
88051e61 | 24 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; |
fa24d78b AM |
25 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; |
26 | import org.eclipse.swtbot.swt.finder.SWTBot; | |
6e4a07af | 27 | import org.eclipse.swtbot.swt.finder.utils.TableCollection; |
21e5206c | 28 | import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; |
fa24d78b AM |
29 | import org.eclipse.swtbot.swt.finder.waits.ICondition; |
30 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; | |
31 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; | |
32 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; | |
2fe6a9ea PT |
33 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; |
34 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; | |
88051e61 PT |
35 | import org.eclipse.ui.IEditorReference; |
36 | import org.hamcrest.Matcher; | |
fa24d78b AM |
37 | |
38 | /** | |
39 | * Is a tree node available | |
40 | * | |
41 | * @author Matthew Khouzam | |
42 | */ | |
43 | public final class ConditionHelpers { | |
44 | ||
45 | private ConditionHelpers() {} | |
46 | ||
47 | /** | |
48 | * Provide default implementations for some {@link ICondition} methods. | |
49 | */ | |
50 | private abstract static class SWTBotTestCondition implements ICondition { | |
51 | ||
52 | @Override | |
53 | public abstract boolean test() throws Exception; | |
54 | ||
55 | @Override | |
56 | public final void init(SWTBot bot) { | |
57 | } | |
58 | ||
59 | @Override | |
2fe6a9ea | 60 | public String getFailureMessage() { |
fa24d78b AM |
61 | return null; |
62 | } | |
63 | } | |
64 | ||
65 | /** | |
66 | * Is a tree node available | |
67 | * | |
68 | * @param name | |
69 | * the name of the node | |
70 | * @param tree | |
71 | * the parent tree | |
72 | * @return true or false, it should swallow all exceptions | |
73 | */ | |
74 | public static ICondition IsTreeNodeAvailable(final String name, final SWTBotTree tree) { | |
75 | return new SWTBotTestCondition() { | |
76 | @Override | |
77 | public boolean test() throws Exception { | |
78 | try { | |
79 | final SWTBotTreeItem[] treeItems = tree.getAllItems(); | |
80 | for (SWTBotTreeItem ti : treeItems) { | |
81 | final String text = ti.getText(); | |
82 | if (text.equals(name)) { | |
83 | return true; | |
84 | } | |
85 | } | |
86 | } catch (Exception e) { | |
87 | } | |
88 | return false; | |
89 | } | |
90 | }; | |
91 | } | |
92 | ||
bbdb3d6d MAL |
93 | /** |
94 | * Is a table item available | |
95 | * | |
96 | * @param name | |
97 | * the name of the item | |
98 | * @param table | |
99 | * the parent table | |
100 | * @return true or false, it should swallow all exceptions | |
101 | */ | |
102 | public static ICondition isTableItemAvailable(final String name, final SWTBotTable table) { | |
103 | return new SWTBotTestCondition() { | |
104 | @Override | |
105 | public boolean test() throws Exception { | |
106 | try { | |
107 | return table.containsItem(name); | |
108 | } catch (Exception e) { | |
109 | } | |
110 | return false; | |
111 | } | |
112 | }; | |
113 | } | |
114 | ||
fa24d78b AM |
115 | /** |
116 | * Is the treeItem's node available | |
117 | * | |
118 | * @param name | |
119 | * the name of the node | |
120 | * @param treeItem | |
121 | * the treeItem | |
122 | * @return true or false, it should swallow all exceptions | |
123 | */ | |
124 | public static ICondition IsTreeChildNodeAvailable(final String name, final SWTBotTreeItem treeItem) { | |
125 | return new SWTBotTestCondition() { | |
126 | @Override | |
127 | public boolean test() throws Exception { | |
128 | try { | |
129 | return treeItem.getNode(name) != null; | |
130 | } catch (Exception e) { | |
131 | } | |
132 | return false; | |
133 | } | |
134 | }; | |
135 | } | |
136 | ||
137 | /** | |
138 | * Checks if the wizard's shell is null | |
139 | * | |
140 | * @param wizard | |
141 | * the null | |
142 | * @return false if either are null | |
143 | */ | |
144 | public static ICondition isWizardReady(final Wizard wizard) { | |
145 | return new SWTBotTestCondition() { | |
146 | @Override | |
147 | public boolean test() throws Exception { | |
148 | if (wizard.getShell() == null) { | |
149 | return false; | |
150 | } | |
151 | return true; | |
152 | } | |
153 | }; | |
154 | } | |
155 | ||
156 | /** | |
157 | * Is the wizard on the page you want? | |
158 | * | |
159 | * @param wizard | |
160 | * wizard | |
161 | * @param page | |
162 | * the desired page | |
163 | * @return true or false | |
164 | */ | |
165 | public static ICondition isWizardOnPage(final Wizard wizard, final IWizardPage page) { | |
166 | return new SWTBotTestCondition() { | |
167 | @Override | |
168 | public boolean test() throws Exception { | |
169 | if (wizard == null || page == null) { | |
170 | return false; | |
171 | } | |
172 | final IWizardContainer container = wizard.getContainer(); | |
173 | if (container == null) { | |
174 | return false; | |
175 | } | |
176 | IWizardPage currentPage = container.getCurrentPage(); | |
177 | return page.equals(currentPage); | |
178 | } | |
179 | }; | |
180 | } | |
181 | ||
182 | /** | |
183 | * Wait for a view to close | |
184 | * | |
185 | * @param view | |
186 | * bot view for the view | |
187 | * @return true if the view is closed, false if it's active. | |
188 | */ | |
189 | public static ICondition ViewIsClosed(final SWTBotView view) { | |
190 | return new SWTBotTestCondition() { | |
191 | @Override | |
192 | public boolean test() throws Exception { | |
193 | return (view != null) && (!view.isActive()); | |
194 | } | |
195 | }; | |
196 | } | |
197 | ||
198 | /** | |
199 | * Wait till table cell has a given content. | |
200 | * | |
201 | * @param table | |
202 | * the table bot reference | |
203 | * @param content | |
204 | * the content to check | |
205 | * @param row | |
206 | * the row of the cell | |
207 | * @param column | |
208 | * the column of the cell | |
209 | * @return ICondition for verification | |
210 | */ | |
211 | public static ICondition isTableCellFilled(final SWTBotTable table, | |
212 | final String content, final int row, final int column) { | |
213 | return new SWTBotTestCondition() { | |
214 | @Override | |
215 | public boolean test() throws Exception { | |
216 | try { | |
2470d687 MK |
217 | String cell = table.cell(row, column); |
218 | if( cell == null ) { | |
219 | return false; | |
220 | } | |
221 | return cell.endsWith(content); | |
fa24d78b AM |
222 | } catch (Exception e) { |
223 | } | |
224 | return false; | |
225 | } | |
226 | }; | |
227 | } | |
21e5206c PT |
228 | |
229 | /** | |
230 | * Condition to check if a tracing project element has a child with the | |
231 | * specified name. A project element label may have a count suffix in the | |
232 | * format ' [n]'. | |
233 | */ | |
234 | public static class ProjectElementHasChild extends DefaultCondition { | |
235 | ||
236 | private final SWTBotTreeItem fParentItem; | |
237 | private final String fName; | |
238 | private final String fRegex; | |
239 | private SWTBotTreeItem fItem = null; | |
240 | ||
241 | /** | |
242 | * Constructor. | |
243 | * | |
244 | * @param parentItem | |
245 | * the parent item | |
246 | * @param name | |
247 | * the child name to look for | |
248 | */ | |
249 | public ProjectElementHasChild(final SWTBotTreeItem parentItem, final String name) { | |
250 | fParentItem = parentItem; | |
251 | fName = name; | |
252 | /* Project element labels may have count suffix */ | |
253 | fRegex = name + "(\\s\\[(\\d)+\\])?"; | |
254 | } | |
255 | ||
256 | @Override | |
257 | public boolean test() throws Exception { | |
258 | fParentItem.expand(); | |
259 | for (SWTBotTreeItem item : fParentItem.getItems()) { | |
260 | if (item.getText().matches(fRegex)) { | |
261 | fItem = item; | |
262 | return true; | |
263 | } | |
264 | } | |
265 | return false; | |
266 | } | |
267 | ||
268 | @Override | |
269 | public String getFailureMessage() { | |
270 | return NLS.bind("No child of {0} found with name {1}", fParentItem.getText(), fName); | |
271 | } | |
272 | ||
273 | /** | |
274 | * Returns the matching child item if the condition returned true. | |
275 | * | |
276 | * @return the matching item | |
277 | */ | |
278 | public SWTBotTreeItem getItem() { | |
279 | return fItem; | |
280 | } | |
281 | } | |
88051e61 PT |
282 | |
283 | /** | |
284 | * Condition to check if an editor with the specified title is opened. | |
285 | * | |
286 | * @param bot | |
287 | * a workbench bot | |
288 | * @param title | |
289 | * the editor title | |
290 | * @return ICondition for verification | |
291 | */ | |
292 | public static ICondition isEditorOpened(final SWTWorkbenchBot bot, final String title) { | |
293 | return new SWTBotTestCondition() { | |
294 | @Override | |
295 | public boolean test() throws Exception { | |
296 | Matcher<IEditorReference> withPartName = withPartName(title); | |
297 | return !bot.editors(withPartName).isEmpty(); | |
298 | } | |
299 | }; | |
300 | } | |
2fe6a9ea PT |
301 | |
302 | /** | |
303 | * Condition to check if the selection range equals the specified range. | |
304 | * | |
305 | * @param range | |
306 | * the selection range | |
307 | * @return ICondition for verification | |
308 | */ | |
309 | public static ICondition selectionRange(final TmfTimeRange range) { | |
310 | return new SWTBotTestCondition() { | |
311 | @Override | |
312 | public boolean test() throws Exception { | |
313 | return TmfTraceManager.getInstance().getCurrentTraceContext().getSelectionRange().equals(range); | |
314 | } | |
315 | ||
316 | @Override | |
317 | public String getFailureMessage() { | |
318 | return NLS.bind("Selection range: {0} expected: {1}", | |
319 | TmfTraceManager.getInstance().getCurrentTraceContext().getSelectionRange(), range); | |
320 | } | |
321 | }; | |
322 | } | |
323 | ||
324 | /** | |
325 | * Condition to check if the window range equals the specified range. | |
326 | * | |
327 | * @param range | |
328 | * the window range | |
329 | * @return ICondition for verification | |
330 | */ | |
331 | public static ICondition windowRange(final TmfTimeRange range) { | |
332 | return new SWTBotTestCondition() { | |
333 | @Override | |
334 | public boolean test() throws Exception { | |
335 | return TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange().equals(range); | |
336 | } | |
337 | ||
338 | @Override | |
339 | public String getFailureMessage() { | |
340 | return NLS.bind("Window range: {0} expected: {1}", | |
341 | TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange(), range); | |
342 | } | |
343 | }; | |
344 | } | |
6e4a07af PT |
345 | |
346 | /** | |
347 | * Condition to check if the selection contains the specified text at the | |
348 | * specified column. The text is checked in any item of the tree selection. | |
349 | * | |
350 | * @param tree | |
351 | * the SWTBot tree | |
352 | * @param column | |
353 | * the column index | |
354 | * @param text | |
355 | * the expected text | |
356 | * @return ICondition for verification | |
357 | */ | |
358 | public static ICondition treeSelectionContains(final SWTBotTree tree, final int column, final String text) { | |
359 | return new SWTBotTestCondition() { | |
360 | @Override | |
361 | public boolean test() throws Exception { | |
362 | TableCollection selection = tree.selection(); | |
363 | for (int row = 0; row < selection.rowCount(); row++) { | |
364 | if (selection.get(row, column).equals(text)) { | |
365 | return true; | |
366 | } | |
367 | } | |
368 | return false; | |
369 | } | |
370 | ||
371 | @Override | |
372 | public String getFailureMessage() { | |
373 | return NLS.bind("Tree selection [0,{0}]: {1} expected: {2}", | |
374 | new Object[] { column, tree.selection().get(0, column), text}); | |
375 | } | |
376 | }; | |
377 | } | |
88051e61 | 378 | } |