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 | 19 | import java.util.Arrays; |
e834a6b4 | 20 | import java.util.List; |
cdfe10e7 | 21 | import java.util.regex.Pattern; |
46958d08 | 22 | |
156e9ead | 23 | import org.eclipse.jdt.annotation.NonNull; |
573087b4 | 24 | import org.eclipse.jface.viewers.StructuredSelection; |
fa24d78b AM |
25 | import org.eclipse.jface.wizard.IWizardContainer; |
26 | import org.eclipse.jface.wizard.IWizardPage; | |
27 | import org.eclipse.jface.wizard.Wizard; | |
21e5206c | 28 | import org.eclipse.osgi.util.NLS; |
88051e61 | 29 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; |
e834a6b4 | 30 | import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory; |
573087b4 | 31 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor; |
fa24d78b AM |
32 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; |
33 | import org.eclipse.swtbot.swt.finder.SWTBot; | |
573087b4 MAL |
34 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; |
35 | import org.eclipse.swtbot.swt.finder.results.Result; | |
6e4a07af | 36 | import org.eclipse.swtbot.swt.finder.utils.TableCollection; |
21e5206c | 37 | import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; |
fa24d78b AM |
38 | import org.eclipse.swtbot.swt.finder.waits.ICondition; |
39 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; | |
40 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; | |
41 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; | |
573087b4 | 42 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
156e9ead | 43 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; |
2fe6a9ea | 44 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; |
e834a6b4 | 45 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
2fe6a9ea | 46 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
573087b4 | 47 | import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor; |
c27e9fec | 48 | import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfXYChartViewer; |
156e9ead | 49 | import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractTimeGraphView; |
88051e61 PT |
50 | import org.eclipse.ui.IEditorReference; |
51 | import org.hamcrest.Matcher; | |
aa5b9bd6 | 52 | import org.swtchart.Chart; |
fa24d78b AM |
53 | |
54 | /** | |
55 | * Is a tree node available | |
56 | * | |
57 | * @author Matthew Khouzam | |
58 | */ | |
59 | public final class ConditionHelpers { | |
60 | ||
61 | private ConditionHelpers() {} | |
62 | ||
63 | /** | |
64 | * Provide default implementations for some {@link ICondition} methods. | |
65 | */ | |
2e65d221 | 66 | public abstract static class SWTBotTestCondition implements ICondition { |
fa24d78b AM |
67 | |
68 | @Override | |
69 | public abstract boolean test() throws Exception; | |
70 | ||
71 | @Override | |
72 | public final void init(SWTBot bot) { | |
73 | } | |
74 | ||
75 | @Override | |
2fe6a9ea | 76 | public String getFailureMessage() { |
fa24d78b AM |
77 | return null; |
78 | } | |
79 | } | |
80 | ||
81 | /** | |
82 | * Is a tree node available | |
83 | * | |
84 | * @param name | |
85 | * the name of the node | |
86 | * @param tree | |
87 | * the parent tree | |
88 | * @return true or false, it should swallow all exceptions | |
89 | */ | |
90 | public static ICondition IsTreeNodeAvailable(final String name, final SWTBotTree tree) { | |
91 | return new SWTBotTestCondition() { | |
92 | @Override | |
93 | public boolean test() throws Exception { | |
94 | try { | |
95 | final SWTBotTreeItem[] treeItems = tree.getAllItems(); | |
96 | for (SWTBotTreeItem ti : treeItems) { | |
97 | final String text = ti.getText(); | |
98 | if (text.equals(name)) { | |
99 | return true; | |
100 | } | |
101 | } | |
102 | } catch (Exception e) { | |
103 | } | |
104 | return false; | |
105 | } | |
46958d08 PT |
106 | |
107 | @Override | |
108 | public String getFailureMessage() { | |
3553c912 | 109 | return NLS.bind("No child of tree {0} found with text {1}. Child items: {2}", |
46958d08 PT |
110 | new String[] { tree.toString(), name, Arrays.toString(tree.getAllItems()) }); |
111 | } | |
fa24d78b AM |
112 | }; |
113 | } | |
114 | ||
bbdb3d6d MAL |
115 | /** |
116 | * Is a table item available | |
117 | * | |
118 | * @param name | |
119 | * the name of the item | |
120 | * @param table | |
121 | * the parent table | |
122 | * @return true or false, it should swallow all exceptions | |
123 | */ | |
124 | public static ICondition isTableItemAvailable(final String name, final SWTBotTable table) { | |
125 | return new SWTBotTestCondition() { | |
126 | @Override | |
127 | public boolean test() throws Exception { | |
128 | try { | |
129 | return table.containsItem(name); | |
130 | } catch (Exception e) { | |
131 | } | |
132 | return false; | |
133 | } | |
46958d08 PT |
134 | |
135 | @Override | |
136 | public String getFailureMessage() { | |
cdfe10e7 | 137 | return NLS.bind("No child of table {0} found with text ''{1}''.", table, name); |
46958d08 | 138 | } |
bbdb3d6d MAL |
139 | }; |
140 | } | |
141 | ||
fa24d78b AM |
142 | /** |
143 | * Is the treeItem's node available | |
144 | * | |
145 | * @param name | |
146 | * the name of the node | |
147 | * @param treeItem | |
148 | * the treeItem | |
149 | * @return true or false, it should swallow all exceptions | |
150 | */ | |
151 | public static ICondition IsTreeChildNodeAvailable(final String name, final SWTBotTreeItem treeItem) { | |
152 | return new SWTBotTestCondition() { | |
153 | @Override | |
154 | public boolean test() throws Exception { | |
155 | try { | |
156 | return treeItem.getNode(name) != null; | |
157 | } catch (Exception e) { | |
158 | } | |
159 | return false; | |
160 | } | |
46958d08 PT |
161 | |
162 | @Override | |
163 | public String getFailureMessage() { | |
cdfe10e7 | 164 | return NLS.bind("No child of tree item {0} found with text ''{1}''. Child items: {2}", |
46958d08 PT |
165 | new String[] { treeItem.toString(), name, Arrays.toString(treeItem.getItems()) }); |
166 | } | |
fa24d78b AM |
167 | }; |
168 | } | |
169 | ||
2e65d221 BH |
170 | /** |
171 | * Is the treeItem's node removed | |
172 | * | |
173 | * @param length | |
174 | * length of the node after removal | |
175 | * @param treeItem | |
176 | * the treeItem | |
177 | * @return true or false, it should swallow all exceptions | |
178 | */ | |
179 | public static ICondition isTreeChildNodeRemoved(final int length, final SWTBotTreeItem treeItem) { | |
180 | return new SWTBotTestCondition() { | |
181 | @Override | |
182 | public boolean test() throws Exception { | |
183 | try { | |
184 | return treeItem.getNodes().size() == length; | |
185 | } catch (Exception e) { | |
186 | } | |
187 | return false; | |
188 | } | |
189 | ||
190 | @Override | |
191 | public String getFailureMessage() { | |
cdfe10e7 | 192 | return NLS.bind("Child of tree item {0} found with text ''{1}'' not removed. Child items: {2}", |
2e65d221 BH |
193 | new String[] { treeItem.toString(), String.valueOf(length), Arrays.toString(treeItem.getItems()) }); |
194 | } | |
195 | }; | |
196 | } | |
197 | ||
3553c912 PT |
198 | /** |
199 | * Condition to check if the number of direct children of the | |
200 | * provided tree item equals the specified count. | |
201 | * | |
202 | * @param treeItem | |
203 | * the SWTBot tree item | |
204 | * @param count | |
205 | * the expected count | |
206 | * @return ICondition for verification | |
207 | */ | |
208 | public static ICondition treeItemCount(final SWTBotTreeItem treeItem, int count) { | |
209 | return new SWTBotTestCondition() { | |
210 | @Override | |
211 | public boolean test() throws Exception { | |
212 | return treeItem.rowCount() == count; | |
213 | } | |
214 | ||
215 | @Override | |
216 | public String getFailureMessage() { | |
217 | return NLS.bind("Tree item count: {0} expected: {1}", | |
218 | treeItem.rowCount(), count); | |
219 | } | |
220 | }; | |
221 | } | |
222 | ||
fa24d78b AM |
223 | /** |
224 | * Checks if the wizard's shell is null | |
225 | * | |
226 | * @param wizard | |
227 | * the null | |
228 | * @return false if either are null | |
229 | */ | |
230 | public static ICondition isWizardReady(final Wizard wizard) { | |
231 | return new SWTBotTestCondition() { | |
232 | @Override | |
233 | public boolean test() throws Exception { | |
234 | if (wizard.getShell() == null) { | |
235 | return false; | |
236 | } | |
237 | return true; | |
238 | } | |
239 | }; | |
240 | } | |
241 | ||
242 | /** | |
243 | * Is the wizard on the page you want? | |
244 | * | |
245 | * @param wizard | |
246 | * wizard | |
247 | * @param page | |
248 | * the desired page | |
249 | * @return true or false | |
250 | */ | |
251 | public static ICondition isWizardOnPage(final Wizard wizard, final IWizardPage page) { | |
252 | return new SWTBotTestCondition() { | |
253 | @Override | |
254 | public boolean test() throws Exception { | |
255 | if (wizard == null || page == null) { | |
256 | return false; | |
257 | } | |
258 | final IWizardContainer container = wizard.getContainer(); | |
259 | if (container == null) { | |
260 | return false; | |
261 | } | |
262 | IWizardPage currentPage = container.getCurrentPage(); | |
263 | return page.equals(currentPage); | |
264 | } | |
265 | }; | |
266 | } | |
267 | ||
268 | /** | |
269 | * Wait for a view to close | |
270 | * | |
271 | * @param view | |
272 | * bot view for the view | |
273 | * @return true if the view is closed, false if it's active. | |
274 | */ | |
275 | public static ICondition ViewIsClosed(final SWTBotView view) { | |
276 | return new SWTBotTestCondition() { | |
277 | @Override | |
278 | public boolean test() throws Exception { | |
279 | return (view != null) && (!view.isActive()); | |
280 | } | |
281 | }; | |
282 | } | |
283 | ||
284 | /** | |
285 | * Wait till table cell has a given content. | |
286 | * | |
287 | * @param table | |
288 | * the table bot reference | |
289 | * @param content | |
290 | * the content to check | |
291 | * @param row | |
292 | * the row of the cell | |
293 | * @param column | |
294 | * the column of the cell | |
295 | * @return ICondition for verification | |
296 | */ | |
297 | public static ICondition isTableCellFilled(final SWTBotTable table, | |
298 | final String content, final int row, final int column) { | |
299 | return new SWTBotTestCondition() { | |
300 | @Override | |
301 | public boolean test() throws Exception { | |
302 | try { | |
2470d687 MK |
303 | String cell = table.cell(row, column); |
304 | if( cell == null ) { | |
305 | return false; | |
306 | } | |
4d760144 | 307 | return cell.contains(content); |
fa24d78b AM |
308 | } catch (Exception e) { |
309 | } | |
310 | return false; | |
311 | } | |
ff578d8e GB |
312 | |
313 | @Override | |
314 | public String getFailureMessage() { | |
315 | String cell = table.cell(row, column); | |
316 | if (cell == null) { | |
317 | return NLS.bind("Cell absent, expected: {0}", content); | |
318 | } | |
319 | return NLS.bind("Cell content: {0} expected: {1}", cell, content); | |
320 | } | |
fa24d78b AM |
321 | }; |
322 | } | |
21e5206c PT |
323 | |
324 | /** | |
325 | * Condition to check if a tracing project element has a child with the | |
326 | * specified name. A project element label may have a count suffix in the | |
327 | * format ' [n]'. | |
328 | */ | |
329 | public static class ProjectElementHasChild extends DefaultCondition { | |
330 | ||
331 | private final SWTBotTreeItem fParentItem; | |
332 | private final String fName; | |
333 | private final String fRegex; | |
334 | private SWTBotTreeItem fItem = null; | |
335 | ||
336 | /** | |
337 | * Constructor. | |
338 | * | |
339 | * @param parentItem | |
340 | * the parent item | |
341 | * @param name | |
342 | * the child name to look for | |
343 | */ | |
344 | public ProjectElementHasChild(final SWTBotTreeItem parentItem, final String name) { | |
345 | fParentItem = parentItem; | |
346 | fName = name; | |
347 | /* Project element labels may have count suffix */ | |
cdfe10e7 | 348 | fRegex = Pattern.quote(name) + "(\\s\\[(\\d)+\\])?"; |
21e5206c PT |
349 | } |
350 | ||
351 | @Override | |
352 | public boolean test() throws Exception { | |
353 | fParentItem.expand(); | |
354 | for (SWTBotTreeItem item : fParentItem.getItems()) { | |
355 | if (item.getText().matches(fRegex)) { | |
356 | fItem = item; | |
357 | return true; | |
358 | } | |
359 | } | |
360 | return false; | |
361 | } | |
362 | ||
363 | @Override | |
364 | public String getFailureMessage() { | |
365 | return NLS.bind("No child of {0} found with name {1}", fParentItem.getText(), fName); | |
366 | } | |
367 | ||
368 | /** | |
369 | * Returns the matching child item if the condition returned true. | |
370 | * | |
371 | * @return the matching item | |
372 | */ | |
373 | public SWTBotTreeItem getItem() { | |
374 | return fItem; | |
375 | } | |
376 | } | |
88051e61 PT |
377 | |
378 | /** | |
379 | * Condition to check if an editor with the specified title is opened. | |
380 | * | |
381 | * @param bot | |
382 | * a workbench bot | |
383 | * @param title | |
384 | * the editor title | |
385 | * @return ICondition for verification | |
386 | */ | |
387 | public static ICondition isEditorOpened(final SWTWorkbenchBot bot, final String title) { | |
388 | return new SWTBotTestCondition() { | |
389 | @Override | |
390 | public boolean test() throws Exception { | |
391 | Matcher<IEditorReference> withPartName = withPartName(title); | |
392 | return !bot.editors(withPartName).isEmpty(); | |
393 | } | |
394 | }; | |
395 | } | |
2fe6a9ea PT |
396 | |
397 | /** | |
398 | * Condition to check if the selection range equals the specified range. | |
399 | * | |
400 | * @param range | |
401 | * the selection range | |
402 | * @return ICondition for verification | |
403 | */ | |
404 | public static ICondition selectionRange(final TmfTimeRange range) { | |
405 | return new SWTBotTestCondition() { | |
406 | @Override | |
407 | public boolean test() throws Exception { | |
408 | return TmfTraceManager.getInstance().getCurrentTraceContext().getSelectionRange().equals(range); | |
409 | } | |
410 | ||
411 | @Override | |
412 | public String getFailureMessage() { | |
413 | return NLS.bind("Selection range: {0} expected: {1}", | |
414 | TmfTraceManager.getInstance().getCurrentTraceContext().getSelectionRange(), range); | |
415 | } | |
416 | }; | |
417 | } | |
418 | ||
419 | /** | |
420 | * Condition to check if the window range equals the specified range. | |
421 | * | |
422 | * @param range | |
423 | * the window range | |
424 | * @return ICondition for verification | |
425 | */ | |
426 | public static ICondition windowRange(final TmfTimeRange range) { | |
427 | return new SWTBotTestCondition() { | |
428 | @Override | |
429 | public boolean test() throws Exception { | |
430 | return TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange().equals(range); | |
431 | } | |
432 | ||
433 | @Override | |
434 | public String getFailureMessage() { | |
435 | return NLS.bind("Window range: {0} expected: {1}", | |
436 | TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange(), range); | |
437 | } | |
438 | }; | |
439 | } | |
6e4a07af PT |
440 | |
441 | /** | |
442 | * Condition to check if the selection contains the specified text at the | |
443 | * specified column. The text is checked in any item of the tree selection. | |
444 | * | |
445 | * @param tree | |
446 | * the SWTBot tree | |
447 | * @param column | |
448 | * the column index | |
449 | * @param text | |
450 | * the expected text | |
451 | * @return ICondition for verification | |
452 | */ | |
453 | public static ICondition treeSelectionContains(final SWTBotTree tree, final int column, final String text) { | |
454 | return new SWTBotTestCondition() { | |
455 | @Override | |
456 | public boolean test() throws Exception { | |
457 | TableCollection selection = tree.selection(); | |
458 | for (int row = 0; row < selection.rowCount(); row++) { | |
459 | if (selection.get(row, column).equals(text)) { | |
460 | return true; | |
461 | } | |
462 | } | |
463 | return false; | |
464 | } | |
465 | ||
466 | @Override | |
467 | public String getFailureMessage() { | |
468 | return NLS.bind("Tree selection [0,{0}]: {1} expected: {2}", | |
469 | new Object[] { column, tree.selection().get(0, column), text}); | |
470 | } | |
471 | }; | |
472 | } | |
573087b4 | 473 | |
2d594808 PT |
474 | /** |
475 | * Condition to check if the selection contains the specified text at the | |
476 | * specified column. The text is checked in any item of the tree selection. | |
477 | * | |
478 | * @param tree | |
479 | * the SWTBot tree | |
480 | * @param column | |
481 | * the column index | |
482 | * @param text | |
483 | * the expected text | |
484 | * @return ICondition for verification | |
485 | */ | |
486 | public static ICondition timeGraphSelectionContains(final SWTBotTimeGraph timeGraph, final int column, final String text) { | |
487 | return new SWTBotTestCondition() { | |
488 | @Override | |
489 | public boolean test() throws Exception { | |
490 | TableCollection selection = timeGraph.selection(); | |
491 | for (int row = 0; row < selection.rowCount(); row++) { | |
492 | if (selection.get(row, column).equals(text)) { | |
493 | return true; | |
494 | } | |
495 | } | |
496 | return false; | |
497 | } | |
498 | ||
499 | @Override | |
500 | public String getFailureMessage() { | |
501 | return NLS.bind("Time graph selection [0,{0}]: {1} expected: {2}", | |
502 | new Object[] { column, timeGraph.selection().get(0, column), text}); | |
503 | } | |
504 | }; | |
505 | } | |
506 | ||
573087b4 MAL |
507 | private static class EventsTableSelectionCondition extends DefaultCondition { |
508 | private long fSelectionTime; | |
509 | private SWTWorkbenchBot fBot; | |
9bb4b61e | 510 | private long fCurValue; |
573087b4 MAL |
511 | |
512 | private EventsTableSelectionCondition(SWTWorkbenchBot bot, long selectionTime) { | |
513 | fBot = bot; | |
514 | fSelectionTime = selectionTime; | |
515 | } | |
516 | ||
517 | @Override | |
518 | public boolean test() throws Exception { | |
519 | StructuredSelection eventsTableSelection = getEventsTableSelection(); | |
520 | if (eventsTableSelection.isEmpty()) { | |
521 | return false; | |
522 | } | |
9bb4b61e MAL |
523 | fCurValue = ((ITmfEvent) eventsTableSelection.getFirstElement()).getTimestamp().getValue(); |
524 | return fCurValue == fSelectionTime; | |
573087b4 MAL |
525 | } |
526 | ||
527 | @Override | |
528 | public String getFailureMessage() { | |
9bb4b61e | 529 | return "The selection in the table was not an event with timestamp " + fSelectionTime + ". Actual is " + fCurValue; |
573087b4 MAL |
530 | } |
531 | ||
532 | private StructuredSelection getEventsTableSelection() { | |
533 | return UIThreadRunnable.syncExec(new Result<StructuredSelection>() { | |
534 | ||
535 | @Override | |
536 | public StructuredSelection run() { | |
537 | SWTBotEditor eventsEditor = SWTBotUtils.activeEventsEditor(fBot); | |
538 | TmfEventsEditor part = (TmfEventsEditor) eventsEditor.getReference().getPart(false); | |
539 | StructuredSelection selection = (StructuredSelection) part.getSelection(); | |
540 | return selection; | |
541 | } | |
542 | }); | |
543 | } | |
544 | } | |
545 | ||
546 | /** | |
547 | * Wait until the events table selection matches the specified time stamp. | |
548 | * | |
549 | * @param bot | |
550 | * a workbench bot | |
551 | * | |
552 | * @param selectionTime | |
553 | * the selection time | |
554 | * @return ICondition for verification | |
555 | */ | |
556 | public static ICondition selectionInEventsTable(final SWTWorkbenchBot bot, long selectionTime) { | |
557 | return new EventsTableSelectionCondition(bot, selectionTime); | |
558 | } | |
156e9ead MAL |
559 | |
560 | private static class TimeGraphIsReadyCondition extends DefaultCondition { | |
561 | ||
562 | private @NonNull TmfTimeRange fSelectionRange; | |
563 | private @NonNull ITmfTimestamp fVisibleTime; | |
564 | private AbstractTimeGraphView fView; | |
f149d124 | 565 | private String fFailureMessage; |
156e9ead MAL |
566 | |
567 | private TimeGraphIsReadyCondition(AbstractTimeGraphView view, @NonNull TmfTimeRange selectionRange, @NonNull ITmfTimestamp visibleTime) { | |
568 | fView = view; | |
569 | fSelectionRange = selectionRange; | |
570 | fVisibleTime = visibleTime; | |
571 | } | |
572 | ||
573 | @Override | |
574 | public boolean test() throws Exception { | |
f149d124 MAL |
575 | ICondition selectionRangeCondition = ConditionHelpers.selectionRange(fSelectionRange); |
576 | if (!selectionRangeCondition.test()) { | |
577 | fFailureMessage = selectionRangeCondition.getFailureMessage(); | |
156e9ead MAL |
578 | return false; |
579 | } | |
f149d124 MAL |
580 | @NonNull TmfTimeRange curWindowRange = TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange(); |
581 | if (!curWindowRange.contains(fVisibleTime)) { | |
582 | fFailureMessage = "Current window range " + curWindowRange + " does not contain " + fVisibleTime; | |
156e9ead MAL |
583 | return false; |
584 | } | |
f149d124 MAL |
585 | |
586 | if (fView.isDirty()) { | |
587 | fFailureMessage = "Time graph is dirty"; | |
588 | return false; | |
589 | ||
590 | } | |
591 | return true; | |
156e9ead MAL |
592 | } |
593 | ||
594 | @Override | |
595 | public String getFailureMessage() { | |
f149d124 | 596 | return fFailureMessage; |
156e9ead MAL |
597 | } |
598 | } | |
599 | ||
600 | /** | |
601 | * | |
602 | * Wait until the Time Graph view is ready. The Time Graph view is | |
603 | * considered ready if the selectionRange is selected, the visibleTime is | |
604 | * visible and the view is not dirty (its model is done updating). | |
605 | * | |
606 | * @param view | |
607 | * the time graph view | |
608 | * @param selectionRange | |
609 | * the selection that the time graph should have | |
610 | * @param visibleTime | |
611 | * the visible time that the time graph should have | |
612 | * @return ICondition for verification | |
613 | */ | |
614 | public static ICondition timeGraphIsReadyCondition(AbstractTimeGraphView view, @NonNull TmfTimeRange selectionRange, @NonNull ITmfTimestamp visibleTime) { | |
615 | return new TimeGraphIsReadyCondition(view, selectionRange, visibleTime); | |
616 | } | |
e834a6b4 | 617 | |
c27e9fec GB |
618 | private static class XYViewerIsReadyCondition extends DefaultCondition { |
619 | ||
620 | private TmfXYChartViewer fViewer; | |
621 | private String fFailureMessage; | |
622 | ||
623 | private XYViewerIsReadyCondition(TmfXYChartViewer view) { | |
624 | fViewer = view; | |
625 | } | |
626 | ||
627 | @Override | |
628 | public boolean test() throws Exception { | |
629 | ||
630 | if (fViewer.isDirty()) { | |
631 | fFailureMessage = "Time graph is dirty"; | |
632 | return false; | |
633 | } | |
634 | return true; | |
635 | } | |
636 | ||
637 | @Override | |
638 | public String getFailureMessage() { | |
639 | return fFailureMessage; | |
640 | } | |
641 | } | |
642 | ||
643 | /** | |
644 | * | |
645 | * Wait until the XY chart viewer is ready. The XY chart viewer is | |
646 | * considered ready when it is not updating. | |
647 | * | |
648 | * @param viewer | |
649 | * the XY chart viewer | |
650 | * @return ICondition for verification | |
651 | */ | |
652 | public static ICondition xyViewerIsReadyCondition(TmfXYChartViewer viewer) { | |
653 | return new XYViewerIsReadyCondition(viewer); | |
654 | } | |
655 | ||
e834a6b4 MAL |
656 | private static class NumberOfEventsCondition extends DefaultCondition { |
657 | ||
658 | private ITmfTrace fTrace; | |
659 | private long fNbEvents; | |
660 | ||
661 | private NumberOfEventsCondition(ITmfTrace trace, long nbEvents) { | |
662 | fTrace = trace; | |
663 | fNbEvents = nbEvents; | |
664 | } | |
665 | ||
666 | @Override | |
667 | public boolean test() throws Exception { | |
668 | return fTrace.getNbEvents() == fNbEvents; | |
669 | } | |
670 | ||
671 | @Override | |
672 | public String getFailureMessage() { | |
673 | return fTrace.getName() + " did not contain the expected number of " + fNbEvents + " events. Current: " + fTrace.getNbEvents(); | |
674 | } | |
675 | } | |
676 | ||
677 | /** | |
678 | * Wait until the trace contains the specified number of events. | |
679 | * | |
680 | * @param trace | |
681 | * the trace | |
682 | * @param nbEvents | |
683 | * the number of events to wait for | |
684 | * @return ICondition for verification | |
685 | */ | |
686 | public static ICondition numberOfEventsInTrace(ITmfTrace trace, long nbEvents) { | |
687 | return new NumberOfEventsCondition(trace, nbEvents); | |
688 | } | |
689 | ||
690 | /** | |
691 | * Wait until there is an active events editor. A title can also be | |
692 | * specified to wait until a more specific editor. | |
693 | */ | |
694 | public static final class ActiveEventsEditor extends DefaultCondition { | |
695 | private final SWTWorkbenchBot fWorkbenchBot; | |
696 | private SWTBotEditor fEditor; | |
697 | private String fEditorTitle; | |
698 | ||
699 | /** | |
700 | * Wait until there is an active events editor. | |
701 | * | |
702 | * @param workbenchBot | |
703 | * a workbench bot | |
704 | * @param editorTitle | |
705 | * If specified, wait until an active events editor with this | |
706 | * title. Can be set to null. | |
707 | */ | |
708 | public ActiveEventsEditor(SWTWorkbenchBot workbenchBot, String editorTitle) { | |
709 | fWorkbenchBot = workbenchBot; | |
710 | fEditorTitle = editorTitle; | |
711 | } | |
712 | ||
713 | @Override | |
714 | public boolean test() throws Exception { | |
715 | List<SWTBotEditor> editors = fWorkbenchBot.editors(WidgetMatcherFactory.withPartId(TmfEventsEditor.ID)); | |
716 | for (SWTBotEditor e : editors) { | |
717 | // We are careful not to call e.getWidget() here because it actually forces the editor to show. | |
718 | // This is especially a problem for cases where we wait until there is no active editor. | |
719 | if (e.isActive()) { | |
720 | if (fEditorTitle != null && !fEditorTitle.equals(e.getTitle())) { | |
721 | return false; | |
722 | } | |
723 | fEditor = e; | |
724 | return true; | |
725 | } | |
726 | } | |
727 | return false; | |
728 | } | |
729 | ||
730 | @Override | |
731 | public String getFailureMessage() { | |
732 | String editorMessage = fEditorTitle != null ? " " + fEditorTitle : ""; | |
733 | return "Active events editor" + editorMessage + " not found"; | |
734 | } | |
735 | ||
736 | /** | |
737 | * @return The active editor found | |
738 | */ | |
739 | public SWTBotEditor getActiveEditor() { | |
740 | return fEditor; | |
741 | } | |
742 | } | |
aa5b9bd6 MAL |
743 | |
744 | private static class NumberOfSeries extends DefaultCondition { | |
745 | private String fFailureMessage; | |
746 | private Chart fChart; | |
747 | private final int fNumberOfSeries; | |
748 | ||
749 | public NumberOfSeries(Chart chart, int numberOfSeries) { | |
750 | fChart = chart; | |
751 | fNumberOfSeries = numberOfSeries; | |
752 | } | |
753 | ||
754 | @Override | |
755 | public boolean test() throws Exception { | |
756 | int length = fChart.getSeriesSet().getSeries().length; | |
757 | if (length != fNumberOfSeries){ | |
758 | fFailureMessage = "Chart did not contain the expected number series. Actual " + length + ", expected " + fNumberOfSeries; | |
759 | return false; | |
760 | } | |
761 | ||
762 | return true; | |
763 | } | |
764 | ||
765 | @Override | |
766 | public String getFailureMessage() { | |
767 | return fFailureMessage; | |
768 | } | |
769 | } | |
770 | ||
771 | /** | |
772 | * Wait until the chart has the specified number of series. | |
773 | * | |
774 | * @param chart | |
775 | * the chart | |
776 | * @param numberOfSeries | |
777 | * the number of expected series | |
778 | * | |
779 | * @return ICondition for verification | |
780 | */ | |
781 | public static ICondition numberOfSeries(Chart chart, int numberOfSeries) { | |
782 | return new NumberOfSeries(chart, numberOfSeries); | |
783 | } | |
1b9a8a8c MAL |
784 | |
785 | /** | |
786 | * Condition to check if the tree item has children | |
787 | * | |
788 | * @param treeItem | |
789 | * the tree item that should have children | |
790 | * @return ICondition for verification | |
791 | */ | |
792 | public static ICondition treeItemHasChildren(SWTBotTreeItem treeItem) { | |
793 | return new TreeItemHasChildren(treeItem); | |
794 | } | |
795 | ||
796 | private static final class TreeItemHasChildren extends DefaultCondition { | |
797 | private SWTBotTreeItem fTreeItem; | |
798 | ||
799 | public TreeItemHasChildren(SWTBotTreeItem treeItem) { | |
800 | fTreeItem = treeItem; | |
801 | } | |
802 | ||
803 | @Override | |
804 | public boolean test() throws Exception { | |
805 | return fTreeItem.getItems().length > 0; | |
806 | } | |
807 | ||
808 | @Override | |
809 | public String getFailureMessage() { | |
810 | return NLS.bind("No child of tree item {0} found.", new String[] { fTreeItem.toString() }); | |
811 | } | |
812 | } | |
88051e61 | 813 | } |