01: package example;
02:
03: import java.awt.*;
04: import javax.swing.*;
05:
06: import junit.extensions.abbot.ComponentTestFixture;
07: import junit.extensions.abbot.TestHelper;
08:
09: import abbot.finder.Matcher;
10: import abbot.finder.matchers.ClassMatcher;
11: import abbot.tester.JListTester;
12: import abbot.tester.JListLocation;
13:
14: /** Source test code for Tutorial 2, test fixture for the LabeledList. */
15:
16: public class LabeledListTest extends ComponentTestFixture {
17:
18: public void testLabelChangedOnSelectionChange() throws Throwable {
19: String[] contents = { "one", "two", "three" };
20: final LabeledList labeledList = new LabeledList(contents);
21: showFrame(labeledList);
22:
23: // The interface abbot.finder.Matcher allows you to define whatever
24: // matching specification you'd like. We know there's only one
25: // JList in the hierarchy we're searching, so we can look up by
26: // class with an instance of ClassMatcher.
27: Component list = getFinder()
28: .find(new ClassMatcher(JList.class));
29: JListTester tester = new JListTester();
30:
31: // We could also use an instance of ClassMatcher, but this shows
32: // how you can put more conditions into the Matcher.
33: JLabel label = (JLabel) getFinder().find(labeledList,
34: new Matcher() {
35: public boolean matches(Component c) {
36: return c.getClass().equals(JLabel.class)
37: && c.getParent() == labeledList;
38: }
39: });
40:
41: // Select by row index or by value
42: tester.actionSelectRow(list, new JListLocation(1));
43: // tester.actionSelect(list, new JListLocation("two"));
44: assertEquals("Wrong label after selection", "Selected: two",
45: label.getText());
46:
47: tester.actionSelectRow(list, new JListLocation(2));
48: assertEquals("Wrong label after selection", "Selected: three",
49: label.getText());
50:
51: tester.actionSelectRow(list, new JListLocation(0));
52: assertEquals("Wrong label after selection", "Selected: one",
53: label.getText());
54: }
55:
56: public LabeledListTest(String name) {
57: super (name);
58: }
59:
60: public static void main(String[] args) {
61: TestHelper.runTests(args, LabeledListTest.class);
62: }
63: }
|