Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/renderer/net/net_error_helper_core.h" | 5 #include "chrome/renderer/net/net_error_helper_core.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/timer/mock_timer.h" | 10 #include "base/timer/mock_timer.h" |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 return ErrorToString(ProbeError(status), false); | 107 return ErrorToString(ProbeError(status), false); |
| 108 } | 108 } |
| 109 | 109 |
| 110 std::string NetErrorString(net::Error net_error) { | 110 std::string NetErrorString(net::Error net_error) { |
| 111 return ErrorToString(NetError(net_error), false); | 111 return ErrorToString(NetError(net_error), false); |
| 112 } | 112 } |
| 113 | 113 |
| 114 class NetErrorHelperCoreTest : public testing::Test, | 114 class NetErrorHelperCoreTest : public testing::Test, |
| 115 public NetErrorHelperCore::Delegate { | 115 public NetErrorHelperCore::Delegate { |
| 116 public: | 116 public: |
| 117 NetErrorHelperCoreTest() : timer_(new base::MockTimer(false, false)), | 117 NetErrorHelperCoreTest() : timer_(new base::MockTimer(false, false)), |
|
jar (doing other things)
2014/04/21 23:48:19
nit: When we have to put initializers on multiple
Randy Smith (Not in Mondays)
2014/04/22 20:40:02
I'm a little reluctant, since this wasn't code I o
mmenke
2014/04/22 21:03:27
I'm fine with either style. Looking at the C++ st
Randy Smith (Not in Mondays)
2014/04/22 21:09:42
Done.
| |
| 118 core_(this), | 118 core_(this), |
| 119 update_count_(0), | 119 update_count_(0), |
| 120 error_html_update_count_(0), | 120 error_html_update_count_(0), |
| 121 reload_count_(0), | 121 reload_count_(0), |
| 122 enable_stale_load_bindings_count_(0) { | 122 load_stale_count_(0), |
| 123 enable_page_helper_functions_count_(0) { | |
| 123 core_.set_auto_reload_enabled(false); | 124 core_.set_auto_reload_enabled(false); |
| 124 core_.set_timer_for_testing(scoped_ptr<base::Timer>(timer_)); | 125 core_.set_timer_for_testing(scoped_ptr<base::Timer>(timer_)); |
| 125 } | 126 } |
| 126 | 127 |
| 127 virtual ~NetErrorHelperCoreTest() { | 128 virtual ~NetErrorHelperCoreTest() { |
| 128 // No test finishes while an error page is being fetched. | 129 // No test finishes while an error page is being fetched. |
| 129 EXPECT_FALSE(is_url_being_fetched()); | 130 EXPECT_FALSE(is_url_being_fetched()); |
| 130 } | 131 } |
| 131 | 132 |
| 132 NetErrorHelperCore& core() { return core_; } | 133 NetErrorHelperCore& core() { return core_; } |
| 133 | 134 |
| 134 const GURL& url_being_fetched() const { return url_being_fetched_; } | 135 const GURL& url_being_fetched() const { return url_being_fetched_; } |
| 135 bool is_url_being_fetched() const { return !url_being_fetched_.is_empty(); } | 136 bool is_url_being_fetched() const { return !url_being_fetched_.is_empty(); } |
| 136 | 137 |
| 137 int reload_count() const { | 138 int reload_count() const { |
| 138 return reload_count_; | 139 return reload_count_; |
| 139 } | 140 } |
| 140 | 141 |
| 141 int enable_stale_load_bindings_count() const { | 142 int load_stale_count() const { |
| 142 return enable_stale_load_bindings_count_; | 143 return load_stale_count_; |
| 144 } | |
| 145 | |
| 146 const GURL& load_stale_url() const { | |
| 147 return load_stale_url_; | |
| 148 } | |
| 149 | |
| 150 int enable_page_helper_functions_count() const { | |
| 151 return enable_page_helper_functions_count_; | |
| 143 } | 152 } |
| 144 | 153 |
| 145 const std::string& last_update_string() const { return last_update_string_; } | 154 const std::string& last_update_string() const { return last_update_string_; } |
| 146 int update_count() const { return update_count_; } | 155 int update_count() const { return update_count_; } |
| 147 | 156 |
| 148 const std::string& last_error_html() const { return last_error_html_; } | 157 const std::string& last_error_html() const { return last_error_html_; } |
| 149 int error_html_update_count() const { return error_html_update_count_; } | 158 int error_html_update_count() const { return error_html_update_count_; } |
| 150 | 159 |
| 151 const LocalizedError::ErrorPageParams* last_error_page_params() const { | 160 const LocalizedError::ErrorPageParams* last_error_page_params() const { |
| 152 return last_error_page_params_.get(); | 161 return last_error_page_params_.get(); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 core().OnSetNavigationCorrectionInfo(navigation_correction_url, | 227 core().OnSetNavigationCorrectionInfo(navigation_correction_url, |
| 219 "en", "us", "api_key", | 228 "en", "us", "api_key", |
| 220 GURL(kSearchUrl)); | 229 GURL(kSearchUrl)); |
| 221 } | 230 } |
| 222 | 231 |
| 223 // NetErrorHelperCore::Delegate implementation: | 232 // NetErrorHelperCore::Delegate implementation: |
| 224 virtual void GenerateLocalizedErrorPage( | 233 virtual void GenerateLocalizedErrorPage( |
| 225 const WebURLError& error, | 234 const WebURLError& error, |
| 226 bool is_failed_post, | 235 bool is_failed_post, |
| 227 scoped_ptr<LocalizedError::ErrorPageParams> params, | 236 scoped_ptr<LocalizedError::ErrorPageParams> params, |
| 237 bool* reload_button_shown, | |
| 238 bool* load_stale_button_shown, | |
| 228 std::string* html) const OVERRIDE { | 239 std::string* html) const OVERRIDE { |
| 229 last_error_page_params_.reset(params.release()); | 240 last_error_page_params_.reset(params.release()); |
| 241 *reload_button_shown = false; | |
| 242 *load_stale_button_shown = false; | |
| 230 *html = ErrorToString(error, is_failed_post); | 243 *html = ErrorToString(error, is_failed_post); |
| 231 } | 244 } |
| 232 | 245 |
| 233 virtual void LoadErrorPageInMainFrame(const std::string& html, | 246 virtual void LoadErrorPageInMainFrame(const std::string& html, |
| 234 const GURL& failed_url) OVERRIDE { | 247 const GURL& failed_url) OVERRIDE { |
| 235 error_html_update_count_++; | 248 error_html_update_count_++; |
| 236 last_error_html_ = html; | 249 last_error_html_ = html; |
| 237 } | 250 } |
| 238 | 251 |
| 239 virtual void EnableStaleLoadBindings(const GURL& page_url) OVERRIDE { | 252 virtual void EnablePageHelperFunctions() OVERRIDE { |
| 240 enable_stale_load_bindings_count_++; | 253 enable_page_helper_functions_count_++; |
| 241 } | 254 } |
| 242 | 255 |
| 243 virtual void UpdateErrorPage(const WebURLError& error, | 256 virtual void UpdateErrorPage(const WebURLError& error, |
| 244 bool is_failed_post) OVERRIDE { | 257 bool is_failed_post) OVERRIDE { |
| 245 update_count_++; | 258 update_count_++; |
| 246 last_error_page_params_.reset(NULL); | 259 last_error_page_params_.reset(NULL); |
| 247 last_error_html_ = ErrorToString(error, is_failed_post); | 260 last_error_html_ = ErrorToString(error, is_failed_post); |
| 248 } | 261 } |
| 249 | 262 |
| 250 virtual void FetchNavigationCorrections( | 263 virtual void FetchNavigationCorrections( |
| 251 const GURL& navigation_correction_url, | 264 const GURL& navigation_correction_url, |
| 252 const std::string& navigation_correction_request_body) OVERRIDE { | 265 const std::string& navigation_correction_request_body) OVERRIDE { |
| 253 EXPECT_TRUE(url_being_fetched_.is_empty()); | 266 EXPECT_TRUE(url_being_fetched_.is_empty()); |
| 254 EXPECT_TRUE(request_body_.empty()); | 267 EXPECT_TRUE(request_body_.empty()); |
| 255 EXPECT_EQ(GURL(kNavigationCorrectionUrl), navigation_correction_url); | 268 EXPECT_EQ(GURL(kNavigationCorrectionUrl), navigation_correction_url); |
| 256 | 269 |
| 257 url_being_fetched_ = navigation_correction_url; | 270 url_being_fetched_ = navigation_correction_url; |
| 258 request_body_ = navigation_correction_request_body; | 271 request_body_ = navigation_correction_request_body; |
| 259 } | 272 } |
| 260 | 273 |
| 261 virtual void CancelFetchNavigationCorrections() OVERRIDE { | 274 virtual void CancelFetchNavigationCorrections() OVERRIDE { |
| 262 url_being_fetched_ = GURL(); | 275 url_being_fetched_ = GURL(); |
| 263 request_body_.clear(); | 276 request_body_.clear(); |
| 264 } | 277 } |
| 265 | 278 |
| 266 virtual void ReloadPage() OVERRIDE { | 279 virtual void ReloadPage() OVERRIDE { |
| 267 reload_count_++; | 280 reload_count_++; |
| 268 } | 281 } |
| 269 | 282 |
| 283 virtual void LoadPageFromCache(const GURL& error_url) OVERRIDE { | |
| 284 load_stale_count_++; | |
| 285 load_stale_url_ = error_url; | |
| 286 } | |
| 287 | |
| 270 base::MockTimer* timer_; | 288 base::MockTimer* timer_; |
| 271 | 289 |
| 272 NetErrorHelperCore core_; | 290 NetErrorHelperCore core_; |
| 273 | 291 |
| 274 GURL url_being_fetched_; | 292 GURL url_being_fetched_; |
| 275 std::string request_body_; | 293 std::string request_body_; |
| 276 | 294 |
| 277 // Contains the information passed to the last call to UpdateErrorPage, as a | 295 // Contains the information passed to the last call to UpdateErrorPage, as a |
| 278 // string. | 296 // string. |
| 279 std::string last_update_string_; | 297 std::string last_update_string_; |
| 280 // Number of times |last_update_string_| has been changed. | 298 // Number of times |last_update_string_| has been changed. |
| 281 int update_count_; | 299 int update_count_; |
| 282 | 300 |
| 283 // Contains the HTML set by the last call to LoadErrorPageInMainFrame. | 301 // Contains the HTML set by the last call to LoadErrorPageInMainFrame. |
| 284 std::string last_error_html_; | 302 std::string last_error_html_; |
| 285 // Number of times |last_error_html_| has been changed. | 303 // Number of times |last_error_html_| has been changed. |
| 286 int error_html_update_count_; | 304 int error_html_update_count_; |
| 287 | 305 |
| 288 // Mutable because GenerateLocalizedErrorPage is const. | 306 // Mutable because GenerateLocalizedErrorPage is const. |
| 289 mutable scoped_ptr<LocalizedError::ErrorPageParams> last_error_page_params_; | 307 mutable scoped_ptr<LocalizedError::ErrorPageParams> last_error_page_params_; |
| 290 | 308 |
| 291 int reload_count_; | 309 int reload_count_; |
| 310 int load_stale_count_; | |
| 311 GURL load_stale_url_; | |
| 292 | 312 |
| 293 int enable_stale_load_bindings_count_; | 313 int enable_page_helper_functions_count_; |
| 294 }; | 314 }; |
| 295 | 315 |
| 296 //------------------------------------------------------------------------------ | 316 //------------------------------------------------------------------------------ |
| 297 // Basic tests that don't update the error page for probes or load navigation | 317 // Basic tests that don't update the error page for probes or load navigation |
| 298 // corrections. | 318 // corrections. |
| 299 //------------------------------------------------------------------------------ | 319 //------------------------------------------------------------------------------ |
| 300 | 320 |
| 301 TEST_F(NetErrorHelperCoreTest, Null) { | 321 TEST_F(NetErrorHelperCoreTest, Null) { |
| 302 } | 322 } |
| 303 | 323 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 327 | 347 |
| 328 // It fails, and an error page is requested. | 348 // It fails, and an error page is requested. |
| 329 std::string html; | 349 std::string html; |
| 330 core().GetErrorHTML(NetErrorHelperCore::MAIN_FRAME, | 350 core().GetErrorHTML(NetErrorHelperCore::MAIN_FRAME, |
| 331 NetError(net::ERR_CONNECTION_RESET), false, &html); | 351 NetError(net::ERR_CONNECTION_RESET), false, &html); |
| 332 // Should have returned a local error page. | 352 // Should have returned a local error page. |
| 333 EXPECT_FALSE(html.empty()); | 353 EXPECT_FALSE(html.empty()); |
| 334 EXPECT_EQ(NetErrorString(net::ERR_CONNECTION_RESET), html); | 354 EXPECT_EQ(NetErrorString(net::ERR_CONNECTION_RESET), html); |
| 335 | 355 |
| 336 // Error page loads. | 356 // Error page loads. |
| 337 EXPECT_EQ(0, enable_stale_load_bindings_count()); | 357 EXPECT_EQ(0, enable_page_helper_functions_count()); |
| 338 core().OnStartLoad(NetErrorHelperCore::MAIN_FRAME, | 358 core().OnStartLoad(NetErrorHelperCore::MAIN_FRAME, |
| 339 NetErrorHelperCore::ERROR_PAGE); | 359 NetErrorHelperCore::ERROR_PAGE); |
| 340 core().OnCommitLoad(NetErrorHelperCore::MAIN_FRAME); | 360 core().OnCommitLoad(NetErrorHelperCore::MAIN_FRAME); |
| 341 core().OnFinishLoad(NetErrorHelperCore::MAIN_FRAME); | 361 core().OnFinishLoad(NetErrorHelperCore::MAIN_FRAME); |
| 342 EXPECT_EQ(0, update_count()); | 362 EXPECT_EQ(0, update_count()); |
| 343 EXPECT_EQ(0, error_html_update_count()); | 363 EXPECT_EQ(0, error_html_update_count()); |
| 344 EXPECT_EQ(1, enable_stale_load_bindings_count()); | 364 EXPECT_EQ(1, enable_page_helper_functions_count()); |
| 345 } | 365 } |
| 346 | 366 |
| 347 TEST_F(NetErrorHelperCoreTest, MainFrameNonDnsErrorWithCorrections) { | 367 TEST_F(NetErrorHelperCoreTest, MainFrameNonDnsErrorWithCorrections) { |
| 348 EnableNavigationCorrections(); | 368 EnableNavigationCorrections(); |
| 349 | 369 |
| 350 // Original page starts loading. | 370 // Original page starts loading. |
| 351 core().OnStartLoad(NetErrorHelperCore::MAIN_FRAME, | 371 core().OnStartLoad(NetErrorHelperCore::MAIN_FRAME, |
| 352 NetErrorHelperCore::NON_ERROR_PAGE); | 372 NetErrorHelperCore::NON_ERROR_PAGE); |
| 353 | 373 |
| 354 // It fails, and an error page is requested. | 374 // It fails, and an error page is requested. |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 732 core().GetErrorHTML(NetErrorHelperCore::MAIN_FRAME, | 752 core().GetErrorHTML(NetErrorHelperCore::MAIN_FRAME, |
| 733 NetError(net::ERR_NAME_NOT_RESOLVED), | 753 NetError(net::ERR_NAME_NOT_RESOLVED), |
| 734 true, &html); | 754 true, &html); |
| 735 // Should have returned a local error page indicating a probe may run. | 755 // Should have returned a local error page indicating a probe may run. |
| 736 EXPECT_EQ(ErrorToString( | 756 EXPECT_EQ(ErrorToString( |
| 737 ProbeError(chrome_common_net::DNS_PROBE_POSSIBLE), | 757 ProbeError(chrome_common_net::DNS_PROBE_POSSIBLE), |
| 738 true), | 758 true), |
| 739 html); | 759 html); |
| 740 | 760 |
| 741 // Error page loads. | 761 // Error page loads. |
| 742 EXPECT_EQ(0, enable_stale_load_bindings_count()); | 762 EXPECT_EQ(0, enable_page_helper_functions_count()); |
| 743 core().OnStartLoad(NetErrorHelperCore::MAIN_FRAME, | 763 core().OnStartLoad(NetErrorHelperCore::MAIN_FRAME, |
| 744 NetErrorHelperCore::ERROR_PAGE); | 764 NetErrorHelperCore::ERROR_PAGE); |
| 745 core().OnCommitLoad(NetErrorHelperCore::MAIN_FRAME); | 765 core().OnCommitLoad(NetErrorHelperCore::MAIN_FRAME); |
| 746 core().OnFinishLoad(NetErrorHelperCore::MAIN_FRAME); | 766 core().OnFinishLoad(NetErrorHelperCore::MAIN_FRAME); |
| 747 EXPECT_EQ(0, update_count()); | 767 EXPECT_EQ(0, update_count()); |
| 748 EXPECT_EQ(0, enable_stale_load_bindings_count()); | 768 EXPECT_EQ(1, enable_page_helper_functions_count()); |
| 749 | 769 |
| 750 core().OnNetErrorInfo(chrome_common_net::DNS_PROBE_STARTED); | 770 core().OnNetErrorInfo(chrome_common_net::DNS_PROBE_STARTED); |
| 751 EXPECT_EQ(1, update_count()); | 771 EXPECT_EQ(1, update_count()); |
| 752 EXPECT_EQ(ErrorToString( | 772 EXPECT_EQ(ErrorToString( |
| 753 ProbeError(chrome_common_net::DNS_PROBE_STARTED), true), | 773 ProbeError(chrome_common_net::DNS_PROBE_STARTED), true), |
| 754 last_error_html()); | 774 last_error_html()); |
| 755 | 775 |
| 756 core().OnNetErrorInfo(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN); | 776 core().OnNetErrorInfo(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN); |
| 757 EXPECT_EQ(2, update_count()); | 777 EXPECT_EQ(2, update_count()); |
| 758 EXPECT_EQ(ErrorToString( | 778 EXPECT_EQ(ErrorToString( |
| (...skipping 1218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1977 DoErrorLoad(net::ERR_CONNECTION_RESET); | 1997 DoErrorLoad(net::ERR_CONNECTION_RESET); |
| 1978 timer()->Fire(); | 1998 timer()->Fire(); |
| 1979 | 1999 |
| 1980 EXPECT_FALSE(core().ShouldSuppressErrorPage(NetErrorHelperCore::SUB_FRAME, | 2000 EXPECT_FALSE(core().ShouldSuppressErrorPage(NetErrorHelperCore::SUB_FRAME, |
| 1981 GURL(kFailedUrl))); | 2001 GURL(kFailedUrl))); |
| 1982 EXPECT_FALSE(core().ShouldSuppressErrorPage(NetErrorHelperCore::MAIN_FRAME, | 2002 EXPECT_FALSE(core().ShouldSuppressErrorPage(NetErrorHelperCore::MAIN_FRAME, |
| 1983 GURL("http://some.other.url"))); | 2003 GURL("http://some.other.url"))); |
| 1984 EXPECT_TRUE(core().ShouldSuppressErrorPage(NetErrorHelperCore::MAIN_FRAME, | 2004 EXPECT_TRUE(core().ShouldSuppressErrorPage(NetErrorHelperCore::MAIN_FRAME, |
| 1985 GURL(kFailedUrl))); | 2005 GURL(kFailedUrl))); |
| 1986 } | 2006 } |
| 2007 | |
| 2008 TEST_F(NetErrorHelperCoreTest, ExplicitReloadSucceeds) { | |
| 2009 DoErrorLoad(net::ERR_CONNECTION_RESET); | |
| 2010 EXPECT_EQ(0, reload_count()); | |
| 2011 core().ExecuteButtonPress(NetErrorHelperCore::RELOAD_BUTTON); | |
| 2012 EXPECT_EQ(1, reload_count()); | |
| 2013 } | |
| 2014 | |
| 2015 TEST_F(NetErrorHelperCoreTest, ExplicitLoadStaleSucceeds) { | |
| 2016 DoErrorLoad(net::ERR_CONNECTION_RESET); | |
| 2017 EXPECT_EQ(0, load_stale_count()); | |
| 2018 core().ExecuteButtonPress(NetErrorHelperCore::LOAD_STALE_BUTTON); | |
| 2019 EXPECT_EQ(1, load_stale_count()); | |
| 2020 EXPECT_EQ(GURL(kFailedUrl), load_stale_url()); | |
| 2021 } | |
| 2022 | |
| 2023 | |
| OLD | NEW |