| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_helpers.h" | 5 #include "base/bind_helpers.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/threading/sequenced_worker_pool.h" | 9 #include "base/test/sequenced_worker_pool_owner.h" |
| 10 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
| 11 #include "net/base/request_priority.h" | 11 #include "net/base/request_priority.h" |
| 12 #include "net/url_request/url_request_job.h" | 12 #include "net/url_request/url_request_job.h" |
| 13 #include "net/url_request/url_request_job_factory.h" | 13 #include "net/url_request/url_request_job_factory.h" |
| 14 #include "net/url_request/url_request_job_factory_impl.h" | 14 #include "net/url_request/url_request_job_factory_impl.h" |
| 15 #include "net/url_request/url_request_simple_job.h" | 15 #include "net/url_request/url_request_simple_job.h" |
| 16 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace net { | 19 namespace net { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 | 107 |
| 108 ~SimpleJobProtocolHandler() override {} | 108 ~SimpleJobProtocolHandler() override {} |
| 109 | 109 |
| 110 private: | 110 private: |
| 111 scoped_refptr<base::TaskRunner> task_runner_; | 111 scoped_refptr<base::TaskRunner> task_runner_; |
| 112 }; | 112 }; |
| 113 | 113 |
| 114 class URLRequestSimpleJobTest : public ::testing::Test { | 114 class URLRequestSimpleJobTest : public ::testing::Test { |
| 115 public: | 115 public: |
| 116 URLRequestSimpleJobTest() | 116 URLRequestSimpleJobTest() |
| 117 : worker_pool_( | 117 : worker_pool_owner_(1, "URLRequestSimpleJobTest"), |
| 118 new base::SequencedWorkerPool(1, "URLRequestSimpleJobTest")), | 118 task_runner_(worker_pool_owner_.pool() |
| 119 task_runner_(worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior( | 119 ->GetSequencedTaskRunnerWithShutdownBehavior( |
| 120 worker_pool_->GetSequenceToken(), | 120 worker_pool_owner_.pool() |
| 121 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | 121 ->GetSequenceToken(), |
| 122 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
| 122 context_(true) { | 123 context_(true) { |
| 123 job_factory_.SetProtocolHandler( | 124 job_factory_.SetProtocolHandler( |
| 124 "data", make_scoped_ptr(new SimpleJobProtocolHandler(task_runner_))); | 125 "data", make_scoped_ptr(new SimpleJobProtocolHandler(task_runner_))); |
| 125 context_.set_job_factory(&job_factory_); | 126 context_.set_job_factory(&job_factory_); |
| 126 context_.Init(); | 127 context_.Init(); |
| 127 | 128 |
| 128 request_ = | 129 request_ = |
| 129 context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, &delegate_); | 130 context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, &delegate_); |
| 130 } | 131 } |
| 131 | 132 |
| 132 ~URLRequestSimpleJobTest() override { worker_pool_->Shutdown(); } | |
| 133 | |
| 134 void StartRequest(const HttpRequestHeaders* headers) { | 133 void StartRequest(const HttpRequestHeaders* headers) { |
| 135 if (headers) | 134 if (headers) |
| 136 request_->SetExtraRequestHeaders(*headers); | 135 request_->SetExtraRequestHeaders(*headers); |
| 137 request_->Start(); | 136 request_->Start(); |
| 138 | 137 |
| 139 EXPECT_TRUE(request_->is_pending()); | 138 EXPECT_TRUE(request_->is_pending()); |
| 140 base::RunLoop().Run(); | 139 base::RunLoop().Run(); |
| 141 EXPECT_FALSE(request_->is_pending()); | 140 EXPECT_FALSE(request_->is_pending()); |
| 142 } | 141 } |
| 143 | 142 |
| 144 void TearDown() override { worker_pool_->Shutdown(); } | |
| 145 | |
| 146 protected: | 143 protected: |
| 147 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | 144 base::SequencedWorkerPoolOwner worker_pool_owner_; |
| 148 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 145 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 149 TestURLRequestContext context_; | 146 TestURLRequestContext context_; |
| 150 URLRequestJobFactoryImpl job_factory_; | 147 URLRequestJobFactoryImpl job_factory_; |
| 151 TestDelegate delegate_; | 148 TestDelegate delegate_; |
| 152 scoped_ptr<URLRequest> request_; | 149 scoped_ptr<URLRequest> request_; |
| 153 }; | 150 }; |
| 154 | 151 |
| 155 } // namespace | 152 } // namespace |
| 156 | 153 |
| 157 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { | 154 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 run_loop.Run(); | 234 run_loop.Run(); |
| 238 | 235 |
| 239 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); | 236 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); |
| 240 EXPECT_EQ(1, cancel_delegate.response_started_count()); | 237 EXPECT_EQ(1, cancel_delegate.response_started_count()); |
| 241 EXPECT_EQ("", cancel_delegate.data_received()); | 238 EXPECT_EQ("", cancel_delegate.data_received()); |
| 242 // Destroy the request so it doesn't outlive its delegate. | 239 // Destroy the request so it doesn't outlive its delegate. |
| 243 request_.reset(); | 240 request_.reset(); |
| 244 } | 241 } |
| 245 | 242 |
| 246 } // namespace net | 243 } // namespace net |
| OLD | NEW |