001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import com.gargoylesoftware.htmlunit.WebTestCase;
041:
042: /**
043: * Tests for {@link DomText}.
044: *
045: * @version $Revision: 2132 $
046: * @author Marc Guillemot
047: * @author Ahmed Ashour
048: * @author Rodney Gitzel
049: */
050: public class DomTextTest extends WebTestCase {
051:
052: /**
053: * Create an instance
054: * @param name Name of the test
055: */
056: public DomTextTest(final String name) {
057: super (name);
058: }
059:
060: /**
061: * Test the clean up of   in strings
062: * @throws Exception if the test fails
063: */
064: public void testAsText_nbsp() throws Exception {
065: testPlainText("a b c d e", "a b c d e");
066: testPlainText("a b c d e", "a b c d e");
067:
068: testPlainText(" a ", " a ");
069: testPlainText(" a ", " a ");
070: testPlainText(" a ", " a ");
071: }
072:
073: /**
074: * Test font formats, as per bug #1731042
075: * (http://sourceforge.net/tracker/index.php?func=detail&aid=1731042&group_id=47038&atid=448266)
076: *
077: * @throws Exception if the test fails
078: */
079: public void testAsText_fontFormat() throws Exception {
080: // specific case reported by rgitzel
081: testAsText("a <b>b</b> c", "a b c");
082: testAsText("a <b>b</b>c", "a bc");
083: testAsText("a<b>b</b> c", "ab c");
084: testAsText("a<b>b</b>c", "abc");
085:
086: // italics and teletype should work the same way
087: testAsText("a <i>b</i> c", "a b c");
088: testAsText("a <i>b</i>c", "a bc");
089: testAsText("a<i>b</i> c", "ab c");
090: testAsText("a<i>b</i>c", "abc");
091:
092: testAsText("a <tt>b</tt> c", "a b c");
093: testAsText("a <tt>b</tt>c", "a bc");
094: testAsText("a<tt>b</tt> c", "ab c");
095: testAsText("a<tt>b</tt>c", "abc");
096:
097: // suggested by asashour ;-)
098: testAsText("a <font>b</font> c", "a b c");
099: testAsText("a<font>b</font> c", "ab c");
100: testAsText("a <font>b</font>c", "a bc");
101: testAsText("a<font>b</font>c", "abc");
102:
103: // I guess 'span' should be just like 'font'
104: testAsText("a <span>b</span> c", "a b c");
105: testAsText("a<span>b</span> c", "ab c");
106: testAsText("a <span>b</span>c", "a bc");
107: testAsText("a<span>b</span>c", "abc");
108:
109: // try some combinations
110: testAsText("a<b><font><i>b</i></font></b>c", "abc");
111: testAsText("a<b><font> <i>b</i></font></b>c", "a bc");
112: }
113:
114: /**
115: * These worked before the changes for bug #1731042, and should afterwards, too
116: *
117: * @throws Exception if the test fails
118: */
119: public void testAsText_regression() throws Exception {
120: testAsText("a<ul><li>b</ul>c", "a b c");
121: testAsText("a<p>b<br>c", "a b c");
122: testAsText("a<table><tr><td>b</td></tr></table>c", "a b c");
123: testAsText("a<div>b</div>c", "a b c");
124:
125: testAsText(
126: "a<table><tr><td> b </td></tr>\n<tr><td> b </td></tr></table>c",
127: "a b b c");
128: }
129:
130: /**
131: * check the HtmlTable* objects themselves
132: *
133: * @throws Exception if the test fails
134: */
135: public void testAsText_table_elements() throws Exception {
136: final String html = "<table id='table'><tr id='row'><td id='cell'> b </td></tr>\n</table>\n";
137: final String content = "<html><body><span id='foo'>" + html
138: + "</span></body></html>";
139:
140: final HtmlPage page = loadPage(content);
141:
142: assertEquals("b", page.getHtmlElementById("cell").asText());
143: assertEquals("b", page.getHtmlElementById("row").asText());
144: assertEquals("b", page.getHtmlElementById("table").asText());
145: }
146:
147: // ====================================================================================
148:
149: private void testPlainText(final String html,
150: final String expectedText) throws Exception {
151: final String content = "<html><body><span id='foo'>" + html
152: + "</span></body></html>";
153:
154: final HtmlPage page = loadPage(content);
155: final HtmlElement elt = page.getHtmlElementById("foo");
156: assertEquals(expectedText, elt.asText());
157:
158: final DomNode node = elt.getFirstDomChild();
159: assertEquals(expectedText, node.asText());
160: }
161:
162: private void testAsText(final String html, final String expectedText)
163: throws Exception {
164: final String content = "<html><body><span id='foo'>" + html
165: + "</span></body></html>";
166:
167: final HtmlPage page = loadPage(content);
168: final HtmlElement elt = page.getHtmlElementById("foo");
169: assertEquals(expectedText, elt.asText());
170: }
171: }
|