KickJava   Java API By Example, From Geeks To Geeks.

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


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.List;
43
44 import com.gargoylesoftware.htmlunit.BrowserVersion;
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
50 /**
51  * Tests for Navigator.
52  *
53  * @version $Revision: 1.9 $
54  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
55  * @author Daniel Gredler
56  */

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

63     public NavigatorTest( final String name ) {
64         super(name);
65     }
66
67     /**
68      * Tests the "appCodeName" property.
69      * @throws Exception on test failure.
70      */

71     public void testAppCodeName() throws Exception {
72         this.testAttribute("appCodeName", BrowserVersion.getDefault().getApplicationCodeName());
73     }
74
75     /**
76      * Tests the "appMinorVersion" property.
77      * @throws Exception on test failure.
78      */

79     public void testAppMinorVersion() throws Exception {
80         this.testAttribute("appMinorVersion", BrowserVersion.getDefault().getApplicationMinorVersion());
81     }
82
83     /**
84      * Tests the "appName" property.
85      * @throws Exception on test failure.
86      */

87     public void testAppName() throws Exception {
88         this.testAttribute("appName", BrowserVersion.getDefault().getApplicationName());
89     }
90
91     /**
92      * Tests the "appVersion" property.
93      * @throws Exception on test failure.
94      */

95     public void testAppVersion() throws Exception {
96         this.testAttribute("appVersion", BrowserVersion.getDefault().getApplicationVersion());
97     }
98
99     /**
100      * Tests the "browserLanguage" property.
101      * @throws Exception on test failure.
102      */

103     public void testBrowserLanguage() throws Exception {
104         this.testAttribute("browserLanguage", BrowserVersion.getDefault().getBrowserLanguage());
105     }
106
107     /**
108      * Tests the "cookieEnabled" property.
109      * @throws Exception on test failure.
110      */

111     public void testCookieEnabled() throws Exception {
112         this.testAttribute("cookieEnabled", "true");
113     }
114
115     /**
116      * Tests the "cpuClass" property.
117      * @throws Exception on test failure.
118      */

119     public void testCpuClass() throws Exception {
120         this.testAttribute("cpuClass", BrowserVersion.getDefault().getCpuClass());
121     }
122
123     /**
124      * Tests the "onLine" property.
125      * @throws Exception on test failure.
126      */

127     public void testOnLine() throws Exception {
128         this.testAttribute("onLine", String.valueOf(BrowserVersion.getDefault().isOnLine()));
129     }
130
131     /**
132      * Tests the "platform" property.
133      * @throws Exception on test failure.
134      */

135     public void testPlatform() throws Exception {
136         this.testAttribute("platform", BrowserVersion.getDefault().getPlatform());
137     }
138
139     /**
140      * Tests the "systemLanguage" property.
141      * @throws Exception on test failure.
142      */

143     public void testSystemLanguage() throws Exception {
144         this.testAttribute("systemLanguage", BrowserVersion.getDefault().getSystemLanguage());
145     }
146
147     /**
148      * Tests the "userAgent" property.
149      * @throws Exception on test failure.
150      */

151     public void testUserAgent() throws Exception {
152         this.testAttribute("userAgent", BrowserVersion.getDefault().getUserAgent());
153     }
154
155     /**
156      * Tests the "userLanguage" property.
157      * @throws Exception on test failure.
158      */

159     public void testUserLanguage() throws Exception {
160         this.testAttribute("userLanguage", BrowserVersion.getDefault().getUserLanguage());
161     }
162
163     /**
164      * Tests the "plugins" property.
165      * @throws Exception on test failure.
166      */

167     public void testPlugins() throws Exception {
168         this.testAttribute("plugins.length", "0");
169     }
170
171     /**
172      * Tests the "javaEnabled" method.
173      * @throws Exception on test failure.
174      */

175     public void testJavaEnabled() throws Exception {
176         this.testAttribute("javaEnabled()", "false");
177     }
178
179     /**
180      * Tests the "taintEnabled" property.
181      * @throws Exception on test failure.
182      */

183     public void testTaintEnabled() throws Exception {
184         this.testAttribute("taintEnabled()", "false");
185     }
186
187     /**
188      * Generic method for testing the value of a specific navigator attribute.
189      * @param name the name of the attribute to test.
190      * @param value the expected value for the named attribute.
191      * @throws Exception on test failure.
192      */

193     private void testAttribute(final String name, final String value) throws Exception {
194         final String content = "<html>\n" +
195                 "<head>\n" +
196                 " <title>test</title>\n" +
197                 " <script>\n" +
198                 " function doTest(){\n" +
199                 " alert('" + name + " = ' + window.navigator." + name + ");\n" +
200                 " }\n" +
201                 " </script>\n" +
202                 "</head>\n" +
203                 "<body onload=\'doTest()\'>\n" +
204                 "</body>\n" +
205                 "</html>\n" +
206                 "";
207         final List collectedAlerts = new ArrayList();
208         loadPage(content, collectedAlerts);
209         final List expectedAlerts = Arrays.asList(new String[]{
210             name + " = " + value
211         });
212         assertEquals(expectedAlerts, collectedAlerts);
213     }
214
215     /**
216      * Test closing using javascript
217      * @throws Exception if the test fails.
218      */

219     public void testUseConfiguredBrowser() throws Exception {
220
221         // activate test when all custom JS objects have a way to get the webclient
222
// they run in
223
if (true) {
224             notImplemented();
225             return;
226         }
227
228         final WebClient webClient = new WebClient(BrowserVersion.MOZILLA_1_0);
229         final MockWebConnection webConnection = new MockWebConnection(webClient);
230
231         final List collectedAlerts = new ArrayList();
232         webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
233
234         final String content
235             = "<html><head><title>First</title></head>"
236             + "<body onload='alert(window.navigator.appName)'></body>"
237             + "</html>";
238
239         webConnection.setDefaultResponse(content);
240         webClient.setWebConnection(webConnection);
241
242         webClient.getPage(URL_FIRST);
243
244         final List expectedAlerts = Arrays.asList(new String[]{ "Netscape" });
245         assertEquals(expectedAlerts, collectedAlerts);
246     }
247     
248 }
249
Popular Tags