001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.utils.AssertionFailureNotDetectedError;
005: import org.uispec4j.utils.DummyActionListener;
006: import org.uispec4j.utils.Functor;
007: import org.uispec4j.utils.UIComponentFactory;
008: import org.uispec4j.xml.XmlAssert;
009:
010: import javax.swing.*;
011: import javax.swing.text.*;
012:
013: public class TextBoxForRawTextComponentTest extends
014: TextBoxComponentTestCase {
015: private JTextComponent jTextComponent;
016:
017: protected void setUp() throws Exception {
018: super .setUp();
019: init(new JTextArea());
020: }
021:
022: private void init(JTextComponent swingComponent) {
023: jTextComponent = swingComponent;
024: jTextComponent.setName("myText");
025: createTextBox("");
026: }
027:
028: protected void createTextBox(String text) {
029: textBox = new TextBox(jTextComponent);
030: textBox.setText(text);
031: }
032:
033: public void testGetComponentTypeName() throws Exception {
034: assertEquals("textBox", UIComponentFactory.createUIComponent(
035: new JTextField()).getDescriptionTypeName());
036: }
037:
038: public void testGetDescription() throws Exception {
039: XmlAssert.assertEquivalent("<textBox name='myText'/>", textBox
040: .getDescription());
041: }
042:
043: public void testFactory() throws Exception {
044: checkFactory(new JTextArea(), TextBox.class);
045: checkFactory(new JTextPane(), TextBox.class);
046: checkFactory(new JEditorPane(), TextBox.class);
047: checkFactory(new JTextField(), TextBox.class);
048: }
049:
050: public void testAssertTextEquals() throws Exception {
051: assertTrue(textBox.textEquals(""));
052: jTextComponent.setText("some text");
053: assertTrue(textBox.textEquals("some text"));
054: assertFalse(textBox.textEquals(""));
055: try {
056: assertTrue(textBox.textEquals("error"));
057: throw new AssertionFailureNotDetectedError();
058: } catch (AssertionFailedError e) {
059: assertEquals("expected:<error> but was:<some text>", e
060: .getMessage());
061: }
062: }
063:
064: public void testAssertTextContains() throws Exception {
065: jTextComponent.setText("some text");
066: assertTrue(textBox.textContains("some"));
067: try {
068: assertTrue(textBox.textContains("error"));
069: throw new AssertionFailureNotDetectedError();
070: } catch (AssertionFailedError e) {
071: assertEquals(
072: "The component text does not contain 'error' - actual content is:some text",
073: e.getMessage());
074: }
075: }
076:
077: public void testAssertTextDoesNotContain() throws Exception {
078: jTextComponent.setText("some text");
079: assertTrue(textBox.textDoesNotContain("xxx"));
080: try {
081: assertTrue(textBox.textDoesNotContain("some"));
082: throw new AssertionFailureNotDetectedError();
083: } catch (AssertionFailedError e) {
084: assertEquals(
085: "The component text should not contain 'some' - actual content is:some text",
086: e.getMessage());
087: }
088: }
089:
090: public void testAssertTextIsEditable() throws Exception {
091: jTextComponent.setEditable(true);
092: assertTrue(textBox.isEditable());
093: jTextComponent.setEditable(false);
094: assertFalse(textBox.isEditable());
095: }
096:
097: public void testAssertEmptyWithPlainText() throws Exception {
098: jTextComponent.setText("");
099: assertTrue(textBox.textIsEmpty());
100: jTextComponent.setText("a");
101: try {
102: assertTrue(textBox.textIsEmpty());
103: throw new AssertionFailureNotDetectedError();
104: } catch (AssertionFailedError e) {
105: assertEquals("Text should be empty but contains: a", e
106: .getMessage());
107: }
108: }
109:
110: public void testSetText() throws Exception {
111: textBox.setText("new text");
112: assertEquals("new text", jTextComponent.getText());
113: }
114:
115: public void testSetTextChecksThatTheComponentIsEditable()
116: throws Exception {
117: textBox.setText("text");
118: jTextComponent.setEditable(false);
119: try {
120: textBox.setText("new text");
121: throw new AssertionFailureNotDetectedError();
122: } catch (AssertionFailedError e) {
123: assertEquals("The text box is not editable", e.getMessage());
124: }
125: assertEquals("text", jTextComponent.getText());
126: }
127:
128: public void testInsertText() throws Exception {
129: jTextComponent.setEditable(true);
130: textBox.insertText("text", 0);
131: assertEquals("text", textBox.getText());
132: textBox.insertText("this is some ", 0);
133: assertEquals("this is some text", textBox.getText());
134: textBox.insertText("interesting ", 13);
135: assertEquals("this is some interesting text", textBox.getText());
136: textBox.insertText(", isn't it?", textBox.getText().length());
137: assertEquals("this is some interesting text, isn't it?",
138: textBox.getText());
139: }
140:
141: public void testInsertTextAtABadPosition() throws Exception {
142: textBox.setText("text");
143: try {
144: textBox.insertText("a", 10);
145: throw new AssertionFailureNotDetectedError();
146: } catch (AssertionFailedError e) {
147: assertEquals("Position should be between 0 and 4", e
148: .getMessage());
149: }
150: }
151:
152: public void testInsertTextDoesNotNotifyActionListeners()
153: throws Exception {
154: JTextField textField = new JTextField();
155: DummyActionListener actionListener = new DummyActionListener();
156: textField.addActionListener(actionListener);
157: textBox = new TextBox(textField);
158:
159: textBox.insertText("text", 0);
160: assertEquals(0, actionListener.getCallCount());
161: }
162:
163: public void testInsertTextChecksThatTheComponentIsEditable()
164: throws Exception {
165: textBox.setText("text");
166: jTextComponent.setEditable(false);
167: try {
168: textBox.insertText("new text", 0);
169: throw new AssertionFailureNotDetectedError();
170: } catch (AssertionFailedError e) {
171: assertEquals("The text box is not editable", e.getMessage());
172: }
173: assertEquals("text", jTextComponent.getText());
174: }
175:
176: public void testGetText() throws Exception {
177: jTextComponent.setText("some text");
178: assertEquals("some text", textBox.getText());
179: textBox.setText("new text");
180: assertEquals("new text", textBox.getText());
181: textBox.setText("new <b>text</b>");
182: assertEquals("new <b>text</b>", textBox.getText());
183: }
184:
185: public void testSetTextNotifiesActionListenersForJTextField()
186: throws Exception {
187: JTextField textField = new JTextField();
188: DummyActionListener actionListener = new DummyActionListener();
189: textField.addActionListener(actionListener);
190: textBox = new TextBox(textField);
191:
192: textBox.setText("text");
193: assertEquals(1, actionListener.getCallCount());
194: }
195:
196: public void testClickOnHyperlinkIsNotSupported() throws Exception {
197: checkAssertionFailedError(new Functor() {
198: public void run() throws Exception {
199: textBox.clickOnHyperlink("text");
200: }
201: }, "This component does not support hyperlinks.");
202: checkAssertionFailedError(new Functor() {
203: public void run() throws Exception {
204: textBox.triggerClickOnHyperlink("text").run();
205: }
206: }, "This component does not support hyperlinks.");
207: }
208:
209: public void testPressingPrintableKeyAddsItToText() throws Exception {
210: JTextField textField = new JTextField();
211: TextBox textBox = new TextBox(textField);
212: textBox.pressKey(Key.A);
213: assertTrue(textBox.textEquals("A"));
214: textBox.pressKey(Key.B);
215: assertTrue(textBox.textEquals("AB"));
216: textBox.pressKey(Key.C);
217: assertTrue(textBox.textEquals("ABC"));
218: }
219:
220: public void testPressingPrintableKeyRejectedByTextField()
221: throws Exception {
222: JTextField textField = new JTextField();
223: ((AbstractDocument) textField.getDocument())
224: .setDocumentFilter(new DocumentFilter() {
225: public void insertString(FilterBypass bypass,
226: int i, String string, AttributeSet set)
227: throws BadLocationException {
228: if (!string.equals("A")) {
229: super .insertString(bypass, i, string, set);
230: }
231: }
232: });
233: TextBox textBox = new TextBox(textField);
234: textBox.pressKey(Key.A);
235: assertTrue(textBox.textEquals(""));
236: textBox.pressKey(Key.B);
237: assertTrue(textBox.textEquals("B"));
238: textBox.pressKey(Key.A);
239: assertTrue(textBox.textEquals("B"));
240: textBox.pressKey(Key.C);
241: assertTrue(textBox.textEquals("BC"));
242: }
243:
244: public void testPressingPrintableKeyInANonEmptyTextBoxStartsAtPosition0()
245: throws Exception {
246: JTextField textField = new JTextField("text");
247: TextBox textBox = new TextBox(textField);
248: textBox.pressKey(Key.A);
249: assertTrue(textBox.textEquals("Atext"));
250: }
251: }
|