KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > CharacterDataImplTest


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.javascript.host;
39
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.List;
44
45 import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
46 import com.gargoylesoftware.htmlunit.MockWebConnection;
47 import com.gargoylesoftware.htmlunit.WebClient;
48 import com.gargoylesoftware.htmlunit.WebTestCase;
49 import com.gargoylesoftware.htmlunit.html.HtmlPage;
50
51
52 /**
53  * Tests for CharacterDataImpl
54  *
55  * @version $Revision: 1.7 $
56  * @author David K. Taylor
57  */

58 public class CharacterDataImplTest extends WebTestCase {
59     /**
60      * Create an instance
61      * @param name The name of the test
62      */

63     public CharacterDataImplTest( final String name ) {
64         super(name);
65     }
66
67
68     /**
69      * Regression test for inline text nodes
70      * @throws Exception if the test fails
71      */

72     public void testCharacterDataImpl_textNode() throws Exception {
73         final WebClient webClient = new WebClient();
74         final MockWebConnection webConnection = new MockWebConnection( webClient );
75
76         final String firstContent
77             = "<html><head><title>First</title><script>"
78             + "function doTest() {\n"
79             + " var div1=document.getElementById('div1');\n"
80             + " var text1=div1.firstChild;\n"
81             + " alert(text1.data);\n"
82             + " alert(text1.length);\n"
83             + " alert(text1.nodeType);\n"
84             + " alert(text1.nodeValue);\n"
85             + " alert(text1.nodeName);\n"
86             + "}\n"
87             + "</script></head><body onload='doTest()'>"
88             + "<div id='div1'>Some Text</div></body></html>";
89
90         webConnection.setResponse(
91             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
92         webClient.setWebConnection( webConnection );
93
94         final List collectedAlerts = new ArrayList();
95         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
96
97         final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
98         assertEquals( "First", firstPage.getTitleText() );
99
100         final List expectedAlerts = Arrays.asList( new String[]{
101             "Some Text", "9", "3", "Some Text", "#text"
102         } );
103         assertEquals( expectedAlerts, collectedAlerts );
104     }
105
106
107     /**
108      * Regression test for setting the data property of a text node
109      * @throws Exception if the test fails
110      */

111     public void testCharacterDataImpl_setData() throws Exception {
112         final WebClient webClient = new WebClient();
113         final MockWebConnection webConnection = new MockWebConnection( webClient );
114
115         final String firstContent
116             = "<html><head><title>First</title><script>"
117             + "function doTest() {\n"
118             + " var div1=document.getElementById('div1');\n"
119             + " var text1=div1.firstChild;\n"
120             + " text1.data='Some New Text';\n"
121             + " alert(text1.data);\n"
122             + " alert(text1.nodeValue);\n"
123             + "}\n"
124             + "</script></head><body onload='doTest()'>"
125             + "<div id='div1'>Some Text</div></body></html>";
126
127         webConnection.setResponse(
128             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
129         webClient.setWebConnection( webConnection );
130
131         final List collectedAlerts = new ArrayList();
132         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
133
134         final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
135         assertEquals( "First", firstPage.getTitleText() );
136
137         final List expectedAlerts = Arrays.asList( new String[]{
138             "Some New Text", "Some New Text"
139         } );
140         assertEquals( expectedAlerts, collectedAlerts );
141     }
142
143
144     /**
145      * Regression test for setting the nodeValue property of a text node
146      * @throws Exception if the test fails
147      */

148     public void testCharacterDataImpl_setNodeValue() throws Exception {
149         final WebClient webClient = new WebClient();
150         final MockWebConnection webConnection = new MockWebConnection( webClient );
151
152         final String firstContent
153             = "<html><head><title>First</title><script>"
154             + "function doTest() {\n"
155             + " var div1=document.getElementById('div1');\n"
156             + " var text1=div1.firstChild;\n"
157             + " text1.nodeValue='Some New Text';\n"
158             + " alert(text1.data);\n"
159             + " alert(text1.nodeValue);\n"
160             + "}\n"
161             + "</script></head><body onload='doTest()'>"
162             + "<div id='div1'>Some Text</div></body></html>";
163
164         webConnection.setResponse(
165             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
166         webClient.setWebConnection( webConnection );
167
168         final List collectedAlerts = new ArrayList();
169         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
170
171         final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
172         assertEquals( "First", firstPage.getTitleText() );
173
174         final List expectedAlerts = Arrays.asList( new String[]{
175             "Some New Text", "Some New Text"
176         } );
177         assertEquals( expectedAlerts, collectedAlerts );
178     }
179
180
181     /**
182      * Regression test for appendData of a text node
183      * @throws Exception if the test fails
184      */

185     public void testCharacterDataImpl_appendData() throws Exception {
186         final WebClient webClient = new WebClient();
187         final MockWebConnection webConnection = new MockWebConnection( webClient );
188
189         final String firstContent
190             = "<html><head><title>First</title><script>"
191             + "function doTest() {\n"
192             + " var div1=document.getElementById('div1');\n"
193             + " var text1=div1.firstChild;\n"
194             + " text1.appendData(' Appended');\n"
195             + " alert(text1.data);\n"
196             + "}\n"
197             + "</script></head><body onload='doTest()'>"
198             + "<div id='div1'>Some Text</div></body></html>";
199
200         webConnection.setResponse(
201             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
202         webClient.setWebConnection( webConnection );
203
204         final List collectedAlerts = new ArrayList();
205         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
206
207         final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
208         assertEquals( "First", firstPage.getTitleText() );
209
210         final List expectedAlerts = Arrays.asList( new String[]{
211             "Some Text Appended"
212         } );
213         assertEquals( expectedAlerts, collectedAlerts );
214     }
215
216
217     /**
218      * Regression test for deleteData of a text node
219      * @throws Exception if the test fails
220      */

221     public void testCharacterDataImpl_deleteData() throws Exception {
222         final WebClient webClient = new WebClient();
223         final MockWebConnection webConnection = new MockWebConnection( webClient );
224
225         final String firstContent
226             = "<html><head><title>First</title><script>"
227             + "function doTest() {\n"
228             + " var div1=document.getElementById('div1');\n"
229             + " var text1=div1.firstChild;\n"
230             + " text1.deleteData(5, 11);\n"
231             + " alert(text1.data);\n"
232             + "}\n"
233             + "</script></head><body onload='doTest()'>"
234             + "<div id='div1'>Some Not So New Text</div></body></html>";
235
236         webConnection.setResponse(
237             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
238         webClient.setWebConnection( webConnection );
239
240         final List collectedAlerts = new ArrayList();
241         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
242
243         final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
244         assertEquals( "First", firstPage.getTitleText() );
245
246         final List expectedAlerts = Arrays.asList( new String[]{
247             "Some Text"
248         } );
249         assertEquals( expectedAlerts, collectedAlerts );
250     }
251
252
253     /**
254      * Regression test for insertData of a text node
255      * @throws Exception if the test fails
256      */

257     public void testCharacterDataImpl_insertData() throws Exception {
258         final WebClient webClient = new WebClient();
259         final MockWebConnection webConnection = new MockWebConnection( webClient );
260
261         final String firstContent
262             = "<html><head><title>First</title><script>"
263             + "function doTest() {\n"
264             + " var div1=document.getElementById('div1');\n"
265             + " var text1=div1.firstChild;\n"
266             + " text1.insertData(5, 'New ');\n"
267             + " alert(text1.data);\n"
268             + "}\n"
269             + "</script></head><body onload='doTest()'>"
270             + "<div id='div1'>Some Text</div></body></html>";
271
272         webConnection.setResponse(
273             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
274         webClient.setWebConnection( webConnection );
275
276         final List collectedAlerts = new ArrayList();
277         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
278
279         final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
280         assertEquals( "First", firstPage.getTitleText() );
281
282         final List expectedAlerts = Arrays.asList( new String[]{
283             "Some New Text"
284         } );
285         assertEquals( expectedAlerts, collectedAlerts );
286     }
287
288
289     /**
290      * Regression test for replaceData of a text node
291      * @throws Exception if the test fails
292      */

293     public void testCharacterDataImpl_replaceData() throws Exception {
294         final WebClient webClient = new WebClient();
295         final MockWebConnection webConnection = new MockWebConnection( webClient );
296
297         final String firstContent
298             = "<html><head><title>First</title><script>"
299             + "function doTest() {\n"
300             + " var div1=document.getElementById('div1');\n"
301             + " var text1=div1.firstChild;\n"
302             + " text1.replaceData(5, 3, 'New');\n"
303             + " alert(text1.data);\n"
304             + "}\n"
305             + "</script></head><body onload='doTest()'>"
306             + "<div id='div1'>Some Old Text</div></body></html>";
307
308         webConnection.setResponse(
309             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
310         webClient.setWebConnection( webConnection );
311
312         final List collectedAlerts = new ArrayList();
313         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
314
315         final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
316         assertEquals( "First", firstPage.getTitleText() );
317
318         final List expectedAlerts = Arrays.asList( new String[]{
319             "Some New Text"
320         } );
321         assertEquals( expectedAlerts, collectedAlerts );
322     }
323
324
325     /**
326      * Regression test for substringData of a text node
327      * @throws Exception if the test fails
328      */

329     public void testCharacterDataImpl_substringData() throws Exception {
330         final WebClient webClient = new WebClient();
331         final MockWebConnection webConnection = new MockWebConnection( webClient );
332
333         final String firstContent
334             = "<html><head><title>First</title><script>"
335             + "function doTest() {\n"
336             + " var div1=document.getElementById('div1');\n"
337             + " var text1=div1.firstChild;\n"
338             + " alert(text1.substringData(5, 3));\n"
339             + " alert(text1.data);\n"
340             + "}\n"
341             + "</script></head><body onload='doTest()'>"
342             + "<div id='div1'>Some New Text</div></body></html>";
343
344         webConnection.setResponse(
345             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
346         webClient.setWebConnection( webConnection );
347
348         final List collectedAlerts = new ArrayList();
349         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
350
351         final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
352         assertEquals( "First", firstPage.getTitleText() );
353
354         final List expectedAlerts = Arrays.asList( new String[]{
355             "New", "Some New Text"
356         } );
357         assertEquals( expectedAlerts, collectedAlerts );
358     }
359
360
361     /**
362      * Regression test for substringData of a text node
363      * @throws Exception if the test fails
364      */

365     public void testTextImpl_splitText() throws Exception {
366         final WebClient webClient = new WebClient();
367         final MockWebConnection webConnection = new MockWebConnection( webClient );
368
369         final String firstContent
370             = "<html><head><title>First</title><script>"
371             + "function doTest() {\n"
372             + " var div1=document.getElementById('div1');\n"
373             + " var text1=div1.firstChild;\n"
374             + " var text2=text1.splitText(5);\n"
375             + " alert(text1.data);\n"
376             + " alert(text2.data);\n"
377             + " alert(text1.nextSibling==text2);\n"
378             + "}\n"
379             + "</script></head><body onload='doTest()'>"
380             + "<div id='div1'>Some Text</div></body></html>";
381
382         webConnection.setResponse(
383             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
384         webClient.setWebConnection( webConnection );
385
386         final List collectedAlerts = new ArrayList();
387         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
388
389         final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST );
390         assertEquals( "First", firstPage.getTitleText() );
391
392         final List expectedAlerts = Arrays.asList( new String[]{
393             "Some ", "Text", "true"
394         } );
395         assertEquals( expectedAlerts, collectedAlerts );
396     }
397 }
398
Popular Tags