Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: chrome/browser/errorpage_browsertest.cc

Issue 136203009: Support auto-reload on errors. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: More fixes Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
6 #include "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
7 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browsing_data/browsing_data_helper.h" 12 #include "chrome/browser/browsing_data/browsing_data_helper.h"
10 #include "chrome/browser/browsing_data/browsing_data_remover.h" 13 #include "chrome/browser/browsing_data/browsing_data_remover.h"
11 #include "chrome/browser/google/google_util.h" 14 #include "chrome/browser/google/google_util.h"
12 #include "chrome/browser/net/url_request_mock_util.h" 15 #include "chrome/browser/net/url_request_mock_util.h"
13 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_commands.h" 18 #include "chrome/browser/ui/browser_commands.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" 19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/in_process_browser_test.h" 22 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/ui_test_utils.h" 23 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/browser/notification_service.h" 24 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/render_view_host.h" 25 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_contents_observer.h" 27 #include "content/public/browser/web_contents_observer.h"
24 #include "content/public/test/browser_test_utils.h" 28 #include "content/public/test/browser_test_utils.h"
25 #include "content/public/test/test_navigation_observer.h" 29 #include "content/public/test/test_navigation_observer.h"
26 #include "content/test/net/url_request_failed_job.h" 30 #include "content/test/net/url_request_failed_job.h"
27 #include "content/test/net/url_request_mock_http_job.h" 31 #include "content/test/net/url_request_mock_http_job.h"
28 #include "net/base/net_errors.h" 32 #include "net/base/net_errors.h"
29 #include "net/base/net_util.h" 33 #include "net/base/net_util.h"
30 #include "net/http/failing_http_transaction_factory.h" 34 #include "net/http/failing_http_transaction_factory.h"
31 #include "net/http/http_cache.h" 35 #include "net/http/http_cache.h"
32 #include "net/test/spawned_test_server/spawned_test_server.h" 36 #include "net/test/spawned_test_server/spawned_test_server.h"
33 #include "net/url_request/url_request_context.h" 37 #include "net/url_request/url_request_context.h"
34 #include "net/url_request/url_request_context_getter.h" 38 #include "net/url_request/url_request_context_getter.h"
35 #include "net/url_request/url_request_filter.h" 39 #include "net/url_request/url_request_filter.h"
40 #include "net/url_request/url_request_job.h"
36 #include "net/url_request/url_request_job_factory.h" 41 #include "net/url_request/url_request_job_factory.h"
42 #include "net/url_request/url_request_test_job.h"
43 #include "net/url_request/url_request_test_util.h"
37 44
38 using content::BrowserThread; 45 using content::BrowserThread;
39 using content::NavigationController; 46 using content::NavigationController;
40 using content::URLRequestFailedJob; 47 using content::URLRequestFailedJob;
48 using net::URLRequestJobFactory;
49 using net::URLRequestTestJob;
41 50
42 namespace { 51 namespace {
43 52
53 // A protocol handler that fails a configurable number of requests, then
54 // succeeds all requests after that, keeping count of failures and successes.
55 class FailFirstNRequestsProtocolHandler
56 : public URLRequestJobFactory::ProtocolHandler {
57 public:
58 FailFirstNRequestsProtocolHandler(const GURL& url, int requests_to_fail)
59 : url_(url), requests_(0), failures_(0),
60 requests_to_fail_(requests_to_fail) {}
61 virtual ~FailFirstNRequestsProtocolHandler() {}
62
63 void AddUrlHandler() {
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
65 scoped_ptr<URLRequestJobFactory::ProtocolHandler> scoped_handler(this);
66 net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler(
67 url_,
68 scoped_handler.Pass());
69 }
70
71 virtual net::URLRequestJob* MaybeCreateJob(
72 net::URLRequest* request,
73 net::NetworkDelegate* network_delegate) OVERRIDE const {
74 DCHECK_EQ(url_, request->url());
75 requests_++;
76 if (failures_ < requests_to_fail_) {
77 failures_++;
78 // Note: net::ERR_CONNECTION_RESET does not summon the Link Doctor; see
79 // NetErrorHelperCore::GetErrorPageURL.
80 return new URLRequestFailedJob(request,
81 network_delegate,
82 net::ERR_CONNECTION_RESET);
83 } else {
84 return new URLRequestTestJob(request, network_delegate,
85 URLRequestTestJob::test_headers(),
86 URLRequestTestJob::test_data_1(),
87 true);
88 }
89 }
90
91 int requests() const { return requests_; }
92 int failures() const { return failures_; }
93
94 private:
95 const GURL url_;
96 // These are mutable because MaybeCreateJob is const but we want this state
97 // for testing.
98 mutable int requests_;
99 mutable int failures_;
100 int requests_to_fail_;
101 };
102
44 class ErrorPageTest : public InProcessBrowserTest { 103 class ErrorPageTest : public InProcessBrowserTest {
45 public: 104 public:
46 enum HistoryNavigationDirection { 105 enum HistoryNavigationDirection {
47 HISTORY_NAVIGATE_BACK, 106 HISTORY_NAVIGATE_BACK,
48 HISTORY_NAVIGATE_FORWARD, 107 HISTORY_NAVIGATE_FORWARD,
49 }; 108 };
50 109
51 // Navigates the active tab to a mock url created for the file at |file_path|. 110 // Navigates the active tab to a mock url created for the file at |file_path|.
52 void NavigateToFileURL(const base::FilePath::StringType& file_path) { 111 void NavigateToFileURL(const base::FilePath::StringType& file_path) {
53 ui_test_utils::NavigateToURL( 112 ui_test_utils::NavigateToURL(
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 // that there is no cached copy. 494 // that there is no cached copy.
436 BrowsingDataRemover* remover = 495 BrowsingDataRemover* remover =
437 BrowsingDataRemover::CreateForUnboundedRange(browser()->profile()); 496 BrowsingDataRemover::CreateForUnboundedRange(browser()->profile());
438 remover->Remove(BrowsingDataRemover::REMOVE_CACHE, 497 remover->Remove(BrowsingDataRemover::REMOVE_CACHE,
439 BrowsingDataHelper::UNPROTECTED_WEB); 498 BrowsingDataHelper::UNPROTECTED_WEB);
440 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( 499 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
441 browser(), test_url, 1); 500 browser(), test_url, 1);
442 EXPECT_TRUE(ProbeStaleCopyValue(false)); 501 EXPECT_TRUE(ProbeStaleCopyValue(false));
443 } 502 }
444 503
504 class ErrorPageAutoReloadTest : public InProcessBrowserTest {
505 public:
506 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
507 command_line->AppendSwitch(switches::kEnableOfflineAutoReload);
508 }
509
510 void InstallProtocolHandler(const GURL& url, int requests_to_fail) {
511 protocol_handler_ = new FailFirstNRequestsProtocolHandler(
512 url,
513 requests_to_fail);
514 // Tests don't need to wait for this task to complete before using the
515 // filter; any requests that might be affected by it will end up in the IO
516 // thread's message loop after this posted task anyway.
517 BrowserThread::PostTask(
518 BrowserThread::IO, FROM_HERE,
519 base::Bind(&ErrorPageAutoReloadTest::AddFilters,
520 base::Unretained(this)));
521 }
522
523 void NavigateToURLAndWaitForTitle(const GURL& url,
524 const std::string& expected_title,
525 int num_navigations) {
526 content::TitleWatcher title_watcher(
527 browser()->tab_strip_model()->GetActiveWebContents(),
528 base::ASCIIToUTF16(expected_title));
529
530 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
531 browser(), url, num_navigations);
532
533 EXPECT_EQ(base::ASCIIToUTF16(expected_title),
534 title_watcher.WaitAndGetTitle());
535 }
536
537 FailFirstNRequestsProtocolHandler* protocol_handler() {
538 return protocol_handler_;
539 }
540
541 private:
542 void AddFilters() {
543 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
544 protocol_handler_->AddUrlHandler();
545 }
546
547 FailFirstNRequestsProtocolHandler* protocol_handler_;
548 };
549
550 IN_PROC_BROWSER_TEST_F(ErrorPageAutoReloadTest, AutoReload) {
551 GURL test_url("http://error.page.auto.reload");
552 const int kRequestsToFail = 2;
553 InstallProtocolHandler(test_url, kRequestsToFail);
554 NavigateToURLAndWaitForTitle(test_url, "Test One", kRequestsToFail + 1);
555 // Note that the protocol handler updates these variables on the IO thread,
556 // but this function reads them on the main thread. The requests have to be
557 // created (on the IO thread) before NavigateToURLAndWaitForTitle returns or
558 // this becomes racey.
559 EXPECT_EQ(kRequestsToFail, protocol_handler()->failures());
560 EXPECT_EQ(kRequestsToFail + 1, protocol_handler()->requests());
561 }
562
445 // Returns Javascript code that executes plain text search for the page. 563 // Returns Javascript code that executes plain text search for the page.
446 // Pass into content::ExecuteScriptAndExtractBool as |script| parameter. 564 // Pass into content::ExecuteScriptAndExtractBool as |script| parameter.
447 std::string GetTextContentContainsStringScript( 565 std::string GetTextContentContainsStringScript(
448 const std::string& value_to_search) { 566 const std::string& value_to_search) {
449 return base::StringPrintf( 567 return base::StringPrintf(
450 "var textContent = document.body.textContent;" 568 "var textContent = document.body.textContent;"
451 "var hasError = textContent.indexOf('%s') >= 0;" 569 "var hasError = textContent.indexOf('%s') >= 0;"
452 "domAutomationController.send(hasError);", 570 "domAutomationController.send(hasError);",
453 value_to_search.c_str()); 571 value_to_search.c_str());
454 } 572 }
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 738
621 bool result = false; 739 bool result = false;
622 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( 740 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
623 browser()->tab_strip_model()->GetActiveWebContents(), 741 browser()->tab_strip_model()->GetActiveWebContents(),
624 GetTextContentContainsStringScript(kHostnameJSUnicode), 742 GetTextContentContainsStringScript(kHostnameJSUnicode),
625 &result)); 743 &result));
626 EXPECT_TRUE(result); 744 EXPECT_TRUE(result);
627 } 745 }
628 746
629 } // namespace 747 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698