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/test/sequenced_worker_pool_owner.h" | 9 #include "base/threading/sequenced_worker_pool.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_owner_(1, "URLRequestSimpleJobTest"), | 117 : worker_pool_( |
118 task_runner_(worker_pool_owner_.pool() | 118 new base::SequencedWorkerPool(1, "URLRequestSimpleJobTest")), |
119 ->GetSequencedTaskRunnerWithShutdownBehavior( | 119 task_runner_(worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior( |
120 worker_pool_owner_.pool() | 120 worker_pool_->GetSequenceToken(), |
121 ->GetSequenceToken(), | 121 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
122 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | |
123 context_(true) { | 122 context_(true) { |
124 job_factory_.SetProtocolHandler( | 123 job_factory_.SetProtocolHandler( |
125 "data", make_scoped_ptr(new SimpleJobProtocolHandler(task_runner_))); | 124 "data", make_scoped_ptr(new SimpleJobProtocolHandler(task_runner_))); |
126 context_.set_job_factory(&job_factory_); | 125 context_.set_job_factory(&job_factory_); |
127 context_.Init(); | 126 context_.Init(); |
128 | 127 |
129 request_ = | 128 request_ = |
130 context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, &delegate_); | 129 context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, &delegate_); |
131 } | 130 } |
132 | 131 |
| 132 ~URLRequestSimpleJobTest() override { worker_pool_->Shutdown(); } |
| 133 |
133 void StartRequest(const HttpRequestHeaders* headers) { | 134 void StartRequest(const HttpRequestHeaders* headers) { |
134 if (headers) | 135 if (headers) |
135 request_->SetExtraRequestHeaders(*headers); | 136 request_->SetExtraRequestHeaders(*headers); |
136 request_->Start(); | 137 request_->Start(); |
137 | 138 |
138 EXPECT_TRUE(request_->is_pending()); | 139 EXPECT_TRUE(request_->is_pending()); |
139 base::RunLoop().Run(); | 140 base::RunLoop().Run(); |
140 EXPECT_FALSE(request_->is_pending()); | 141 EXPECT_FALSE(request_->is_pending()); |
141 } | 142 } |
142 | 143 |
| 144 void TearDown() override { worker_pool_->Shutdown(); } |
| 145 |
143 protected: | 146 protected: |
144 base::SequencedWorkerPoolOwner worker_pool_owner_; | 147 scoped_refptr<base::SequencedWorkerPool> worker_pool_; |
145 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 148 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
146 TestURLRequestContext context_; | 149 TestURLRequestContext context_; |
147 URLRequestJobFactoryImpl job_factory_; | 150 URLRequestJobFactoryImpl job_factory_; |
148 TestDelegate delegate_; | 151 TestDelegate delegate_; |
149 scoped_ptr<URLRequest> request_; | 152 scoped_ptr<URLRequest> request_; |
150 }; | 153 }; |
151 | 154 |
152 } // namespace | 155 } // namespace |
153 | 156 |
154 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { | 157 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 run_loop.Run(); | 237 run_loop.Run(); |
235 | 238 |
236 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); | 239 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); |
237 EXPECT_EQ(1, cancel_delegate.response_started_count()); | 240 EXPECT_EQ(1, cancel_delegate.response_started_count()); |
238 EXPECT_EQ("", cancel_delegate.data_received()); | 241 EXPECT_EQ("", cancel_delegate.data_received()); |
239 // Destroy the request so it doesn't outlive its delegate. | 242 // Destroy the request so it doesn't outlive its delegate. |
240 request_.reset(); | 243 request_.reset(); |
241 } | 244 } |
242 | 245 |
243 } // namespace net | 246 } // namespace net |
OLD | NEW |