|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| KeyTagHandler.java | 66.7% | 84.8% | 100% | 81.9% |
|
||||||||||||||
| 1 |
package junit.extensions.jfcunit.eventdata;
|
|
| 2 |
|
|
| 3 |
import junit.extensions.jfcunit.JFCTestCase;
|
|
| 4 |
import junit.extensions.jfcunit.xml.JFCXMLConstants;
|
|
| 5 |
|
|
| 6 |
import junit.extensions.xml.IXMLTestCase;
|
|
| 7 |
import junit.extensions.xml.XMLException;
|
|
| 8 |
import junit.extensions.xml.elements.AbstractTagHandler;
|
|
| 9 |
|
|
| 10 |
import org.w3c.dom.Element;
|
|
| 11 |
|
|
| 12 |
import java.awt.Component;
|
|
| 13 |
import java.awt.event.InputEvent;
|
|
| 14 |
import java.awt.event.KeyEvent;
|
|
| 15 |
|
|
| 16 |
import java.util.StringTokenizer;
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
/**
|
|
| 20 |
* This class will handle the processing of <key> nodes.
|
|
| 21 |
*
|
|
| 22 |
* <h3>Description</h3>
|
|
| 23 |
* <p>
|
|
| 24 |
* This element will be used to submit key codes and strings
|
|
| 25 |
* to a component.
|
|
| 26 |
* </p>
|
|
| 27 |
*
|
|
| 28 |
* <h3>Parameters</h3>
|
|
| 29 |
* <table border="1" cellpadding="2" cellspacing="0">
|
|
| 30 |
* <tr>
|
|
| 31 |
* <td valign="top"><b>Attribute</b></td>
|
|
| 32 |
* <td valign="top"><b>Description</b></td>
|
|
| 33 |
* <td align="center" valign="top"><b>Required</b></td>
|
|
| 34 |
* <td valign="top"><b>Default</b></td>
|
|
| 35 |
* <td valign="top"><b>Values</b></td>
|
|
| 36 |
* </tr>
|
|
| 37 |
* <tr>
|
|
| 38 |
* <td valign="top">refid</td>
|
|
| 39 |
* <td valign="top">Id of a object reference which has been previously found.</td>
|
|
| 40 |
* <td valign="top" align="center">Yes</td>
|
|
| 41 |
* <td valign="top">N/A</td>
|
|
| 42 |
* <td valign="top">N/A</td>
|
|
| 43 |
* </tr>
|
|
| 44 |
* <tr>
|
|
| 45 |
* <td valign="top">string</td>
|
|
| 46 |
* <td valign="top">The string to be entered.</td>
|
|
| 47 |
* <td valign="top" align="center">One string or code attribute is required</td>
|
|
| 48 |
* <td valign="top">empty string</td>
|
|
| 49 |
* <td valign="top">Any characters</td>
|
|
| 50 |
* </tr>
|
|
| 51 |
* <tr>
|
|
| 52 |
* <td valign="top">code</td>
|
|
| 53 |
* <td valign="top">The key code to be entered.</td>
|
|
| 54 |
* <td valign="top" align="center">One string or code attribute is required</td>
|
|
| 55 |
* <td valign="top">None</td>
|
|
| 56 |
* <td valign="top">The key codes to use should follow the VK_(codes) of KeyEventData. Ex: VK_F4</td>
|
|
| 57 |
* </tr>
|
|
| 58 |
* <tr>
|
|
| 59 |
* <td valign="top">modifiers</td>
|
|
| 60 |
* <td valign="top">The modifiers to be used for this event.</td>
|
|
| 61 |
* <td valign="top" align="center">No</td>
|
|
| 62 |
* <td valign="top">None</td>
|
|
| 63 |
* <td valign="top">Alt Altgr Ctrl Meta Shift</td>
|
|
| 64 |
* </tr>
|
|
| 65 |
* <tr>
|
|
| 66 |
* <td valign="top">sleeptime</td>
|
|
| 67 |
* <td valign="top">Maximum sleep time</td>
|
|
| 68 |
* <td valign="top" align="center">No</td>
|
|
| 69 |
* <td valign="top">500</td>
|
|
| 70 |
* <td valign="top">Positive Integer representing the milli-seconds</td>
|
|
| 71 |
* </tr>
|
|
| 72 |
*
|
|
| 73 |
* </table>
|
|
| 74 |
* <h3>Example</h3>
|
|
| 75 |
* <blockquote><pre>
|
|
| 76 |
* <key
|
|
| 77 |
* refid="FileMenuItem"
|
|
| 78 |
* code="VK_F2"
|
|
| 79 |
* modifiers="Shift"
|
|
| 80 |
* />
|
|
| 81 |
* </pre></blockquote>
|
|
| 82 |
* <blockquote><pre>
|
|
| 83 |
* <key
|
|
| 84 |
* refid="FileMenuItem"
|
|
| 85 |
* code="VK_F2"
|
|
| 86 |
* modifiers="Ctrl+Shift"
|
|
| 87 |
* />
|
|
| 88 |
* </pre></blockquote>
|
|
| 89 |
* <p>
|
|
| 90 |
* The above simulates pressing shift F2.
|
|
| 91 |
* </p>
|
|
| 92 |
* @see junit.extensions.jfcunit.eventdata.KeyEventData
|
|
| 93 |
* @author Kevin Wilson
|
|
| 94 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
| 95 |
*/
|
|
| 96 |
public class KeyTagHandler extends AbstractTagHandler implements JFCXMLConstants { |
|
| 97 |
/**
|
|
| 98 |
* Constructor for FindTagHandler.
|
|
| 99 |
*
|
|
| 100 |
* @param element The element to be processed
|
|
| 101 |
* @param testCase The IXMLTestCase that uses this element
|
|
| 102 |
*/
|
|
| 103 | 2 |
public KeyTagHandler(final Element element, final IXMLTestCase testCase) {
|
| 104 | 2 |
super(element, testCase);
|
| 105 |
} |
|
| 106 |
|
|
| 107 |
/**
|
|
| 108 |
* Submit the keystrokes specified by the event.
|
|
| 109 |
* @throws XMLException may be thrown.
|
|
| 110 |
*/
|
|
| 111 | 2 |
public void processElement() throws XMLException { |
| 112 | 2 |
validateElement(); |
| 113 |
|
|
| 114 | 2 |
String refid = getRefId(); |
| 115 | 2 |
Component comp = (Component) getXMLTestCase().getProperty(refid); |
| 116 |
|
|
| 117 | 2 |
if (comp == null) { |
| 118 | 0 |
throw new XMLException("Component not found for:" + refid, null, |
| 119 |
getElement(), |
|
| 120 |
getXMLTestCase().getPropertyCache()); |
|
| 121 |
} |
|
| 122 |
|
|
| 123 | 2 |
int modifiers = getModifiers();
|
| 124 | 2 |
long sleepTime = getSleepTime();
|
| 125 |
|
|
| 126 | 2 |
String string = getString(STRING); |
| 127 | 2 |
AbstractKeyEventData data = null;
|
| 128 |
|
|
| 129 | 2 |
if (string != null) { |
| 130 | 1 |
data = new StringEventData((JFCTestCase) getTestCase(), comp,
|
| 131 |
string, modifiers, sleepTime); |
|
| 132 |
} else {
|
|
| 133 | 1 |
int character = getCharacter();
|
| 134 | 1 |
data = new KeyEventData((JFCTestCase) getTestCase(), comp,
|
| 135 |
character, modifiers, sleepTime); |
|
| 136 |
} |
|
| 137 |
|
|
| 138 | 2 |
((JFCTestCase) getTestCase()).getHelper().sendString(data); |
| 139 |
} |
|
| 140 |
|
|
| 141 |
/**
|
|
| 142 |
* Validate that the refid attribute is specified and
|
|
| 143 |
* either code or string attributes are specified.
|
|
| 144 |
* @throws XMLException if the refid is missing or (code and string)
|
|
| 145 |
* attributes are not present.
|
|
| 146 |
*/
|
|
| 147 | 2 |
public void validateElement() throws XMLException { |
| 148 |
// do the default validations from the super class
|
|
| 149 | 2 |
super.validateElement();
|
| 150 |
|
|
| 151 |
// reqd attribute: refid
|
|
| 152 | 2 |
checkRequiredAttribute(REFID); |
| 153 |
|
|
| 154 |
// Only one of the following is required.
|
|
| 155 | 2 |
checkOneRequiredAttribute( |
| 156 |
getElement(), |
|
| 157 |
new String[] {CODE, STRING});
|
|
| 158 |
} |
|
| 159 |
|
|
| 160 |
/**
|
|
| 161 |
* Returns the value of the MODIFIERS attribute for this element. Defaults to
|
|
| 162 |
* defaultValue.
|
|
| 163 |
* @param defaultValue value to be returned if the element does not exist.
|
|
| 164 |
* @return int The value of the MODIFIERS attribute, defaultValue if not specified.
|
|
| 165 |
*/
|
|
| 166 | 2 |
protected int getModifiers(final int defaultValue) { |
| 167 | 2 |
int modifiers = 0;
|
| 168 | 2 |
String s = getString(MODIFIERS); |
| 169 |
|
|
| 170 | 2 |
if (s == null) { |
| 171 | 1 |
return defaultValue;
|
| 172 |
} |
|
| 173 |
|
|
| 174 | 1 |
try {
|
| 175 |
// If we can parse a integer from the string then use the integer
|
|
| 176 |
// value.
|
|
| 177 | 1 |
return Integer.parseInt(s);
|
| 178 |
} catch (NumberFormatException nfe) {
|
|
| 179 |
// Ignore
|
|
| 180 |
} |
|
| 181 |
|
|
| 182 | 1 |
StringTokenizer tok = new StringTokenizer(s, "+"); |
| 183 |
|
|
| 184 | 1 |
while (tok.hasMoreElements()) {
|
| 185 | 1 |
String token = tok.nextToken(); |
| 186 |
|
|
| 187 | 1 |
if (token.equalsIgnoreCase(SHIFT)) {
|
| 188 | 0 |
modifiers += InputEvent.SHIFT_MASK; |
| 189 |
} |
|
| 190 |
|
|
| 191 | 1 |
if (token.equalsIgnoreCase(CTRL)) {
|
| 192 | 0 |
modifiers += InputEvent.CTRL_MASK; |
| 193 |
} |
|
| 194 |
|
|
| 195 | 1 |
if (token.equalsIgnoreCase(ALT)) {
|
| 196 | 0 |
modifiers += InputEvent.ALT_MASK; |
| 197 |
} |
|
| 198 |
|
|
| 199 | 1 |
if (token.equalsIgnoreCase(ALTGR)) {
|
| 200 | 0 |
modifiers += InputEvent.ALT_GRAPH_MASK; |
| 201 |
} |
|
| 202 |
|
|
| 203 | 1 |
if (token.equalsIgnoreCase(META)) {
|
| 204 | 0 |
modifiers += InputEvent.META_MASK; |
| 205 |
} |
|
| 206 |
} |
|
| 207 |
|
|
| 208 | 1 |
return modifiers;
|
| 209 |
} |
|
| 210 |
|
|
| 211 |
/**
|
|
| 212 |
* Returns the value of the SLEEPTIME attribute for this element. Defaults to DEFAULT_SLEEPTIME.
|
|
| 213 |
* @return String The value of the SLEEPTIME attribute, DEFAULT_SLEEPTIME if not specified.
|
|
| 214 |
*/
|
|
| 215 | 2 |
protected long getSleepTime() { |
| 216 | 2 |
return getLong(SLEEPTIME, DEFAULT_SLEEPTIME);
|
| 217 |
} |
|
| 218 |
|
|
| 219 |
/**
|
|
| 220 |
* Returns the value of the CODE attribute for this element.
|
|
| 221 |
* @return String The value of the CODE attribute.
|
|
| 222 |
*/
|
|
| 223 | 1 |
private int getCharacter() { |
| 224 | 1 |
String keyCodeStr = getString(CODE); |
| 225 |
|
|
| 226 | 1 |
try {
|
| 227 | 1 |
Object code = KeyEvent.class.getField(keyCodeStr).get(null); |
| 228 |
|
|
| 229 | 1 |
return ((Integer) code).intValue();
|
| 230 |
} catch (Exception e) {
|
|
| 231 |
// Ignore
|
|
| 232 |
} |
|
| 233 |
|
|
| 234 | 0 |
return Integer.parseInt(keyCodeStr);
|
| 235 |
} |
|
| 236 |
|
|
| 237 |
/**
|
|
| 238 |
* Returns the value of the MODIFIERS attribute for this element. Defaults to zero.
|
|
| 239 |
* @return int The value of the MODIFIERS attribute, zero if not specified.
|
|
| 240 |
*/
|
|
| 241 | 2 |
private int getModifiers() { |
| 242 | 2 |
return getModifiers(0);
|
| 243 |
} |
|
| 244 |
|
|
| 245 |
/**
|
|
| 246 |
* Returns the value of the REFID attribute for this element.
|
|
| 247 |
* @return String The value of the REFID attribute.
|
|
| 248 |
*/
|
|
| 249 | 2 |
private String getRefId() {
|
| 250 | 2 |
return getString(REFID);
|
| 251 |
} |
|
| 252 |
} |
|
| 253 |
|
|
||||||||||