KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Tests for NodeImpl
53  *
54  * @author yourgod
55  * @author <a HREF="mailto:george@murnock.com">George Murnock</a>
56  * @version $Revision: 1.8 $
57  *
58  */

59 public class NodeImplTest extends WebTestCase {
60
61     /**
62      * @param name The name of the test case
63      */

64     public NodeImplTest(final String name) {
65         super(name);
66     }
67
68     /**
69      * @throws Exception on test failure
70      */

71     public void test_hasChildNodes_true() throws Exception {
72         final String content = "<html><head><title>test_hasChildNodes</title>"
73                 + "<script>"
74                 + "function doTest(){"
75                 + " alert(document.getElementById('myNode').hasChildNodes());"
76                 + "}"
77                 + "</script>"
78                 + "</head><body onload='doTest()'>"
79                 + "<p id='myNode'>hello world<span>Child Node</span></p>"
80                 + "</body></html>";
81
82         final List collectedAlerts = new ArrayList();
83         final HtmlPage page = loadPage(content, collectedAlerts);
84         assertEquals("test_hasChildNodes", page.getTitleText());
85
86         final List expectedAlerts = Arrays.asList(new String[]{
87             "true"
88         });
89
90         assertEquals(expectedAlerts, collectedAlerts);
91     }
92     /**
93      * @throws Exception on test failure
94      */

95     public void test_hasChildNodes_false() throws Exception {
96         final String content = "<html><head><title>test_hasChildNodes</title>"
97                 + "<script>"
98                 + "function doTest(){"
99                 + " alert(document.getElementById('myNode').hasChildNodes());"
100                 + "}"
101                 + "</script>"
102                 + "</head><body onload='doTest()'>"
103                 + "<p id='myNode'></p>"
104                 + "</body></html>";
105
106         final List collectedAlerts = new ArrayList();
107         final HtmlPage page = loadPage(content, collectedAlerts);
108         assertEquals("test_hasChildNodes", page.getTitleText());
109
110         final List expectedAlerts = Arrays.asList(new String[]{
111             "false"
112         });
113
114         assertEquals(expectedAlerts, collectedAlerts);
115     }
116     /**
117      * Regression test for removeChild
118      * @throws Exception if the test fails
119      */

120     public void testRemoveChild() throws Exception {
121         final WebClient webClient = new WebClient();
122         final MockWebConnection webConnection = new MockWebConnection( webClient );
123         webClient.setWebConnection( webConnection );
124
125         final String content
126             = "<html><head><title>foo</title><script>\n"
127             + "function doTest(){\n"
128             + " var form = document.forms['form1'];\n"
129             + " var div = form.firstChild;\n"
130             + " var removedDiv = form.removeChild(div);\n"
131             + " alert(div==removedDiv);\n"
132             + " alert(form.firstChild==null);\n"
133             + "}\n"
134             + "</script></head><body onload='doTest()'>\n"
135             + "<form name='form1'><div id='formChild'/></form>"
136             + "</body></html>";
137         webConnection.setResponse(
138             URL_FIRST, content, 200, "OK", "text/html", Collections.EMPTY_LIST );
139
140         final List collectedAlerts = new ArrayList();
141         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
142
143         final HtmlPage page = ( HtmlPage )webClient.getPage( URL_FIRST );
144         assertEquals("foo", page.getTitleText());
145
146         final List expectedAlerts = Arrays.asList( new String[]{
147             "true", "true"
148         } );
149
150         assertEquals( expectedAlerts, collectedAlerts );
151     }
152
153     /**
154      * Regression test for replaceChild
155      * @throws Exception if the test fails
156      */

157     public void testReplaceChild() throws Exception {
158         final WebClient webClient = new WebClient();
159         final MockWebConnection webConnection = new MockWebConnection( webClient );
160         webClient.setWebConnection( webConnection );
161
162         final String content
163             = "<html><head><title>foo</title><script>\n"
164             + "function doTest(){\n"
165             + " var form = document.forms['form1'];\n"
166             + " var div1 = form.firstChild;\n"
167             + " var div2 = document.getElementById('newChild');\n"
168             + " var removedDiv = form.replaceChild(div2,div1);\n"
169             + " alert(div1==removedDiv);\n"
170             + " alert(form.firstChild==div2);\n"
171             + "}\n"
172             + "</script></head><body onload='doTest()'>\n"
173             + "<form name='form1'><div id='formChild'/></form>"
174             + "</body><div id='newChild'/></html>";
175         webConnection.setResponse(
176             URL_FIRST, content, 200, "OK", "text/html", Collections.EMPTY_LIST );
177
178         final List collectedAlerts = new ArrayList();
179         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
180
181         final HtmlPage page = ( HtmlPage )webClient.getPage( URL_FIRST );
182         assertEquals("foo", page.getTitleText());
183
184         final List expectedAlerts = Arrays.asList( new String[]{
185             "true", "true"
186         } );
187
188         assertEquals( expectedAlerts, collectedAlerts );
189     }
190     
191     /**
192      * The common browsers always return node names in uppercase. Test this.
193      * @throws Exception on test failure
194      */

195     public void testNodeNameIsUppercase() throws Exception {
196         final String content = "<html><head><title>test_hasChildNodes</title>"
197                 + "<script>"
198                 + "function doTest(){"
199                 + " alert(document.getElementById('myNode').nodeName);"
200                 + "}"
201                 + "</script>"
202                 + "</head><body onload='doTest()'>"
203                 + "<div id='myNode'>hello world<span>Child Node</span></div>"
204                 + "</body></html>";
205
206         final List collectedAlerts = new ArrayList();
207         final HtmlPage page = loadPage(content, collectedAlerts);
208         assertEquals("test_hasChildNodes", page.getTitleText());
209
210         final List expectedAlerts = Arrays.asList(new String[]{
211             "DIV"
212         });
213
214         assertEquals(expectedAlerts, collectedAlerts);
215     }
216     
217     /**
218      * @throws Exception on test failure
219      */

220     public void test_getChildNodes() throws Exception {
221         final String content = "<html><head><title>test_getChildNodes</title>"
222             + "<script>"
223             + "function doTest() {"
224             + "var aNode = document.getElementById('myNode');"
225             + "alert(aNode.childNodes.length);"
226             + "alert(aNode.childNodes[0].nodeName);"
227             + "alert(aNode.childNodes[0].childNodes.length);"
228             + "alert(aNode.childNodes[0].childNodes[0].nodeName);"
229             + "alert(aNode.childNodes[0].childNodes[1].nodeName);"
230             + "alert(aNode.childNodes[1].nodeName);"
231             + "}"
232             + "</script>"
233             + "</head><body onload='doTest()'>"
234             + "<div id='myNode'><span>Child Node 1-A"
235             + "<h1>Child Node 1-B</h1></span>"
236             + "<h2>Child Node 2-A</h2></div>"
237             + "</body></html>";
238
239         final List expectedAlerts = Arrays.asList(new String[] { "2", "SPAN", "2", "#text", "H1", "H2" });
240         createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
241
242         final List collectedAlerts = new ArrayList();
243         final HtmlPage page = loadPage(content, collectedAlerts);
244         assertEquals("test_getChildNodes", page.getTitleText());
245
246         assertEquals(expectedAlerts, collectedAlerts);
247     }
248 }
249
Popular Tags