001: package abbot.tester;
002:
003: import java.awt.*;
004: import javax.swing.*;
005: import javax.swing.text.JTextComponent;
006:
007: import junit.extensions.abbot.*;
008:
009: /** Unit test to verify the JTextComponentTester class.<p> */
010:
011: public class JTextComponentTesterTest extends ComponentTestFixture {
012:
013: private JTextComponentTester tester;
014: private JTextComponent tc;
015:
016: protected void setUp() {
017: JTextField tf = new JTextField();
018: tf.setColumns(10);
019: tc = tf;
020: tester = new JTextComponentTester();
021: }
022:
023: public void testActionEnterText() {
024: tc.setText("Some initial text to slow things down");
025: showFrame(tc);
026: String text = "short";
027: tester.actionEnterText(tc, text);
028: assertEquals("Wrong short text typed,", text, tc.getText());
029: text = "longer";
030: tester.actionEnterText(tc, text);
031: assertEquals("Wrong replacement text,", text, tc.getText());
032: text = "Some longer text that will surely exceed the field width";
033: tester.actionEnterText(tc, text);
034: assertEquals("Wrong long replacement text,", text, tc.getText());
035: text = "shorter";
036: tester.actionEnterText(tc, text);
037: assertEquals("Wrong shorter replacement text,", text, tc
038: .getText());
039:
040: }
041:
042: public void testActionEnterTextWithEmptyString() {
043: tc.setText("Some initial text to slow things down");
044: showFrame(tc);
045: String EXPECTED = "";
046: tester.actionEnterText(tc, EXPECTED);
047: assertEquals("Text should be cleared", EXPECTED, tc.getText());
048: }
049:
050: public void testClick() {
051: showFrame(tc);
052: String text = "Some somewhat long text which exceeds the component size";
053: tc.setText(text);
054: tester.waitForIdle();
055: tester.actionClick(tc, 0);
056: assertEquals("Wrong location", 0, tc.getCaretPosition());
057: tester.actionClick(tc, text.length() / 2);
058: assertEquals("Wrong location", text.length() / 2, tc
059: .getCaretPosition());
060: tester.actionClick(tc, text.length());
061: assertEquals("Wrong location", text.length(), tc
062: .getCaretPosition());
063: }
064:
065: public void testSelectVisibleText() {
066: showFrame(tc);
067: final String text = "short";
068: tester.invokeAndWait(new Runnable() {
069: public void run() {
070: tc.setText(text);
071: }
072: });
073: tester.actionSelectText(tc, text.length() - 1, 1);
074: assertEquals("Wrong selection start", 1, tc.getSelectionStart());
075: assertEquals("Wrong selection end", text.length() - 1, tc
076: .getSelectionEnd());
077:
078: tester.actionSelectText(tc, text.length(), 0);
079: assertEquals("Wrong selection start", 0, tc.getSelectionStart());
080: assertEquals("Wrong selection end", text.length(), tc
081: .getSelectionEnd());
082:
083: tester.actionSelectText(tc, 0, text.length());
084: assertEquals("Wrong selection start", 0, tc.getSelectionStart());
085: assertEquals("Wrong selection end", text.length(), tc
086: .getSelectionEnd());
087:
088: tester.actionSelectText(tc, 1, text.length() - 1);
089: assertEquals("Wrong selection start", 1, tc.getSelectionStart());
090: assertEquals("Wrong selection end", text.length() - 1, tc
091: .getSelectionEnd());
092: }
093:
094: public void testSelectObscuredText() {
095: showFrame(tc);
096: // Try a long selection which exceeds the visible area
097: String text = "The quick brown fox jumped over the lazy dog";
098: tc.setText(text);
099: tester.waitForIdle();
100: tester.actionSelectText(tc, text.length(), 0);
101: assertEquals("Wrong selection start", 0, tc.getSelectionStart());
102: assertEquals("Wrong selection end (hidden)", text.length(), tc
103: .getSelectionEnd());
104:
105: tester.actionSelectText(tc, 1, text.length() - 1);
106: assertEquals("Wrong selection start (hidden)", 1, tc
107: .getSelectionStart());
108: assertEquals("Wrong selection end", text.length() - 1, tc
109: .getSelectionEnd());
110:
111: tester.actionSelectText(tc, 0, text.length());
112: assertEquals("Wrong selection start", 0, tc.getSelectionStart());
113: assertEquals("Wrong selection end (hidden)", text.length(), tc
114: .getSelectionEnd());
115:
116: tester.actionSelectText(tc, text.length() - 1, 1);
117: assertEquals("Wrong selection start (hidden)", 1, tc
118: .getSelectionStart());
119: assertEquals("Wrong selection end", text.length() - 1, tc
120: .getSelectionEnd());
121: }
122:
123: public void testScrollToVisible() {
124: JPanel p = new JPanel();
125: p.setPreferredSize(new Dimension(200, 400));
126: JPanel scrolled = new JPanel(new BorderLayout());
127: scrolled.add(p);
128: scrolled.add(tc, BorderLayout.SOUTH);
129: showFrame(new JScrollPane(scrolled), new Dimension(200, 200));
130:
131: Rectangle visible = tc.getVisibleRect();
132: Rectangle empty = new Rectangle(0, 0, 0, 0);
133: assertTrue("Text should not be visible", visible == null
134: || visible.equals(empty));
135:
136: tester.actionScrollToVisible(tc, new ComponentLocation());
137: visible = tc.getVisibleRect();
138: assertFalse("Text should be visible", visible == null
139: || visible.equals(empty));
140: }
141:
142: public static void main(String[] args) {
143: RepeatHelper.runTests(args, JTextComponentTesterTest.class);
144: }
145: }
|