|
Added
Link Here
|
| 1 |
package org.eclipse.jubula.rc.common.caps; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
import org.apache.commons.lang.ArrayUtils; |
| 6 |
import org.apache.commons.lang.StringUtils; |
| 7 |
import org.eclipse.jubula.rc.common.CompSystemConstants; |
| 8 |
import org.eclipse.jubula.rc.common.driver.ClickOptions; |
| 9 |
import org.eclipse.jubula.rc.common.driver.DragAndDropHelper; |
| 10 |
import org.eclipse.jubula.rc.common.exception.StepExecutionException; |
| 11 |
import org.eclipse.jubula.rc.common.implclasses.IndexConverter; |
| 12 |
import org.eclipse.jubula.rc.common.implclasses.ListSelectionVerifier; |
| 13 |
import org.eclipse.jubula.rc.common.implclasses.MatchUtil; |
| 14 |
import org.eclipse.jubula.rc.common.implclasses.Verifier; |
| 15 |
import org.eclipse.jubula.rc.common.uiadapter.interfaces.IListAdapter; |
| 16 |
import org.eclipse.jubula.tools.constants.StringConstants; |
| 17 |
import org.eclipse.jubula.tools.constants.TestDataConstants; |
| 18 |
import org.eclipse.jubula.tools.objects.event.EventFactory; |
| 19 |
import org.eclipse.jubula.tools.objects.event.TestErrorEvent; |
| 20 |
import org.eclipse.jubula.tools.utils.StringParsing; |
| 21 |
|
| 22 |
/** |
| 23 |
* |
| 24 |
* @author BREDEX GmbH |
| 25 |
* |
| 26 |
*/ |
| 27 |
public abstract class AbstractListCAPs extends AbstractTextVerifiable { |
| 28 |
|
| 29 |
/** The default separator for enumerations of list values. */ |
| 30 |
public static final char INDEX_LIST_SEP_CHAR = |
| 31 |
TestDataConstants.VALUE_CHAR_DEFAULT; |
| 32 |
//FIXME this should be in an upper class or Util |
| 33 |
/** The default separator of a list of values */ |
| 34 |
public static final char VALUE_SEPARATOR = |
| 35 |
TestDataConstants.VALUE_CHAR_DEFAULT; |
| 36 |
|
| 37 |
/** |
| 38 |
* Splits the enumeration of values. |
| 39 |
* @param values The values to split |
| 40 |
* @param separator The separator, may be <code>null</code> |
| 41 |
* @return The array of values |
| 42 |
*/ |
| 43 |
private String[] split(String values, String separator) { |
| 44 |
String[] list = StringParsing.splitToArray(values, ((separator == null) |
| 45 |
|| (separator.length() == 0) ? INDEX_LIST_SEP_CHAR |
| 46 |
: separator.charAt(0)), |
| 47 |
TestDataConstants.ESCAPE_CHAR_DEFAULT); |
| 48 |
list = StringUtils.stripAll(list); |
| 49 |
return list; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @return The array of selected indices |
| 54 |
* @throws StepExecutionException If there are no indices selected |
| 55 |
*/ |
| 56 |
private int[] getCheckedSelectedIndices() throws StepExecutionException { |
| 57 |
int[] selected = getListAdapter().getSelectedIndices(); |
| 58 |
if (selected.length == 0) { |
| 59 |
throw new StepExecutionException("No list element selected", //$NON-NLS-1$ |
| 60 |
EventFactory.createActionError(TestErrorEvent.NO_SELECTION)); |
| 61 |
} |
| 62 |
return selected; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* |
| 67 |
* @return the List Adapter |
| 68 |
*/ |
| 69 |
private IListAdapter getListAdapter() { |
| 70 |
return ((IListAdapter) getComponent()); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Verifies if the passed index is selected. |
| 75 |
* |
| 76 |
* @param index The index to verify |
| 77 |
* @param expectSelected Whether the index should be selected. |
| 78 |
*/ |
| 79 |
public void gdVerifySelectedIndex(String index, boolean expectSelected) { |
| 80 |
int[] selected = getCheckedSelectedIndices(); |
| 81 |
int implIndex = IndexConverter.toImplementationIndex( |
| 82 |
Integer.parseInt(index)); |
| 83 |
|
| 84 |
boolean isSelected = ArrayUtils.contains(selected, implIndex); |
| 85 |
if (expectSelected != isSelected) { |
| 86 |
throw new StepExecutionException( |
| 87 |
"Selection check failed for index: " + index, //$NON-NLS-1$ |
| 88 |
EventFactory.createVerifyFailed( |
| 89 |
String.valueOf(expectSelected), |
| 90 |
String.valueOf(isSelected))); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Verifies if the passed value or enumeration of values is selected. By |
| 96 |
* default, the enumeration separator is <code>,</code> |
| 97 |
* @param valueList The value or list of values to verify |
| 98 |
*/ |
| 99 |
public void gdVerifySelectedValue(String valueList) { |
| 100 |
gdVerifySelectedValue(valueList, MatchUtil.DEFAULT_OPERATOR, true); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Verifies if the passed value is selected. |
| 105 |
* |
| 106 |
* @param value The value to verify |
| 107 |
* @param operator The operator to use when comparing the |
| 108 |
* expected and actual values. |
| 109 |
* @param isSelected if the value should be selected or not. |
| 110 |
*/ |
| 111 |
public void gdVerifySelectedValue(String value, String operator, |
| 112 |
boolean isSelected) { |
| 113 |
|
| 114 |
final String[] current = getListAdapter().getSelectedValues(); |
| 115 |
final ListSelectionVerifier listSelVerifier = |
| 116 |
new ListSelectionVerifier(); |
| 117 |
for (int i = 0; i < current.length; i++) { |
| 118 |
listSelVerifier.addItem(i, current[i], true); |
| 119 |
} |
| 120 |
listSelVerifier.verifySelection(value, operator, isSelected); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Verifies if all selected elements of a list match a text. |
| 125 |
* @param text The text to verify |
| 126 |
* @param operator The operator used to verify |
| 127 |
*/ |
| 128 |
public void gdVerifyText(String text, String operator) { |
| 129 |
String[] selected = getListAdapter().getSelectedValues(); |
| 130 |
final int selCount = selected.length; |
| 131 |
if (selCount < 1) { |
| 132 |
throw new StepExecutionException("No selection", //$NON-NLS-1$ |
| 133 |
EventFactory.createActionError(TestErrorEvent.NO_SELECTION)); |
| 134 |
} |
| 135 |
for (int i = 0; i < selCount; i++) { |
| 136 |
Verifier.match(selected[i], text, operator); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Selects the passed index or enumeration of indices. The enumeration must |
| 142 |
* be separated by <code>,</code>, e.g. <code>1, 3,6</code>. |
| 143 |
* @param indexList The index or indices to select |
| 144 |
* @param extendSelection Whether this selection extends a previous |
| 145 |
* selection. |
| 146 |
* @param button what mouse button should be used |
| 147 |
*/ |
| 148 |
public void gdSelectIndex(String indexList, final String extendSelection, |
| 149 |
int button) { |
| 150 |
final boolean isExtendSelection = extendSelection |
| 151 |
.equals(CompSystemConstants.EXTEND_SELECTION_YES); |
| 152 |
selectIndices(IndexConverter |
| 153 |
.toImplementationIndices(parseIndices(indexList)), ClickOptions |
| 154 |
.create().setClickCount(1).setMouseButton(button), |
| 155 |
isExtendSelection); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Selects the passed value or enumeration of values. By default, the |
| 160 |
* enumeration separator is <code>,</code>. |
| 161 |
* @param valueList The value or list of values to select |
| 162 |
* @param operator If regular expressions are used |
| 163 |
* @param searchType Determines where the search begins ("relative" or "absolute") |
| 164 |
* @param extendSelection Whether this selection extends a previous |
| 165 |
* selection. If <code>true</code>, the first |
| 166 |
* element will be selected with CONTROL as a |
| 167 |
* modifier. |
| 168 |
* @param button what mouse button should be used |
| 169 |
*/ |
| 170 |
public void gdSelectValue(String valueList, String operator, |
| 171 |
String searchType, final String extendSelection, int button) { |
| 172 |
final boolean isExtendSelection = |
| 173 |
extendSelection.equals(CompSystemConstants.EXTEND_SELECTION_YES); |
| 174 |
selectValue(valueList, String.valueOf(VALUE_SEPARATOR), operator, |
| 175 |
searchType, ClickOptions.create() |
| 176 |
.setClickCount(1) |
| 177 |
.setMouseButton(button), isExtendSelection); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Selects the passed value or enumeration of values. By default, the |
| 182 |
* enumeration separator is <code>,</code>, but may be changed by |
| 183 |
* <code>separator</code>. |
| 184 |
* @param valueList The value or list of values to select |
| 185 |
* @param separator The separator, optional |
| 186 |
* @param operator If regular expressions are used |
| 187 |
* @param searchType Determines where the search begins ("relative" or "absolute") |
| 188 |
* @param clickCount the amount of clicks to use |
| 189 |
* @param extendSelection Whether this selection extends a previous |
| 190 |
* selection. |
| 191 |
*/ |
| 192 |
public void gdSelectValue(String valueList, String separator, |
| 193 |
String operator, final String searchType, int clickCount, |
| 194 |
final String extendSelection) { |
| 195 |
final boolean isExtendSelection = |
| 196 |
extendSelection.equals(CompSystemConstants.EXTEND_SELECTION_YES); |
| 197 |
selectValue(valueList, separator, operator, searchType, ClickOptions |
| 198 |
.create().setClickCount(clickCount), isExtendSelection); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Verifies if the list contains an element that renderes <code>value</code>. |
| 203 |
* @param value The text to verify |
| 204 |
*/ |
| 205 |
public void gdVerifyContainsValue(String value) { |
| 206 |
Verifier.equals(true, containsValue(value)); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Verifies if the list contains an element that renderes <code>value</code>. |
| 211 |
* @param value The text to verify |
| 212 |
* @param operator The operator used to verify |
| 213 |
* @param exists if the wanted value should exist or not. |
| 214 |
*/ |
| 215 |
public void gdVerifyContainsValue(String value, String operator, |
| 216 |
boolean exists) { |
| 217 |
|
| 218 |
Verifier.equals(exists, containsValue(value, operator)); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Action to read the value of the current selected item of the JList |
| 223 |
* to store it in a variable in the Client |
| 224 |
* @param variable the name of the variable |
| 225 |
* @return the text value. |
| 226 |
*/ |
| 227 |
public String gdReadValue(String variable) { |
| 228 |
String[] selected = getListAdapter().getSelectedValues(); |
| 229 |
if (selected.length > 0) { |
| 230 |
return selected[0]; |
| 231 |
} |
| 232 |
throw new StepExecutionException("No list item selected", //$NON-NLS-1$ |
| 233 |
EventFactory.createActionError(TestErrorEvent.NO_SELECTION)); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Drags the passed value. |
| 238 |
* |
| 239 |
* @param mouseButton the mouseButton. |
| 240 |
* @param modifier the modifier. |
| 241 |
* @param value The value to drag |
| 242 |
* @param operator If regular expressions are used |
| 243 |
* @param searchType Determines where the search begins ("relative" or "absolute") |
| 244 |
*/ |
| 245 |
public void gdDragValue(int mouseButton, String modifier, String value, |
| 246 |
String operator, final String searchType) { |
| 247 |
|
| 248 |
DragAndDropHelper dndHelper = DragAndDropHelper.getInstance(); |
| 249 |
dndHelper.setModifier(modifier); |
| 250 |
dndHelper.setMouseButton(mouseButton); |
| 251 |
|
| 252 |
Integer [] indices = getListAdapter().findIndicesOfValues( |
| 253 |
new String [] {value}, operator, searchType); |
| 254 |
selectIndices(ArrayUtils.toPrimitive(indices), |
| 255 |
ClickOptions.create().setClickCount(0), false); |
| 256 |
|
| 257 |
pressOrReleaseModifiers(modifier, true); |
| 258 |
getRobot().mousePress(null, null, mouseButton); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Drops on the passed value. |
| 263 |
* |
| 264 |
* @param value The value on which to drop |
| 265 |
* @param operator If regular expressions are used |
| 266 |
* @param searchType Determines where the search begins ("relative" or "absolute") |
| 267 |
* @param delayBeforeDrop the amount of time (in milliseconds) to wait |
| 268 |
* between moving the mouse to the drop point and |
| 269 |
* releasing the mouse button |
| 270 |
*/ |
| 271 |
public void gdDropValue(String value, String operator, |
| 272 |
final String searchType, int delayBeforeDrop) { |
| 273 |
|
| 274 |
DragAndDropHelper dndHelper = DragAndDropHelper.getInstance(); |
| 275 |
try { |
| 276 |
Integer [] indices = getListAdapter().findIndicesOfValues( |
| 277 |
new String [] {value}, operator, searchType); |
| 278 |
selectIndices(ArrayUtils.toPrimitive(indices), |
| 279 |
ClickOptions.create().setClickCount(0), false); |
| 280 |
waitBeforeDrop(delayBeforeDrop); |
| 281 |
} finally { |
| 282 |
getRobot().mouseRelease(null, null, dndHelper.getMouseButton()); |
| 283 |
pressOrReleaseModifiers(dndHelper.getModifier(), false); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Drags the passed index. |
| 289 |
* |
| 290 |
* @param mouseButton the mouseButton. |
| 291 |
* @param modifier the modifier. |
| 292 |
* @param index The index to drag |
| 293 |
*/ |
| 294 |
public void gdDragIndex(final int mouseButton, final String modifier, |
| 295 |
int index) { |
| 296 |
|
| 297 |
DragAndDropHelper dndHelper = DragAndDropHelper.getInstance(); |
| 298 |
dndHelper.setModifier(modifier); |
| 299 |
dndHelper.setMouseButton(mouseButton); |
| 300 |
|
| 301 |
selectIndices( |
| 302 |
new int [] {IndexConverter.toImplementationIndex(index)}, |
| 303 |
ClickOptions.create().setClickCount(0), false); |
| 304 |
|
| 305 |
pressOrReleaseModifiers(modifier, true); |
| 306 |
getRobot().mousePress(null, null, mouseButton); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Drops onto the passed index. |
| 311 |
* |
| 312 |
* @param index The index on which to drop |
| 313 |
* @param delayBeforeDrop the amount of time (in milliseconds) to wait |
| 314 |
* between moving the mouse to the drop point and |
| 315 |
* releasing the mouse button |
| 316 |
*/ |
| 317 |
public void gdDropIndex(final int index, int delayBeforeDrop) { |
| 318 |
|
| 319 |
DragAndDropHelper dndHelper = DragAndDropHelper.getInstance(); |
| 320 |
|
| 321 |
try { |
| 322 |
selectIndices( |
| 323 |
new int [] {IndexConverter.toImplementationIndex(index)}, |
| 324 |
ClickOptions.create().setClickCount(0), false); |
| 325 |
waitBeforeDrop(delayBeforeDrop); |
| 326 |
} finally { |
| 327 |
getRobot().mouseRelease(null, null, dndHelper.getMouseButton()); |
| 328 |
pressOrReleaseModifiers(dndHelper.getModifier(), false); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* @param value The value |
| 334 |
* @return <code>true</code> if the list contains an element that is rendered with <code>value</code> |
| 335 |
*/ |
| 336 |
public boolean containsValue(String value) { |
| 337 |
Integer[] indices = getListAdapter().findIndicesOfValues( |
| 338 |
new String[] { value }, |
| 339 |
MatchUtil.EQUALS, CompSystemConstants.SEARCH_TYPE_ABSOLUTE); |
| 340 |
return indices.length > 0; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* @param value The value |
| 345 |
* @param operator The operator used to verify |
| 346 |
* @return <code>true</code> if the list contains an element that is rendered with <code>value</code> |
| 347 |
*/ |
| 348 |
public boolean containsValue(String value, String operator) { |
| 349 |
Integer[] indices = null; |
| 350 |
if (operator.equals(MatchUtil.NOT_EQUALS)) { |
| 351 |
indices = getListAdapter().findIndicesOfValues( |
| 352 |
new String[] { value }, |
| 353 |
MatchUtil.EQUALS, CompSystemConstants.SEARCH_TYPE_ABSOLUTE); |
| 354 |
return indices.length == 0; |
| 355 |
} |
| 356 |
indices = getListAdapter().findIndicesOfValues(new String[] { value }, |
| 357 |
operator, CompSystemConstants.SEARCH_TYPE_ABSOLUTE); |
| 358 |
return indices.length > 0; |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Selects the passed value or enumeration of values. By default, the |
| 363 |
* enumeration separator is <code>,</code>, but may be changed by |
| 364 |
* <code>separator</code>. |
| 365 |
* @param valueList The value or list of values to select |
| 366 |
* @param separator The separator, optional |
| 367 |
* @param operator If regular expressions are used |
| 368 |
* @param searchType Determines where the search begins ("relative" or "absolute") |
| 369 |
* @param co the click options to use |
| 370 |
* @param isExtendSelection Whether this selection extends a previous |
| 371 |
* selection. |
| 372 |
*/ |
| 373 |
private void selectValue(String valueList, String separator, |
| 374 |
String operator, final String searchType, ClickOptions co, |
| 375 |
final boolean isExtendSelection) { |
| 376 |
|
| 377 |
String[] values = null; |
| 378 |
if (StringConstants.EMPTY.equals(valueList)) { |
| 379 |
values = new String[1]; |
| 380 |
values[0] = StringConstants.EMPTY; |
| 381 |
} else { |
| 382 |
values = split(valueList, separator); |
| 383 |
} |
| 384 |
Integer[] indices = getListAdapter().findIndicesOfValues(values, |
| 385 |
operator, searchType); |
| 386 |
Arrays.sort(indices); |
| 387 |
if (!operator.equals(MatchUtil.NOT_EQUALS) |
| 388 |
&& (indices.length < values.length)) { |
| 389 |
throw new StepExecutionException("One or more values not found of set: " //$NON-NLS-1$ |
| 390 |
+ Arrays.asList(values).toString(), |
| 391 |
EventFactory.createActionError(TestErrorEvent.NOT_FOUND)); |
| 392 |
} |
| 393 |
selectIndices(ArrayUtils.toPrimitive(indices), co, isExtendSelection); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Parses the enumeration of indices. |
| 398 |
* @param indexList The enumeration of indices |
| 399 |
* @return The array of parsed indices |
| 400 |
*/ |
| 401 |
private int[] parseIndices(String indexList) { |
| 402 |
String[] list = StringParsing.splitToArray(indexList, |
| 403 |
INDEX_LIST_SEP_CHAR, TestDataConstants.ESCAPE_CHAR_DEFAULT); |
| 404 |
int[] indices = new int[list.length]; |
| 405 |
for (int i = 0; i < list.length; i++) { |
| 406 |
indices[i] = IndexConverter.intValue(list[i]); |
| 407 |
} |
| 408 |
return indices; |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* @param indices The indices to select |
| 413 |
* @param co the click options to use |
| 414 |
* @param isExtendSelection Whether this selection extends a previous |
| 415 |
* selection. If <code>true</code>, the first |
| 416 |
* element will be selected with CONTROL as a |
| 417 |
* modifier. |
| 418 |
*/ |
| 419 |
private void selectIndices(int[] indices, ClickOptions co, |
| 420 |
boolean isExtendSelection) { |
| 421 |
Object list = getListAdapter().getRealComponent(); |
| 422 |
if (indices.length > 0) { |
| 423 |
try { |
| 424 |
if (isExtendSelection) { |
| 425 |
getRobot().keyPress(list, |
| 426 |
getSystemDefaultModifier()); |
| 427 |
} |
| 428 |
|
| 429 |
// first selection |
| 430 |
getListAdapter().clickOnIndex( |
| 431 |
new Integer(indices[0]), co); |
| 432 |
} finally { |
| 433 |
if (isExtendSelection) { |
| 434 |
getRobot().keyRelease(list, |
| 435 |
getSystemDefaultModifier()); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
try { |
| 440 |
getRobot().keyPress(list, |
| 441 |
getSystemDefaultModifier()); |
| 442 |
// following selections |
| 443 |
for (int i = 1; i < indices.length; i++) { |
| 444 |
getListAdapter().clickOnIndex( |
| 445 |
new Integer(indices[i]), co); |
| 446 |
} |
| 447 |
} finally { |
| 448 |
getRobot().keyRelease(list, |
| 449 |
getSystemDefaultModifier()); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* {@inheritDoc} |
| 455 |
*/ |
| 456 |
public String[] getTextArrayFromComponent() { |
| 457 |
return null; |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* |
| 462 |
* @return - |
| 463 |
*/ |
| 464 |
protected abstract int getSystemDefaultModifier(); |
| 465 |
|
| 466 |
} |