1
|
|
package abbot.editor.widgets;
|
2
|
|
|
3
|
|
import java.awt.*;
|
4
|
|
import java.awt.event.*;
|
5
|
|
import javax.swing.*;
|
6
|
|
|
7
|
|
import junit.extensions.abbot.*;
|
8
|
|
import abbot.tester.*;
|
9
|
|
import abbot.finder.*;
|
10
|
|
import abbot.finder.matchers.*;
|
11
|
|
|
12
|
|
public class ArrayEditorTest extends ComponentTestFixture {
|
13
|
|
|
14
|
|
private class EditorMatcher extends ClassMatcher {
|
15
|
|
private int currentRow = 0;
|
16
|
|
private int row;
|
17
|
17
|
public EditorMatcher(int row) {
|
18
|
17
|
super(JTextField.class);
|
19
|
17
|
this.row = row;
|
20
|
|
}
|
21
|
111
|
public boolean matches(Component c) {
|
22
|
111
|
return super.matches(c) && currentRow++ == row;
|
23
|
|
}
|
24
|
|
}
|
25
|
|
|
26
|
|
private class ButtonMatcher extends ClassMatcher {
|
27
|
|
private int currentRow = 0;
|
28
|
|
private int row;
|
29
|
|
private String text;
|
30
|
9
|
public ButtonMatcher(int row, String text) {
|
31
|
9
|
super(JButton.class);
|
32
|
9
|
this.text = text;
|
33
|
9
|
this.row = row;
|
34
|
|
}
|
35
|
69
|
public boolean matches(Component c) {
|
36
|
69
|
return super.matches(c) && ((JButton)c).getText().equals(text)
|
37
|
|
&& currentRow++ == row;
|
38
|
|
}
|
39
|
|
}
|
40
|
|
|
41
|
17
|
private JTextField getEditorAt(ArrayEditor ed, int row) {
|
42
|
17
|
try {
|
43
|
17
|
return (JTextField)getFinder().find(ed, new EditorMatcher(row));
|
44
|
|
}
|
45
|
|
catch(ComponentSearchException e) {
|
46
|
0
|
return null;
|
47
|
|
}
|
48
|
|
}
|
49
|
|
|
50
|
9
|
private JButton getButtonAt(ArrayEditor ed, int row, String text) {
|
51
|
9
|
try {
|
52
|
9
|
return (JButton)getFinder().find(ed, new ButtonMatcher(row, text));
|
53
|
|
}
|
54
|
|
catch(ComponentSearchException e) {
|
55
|
0
|
return null;
|
56
|
|
}
|
57
|
|
}
|
58
|
|
|
59
|
1
|
public void testCreateEmptyList() {
|
60
|
1
|
ArrayEditor ed = new ArrayEditor();
|
61
|
1
|
assertEquals("Wrong row count", 0, ed.getRowCount());
|
62
|
1
|
assertEquals("Should have some contents", 1, ed.getComponentCount());
|
63
|
|
}
|
64
|
|
|
65
|
1
|
public void testPopulated() {
|
66
|
1
|
String[] values = { "one", "two", "three", "four" };
|
67
|
1
|
ArrayEditor ed = new ArrayEditor(values);
|
68
|
1
|
assertEquals("Wrong row count", values.length, ed.getRowCount());
|
69
|
1
|
for (int i=0;i < values.length;i++) {
|
70
|
4
|
assertEquals("Wrong value", values[i], ed.getValueAt(i));
|
71
|
|
}
|
72
|
|
}
|
73
|
|
|
74
|
1
|
public void testInsertRow() {
|
75
|
1
|
ArrayEditor ed = new ArrayEditor();
|
76
|
1
|
assertTrue("text should be disabled when empty",
|
77
|
|
!getEditorAt(ed, 0).isEnabled());
|
78
|
1
|
assertTrue("remove should be disabled when empty",
|
79
|
|
!getButtonAt(ed, 0, "-").isEnabled());
|
80
|
1
|
assertTrue("add should be disabled when empty",
|
81
|
|
getButtonAt(ed, 0, "+").isEnabled());
|
82
|
|
|
83
|
1
|
ed.insertRow(0);
|
84
|
1
|
assertEquals("Wrong row count", 1, ed.getRowCount());
|
85
|
1
|
assertEquals("Wrong component count", 1, ed.getComponentCount());
|
86
|
1
|
assertEquals("Expect empty initial value", "", ed.getValueAt(0));
|
87
|
1
|
assertTrue("text should be enabled when non-empty",
|
88
|
|
getEditorAt(ed, 0).isEnabled());
|
89
|
1
|
assertTrue("remove should be enabled when non-empty",
|
90
|
|
getButtonAt(ed, 0, "-").isEnabled());
|
91
|
1
|
assertTrue("add should be enabled when non-empty",
|
92
|
|
getButtonAt(ed, 0, "+").isEnabled());
|
93
|
|
|
94
|
1
|
assertEquals("Wrong value", "", ed.getValueAt(0));
|
95
|
1
|
assertEquals("Value mismatch at 0", "",
|
96
|
|
getEditorAt(ed, 0).getText());
|
97
|
|
|
98
|
|
|
99
|
1
|
ed.insertRow(ed.getRowCount(), "bubba");
|
100
|
1
|
assertEquals("Wrong row count", 2, ed.getRowCount());
|
101
|
1
|
assertEquals("Wrong value", "bubba", ed.getValueAt(1));
|
102
|
1
|
assertEquals("Value mismatch at 1", "bubba",
|
103
|
|
getEditorAt(ed, 1).getText());
|
104
|
|
|
105
|
1
|
ed.insertRow(ed.getRowCount(), "shrimp");
|
106
|
1
|
assertEquals("Wrong row count", 3, ed.getRowCount());
|
107
|
1
|
assertEquals("Wrong value", "shrimp", ed.getValueAt(2));
|
108
|
1
|
assertEquals("Value mismatch at 2", "shrimp",
|
109
|
|
getEditorAt(ed, 2).getText());
|
110
|
|
|
111
|
|
|
112
|
1
|
ed.insertRow(ed.getRowCount()-1, "gump");
|
113
|
1
|
assertEquals("Wrong row count", 4, ed.getRowCount());
|
114
|
1
|
assertEquals("Wrong value", "gump", ed.getValueAt(2));
|
115
|
1
|
assertEquals("Value mismatch at 2", "gump",
|
116
|
|
getEditorAt(ed, 2).getText());
|
117
|
1
|
assertEquals("Wrong value", "shrimp", ed.getValueAt(3));
|
118
|
1
|
assertEquals("Value mismatch at 3", "shrimp",
|
119
|
|
getEditorAt(ed, 3).getText());
|
120
|
|
|
121
|
1
|
String value = "salvation";
|
122
|
1
|
ed.insertRow(-1, value);
|
123
|
1
|
assertEquals("Insert with negative adds to end",
|
124
|
|
value, ed.getValueAt(ed.getRowCount()-1));
|
125
|
|
|
126
|
|
|
127
|
1
|
showFrame(ed);
|
128
|
1
|
int count = ed.getRowCount();
|
129
|
1
|
tester.actionClick(getButtonAt(ed, 1, "+"));
|
130
|
1
|
assertEquals("Row not inserted", count+1, ed.getRowCount());
|
131
|
1
|
assertEquals("Wrong insertion point", "", ed.getValueAt(2));
|
132
|
|
}
|
133
|
|
|
134
|
1
|
public void testRemoveRow() {
|
135
|
1
|
String[] values = { "one", "two", "three", "four" };
|
136
|
1
|
ArrayEditor ed = new ArrayEditor(values);
|
137
|
1
|
assertEquals("Wrong row count", values.length, ed.getRowCount());
|
138
|
|
|
139
|
|
|
140
|
1
|
ed.removeRow(1);
|
141
|
1
|
assertEquals("Row not removed", values.length-1, ed.getRowCount());
|
142
|
1
|
assertEquals("Wrong values remaining", values[0], ed.getValueAt(0));
|
143
|
1
|
assertEquals("Value mismatch at 0", values[0],
|
144
|
|
getEditorAt(ed, 0).getText());
|
145
|
1
|
assertEquals("Wrong values remaining", values[2], ed.getValueAt(1));
|
146
|
1
|
assertEquals("Value mismatch at 1", values[2],
|
147
|
|
getEditorAt(ed, 1).getText());
|
148
|
|
|
149
|
|
|
150
|
1
|
ed.removeRow(ed.getRowCount()-1);
|
151
|
1
|
assertEquals("Wrong row count" , 2, ed.getRowCount());
|
152
|
1
|
assertEquals("Wrong value", "three", ed.getValueAt(1));
|
153
|
1
|
assertEquals("Value mismatch at 1", "three",
|
154
|
|
getEditorAt(ed, 1).getText());
|
155
|
1
|
ed.removeRow(ed.getRowCount()-1);
|
156
|
1
|
assertEquals("Wrong row count", 1, ed.getRowCount());
|
157
|
1
|
assertEquals("Wrong value", "one", ed.getValueAt(0));
|
158
|
1
|
assertEquals("Value mismatch at 0", "one",
|
159
|
|
getEditorAt(ed, 0).getText());
|
160
|
1
|
ed.removeRow(0);
|
161
|
1
|
assertEquals("Should be empty", 0, ed.getRowCount());
|
162
|
|
|
163
|
1
|
assertTrue("text should be disabled when empty",
|
164
|
|
!getEditorAt(ed, 0).isEnabled());
|
165
|
1
|
assertTrue("remove should be disabled when empty",
|
166
|
|
!getButtonAt(ed, 0, "-").isEnabled());
|
167
|
1
|
assertTrue("add should be disabled when empty",
|
168
|
|
getButtonAt(ed, 0, "+").isEnabled());
|
169
|
|
|
170
|
|
|
171
|
1
|
ed.setValues(values);
|
172
|
1
|
showFrame(ed);
|
173
|
1
|
int count = ed.getRowCount();
|
174
|
1
|
tester.actionClick(getButtonAt(ed, count-1, "-"));
|
175
|
1
|
assertEquals("Row not removed", count-1, ed.getRowCount());
|
176
|
1
|
tester.actionClick(getButtonAt(ed, 0, "-"));
|
177
|
1
|
assertEquals("Row not removed", count-2, ed.getRowCount());
|
178
|
1
|
assertEquals("Wrong row removed", values[1], ed.getValueAt(0));
|
179
|
|
}
|
180
|
|
|
181
|
1
|
public void testChangeRow() {
|
182
|
1
|
String[] values = { "one", "two", "three" };
|
183
|
1
|
ArrayEditor ed = new ArrayEditor(values);
|
184
|
1
|
showFrame(ed);
|
185
|
1
|
tester.actionSetCaretPosition(getEditorAt(ed, 0), 0);
|
186
|
1
|
tester.actionKeyStroke(getEditorAt(ed, 0), KeyEvent.VK_A);
|
187
|
1
|
assertEquals("Wrong text", "aone", ed.getValueAt(0));
|
188
|
1
|
tester.actionKeyStroke(getEditorAt(ed, 0), KeyEvent.VK_B);
|
189
|
1
|
assertEquals("Wrong text", "abone", ed.getValueAt(0));
|
190
|
1
|
tester.actionSetCaretPosition(getEditorAt(ed, 1), 0);
|
191
|
1
|
tester.actionKeyStroke(getEditorAt(ed, 1), KeyEvent.VK_A);
|
192
|
1
|
assertEquals("Wrong text", "atwo", ed.getValueAt(1));
|
193
|
|
}
|
194
|
|
|
195
|
|
private class Listener implements ActionListener {
|
196
|
|
public boolean triggered;
|
197
|
|
public String event;
|
198
|
|
public int index;
|
199
|
6
|
public void actionPerformed(ActionEvent e) {
|
200
|
6
|
event = e.getActionCommand();
|
201
|
6
|
index = e.getID();
|
202
|
6
|
triggered = true;
|
203
|
|
}
|
204
|
|
}
|
205
|
1
|
public void testListeners() {
|
206
|
1
|
ArrayEditor ed = new ArrayEditor();
|
207
|
1
|
Listener l = new Listener();
|
208
|
1
|
ed.addActionListener(l);
|
209
|
|
|
210
|
1
|
l.triggered = false;
|
211
|
1
|
ed.insertRow(0, "new");
|
212
|
1
|
assertTrue("No event triggered", l.triggered);
|
213
|
1
|
assertEquals("Wrong event type",
|
214
|
|
ArrayEditor.ACTION_ITEM_INSERTED, l.event);
|
215
|
1
|
assertEquals("Wrong index", 0, l.index);
|
216
|
|
|
217
|
1
|
l.triggered = false;
|
218
|
1
|
ed.setValueAt(0, "old");
|
219
|
1
|
assertTrue("No event triggered", l.triggered);
|
220
|
1
|
assertEquals("Wrong event type",
|
221
|
|
ArrayEditor.ACTION_ITEM_CHANGED, l.event);
|
222
|
1
|
assertEquals("Wrong index", 0, l.index);
|
223
|
|
|
224
|
1
|
l.triggered = false;
|
225
|
1
|
ed.setValues(new String[] { "old", "new" });
|
226
|
1
|
assertTrue("No event triggered", l.triggered);
|
227
|
1
|
assertEquals("Wrong event type",
|
228
|
|
ArrayEditor.ACTION_LIST_CHANGED, l.event);
|
229
|
1
|
assertEquals("Wrong index", -1, l.index);
|
230
|
|
|
231
|
1
|
l.triggered = false;
|
232
|
1
|
ed.removeRow(0);
|
233
|
1
|
assertTrue("No event triggered", l.triggered);
|
234
|
1
|
assertEquals("Wrong event type",
|
235
|
|
ArrayEditor.ACTION_ITEM_DELETED, l.event);
|
236
|
1
|
assertEquals("Wrong index", 0, l.index);
|
237
|
|
}
|
238
|
|
|
239
|
1
|
public void testRemoveBadIndex() {
|
240
|
1
|
ArrayEditor ed = new ArrayEditor();
|
241
|
1
|
try {
|
242
|
1
|
ed.removeRow(10);
|
243
|
0
|
fail("Should throw an exception when arg out of range");
|
244
|
|
}
|
245
|
|
catch(IllegalArgumentException e) {
|
246
|
|
}
|
247
|
|
}
|
248
|
|
|
249
|
|
private JTextComponentTester tester;
|
250
|
7
|
protected void setUp() {
|
251
|
7
|
tester = new JTextComponentTester();
|
252
|
|
}
|
253
|
|
|
254
|
|
|
255
|
7
|
public ArrayEditorTest(String name) { super(name); }
|
256
|
|
|
257
|
|
|
258
|
0
|
public static void main(String[] args) {
|
259
|
0
|
TestHelper.runTests(args, ArrayEditorTest.class);
|
260
|
|
}
|
261
|
|
}
|
262
|
|
|