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/command_line.h" |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
28 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
29 #include "content/public/browser/render_frame_host.h" | 29 #include "content/public/browser/render_frame_host.h" |
30 #include "content/public/browser/render_view_host.h" | 30 #include "content/public/browser/render_view_host.h" |
31 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
32 #include "content/public/browser/web_contents_observer.h" | 32 #include "content/public/browser/web_contents_observer.h" |
33 #include "content/public/test/browser_test_utils.h" | 33 #include "content/public/test/browser_test_utils.h" |
34 #include "content/public/test/test_navigation_observer.h" | 34 #include "content/public/test/test_navigation_observer.h" |
35 #include "content/test/net/url_request_failed_job.h" | 35 #include "content/test/net/url_request_failed_job.h" |
36 #include "content/test/net/url_request_mock_http_job.h" | 36 #include "content/test/net/url_request_mock_http_job.h" |
| 37 #include "grit/generated_resources.h" |
37 #include "net/base/net_errors.h" | 38 #include "net/base/net_errors.h" |
38 #include "net/base/net_util.h" | 39 #include "net/base/net_util.h" |
39 #include "net/http/failing_http_transaction_factory.h" | 40 #include "net/http/failing_http_transaction_factory.h" |
40 #include "net/http/http_cache.h" | 41 #include "net/http/http_cache.h" |
41 #include "net/test/spawned_test_server/spawned_test_server.h" | 42 #include "net/test/spawned_test_server/spawned_test_server.h" |
42 #include "net/url_request/url_request_context.h" | 43 #include "net/url_request/url_request_context.h" |
43 #include "net/url_request/url_request_context_getter.h" | 44 #include "net/url_request/url_request_context_getter.h" |
44 #include "net/url_request/url_request_filter.h" | 45 #include "net/url_request/url_request_filter.h" |
45 #include "net/url_request/url_request_job.h" | 46 #include "net/url_request/url_request_job.h" |
46 #include "net/url_request/url_request_job_factory.h" | 47 #include "net/url_request/url_request_job_factory.h" |
47 #include "net/url_request/url_request_test_job.h" | 48 #include "net/url_request/url_request_test_job.h" |
48 #include "net/url_request/url_request_test_util.h" | 49 #include "net/url_request/url_request_test_util.h" |
| 50 #include "ui/base/l10n/l10n_util.h" |
49 | 51 |
50 using content::BrowserThread; | 52 using content::BrowserThread; |
51 using content::NavigationController; | 53 using content::NavigationController; |
52 using content::URLRequestFailedJob; | 54 using content::URLRequestFailedJob; |
53 using net::URLRequestJobFactory; | 55 using net::URLRequestJobFactory; |
54 using net::URLRequestTestJob; | 56 using net::URLRequestTestJob; |
55 | 57 |
56 namespace { | 58 namespace { |
57 | 59 |
58 // Returns true if |text| is displayed on the page |browser| is currently | 60 // Returns true if |text| is displayed on the page |browser| is currently |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 // Check that the search box is populated correctly. | 131 // Check that the search box is populated correctly. |
130 bool search_box_populated = false; | 132 bool search_box_populated = false; |
131 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | 133 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
132 browser->tab_strip_model()->GetActiveWebContents(), | 134 browser->tab_strip_model()->GetActiveWebContents(), |
133 "var searchText = document.getElementById('search-box').value;" | 135 "var searchText = document.getElementById('search-box').value;" |
134 "domAutomationController.send(searchText == 'search query');", | 136 "domAutomationController.send(searchText == 'search query');", |
135 &search_box_populated)); | 137 &search_box_populated)); |
136 EXPECT_TRUE(search_box_populated); | 138 EXPECT_TRUE(search_box_populated); |
137 } | 139 } |
138 | 140 |
| 141 std::string GetLoadStaleButtonLabel() { |
| 142 return l10n_util::GetStringUTF8(IDS_ERRORPAGES_BUTTON_LOAD_STALE); |
| 143 } |
| 144 |
139 // A protocol handler that fails a configurable number of requests, then | 145 // A protocol handler that fails a configurable number of requests, then |
140 // succeeds all requests after that, keeping count of failures and successes. | 146 // succeeds all requests after that, keeping count of failures and successes. |
141 class FailFirstNRequestsProtocolHandler | 147 class FailFirstNRequestsProtocolHandler |
142 : public URLRequestJobFactory::ProtocolHandler { | 148 : public URLRequestJobFactory::ProtocolHandler { |
143 public: | 149 public: |
144 FailFirstNRequestsProtocolHandler(const GURL& url, int requests_to_fail) | 150 FailFirstNRequestsProtocolHandler(const GURL& url, int requests_to_fail) |
145 : url_(url), requests_(0), failures_(0), | 151 : url_(url), requests_(0), failures_(0), |
146 requests_to_fail_(requests_to_fail) {} | 152 requests_to_fail_(requests_to_fail) {} |
147 virtual ~FailFirstNRequestsProtocolHandler() {} | 153 virtual ~FailFirstNRequestsProtocolHandler() {} |
148 | 154 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 | 248 |
243 void GoBackAndWaitForNavigations(int num_navigations) { | 249 void GoBackAndWaitForNavigations(int num_navigations) { |
244 NavigateHistory(num_navigations, HISTORY_NAVIGATE_BACK); | 250 NavigateHistory(num_navigations, HISTORY_NAVIGATE_BACK); |
245 } | 251 } |
246 | 252 |
247 void GoForwardAndWaitForNavigations(int num_navigations) { | 253 void GoForwardAndWaitForNavigations(int num_navigations) { |
248 NavigateHistory(num_navigations, HISTORY_NAVIGATE_FORWARD); | 254 NavigateHistory(num_navigations, HISTORY_NAVIGATE_FORWARD); |
249 } | 255 } |
250 | 256 |
251 // Confirms that the javascript variable indicating whether or not we have | 257 // Confirms that the javascript variable indicating whether or not we have |
252 // a stale copy in the cache has been set to |expected|. | 258 // a stale copy in the cache has been set to |expected|, and that the |
253 bool ProbeStaleCopyValue(bool expected) { | 259 // stale load button is or isn't there based on the same expectation. |
| 260 testing::AssertionResult ProbeStaleCopyValue(bool expected) { |
254 const char* js_cache_probe = | 261 const char* js_cache_probe = |
255 "try {\n" | 262 "try {\n" |
256 " domAutomationController.send(\n" | 263 " domAutomationController.send(\n" |
257 " templateData.staleCopyInCache ? 'yes' : 'no');\n" | 264 " 'staleLoadButton' in templateData ? 'yes' : 'no');\n" |
258 "} catch (e) {\n" | 265 "} catch (e) {\n" |
259 " domAutomationController.send(e.message);\n" | 266 " domAutomationController.send(e.message);\n" |
260 "}\n"; | 267 "}\n"; |
261 | 268 |
262 std::string result; | 269 std::string result; |
263 bool ret = | 270 bool ret = |
264 content::ExecuteScriptAndExtractString( | 271 content::ExecuteScriptAndExtractString( |
265 browser()->tab_strip_model()->GetActiveWebContents(), | 272 browser()->tab_strip_model()->GetActiveWebContents(), |
266 js_cache_probe, | 273 js_cache_probe, |
267 &result); | 274 &result); |
268 EXPECT_TRUE(ret); | 275 if (!ret) { |
269 if (!ret) | 276 return testing::AssertionFailure() |
270 return false; | 277 << "Failing return from ExecuteScriptAndExtractString."; |
271 EXPECT_EQ(expected ? "yes" : "no", result); | 278 } |
272 return ((expected ? "yes" : "no") == result); | 279 |
| 280 if ((expected && "yes" == result) || (!expected && "no" == result)) |
| 281 return testing::AssertionSuccess(); |
| 282 |
| 283 return testing::AssertionFailure() << "Cache probe result is " << result; |
273 } | 284 } |
274 | 285 |
275 testing::AssertionResult ReloadStaleCopyFromCache() { | 286 testing::AssertionResult ReloadStaleCopyFromCache() { |
276 const char* js_reload_script = | 287 const char* js_reload_script = |
277 "try {\n" | 288 "try {\n" |
278 " errorCacheLoad.reloadStaleInstance();\n" | 289 " document.getElementById('stale-load-button').click();\n" |
279 " domAutomationController.send('success');\n" | 290 " domAutomationController.send('success');\n" |
280 "} catch (e) {\n" | 291 "} catch (e) {\n" |
281 " domAutomationController.send(e.message);\n" | 292 " domAutomationController.send(e.message);\n" |
282 "}\n"; | 293 "}\n"; |
283 | 294 |
284 std::string result; | 295 std::string result; |
285 bool ret = content::ExecuteScriptAndExtractString( | 296 bool ret = content::ExecuteScriptAndExtractString( |
286 browser()->tab_strip_model()->GetActiveWebContents(), | 297 browser()->tab_strip_model()->GetActiveWebContents(), |
287 js_reload_script, | 298 js_reload_script, |
288 &result); | 299 &result); |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 browser()->profile()->GetRequestContext(); | 685 browser()->profile()->GetRequestContext(); |
675 BrowserThread::PostTask( | 686 BrowserThread::PostTask( |
676 BrowserThread::IO, FROM_HERE, | 687 BrowserThread::IO, FROM_HERE, |
677 base::Bind(&InterceptNetworkTransactions, url_request_context_getter, | 688 base::Bind(&InterceptNetworkTransactions, url_request_context_getter, |
678 net::ERR_FAILED)); | 689 net::ERR_FAILED)); |
679 | 690 |
680 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 691 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
681 // With no navigation corrections to load, there's only one navigation. | 692 // With no navigation corrections to load, there's only one navigation. |
682 browser(), test_url, 1); | 693 browser(), test_url, 1); |
683 EXPECT_TRUE(ProbeStaleCopyValue(true)); | 694 EXPECT_TRUE(ProbeStaleCopyValue(true)); |
| 695 EXPECT_TRUE(IsDisplayingText(browser(), GetLoadStaleButtonLabel())); |
684 EXPECT_NE(base::ASCIIToUTF16("Nocache Test Page"), | 696 EXPECT_NE(base::ASCIIToUTF16("Nocache Test Page"), |
685 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); | 697 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); |
686 | 698 |
687 // Confirm that loading the stale copy from the cache works. | 699 // Confirm that loading the stale copy from the cache works. |
688 content::TestNavigationObserver same_tab_observer( | 700 content::TestNavigationObserver same_tab_observer( |
689 browser()->tab_strip_model()->GetActiveWebContents(), 1); | 701 browser()->tab_strip_model()->GetActiveWebContents(), 1); |
690 ASSERT_TRUE(ReloadStaleCopyFromCache()); | 702 ASSERT_TRUE(ReloadStaleCopyFromCache()); |
691 same_tab_observer.Wait(); | 703 same_tab_observer.Wait(); |
692 EXPECT_EQ(base::ASCIIToUTF16("Nocache Test Page"), | 704 EXPECT_EQ(base::ASCIIToUTF16("Nocache Test Page"), |
693 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); | 705 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); |
694 | 706 |
695 // Clear the cache and reload the same URL; confirm the error page is told | 707 // Clear the cache and reload the same URL; confirm the error page is told |
696 // that there is no cached copy. | 708 // that there is no cached copy. |
697 BrowsingDataRemover* remover = | 709 BrowsingDataRemover* remover = |
698 BrowsingDataRemover::CreateForUnboundedRange(browser()->profile()); | 710 BrowsingDataRemover::CreateForUnboundedRange(browser()->profile()); |
699 remover->Remove(BrowsingDataRemover::REMOVE_CACHE, | 711 remover->Remove(BrowsingDataRemover::REMOVE_CACHE, |
700 BrowsingDataHelper::UNPROTECTED_WEB); | 712 BrowsingDataHelper::UNPROTECTED_WEB); |
701 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 713 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
702 browser(), test_url, 1); | 714 browser(), test_url, 1); |
703 EXPECT_TRUE(ProbeStaleCopyValue(false)); | 715 EXPECT_TRUE(ProbeStaleCopyValue(false)); |
| 716 EXPECT_FALSE(IsDisplayingText(browser(), GetLoadStaleButtonLabel())); |
704 } | 717 } |
705 | 718 |
706 class ErrorPageAutoReloadTest : public InProcessBrowserTest { | 719 class ErrorPageAutoReloadTest : public InProcessBrowserTest { |
707 public: | 720 public: |
708 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 721 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
709 command_line->AppendSwitch(switches::kEnableOfflineAutoReload); | 722 command_line->AppendSwitch(switches::kEnableOfflineAutoReload); |
710 } | 723 } |
711 | 724 |
712 void InstallProtocolHandler(const GURL& url, int requests_to_fail) { | 725 void InstallProtocolHandler(const GURL& url, int requests_to_fail) { |
713 protocol_handler_ = new FailFirstNRequestsProtocolHandler( | 726 protocol_handler_ = new FailFirstNRequestsProtocolHandler( |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
855 // confirm that the error page is told the cached copy exists. | 868 // confirm that the error page is told the cached copy exists. |
856 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter = | 869 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter = |
857 browser()->profile()->GetRequestContext(); | 870 browser()->profile()->GetRequestContext(); |
858 BrowserThread::PostTask( | 871 BrowserThread::PostTask( |
859 BrowserThread::IO, FROM_HERE, | 872 BrowserThread::IO, FROM_HERE, |
860 base::Bind(&InterceptNetworkTransactions, url_request_context_getter, | 873 base::Bind(&InterceptNetworkTransactions, url_request_context_getter, |
861 net::ERR_CONNECTION_FAILED)); | 874 net::ERR_CONNECTION_FAILED)); |
862 | 875 |
863 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 876 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
864 browser(), test_url, 2); | 877 browser(), test_url, 2); |
865 ProbeStaleCopyValue(true); | 878 EXPECT_TRUE(IsDisplayingText(browser(), GetLoadStaleButtonLabel())); |
| 879 EXPECT_TRUE(ProbeStaleCopyValue(true)); |
866 | 880 |
867 // Confirm that loading the stale copy from the cache works. | 881 // Confirm that loading the stale copy from the cache works. |
868 content::TestNavigationObserver same_tab_observer( | 882 content::TestNavigationObserver same_tab_observer( |
869 browser()->tab_strip_model()->GetActiveWebContents(), 1); | 883 browser()->tab_strip_model()->GetActiveWebContents(), 1); |
870 ASSERT_TRUE(ReloadStaleCopyFromCache()); | 884 ASSERT_TRUE(ReloadStaleCopyFromCache()); |
871 same_tab_observer.Wait(); | 885 same_tab_observer.Wait(); |
872 EXPECT_EQ(base::ASCIIToUTF16("Nocache Test Page"), | 886 EXPECT_EQ(base::ASCIIToUTF16("Nocache Test Page"), |
873 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); | 887 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); |
874 | 888 |
875 // Clear the cache and reload the same URL; confirm the error page is told | 889 // Clear the cache and reload the same URL; confirm the error page is told |
876 // that there is no cached copy. | 890 // that there is no cached copy. |
877 BrowsingDataRemover* remover = | 891 BrowsingDataRemover* remover = |
878 BrowsingDataRemover::CreateForUnboundedRange(browser()->profile()); | 892 BrowsingDataRemover::CreateForUnboundedRange(browser()->profile()); |
879 remover->Remove(BrowsingDataRemover::REMOVE_CACHE, | 893 remover->Remove(BrowsingDataRemover::REMOVE_CACHE, |
880 BrowsingDataHelper::UNPROTECTED_WEB); | 894 BrowsingDataHelper::UNPROTECTED_WEB); |
881 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 895 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
882 browser(), test_url, 2); | 896 browser(), test_url, 2); |
883 ProbeStaleCopyValue(false); | 897 EXPECT_TRUE(ProbeStaleCopyValue(false)); |
| 898 EXPECT_FALSE(IsDisplayingText(browser(), GetLoadStaleButtonLabel())); |
884 } | 899 } |
885 | 900 |
886 // A test fixture that simulates failing requests for an IDN domain name. | 901 // A test fixture that simulates failing requests for an IDN domain name. |
887 class ErrorPageForIDNTest : public InProcessBrowserTest { | 902 class ErrorPageForIDNTest : public InProcessBrowserTest { |
888 public: | 903 public: |
889 // Target hostname in different forms. | 904 // Target hostname in different forms. |
890 static const char kHostname[]; | 905 static const char kHostname[]; |
891 static const char kHostnameJSUnicode[]; | 906 static const char kHostnameJSUnicode[]; |
892 | 907 |
893 // InProcessBrowserTest: | 908 // InProcessBrowserTest: |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
931 browser(), | 946 browser(), |
932 URLRequestFailedJob::GetMockHttpUrlForHostname(net::ERR_UNSAFE_PORT, | 947 URLRequestFailedJob::GetMockHttpUrlForHostname(net::ERR_UNSAFE_PORT, |
933 kHostname), | 948 kHostname), |
934 1); | 949 1); |
935 | 950 |
936 ToggleHelpBox(browser()); | 951 ToggleHelpBox(browser()); |
937 EXPECT_TRUE(IsDisplayingText(browser(), kHostnameJSUnicode)); | 952 EXPECT_TRUE(IsDisplayingText(browser(), kHostnameJSUnicode)); |
938 } | 953 } |
939 | 954 |
940 } // namespace | 955 } // namespace |
OLD | NEW |