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