01: package abbot.tester;
02:
03: import java.awt.*;
04: import java.awt.event.*;
05:
06: import abbot.util.*;
07:
08: /** AWT Choice (ComboBox/picklist) support. */
09: public class ChoiceTester extends ComponentTester {
10:
11: private int CHOICE_DELAY = Properties.getProperty(
12: "abbot.tester.choice_delay", 30000, 0, 60000);
13:
14: private class Listener implements AWTEventListener {
15: public volatile boolean gotChange;
16: private int targetIndex = -1;
17:
18: public Listener(int index) {
19: targetIndex = index;
20: }
21:
22: public void eventDispatched(AWTEvent e) {
23: if (e.getID() == ItemEvent.ITEM_STATE_CHANGED
24: && e.getSource() instanceof Choice) {
25: gotChange = ((Choice) e.getSource()).getSelectedIndex() == targetIndex;
26: }
27: }
28: }
29:
30: /** Select an item by index. */
31: public void actionSelectIndex(Component c, final int index) {
32: final Choice choice = (Choice) c;
33: int current = choice.getSelectedIndex();
34: if (current == index)
35: return;
36:
37: // Don't add an item listener, because then we're at the mercy of any
38: // other ItemListener finishing. Don't bother clicking or otherwise
39: // sending events, since the behavior is platform-specific.
40: Listener listener = new Listener(index);
41: new WeakAWTEventListener(listener, ItemEvent.ITEM_EVENT_MASK);
42:
43: choice.select(index);
44: ItemEvent ie = new ItemEvent(choice,
45: ItemEvent.ITEM_STATE_CHANGED, choice
46: .getSelectedObjects()[0], ItemEvent.SELECTED);
47: postEvent(choice, ie);
48:
49: long now = System.currentTimeMillis();
50: while (!listener.gotChange) {
51: if (System.currentTimeMillis() - now > CHOICE_DELAY)
52: throw new ActionFailedException(
53: "Choice didn't fire for " + "index " + index);
54: sleep();
55: }
56: waitForIdle();
57: }
58:
59: /** Select an item by its String representation. */
60: public void actionSelectItem(Component c, String item) {
61: Choice choice = (Choice) c;
62: for (int i = 0; i < choice.getItemCount(); i++) {
63: if (ExtendedComparator
64: .stringsMatch(choice.getItem(i), item)) {
65: try {
66: actionSelectIndex(c, i);
67: return;
68: } catch (ActionFailedException e) {
69: throw new ActionFailedException(
70: "Choice didn't fire for " + "item '" + item
71: + "'");
72: }
73: }
74: }
75: throw new ActionFailedException("Item '" + item
76: + "' not found in Choice");
77: }
78: }
|