Commit | Line | Data |
---|---|---|
fa24d78b | 1 | /******************************************************************************* |
3553c912 | 2 | * Copyright (c) 2013, 2016 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() { | |
3553c912 | 103 | return NLS.bind("No child of tree {0} found with text {1}. Child items: {2}", |
46958d08 PT |
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 | ||
3553c912 PT |
192 | /** |
193 | * Condition to check if the number of direct children of the | |
194 | * provided tree item equals the specified count. | |
195 | * | |
196 | * @param treeItem | |
197 | * the SWTBot tree item | |
198 | * @param count | |
199 | * the expected count | |
200 | * @return ICondition for verification | |
201 | */ | |
202 | public static ICondition treeItemCount(final SWTBotTreeItem treeItem, int count) { | |
203 | return new SWTBotTestCondition() { | |
204 | @Override | |
205 | public boolean test() throws Exception { | |
206 | return treeItem.rowCount() == count; | |
207 | } | |
208 | ||
209 | @Override | |
210 | public String getFailureMessage() { | |
211 | return NLS.bind("Tree item count: {0} expected: {1}", | |
212 | treeItem.rowCount(), count); | |
213 | } | |
214 | }; | |
215 | } | |
216 | ||
fa24d78b AM |
217 | /** |
218 | * Checks if the wizard's shell is null | |
219 | * | |
220 | * @param wizard | |
221 | * the null | |
222 | * @return false if either are null | |
223 | */ | |
224 | public static ICondition isWizardReady(final Wizard wizard) { | |
225 | return new SWTBotTestCondition() { | |
226 | @Override | |
227 | public boolean test() throws Exception { | |
228 | if (wizard.getShell() == null) { | |
229 | return false; | |
230 | } | |
231 | return true; | |
232 | } | |
233 | }; | |
234 | } | |
235 | ||
236 | /** | |
237 | * Is the wizard on the page you want? | |
238 | * | |
239 | * @param wizard | |
240 | * wizard | |
241 | * @param page | |
242 | * the desired page | |
243 | * @return true or false | |
244 | */ | |
245 | public static ICondition isWizardOnPage(final Wizard wizard, final IWizardPage page) { | |
246 | return new SWTBotTestCondition() { | |
247 | @Override | |
248 | public boolean test() throws Exception { | |
249 | if (wizard == null || page == null) { | |
250 | return false; | |
251 | } | |
252 | final IWizardContainer container = wizard.getContainer(); | |
253 | if (container == null) { | |
254 | return false; | |
255 | } | |
256 | IWizardPage currentPage = container.getCurrentPage(); | |
257 | return page.equals(currentPage); | |
258 | } | |
259 | }; | |
260 | } | |
261 | ||
262 | /** | |
263 | * Wait for a view to close | |
264 | * | |
265 | * @param view | |
266 | * bot view for the view | |
267 | * @return true if the view is closed, false if it's active. | |
268 | */ | |
269 | public static ICondition ViewIsClosed(final SWTBotView view) { | |
270 | return new SWTBotTestCondition() { | |
271 | @Override | |
272 | public boolean test() throws Exception { | |
273 | return (view != null) && (!view.isActive()); | |
274 | } | |
275 | }; | |
276 | } | |
277 | ||
278 | /** | |
279 | * Wait till table cell has a given content. | |
280 | * | |
281 | * @param table | |
282 | * the table bot reference | |
283 | * @param content | |
284 | * the content to check | |
285 | * @param row | |
286 | * the row of the cell | |
287 | * @param column | |
288 | * the column of the cell | |
289 | * @return ICondition for verification | |
290 | */ | |
291 | public static ICondition isTableCellFilled(final SWTBotTable table, | |
292 | final String content, final int row, final int column) { | |
293 | return new SWTBotTestCondition() { | |
294 | @Override | |
295 | public boolean test() throws Exception { | |
296 | try { | |
2470d687 MK |
297 | String cell = table.cell(row, column); |
298 | if( cell == null ) { | |
299 | return false; | |
300 | } | |
4d760144 | 301 | return cell.contains(content); |
fa24d78b AM |
302 | } catch (Exception e) { |
303 | } | |
304 | return false; | |
305 | } | |
306 | }; | |
307 | } | |
21e5206c PT |
308 | |
309 | /** | |
310 | * Condition to check if a tracing project element has a child with the | |
311 | * specified name. A project element label may have a count suffix in the | |
312 | * format ' [n]'. | |
313 | */ | |
314 | public static class ProjectElementHasChild extends DefaultCondition { | |
315 | ||
316 | private final SWTBotTreeItem fParentItem; | |
317 | private final String fName; | |
318 | private final String fRegex; | |
319 | private SWTBotTreeItem fItem = null; | |
320 | ||
321 | /** | |
322 | * Constructor. | |
323 | * | |
324 | * @param parentItem | |
325 | * the parent item | |
326 | * @param name | |
327 | * the child name to look for | |
328 | */ | |
329 | public ProjectElementHasChild(final SWTBotTreeItem parentItem, final String name) { | |
330 | fParentItem = parentItem; | |
331 | fName = name; | |
332 | /* Project element labels may have count suffix */ | |
333 | fRegex = name + "(\\s\\[(\\d)+\\])?"; | |
334 | } | |
335 | ||
336 | @Override | |
337 | public boolean test() throws Exception { | |
338 | fParentItem.expand(); | |
339 | for (SWTBotTreeItem item : fParentItem.getItems()) { | |
340 | if (item.getText().matches(fRegex)) { | |
341 | fItem = item; | |
342 | return true; | |
343 | } | |
344 | } | |
345 | return false; | |
346 | } | |
347 | ||
348 | @Override | |
349 | public String getFailureMessage() { | |
350 | return NLS.bind("No child of {0} found with name {1}", fParentItem.getText(), fName); | |
351 | } | |
352 | ||
353 | /** | |
354 | * Returns the matching child item if the condition returned true. | |
355 | * | |
356 | * @return the matching item | |
357 | */ | |
358 | public SWTBotTreeItem getItem() { | |
359 | return fItem; | |
360 | } | |
361 | } | |
88051e61 PT |
362 | |
363 | /** | |
364 | * Condition to check if an editor with the specified title is opened. | |
365 | * | |
366 | * @param bot | |
367 | * a workbench bot | |
368 | * @param title | |
369 | * the editor title | |
370 | * @return ICondition for verification | |
371 | */ | |
372 | public static ICondition isEditorOpened(final SWTWorkbenchBot bot, final String title) { | |
373 | return new SWTBotTestCondition() { | |
374 | @Override | |
375 | public boolean test() throws Exception { | |
376 | Matcher<IEditorReference> withPartName = withPartName(title); | |
377 | return !bot.editors(withPartName).isEmpty(); | |
378 | } | |
379 | }; | |
380 | } | |
2fe6a9ea PT |
381 | |
382 | /** | |
383 | * Condition to check if the selection range equals the specified range. | |
384 | * | |
385 | * @param range | |
386 | * the selection range | |
387 | * @return ICondition for verification | |
388 | */ | |
389 | public static ICondition selectionRange(final TmfTimeRange range) { | |
390 | return new SWTBotTestCondition() { | |
391 | @Override | |
392 | public boolean test() throws Exception { | |
393 | return TmfTraceManager.getInstance().getCurrentTraceContext().getSelectionRange().equals(range); | |
394 | } | |
395 | ||
396 | @Override | |
397 | public String getFailureMessage() { | |
398 | return NLS.bind("Selection range: {0} expected: {1}", | |
399 | TmfTraceManager.getInstance().getCurrentTraceContext().getSelectionRange(), range); | |
400 | } | |
401 | }; | |
402 | } | |
403 | ||
404 | /** | |
405 | * Condition to check if the window range equals the specified range. | |
406 | * | |
407 | * @param range | |
408 | * the window range | |
409 | * @return ICondition for verification | |
410 | */ | |
411 | public static ICondition windowRange(final TmfTimeRange range) { | |
412 | return new SWTBotTestCondition() { | |
413 | @Override | |
414 | public boolean test() throws Exception { | |
415 | return TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange().equals(range); | |
416 | } | |
417 | ||
418 | @Override | |
419 | public String getFailureMessage() { | |
420 | return NLS.bind("Window range: {0} expected: {1}", | |
421 | TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange(), range); | |
422 | } | |
423 | }; | |
424 | } | |
6e4a07af PT |
425 | |
426 | /** | |
427 | * Condition to check if the selection contains the specified text at the | |
428 | * specified column. The text is checked in any item of the tree selection. | |
429 | * | |
430 | * @param tree | |
431 | * the SWTBot tree | |
432 | * @param column | |
433 | * the column index | |
434 | * @param text | |
435 | * the expected text | |
436 | * @return ICondition for verification | |
437 | */ | |
438 | public static ICondition treeSelectionContains(final SWTBotTree tree, final int column, final String text) { | |
439 | return new SWTBotTestCondition() { | |
440 | @Override | |
441 | public boolean test() throws Exception { | |
442 | TableCollection selection = tree.selection(); | |
443 | for (int row = 0; row < selection.rowCount(); row++) { | |
444 | if (selection.get(row, column).equals(text)) { | |
445 | return true; | |
446 | } | |
447 | } | |
448 | return false; | |
449 | } | |
450 | ||
451 | @Override | |
452 | public String getFailureMessage() { | |
453 | return NLS.bind("Tree selection [0,{0}]: {1} expected: {2}", | |
454 | new Object[] { column, tree.selection().get(0, column), text}); | |
455 | } | |
456 | }; | |
457 | } | |
573087b4 MAL |
458 | |
459 | private static class EventsTableSelectionCondition extends DefaultCondition { | |
460 | private long fSelectionTime; | |
461 | private SWTWorkbenchBot fBot; | |
9bb4b61e | 462 | private long fCurValue; |
573087b4 MAL |
463 | |
464 | private EventsTableSelectionCondition(SWTWorkbenchBot bot, long selectionTime) { | |
465 | fBot = bot; | |
466 | fSelectionTime = selectionTime; | |
467 | } | |
468 | ||
469 | @Override | |
470 | public boolean test() throws Exception { | |
471 | StructuredSelection eventsTableSelection = getEventsTableSelection(); | |
472 | if (eventsTableSelection.isEmpty()) { | |
473 | return false; | |
474 | } | |
9bb4b61e MAL |
475 | fCurValue = ((ITmfEvent) eventsTableSelection.getFirstElement()).getTimestamp().getValue(); |
476 | return fCurValue == fSelectionTime; | |
573087b4 MAL |
477 | } |
478 | ||
479 | @Override | |
480 | public String getFailureMessage() { | |
9bb4b61e | 481 | return "The selection in the table was not an event with timestamp " + fSelectionTime + ". Actual is " + fCurValue; |
573087b4 MAL |
482 | } |
483 | ||
484 | private StructuredSelection getEventsTableSelection() { | |
485 | return UIThreadRunnable.syncExec(new Result<StructuredSelection>() { | |
486 | ||
487 | @Override | |
488 | public StructuredSelection run() { | |
489 | SWTBotEditor eventsEditor = SWTBotUtils.activeEventsEditor(fBot); | |
490 | TmfEventsEditor part = (TmfEventsEditor) eventsEditor.getReference().getPart(false); | |
491 | StructuredSelection selection = (StructuredSelection) part.getSelection(); | |
492 | return selection; | |
493 | } | |
494 | }); | |
495 | } | |
496 | } | |
497 | ||
498 | /** | |
499 | * Wait until the events table selection matches the specified time stamp. | |
500 | * | |
501 | * @param bot | |
502 | * a workbench bot | |
503 | * | |
504 | * @param selectionTime | |
505 | * the selection time | |
506 | * @return ICondition for verification | |
507 | */ | |
508 | public static ICondition selectionInEventsTable(final SWTWorkbenchBot bot, long selectionTime) { | |
509 | return new EventsTableSelectionCondition(bot, selectionTime); | |
510 | } | |
156e9ead MAL |
511 | |
512 | private static class TimeGraphIsReadyCondition extends DefaultCondition { | |
513 | ||
514 | private @NonNull TmfTimeRange fSelectionRange; | |
515 | private @NonNull ITmfTimestamp fVisibleTime; | |
516 | private AbstractTimeGraphView fView; | |
f149d124 | 517 | private String fFailureMessage; |
156e9ead MAL |
518 | |
519 | private TimeGraphIsReadyCondition(AbstractTimeGraphView view, @NonNull TmfTimeRange selectionRange, @NonNull ITmfTimestamp visibleTime) { | |
520 | fView = view; | |
521 | fSelectionRange = selectionRange; | |
522 | fVisibleTime = visibleTime; | |
523 | } | |
524 | ||
525 | @Override | |
526 | public boolean test() throws Exception { | |
f149d124 MAL |
527 | ICondition selectionRangeCondition = ConditionHelpers.selectionRange(fSelectionRange); |
528 | if (!selectionRangeCondition.test()) { | |
529 | fFailureMessage = selectionRangeCondition.getFailureMessage(); | |
156e9ead MAL |
530 | return false; |
531 | } | |
f149d124 MAL |
532 | @NonNull TmfTimeRange curWindowRange = TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange(); |
533 | if (!curWindowRange.contains(fVisibleTime)) { | |
534 | fFailureMessage = "Current window range " + curWindowRange + " does not contain " + fVisibleTime; | |
156e9ead MAL |
535 | return false; |
536 | } | |
f149d124 MAL |
537 | |
538 | if (fView.isDirty()) { | |
539 | fFailureMessage = "Time graph is dirty"; | |
540 | return false; | |
541 | ||
542 | } | |
543 | return true; | |
156e9ead MAL |
544 | } |
545 | ||
546 | @Override | |
547 | public String getFailureMessage() { | |
f149d124 | 548 | return fFailureMessage; |
156e9ead MAL |
549 | } |
550 | } | |
551 | ||
552 | /** | |
553 | * | |
554 | * Wait until the Time Graph view is ready. The Time Graph view is | |
555 | * considered ready if the selectionRange is selected, the visibleTime is | |
556 | * visible and the view is not dirty (its model is done updating). | |
557 | * | |
558 | * @param view | |
559 | * the time graph view | |
560 | * @param selectionRange | |
561 | * the selection that the time graph should have | |
562 | * @param visibleTime | |
563 | * the visible time that the time graph should have | |
564 | * @return ICondition for verification | |
565 | */ | |
566 | public static ICondition timeGraphIsReadyCondition(AbstractTimeGraphView view, @NonNull TmfTimeRange selectionRange, @NonNull ITmfTimestamp visibleTime) { | |
567 | return new TimeGraphIsReadyCondition(view, selectionRange, visibleTime); | |
568 | } | |
88051e61 | 569 | } |