| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/string_util.h" | |
| 6 #include "base/utf_string_conversions.h" | |
| 7 #include "content/browser/download/download_manager_impl.h" | |
| 8 #include "content/browser/web_contents/web_contents_impl.h" | |
| 9 #include "content/public/browser/browser_context.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 #include "content/public/browser/notification_service.h" | |
| 12 #include "content/public/browser/notification_types.h" | |
| 13 #include "content/public/browser/web_contents.h" | |
| 14 #include "content/public/common/url_constants.h" | |
| 15 #include "content/public/test/browser_test_utils.h" | |
| 16 #include "content/public/test/test_utils.h" | |
| 17 #include "content/shell/shell.h" | |
| 18 #include "content/test/content_browser_test.h" | |
| 19 #include "content/test/content_browser_test_utils.h" | |
| 20 #include "content/test/net/url_request_failed_job.h" | |
| 21 #include "content/test/net/url_request_mock_http_job.h" | |
| 22 #include "net/base/net_errors.h" | |
| 23 #include "net/test/test_server.h" | |
| 24 | |
| 25 namespace content { | |
| 26 | |
| 27 class ResourceDispatcherHostBrowserTest : public ContentBrowserTest, | |
| 28 public DownloadManager::Observer { | |
| 29 public: | |
| 30 ResourceDispatcherHostBrowserTest() : got_downloads_(false) {} | |
| 31 | |
| 32 protected: | |
| 33 virtual void SetUpOnMainThread() OVERRIDE { | |
| 34 FilePath path = GetTestFilePath("", ""); | |
| 35 BrowserThread::PostTask( | |
| 36 BrowserThread::IO, FROM_HERE, | |
| 37 base::Bind(&URLRequestMockHTTPJob::AddUrlHandler, path)); | |
| 38 BrowserThread::PostTask( | |
| 39 BrowserThread::IO, FROM_HERE, | |
| 40 base::Bind(&URLRequestFailedJob::AddUrlHandler)); | |
| 41 } | |
| 42 | |
| 43 virtual void OnDownloadCreated( | |
| 44 DownloadManager* manager, | |
| 45 DownloadItem* item) OVERRIDE { | |
| 46 if (!got_downloads_) | |
| 47 got_downloads_ = !!manager->InProgressCount(); | |
| 48 } | |
| 49 | |
| 50 RenderViewHost* render_view_host() { | |
| 51 return shell()->web_contents()->GetRenderViewHost(); | |
| 52 } | |
| 53 | |
| 54 GURL GetMockURL(const std::string& file) { | |
| 55 return URLRequestMockHTTPJob::GetMockUrl(FilePath().AppendASCII(file)); | |
| 56 } | |
| 57 | |
| 58 void CheckTitleTest(const GURL& url, | |
| 59 const std::string& expected_title) { | |
| 60 string16 expected_title16(ASCIIToUTF16(expected_title)); | |
| 61 TitleWatcher title_watcher(shell()->web_contents(), expected_title16); | |
| 62 NavigateToURL(shell(), url); | |
| 63 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); | |
| 64 } | |
| 65 | |
| 66 bool GetPopupTitle(const GURL& url, string16* title) { | |
| 67 NavigateToURL(shell(), url); | |
| 68 | |
| 69 ShellAddedObserver new_shell_observer; | |
| 70 | |
| 71 // Create dynamic popup. | |
| 72 if (!ExecuteJavaScript(render_view_host(), L"", L"OpenPopup();")) | |
| 73 return false; | |
| 74 | |
| 75 Shell* new_shell = new_shell_observer.GetShell(); | |
| 76 *title = new_shell->web_contents()->GetTitle(); | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 std::string GetCookies(const GURL& url) { | |
| 81 return content::GetCookies( | |
| 82 shell()->web_contents()->GetBrowserContext(), url); | |
| 83 } | |
| 84 | |
| 85 bool got_downloads() const { return got_downloads_; } | |
| 86 | |
| 87 private: | |
| 88 bool got_downloads_; | |
| 89 }; | |
| 90 | |
| 91 // Test title for content created by javascript window.open(). | |
| 92 // See http://crbug.com/5988 | |
| 93 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, DynamicTitle1) { | |
| 94 ASSERT_TRUE(test_server()->Start()); | |
| 95 | |
| 96 GURL url(test_server()->GetURL("files/dynamic1.html")); | |
| 97 string16 title; | |
| 98 ASSERT_TRUE(GetPopupTitle(url, &title)); | |
| 99 EXPECT_TRUE(StartsWith(title, ASCIIToUTF16("My Popup Title"), true)) | |
| 100 << "Actual title: " << title; | |
| 101 } | |
| 102 | |
| 103 // Test title for content created by javascript window.open(). | |
| 104 // See http://crbug.com/5988 | |
| 105 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, DynamicTitle2) { | |
| 106 ASSERT_TRUE(test_server()->Start()); | |
| 107 | |
| 108 GURL url(test_server()->GetURL("files/dynamic2.html")); | |
| 109 string16 title; | |
| 110 ASSERT_TRUE(GetPopupTitle(url, &title)); | |
| 111 EXPECT_TRUE(StartsWith(title, ASCIIToUTF16("My Dynamic Title"), true)) | |
| 112 << "Actual title: " << title; | |
| 113 } | |
| 114 | |
| 115 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 116 SniffHTMLWithNoContentType) { | |
| 117 CheckTitleTest(GetMockURL("content-sniffer-test0.html"), | |
| 118 "Content Sniffer Test 0"); | |
| 119 } | |
| 120 | |
| 121 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 122 RespectNoSniffDirective) { | |
| 123 CheckTitleTest(GetMockURL("nosniff-test.html"), | |
| 124 "mock.http/nosniff-test.html"); | |
| 125 } | |
| 126 | |
| 127 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 128 DoNotSniffHTMLFromTextPlain) { | |
| 129 CheckTitleTest(GetMockURL("content-sniffer-test1.html"), | |
| 130 "mock.http/content-sniffer-test1.html"); | |
| 131 } | |
| 132 | |
| 133 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 134 DoNotSniffHTMLFromImageGIF) { | |
| 135 CheckTitleTest(GetMockURL("content-sniffer-test2.html"), | |
| 136 "mock.http/content-sniffer-test2.html"); | |
| 137 } | |
| 138 | |
| 139 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 140 SniffNoContentTypeNoData) { | |
| 141 // Make sure no downloads start. | |
| 142 BrowserContext::GetDownloadManager( | |
| 143 shell()->web_contents()->GetBrowserContext())->AddObserver(this); | |
| 144 CheckTitleTest(GetMockURL("content-sniffer-test3.html"), | |
| 145 "Content Sniffer Test 3"); | |
| 146 EXPECT_EQ(1u, Shell::windows().size()); | |
| 147 ASSERT_FALSE(got_downloads()); | |
| 148 } | |
| 149 | |
| 150 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 151 ContentDispositionEmpty) { | |
| 152 CheckTitleTest(GetMockURL("content-disposition-empty.html"), "success"); | |
| 153 } | |
| 154 | |
| 155 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 156 ContentDispositionInline) { | |
| 157 CheckTitleTest(GetMockURL("content-disposition-inline.html"), "success"); | |
| 158 } | |
| 159 | |
| 160 // Test for bug #1091358. | |
| 161 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, SyncXMLHttpRequest) { | |
| 162 ASSERT_TRUE(test_server()->Start()); | |
| 163 NavigateToURL( | |
| 164 shell(), test_server()->GetURL("files/sync_xmlhttprequest.html")); | |
| 165 | |
| 166 // Let's check the XMLHttpRequest ran successfully. | |
| 167 bool success = false; | |
| 168 EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( | |
| 169 shell()->web_contents()->GetRenderViewHost(), | |
| 170 L"", | |
| 171 L"window.domAutomationController.send(DidSyncRequestSucceed());", | |
| 172 &success)); | |
| 173 EXPECT_TRUE(success); | |
| 174 } | |
| 175 | |
| 176 // If this flakes, use http://crbug.com/62776. | |
| 177 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 178 SyncXMLHttpRequest_Disallowed) { | |
| 179 ASSERT_TRUE(test_server()->Start()); | |
| 180 NavigateToURL( | |
| 181 shell(), | |
| 182 test_server()->GetURL("files/sync_xmlhttprequest_disallowed.html")); | |
| 183 | |
| 184 // Let's check the XMLHttpRequest ran successfully. | |
| 185 bool success = false; | |
| 186 EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( | |
| 187 shell()->web_contents()->GetRenderViewHost(), | |
| 188 L"", | |
| 189 L"window.domAutomationController.send(DidSucceed());", | |
| 190 &success)); | |
| 191 EXPECT_TRUE(success); | |
| 192 } | |
| 193 | |
| 194 // Test for bug #1159553 -- A synchronous xhr (whose content-type is | |
| 195 // downloadable) would trigger download and hang the renderer process, | |
| 196 // if executed while navigating to a new page. | |
| 197 // If this flakes, use http://crbug.com/56264. | |
| 198 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 199 SyncXMLHttpRequest_DuringUnload) { | |
| 200 ASSERT_TRUE(test_server()->Start()); | |
| 201 BrowserContext::GetDownloadManager( | |
| 202 shell()->web_contents()->GetBrowserContext())->AddObserver(this); | |
| 203 | |
| 204 CheckTitleTest( | |
| 205 test_server()->GetURL("files/sync_xmlhttprequest_during_unload.html"), | |
| 206 "sync xhr on unload"); | |
| 207 | |
| 208 // Navigate to a new page, to dispatch unload event and trigger xhr. | |
| 209 // (the bug would make this step hang the renderer). | |
| 210 CheckTitleTest( | |
| 211 test_server()->GetURL("files/title2.html"), "Title Of Awesomeness"); | |
| 212 | |
| 213 ASSERT_FALSE(got_downloads()); | |
| 214 } | |
| 215 | |
| 216 // Tests that onunload is run for cross-site requests. (Bug 1114994) | |
| 217 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 218 CrossSiteOnunloadCookie) { | |
| 219 ASSERT_TRUE(test_server()->Start()); | |
| 220 | |
| 221 GURL url = test_server()->GetURL("files/onunload_cookie.html"); | |
| 222 CheckTitleTest(url, "set cookie on unload"); | |
| 223 | |
| 224 // Navigate to a new cross-site page, to dispatch unload event and set the | |
| 225 // cookie. | |
| 226 CheckTitleTest(GetMockURL("content-sniffer-test0.html"), | |
| 227 "Content Sniffer Test 0"); | |
| 228 | |
| 229 // Check that the cookie was set. | |
| 230 EXPECT_EQ("onunloadCookie=foo", GetCookies(url)); | |
| 231 } | |
| 232 | |
| 233 // If this flakes, use http://crbug.com/130404 | |
| 234 // Tests that onunload is run for cross-site requests to URLs that complete | |
| 235 // without network loads (e.g., about:blank, data URLs). | |
| 236 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 237 DISABLED_CrossSiteImmediateLoadOnunloadCookie) { | |
| 238 ASSERT_TRUE(test_server()->Start()); | |
| 239 | |
| 240 GURL url = test_server()->GetURL("files/onunload_cookie.html"); | |
| 241 CheckTitleTest(url, "set cookie on unload"); | |
| 242 | |
| 243 // Navigate to a cross-site page that loads immediately without making a | |
| 244 // network request. The unload event should still be run. | |
| 245 NavigateToURL(shell(), GURL("about:blank")); | |
| 246 | |
| 247 // Check that the cookie was set. | |
| 248 EXPECT_EQ("onunloadCookie=foo", GetCookies(url)); | |
| 249 } | |
| 250 | |
| 251 // Tests that the unload handler is not run for 204 responses. | |
| 252 // If this flakes use http://crbug.com/80596. | |
| 253 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 254 CrossSiteNoUnloadOn204) { | |
| 255 ASSERT_TRUE(test_server()->Start()); | |
| 256 | |
| 257 // Start with a URL that sets a cookie in its unload handler. | |
| 258 GURL url = test_server()->GetURL("files/onunload_cookie.html"); | |
| 259 CheckTitleTest(url, "set cookie on unload"); | |
| 260 | |
| 261 // Navigate to a cross-site URL that returns a 204 No Content response. | |
| 262 NavigateToURL(shell(), test_server()->GetURL("nocontent")); | |
| 263 | |
| 264 // Check that the unload cookie was not set. | |
| 265 EXPECT_EQ("", GetCookies(url)); | |
| 266 } | |
| 267 | |
| 268 #if !defined(OS_MACOSX) | |
| 269 // Tests that the onbeforeunload and onunload logic is short-circuited if the | |
| 270 // old renderer is gone. In that case, we don't want to wait for the old | |
| 271 // renderer to run the handlers. | |
| 272 // We need to disable this on Mac because the crash causes the OS CrashReporter | |
| 273 // process to kick in to analyze the poor dead renderer. Unfortunately, if the | |
| 274 // app isn't stripped of debug symbols, this takes about five minutes to | |
| 275 // complete and isn't conducive to quick turnarounds. As we don't currently | |
| 276 // strip the app on the build bots, this is bad times. | |
| 277 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, CrossSiteAfterCrash) { | |
| 278 // Cause the renderer to crash. | |
| 279 WindowedNotificationObserver crash_observer( | |
| 280 NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 281 NotificationService::AllSources()); | |
| 282 NavigateToURL(shell(), GURL(chrome::kChromeUICrashURL)); | |
| 283 // Wait for browser to notice the renderer crash. | |
| 284 crash_observer.Wait(); | |
| 285 | |
| 286 // Navigate to a new cross-site page. The browser should not wait around for | |
| 287 // the old renderer's on{before}unload handlers to run. | |
| 288 CheckTitleTest(GetMockURL("content-sniffer-test0.html"), | |
| 289 "Content Sniffer Test 0"); | |
| 290 } | |
| 291 #endif // !defined(OS_MACOSX) | |
| 292 | |
| 293 // Tests that cross-site navigations work when the new page does not go through | |
| 294 // the BufferedEventHandler (e.g., non-http{s} URLs). (Bug 1225872) | |
| 295 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 296 CrossSiteNavigationNonBuffered) { | |
| 297 // Start with an HTTP page. | |
| 298 CheckTitleTest(GetMockURL("content-sniffer-test0.html"), | |
| 299 "Content Sniffer Test 0"); | |
| 300 | |
| 301 // Now load a file:// page, which does not use the BufferedEventHandler. | |
| 302 // Make sure that the page loads and displays a title, and doesn't get stuck. | |
| 303 GURL url = GetTestUrl("", "title2.html"); | |
| 304 CheckTitleTest(url, "Title Of Awesomeness"); | |
| 305 } | |
| 306 | |
| 307 // Tests that a cross-site navigation to an error page (resulting in the link | |
| 308 // doctor page) still runs the onunload handler and can support navigations | |
| 309 // away from the link doctor page. (Bug 1235537) | |
| 310 // Flaky: http://crbug.com/100823 | |
| 311 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 312 CrossSiteNavigationErrorPage) { | |
| 313 ASSERT_TRUE(test_server()->Start()); | |
| 314 | |
| 315 GURL url(test_server()->GetURL("files/onunload_cookie.html")); | |
| 316 CheckTitleTest(url, "set cookie on unload"); | |
| 317 | |
| 318 // Navigate to a new cross-site URL that results in an error. | |
| 319 // TODO(creis): If this causes crashes or hangs, it might be for the same | |
| 320 // reason as ErrorPageTest::DNSError. See bug 1199491 and | |
| 321 // http://crbug.com/22877. | |
| 322 GURL failed_url = URLRequestFailedJob::GetMockHttpUrl( | |
| 323 net::ERR_NAME_NOT_RESOLVED); | |
| 324 NavigateToURL(shell(), failed_url); | |
| 325 | |
| 326 EXPECT_NE(ASCIIToUTF16("set cookie on unload"), | |
| 327 shell()->web_contents()->GetTitle()); | |
| 328 | |
| 329 // Check that the cookie was set, meaning that the onunload handler ran. | |
| 330 EXPECT_EQ("onunloadCookie=foo", GetCookies(url)); | |
| 331 | |
| 332 // Check that renderer-initiated navigations still work. In a previous bug, | |
| 333 // the ResourceDispatcherHost would think that such navigations were | |
| 334 // cross-site, because we didn't clean up from the previous request. Since | |
| 335 // WebContentsImpl was in the NORMAL state, it would ignore the attempt to run | |
| 336 // the onunload handler, and the navigation would fail. We can't test by | |
| 337 // redirecting to javascript:window.location='someURL', since javascript: | |
| 338 // URLs are prohibited by policy from interacting with sensitive chrome | |
| 339 // pages of which the error page is one. Instead, use automation to kick | |
| 340 // off the navigation, and wait to see that the tab loads. | |
| 341 string16 expected_title16(ASCIIToUTF16("Title Of Awesomeness")); | |
| 342 TitleWatcher title_watcher(shell()->web_contents(), expected_title16); | |
| 343 | |
| 344 bool success; | |
| 345 GURL test_url(test_server()->GetURL("files/title2.html")); | |
| 346 std::string redirect_script = "window.location='" + | |
| 347 test_url.possibly_invalid_spec() + "';" + | |
| 348 "window.domAutomationController.send(true);"; | |
| 349 EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( | |
| 350 shell()->web_contents()->GetRenderViewHost(), | |
| 351 L"", ASCIIToWide(redirect_script), &success)); | |
| 352 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); | |
| 353 } | |
| 354 | |
| 355 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 356 CrossSiteNavigationErrorPage2) { | |
| 357 ASSERT_TRUE(test_server()->Start()); | |
| 358 | |
| 359 GURL url(test_server()->GetURL("files/title2.html")); | |
| 360 CheckTitleTest(url, "Title Of Awesomeness"); | |
| 361 | |
| 362 // Navigate to a new cross-site URL that results in an error. | |
| 363 // TODO(creis): If this causes crashes or hangs, it might be for the same | |
| 364 // reason as ErrorPageTest::DNSError. See bug 1199491 and | |
| 365 // http://crbug.com/22877. | |
| 366 GURL failed_url = URLRequestFailedJob::GetMockHttpUrl( | |
| 367 net::ERR_NAME_NOT_RESOLVED); | |
| 368 | |
| 369 NavigateToURL(shell(), failed_url); | |
| 370 EXPECT_NE(ASCIIToUTF16("Title Of Awesomeness"), | |
| 371 shell()->web_contents()->GetTitle()); | |
| 372 | |
| 373 // Repeat navigation. We are testing that this completes. | |
| 374 NavigateToURL(shell(), failed_url); | |
| 375 EXPECT_NE(ASCIIToUTF16("Title Of Awesomeness"), | |
| 376 shell()->web_contents()->GetTitle()); | |
| 377 } | |
| 378 | |
| 379 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 380 CrossOriginRedirectBlocked) { | |
| 381 // We expect the following URL requests from this test: | |
| 382 // 1- http://mock.http/cross-origin-redirect-blocked.html | |
| 383 // 2- http://mock.http/redirect-to-title2.html | |
| 384 // 3- http://mock.http/title2.html | |
| 385 // | |
| 386 // If the redirect in #2 were not blocked, we'd also see a request | |
| 387 // for http://mock.http:4000/title2.html, and the title would be different. | |
| 388 CheckTitleTest(GetMockURL("cross-origin-redirect-blocked.html"), | |
| 389 "Title Of More Awesomeness"); | |
| 390 } | |
| 391 | |
| 392 // Tests that ResourceRequestInfoImpl is updated correctly on failed | |
| 393 // requests, to prevent calling Read on a request that has already failed. | |
| 394 // See bug 40250. | |
| 395 IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, | |
| 396 CrossSiteFailedRequest) { | |
| 397 // Visit another URL first to trigger a cross-site navigation. | |
| 398 NavigateToURL(shell(), GetTestUrl("", "simple_page.html")); | |
| 399 | |
| 400 // Visit a URL that fails without calling ResourceDispatcherHost::Read. | |
| 401 GURL broken_url("chrome://theme"); | |
| 402 NavigateToURL(shell(), broken_url); | |
| 403 } | |
| 404 | |
| 405 } // namespace content | |
| OLD | NEW |