|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2012 BREDEX GmbH. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* BREDEX GmbH - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.jubula.rc.common.caps; |
| 12 |
|
| 13 |
import org.apache.commons.lang.StringUtils; |
| 14 |
import org.eclipse.jubula.rc.common.exception.StepExecutionException; |
| 15 |
import org.eclipse.jubula.rc.common.implclasses.MatchUtil; |
| 16 |
import org.eclipse.jubula.rc.common.implclasses.Verifier; |
| 17 |
import org.eclipse.jubula.rc.common.uiadapter.interfaces.ITextComponentAdapter; |
| 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.TimeUtil; |
| 21 |
/** |
| 22 |
* Implementation of the CAPs from all Text like components. |
| 23 |
* @author BREDEX GmbH |
| 24 |
* |
| 25 |
*/ |
| 26 |
public class AbstractTextComponent extends AbstractTextInputSupport { |
| 27 |
|
| 28 |
/** |
| 29 |
* Gets the specific adapter |
| 30 |
* @return the specific adapter |
| 31 |
*/ |
| 32 |
private ITextComponentAdapter getTextCompAdapter() { |
| 33 |
return (ITextComponentAdapter) getComponent(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Sets the caret at the position <code>index</code>. |
| 38 |
* @param index The caret position |
| 39 |
*/ |
| 40 |
private void setCaretPosition(final int index) { |
| 41 |
if (index < 0) { |
| 42 |
throw new StepExecutionException("Invalid position: " + index, //$NON-NLS-1$ |
| 43 |
EventFactory.createActionError(TestErrorEvent.INPUT_FAILED)); |
| 44 |
} |
| 45 |
int index2 = 0; |
| 46 |
String text = getTextCompAdapter().getText(); |
| 47 |
if (text != null) { |
| 48 |
index2 = index > text.length() ? text.length() : index; |
| 49 |
} |
| 50 |
getTextCompAdapter().setSelection(index2); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @param text The text to insert at the current caret position |
| 55 |
*/ |
| 56 |
protected void insertText(final String text) { |
| 57 |
// Scroll it to visible first to ensure that the typing |
| 58 |
// performs correctly. |
| 59 |
getRobot().scrollToVisible(getComponent().getRealComponent(), null); |
| 60 |
getRobot().type(getComponent().getRealComponent(), text); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Types <code>text</code> into the component. This replaces the shown |
| 65 |
* content. |
| 66 |
* |
| 67 |
* @param text the text to type in |
| 68 |
*/ |
| 69 |
public void gdReplaceText(String text) { |
| 70 |
gdSelect(); |
| 71 |
if (StringUtils.EMPTY.equals(text)) { |
| 72 |
getRobot().keyStroke("DELETE"); //$NON-NLS-1$ |
| 73 |
} |
| 74 |
insertText(text); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Types <code>text</code> into the component. |
| 79 |
* |
| 80 |
* @param text the text to type in |
| 81 |
*/ |
| 82 |
public void gdInputText(String text) { |
| 83 |
if (!getTextCompAdapter().hasFocus()) { |
| 84 |
TimeUtil.delay(100); |
| 85 |
gdClick(1, 1); |
| 86 |
} |
| 87 |
insertText(text); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Inserts <code>text</code> at the position <code>index</code>. |
| 92 |
* |
| 93 |
* @param text The text to insert |
| 94 |
* @param index The position for insertion |
| 95 |
*/ |
| 96 |
public void gdInsertText(String text, int index) { |
| 97 |
gdClick(1, 1); |
| 98 |
setCaretPosition(index); |
| 99 |
insertText(text); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Inserts <code>text</code> before or after the first appearance of |
| 104 |
* <code>pattern</code>. |
| 105 |
* @param text The text to insert |
| 106 |
* @param pattern The pattern to find the position for insertion |
| 107 |
* @param operator Operator to select Matching Algorithm |
| 108 |
* @param after If <code>true</code>, the text will be inserted after the |
| 109 |
* pattern, otherwise before the pattern. |
| 110 |
* @throws StepExecutionException If the pattern is invalid or cannot be found |
| 111 |
*/ |
| 112 |
public void gdInsertText(String text, String pattern, String operator, |
| 113 |
boolean after) |
| 114 |
throws StepExecutionException { |
| 115 |
|
| 116 |
if (text == null) { |
| 117 |
throw new StepExecutionException( |
| 118 |
"The text to be inserted must not be null", EventFactory //$NON-NLS-1$ |
| 119 |
.createActionError()); |
| 120 |
} |
| 121 |
final MatchUtil.FindResult matchedText = MatchUtil.getInstance(). |
| 122 |
find(getTextCompAdapter().getText(), pattern, operator); |
| 123 |
|
| 124 |
if ((matchedText == null) || (matchedText.getStr() == null)) { |
| 125 |
throw new StepExecutionException("The pattern '" + pattern //$NON-NLS-1$ |
| 126 |
+ "' could not be found", //$NON-NLS-1$ |
| 127 |
EventFactory.createActionError(TestErrorEvent.NOT_FOUND)); |
| 128 |
} |
| 129 |
|
| 130 |
int index = matchedText.getPos(); |
| 131 |
|
| 132 |
int insertPos = after ? index + matchedText.getStr().length() : index; |
| 133 |
gdInsertText(text, insertPos); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* select the whole text of the textfield by calling "selectAll()". |
| 140 |
*/ |
| 141 |
public void gdSelect() { |
| 142 |
gdClick(1, 1); |
| 143 |
// Wait a while. Without this, we got no selectAll sometimes! |
| 144 |
TimeUtil.delay(100); |
| 145 |
getTextCompAdapter().selectAll(); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Verifies the editable property. |
| 150 |
* @param editable The editable property to verify. |
| 151 |
*/ |
| 152 |
public void gdVerifyEditable(boolean editable) { |
| 153 |
Verifier.equals(editable, getTextCompAdapter().isEditable()); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Selects the first (not)appearance of <code>pattern</code> in the text |
| 158 |
* component's content. |
| 159 |
* |
| 160 |
* @param pattern The pattern to select |
| 161 |
* @param operator operator |
| 162 |
* @throws StepExecutionException |
| 163 |
* If the pattern is invalid or cannot be found |
| 164 |
*/ |
| 165 |
public void gdSelect(final String pattern, String operator) |
| 166 |
throws StepExecutionException { |
| 167 |
gdClick(1, 1); |
| 168 |
final MatchUtil.FindResult matchedText = MatchUtil.getInstance(). |
| 169 |
find(getTextCompAdapter().getText(), pattern, operator); |
| 170 |
if ((matchedText == null) || (matchedText.getStr().length() == 0)) { |
| 171 |
throw new StepExecutionException("Invalid pattern for insertion", //$NON-NLS-1$ |
| 172 |
EventFactory.createActionError()); |
| 173 |
} |
| 174 |
final int index = matchedText.getPos(); |
| 175 |
if (operator.startsWith("not")) { //$NON-NLS-1$ |
| 176 |
if (pattern.equals(getTextCompAdapter().getText())) { |
| 177 |
String msg = "The pattern '" + pattern //$NON-NLS-1$ |
| 178 |
+ "' is equal to current text"; //$NON-NLS-1$ |
| 179 |
throw new StepExecutionException(msg, EventFactory |
| 180 |
.createActionError(TestErrorEvent |
| 181 |
.EXECUTION_ERROR, new String[] {msg})); |
| 182 |
} else if (index > 0) { |
| 183 |
// select part before pattern |
| 184 |
getTextCompAdapter().setSelection(0, index); |
| 185 |
} else { |
| 186 |
// select part after pattern |
| 187 |
getTextCompAdapter().setSelection( |
| 188 |
matchedText.getStr().length(), |
| 189 |
getTextCompAdapter().getText().length()); |
| 190 |
} |
| 191 |
} else { |
| 192 |
getTextCompAdapter().setSelection(index, |
| 193 |
index + matchedText.getStr().length()); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* {@inheritDoc} |
| 199 |
*/ |
| 200 |
public String[] getTextArrayFromComponent() { |
| 201 |
return null; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
} |