001: package abbot.tester;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005:
006: import javax.swing.*;
007:
008: import junit.extensions.abbot.*;
009: import junit.extensions.abbot.Timer;
010: import junit.framework.*;
011:
012: /** Unit test to verify the AbstractButtonTester class.<p>
013:
014: <ul>
015: <li>Test all exported actions.
016: </ul>
017: */
018:
019: public class AbstractButtonTesterTest extends ComponentTestFixture {
020:
021: private AbstractButtonTester tester;
022:
023: /** Create a new test case with the given name. */
024: public AbstractButtonTesterTest(String name) {
025: super (name);
026: }
027:
028: protected void setUp() {
029: tester = (AbstractButtonTester) ComponentTester
030: .getTester(AbstractButton.class);
031: }
032:
033: private JButton findButton(Component comp, String text) {
034: if (comp instanceof JButton) {
035: if (((JButton) comp).getText().equals(text))
036: return (JButton) comp;
037: } else if (comp instanceof Container) {
038: Component[] children = ((Container) comp).getComponents();
039: for (int i = 0; i < children.length; i++) {
040: JButton b = findButton(children[i], text);
041: if (b != null)
042: return b;
043: }
044: }
045: return null;
046: }
047:
048: private JButton findButton(String text) {
049: Frame[] frames = Frame.getFrames();
050: for (int i = 0; i < frames.length; i++) {
051: if (frames[i].isShowing()) {
052: JButton button = findButton(frames[i], text);
053: if (button != null)
054: return button;
055: }
056: Window[] subs = frames[i].getOwnedWindows();
057: for (int j = 0; j < subs.length; j++) {
058: if (subs[j].isShowing()) {
059: JButton button = findButton(subs[j], text);
060: if (button != null)
061: return button;
062: }
063: }
064: }
065: return null;
066: }
067:
068: private class ButtonWatcher implements ActionListener {
069: public boolean gotAction = false;
070:
071: public void actionPerformed(ActionEvent ev) {
072: gotAction = true;
073: }
074: }
075:
076: public void testClick() {
077: final JButton button = new JButton("Hit me");
078: String[] values = { "one", "two", "three" };
079: final JList list = new JList(values);
080: ButtonWatcher bw = new ButtonWatcher() {
081: public void actionPerformed(ActionEvent ev) {
082: super .actionPerformed(ev);
083: JOptionPane.showInputDialog(button, list);
084: }
085: };
086: button.addActionListener(bw);
087: showFrame(button);
088: tester.actionClick(button);
089: assertTrue("Button not pressed", bw.gotAction);
090: Timer timer = new Timer();
091: while (!isShowing("Input")) {
092: if (timer.elapsed() > EVENT_GENERATION_DELAY)
093: fail("Timed out waiting for input dialog");
094: tester.sleep();
095: }
096: tester.actionClick(list);
097: tester.reset();
098: JButton ok = findButton("OK");
099: assertNotNull("Couldn't find OK button", ok);
100: ButtonWatcher bw2 = new ButtonWatcher();
101: ok.addActionListener(bw2);
102: tester.actionClick(ok);
103: timer.reset();
104: while (isShowing("Input")) {
105: if (timer.elapsed() > EVENT_GENERATION_DELAY)
106: fail("Timed out waiting for dialog to close");
107: tester.sleep();
108: }
109: assertTrue("OK button not pressed", bw2.gotAction);
110: }
111:
112: /** Return the default test suite. */
113: public static Test suite() {
114: return new TestSuite(AbstractButtonTesterTest.class);
115: }
116:
117: public static void main(String[] args) {
118: RepeatHelper.runTests(args, AbstractButtonTesterTest.class);
119: }
120: }
|