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