001: package abbot.editor.widgets;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006:
007: import junit.extensions.abbot.*;
008: import abbot.tester.*;
009: import abbot.finder.*;
010: import abbot.finder.matchers.*;
011:
012: public class ArrayEditorTest extends ComponentTestFixture {
013:
014: private class EditorMatcher extends ClassMatcher {
015: private int currentRow = 0;
016: private int row;
017:
018: public EditorMatcher(int row) {
019: super (JTextField.class);
020: this .row = row;
021: }
022:
023: public boolean matches(Component c) {
024: return super .matches(c) && currentRow++ == row;
025: }
026: }
027:
028: private class ButtonMatcher extends ClassMatcher {
029: private int currentRow = 0;
030: private int row;
031: private String text;
032:
033: public ButtonMatcher(int row, String text) {
034: super (JButton.class);
035: this .text = text;
036: this .row = row;
037: }
038:
039: public boolean matches(Component c) {
040: return super .matches(c)
041: && ((JButton) c).getText().equals(text)
042: && currentRow++ == row;
043: }
044: }
045:
046: private JTextField getEditorAt(ArrayEditor ed, int row) {
047: try {
048: return (JTextField) getFinder().find(ed,
049: new EditorMatcher(row));
050: } catch (ComponentSearchException e) {
051: return null;
052: }
053: }
054:
055: private JButton getButtonAt(ArrayEditor ed, int row, String text) {
056: try {
057: return (JButton) getFinder().find(ed,
058: new ButtonMatcher(row, text));
059: } catch (ComponentSearchException e) {
060: return null;
061: }
062: }
063:
064: public void testCreateEmptyList() {
065: ArrayEditor ed = new ArrayEditor();
066: assertEquals("Wrong row count", 0, ed.getRowCount());
067: assertEquals("Should have some contents", 1, ed
068: .getComponentCount());
069: }
070:
071: public void testPopulated() {
072: String[] values = { "one", "two", "three", "four" };
073: ArrayEditor ed = new ArrayEditor(values);
074: assertEquals("Wrong row count", values.length, ed.getRowCount());
075: for (int i = 0; i < values.length; i++) {
076: assertEquals("Wrong value", values[i], ed.getValueAt(i));
077: }
078: }
079:
080: public void testInsertRow() {
081: ArrayEditor ed = new ArrayEditor();
082: assertTrue("text should be disabled when empty", !getEditorAt(
083: ed, 0).isEnabled());
084: assertTrue("remove should be disabled when empty",
085: !getButtonAt(ed, 0, "-").isEnabled());
086: assertTrue("add should be disabled when empty", getButtonAt(ed,
087: 0, "+").isEnabled());
088:
089: ed.insertRow(0);
090: assertEquals("Wrong row count", 1, ed.getRowCount());
091: assertEquals("Wrong component count", 1, ed.getComponentCount());
092: assertEquals("Expect empty initial value", "", ed.getValueAt(0));
093: assertTrue("text should be enabled when non-empty",
094: getEditorAt(ed, 0).isEnabled());
095: assertTrue("remove should be enabled when non-empty",
096: getButtonAt(ed, 0, "-").isEnabled());
097: assertTrue("add should be enabled when non-empty", getButtonAt(
098: ed, 0, "+").isEnabled());
099:
100: assertEquals("Wrong value", "", ed.getValueAt(0));
101: assertEquals("Value mismatch at 0", "", getEditorAt(ed, 0)
102: .getText());
103:
104: ed.insertRow(ed.getRowCount(), "bubba");
105: assertEquals("Wrong row count", 2, ed.getRowCount());
106: assertEquals("Wrong value", "bubba", ed.getValueAt(1));
107: assertEquals("Value mismatch at 1", "bubba", getEditorAt(ed, 1)
108: .getText());
109:
110: ed.insertRow(ed.getRowCount(), "shrimp");
111: assertEquals("Wrong row count", 3, ed.getRowCount());
112: assertEquals("Wrong value", "shrimp", ed.getValueAt(2));
113: assertEquals("Value mismatch at 2", "shrimp",
114: getEditorAt(ed, 2).getText());
115:
116: // insert into middle
117: ed.insertRow(ed.getRowCount() - 1, "gump");
118: assertEquals("Wrong row count", 4, ed.getRowCount());
119: assertEquals("Wrong value", "gump", ed.getValueAt(2));
120: assertEquals("Value mismatch at 2", "gump", getEditorAt(ed, 2)
121: .getText());
122: assertEquals("Wrong value", "shrimp", ed.getValueAt(3));
123: assertEquals("Value mismatch at 3", "shrimp",
124: getEditorAt(ed, 3).getText());
125:
126: String value = "salvation";
127: ed.insertRow(-1, value);
128: assertEquals("Insert with negative adds to end", value, ed
129: .getValueAt(ed.getRowCount() - 1));
130:
131: // Use the button to add a row
132: showFrame(ed);
133: int count = ed.getRowCount();
134: tester.actionClick(getButtonAt(ed, 1, "+"));
135: assertEquals("Row not inserted", count + 1, ed.getRowCount());
136: assertEquals("Wrong insertion point", "", ed.getValueAt(2));
137: }
138:
139: public void testRemoveRow() {
140: String[] values = { "one", "two", "three", "four" };
141: ArrayEditor ed = new ArrayEditor(values);
142: assertEquals("Wrong row count", values.length, ed.getRowCount());
143:
144: // remove middle
145: ed.removeRow(1);
146: assertEquals("Row not removed", values.length - 1, ed
147: .getRowCount());
148: assertEquals("Wrong values remaining", values[0], ed
149: .getValueAt(0));
150: assertEquals("Value mismatch at 0", values[0], getEditorAt(ed,
151: 0).getText());
152: assertEquals("Wrong values remaining", values[2], ed
153: .getValueAt(1));
154: assertEquals("Value mismatch at 1", values[2], getEditorAt(ed,
155: 1).getText());
156:
157: // remove last
158: ed.removeRow(ed.getRowCount() - 1);
159: assertEquals("Wrong row count", 2, ed.getRowCount());
160: assertEquals("Wrong value", "three", ed.getValueAt(1));
161: assertEquals("Value mismatch at 1", "three", getEditorAt(ed, 1)
162: .getText());
163: ed.removeRow(ed.getRowCount() - 1);
164: assertEquals("Wrong row count", 1, ed.getRowCount());
165: assertEquals("Wrong value", "one", ed.getValueAt(0));
166: assertEquals("Value mismatch at 0", "one", getEditorAt(ed, 0)
167: .getText());
168: ed.removeRow(0);
169: assertEquals("Should be empty", 0, ed.getRowCount());
170:
171: assertTrue("text should be disabled when empty", !getEditorAt(
172: ed, 0).isEnabled());
173: assertTrue("remove should be disabled when empty",
174: !getButtonAt(ed, 0, "-").isEnabled());
175: assertTrue("add should be disabled when empty", getButtonAt(ed,
176: 0, "+").isEnabled());
177:
178: // Use the button to remove one
179: ed.setValues(values);
180: showFrame(ed);
181: int count = ed.getRowCount();
182: tester.actionClick(getButtonAt(ed, count - 1, "-"));
183: assertEquals("Row not removed", count - 1, ed.getRowCount());
184: tester.actionClick(getButtonAt(ed, 0, "-"));
185: assertEquals("Row not removed", count - 2, ed.getRowCount());
186: assertEquals("Wrong row removed", values[1], ed.getValueAt(0));
187: }
188:
189: public void testChangeRow() {
190: String[] values = { "one", "two", "three" };
191: ArrayEditor ed = new ArrayEditor(values);
192: showFrame(ed);
193: tester.actionSetCaretPosition(getEditorAt(ed, 0), 0);
194: tester.actionKeyStroke(getEditorAt(ed, 0), KeyEvent.VK_A);
195: assertEquals("Wrong text", "aone", ed.getValueAt(0));
196: tester.actionKeyStroke(getEditorAt(ed, 0), KeyEvent.VK_B);
197: assertEquals("Wrong text", "abone", ed.getValueAt(0));
198: tester.actionSetCaretPosition(getEditorAt(ed, 1), 0);
199: tester.actionKeyStroke(getEditorAt(ed, 1), KeyEvent.VK_A);
200: assertEquals("Wrong text", "atwo", ed.getValueAt(1));
201: }
202:
203: private class Listener implements ActionListener {
204: public boolean triggered;
205: public String event;
206: public int index;
207:
208: public void actionPerformed(ActionEvent e) {
209: event = e.getActionCommand();
210: index = e.getID();
211: triggered = true;
212: }
213: }
214:
215: public void testListeners() {
216: ArrayEditor ed = new ArrayEditor();
217: Listener l = new Listener();
218: ed.addActionListener(l);
219:
220: l.triggered = false;
221: ed.insertRow(0, "new");
222: assertTrue("No event triggered", l.triggered);
223: assertEquals("Wrong event type",
224: ArrayEditor.ACTION_ITEM_INSERTED, l.event);
225: assertEquals("Wrong index", 0, l.index);
226:
227: l.triggered = false;
228: ed.setValueAt(0, "old");
229: assertTrue("No event triggered", l.triggered);
230: assertEquals("Wrong event type",
231: ArrayEditor.ACTION_ITEM_CHANGED, l.event);
232: assertEquals("Wrong index", 0, l.index);
233:
234: l.triggered = false;
235: ed.setValues(new String[] { "old", "new" });
236: assertTrue("No event triggered", l.triggered);
237: assertEquals("Wrong event type",
238: ArrayEditor.ACTION_LIST_CHANGED, l.event);
239: assertEquals("Wrong index", -1, l.index);
240:
241: l.triggered = false;
242: ed.removeRow(0);
243: assertTrue("No event triggered", l.triggered);
244: assertEquals("Wrong event type",
245: ArrayEditor.ACTION_ITEM_DELETED, l.event);
246: assertEquals("Wrong index", 0, l.index);
247: }
248:
249: public void testRemoveBadIndex() {
250: ArrayEditor ed = new ArrayEditor();
251: try {
252: ed.removeRow(10);
253: fail("Should throw an exception when arg out of range");
254: } catch (IllegalArgumentException e) {
255: }
256: }
257:
258: private JTextComponentTester tester;
259:
260: protected void setUp() {
261: tester = new JTextComponentTester();
262: }
263:
264: /** Construct a test case with the given name. */
265: public ArrayEditorTest(String name) {
266: super (name);
267: }
268:
269: /** Run the default test suite. */
270: public static void main(String[] args) {
271: TestHelper.runTests(args, ArrayEditorTest.class);
272: }
273: }
|