001: package abbot.tester;
002:
003: import abbot.WaitTimedOutError;
004:
005: import java.awt.event.*;
006:
007: import javax.swing.*;
008:
009: import junit.extensions.abbot.*;
010: import junit.extensions.abbot.Timer;
011: import abbot.util.ExtendedComparator;
012:
013: import java.awt.Component;
014:
015: import javax.swing.tree.DefaultMutableTreeNode;
016: import javax.swing.tree.TreeCellRenderer;
017: import javax.swing.tree.TreePath;
018:
019: /** Unit test to verify the JListTester class.<p> */
020:
021: public class JListTesterTest extends ComponentTestFixture {
022:
023: private JListTester tester;
024: private JList list;
025: private JScrollPane scrollPane;
026:
027: private String[] data = { "zero", "one", "two", "three", "four",
028: "five", "six", "seven", "eight" };
029:
030: protected void setUp() {
031: tester = new JListTester();
032: list = new JList(data);
033: }
034:
035: public void testContents() {
036: showFrame(list);
037: assertTrue("Wrong contents returned", ExtendedComparator
038: .equals(data, tester.getContents(list)));
039: }
040:
041: private class MouseWatcher extends MouseAdapter {
042: public volatile int clickCount = 0;
043:
044: public void mouseClicked(MouseEvent ev) {
045: clickCount = ev.getClickCount();
046: }
047: }
048:
049: public void testSelectFirstValue() {
050: list.setSelectedIndex(1);
051: MouseWatcher mw = new MouseWatcher();
052: list.addMouseListener(mw);
053: showFrame(list);
054: tester.actionSelectRow(list, new JListLocation(data[0]));
055: assertEquals("Wrong item selected", 0, list.getSelectedIndex());
056: Timer timer = new Timer();
057: while (mw.clickCount == 0) {
058: if (timer.elapsed() > EVENT_GENERATION_DELAY)
059: throw new RuntimeException(
060: "Timed out waiting for clicks");
061: tester.sleep();
062: }
063: assertEquals("Too many clicks", 1, mw.clickCount);
064: }
065:
066: public void testSelectFirstIndex() {
067: list.setSelectedIndex(1);
068: MouseWatcher mw = new MouseWatcher();
069: list.addMouseListener(mw);
070: showFrame(list);
071: tester.actionSelectIndex(list, 0);
072: assertEquals("Wrong item selected", 0, list.getSelectedIndex());
073: Timer timer = new Timer();
074: while (mw.clickCount == 0) {
075: if (timer.elapsed() > EVENT_GENERATION_DELAY)
076: throw new RuntimeException(
077: "Timed out waiting for clicks");
078: tester.sleep();
079: }
080: assertEquals("Too many clicks", 1, mw.clickCount);
081: }
082:
083: /**
084: * Uses a JList in a JScrollPane to test the scrollToVisible feature of
085: * JComponentTester.
086: */
087: public void testScrollToVisibleIndex() {
088: list.setVisibleRowCount(4);
089: scrollPane = new JScrollPane(list);
090: showFrame(scrollPane);
091: tester.actionSelectIndex(list, 0);
092: assertEquals("Wrong Cell Selected", list.getSelectedIndex(), 0);
093: tester.actionSelectIndex(list, 7);
094: assertEquals("Wrong Cell Selected", list.getSelectedIndex(), 7);
095: tester.actionSelectIndex(list, 6);
096: assertEquals("Wrong Cell Selected", list.getSelectedIndex(), 6);
097: tester.actionSelectIndex(list, 2);
098: assertEquals("Wrong Cell Selected", list.getSelectedIndex(), 2);
099: tester.actionSelectIndex(list, 8);
100: assertEquals("Wrong Cell Selected", list.getSelectedIndex(), 8);
101: tester.actionSelectIndex(list, 5);
102: assertEquals("Wrong Cell Selected", list.getSelectedIndex(), 5);
103: tester.actionSelectIndex(list, 1);
104: assertEquals("Wrong Cell Selected", list.getSelectedIndex(), 1);
105: try {
106: tester.actionSelectIndex(list, -1);
107: fail("Action with out-of-bounds index should fail");
108: } catch (ActionFailedException a) {
109: }
110: try {
111: tester.actionSelectIndex(list, data.length, 1000); // Use shorted delay to speed test run
112: fail("Action with out-of-bounds index should fail");
113: } catch (ActionFailedException a) {
114: } catch (WaitTimedOutError w) {
115: }
116: }
117:
118: /**
119: * Uses a JList in a JScrollPane to test the scrollToVisible feature of
120: * JComponentTester.
121: */
122: public void testLazyLoad() {
123: list.setVisibleRowCount(4);
124: scrollPane = new JScrollPane(list);
125: showFrame(scrollPane);
126: final DefaultListModel dlm = new DefaultListModel();
127: list.setModel(dlm);
128:
129: // Slight later on add some entries
130: //
131:
132: new Thread(new Runnable() {
133: public void run() {
134: try {
135: Thread.sleep(2000);
136: dlm.addElement("Zero");
137: Thread.sleep(1000);
138: dlm.addElement("One");
139: } catch (Exception e) {
140:
141: }
142:
143: }
144: }).start();
145:
146: tester.actionSelectIndex(list, 0);
147: assertEquals("Cell Not Selected", list.getSelectedIndex(), 0);
148: tester.actionSelectIndex(list, 1);
149: assertEquals("Cell Not Selected", list.getSelectedIndex(), 1);
150: }
151:
152: public void testGetTextRenderer() {
153:
154: class OtherRenderer extends JTextField implements
155: ListCellRenderer {
156:
157: public Component getListCellRendererComponent(JList list,
158: Object value, int index, boolean isSelected,
159: boolean cellHasFocus) {
160:
161: setText(value.toString());
162: return this ;
163: }
164: }
165:
166: //
167:
168: list.setCellRenderer(new OtherRenderer());
169:
170: assertEquals(
171: "Must return the correct child text for non JLabel renderers",
172: JListTester.valueToString(list, 0), "zero");
173: assertEquals(
174: "Must return the correct child text for non JLabel renderers",
175: JListTester.valueToString(list, 1), "one");
176:
177: }
178:
179: public static void main(String[] args) {
180: RepeatHelper.runTests(args, JListTesterTest.class);
181: }
182: }
|