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
;
18 import static org
.eclipse
.swtbot
.eclipse
.finder
.matchers
.WidgetMatcherFactory
.withPartName
;
20 import org
.eclipse
.jface
.wizard
.IWizardContainer
;
21 import org
.eclipse
.jface
.wizard
.IWizardPage
;
22 import org
.eclipse
.jface
.wizard
.Wizard
;
23 import org
.eclipse
.osgi
.util
.NLS
;
24 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
25 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
26 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
27 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
28 import org
.eclipse
.swtbot
.swt
.finder
.waits
.ICondition
;
29 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
30 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
31 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
32 import org
.eclipse
.ui
.IEditorReference
;
33 import org
.hamcrest
.Matcher
;
36 * Is a tree node available
38 * @author Matthew Khouzam
40 public final class ConditionHelpers
{
42 private ConditionHelpers() {}
45 * Provide default implementations for some {@link ICondition} methods.
47 private abstract static class SWTBotTestCondition
implements ICondition
{
50 public abstract boolean test() throws Exception
;
53 public final void init(SWTBot bot
) {
57 public final String
getFailureMessage() {
63 * Is a tree node available
66 * the name of the node
69 * @return true or false, it should swallow all exceptions
71 public static ICondition
IsTreeNodeAvailable(final String name
, final SWTBotTree tree
) {
72 return new SWTBotTestCondition() {
74 public boolean test() throws Exception
{
76 final SWTBotTreeItem
[] treeItems
= tree
.getAllItems();
77 for (SWTBotTreeItem ti
: treeItems
) {
78 final String text
= ti
.getText();
79 if (text
.equals(name
)) {
83 } catch (Exception e
) {
91 * Is the treeItem's node available
94 * the name of the node
97 * @return true or false, it should swallow all exceptions
99 public static ICondition
IsTreeChildNodeAvailable(final String name
, final SWTBotTreeItem treeItem
) {
100 return new SWTBotTestCondition() {
102 public boolean test() throws Exception
{
104 return treeItem
.getNode(name
) != null;
105 } catch (Exception e
) {
113 * Checks if the wizard's shell is null
117 * @return false if either are null
119 public static ICondition
isWizardReady(final Wizard wizard
) {
120 return new SWTBotTestCondition() {
122 public boolean test() throws Exception
{
123 if (wizard
.getShell() == null) {
132 * Is the wizard on the page you want?
138 * @return true or false
140 public static ICondition
isWizardOnPage(final Wizard wizard
, final IWizardPage page
) {
141 return new SWTBotTestCondition() {
143 public boolean test() throws Exception
{
144 if (wizard
== null || page
== null) {
147 final IWizardContainer container
= wizard
.getContainer();
148 if (container
== null) {
151 IWizardPage currentPage
= container
.getCurrentPage();
152 return page
.equals(currentPage
);
158 * Wait for a view to close
161 * bot view for the view
162 * @return true if the view is closed, false if it's active.
164 public static ICondition
ViewIsClosed(final SWTBotView view
) {
165 return new SWTBotTestCondition() {
167 public boolean test() throws Exception
{
168 return (view
!= null) && (!view
.isActive());
174 * Wait till table cell has a given content.
177 * the table bot reference
179 * the content to check
181 * the row of the cell
183 * the column of the cell
184 * @return ICondition for verification
186 public static ICondition
isTableCellFilled(final SWTBotTable table
,
187 final String content
, final int row
, final int column
) {
188 return new SWTBotTestCondition() {
190 public boolean test() throws Exception
{
192 String cell
= table
.cell(row
, column
);
196 return cell
.endsWith(content
);
197 } catch (Exception e
) {
205 * Condition to check if a tracing project element has a child with the
206 * specified name. A project element label may have a count suffix in the
209 public static class ProjectElementHasChild
extends DefaultCondition
{
211 private final SWTBotTreeItem fParentItem
;
212 private final String fName
;
213 private final String fRegex
;
214 private SWTBotTreeItem fItem
= null;
222 * the child name to look for
224 public ProjectElementHasChild(final SWTBotTreeItem parentItem
, final String name
) {
225 fParentItem
= parentItem
;
227 /* Project element labels may have count suffix */
228 fRegex
= name
+ "(\\s\\[(\\d)+\\])?";
232 public boolean test() throws Exception
{
233 fParentItem
.expand();
234 for (SWTBotTreeItem item
: fParentItem
.getItems()) {
235 if (item
.getText().matches(fRegex
)) {
244 public String
getFailureMessage() {
245 return NLS
.bind("No child of {0} found with name {1}", fParentItem
.getText(), fName
);
249 * Returns the matching child item if the condition returned true.
251 * @return the matching item
253 public SWTBotTreeItem
getItem() {
259 * Condition to check if an editor with the specified title is opened.
265 * @return ICondition for verification
267 public static ICondition
isEditorOpened(final SWTWorkbenchBot bot
, final String title
) {
268 return new SWTBotTestCondition() {
270 public boolean test() throws Exception
{
271 Matcher
<IEditorReference
> withPartName
= withPartName(title
);
272 return !bot
.editors(withPartName
).isEmpty();