1 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 62 public class LocationTest extends WebTestCase { 63 64 68 public LocationTest( final String name ) { 69 super(name); 70 } 71 72 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 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 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 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 client.getPage( new URL("http://first") ); 193 expectedAlerts = Arrays.asList( new String[]{ 194 "", "first", "first", "http://first", "", "", "http:", "" } ); 203 assertEquals( "simple url", expectedAlerts, collectedAlerts ); 204 205 collectedAlerts.clear(); 206 207 client.getPage( new URL("http://www.first:77/foo?bar#wahoo") ); 209 expectedAlerts = Arrays.asList( new String[]{ 210 "wahoo", "www.first:77", "www.first", "http://www.first:77/foo?bar#wahoo", "/foo", "77", "http:", "?bar" } ); 219 assertEquals( "complete url", expectedAlerts, collectedAlerts ); 220 } 221 222 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 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 |