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.event.HyperlinkEvent;
012: import javax.swing.event.HyperlinkListener;
013: import javax.swing.text.JTextComponent;
014:
015: public class TextBoxForTextComponentTest extends
016: TextBoxComponentTestCase {
017: private JTextComponent jTextComponent;
018:
019: protected void setUp() throws Exception {
020: super .setUp();
021: init(new JTextArea());
022: }
023:
024: private void init(JTextComponent swingComponent) {
025: jTextComponent = swingComponent;
026: jTextComponent.setName("myText");
027: createTextBox("");
028: }
029:
030: protected void createTextBox(String text) {
031: textBox = new TextBox(jTextComponent);
032: textBox.setText(text);
033: }
034:
035: private void initWithHtmlTextPane() {
036: JTextPane pane = new JTextPane();
037: pane.setContentType("text/html; charset=EUC-JP");
038: init(pane);
039: }
040:
041: public void testGetComponentTypeName() throws Exception {
042: assertEquals("textBox", UIComponentFactory.createUIComponent(
043: new JTextField()).getDescriptionTypeName());
044: }
045:
046: public void testGetDescription() throws Exception {
047: XmlAssert.assertEquivalent("<textBox name='myText'/>", textBox
048: .getDescription());
049: }
050:
051: public void testFactory() throws Exception {
052: checkFactory(new JTextArea(), TextBox.class);
053: checkFactory(new JTextPane(), TextBox.class);
054: checkFactory(new JEditorPane(), TextBox.class);
055: checkFactory(new JTextField(), TextBox.class);
056: }
057:
058: public void testAssertTextEquals() throws Exception {
059: assertTrue(textBox.textEquals(""));
060: jTextComponent.setText("some text");
061: assertTrue(textBox.textEquals("some text"));
062: assertFalse(textBox.textEquals(""));
063: try {
064: assertTrue(textBox.textEquals("error"));
065: throw new AssertionFailureNotDetectedError();
066: } catch (AssertionFailedError e) {
067: assertEquals("expected:<error> but was:<some text>", e
068: .getMessage());
069: }
070: }
071:
072: public void testAssertTextEqualsWithHtml() throws Exception {
073: initWithHtmlTextPane();
074: String text = "Universal <b>rules</b>:"
075: + "<ul>"
076: + "<li style=\"margin-top: 0\" align=\"center\">a < b</li>"
077: + "<li>2 > 1</li>" + "</ul>";
078: textBox.setText(text);
079: assertTrue(textBox.textEquals("Universal rules: a < b 2 > 1"));
080: try {
081: assertTrue(textBox
082: .textEquals("Universal rules: a < b 2 > 1, seb is the best"));
083: throw new AssertionFailureNotDetectedError();
084: } catch (AssertionFailedError e) {
085: }
086: }
087:
088: public void testAssertHtmlEquals() throws Exception {
089: initWithHtmlTextPane();
090: String text = "Universal <b>rules</b>:" + "<ul>"
091: + "<li>a < b</li>" + "<li>2 > 1</li>" + "</ul>";
092: textBox.setText(text);
093: assertTrue(textBox.htmlEquals(text));
094: try {
095: assertTrue(textBox.htmlEquals("Universal <b>rules</b>:"
096: + "<ul>" + "<li>a < b</li>" + "</ul>"));
097: throw new AssertionFailureNotDetectedError();
098: } catch (AssertionFailedError e) {
099: }
100: }
101:
102: public void testAssertTextContains() throws Exception {
103: jTextComponent.setText("some text");
104: assertTrue(textBox.textContains("some"));
105: try {
106: assertTrue(textBox.textContains("error"));
107: throw new AssertionFailureNotDetectedError();
108: } catch (AssertionFailedError e) {
109: assertEquals(
110: "The component text does not contain 'error' - actual content is:some text",
111: e.getMessage());
112: }
113: }
114:
115: public void testAssertTextContainsWithHtml() throws Exception {
116: initWithHtmlTextPane();
117: String text = "My name is <b>Bond</b>";
118: textBox.setText(text);
119: assertTrue(textBox.textContains("Bond"));
120: try {
121: assertTrue(textBox.textContains("error"));
122: throw new AssertionFailureNotDetectedError();
123: } catch (AssertionFailedError e) {
124: assertEquals(
125: "The component text does not contain 'error' - actual content is:<html>\n"
126: + " <head>\n" + " </head>\n"
127: + " <body>My name is <b>Bond</b></body>\n"
128: + "</html>\n", e.getMessage());
129: }
130: }
131:
132: public void testAssertTextEqualsWithEmptyStringIsTheSameAsAssertTextIsEmpty()
133: throws Exception {
134: initWithHtmlTextPane();
135: assertTrue(textBox.textEquals(""));
136: jTextComponent.setText("blah");
137: jTextComponent.setText("");
138: assertTrue(textBox.textEquals(""));
139: }
140:
141: public void testAssertTextContainsHandlesHtmlLineBreaksAndFormatting()
142: throws Exception {
143: initWithHtmlTextPane();
144: StringBuffer buffer = new StringBuffer();
145: for (int i = 0; i < 20; i++) {
146: buffer.append("blah ");
147: }
148: String text = buffer.toString();
149: textBox.setText(text);
150: assertTrue(textBox.textContains(text));
151: }
152:
153: public void testAssertTextDoesNotContain() throws Exception {
154: jTextComponent.setText("some text");
155: assertTrue(textBox.textDoesNotContain("xxx"));
156: try {
157: assertTrue(textBox.textDoesNotContain("some"));
158: throw new AssertionFailureNotDetectedError();
159: } catch (AssertionFailedError e) {
160: assertEquals(
161: "The component text should not contain 'some' - actual content is:some text",
162: e.getMessage());
163: }
164: }
165:
166: public void testAssertTextIsEditable() throws Exception {
167: jTextComponent.setEditable(true);
168: assertTrue(textBox.isEditable());
169: jTextComponent.setEditable(false);
170: assertFalse(textBox.isEditable());
171: }
172:
173: public void testAssertEmptyWithPlainText() throws Exception {
174: jTextComponent.setText("");
175: assertTrue(textBox.textIsEmpty());
176: jTextComponent.setText("a");
177: try {
178: assertTrue(textBox.textIsEmpty());
179: throw new AssertionFailureNotDetectedError();
180: } catch (AssertionFailedError e) {
181: assertEquals("Text should be empty but contains: a", e
182: .getMessage());
183: }
184: }
185:
186: public void testAssertEmptyWithHtml() throws Exception {
187: initWithHtmlTextPane();
188: assertTrue(textBox.textIsEmpty());
189: jTextComponent.setText("");
190: assertTrue(textBox.textIsEmpty());
191: jTextComponent.setText("a");
192: try {
193: assertTrue(textBox.textIsEmpty());
194: throw new AssertionFailureNotDetectedError();
195: } catch (AssertionFailedError e) {
196: assertEquals("Text should be empty but contains: <html>\n"
197: + " <head>\n" + " \n" + " </head>\n"
198: + " <body>\n" + " a\n" + " </body>\n"
199: + "</html>\n", e.getMessage());
200: }
201:
202: jTextComponent.setText("<html>\n" + " <head>\n" + "\n"
203: + " </head>\n" + " <body>\n" + " <p>\n"
204: + " \n" + " </p>\n" + " </body>\n"
205: + "</html>\n" + "\n" + "");
206: assertTrue(textBox.textIsEmpty());
207:
208: jTextComponent.setText("<html><p></html>");
209: jTextComponent.setText("");
210: assertTrue(textBox.textIsEmpty());
211: }
212:
213: public void testAssertEmptyAfterReset() throws Exception {
214: initWithHtmlTextPane();
215: assertTrue(textBox.textIsEmpty());
216: jTextComponent.setText("blah");
217: jTextComponent.setText("");
218: assertTrue(textBox.textIsEmpty());
219: }
220:
221: public void testSetText() throws Exception {
222: textBox.setText("new text");
223: assertEquals("new text", jTextComponent.getText());
224: }
225:
226: public void testSetTextChecksThatTheComponentIsEditable()
227: throws Exception {
228: textBox.setText("text");
229: jTextComponent.setEditable(false);
230: try {
231: textBox.setText("new text");
232: throw new AssertionFailureNotDetectedError();
233: } catch (AssertionFailedError e) {
234: assertEquals("The text box is not editable", e.getMessage());
235: }
236: assertEquals("text", jTextComponent.getText());
237: }
238:
239: public void testInsertText() throws Exception {
240: jTextComponent.setEditable(true);
241: textBox.insertText("text", 0);
242: assertEquals("text", textBox.getText());
243: textBox.insertText("this is some ", 0);
244: assertEquals("this is some text", textBox.getText());
245: textBox.insertText("interesting ", 13);
246: assertEquals("this is some interesting text", textBox.getText());
247: textBox.insertText(", isn't it?", textBox.getText().length());
248: assertEquals("this is some interesting text, isn't it?",
249: textBox.getText());
250: }
251:
252: public void testInsertTextAtABadPosition() throws Exception {
253: textBox.setText("text");
254: try {
255: textBox.insertText("a", 10);
256: throw new AssertionFailureNotDetectedError();
257: } catch (AssertionFailedError e) {
258: assertEquals("Position should be between 0 and 4", e
259: .getMessage());
260: }
261: }
262:
263: public void testInsertTextDoesNotNotifyActionListeners()
264: throws Exception {
265: JTextField textField = new JTextField();
266: DummyActionListener actionListener = new DummyActionListener();
267: textField.addActionListener(actionListener);
268: textBox = new TextBox(textField);
269:
270: textBox.insertText("text", 0);
271: assertEquals(0, actionListener.getCallCount());
272: }
273:
274: public void testInsertTextChecksThatTheComponentIsEditable()
275: throws Exception {
276: textBox.setText("text");
277: jTextComponent.setEditable(false);
278: try {
279: textBox.insertText("new text", 0);
280: throw new AssertionFailureNotDetectedError();
281: } catch (AssertionFailedError e) {
282: assertEquals("The text box is not editable", e.getMessage());
283: }
284: assertEquals("text", jTextComponent.getText());
285: }
286:
287: public void testGetText() throws Exception {
288: jTextComponent.setText("some text");
289: assertEquals("some text", textBox.getText());
290: textBox.setText("new text");
291: assertEquals("new text", textBox.getText());
292: textBox.setText("new <b>text</b>");
293: assertEquals("new <b>text</b>", textBox.getText());
294: }
295:
296: public void testSetTextNotifiesActionListenersForJTextField()
297: throws Exception {
298: JTextField textField = new JTextField();
299: DummyActionListener actionListener = new DummyActionListener();
300: textField.addActionListener(actionListener);
301: textBox = new TextBox(textField);
302:
303: textBox.setText("text");
304: assertEquals(1, actionListener.getCallCount());
305: }
306:
307: public void testClickOnHyperlink() throws Exception {
308: checkClickOnHyperlink(
309: "<html>blah blah<a href=\"http://www.junit.org\">link text</a>reblah</html>",
310: "link text", "http://www.junit.org");
311: }
312:
313: public void testClickOnHyperlinkAcceptsSubstrings()
314: throws Exception {
315: checkClickOnHyperlink(
316: "<html>blah blah<a href=\"http://www.junit.org\">link text</a>reblah</html>",
317: "link", "http://www.junit.org");
318: }
319:
320: public void testClickOnHyperLinkAcceptsLineSeparators()
321: throws Exception {
322: String link = "link text is very long so it will be on two lines";
323: checkClickOnHyperlink(
324: "<html>blah blah<a href=\"http://www.junit.org\">"
325: + link + "</a>reblah</html>", link,
326: "http://www.junit.org");
327: }
328:
329: public void testClickOnHyperlinkIsCaseInsensitive()
330: throws Exception {
331: checkClickOnHyperlink(
332: "<html>blah blah<a href=\"http://www.junit.org\">link text</a>reblah</html>",
333: "liNk tEXt", "http://www.junit.org");
334: }
335:
336: public void testClickOnHyperlinkGivesPriorityToExactMatches()
337: throws Exception {
338: checkClickOnHyperlink(
339: "<html>blah blah<a href=\"http://www.junit.org\">a link text</a>reblah"
340: + "blah blah<a href=\"http://www.apache.org\">link text</a>reblah</html>",
341: "link text", "http://www.apache.org");
342: }
343:
344: public void testClickOnUnknownHyperlink() throws Exception {
345: checkClickOnHyperlinkError(
346: "<html>blah blah<a href=\"http://www.junit.org\">a link text</a>reblah"
347: + "blah blah<a href=\"http://www.apache.org\">link text</a>reblah</html>",
348: "unknown", "Hyperlink 'unknown' not found");
349: }
350:
351: public void testClickOnHyperlinkWithAmbiguity() throws Exception {
352: checkClickOnHyperlinkError(
353: "<html>blah blah<a href=\"http://www.junit.org\">a link text</a>reblah"
354: + "blah blah<a href=\"http://www.apache.org\">another link text</a>reblah</html>",
355: "link text",
356: "Ambiguous command - found several hyperlinks matching 'link text'");
357: }
358:
359: public void testClickOnHyperLinkWithABadTextComponentFails()
360: throws Exception {
361: final TextBox textBox = new TextBox(new JTextArea());
362:
363: checkAssertionFailedError(new Functor() {
364: public void run() throws Exception {
365: textBox.clickOnHyperlink("toto");
366: }
367: }, "This component does not support hyperlinks.");
368:
369: checkAssertionFailedError(new Functor() {
370: public void run() throws Exception {
371: textBox.triggerClickOnHyperlink("toto").run();
372: }
373: }, "This component does not support hyperlinks.");
374: }
375:
376: public void testPressingPrintableKeyAddsItToText() throws Exception {
377: JTextField textField = new JTextField();
378: TextBox textBox = new TextBox(textField);
379: textBox.pressKey(Key.A);
380: assertTrue(textBox.textEquals("A"));
381: textBox.pressKey(Key.B);
382: assertTrue(textBox.textEquals("AB"));
383: textBox.pressKey(Key.C);
384: assertTrue(textBox.textEquals("ABC"));
385: }
386:
387: public void testPressingPrintableKeyInANonEmptyTextBoxStartsAtPosition0()
388: throws Exception {
389: JTextField textField = new JTextField("text");
390: TextBox textBox = new TextBox(textField);
391: textBox.pressKey(Key.A);
392: assertTrue(textBox.textEquals("Atext"));
393: }
394:
395: private JTextPane createTextPane(String html) {
396: JTextPane textPane = new JTextPane();
397: textPane.setContentType("text/html; charset=EUC-JP");
398: textPane.setText(html);
399: return textPane;
400: }
401:
402: private void checkClickOnHyperlink(String html, String link,
403: String expectedTarget) throws Exception {
404: JTextPane textPane = createTextPane(html);
405: DummyHyperlinkListener listener = new DummyHyperlinkListener();
406: textPane.addHyperlinkListener(listener);
407: TextBox textComponent = new TextBox(textPane);
408: textComponent.clickOnHyperlink(link);
409: assertEquals(1, listener.getCallCount());
410: assertEquals(expectedTarget, listener.getLastEvent()
411: .getDescription());
412:
413: listener.reset();
414: textComponent.triggerClickOnHyperlink(link).run();
415: assertEquals(1, listener.getCallCount());
416: assertEquals(expectedTarget, listener.getLastEvent()
417: .getDescription());
418: }
419:
420: private void checkClickOnHyperlinkError(String html,
421: final String link, String errorMessage) throws Exception {
422: final TextBox textComponent = new TextBox(createTextPane(html));
423: checkAssertionFailedError(new Functor() {
424: public void run() throws Exception {
425: textComponent.clickOnHyperlink(link);
426: }
427: }, errorMessage);
428:
429: checkAssertionFailedError(new Functor() {
430: public void run() throws Exception {
431: textComponent.triggerClickOnHyperlink(link).run();
432: }
433: }, errorMessage);
434: }
435:
436: private static class DummyHyperlinkListener implements
437: HyperlinkListener {
438: int callCount;
439: HyperlinkEvent lastEvent;
440:
441: public void hyperlinkUpdate(HyperlinkEvent event) {
442: callCount++;
443: this .lastEvent = event;
444: }
445:
446: public int getCallCount() {
447: return callCount;
448: }
449:
450: public HyperlinkEvent getLastEvent() {
451: return lastEvent;
452: }
453:
454: public void reset() {
455: callCount = 0;
456: lastEvent = null;
457: }
458: }
459: }
|