|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
JMenuMouseEventData.java | 43.3% | 66% | 60% | 60.4% |
|
1 |
package junit.extensions.jfcunit.eventdata;
|
|
2 |
|
|
3 |
import junit.extensions.jfcunit.JFCTestCase;
|
|
4 |
import junit.extensions.jfcunit.TestHelper;
|
|
5 |
import junit.extensions.jfcunit.xml.JFCXMLConstants;
|
|
6 |
|
|
7 |
import org.w3c.dom.Element;
|
|
8 |
|
|
9 |
import java.awt.AWTEvent;
|
|
10 |
import java.awt.Component;
|
|
11 |
|
|
12 |
import javax.swing.JComponent;
|
|
13 |
import javax.swing.JMenu;
|
|
14 |
import javax.swing.JMenuBar;
|
|
15 |
import javax.swing.JMenuItem;
|
|
16 |
import javax.swing.JPopupMenu;
|
|
17 |
|
|
18 |
|
|
19 |
/**
|
|
20 |
* Data container class that holds all the data necessary for jfcUnit to fire mouse events.
|
|
21 |
* This class is specific to events on a {@link javax.swing.JComboBox}.
|
|
22 |
*
|
|
23 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
24 |
*/
|
|
25 |
public class JMenuMouseEventData extends AbstractMouseEventData { |
|
26 |
/**
|
|
27 |
* The {@link JMenuBar} on which to trigger the event.
|
|
28 |
*/
|
|
29 |
private JComponent m_menu;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* The zero-based index of the specific menu elements on which to trigger the event.
|
|
33 |
*/
|
|
34 |
private int[] m_elementIndexes; |
|
35 |
|
|
36 |
/**
|
|
37 |
* Constructor.
|
|
38 |
*/
|
|
39 | 300 |
public JMenuMouseEventData() {
|
40 | 300 |
super();
|
41 | 300 |
setValid(false);
|
42 |
} |
|
43 |
|
|
44 |
/**
|
|
45 |
* Constructor.
|
|
46 |
*
|
|
47 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
48 |
* @param menu The {@link JMenu} on which to trigger the event.
|
|
49 |
* @param elementIndexes
|
|
50 |
* The zero-based index of the menu items to select.
|
|
51 |
*/
|
|
52 | 0 |
public JMenuMouseEventData(final JFCTestCase testCase,
|
53 |
final JComponent menu, final int[] elementIndexes) {
|
|
54 | 0 |
this(testCase, menu, elementIndexes, DEFAULT_NUMBEROFCLICKS,
|
55 |
DEFAULT_MOUSE_MODIFIERS, DEFAULT_ISPOPUPTRIGGER, DEFAULT_SLEEPTIME); |
|
56 | 0 |
setValid(true);
|
57 |
} |
|
58 |
|
|
59 |
/**
|
|
60 |
* Constructor.
|
|
61 |
*
|
|
62 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
63 |
* @param menu The {@link JMenu} on which to trigger the event.
|
|
64 |
* @param elementIndexes
|
|
65 |
* The zero-based index of the menu items to select.
|
|
66 |
* @param numberOfClicks
|
|
67 |
* Number of clicks in the {@link MouseEvent} (1 for single-click, 2 for double clicks)
|
|
68 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
69 |
* @param isPopupTrigger
|
|
70 |
* boolean specifying whether this event will show a popup.
|
|
71 |
* @param sleepTime
|
|
72 |
* The wait time in ms between each event.
|
|
73 |
*/
|
|
74 | 32 |
public JMenuMouseEventData(final JFCTestCase testCase,
|
75 |
final JComponent menu, final int[] elementIndexes,
|
|
76 |
final int numberOfClicks, final int modifiers, |
|
77 |
final boolean isPopupTrigger, final long sleepTime) { |
|
78 | 32 |
setTestCase(testCase); |
79 | 32 |
setSource(menu); |
80 | 32 |
setNumberOfClicks(numberOfClicks); |
81 | 32 |
setModifiers(modifiers); |
82 | 32 |
setPopupTrigger(isPopupTrigger); |
83 | 32 |
setElementIndexes(elementIndexes); |
84 | 32 |
setSleepTime(sleepTime); |
85 | 32 |
setValid(true);
|
86 |
} |
|
87 |
|
|
88 |
/**
|
|
89 |
* Set the attribute value.
|
|
90 |
*
|
|
91 |
* @param menu The new value of the attribute.
|
|
92 |
*/
|
|
93 | 52 |
public void setSource(final JComponent menu) { |
94 | 52 |
m_menu = menu; |
95 |
} |
|
96 |
|
|
97 |
/**
|
|
98 |
* Get the attribute value.
|
|
99 |
*
|
|
100 |
* @return JComboBox The value of the attribute.
|
|
101 |
*/
|
|
102 | 10 |
public final JComponent getSource() {
|
103 | 10 |
return m_menu;
|
104 |
} |
|
105 |
|
|
106 |
/**
|
|
107 |
* The component on which the event has to be fired.
|
|
108 |
*
|
|
109 |
* @return The component.
|
|
110 |
*/
|
|
111 | 10 |
public Component getComponent() {
|
112 |
// by default, the component is the same as the source
|
|
113 | 10 |
return getSource();
|
114 |
} |
|
115 |
|
|
116 |
/**
|
|
117 |
* Set the attribute value.
|
|
118 |
*
|
|
119 |
* @param elementIndexes The new value of the attribute.
|
|
120 |
*/
|
|
121 | 52 |
public final void setElementIndexes(final int[] elementIndexes) { |
122 | 52 |
m_elementIndexes = elementIndexes; |
123 |
} |
|
124 |
|
|
125 |
/**
|
|
126 |
* Get the attribute value.
|
|
127 |
*
|
|
128 |
* @return int The value of the attribute.
|
|
129 |
*/
|
|
130 | 0 |
public final int[] getElementIndexes() { |
131 | 0 |
return m_elementIndexes;
|
132 |
} |
|
133 |
|
|
134 |
/**
|
|
135 |
* Returns true if the event can be consumed by
|
|
136 |
* this instance of event data.
|
|
137 |
*
|
|
138 |
* @param ae Event to be consumed.
|
|
139 |
* @return true if the event was consumed.
|
|
140 |
*/
|
|
141 | 466 |
public boolean canConsume(final AWTEvent ae) { |
142 | 466 |
Object source = ae.getSource(); |
143 |
|
|
144 | 466 |
if (!super.canConsume(ae) |
145 |
|| !((source instanceof JMenuItem)
|
|
146 |
|| (source instanceof JPopupMenu)
|
|
147 |
|| (source instanceof JMenuBar))) {
|
|
148 | 278 |
return false; |
149 |
} |
|
150 |
|
|
151 | 188 |
return true; |
152 |
} |
|
153 |
|
|
154 |
/**
|
|
155 |
* Consume the event.
|
|
156 |
*
|
|
157 |
* @param ae AWTEvent to be consumed.
|
|
158 |
* @return boolean true if the event was consumed.
|
|
159 |
*/
|
|
160 | 188 |
public boolean consume(final AWTEvent ae) { |
161 | 188 |
Object source = ae.getSource(); |
162 |
|
|
163 | 188 |
if (source instanceof JMenu) { |
164 | 108 |
return true; |
165 |
} |
|
166 |
|
|
167 | 80 |
if (super.consume(ae)) { |
168 | 60 |
return true; |
169 |
} |
|
170 |
|
|
171 | 20 |
JMenuItem item = (JMenuItem) source; |
172 | 20 |
PathData data = new PathData(item);
|
173 | 20 |
Object root = data.getRoot(item); |
174 |
|
|
175 | 20 |
if (root instanceof JMenuBar) { |
176 | 20 |
JMenuBar bar = (JMenuBar) root; |
177 | 20 |
setSource(bar); |
178 | 20 |
setElementIndexes(data.getIndexes(bar)); |
179 | 0 |
} else if (root instanceof JPopupMenu) { |
180 | 0 |
JPopupMenu popup = (JPopupMenu) root; |
181 | 0 |
setSource(popup); |
182 | 0 |
setElementIndexes(data.getIndexes(popup)); |
183 |
} |
|
184 |
|
|
185 |
// JMenuBar bar = (JMenuBar)data.getRoot(item);
|
|
186 |
// setSource(bar);
|
|
187 | 20 |
setPosition(CENTER); |
188 | 20 |
setReferencePoint(null);
|
189 | 20 |
setSleepTime(getDefaultSleepTime()); |
190 | 20 |
setValid(true);
|
191 |
|
|
192 | 20 |
return true; |
193 |
} |
|
194 |
|
|
195 |
/**
|
|
196 |
* Compare to event datas and determine if they are equal.
|
|
197 |
*
|
|
198 |
* @param o Object to be compared.
|
|
199 |
* @return true if the events are the same.
|
|
200 |
*/
|
|
201 | 0 |
public boolean equals(final Object o) { |
202 | 0 |
if (!super.equals(o)) { |
203 | 0 |
return false; |
204 |
} |
|
205 |
|
|
206 | 0 |
JMenuMouseEventData data = (JMenuMouseEventData) o; |
207 |
|
|
208 | 0 |
return (data.getSource() == getSource())
|
209 |
&& (data.getElementIndexes() == getElementIndexes()); |
|
210 |
} |
|
211 |
|
|
212 |
/**
|
|
213 |
* Get the hashCode.
|
|
214 |
* @return int hashCode.
|
|
215 |
*/
|
|
216 | 0 |
public int hashCode() { |
217 | 0 |
return super.hashCode(); |
218 |
} |
|
219 |
|
|
220 |
/**
|
|
221 |
* Populate the JComboBoxMouseEventData XML element.
|
|
222 |
* @param e element to be populated.
|
|
223 |
*/
|
|
224 | 0 |
public void populate(final Element e) { |
225 | 0 |
super.populate(e);
|
226 |
|
|
227 | 0 |
StringBuffer buf = new StringBuffer();
|
228 |
|
|
229 | 0 |
for (int i = 0; i < this.m_elementIndexes.length; i++) { |
230 | 0 |
buf.append(m_elementIndexes[i]); |
231 | 0 |
buf.append(",");
|
232 |
} |
|
233 |
|
|
234 | 0 |
buf.setLength(buf.length() - 1); |
235 | 0 |
e.setAttribute( |
236 |
JFCXMLConstants.INDEXES, |
|
237 |
buf.toString()); |
|
238 | 0 |
e.setAttribute(JFCXMLConstants.TYPE, "JMenuMouseEventData");
|
239 |
} |
|
240 |
|
|
241 |
/**
|
|
242 |
* Prepare the {@link Component} to receive the event.
|
|
243 |
*
|
|
244 |
* @return true if the component is ready to receive the event.
|
|
245 |
*/
|
|
246 | 32 |
public boolean prepareComponent() { |
247 | 32 |
JFCTestCase tc = getTestCase(); |
248 | 32 |
TestHelper th = tc.getHelper(); |
249 | 32 |
tc.resumeAWT(); |
250 |
|
|
251 | 32 |
JComponent m = m_menu; |
252 | 32 |
Component comp = m.getComponent(m_elementIndexes[0]); |
253 | 32 |
th.enterClickAndLeave(new MouseEventData(tc, comp, 1));
|
254 |
|
|
255 | 32 |
for (int i = 1; i < m_elementIndexes.length; i++) { |
256 | 64 |
if (comp instanceof JMenu) { |
257 | 64 |
JMenu menu = (JMenu) comp; |
258 | 64 |
comp = menu.getMenuComponent(m_elementIndexes[i]); |
259 | 64 |
th.enterClickAndLeave( |
260 |
new MouseEventData(
|
|
261 |
getTestCase(), |
|
262 |
comp, |
|
263 |
0, |
|
264 |
0)); |
|
265 | 64 |
tc.flushAWT(); |
266 |
|
|
267 | 64 |
if (i != (m_elementIndexes.length - 1)) {
|
268 | 32 |
JPopupMenu pm = menu.getPopupMenu(); |
269 | 32 |
int popupLeft = pm.getLocationOnScreen().x;
|
270 | 32 |
int popupRight = popupLeft + pm.getSize().width;
|
271 | 32 |
int popupTop = pm.getLocationOnScreen().y;
|
272 | 32 |
int popupBottom = popupTop + pm.getSize().height;
|
273 | 32 |
int left = menu.getLocationOnScreen().x;
|
274 | 32 |
int right = left + menu.getSize().width;
|
275 | 32 |
int top = menu.getLocationOnScreen().y;
|
276 | 32 |
int bottom = top + menu.getSize().height;
|
277 |
|
|
278 |
// Rectangle popupBounds = pm.getBounds();
|
|
279 |
// Rectangle bounds = menu.getBounds();
|
|
280 |
// // check for horizontal move to submenu
|
|
281 |
// // Off to the right
|
|
282 |
// int right = bounds.x + bounds.width;
|
|
283 |
// int top = bounds.y;
|
|
284 |
// int bottom = bounds.y + bounds.height;
|
|
285 |
// int left = bounds.x;
|
|
286 |
// int popupRight = popupBounds.x + popupBounds.width;
|
|
287 |
// int popupLeft = popupBounds.x;
|
|
288 |
// int popupTop = popupBounds.y;
|
|
289 |
// int popupBottom = popupBounds.y + popupBounds.height;
|
|
290 | 32 |
if (right < popupRight) {
|
291 |
// Move to the right edge
|
|
292 | 32 |
th.enterClickAndLeave( |
293 |
new MouseEventData(
|
|
294 |
getTestCase(), |
|
295 |
comp, |
|
296 |
0, |
|
297 |
0, |
|
298 |
false,
|
|
299 |
0, |
|
300 |
EAST)); |
|
301 | 0 |
} else if (popupLeft < left) { |
302 |
// Off to the left
|
|
303 | 0 |
th.enterClickAndLeave( |
304 |
new MouseEventData(
|
|
305 |
getTestCase(), |
|
306 |
comp, |
|
307 |
0, |
|
308 |
0, |
|
309 |
false,
|
|
310 |
0, |
|
311 |
WEST)); |
|
312 | 0 |
} else if (top < popupTop) { |
313 |
// Below
|
|
314 |
// Move to the right edge
|
|
315 | 0 |
th.enterClickAndLeave( |
316 |
new MouseEventData(
|
|
317 |
getTestCase(), |
|
318 |
comp, |
|
319 |
0, |
|
320 |
0, |
|
321 |
false,
|
|
322 |
0, |
|
323 |
SOUTH)); |
|
324 | 0 |
} else if (popupBottom < bottom) { |
325 |
// Above
|
|
326 | 0 |
th.enterClickAndLeave( |
327 |
new MouseEventData(
|
|
328 |
getTestCase(), |
|
329 |
comp, |
|
330 |
0, |
|
331 |
0, |
|
332 |
false,
|
|
333 |
0, |
|
334 |
NORTH)); |
|
335 |
} |
|
336 |
|
|
337 |
// tc.flushAWT();
|
|
338 |
} |
|
339 |
} |
|
340 |
} |
|
341 |
|
|
342 | 32 |
th.enterClickAndLeave(new MouseEventData(
|
343 |
getTestCase(), |
|
344 |
comp)); |
|
345 | 32 |
getTestCase().flushAWT(); |
346 |
|
|
347 | 32 |
return false; |
348 |
} |
|
349 |
|
|
350 |
/**
|
|
351 |
* Get string description of event.
|
|
352 |
*
|
|
353 |
* @return String description of event.
|
|
354 |
*/
|
|
355 | 0 |
public String toString() {
|
356 | 0 |
if (!isValid()) {
|
357 | 0 |
return super.toString(); |
358 |
} |
|
359 |
|
|
360 | 0 |
StringBuffer buf = new StringBuffer(1000);
|
361 | 0 |
buf.append(super.toString());
|
362 | 0 |
buf.append(" index: " + getElementIndexes());
|
363 |
|
|
364 | 0 |
return buf.toString();
|
365 |
} |
|
366 |
} |
|
367 |
|
|