tmf: Add periodic marker event source
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / ConditionHelpers.java
CommitLineData
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
15package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared;
16
88051e61
PT
17import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartName;
18
46958d08
PT
19import java.util.Arrays;
20
156e9ead 21import org.eclipse.jdt.annotation.NonNull;
573087b4 22import org.eclipse.jface.viewers.StructuredSelection;
fa24d78b
AM
23import org.eclipse.jface.wizard.IWizardContainer;
24import org.eclipse.jface.wizard.IWizardPage;
25import org.eclipse.jface.wizard.Wizard;
21e5206c 26import org.eclipse.osgi.util.NLS;
88051e61 27import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
573087b4 28import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
fa24d78b
AM
29import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
30import org.eclipse.swtbot.swt.finder.SWTBot;
573087b4
MAL
31import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
32import org.eclipse.swtbot.swt.finder.results.Result;
6e4a07af 33import org.eclipse.swtbot.swt.finder.utils.TableCollection;
21e5206c 34import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
fa24d78b
AM
35import org.eclipse.swtbot.swt.finder.waits.ICondition;
36import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
37import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
38import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
573087b4 39import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
156e9ead 40import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
2fe6a9ea
PT
41import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
42import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
573087b4 43import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
156e9ead 44import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractTimeGraphView;
88051e61
PT
45import org.eclipse.ui.IEditorReference;
46import org.hamcrest.Matcher;
fa24d78b
AM
47
48/**
49 * Is a tree node available
50 *
51 * @author Matthew Khouzam
52 */
53public 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;
437
438 private EventsTableSelectionCondition(SWTWorkbenchBot bot, long selectionTime) {
439 fBot = bot;
440 fSelectionTime = selectionTime;
441 }
442
443 @Override
444 public boolean test() throws Exception {
445 StructuredSelection eventsTableSelection = getEventsTableSelection();
446 if (eventsTableSelection.isEmpty()) {
447 return false;
448 }
449 return ((ITmfEvent) eventsTableSelection.getFirstElement()).getTimestamp().getValue() == fSelectionTime;
450 }
451
452 @Override
453 public String getFailureMessage() {
454 return "The selection in the table was not an event with timestamp " + fSelectionTime;
455 }
456
457 private StructuredSelection getEventsTableSelection() {
458 return UIThreadRunnable.syncExec(new Result<StructuredSelection>() {
459
460 @Override
461 public StructuredSelection run() {
462 SWTBotEditor eventsEditor = SWTBotUtils.activeEventsEditor(fBot);
463 TmfEventsEditor part = (TmfEventsEditor) eventsEditor.getReference().getPart(false);
464 StructuredSelection selection = (StructuredSelection) part.getSelection();
465 return selection;
466 }
467 });
468 }
469 }
470
471 /**
472 * Wait until the events table selection matches the specified time stamp.
473 *
474 * @param bot
475 * a workbench bot
476 *
477 * @param selectionTime
478 * the selection time
479 * @return ICondition for verification
480 */
481 public static ICondition selectionInEventsTable(final SWTWorkbenchBot bot, long selectionTime) {
482 return new EventsTableSelectionCondition(bot, selectionTime);
483 }
156e9ead
MAL
484
485 private static class TimeGraphIsReadyCondition extends DefaultCondition {
486
487 private @NonNull TmfTimeRange fSelectionRange;
488 private @NonNull ITmfTimestamp fVisibleTime;
489 private AbstractTimeGraphView fView;
490
491 private TimeGraphIsReadyCondition(AbstractTimeGraphView view, @NonNull TmfTimeRange selectionRange, @NonNull ITmfTimestamp visibleTime) {
492 fView = view;
493 fSelectionRange = selectionRange;
494 fVisibleTime = visibleTime;
495 }
496
497 @Override
498 public boolean test() throws Exception {
499 if (!ConditionHelpers.selectionRange(fSelectionRange).test()) {
500 return false;
501 }
502 if (!TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange().contains(fVisibleTime)) {
503 return false;
504 }
505 return !fView.isDirty();
506 }
507
508 @Override
509 public String getFailureMessage() {
510 return "Time graph is not ready";
511 }
512 }
513
514 /**
515 *
516 * Wait until the Time Graph view is ready. The Time Graph view is
517 * considered ready if the selectionRange is selected, the visibleTime is
518 * visible and the view is not dirty (its model is done updating).
519 *
520 * @param view
521 * the time graph view
522 * @param selectionRange
523 * the selection that the time graph should have
524 * @param visibleTime
525 * the visible time that the time graph should have
526 * @return ICondition for verification
527 */
528 public static ICondition timeGraphIsReadyCondition(AbstractTimeGraphView view, @NonNull TmfTimeRange selectionRange, @NonNull ITmfTimestamp visibleTime) {
529 return new TimeGraphIsReadyCondition(view, selectionRange, visibleTime);
530 }
88051e61 531}
This page took 0.069307 seconds and 5 git commands to generate.