|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
KeyEventData.java | 100% | 95% | 92.9% | 95.2% |
|
1 |
package junit.extensions.jfcunit.eventdata;
|
|
2 |
|
|
3 |
import junit.extensions.jfcunit.JFCTestCase;
|
|
4 |
import junit.extensions.jfcunit.TestHelper;
|
|
5 |
import junit.extensions.jfcunit.keyboard.JFCKeyStroke;
|
|
6 |
import junit.extensions.jfcunit.keyboard.KeyMapping;
|
|
7 |
|
|
8 |
import org.w3c.dom.Element;
|
|
9 |
|
|
10 |
import java.awt.AWTEvent;
|
|
11 |
import java.awt.Component;
|
|
12 |
import java.awt.event.KeyEvent;
|
|
13 |
|
|
14 |
|
|
15 |
/**
|
|
16 |
* Data container class that holds all the data necessary for jfcUnit to fire key events.
|
|
17 |
*
|
|
18 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
19 |
*/
|
|
20 |
public class KeyEventData extends AbstractKeyEventData { |
|
21 |
/**
|
|
22 |
* The component to be fired upon.
|
|
23 |
*/
|
|
24 |
private Component m_comp = null; |
|
25 |
|
|
26 |
/**
|
|
27 |
* The key code to be fired.
|
|
28 |
*/
|
|
29 |
private int m_keyCode = 0; |
|
30 |
|
|
31 |
/**
|
|
32 |
* Default Constructor.
|
|
33 |
*/
|
|
34 | 177 |
public KeyEventData() {
|
35 | 177 |
super();
|
36 |
} |
|
37 |
|
|
38 |
/**
|
|
39 |
* Constructor.
|
|
40 |
*
|
|
41 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
42 |
* @param comp The component on which to trigger the event.
|
|
43 |
* @param keyCode The key to be sent to the component
|
|
44 |
*/
|
|
45 | 9 |
public KeyEventData(final JFCTestCase testCase, final Component comp,
|
46 |
final int keyCode) {
|
|
47 | 9 |
this(testCase, comp, keyCode, DEFAULT_KEY_MODIFIERS, DEFAULT_SLEEPTIME);
|
48 |
} |
|
49 |
|
|
50 |
/**
|
|
51 |
* Constructor.
|
|
52 |
*
|
|
53 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
54 |
* @param comp The component on which to trigger the event.
|
|
55 |
* @param keyCode The key to be sent to the component
|
|
56 |
* @param sleepTime The wait time in ms between each event.
|
|
57 |
*/
|
|
58 | 2 |
public KeyEventData(final JFCTestCase testCase, final Component comp,
|
59 |
final int keyCode, final long sleepTime) { |
|
60 | 2 |
this(testCase, comp, keyCode, DEFAULT_KEY_MODIFIERS, sleepTime);
|
61 |
} |
|
62 |
|
|
63 |
/**
|
|
64 |
* Constructor.
|
|
65 |
*
|
|
66 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
67 |
* @param comp The component on which to trigger the event.
|
|
68 |
* @param keyCode The key to be sent to the component
|
|
69 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
70 |
* @param sleepTime
|
|
71 |
* The wait time in ms between each event.
|
|
72 |
*/
|
|
73 | 61 |
public KeyEventData(final JFCTestCase testCase, final Component comp,
|
74 |
final int keyCode, final int modifiers, final long sleepTime) { |
|
75 | 61 |
setTestCase(testCase); |
76 | 61 |
setSource(comp); |
77 | 61 |
setModifiers(modifiers); |
78 | 61 |
setKeyCode(keyCode); |
79 | 61 |
setSleepTime(sleepTime); |
80 | 61 |
setValid(true);
|
81 |
} |
|
82 |
|
|
83 |
/**
|
|
84 |
* Set the key code to be sent.
|
|
85 |
*
|
|
86 |
* @param keyCode Key code.
|
|
87 |
*/
|
|
88 | 67 |
public final void setKeyCode(final int keyCode) { |
89 | 67 |
m_keyCode = keyCode; |
90 | 67 |
setupKeyStrokes(); |
91 |
} |
|
92 |
|
|
93 |
/**
|
|
94 |
* Get the key code to be sent.
|
|
95 |
*
|
|
96 |
* @return int key code to be sent.
|
|
97 |
*/
|
|
98 | 7 |
public final int getKeyCode() { |
99 | 7 |
return m_keyCode;
|
100 |
} |
|
101 |
|
|
102 |
/**
|
|
103 |
* Set the attribute value.
|
|
104 |
*
|
|
105 |
* @param comp The new value of the attribute
|
|
106 |
*/
|
|
107 | 67 |
public final void setSource(final Component comp) { |
108 | 67 |
m_comp = comp; |
109 |
} |
|
110 |
|
|
111 |
/**
|
|
112 |
* Get the attribute value.
|
|
113 |
*
|
|
114 |
* @return Component The value of the attribute.
|
|
115 |
*/
|
|
116 | 366 |
public final Component getSource() {
|
117 | 366 |
return m_comp;
|
118 |
} |
|
119 |
|
|
120 |
/**
|
|
121 |
* The component on which the event has to be fired.
|
|
122 |
*
|
|
123 |
* @return The component.
|
|
124 |
*/
|
|
125 | 359 |
public Component getComponent() {
|
126 |
// by default, the component is the same as the source
|
|
127 | 359 |
return getSource();
|
128 |
} |
|
129 |
|
|
130 |
/**
|
|
131 |
* Check if this event can consume the AWTEvent.
|
|
132 |
*
|
|
133 |
* @param ae Event to be consumed.
|
|
134 |
* @return boolean true if the event can be consumed.
|
|
135 |
*/
|
|
136 | 244 |
public boolean canConsume(final AWTEvent ae) { |
137 | 244 |
if (!(ae instanceof KeyEvent)) { |
138 | 100 |
return false; |
139 |
} |
|
140 |
|
|
141 | 144 |
if (isValid()) {
|
142 | 6 |
return false; |
143 |
} |
|
144 |
|
|
145 | 138 |
char c = ((KeyEvent) ae).getKeyChar();
|
146 |
|
|
147 | 138 |
return super.canConsume(ae) |
148 |
&& ((c == KeyEvent.CHAR_UNDEFINED) || Character.isISOControl(c)); |
|
149 |
} |
|
150 |
|
|
151 |
/**
|
|
152 |
* Consume the event.
|
|
153 |
*
|
|
154 |
* @param ae Event to be consumed.
|
|
155 |
* @return boolean true if the event was consumed.
|
|
156 |
*/
|
|
157 | 87 |
public boolean consume(final AWTEvent ae) { |
158 | 87 |
if (super.consume(ae)) { |
159 | 81 |
return true; |
160 |
} |
|
161 |
|
|
162 | 6 |
setSource((Component) ae.getSource()); |
163 | 6 |
setModifiers(((KeyEvent) ae).getModifiers()); |
164 |
|
|
165 | 6 |
char c = ((KeyEvent) ae).getKeyChar();
|
166 |
|
|
167 | 6 |
if (Character.isISOControl(c)) {
|
168 | 1 |
setKeyCode((int) c);
|
169 |
} else {
|
|
170 | 5 |
setKeyCode(((KeyEvent) ae).getKeyCode()); |
171 |
} |
|
172 |
|
|
173 | 6 |
setSleepTime(getDefaultSleepTime()); |
174 |
|
|
175 | 6 |
setValid(true);
|
176 |
|
|
177 | 6 |
return true; |
178 |
} |
|
179 |
|
|
180 |
/**
|
|
181 |
* Populate the element with the key code.
|
|
182 |
* @param e Element to be populated with the data for this event.
|
|
183 |
*/
|
|
184 | 0 |
public void populate(final Element e) { |
185 | 0 |
super.populate(e);
|
186 | 0 |
e.setAttribute("code", "" + m_keyCode); |
187 |
} |
|
188 |
|
|
189 |
/**
|
|
190 |
* Generate a description of the event.
|
|
191 |
*
|
|
192 |
* @return String description of the event.
|
|
193 |
*/
|
|
194 | 1 |
public String toString() {
|
195 | 1 |
return "KeyEventData:" + m_keyCode + " on " + getSource() + " " |
196 |
+ getModifiers() + " " + getSleepTime();
|
|
197 |
} |
|
198 |
|
|
199 |
/**
|
|
200 |
* Setup the keystrokes for the event.
|
|
201 |
*/
|
|
202 | 134 |
protected void setupKeyStrokes() { |
203 | 134 |
KeyMapping km = TestHelper.getKeyMapping(); |
204 | 134 |
clearKeyStrokes(); |
205 |
|
|
206 | 134 |
JFCKeyStroke[] strokes = km.getKeyStrokes(m_keyCode); |
207 | 134 |
addKeyStrokes(strokes); |
208 | 134 |
applyModifier(getModifiers()); |
209 |
} |
|
210 |
} |
|
211 |
|
|