swtbot: Fix TestTraceOffsetting
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / ConditionHelpers.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 Ericsson
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
12 * Patrick Tasse - Add projectElementHasChild and isEditorOpened conditions
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared;
16
17
18 import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartName;
19
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;
34
35 /**
36 * Is a tree node available
37 *
38 * @author Matthew Khouzam
39 */
40 public final class ConditionHelpers {
41
42 private ConditionHelpers() {}
43
44 /**
45 * Provide default implementations for some {@link ICondition} methods.
46 */
47 private abstract static class SWTBotTestCondition implements ICondition {
48
49 @Override
50 public abstract boolean test() throws Exception;
51
52 @Override
53 public final void init(SWTBot bot) {
54 }
55
56 @Override
57 public final String getFailureMessage() {
58 return null;
59 }
60 }
61
62 /**
63 * Is a tree node available
64 *
65 * @param name
66 * the name of the node
67 * @param tree
68 * the parent tree
69 * @return true or false, it should swallow all exceptions
70 */
71 public static ICondition IsTreeNodeAvailable(final String name, final SWTBotTree tree) {
72 return new SWTBotTestCondition() {
73 @Override
74 public boolean test() throws Exception {
75 try {
76 final SWTBotTreeItem[] treeItems = tree.getAllItems();
77 for (SWTBotTreeItem ti : treeItems) {
78 final String text = ti.getText();
79 if (text.equals(name)) {
80 return true;
81 }
82 }
83 } catch (Exception e) {
84 }
85 return false;
86 }
87 };
88 }
89
90 /**
91 * Is the treeItem's node available
92 *
93 * @param name
94 * the name of the node
95 * @param treeItem
96 * the treeItem
97 * @return true or false, it should swallow all exceptions
98 */
99 public static ICondition IsTreeChildNodeAvailable(final String name, final SWTBotTreeItem treeItem) {
100 return new SWTBotTestCondition() {
101 @Override
102 public boolean test() throws Exception {
103 try {
104 return treeItem.getNode(name) != null;
105 } catch (Exception e) {
106 }
107 return false;
108 }
109 };
110 }
111
112 /**
113 * Checks if the wizard's shell is null
114 *
115 * @param wizard
116 * the null
117 * @return false if either are null
118 */
119 public static ICondition isWizardReady(final Wizard wizard) {
120 return new SWTBotTestCondition() {
121 @Override
122 public boolean test() throws Exception {
123 if (wizard.getShell() == null) {
124 return false;
125 }
126 return true;
127 }
128 };
129 }
130
131 /**
132 * Is the wizard on the page you want?
133 *
134 * @param wizard
135 * wizard
136 * @param page
137 * the desired page
138 * @return true or false
139 */
140 public static ICondition isWizardOnPage(final Wizard wizard, final IWizardPage page) {
141 return new SWTBotTestCondition() {
142 @Override
143 public boolean test() throws Exception {
144 if (wizard == null || page == null) {
145 return false;
146 }
147 final IWizardContainer container = wizard.getContainer();
148 if (container == null) {
149 return false;
150 }
151 IWizardPage currentPage = container.getCurrentPage();
152 return page.equals(currentPage);
153 }
154 };
155 }
156
157 /**
158 * Wait for a view to close
159 *
160 * @param view
161 * bot view for the view
162 * @return true if the view is closed, false if it's active.
163 */
164 public static ICondition ViewIsClosed(final SWTBotView view) {
165 return new SWTBotTestCondition() {
166 @Override
167 public boolean test() throws Exception {
168 return (view != null) && (!view.isActive());
169 }
170 };
171 }
172
173 /**
174 * Wait till table cell has a given content.
175 *
176 * @param table
177 * the table bot reference
178 * @param content
179 * the content to check
180 * @param row
181 * the row of the cell
182 * @param column
183 * the column of the cell
184 * @return ICondition for verification
185 */
186 public static ICondition isTableCellFilled(final SWTBotTable table,
187 final String content, final int row, final int column) {
188 return new SWTBotTestCondition() {
189 @Override
190 public boolean test() throws Exception {
191 try {
192 String cell = table.cell(row, column);
193 if( cell == null ) {
194 return false;
195 }
196 return cell.endsWith(content);
197 } catch (Exception e) {
198 }
199 return false;
200 }
201 };
202 }
203
204 /**
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
207 * format ' [n]'.
208 */
209 public static class ProjectElementHasChild extends DefaultCondition {
210
211 private final SWTBotTreeItem fParentItem;
212 private final String fName;
213 private final String fRegex;
214 private SWTBotTreeItem fItem = null;
215
216 /**
217 * Constructor.
218 *
219 * @param parentItem
220 * the parent item
221 * @param name
222 * the child name to look for
223 */
224 public ProjectElementHasChild(final SWTBotTreeItem parentItem, final String name) {
225 fParentItem = parentItem;
226 fName = name;
227 /* Project element labels may have count suffix */
228 fRegex = name + "(\\s\\[(\\d)+\\])?";
229 }
230
231 @Override
232 public boolean test() throws Exception {
233 fParentItem.expand();
234 for (SWTBotTreeItem item : fParentItem.getItems()) {
235 if (item.getText().matches(fRegex)) {
236 fItem = item;
237 return true;
238 }
239 }
240 return false;
241 }
242
243 @Override
244 public String getFailureMessage() {
245 return NLS.bind("No child of {0} found with name {1}", fParentItem.getText(), fName);
246 }
247
248 /**
249 * Returns the matching child item if the condition returned true.
250 *
251 * @return the matching item
252 */
253 public SWTBotTreeItem getItem() {
254 return fItem;
255 }
256 }
257
258 /**
259 * Condition to check if an editor with the specified title is opened.
260 *
261 * @param bot
262 * a workbench bot
263 * @param title
264 * the editor title
265 * @return ICondition for verification
266 */
267 public static ICondition isEditorOpened(final SWTWorkbenchBot bot, final String title) {
268 return new SWTBotTestCondition() {
269 @Override
270 public boolean test() throws Exception {
271 Matcher<IEditorReference> withPartName = withPartName(title);
272 return !bot.editors(withPartName).isEmpty();
273 }
274 };
275 }
276 }
This page took 0.062889 seconds and 6 git commands to generate.