Chromium Code Reviews| Index: net/url_request/url_request_unittest.cc |
| diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc |
| index 72cb56e256be50a96901dc47a68cad2f2cbf6d77..e267450fcf3193c997028ebd96342f0a683da508 100644 |
| --- a/net/url_request/url_request_unittest.cc |
| +++ b/net/url_request/url_request_unittest.cc |
| @@ -26,6 +26,8 @@ |
| #include "base/memory/weak_ptr.h" |
| #include "base/message_loop/message_loop.h" |
| #include "base/path_service.h" |
| +#include "base/power_monitor/power_monitor.h" |
| +#include "base/power_monitor/power_monitor_source.h" |
| #include "base/run_loop.h" |
| #include "base/single_thread_task_runner.h" |
| #include "base/strings/string_number_conversions.h" |
| @@ -258,6 +260,21 @@ void TestLoadTimingNoHttpResponse( |
| } |
| #endif |
| +// Test power monitor source that can simulate entering suspend mode. Can't use |
| +// the one in base/ because it insists on bringing its own MessageLoop. |
| +class TestPowerMonitorSource : public base::PowerMonitorSource { |
| + public: |
| + TestPowerMonitorSource() {} |
| + ~TestPowerMonitorSource() override {} |
| + |
| + void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } |
| + |
| + bool IsOnBatteryPowerImpl() override { return false; } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); |
| +}; |
| + |
| // Do a case-insensitive search through |haystack| for |needle|. |
| bool ContainsString(const std::string& haystack, const char* needle) { |
| std::string::const_iterator it = std::search( |
| @@ -3406,6 +3423,28 @@ TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
| // Tests that the network delegate can block and redirect a request to a new |
| // URL during OnHeadersReceived. |
| +TEST_F(URLRequestTestHTTP, NetworkDelegateCanceledOnAsyncOnHeadersReceived) { |
| + TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
| + base::PowerMonitor power_monitor(make_scoped_ptr(power_monitor_source)); |
| + ASSERT_TRUE(test_server_.Start()); |
| + |
| + TestDelegate d; |
| + // Request that won't complete any time soon. |
| + GURL url(test_server_.GetURL("slow?600")); |
| + scoped_ptr<URLRequest> r( |
| + default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
| + r->Start(); |
| + |
| + power_monitor_source->Suspend(); |
| + // Wait for the event to propogated to the request, and the request to fail. |
| + base::MessageLoop::current()->Run(); |
|
davidben
2015/09/25 21:24:55
Nit: base::RunLoop().Run(); ?
mmenke
2015/09/25 21:37:52
Done (Not really sure...we're waiting on URLReques
|
| + EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| + EXPECT_TRUE(d.request_failed()); |
| + EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| +} |
| + |
| +// Tests that the network delegate can block and redirect a request to a new |
| +// URL during OnHeadersReceived. |
| TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
| ASSERT_TRUE(test_server_.Start()); |