| 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.tracecompass.tmf.core.timestamp.TmfTimeRange; |
| 33 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
| 34 | import org.eclipse.ui.IEditorReference; |
| 35 | import org.hamcrest.Matcher; |
| 36 | |
| 37 | /** |
| 38 | * Is a tree node available |
| 39 | * |
| 40 | * @author Matthew Khouzam |
| 41 | */ |
| 42 | public final class ConditionHelpers { |
| 43 | |
| 44 | private ConditionHelpers() {} |
| 45 | |
| 46 | /** |
| 47 | * Provide default implementations for some {@link ICondition} methods. |
| 48 | */ |
| 49 | private abstract static class SWTBotTestCondition implements ICondition { |
| 50 | |
| 51 | @Override |
| 52 | public abstract boolean test() throws Exception; |
| 53 | |
| 54 | @Override |
| 55 | public final void init(SWTBot bot) { |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String getFailureMessage() { |
| 60 | return null; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Is a tree node available |
| 66 | * |
| 67 | * @param name |
| 68 | * the name of the node |
| 69 | * @param tree |
| 70 | * the parent tree |
| 71 | * @return true or false, it should swallow all exceptions |
| 72 | */ |
| 73 | public static ICondition IsTreeNodeAvailable(final String name, final SWTBotTree tree) { |
| 74 | return new SWTBotTestCondition() { |
| 75 | @Override |
| 76 | public boolean test() throws Exception { |
| 77 | try { |
| 78 | final SWTBotTreeItem[] treeItems = tree.getAllItems(); |
| 79 | for (SWTBotTreeItem ti : treeItems) { |
| 80 | final String text = ti.getText(); |
| 81 | if (text.equals(name)) { |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | } catch (Exception e) { |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Is a table item available |
| 94 | * |
| 95 | * @param name |
| 96 | * the name of the item |
| 97 | * @param table |
| 98 | * the parent table |
| 99 | * @return true or false, it should swallow all exceptions |
| 100 | */ |
| 101 | public static ICondition isTableItemAvailable(final String name, final SWTBotTable table) { |
| 102 | return new SWTBotTestCondition() { |
| 103 | @Override |
| 104 | public boolean test() throws Exception { |
| 105 | try { |
| 106 | return table.containsItem(name); |
| 107 | } catch (Exception e) { |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Is the treeItem's node available |
| 116 | * |
| 117 | * @param name |
| 118 | * the name of the node |
| 119 | * @param treeItem |
| 120 | * the treeItem |
| 121 | * @return true or false, it should swallow all exceptions |
| 122 | */ |
| 123 | public static ICondition IsTreeChildNodeAvailable(final String name, final SWTBotTreeItem treeItem) { |
| 124 | return new SWTBotTestCondition() { |
| 125 | @Override |
| 126 | public boolean test() throws Exception { |
| 127 | try { |
| 128 | return treeItem.getNode(name) != null; |
| 129 | } catch (Exception e) { |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | }; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Checks if the wizard's shell is null |
| 138 | * |
| 139 | * @param wizard |
| 140 | * the null |
| 141 | * @return false if either are null |
| 142 | */ |
| 143 | public static ICondition isWizardReady(final Wizard wizard) { |
| 144 | return new SWTBotTestCondition() { |
| 145 | @Override |
| 146 | public boolean test() throws Exception { |
| 147 | if (wizard.getShell() == null) { |
| 148 | return false; |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | }; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Is the wizard on the page you want? |
| 157 | * |
| 158 | * @param wizard |
| 159 | * wizard |
| 160 | * @param page |
| 161 | * the desired page |
| 162 | * @return true or false |
| 163 | */ |
| 164 | public static ICondition isWizardOnPage(final Wizard wizard, final IWizardPage page) { |
| 165 | return new SWTBotTestCondition() { |
| 166 | @Override |
| 167 | public boolean test() throws Exception { |
| 168 | if (wizard == null || page == null) { |
| 169 | return false; |
| 170 | } |
| 171 | final IWizardContainer container = wizard.getContainer(); |
| 172 | if (container == null) { |
| 173 | return false; |
| 174 | } |
| 175 | IWizardPage currentPage = container.getCurrentPage(); |
| 176 | return page.equals(currentPage); |
| 177 | } |
| 178 | }; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Wait for a view to close |
| 183 | * |
| 184 | * @param view |
| 185 | * bot view for the view |
| 186 | * @return true if the view is closed, false if it's active. |
| 187 | */ |
| 188 | public static ICondition ViewIsClosed(final SWTBotView view) { |
| 189 | return new SWTBotTestCondition() { |
| 190 | @Override |
| 191 | public boolean test() throws Exception { |
| 192 | return (view != null) && (!view.isActive()); |
| 193 | } |
| 194 | }; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Wait till table cell has a given content. |
| 199 | * |
| 200 | * @param table |
| 201 | * the table bot reference |
| 202 | * @param content |
| 203 | * the content to check |
| 204 | * @param row |
| 205 | * the row of the cell |
| 206 | * @param column |
| 207 | * the column of the cell |
| 208 | * @return ICondition for verification |
| 209 | */ |
| 210 | public static ICondition isTableCellFilled(final SWTBotTable table, |
| 211 | final String content, final int row, final int column) { |
| 212 | return new SWTBotTestCondition() { |
| 213 | @Override |
| 214 | public boolean test() throws Exception { |
| 215 | try { |
| 216 | String cell = table.cell(row, column); |
| 217 | if( cell == null ) { |
| 218 | return false; |
| 219 | } |
| 220 | return cell.endsWith(content); |
| 221 | } catch (Exception e) { |
| 222 | } |
| 223 | return false; |
| 224 | } |
| 225 | }; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Condition to check if a tracing project element has a child with the |
| 230 | * specified name. A project element label may have a count suffix in the |
| 231 | * format ' [n]'. |
| 232 | */ |
| 233 | public static class ProjectElementHasChild extends DefaultCondition { |
| 234 | |
| 235 | private final SWTBotTreeItem fParentItem; |
| 236 | private final String fName; |
| 237 | private final String fRegex; |
| 238 | private SWTBotTreeItem fItem = null; |
| 239 | |
| 240 | /** |
| 241 | * Constructor. |
| 242 | * |
| 243 | * @param parentItem |
| 244 | * the parent item |
| 245 | * @param name |
| 246 | * the child name to look for |
| 247 | */ |
| 248 | public ProjectElementHasChild(final SWTBotTreeItem parentItem, final String name) { |
| 249 | fParentItem = parentItem; |
| 250 | fName = name; |
| 251 | /* Project element labels may have count suffix */ |
| 252 | fRegex = name + "(\\s\\[(\\d)+\\])?"; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public boolean test() throws Exception { |
| 257 | fParentItem.expand(); |
| 258 | for (SWTBotTreeItem item : fParentItem.getItems()) { |
| 259 | if (item.getText().matches(fRegex)) { |
| 260 | fItem = item; |
| 261 | return true; |
| 262 | } |
| 263 | } |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public String getFailureMessage() { |
| 269 | return NLS.bind("No child of {0} found with name {1}", fParentItem.getText(), fName); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Returns the matching child item if the condition returned true. |
| 274 | * |
| 275 | * @return the matching item |
| 276 | */ |
| 277 | public SWTBotTreeItem getItem() { |
| 278 | return fItem; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Condition to check if an editor with the specified title is opened. |
| 284 | * |
| 285 | * @param bot |
| 286 | * a workbench bot |
| 287 | * @param title |
| 288 | * the editor title |
| 289 | * @return ICondition for verification |
| 290 | */ |
| 291 | public static ICondition isEditorOpened(final SWTWorkbenchBot bot, final String title) { |
| 292 | return new SWTBotTestCondition() { |
| 293 | @Override |
| 294 | public boolean test() throws Exception { |
| 295 | Matcher<IEditorReference> withPartName = withPartName(title); |
| 296 | return !bot.editors(withPartName).isEmpty(); |
| 297 | } |
| 298 | }; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Condition to check if the selection range equals the specified range. |
| 303 | * |
| 304 | * @param range |
| 305 | * the selection range |
| 306 | * @return ICondition for verification |
| 307 | */ |
| 308 | public static ICondition selectionRange(final TmfTimeRange range) { |
| 309 | return new SWTBotTestCondition() { |
| 310 | @Override |
| 311 | public boolean test() throws Exception { |
| 312 | return TmfTraceManager.getInstance().getCurrentTraceContext().getSelectionRange().equals(range); |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public String getFailureMessage() { |
| 317 | return NLS.bind("Selection range: {0} expected: {1}", |
| 318 | TmfTraceManager.getInstance().getCurrentTraceContext().getSelectionRange(), range); |
| 319 | } |
| 320 | }; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Condition to check if the window range equals the specified range. |
| 325 | * |
| 326 | * @param range |
| 327 | * the window range |
| 328 | * @return ICondition for verification |
| 329 | */ |
| 330 | public static ICondition windowRange(final TmfTimeRange range) { |
| 331 | return new SWTBotTestCondition() { |
| 332 | @Override |
| 333 | public boolean test() throws Exception { |
| 334 | return TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange().equals(range); |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public String getFailureMessage() { |
| 339 | return NLS.bind("Window range: {0} expected: {1}", |
| 340 | TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange(), range); |
| 341 | } |
| 342 | }; |
| 343 | } |
| 344 | } |