001: package abbot.editor.editors;
002:
003: import java.util.ArrayList;
004:
005: import java.awt.Frame;
006: import java.awt.event.KeyEvent;
007: import javax.swing.*;
008:
009: import junit.extensions.abbot.*;
010: import abbot.finder.matchers.*;
011: import abbot.script.*;
012: import abbot.tester.*;
013:
014: /** Verify AssertEditor operation. */
015:
016: public class AssertEditorTest extends ResolverFixture implements
017: XMLConstants {
018:
019: private Assert step;
020: private AssertEditor editor;
021: private JComponentTester tester;
022:
023: public void setUp() {
024: step = new Assert(getResolver(), getName(),
025: ComponentTester.class.getName(), "assertFrameShowing",
026: new String[0], "true", false);
027: editor = new AssertEditor(step);
028: tester = new JComponentTester();
029: }
030:
031: public void testWaitOptions() throws Exception {
032: showFrame(editor);
033: JCheckBox waitBox = (JCheckBox) getFinder().find(editor,
034: new NameMatcher(TAG_WAIT));
035:
036: int count = editor.getComponentCount();
037: tester.actionClick(waitBox);
038: assertTrue("No components were added", count < editor
039: .getComponentCount());
040:
041: tester.actionClick(waitBox);
042: assertEquals("Wait option components not properly removed",
043: count, editor.getComponentCount());
044: }
045:
046: public void testClearTimeout() throws Exception {
047: // When a required field is cleared, its contents should be restored
048: // to the default value on ENTER
049: final Frame frame = showFrame(editor);
050: JCheckBox wait = (JCheckBox) getFinder().find(editor,
051: new NameMatcher(TAG_WAIT));
052: tester.actionClick(wait);
053: invokeAndWait(new Runnable() {
054: public void run() {
055: frame.setSize(frame.getPreferredSize());
056: }
057: });
058: editor.revalidate();
059: editor.repaint();
060:
061: JTextField timeout = (JTextField) getFinder().find(editor,
062: new NameMatcher(TAG_TIMEOUT));
063: tester.actionActionMap(timeout, "select-all");
064: tester.actionKeyStroke(timeout, KeyEvent.VK_DELETE);
065: tester.waitForIdle();
066: assertEquals("Text field should be empty on delete contents",
067: "", timeout.getText());
068: tester.actionKeyStroke(timeout, KeyEvent.VK_ENTER);
069: assertEquals(
070: "Text field should hold default value after Enter",
071: String.valueOf(step.getTimeout()), timeout.getText());
072: }
073:
074: public void testTesterMethodList() throws Throwable {
075: showFrame(editor);
076:
077: JComboBox cb = editor.method;
078: String[] expected = { "assertComponentShowing",
079: "assertFrameShowing", "assertImage", };
080: assertTrue(
081: "Too few methods in combo box: " + cb.getItemCount(),
082: cb.getItemCount() >= expected.length);
083: for (int i = 0; i < cb.getItemCount(); i++) {
084: assertTrue("Too many combo box items (next is "
085: + cb.getItemAt(i) + ")", i < expected.length);
086: assertEquals("Wrong method in combo list", expected[i], cb
087: .getItemAt(i));
088: }
089: assertEquals("Wrong number of combo box items",
090: expected.length, cb.getItemCount());
091: }
092:
093: public void testComponentMethodList() throws Throwable {
094: JComboBox methodChooser = (JComboBox) getFinder().find(editor,
095: new NameMatcher(TAG_METHOD));
096:
097: showFrame(editor);
098: // Make sure we have at least one reference in the list
099: ComponentReference cr = step.getResolver().addComponent(
100: methodChooser);
101:
102: JComboBoxTester tester = new JComboBoxTester();
103: JComboBox refChooser = (JComboBox) getFinder().find(editor,
104: new NameMatcher(TAG_COMPONENT));
105:
106: // Alter the step to refer to a property on a component
107: tester.actionSelectItem(refChooser, cr.getID());
108: tester.actionSelectItem(methodChooser, "getItemCount");
109: assertEquals("Wrong method selected", "getItemCount",
110: editor.method.getSelectedItem());
111:
112: ArrayList list = new ArrayList();
113: for (int i = 0; i < methodChooser.getItemCount(); i++) {
114: list.add(methodChooser.getItemAt(i));
115: }
116: assertEquals("Wrong target class", JComboBox.class, step
117: .getTargetClass());
118: assertTrue("No items in the list",
119: methodChooser.getItemCount() > 0);
120: String[] expected = { "getParent", "isOpaque", "hasFocus",
121: // From the JComboBoxTester
122: "getContents", };
123: for (int i = 0; i < expected.length; i++) {
124: assertTrue(expected[i] + " method missing", list
125: .contains(expected[i]));
126: }
127: }
128:
129: public void testSwitchFromTesterToComponentMethod()
130: throws Exception {
131: Frame f = showFrame(editor);
132: JComboBox methodChooser = (JComboBox) getFinder().find(editor,
133: new NameMatcher(TAG_METHOD));
134:
135: ComponentReference cr = step.getResolver().addComponent(
136: methodChooser);
137: hideWindow(f);
138:
139: step = new Assert(getResolver(), getName(),
140: JComboBoxTester.class.getName(), "getContents",
141: new String[] { cr.getID() }, "[one,two,three]", false);
142:
143: editor = new AssertEditor(step);
144: showFrame(editor);
145: methodChooser = (JComboBox) getFinder().find(editor,
146: new NameMatcher(TAG_METHOD));
147:
148: JComboBoxTester tester = new JComboBoxTester();
149: // FIXME why do we need to do this twice?
150: tester.actionSelectItem(methodChooser, "getToolkit");
151: tester.actionSelectItem(methodChooser, "getToolkit");
152: assertEquals("Component method not selected", "getToolkit",
153: methodChooser.getSelectedItem());
154:
155: // FIXME check other parts of editor
156: }
157:
158: /** Construct a test case with the given name. */
159: public AssertEditorTest(String name) {
160: super (name);
161: }
162:
163: /** Run the default test suite. */
164: public static void main(String[] args) {
165: TestHelper.runTests(args, AssertEditorTest.class);
166: }
167: }
|