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
.utils
.TableCollection
;
28 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
29 import org
.eclipse
.swtbot
.swt
.finder
.waits
.ICondition
;
30 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
31 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
32 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
35 import org
.eclipse
.ui
.IEditorReference
;
36 import org
.hamcrest
.Matcher
;
39 * Is a tree node available
41 * @author Matthew Khouzam
43 public final class ConditionHelpers
{
45 private ConditionHelpers() {}
48 * Provide default implementations for some {@link ICondition} methods.
50 private abstract static class SWTBotTestCondition
implements ICondition
{
53 public abstract boolean test() throws Exception
;
56 public final void init(SWTBot bot
) {
60 public String
getFailureMessage() {
66 * Is a tree node available
69 * the name of the node
72 * @return true or false, it should swallow all exceptions
74 public static ICondition
IsTreeNodeAvailable(final String name
, final SWTBotTree tree
) {
75 return new SWTBotTestCondition() {
77 public boolean test() throws Exception
{
79 final SWTBotTreeItem
[] treeItems
= tree
.getAllItems();
80 for (SWTBotTreeItem ti
: treeItems
) {
81 final String text
= ti
.getText();
82 if (text
.equals(name
)) {
86 } catch (Exception e
) {
94 * Is a table item available
97 * the name of the item
100 * @return true or false, it should swallow all exceptions
102 public static ICondition
isTableItemAvailable(final String name
, final SWTBotTable table
) {
103 return new SWTBotTestCondition() {
105 public boolean test() throws Exception
{
107 return table
.containsItem(name
);
108 } catch (Exception e
) {
116 * Is the treeItem's node available
119 * the name of the node
122 * @return true or false, it should swallow all exceptions
124 public static ICondition
IsTreeChildNodeAvailable(final String name
, final SWTBotTreeItem treeItem
) {
125 return new SWTBotTestCondition() {
127 public boolean test() throws Exception
{
129 return treeItem
.getNode(name
) != null;
130 } catch (Exception e
) {
138 * Checks if the wizard's shell is null
142 * @return false if either are null
144 public static ICondition
isWizardReady(final Wizard wizard
) {
145 return new SWTBotTestCondition() {
147 public boolean test() throws Exception
{
148 if (wizard
.getShell() == null) {
157 * Is the wizard on the page you want?
163 * @return true or false
165 public static ICondition
isWizardOnPage(final Wizard wizard
, final IWizardPage page
) {
166 return new SWTBotTestCondition() {
168 public boolean test() throws Exception
{
169 if (wizard
== null || page
== null) {
172 final IWizardContainer container
= wizard
.getContainer();
173 if (container
== null) {
176 IWizardPage currentPage
= container
.getCurrentPage();
177 return page
.equals(currentPage
);
183 * Wait for a view to close
186 * bot view for the view
187 * @return true if the view is closed, false if it's active.
189 public static ICondition
ViewIsClosed(final SWTBotView view
) {
190 return new SWTBotTestCondition() {
192 public boolean test() throws Exception
{
193 return (view
!= null) && (!view
.isActive());
199 * Wait till table cell has a given content.
202 * the table bot reference
204 * the content to check
206 * the row of the cell
208 * the column of the cell
209 * @return ICondition for verification
211 public static ICondition
isTableCellFilled(final SWTBotTable table
,
212 final String content
, final int row
, final int column
) {
213 return new SWTBotTestCondition() {
215 public boolean test() throws Exception
{
217 String cell
= table
.cell(row
, column
);
221 return cell
.endsWith(content
);
222 } catch (Exception e
) {
230 * Condition to check if a tracing project element has a child with the
231 * specified name. A project element label may have a count suffix in the
234 public static class ProjectElementHasChild
extends DefaultCondition
{
236 private final SWTBotTreeItem fParentItem
;
237 private final String fName
;
238 private final String fRegex
;
239 private SWTBotTreeItem fItem
= null;
247 * the child name to look for
249 public ProjectElementHasChild(final SWTBotTreeItem parentItem
, final String name
) {
250 fParentItem
= parentItem
;
252 /* Project element labels may have count suffix */
253 fRegex
= name
+ "(\\s\\[(\\d)+\\])?";
257 public boolean test() throws Exception
{
258 fParentItem
.expand();
259 for (SWTBotTreeItem item
: fParentItem
.getItems()) {
260 if (item
.getText().matches(fRegex
)) {
269 public String
getFailureMessage() {
270 return NLS
.bind("No child of {0} found with name {1}", fParentItem
.getText(), fName
);
274 * Returns the matching child item if the condition returned true.
276 * @return the matching item
278 public SWTBotTreeItem
getItem() {
284 * Condition to check if an editor with the specified title is opened.
290 * @return ICondition for verification
292 public static ICondition
isEditorOpened(final SWTWorkbenchBot bot
, final String title
) {
293 return new SWTBotTestCondition() {
295 public boolean test() throws Exception
{
296 Matcher
<IEditorReference
> withPartName
= withPartName(title
);
297 return !bot
.editors(withPartName
).isEmpty();
303 * Condition to check if the selection range equals the specified range.
306 * the selection range
307 * @return ICondition for verification
309 public static ICondition
selectionRange(final TmfTimeRange range
) {
310 return new SWTBotTestCondition() {
312 public boolean test() throws Exception
{
313 return TmfTraceManager
.getInstance().getCurrentTraceContext().getSelectionRange().equals(range
);
317 public String
getFailureMessage() {
318 return NLS
.bind("Selection range: {0} expected: {1}",
319 TmfTraceManager
.getInstance().getCurrentTraceContext().getSelectionRange(), range
);
325 * Condition to check if the window range equals the specified range.
329 * @return ICondition for verification
331 public static ICondition
windowRange(final TmfTimeRange range
) {
332 return new SWTBotTestCondition() {
334 public boolean test() throws Exception
{
335 return TmfTraceManager
.getInstance().getCurrentTraceContext().getWindowRange().equals(range
);
339 public String
getFailureMessage() {
340 return NLS
.bind("Window range: {0} expected: {1}",
341 TmfTraceManager
.getInstance().getCurrentTraceContext().getWindowRange(), range
);
347 * Condition to check if the selection contains the specified text at the
348 * specified column. The text is checked in any item of the tree selection.
356 * @return ICondition for verification
358 public static ICondition
treeSelectionContains(final SWTBotTree tree
, final int column
, final String text
) {
359 return new SWTBotTestCondition() {
361 public boolean test() throws Exception
{
362 TableCollection selection
= tree
.selection();
363 for (int row
= 0; row
< selection
.rowCount(); row
++) {
364 if (selection
.get(row
, column
).equals(text
)) {
372 public String
getFailureMessage() {
373 return NLS
.bind("Tree selection [0,{0}]: {1} expected: {2}",
374 new Object
[] { column
, tree
.selection().get(0, column
), text
});