Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" | |
| 6 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 7 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/google/google_util.h" | 10 #include "chrome/browser/google/google_util.h" |
| 10 #include "chrome/browser/net/url_request_mock_util.h" | 11 #include "chrome/browser/net/url_request_mock_util.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_commands.h" | 14 #include "chrome/browser/ui/browser_commands.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 16 #include "chrome/common/chrome_switches.h" | |
| 15 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/test/base/in_process_browser_test.h" | 18 #include "chrome/test/base/in_process_browser_test.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/render_view_host.h" | 21 #include "content/public/browser/render_view_host.h" |
| 20 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/browser/web_contents_observer.h" | 23 #include "content/public/browser/web_contents_observer.h" |
| 22 #include "content/public/test/browser_test_utils.h" | 24 #include "content/public/test/browser_test_utils.h" |
| 23 #include "content/public/test/test_navigation_observer.h" | 25 #include "content/public/test/test_navigation_observer.h" |
| 24 #include "content/test/net/url_request_failed_job.h" | 26 #include "content/test/net/url_request_failed_job.h" |
| 25 #include "content/test/net/url_request_mock_http_job.h" | 27 #include "content/test/net/url_request_mock_http_job.h" |
| 26 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 27 #include "net/base/net_util.h" | 29 #include "net/base/net_util.h" |
| 28 #include "net/url_request/url_request_filter.h" | 30 #include "net/url_request/url_request_filter.h" |
| 31 #include "net/url_request/url_request_job.h" | |
| 29 #include "net/url_request/url_request_job_factory.h" | 32 #include "net/url_request/url_request_job_factory.h" |
| 33 #include "net/url_request/url_request_test_job.h" | |
| 34 #include "net/url_request/url_request_test_util.h" | |
| 30 | 35 |
| 31 using content::BrowserThread; | 36 using content::BrowserThread; |
| 32 using content::NavigationController; | 37 using content::NavigationController; |
| 33 using content::URLRequestFailedJob; | 38 using content::URLRequestFailedJob; |
| 39 using net::TestJobInterceptor; | |
| 40 using net::URLRequestJobFactory; | |
| 41 using net::URLRequestTestJob; | |
| 42 | |
| 43 // A protocol handler that fails a configurable number of requests, then | |
| 44 // succeeds all requests after that, keeping count of failures and successes. | |
| 45 class FailFirstNRequestsProtocolHandler | |
|
mmenke
2014/02/25 22:09:52
This should be in an anonymous namespace below.
Elly Fong-Jones
2014/03/03 19:31:07
Done.
| |
| 46 : public URLRequestJobFactory::ProtocolHandler { | |
| 47 public: | |
| 48 FailFirstNRequestsProtocolHandler(const GURL& url, int requests_to_fail) | |
| 49 : url_(url), requests_(0), failures_(0), | |
| 50 requests_to_fail_(requests_to_fail) {} | |
| 51 virtual ~FailFirstNRequestsProtocolHandler() {} | |
| 52 | |
| 53 void AddUrlHandler() { | |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
|
mmenke
2014/02/25 22:09:52
Should include base/logging.h for DCHECK
Elly Fong-Jones
2014/03/03 19:31:07
Done.
| |
| 55 scoped_ptr<URLRequestJobFactory::ProtocolHandler> scoped_handler(this); | |
|
mmenke
2014/02/25 22:09:52
Should also probably include the header for scoped
Elly Fong-Jones
2014/03/03 19:31:07
Done.
| |
| 56 net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler(url_, | |
| 57 scoped_handler.Pass()); | |
| 58 } | |
| 59 | |
| 60 virtual net::URLRequestJob* MaybeCreateJob( | |
| 61 net::URLRequest* request, | |
| 62 net::NetworkDelegate* network_delegate) OVERRIDE const { | |
| 63 if (request->url() != url_) | |
| 64 return NULL; | |
|
mmenke
2014/02/25 22:09:52
This shouldn't be needed. Maybe a DCHECK_EQ inste
Elly Fong-Jones
2014/03/03 19:31:07
Done.
| |
| 65 requests_++; | |
| 66 if (failures_ < requests_to_fail_) { | |
| 67 failures_++; | |
| 68 return new URLRequestFailedJob(request, | |
| 69 network_delegate, | |
| 70 net::ERR_CONNECTION_RESET); | |
| 71 } else { | |
| 72 return new URLRequestTestJob(request, network_delegate, | |
| 73 URLRequestTestJob::test_headers(), | |
| 74 URLRequestTestJob::test_data_1(), | |
| 75 true); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 int requests() const { return requests_; } | |
| 80 int failures() const { return failures_; } | |
| 81 int requests_to_fail() const { return requests_to_fail_; } | |
| 82 | |
| 83 private: | |
| 84 GURL url_; | |
|
mmenke
2014/02/25 22:09:52
maybe const?
Elly Fong-Jones
2014/03/03 19:31:07
Done.
| |
| 85 // these are mutable because MaybeCreateJob is const but we want this state | |
| 86 // for testing. | |
| 87 mutable int requests_; | |
| 88 mutable int failures_; | |
| 89 int requests_to_fail_; | |
| 90 }; | |
| 34 | 91 |
| 35 namespace { | 92 namespace { |
| 36 | 93 |
| 37 class ErrorPageTest : public InProcessBrowserTest { | 94 class ErrorPageTest : public InProcessBrowserTest { |
| 38 public: | 95 public: |
| 39 enum HistoryNavigationDirection { | 96 enum HistoryNavigationDirection { |
| 40 HISTORY_NAVIGATE_BACK, | 97 HISTORY_NAVIGATE_BACK, |
| 41 HISTORY_NAVIGATE_FORWARD, | 98 HISTORY_NAVIGATE_FORWARD, |
| 42 }; | 99 }; |
| 43 | 100 |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 349 | 406 |
| 350 // Checks that the Link Doctor is not loaded when we receive an actual 404 page. | 407 // Checks that the Link Doctor is not loaded when we receive an actual 404 page. |
| 351 IN_PROC_BROWSER_TEST_F(ErrorPageTest, Page404) { | 408 IN_PROC_BROWSER_TEST_F(ErrorPageTest, Page404) { |
| 352 NavigateToURLAndWaitForTitle( | 409 NavigateToURLAndWaitForTitle( |
| 353 content::URLRequestMockHTTPJob::GetMockUrl( | 410 content::URLRequestMockHTTPJob::GetMockUrl( |
| 354 base::FilePath(FILE_PATH_LITERAL("page404.html"))), | 411 base::FilePath(FILE_PATH_LITERAL("page404.html"))), |
| 355 "SUCCESS", | 412 "SUCCESS", |
| 356 1); | 413 1); |
| 357 } | 414 } |
| 358 | 415 |
| 416 class ErrorPageAutoReloadTest : public InProcessBrowserTest { | |
| 417 public: | |
| 418 virtual void SetUpCommandLine(CommandLine* command_line) { | |
| 419 LOG(ERROR) << "SetUpCommandLine"; | |
| 420 command_line->AppendSwitch(switches::kEnableOfflineAutoReload); | |
| 421 } | |
| 422 | |
| 423 void InstallProtocolHandler(const GURL& url, int requests_to_fail) { | |
| 424 protocol_handler_ = new FailFirstNRequestsProtocolHandler(url, | |
| 425 requests_to_fail); | |
| 426 // Tests don't need to wait for this task to complete before using the | |
| 427 // filter; any requests that might be affected by it will end up in the IO | |
| 428 // thread's message loop after this posted task anyway. | |
| 429 BrowserThread::PostTask( | |
| 430 BrowserThread::IO, FROM_HERE, | |
| 431 base::Bind(&ErrorPageAutoReloadTest::AddFilters, | |
| 432 base::Unretained(this))); | |
| 433 } | |
| 434 | |
| 435 protected: | |
|
mmenke
2014/02/25 22:09:52
nit: I don't think protected really matters for t
Elly Fong-Jones
2014/03/03 19:31:07
Done.
| |
| 436 void NavigateToURLAndWaitForTitle(const GURL& url, | |
| 437 const std::string& expected_title, | |
| 438 int num_navigations) { | |
| 439 content::TitleWatcher title_watcher( | |
| 440 browser()->tab_strip_model()->GetActiveWebContents(), | |
| 441 base::ASCIIToUTF16(expected_title)); | |
| 442 | |
| 443 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | |
| 444 browser(), url, num_navigations); | |
| 445 | |
| 446 EXPECT_EQ(base::ASCIIToUTF16(expected_title), | |
| 447 title_watcher.WaitAndGetTitle()); | |
| 448 } | |
| 449 | |
| 450 FailFirstNRequestsProtocolHandler* protocol_handler() { | |
| 451 return protocol_handler_; | |
| 452 } | |
| 453 | |
| 454 private: | |
| 455 void AddFilters() { | |
| 456 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 457 protocol_handler_->AddUrlHandler(); | |
| 458 } | |
| 459 | |
| 460 void RemoveFilters() { | |
| 461 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 462 net::URLRequestFilter::GetInstance()->ClearHandlers(); | |
| 463 } | |
| 464 | |
| 465 FailFirstNRequestsProtocolHandler* protocol_handler_; | |
| 466 }; | |
| 467 | |
| 468 IN_PROC_BROWSER_TEST_F(ErrorPageAutoReloadTest, AutoReload) { | |
|
mmenke
2014/02/25 22:09:52
If we could do it non-racily, I'd love to have a t
Elly Fong-Jones
2014/03/03 19:31:07
Yeah. I thought about this, but my mind recoiled,
| |
| 469 GURL test_url("http://error.page.auto.reload"); | |
| 470 InstallProtocolHandler(test_url, 2); | |
| 471 NavigateToURLAndWaitForTitle(test_url, "Test One", 3); | |
| 472 EXPECT_EQ(protocol_handler()->failures(), | |
| 473 protocol_handler()->requests_to_fail()); | |
| 474 EXPECT_EQ(protocol_handler()->requests(), | |
| 475 protocol_handler()->requests_to_fail() + 1); | |
|
mmenke
2014/02/25 22:09:52
nit: Both of these pairs are in the wrong order.
mmenke
2014/02/25 22:09:52
nit: I think this test is clearer if you have:
"c
mmenke
2014/02/25 22:09:52
I don't see a comment about it. I just think it's
Elly Fong-Jones
2014/03/03 19:31:07
Done.
Elly Fong-Jones
2014/03/03 19:31:07
Done.
Elly Fong-Jones
2014/03/03 19:31:07
Done.
| |
| 476 } | |
| 477 | |
| 359 // Returns Javascript code that executes plain text search for the page. | 478 // Returns Javascript code that executes plain text search for the page. |
| 360 // Pass into content::ExecuteScriptAndExtractBool as |script| parameter. | 479 // Pass into content::ExecuteScriptAndExtractBool as |script| parameter. |
| 361 std::string GetTextContentContainsStringScript( | 480 std::string GetTextContentContainsStringScript( |
| 362 const std::string& value_to_search) { | 481 const std::string& value_to_search) { |
| 363 return base::StringPrintf( | 482 return base::StringPrintf( |
| 364 "var textContent = document.body.textContent;" | 483 "var textContent = document.body.textContent;" |
| 365 "var hasError = textContent.indexOf('%s') >= 0;" | 484 "var hasError = textContent.indexOf('%s') >= 0;" |
| 366 "domAutomationController.send(hasError);", | 485 "domAutomationController.send(hasError);", |
| 367 value_to_search.c_str()); | 486 value_to_search.c_str()); |
| 368 } | 487 } |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 | 618 |
| 500 bool result = false; | 619 bool result = false; |
| 501 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | 620 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 502 browser()->tab_strip_model()->GetActiveWebContents(), | 621 browser()->tab_strip_model()->GetActiveWebContents(), |
| 503 GetTextContentContainsStringScript(kHostnameJSUnicode), | 622 GetTextContentContainsStringScript(kHostnameJSUnicode), |
| 504 &result)); | 623 &result)); |
| 505 EXPECT_TRUE(result); | 624 EXPECT_TRUE(result); |
| 506 } | 625 } |
| 507 | 626 |
| 508 } // namespace | 627 } // namespace |
| OLD | NEW |