|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| BaseEventDataTagHandler.java | 77.1% | 82.5% | 100% | 82.4% |
|
||||||||||||||
| 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 junit.framework.Assert;
|
|
| 11 |
|
|
| 12 |
import org.w3c.dom.Element;
|
|
| 13 |
|
|
| 14 |
import java.awt.Component;
|
|
| 15 |
import java.awt.Point;
|
|
| 16 |
import java.awt.event.InputEvent;
|
|
| 17 |
|
|
| 18 |
import java.util.StringTokenizer;
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
/**
|
|
| 22 |
* Implements the common methods used by many of the EventData classes.
|
|
| 23 |
* @author Kevin Wilson
|
|
| 24 |
*/
|
|
| 25 |
public abstract class BaseEventDataTagHandler extends AbstractTagHandler |
|
| 26 |
implements EventDataConstants,
|
|
| 27 |
JFCXMLConstants {
|
|
| 28 |
/**
|
|
| 29 |
* Event data generated by the tag handler implementation.
|
|
| 30 |
*/
|
|
| 31 |
private AbstractMouseEventData m_eventData = null; |
|
| 32 |
|
|
| 33 |
/**
|
|
| 34 |
* Constructor for BaseEventDataTagHandler.
|
|
| 35 |
*
|
|
| 36 |
* @param element The element to be processed
|
|
| 37 |
* @param testCase The IXMLTestCase that uses this element
|
|
| 38 |
*/
|
|
| 39 | 418 |
public BaseEventDataTagHandler(final Element element,
|
| 40 |
final IXMLTestCase testCase) {
|
|
| 41 | 418 |
super(element, testCase);
|
| 42 |
} |
|
| 43 |
|
|
| 44 |
/**
|
|
| 45 |
* Returns the test case.
|
|
| 46 |
* @return testCase
|
|
| 47 |
*/
|
|
| 48 | 424 |
public JFCTestCase getJFCTestCase() {
|
| 49 | 424 |
IXMLTestCase tc = super.getXMLTestCase();
|
| 50 |
|
|
| 51 | 424 |
if (tc instanceof JFCTestCase) { |
| 52 | 424 |
return (JFCTestCase) tc;
|
| 53 |
} |
|
| 54 |
|
|
| 55 | 0 |
return null; |
| 56 |
} |
|
| 57 |
|
|
| 58 |
/**
|
|
| 59 |
* @see junit.extensions.xml.elements.AbstractTagHandler#validateElement()
|
|
| 60 |
* @throws XMLException when the required elements are not present.
|
|
| 61 |
*/
|
|
| 62 | 418 |
public void validateElement() throws XMLException { |
| 63 |
// do the default validations from the super class
|
|
| 64 | 418 |
super.validateElement();
|
| 65 |
|
|
| 66 |
// reqd attribute: refid
|
|
| 67 | 418 |
checkRequiredAttribute(REFID); |
| 68 |
} |
|
| 69 |
|
|
| 70 |
/**
|
|
| 71 |
* Returns the value of the CLICKS attribute for this element. Defaults to
|
|
| 72 |
* DEFAULT_NUMBEROFCLICKS.
|
|
| 73 |
* @return int The value of the CLICKS attribute, DEFAULT_NUMBEROFCLICKS if not specified.
|
|
| 74 |
*/
|
|
| 75 | 412 |
protected int getClicks() { |
| 76 | 412 |
return getInt(CLICKS, DEFAULT_NUMBEROFCLICKS);
|
| 77 |
} |
|
| 78 |
|
|
| 79 |
/**
|
|
| 80 |
* Returns the value of the (previously found) component whose name is the value of the REFID
|
|
| 81 |
* attribute for this element.
|
|
| 82 |
* @return Component The found component whose name matches with the REFID attribute.
|
|
| 83 |
*/
|
|
| 84 | 418 |
protected Component getComponent() {
|
| 85 | 418 |
Component comp = (Component) getXMLTestCase().getProperty( |
| 86 |
getString(REFID)); |
|
| 87 | 418 |
Assert.assertNotNull("Component for " + getString(REFID) + " is null", |
| 88 |
comp); |
|
| 89 |
|
|
| 90 | 418 |
return comp;
|
| 91 |
} |
|
| 92 |
|
|
| 93 |
/**
|
|
| 94 |
* Returns the value of theMODIFIERS attribute for this element. Defaults
|
|
| 95 |
* to DEFAULT_MOUSE_MODIFIERS.
|
|
| 96 |
* @return int The value of the MODIFIERS attribute, DEFAULT_MOUSE_MODIFIERS if not specified.
|
|
| 97 |
*/
|
|
| 98 | 412 |
protected int getModifiers() { |
| 99 | 412 |
if (this.getPopupTrigger()) { |
| 100 | 0 |
return getModifiers(DEFAULT_POPUP_MODIFIERS);
|
| 101 |
} |
|
| 102 |
|
|
| 103 | 412 |
return getModifiers(DEFAULT_MOUSE_MODIFIERS);
|
| 104 |
} |
|
| 105 |
|
|
| 106 |
/**
|
|
| 107 |
* Returns the value of the MODIFIERS attribute for this element. Defaults to
|
|
| 108 |
* defaultValue.
|
|
| 109 |
* @param defaultValue value to be returned if the element does not exist.
|
|
| 110 |
* @return int The value of the MODIFIERS attribute, defaultValue if not specified.
|
|
| 111 |
*/
|
|
| 112 | 418 |
protected int getModifiers(final int defaultValue) { |
| 113 | 418 |
int modifiers = 0;
|
| 114 | 418 |
String s = getString(MODIFIERS); |
| 115 |
|
|
| 116 | 418 |
if (s == null) { |
| 117 | 397 |
return defaultValue;
|
| 118 |
} |
|
| 119 |
|
|
| 120 | 21 |
try {
|
| 121 |
// If we can parse a integer from the string then use the integer
|
|
| 122 |
// value.
|
|
| 123 | 21 |
return Integer.parseInt(s);
|
| 124 |
} catch (NumberFormatException nfe) {
|
|
| 125 |
// Ignore
|
|
| 126 |
} |
|
| 127 |
|
|
| 128 | 2 |
StringTokenizer tok = new StringTokenizer(s, "+"); |
| 129 | 2 |
boolean button = false; |
| 130 |
|
|
| 131 | 2 |
while (tok.hasMoreElements()) {
|
| 132 | 2 |
String token = tok.nextToken(); |
| 133 |
|
|
| 134 | 2 |
if (token.equalsIgnoreCase(SHIFT)) {
|
| 135 | 0 |
modifiers += InputEvent.SHIFT_MASK; |
| 136 |
} |
|
| 137 |
|
|
| 138 | 2 |
if (token.equalsIgnoreCase(CTRL)) {
|
| 139 | 0 |
modifiers += InputEvent.CTRL_MASK; |
| 140 |
} |
|
| 141 |
|
|
| 142 | 2 |
if (token.equalsIgnoreCase(ALT)) {
|
| 143 | 0 |
modifiers += InputEvent.ALT_MASK; |
| 144 |
} |
|
| 145 |
|
|
| 146 | 2 |
if (token.equalsIgnoreCase(ALTGR)) {
|
| 147 | 0 |
modifiers += InputEvent.ALT_GRAPH_MASK; |
| 148 |
} |
|
| 149 |
|
|
| 150 | 2 |
if (token.equalsIgnoreCase(META)) {
|
| 151 | 0 |
modifiers += InputEvent.META_MASK; |
| 152 |
} |
|
| 153 |
|
|
| 154 | 2 |
if (token.equalsIgnoreCase(BUTTON1)) {
|
| 155 | 0 |
button = true;
|
| 156 | 0 |
modifiers += InputEvent.BUTTON1_MASK; |
| 157 |
} |
|
| 158 |
|
|
| 159 | 2 |
if (token.equalsIgnoreCase(BUTTON2)) {
|
| 160 | 0 |
button = true;
|
| 161 | 0 |
modifiers += InputEvent.BUTTON2_MASK; |
| 162 |
} |
|
| 163 |
|
|
| 164 | 2 |
if (token.equalsIgnoreCase(BUTTON3)) {
|
| 165 | 0 |
button = true;
|
| 166 | 0 |
modifiers += InputEvent.BUTTON3_MASK; |
| 167 |
} |
|
| 168 |
} |
|
| 169 |
|
|
| 170 | 2 |
if (!button) {
|
| 171 | 2 |
int defModifiers = AbstractMouseEventData.getDefaultModifiers(
|
| 172 |
getPopupTrigger()); |
|
| 173 | 2 |
modifiers = (modifiers | defModifiers); |
| 174 |
} |
|
| 175 |
|
|
| 176 | 2 |
return modifiers;
|
| 177 |
} |
|
| 178 |
|
|
| 179 |
/**
|
|
| 180 |
* Returns the value of the INDEX attribute for this element. Defaults to 0 (zero) if not found.
|
|
| 181 |
* @return int The value of the parsed INDEX attribute, zero if not found.
|
|
| 182 |
*/
|
|
| 183 | 5 |
protected int getOffset() { |
| 184 | 5 |
try {
|
| 185 | 5 |
return getInt(INDEX, INVALID_TEXT_OFFSET);
|
| 186 |
} catch (NumberFormatException e) {
|
|
| 187 | 0 |
throw new NumberFormatException("Invalid offset specification"); |
| 188 |
} |
|
| 189 |
} |
|
| 190 |
|
|
| 191 |
/**
|
|
| 192 |
* Returns the value of the POPUPTRIGGER attribute for this element.
|
|
| 193 |
* @return boolean The value of the POPUPTRIGGER attribute.
|
|
| 194 |
*/
|
|
| 195 | 832 |
protected boolean getPopupTrigger() { |
| 196 | 832 |
return getBoolean(POPUPTRIGGER);
|
| 197 |
} |
|
| 198 |
|
|
| 199 |
/**
|
|
| 200 |
* Returns the constant (defined in {@link EventDataConstants}) which corresponds to the value
|
|
| 201 |
* of the POSITION attribute of this element.
|
|
| 202 |
* @return int The corresponding constant from EventDataConstants.
|
|
| 203 |
*/
|
|
| 204 | 418 |
protected int getPosition() { |
| 205 | 418 |
String str = getString(POSITION); |
| 206 |
|
|
| 207 | 418 |
if ("CENTER".equalsIgnoreCase(str)) { |
| 208 | 1 |
return CENTER;
|
| 209 | 417 |
} else if ("NORTH".equalsIgnoreCase(str) || "N".equalsIgnoreCase(str)) { |
| 210 | 1 |
return NORTH;
|
| 211 | 416 |
} else if ("NORTH_EAST".equalsIgnoreCase(str) |
| 212 |
|| "NORTHEAST".equalsIgnoreCase(str)
|
|
| 213 |
|| "NE".equalsIgnoreCase(str)) {
|
|
| 214 | 1 |
return NORTH_EAST;
|
| 215 | 415 |
} else if ("EAST".equalsIgnoreCase(str) || "E".equalsIgnoreCase(str)) { |
| 216 | 2 |
return EAST;
|
| 217 | 413 |
} else if ("SOUTH_EAST".equalsIgnoreCase(str) |
| 218 |
|| "SOUTHEAST".equalsIgnoreCase(str)
|
|
| 219 |
|| "SE".equalsIgnoreCase(str)) {
|
|
| 220 | 1 |
return SOUTH_EAST;
|
| 221 | 412 |
} else if ("SOUTH".equalsIgnoreCase(str) || "S".equalsIgnoreCase(str)) { |
| 222 | 1 |
return SOUTH;
|
| 223 | 411 |
} else if ("SOUTH_WEST".equalsIgnoreCase(str) |
| 224 |
|| "SOUTHWEST".equalsIgnoreCase(str)
|
|
| 225 |
|| "SW".equalsIgnoreCase(str)) {
|
|
| 226 | 1 |
return SOUTH_WEST;
|
| 227 | 410 |
} else if ("WEST".equalsIgnoreCase(str) || "W".equalsIgnoreCase(str)) { |
| 228 | 1 |
return WEST;
|
| 229 | 409 |
} else if ("NORTH_WEST".equalsIgnoreCase(str) |
| 230 |
|| "NORTHWEST".equalsIgnoreCase(str)
|
|
| 231 |
|| "NW".equalsIgnoreCase(str)) {
|
|
| 232 | 1 |
return NORTH_WEST;
|
| 233 | 408 |
} else if ("CUSTOM".equalsIgnoreCase(str)) { |
| 234 | 1 |
return CUSTOM;
|
| 235 | 407 |
} else if ("PERCENT".equalsIgnoreCase(str)) { |
| 236 | 10 |
return PERCENT;
|
| 237 |
} |
|
| 238 |
|
|
| 239 |
// default to CENTER
|
|
| 240 | 397 |
return getInt(POSITION, CENTER);
|
| 241 |
} |
|
| 242 |
|
|
| 243 |
/**
|
|
| 244 |
* Returns the value of the REFERENCE attribute for this element. Defaults to null.
|
|
| 245 |
* @return Point The value of the REFERENCE attribute. Defaults to null of not found.
|
|
| 246 |
* @throws XMLException may be throws if the point cannot be parsed properly.
|
|
| 247 |
*/
|
|
| 248 | 418 |
protected Point getReference() throws XMLException { |
| 249 | 418 |
return getPoint(REFERENCE, null); |
| 250 |
} |
|
| 251 |
|
|
| 252 |
/**
|
|
| 253 |
* Returns the value of the SLEEPTIME attribute for this element. Defaults to DEFAULT_SLEEPTIME
|
|
| 254 |
* if not found.
|
|
| 255 |
* @return long The value of the SLEEPTIME attribute. Defaults to DEFAULT_SLEEPTIME.
|
|
| 256 |
*/
|
|
| 257 | 418 |
protected long getSleepTime() { |
| 258 | 418 |
return getLong(SLEEPTIME, DEFAULT_SLEEPTIME);
|
| 259 |
} |
|
| 260 |
|
|
| 261 |
/**
|
|
| 262 |
* Set the event data.
|
|
| 263 |
* @param data Data to be used as the event data for this TagHandler.
|
|
| 264 |
*/
|
|
| 265 | 412 |
void setEventData(final AbstractMouseEventData data) {
|
| 266 | 412 |
m_eventData = data; |
| 267 |
} |
|
| 268 |
|
|
| 269 |
/**
|
|
| 270 |
* Gets the event data generated by this class.
|
|
| 271 |
* @return event data created.
|
|
| 272 |
* @throws XMLException may be thrown when processing children.
|
|
| 273 |
*/
|
|
| 274 | 412 |
AbstractMouseEventData getEventData() throws XMLException {
|
| 275 | 412 |
processElement(); |
| 276 |
|
|
| 277 | 412 |
return m_eventData;
|
| 278 |
} |
|
| 279 |
} |
|
| 280 |
|
|
||||||||||