1 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 57 public class NavigatorTest extends WebTestCase { 58 59 63 public NavigatorTest( final String name ) { 64 super(name); 65 } 66 67 71 public void testAppCodeName() throws Exception { 72 this.testAttribute("appCodeName", BrowserVersion.getDefault().getApplicationCodeName()); 73 } 74 75 79 public void testAppMinorVersion() throws Exception { 80 this.testAttribute("appMinorVersion", BrowserVersion.getDefault().getApplicationMinorVersion()); 81 } 82 83 87 public void testAppName() throws Exception { 88 this.testAttribute("appName", BrowserVersion.getDefault().getApplicationName()); 89 } 90 91 95 public void testAppVersion() throws Exception { 96 this.testAttribute("appVersion", BrowserVersion.getDefault().getApplicationVersion()); 97 } 98 99 103 public void testBrowserLanguage() throws Exception { 104 this.testAttribute("browserLanguage", BrowserVersion.getDefault().getBrowserLanguage()); 105 } 106 107 111 public void testCookieEnabled() throws Exception { 112 this.testAttribute("cookieEnabled", "true"); 113 } 114 115 119 public void testCpuClass() throws Exception { 120 this.testAttribute("cpuClass", BrowserVersion.getDefault().getCpuClass()); 121 } 122 123 127 public void testOnLine() throws Exception { 128 this.testAttribute("onLine", String.valueOf(BrowserVersion.getDefault().isOnLine())); 129 } 130 131 135 public void testPlatform() throws Exception { 136 this.testAttribute("platform", BrowserVersion.getDefault().getPlatform()); 137 } 138 139 143 public void testSystemLanguage() throws Exception { 144 this.testAttribute("systemLanguage", BrowserVersion.getDefault().getSystemLanguage()); 145 } 146 147 151 public void testUserAgent() throws Exception { 152 this.testAttribute("userAgent", BrowserVersion.getDefault().getUserAgent()); 153 } 154 155 159 public void testUserLanguage() throws Exception { 160 this.testAttribute("userLanguage", BrowserVersion.getDefault().getUserLanguage()); 161 } 162 163 167 public void testPlugins() throws Exception { 168 this.testAttribute("plugins.length", "0"); 169 } 170 171 175 public void testJavaEnabled() throws Exception { 176 this.testAttribute("javaEnabled()", "false"); 177 } 178 179 183 public void testTaintEnabled() throws Exception { 184 this.testAttribute("taintEnabled()", "false"); 185 } 186 187 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 219 public void testUseConfiguredBrowser() throws Exception { 220 221 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 |