KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.List;
45
46 import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
47 import com.gargoylesoftware.htmlunit.MockWebConnection;
48 import com.gargoylesoftware.htmlunit.WebClient;
49 import com.gargoylesoftware.htmlunit.WebTestCase;
50 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
51 import com.gargoylesoftware.htmlunit.html.HtmlPage;
52
53 /**
54  * Tests for Location.
55  *
56  * @version $Revision: 1.10 $
57  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
58  * @author Michael Ottati
59  * @author Marc Guillemot
60  * @author Daniel Gredler
61  */

62 public class LocationTest extends WebTestCase {
63
64     /**
65      * Create an instance
66      * @param name The name of the test
67      */

68     public LocationTest( final String name ) {
69         super(name);
70     }
71     
72     /**
73      * Regression test for bug 742902
74      * @throws Exception if the test fails
75      */

76     public void testDocumentLocationGet() throws Exception {
77         final String firstContent
78             = "<html><head><title>First</title><script>"
79             + "function doTest() {\n"
80             + " alert(top.document.location);\n"
81             + "}\n"
82             + "</script></head><body onload='doTest()'>"
83             + "</body></html>";
84
85         final List collectedAlerts = new ArrayList();
86
87         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
88         assertEquals( "First", firstPage.getTitleText() );
89
90         final List expectedAlerts = Collections.singletonList(URL_GARGOYLE.toExternalForm());
91         assertEquals( expectedAlerts, collectedAlerts );
92     }
93     
94     /**
95      * @throws Exception if the test fails
96      */

97     public void testDocumentLocationSet() throws Exception {
98
99         final WebClient webClient = new WebClient();
100         final MockWebConnection webConnection = new MockWebConnection( webClient );
101
102         final String html1 =
103               "<html>\n"
104             + "<head>\n"
105             + " <title>test1</title>\n"
106             + " <script>\n"
107             + " function test() {\n"
108             + " document.location = '" + URL_SECOND.toExternalForm() + "';\n"
109             + " }\n"
110             + " </script>\n"
111             + "</head>\n"
112             + "<body onload='test()'></body>\n"
113             + "</html>";
114         final String html2 =
115               "<html>\n"
116             + "<head>\n"
117             + " <title>test2</title>\n"
118             + " <script>\n"
119             + " function test() {\n"
120             + " alert('ok');\n"
121             + " }\n"
122             + " </script>\n"
123             + "</head>\n"
124             + "<body onload='test()'></body>\n"
125             + "</html>";
126
127         webConnection.setResponse(URL_FIRST, html1);
128         webConnection.setResponse(URL_SECOND, html2);
129         webClient.setWebConnection( webConnection );
130
131         final List collectedAlerts = new ArrayList();
132         webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
133
134         final HtmlPage page = ( HtmlPage )webClient.getPage( URL_FIRST );
135         assertEquals( "test2", page.getTitleText() );
136
137         final List expectedAlerts = Collections.singletonList( "ok" );
138         assertEquals( expectedAlerts, collectedAlerts );
139     }
140
141     /**
142      * @throws Exception if the test fails
143      */

144     public void testDocumentLocationHref() throws Exception {
145         final String firstContent
146             = "<html><head><title>First</title><script>"
147             + "function doTest() {\n"
148             + " alert(top.document.location.href);\n"
149             + "}\n"
150             + "</script></head><body onload='doTest()'>"
151             + "</body></html>";
152
153         final List collectedAlerts = new ArrayList();
154         final HtmlPage firstPage = loadPage(firstContent, collectedAlerts);
155         assertEquals( "First", firstPage.getTitleText() );
156
157         final List expectedAlerts = Collections.singletonList(URL_GARGOYLE.toExternalForm());
158         assertEquals( expectedAlerts, collectedAlerts );
159     }
160     
161     /**
162      * @throws Exception if the test fails
163      */

164     public void testLocation_variousAttributes() throws Exception {
165         final WebClient client = new WebClient();
166         final MockWebConnection webConnection = new MockWebConnection( client );
167
168         final String firstContent
169             = "<html><head><title>First</title><script>\n"
170             + "function doTest() {\n"
171             + " var location = document.location;"
172             + " alert(location.hash);\n"
173             + " alert(location.host);\n"
174             + " alert(location.hostname);\n"
175             + " alert(location.href);\n"
176             + " alert(location.pathname);\n"
177             + " alert(location.port);\n"
178             + " alert(location.protocol);\n"
179             + " alert(location.search);\n"
180             + "}\n</script></head>"
181             + "<body onload='doTest()'></body></html>";
182
183         webConnection.setDefaultResponse( firstContent );
184         client.setWebConnection( webConnection );
185
186         final List collectedAlerts = new ArrayList();
187         client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
188
189         List expectedAlerts;
190
191         // Try page with only a server name
192
client.getPage( new URL("http://first") );
193         expectedAlerts = Arrays.asList( new String[]{
194             "", // hash
195
"first", // host
196
"first", // hostname
197
"http://first", // href
198
"", // pathname
199
"", // port
200
"http:", // protocol
201
"" // search
202
} );
203         assertEquals( "simple url", expectedAlerts, collectedAlerts );
204
205         collectedAlerts.clear();
206         
207         // Try page with all the appropriate parts
208
client.getPage( new URL("http://www.first:77/foo?bar#wahoo") );
209         expectedAlerts = Arrays.asList( new String[]{
210             "wahoo", // hash
211
"www.first:77", // host
212
"www.first", // hostname
213
"http://www.first:77/foo?bar#wahoo", // href
214
"/foo", // pathname
215
"77", // port
216
"http:", // protocol
217
"?bar" // search
218
} );
219         assertEquals( "complete url", expectedAlerts, collectedAlerts );
220     }
221
222     /**
223      * Test for replace
224      * @throws Exception if the test fails
225      */

226     public void testReplace() throws Exception {
227         final WebClient webClient = new WebClient();
228         final MockWebConnection webConnection = new MockWebConnection( webClient );
229
230         final String firstContent
231             = "<html><head><title>First</title><script>"
232             + "function doTest() {\n"
233             + " location.replace('" + URL_SECOND.toExternalForm() + "');\n"
234             + "}\n"
235             + "</script></head><body onload='doTest()'>"
236             + "</body></html>";
237
238         final String secondContent
239             = "<html><head><title>Second</title></head><body></body></html>";
240
241         webConnection.setResponse(URL_FIRST, firstContent);
242         webConnection.setResponse(URL_SECOND, secondContent);
243         webClient.setWebConnection( webConnection );
244
245         final HtmlPage page = (HtmlPage) webClient.getPage( URL_FIRST );
246         assertEquals("Second", page.getTitleText());
247     }
248
249     /**
250      * Tests that location.reload() works correctly.
251      * @throws Exception If the test fails.
252      */

253     public void testReload() throws Exception {
254
255         String content =
256               "<html>\n"
257             + " <head><title>test</title></head>\n"
258             + " <body>\n"
259             + " <a HREF='javascript:window.location.reload();' id='link1'>reload</a>\n"
260             + " </body>\n"
261             + "</html>";
262
263         final HtmlPage page1 = loadPage(content);
264         final HtmlAnchor link = (HtmlAnchor) page1.getHtmlElementById("link1");
265         final HtmlPage page2 = (HtmlPage) link.click();
266
267         assertEquals( page1.getTitleText(), page2.getTitleText() );
268         assertNotSame( page1, page2 );
269     }
270
271 }
272
Popular Tags