1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 * Alexandre Montplaisir - Replaced separate Condition objects by anonymous classes
12 * Patrick Tasse - Add projectElementHasChild and isEditorOpened conditions
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
;
17 import static org
.eclipse
.swtbot
.eclipse
.finder
.matchers
.WidgetMatcherFactory
.withPartName
;
19 import java
.util
.Arrays
;
21 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
22 import org
.eclipse
.jface
.wizard
.IWizardContainer
;
23 import org
.eclipse
.jface
.wizard
.IWizardPage
;
24 import org
.eclipse
.jface
.wizard
.Wizard
;
25 import org
.eclipse
.osgi
.util
.NLS
;
26 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
27 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
28 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
29 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
30 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
31 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
32 import org
.eclipse
.swtbot
.swt
.finder
.utils
.TableCollection
;
33 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
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
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
42 import org
.eclipse
.ui
.IEditorReference
;
43 import org
.hamcrest
.Matcher
;
46 * Is a tree node available
48 * @author Matthew Khouzam
50 public final class ConditionHelpers
{
52 private ConditionHelpers() {}
55 * Provide default implementations for some {@link ICondition} methods.
57 private abstract static class SWTBotTestCondition
implements ICondition
{
60 public abstract boolean test() throws Exception
;
63 public final void init(SWTBot bot
) {
67 public String
getFailureMessage() {
73 * Is a tree node available
76 * the name of the node
79 * @return true or false, it should swallow all exceptions
81 public static ICondition
IsTreeNodeAvailable(final String name
, final SWTBotTree tree
) {
82 return new SWTBotTestCondition() {
84 public boolean test() throws Exception
{
86 final SWTBotTreeItem
[] treeItems
= tree
.getAllItems();
87 for (SWTBotTreeItem ti
: treeItems
) {
88 final String text
= ti
.getText();
89 if (text
.equals(name
)) {
93 } catch (Exception e
) {
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()) });
107 * Is a table item available
110 * the name of the item
113 * @return true or false, it should swallow all exceptions
115 public static ICondition
isTableItemAvailable(final String name
, final SWTBotTable table
) {
116 return new SWTBotTestCondition() {
118 public boolean test() throws Exception
{
120 return table
.containsItem(name
);
121 } catch (Exception e
) {
127 public String
getFailureMessage() {
128 return NLS
.bind("No child of table {0} found with text '{1}'.", table
, name
);
134 * Is the treeItem's node available
137 * the name of the node
140 * @return true or false, it should swallow all exceptions
142 public static ICondition
IsTreeChildNodeAvailable(final String name
, final SWTBotTreeItem treeItem
) {
143 return new SWTBotTestCondition() {
145 public boolean test() throws Exception
{
147 return treeItem
.getNode(name
) != null;
148 } catch (Exception e
) {
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()) });
162 * Checks if the wizard's shell is null
166 * @return false if either are null
168 public static ICondition
isWizardReady(final Wizard wizard
) {
169 return new SWTBotTestCondition() {
171 public boolean test() throws Exception
{
172 if (wizard
.getShell() == null) {
181 * Is the wizard on the page you want?
187 * @return true or false
189 public static ICondition
isWizardOnPage(final Wizard wizard
, final IWizardPage page
) {
190 return new SWTBotTestCondition() {
192 public boolean test() throws Exception
{
193 if (wizard
== null || page
== null) {
196 final IWizardContainer container
= wizard
.getContainer();
197 if (container
== null) {
200 IWizardPage currentPage
= container
.getCurrentPage();
201 return page
.equals(currentPage
);
207 * Wait for a view to close
210 * bot view for the view
211 * @return true if the view is closed, false if it's active.
213 public static ICondition
ViewIsClosed(final SWTBotView view
) {
214 return new SWTBotTestCondition() {
216 public boolean test() throws Exception
{
217 return (view
!= null) && (!view
.isActive());
223 * Wait till table cell has a given content.
226 * the table bot reference
228 * the content to check
230 * the row of the cell
232 * the column of the cell
233 * @return ICondition for verification
235 public static ICondition
isTableCellFilled(final SWTBotTable table
,
236 final String content
, final int row
, final int column
) {
237 return new SWTBotTestCondition() {
239 public boolean test() throws Exception
{
241 String cell
= table
.cell(row
, column
);
245 return cell
.endsWith(content
);
246 } catch (Exception e
) {
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
258 public static class ProjectElementHasChild
extends DefaultCondition
{
260 private final SWTBotTreeItem fParentItem
;
261 private final String fName
;
262 private final String fRegex
;
263 private SWTBotTreeItem fItem
= null;
271 * the child name to look for
273 public ProjectElementHasChild(final SWTBotTreeItem parentItem
, final String name
) {
274 fParentItem
= parentItem
;
276 /* Project element labels may have count suffix */
277 fRegex
= name
+ "(\\s\\[(\\d)+\\])?";
281 public boolean test() throws Exception
{
282 fParentItem
.expand();
283 for (SWTBotTreeItem item
: fParentItem
.getItems()) {
284 if (item
.getText().matches(fRegex
)) {
293 public String
getFailureMessage() {
294 return NLS
.bind("No child of {0} found with name {1}", fParentItem
.getText(), fName
);
298 * Returns the matching child item if the condition returned true.
300 * @return the matching item
302 public SWTBotTreeItem
getItem() {
308 * Condition to check if an editor with the specified title is opened.
314 * @return ICondition for verification
316 public static ICondition
isEditorOpened(final SWTWorkbenchBot bot
, final String title
) {
317 return new SWTBotTestCondition() {
319 public boolean test() throws Exception
{
320 Matcher
<IEditorReference
> withPartName
= withPartName(title
);
321 return !bot
.editors(withPartName
).isEmpty();
327 * Condition to check if the selection range equals the specified range.
330 * the selection range
331 * @return ICondition for verification
333 public static ICondition
selectionRange(final TmfTimeRange range
) {
334 return new SWTBotTestCondition() {
336 public boolean test() throws Exception
{
337 return TmfTraceManager
.getInstance().getCurrentTraceContext().getSelectionRange().equals(range
);
341 public String
getFailureMessage() {
342 return NLS
.bind("Selection range: {0} expected: {1}",
343 TmfTraceManager
.getInstance().getCurrentTraceContext().getSelectionRange(), range
);
349 * Condition to check if the window range equals the specified range.
353 * @return ICondition for verification
355 public static ICondition
windowRange(final TmfTimeRange range
) {
356 return new SWTBotTestCondition() {
358 public boolean test() throws Exception
{
359 return TmfTraceManager
.getInstance().getCurrentTraceContext().getWindowRange().equals(range
);
363 public String
getFailureMessage() {
364 return NLS
.bind("Window range: {0} expected: {1}",
365 TmfTraceManager
.getInstance().getCurrentTraceContext().getWindowRange(), range
);
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.
380 * @return ICondition for verification
382 public static ICondition
treeSelectionContains(final SWTBotTree tree
, final int column
, final String text
) {
383 return new SWTBotTestCondition() {
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
)) {
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
});
403 private static class EventsTableSelectionCondition
extends DefaultCondition
{
404 private long fSelectionTime
;
405 private SWTWorkbenchBot fBot
;
407 private EventsTableSelectionCondition(SWTWorkbenchBot bot
, long selectionTime
) {
409 fSelectionTime
= selectionTime
;
413 public boolean test() throws Exception
{
414 StructuredSelection eventsTableSelection
= getEventsTableSelection();
415 if (eventsTableSelection
.isEmpty()) {
418 return ((ITmfEvent
) eventsTableSelection
.getFirstElement()).getTimestamp().getValue() == fSelectionTime
;
422 public String
getFailureMessage() {
423 return "The selection in the table was not an event with timestamp " + fSelectionTime
;
426 private StructuredSelection
getEventsTableSelection() {
427 return UIThreadRunnable
.syncExec(new Result
<StructuredSelection
>() {
430 public StructuredSelection
run() {
431 SWTBotEditor eventsEditor
= SWTBotUtils
.activeEventsEditor(fBot
);
432 TmfEventsEditor part
= (TmfEventsEditor
) eventsEditor
.getReference().getPart(false);
433 StructuredSelection selection
= (StructuredSelection
) part
.getSelection();
441 * Wait until the events table selection matches the specified time stamp.
446 * @param selectionTime
448 * @return ICondition for verification
450 public static ICondition
selectionInEventsTable(final SWTWorkbenchBot bot
, long selectionTime
) {
451 return new EventsTableSelectionCondition(bot
, selectionTime
);