|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
StringEventData.java | 100% | 95.9% | 92.9% | 96.1% |
|
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 mouse events.
|
|
17 |
*
|
|
18 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
19 |
*/
|
|
20 |
public class StringEventData extends AbstractKeyEventData { |
|
21 |
/**
|
|
22 |
* The string code to be fired.
|
|
23 |
*/
|
|
24 |
private Component m_comp = null; |
|
25 |
|
|
26 |
/**
|
|
27 |
* The string code to be fired.
|
|
28 |
*/
|
|
29 |
private String m_string = ""; |
|
30 |
|
|
31 |
/**
|
|
32 |
* Default Constructor.
|
|
33 |
*/
|
|
34 | 233 |
public StringEventData() {
|
35 | 233 |
super();
|
36 | 233 |
setValid(false);
|
37 |
} |
|
38 |
|
|
39 |
/**
|
|
40 |
* Constructor.
|
|
41 |
*
|
|
42 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
43 |
* has to be invoked.
|
|
44 |
* @param comp The component on which to trigger the event.
|
|
45 |
* @param string The string to be sent to the component
|
|
46 |
*/
|
|
47 | 31 |
public StringEventData(final JFCTestCase testCase, final Component comp,
|
48 |
final String string) { |
|
49 | 31 |
this(testCase, comp, string, DEFAULT_KEY_MODIFIERS, DEFAULT_SLEEPTIME);
|
50 |
} |
|
51 |
|
|
52 |
/**
|
|
53 |
* Constructor.
|
|
54 |
*
|
|
55 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
56 |
* @param comp The component on which to trigger the event.
|
|
57 |
* @param string The string to be sent to the component
|
|
58 |
* @param sleepTime The wait time in ms between each event.
|
|
59 |
*/
|
|
60 | 2 |
public StringEventData(final JFCTestCase testCase, final Component comp,
|
61 |
final String string, final long sleepTime) {
|
|
62 | 2 |
this(testCase, comp, string, DEFAULT_KEY_MODIFIERS, sleepTime);
|
63 |
} |
|
64 |
|
|
65 |
/**
|
|
66 |
* Constructor.
|
|
67 |
*
|
|
68 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
69 |
* @param comp The component on which to trigger the event.
|
|
70 |
* @param string The string to be sent to the component
|
|
71 |
* @param modifiers The modifier string values that need to be passed onto the event.
|
|
72 |
* @param sleepTime
|
|
73 |
* The wait time in ms between each event.
|
|
74 |
*/
|
|
75 | 42 |
public StringEventData(final JFCTestCase testCase, final Component comp,
|
76 |
final String string, final int modifiers, final long sleepTime) { |
|
77 | 42 |
setTestCase(testCase); |
78 | 42 |
setSource(comp); |
79 | 42 |
setString(string); |
80 | 42 |
setModifiers(modifiers); |
81 | 42 |
setSleepTime(sleepTime); |
82 | 42 |
setValid(true);
|
83 |
} |
|
84 |
|
|
85 |
/**
|
|
86 |
* Set the attribute value.
|
|
87 |
*
|
|
88 |
* @param comp The new value of the attribute.
|
|
89 |
*/
|
|
90 | 337 |
public final void setSource(final Component comp) { |
91 | 337 |
m_comp = comp; |
92 |
} |
|
93 |
|
|
94 |
/**
|
|
95 |
* Get the attribute value.
|
|
96 |
*
|
|
97 |
* @return Component The value of the attribute.
|
|
98 |
*/
|
|
99 | 1343 |
public final Component getSource() {
|
100 | 1343 |
return m_comp;
|
101 |
} |
|
102 |
|
|
103 |
/**
|
|
104 |
* Set the string to be sent.
|
|
105 |
*
|
|
106 |
* @param string Key code.
|
|
107 |
*/
|
|
108 | 337 |
public final void setString(final String string) { |
109 | 337 |
m_string = string; |
110 | 337 |
setupKeyStrokes(); |
111 |
} |
|
112 |
|
|
113 |
/**
|
|
114 |
* The component on which the event has to be fired.
|
|
115 |
*
|
|
116 |
* @return The component
|
|
117 |
*/
|
|
118 | 1337 |
public Component getComponent() {
|
119 |
// by default, the component is the same as the source
|
|
120 | 1337 |
return getSource();
|
121 |
} |
|
122 |
|
|
123 |
/**
|
|
124 |
* Get the string to be sent.
|
|
125 |
*
|
|
126 |
* @return String string to be sent.
|
|
127 |
*/
|
|
128 | 290 |
public String getString() {
|
129 | 290 |
return m_string;
|
130 |
} |
|
131 |
|
|
132 |
/**
|
|
133 |
* Check if this event can consume the AWTEvent.
|
|
134 |
*
|
|
135 |
* @param ae Event to be consumed.
|
|
136 |
* @return boolean true if the event can be consumed.
|
|
137 |
*/
|
|
138 | 1111 |
public boolean canConsume(final AWTEvent ae) { |
139 | 1111 |
if (!(ae instanceof KeyEvent)) { |
140 | 100 |
return false; |
141 |
} |
|
142 |
|
|
143 |
// If not a defined keystoke then cannot consume by the string.
|
|
144 | 1011 |
JFCKeyStroke[] stroke = getKeyMapping().getKeyStrokes( |
145 |
((KeyEvent) ae).getKeyChar()); |
|
146 | 1011 |
char c = ((KeyEvent) ae).getKeyChar();
|
147 |
|
|
148 | 1011 |
if (Character.isISOControl(c)) {
|
149 | 3 |
return false; |
150 |
} |
|
151 |
|
|
152 | 1008 |
if (isValid()) {
|
153 | 878 |
int mods = ((KeyEvent) ae).getModifiers();
|
154 |
|
|
155 | 878 |
if (getModifiers() != mods) {
|
156 | 49 |
return false; |
157 |
} |
|
158 |
} |
|
159 |
|
|
160 | 959 |
return ((stroke != null) && (stroke.length > 0)) |
161 |
&& super.canConsume(ae)
|
|
162 |
&& (isMetaChar(((KeyEvent) ae).getKeyCode()) |
|
163 |
|| (((KeyEvent) ae).getKeyChar() != KeyEvent.CHAR_UNDEFINED)); |
|
164 |
} |
|
165 |
|
|
166 |
/**
|
|
167 |
* Consume the event.
|
|
168 |
* @param ae Event to be consumed.
|
|
169 |
* @return boolean true if the event was consumed.
|
|
170 |
*/
|
|
171 | 885 |
public boolean consume(final AWTEvent ae) { |
172 | 885 |
if (super.consume(ae)) { |
173 | 590 |
return true; |
174 |
} |
|
175 |
|
|
176 | 295 |
setSource((Component) ae.getSource()); |
177 |
|
|
178 | 295 |
StringBuffer buf = new StringBuffer();
|
179 |
|
|
180 | 295 |
if (isValid()) {
|
181 | 239 |
buf.append(getString()); |
182 |
} |
|
183 |
|
|
184 | 295 |
buf.append(((KeyEvent) ae).getKeyChar()); |
185 | 295 |
setSleepTime(getDefaultSleepTime()); |
186 |
|
|
187 | 295 |
int mods = ((KeyEvent) ae).getModifiers();
|
188 |
|
|
189 | 295 |
super.setModifiers(mods);
|
190 | 295 |
setString(buf.toString()); |
191 | 295 |
setValid(true);
|
192 |
|
|
193 | 295 |
return true; |
194 |
} |
|
195 |
|
|
196 |
/**
|
|
197 |
* Populate the element with the key code.
|
|
198 |
* @param e Element to be populated with the data for this event.
|
|
199 |
*/
|
|
200 | 0 |
public void populate(final Element e) { |
201 | 0 |
super.populate(e);
|
202 | 0 |
e.setAttribute("string", m_string);
|
203 |
} |
|
204 |
|
|
205 |
/**
|
|
206 |
* Generate a description of the event.
|
|
207 |
*
|
|
208 |
* @return String description of the event.
|
|
209 |
*/
|
|
210 | 6 |
public String toString() {
|
211 | 6 |
return "StringEventData:" + getString() + " on " + getComponent() + " " |
212 |
+ getModifiers() + " " + getSleepTime();
|
|
213 |
} |
|
214 |
|
|
215 |
/**
|
|
216 |
* Setup the keystrokes for the event.
|
|
217 |
*/
|
|
218 | 674 |
protected void setupKeyStrokes() { |
219 | 674 |
KeyMapping km = TestHelper.getKeyMapping(); |
220 | 674 |
clearKeyStrokes(); |
221 |
|
|
222 | 674 |
for (int i = 0; i < m_string.length(); i++) { |
223 | 7473 |
JFCKeyStroke[] strokes = km.getKeyStrokes(m_string.charAt(i)); |
224 | 7473 |
addKeyStrokes(strokes); |
225 |
} |
|
226 |
|
|
227 | 674 |
applyModifier(getModifiers()); |
228 |
} |
|
229 |
} |
|
230 |
|
|