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/prefs/pref_service.h" | 6 #include "base/prefs/pref_service.h" |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/google/google_util.h" | 9 #include "chrome/browser/google/google_util.h" |
10 #include "chrome/browser/net/url_request_mock_util.h" | 10 #include "chrome/browser/net/url_request_mock_util.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/browser_commands.h" | 13 #include "chrome/browser/ui/browser_commands.h" |
14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
16 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
17 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
19 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
21 #include "content/public/browser/web_contents_observer.h" | 21 #include "content/public/browser/web_contents_observer.h" |
22 #include "content/public/test/browser_test_utils.h" | 22 #include "content/public/test/browser_test_utils.h" |
23 #include "content/public/test/test_navigation_observer.h" | 23 #include "content/public/test/test_navigation_observer.h" |
24 #include "content/test/net/url_request_failed_job.h" | 24 #include "content/test/net/url_request_failed_job.h" |
25 #include "content/test/net/url_request_mock_http_job.h" | 25 #include "content/test/net/url_request_mock_http_job.h" |
26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
27 #include "net/base/net_util.h" | 27 #include "net/base/net_util.h" |
28 #include "net/url_request/url_request_filter.h" | 28 #include "net/url_request/url_request_filter.h" |
| 29 #include "net/url_request/url_request_job.h" |
29 #include "net/url_request/url_request_job_factory.h" | 30 #include "net/url_request/url_request_job_factory.h" |
| 31 #include "net/url_request/url_request_test_job.h" |
| 32 #include "net/url_request/url_request_test_util.h" |
30 | 33 |
31 using content::BrowserThread; | 34 using content::BrowserThread; |
32 using content::NavigationController; | 35 using content::NavigationController; |
33 using content::URLRequestFailedJob; | 36 using content::URLRequestFailedJob; |
| 37 using net::URLRequestTestJob; |
| 38 using net::TestJobInterceptor; |
| 39 |
| 40 class TestProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { |
| 41 public: |
| 42 TestProtocolHandler(const GURL& url, int failure_threshold) |
| 43 : url_(url), requests_(0), failures_(0), |
| 44 failure_threshold_(failure_threshold) {} |
| 45 virtual ~TestProtocolHandler() {} |
| 46 |
| 47 virtual net::URLRequestJob* MaybeCreateJob( |
| 48 net::URLRequest* request, |
| 49 net::NetworkDelegate* network_delegate) const { |
| 50 if (request->url() != url_) |
| 51 return NULL; |
| 52 requests_++; |
| 53 if (failures_ < failure_threshold_) { |
| 54 failures_++; |
| 55 return new URLRequestFailedJob(request, |
| 56 network_delegate, |
| 57 net::ERR_CONNECTION_RESET); |
| 58 } else { |
| 59 return new URLRequestTestJob(request, network_delegate, |
| 60 URLRequestTestJob::test_headers(), |
| 61 URLRequestTestJob::test_data_1(), |
| 62 true); |
| 63 } |
| 64 } |
| 65 |
| 66 int requests() const { return requests_; } |
| 67 int failures() const { return failures_; } |
| 68 int threshold() const { return failure_threshold_; } |
| 69 |
| 70 private: |
| 71 GURL url_; |
| 72 // these are mutable because MaybeCreateJob is const but we want this state |
| 73 // for testing. |
| 74 mutable int requests_; |
| 75 mutable int failures_; |
| 76 int failure_threshold_; |
| 77 }; |
34 | 78 |
35 namespace { | 79 namespace { |
36 | 80 |
37 class ErrorPageTest : public InProcessBrowserTest { | 81 class ErrorPageTest : public InProcessBrowserTest { |
38 public: | 82 public: |
39 enum HistoryNavigationDirection { | 83 enum HistoryNavigationDirection { |
40 HISTORY_NAVIGATE_BACK, | 84 HISTORY_NAVIGATE_BACK, |
41 HISTORY_NAVIGATE_FORWARD, | 85 HISTORY_NAVIGATE_FORWARD, |
42 }; | 86 }; |
43 | 87 |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 | 393 |
350 // Checks that the Link Doctor is not loaded when we receive an actual 404 page. | 394 // Checks that the Link Doctor is not loaded when we receive an actual 404 page. |
351 IN_PROC_BROWSER_TEST_F(ErrorPageTest, Page404) { | 395 IN_PROC_BROWSER_TEST_F(ErrorPageTest, Page404) { |
352 NavigateToURLAndWaitForTitle( | 396 NavigateToURLAndWaitForTitle( |
353 content::URLRequestMockHTTPJob::GetMockUrl( | 397 content::URLRequestMockHTTPJob::GetMockUrl( |
354 base::FilePath(FILE_PATH_LITERAL("page404.html"))), | 398 base::FilePath(FILE_PATH_LITERAL("page404.html"))), |
355 "SUCCESS", | 399 "SUCCESS", |
356 1); | 400 1); |
357 } | 401 } |
358 | 402 |
| 403 class ErrorPageAutoReloadTest : public InProcessBrowserTest { |
| 404 public: |
| 405 virtual void SetUpOnMainThread() OVERRIDE { |
| 406 BrowserThread::PostTask( |
| 407 BrowserThread::IO, FROM_HERE, |
| 408 base::Bind(&ErrorPageAutoReloadTest::AddFilters)); |
| 409 } |
| 410 |
| 411 virtual void CleanUpOnMainThread() OVERRIDE { |
| 412 BrowserThread::PostTask( |
| 413 BrowserThread::IO, FROM_HERE, |
| 414 base::Bind(&ErrorPageAutoReloadTest::RemoveFilters)); |
| 415 } |
| 416 |
| 417 protected: |
| 418 void NavigateToURLAndWaitForTitle(const GURL& url, |
| 419 const std::string& expected_title, |
| 420 int num_navigations) { |
| 421 content::TitleWatcher title_watcher( |
| 422 browser()->tab_strip_model()->GetActiveWebContents(), |
| 423 base::ASCIIToUTF16(expected_title)); |
| 424 |
| 425 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 426 browser(), url, num_navigations); |
| 427 |
| 428 EXPECT_EQ(base::ASCIIToUTF16(expected_title), |
| 429 title_watcher.WaitAndGetTitle()); |
| 430 } |
| 431 |
| 432 static std::string test_schema() { return "http"; } |
| 433 static std::string test_domain() { return "error.page.auto.reload"; } |
| 434 static GURL test_url() { |
| 435 return GURL(test_schema() + "://" + test_domain()); |
| 436 } |
| 437 static TestProtocolHandler* protocol_handler() { return protocol_handler_; } |
| 438 |
| 439 private: |
| 440 static void AddFilters() { |
| 441 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 442 |
| 443 content::URLRequestFailedJob::AddUrlHandler(); |
| 444 protocol_handler_ = new TestProtocolHandler(test_url(), 2); |
| 445 // pass ownership of the pointer to the URLRequestFilter, which outlives |
| 446 // this object, but keep a copy of it so tests can query its state. |
| 447 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> |
| 448 scoped_thing(protocol_handler_); |
| 449 |
| 450 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( |
| 451 test_schema(), test_domain(), scoped_thing.Pass()); |
| 452 } |
| 453 |
| 454 static void RemoveFilters() { |
| 455 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 456 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| 457 } |
| 458 |
| 459 static TestProtocolHandler* protocol_handler_; |
| 460 |
| 461 }; |
| 462 |
| 463 TestProtocolHandler* ErrorPageAutoReloadTest::protocol_handler_ = NULL; |
| 464 |
| 465 IN_PROC_BROWSER_TEST_F(ErrorPageAutoReloadTest, AutoReload) { |
| 466 NavigateToURLAndWaitForTitle(test_url(), "Test One", 3); |
| 467 EXPECT_EQ(protocol_handler()->failures(), protocol_handler()->threshold()); |
| 468 EXPECT_EQ(protocol_handler()->requests(), |
| 469 protocol_handler()->threshold() + 1); |
| 470 } |
| 471 |
359 // Returns Javascript code that executes plain text search for the page. | 472 // Returns Javascript code that executes plain text search for the page. |
360 // Pass into content::ExecuteScriptAndExtractBool as |script| parameter. | 473 // Pass into content::ExecuteScriptAndExtractBool as |script| parameter. |
361 std::string GetTextContentContainsStringScript( | 474 std::string GetTextContentContainsStringScript( |
362 const std::string& value_to_search) { | 475 const std::string& value_to_search) { |
363 return base::StringPrintf( | 476 return base::StringPrintf( |
364 "var textContent = document.body.textContent;" | 477 "var textContent = document.body.textContent;" |
365 "var hasError = textContent.indexOf('%s') >= 0;" | 478 "var hasError = textContent.indexOf('%s') >= 0;" |
366 "domAutomationController.send(hasError);", | 479 "domAutomationController.send(hasError);", |
367 value_to_search.c_str()); | 480 value_to_search.c_str()); |
368 } | 481 } |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 | 612 |
500 bool result = false; | 613 bool result = false; |
501 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | 614 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
502 browser()->tab_strip_model()->GetActiveWebContents(), | 615 browser()->tab_strip_model()->GetActiveWebContents(), |
503 GetTextContentContainsStringScript(kHostnameJSUnicode), | 616 GetTextContentContainsStringScript(kHostnameJSUnicode), |
504 &result)); | 617 &result)); |
505 EXPECT_TRUE(result); | 618 EXPECT_TRUE(result); |
506 } | 619 } |
507 | 620 |
508 } // namespace | 621 } // namespace |
OLD | NEW |