001: package abbot.editor.editors;
002:
003: import java.awt.event.*;
004: import javax.swing.*;
005:
006: import junit.extensions.abbot.*;
007: import abbot.finder.matchers.*;
008: import abbot.script.*;
009: import abbot.tester.*;
010:
011: /** Verify StepEditor operation. */
012:
013: public class StepEditorTest extends ResolverFixture {
014:
015: private Step step;
016: private DefaultStepEditor editor;
017: private String description = "default value";
018: private JTextComponentTester tester = new JTextComponentTester();
019: private JTextField textField;
020:
021: private class DefaultStepEditor extends StepEditor {
022: JTextField text;
023:
024: public DefaultStepEditor(Step s) {
025: super (s);
026: text = addTextField("dummy field", "empty");
027: }
028:
029: public void actionPerformed(ActionEvent e) {
030: if (e.getSource() == text) {
031: StepEditorTest.this .description = text.getText();
032: // tell the editor the underlying step data has changed
033: fireStepChanged();
034: } else {
035: super .actionPerformed(e);
036: }
037: }
038:
039: public void descriptionChanged() {
040: fireStepChanged();
041: }
042: }
043:
044: protected void setUp() throws Exception {
045: step = new Step(getResolver(), (String) null) {
046: public String getDefaultDescription() {
047: return description;
048: }
049:
050: public String getUsage() {
051: return null;
052: }
053:
054: public String getXMLTag() {
055: return "step";
056: }
057:
058: public void runStep() {
059: }
060: };
061: editor = new DefaultStepEditor(step);
062: textField = (JTextField) getFinder().find(editor,
063: new ClassMatcher(JTextField.class));
064: }
065:
066: public void testInit() {
067: showFrame(editor);
068: assertEquals("Step should match text field", step
069: .getDescription(), textField.getText());
070: }
071:
072: public void testRevertDescription() {
073: showFrame(editor);
074: textField.setText("");
075: String text = step.getDescription();
076: tester.actionEnterText(textField, "dummy");
077: tester.actionKeyStroke(textField, KeyEvent.VK_ESCAPE);
078: assertEquals("Text not reverted", text, textField.getText());
079: assertEquals("Description not reverted", text, step
080: .getDescription());
081: }
082:
083: public void testSetDescription() {
084: showFrame(editor);
085: assertEquals("Wrong initial description", description,
086: textField.getText());
087: textField.setText("");
088: tester.actionEnterText(textField, "hello");
089: assertEquals("Text entry did not change step data", "hello",
090: step.getDescription());
091:
092: // Clearing it should result in the default description
093: description = "default";
094: textField.setText("");
095: tester.actionKeyStroke(textField, KeyEvent.VK_ENTER);
096: assertEquals("Step should revert to default when cleared",
097: description, step.getDescription());
098: assertEquals(
099: "Text field should revert to default when cleared",
100: description, textField.getText());
101:
102: // Now edit with key input
103: tester.actionSetCaretPosition(textField, textField.getText()
104: .length());
105: tester.actionKeyStroke(textField, KeyEvent.VK_BACK_SPACE);
106: assertEquals("Step should be properly edited", description
107: .substring(0, description.length() - 1), textField
108: .getText());
109: }
110:
111: public void testClearDescription() {
112: // clearing followed by enter or focus change should revert to default
113: showFrame(editor);
114: tester.actionSelectText(textField, 0, step.getDescription()
115: .length());
116: tester.actionKeyStroke(textField, KeyEvent.VK_BACK_SPACE);
117: tester.actionKeyStroke(textField, KeyEvent.VK_ENTER);
118: assertEquals("Step should revert to default description", step
119: .getDefaultDescription(), step.getDescription());
120: assertEquals("Text field should revert to default description",
121: step.getDefaultDescription(), textField.getText());
122: }
123:
124: public void testUpdateDefaultDescription() throws Throwable {
125: showFrame(editor);
126: // Change from underlying step data
127: description = "something else";
128: editor.descriptionChanged();
129: assertEquals("Editor didn't pick up underlying data change",
130: description, textField.getText());
131:
132: // Change from other field which affects the description should cause
133: // the description field to update if the description is the default
134: tester.actionKeyString(editor.text, "hello");
135: assertEquals(
136: "Description didn't change in response to step data",
137: description, textField.getText());
138: // Remove the description, it should be restored
139: textField.setText("");
140: tester.actionKeyStroke(editor.text, KeyEvent.VK_ENTER);
141: assertEquals(
142: "Description should revert to default when removed",
143: description, textField.getText());
144: }
145:
146: /** Construct a test case with the given name. */
147: public StepEditorTest(String name) {
148: super (name);
149: }
150:
151: /** Run the default test suite. */
152: public static void main(String[] args) {
153: TestHelper.runTests(args, StepEditorTest.class);
154: }
155: }
|