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 <stack> | 5 #include <stack> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/location.h" | |
13 #include "base/logging.h" | 12 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
15 #include "base/pickle.h" | 14 #include "base/pickle.h" |
16 #include "base/single_thread_task_runner.h" | |
17 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
18 #include "base/thread_task_runner_handle.h" | |
19 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
20 #include "content/browser/appcache/appcache_response.h" | 17 #include "content/browser/appcache/appcache_response.h" |
21 #include "content/browser/appcache/appcache_url_request_job.h" | 18 #include "content/browser/appcache/appcache_url_request_job.h" |
22 #include "content/browser/appcache/mock_appcache_service.h" | 19 #include "content/browser/appcache/mock_appcache_service.h" |
23 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
24 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
25 #include "net/base/request_priority.h" | 22 #include "net/base/request_priority.h" |
26 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
27 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
28 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 210 |
214 static void TearDownTestCase() { | 211 static void TearDownTestCase() { |
215 io_thread_.reset(NULL); | 212 io_thread_.reset(NULL); |
216 } | 213 } |
217 | 214 |
218 AppCacheURLRequestJobTest() {} | 215 AppCacheURLRequestJobTest() {} |
219 | 216 |
220 template <class Method> | 217 template <class Method> |
221 void RunTestOnIOThread(Method method) { | 218 void RunTestOnIOThread(Method method) { |
222 test_finished_event_ .reset(new base::WaitableEvent(false, false)); | 219 test_finished_event_ .reset(new base::WaitableEvent(false, false)); |
223 io_thread_->task_runner()->PostTask( | 220 io_thread_->message_loop()->PostTask( |
224 FROM_HERE, base::Bind(&AppCacheURLRequestJobTest::MethodWrapper<Method>, | 221 FROM_HERE, base::Bind(&AppCacheURLRequestJobTest::MethodWrapper<Method>, |
225 base::Unretained(this), method)); | 222 base::Unretained(this), method)); |
226 test_finished_event_->Wait(); | 223 test_finished_event_->Wait(); |
227 } | 224 } |
228 | 225 |
229 void SetUpTest() { | 226 void SetUpTest() { |
230 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); | 227 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); |
231 DCHECK(task_stack_.empty()); | 228 DCHECK(task_stack_.empty()); |
232 | 229 |
233 storage_delegate_.reset(new MockStorageDelegate(this)); | 230 storage_delegate_.reset(new MockStorageDelegate(this)); |
(...skipping 29 matching lines...) Expand all Loading... |
263 DCHECK(!job_factory_->has_job()); | 260 DCHECK(!job_factory_->has_job()); |
264 empty_context_.reset(); | 261 empty_context_.reset(); |
265 job_factory_.reset(); | 262 job_factory_.reset(); |
266 url_request_delegate_.reset(); | 263 url_request_delegate_.reset(); |
267 } | 264 } |
268 | 265 |
269 void TestFinished() { | 266 void TestFinished() { |
270 // We unwind the stack prior to finishing up to let stack | 267 // We unwind the stack prior to finishing up to let stack |
271 // based objects get deleted. | 268 // based objects get deleted. |
272 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); | 269 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); |
273 base::ThreadTaskRunnerHandle::Get()->PostTask( | 270 base::MessageLoop::current()->PostTask( |
274 FROM_HERE, base::Bind(&AppCacheURLRequestJobTest::TestFinishedUnwound, | 271 FROM_HERE, |
275 base::Unretained(this))); | 272 base::Bind(&AppCacheURLRequestJobTest::TestFinishedUnwound, |
| 273 base::Unretained(this))); |
276 } | 274 } |
277 | 275 |
278 void TestFinishedUnwound() { | 276 void TestFinishedUnwound() { |
279 TearDownTest(); | 277 TearDownTest(); |
280 test_finished_event_->Signal(); | 278 test_finished_event_->Signal(); |
281 } | 279 } |
282 | 280 |
283 void PushNextTask(const base::Closure& task) { | 281 void PushNextTask(const base::Closure& task) { |
284 task_stack_.push(std::pair<base::Closure, bool>(task, false)); | 282 task_stack_.push(std::pair<base::Closure, bool>(task, false)); |
285 } | 283 } |
286 | 284 |
287 void PushNextTaskAsImmediate(const base::Closure& task) { | 285 void PushNextTaskAsImmediate(const base::Closure& task) { |
288 task_stack_.push(std::pair<base::Closure, bool>(task, true)); | 286 task_stack_.push(std::pair<base::Closure, bool>(task, true)); |
289 } | 287 } |
290 | 288 |
291 void ScheduleNextTask() { | 289 void ScheduleNextTask() { |
292 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); | 290 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); |
293 if (task_stack_.empty()) { | 291 if (task_stack_.empty()) { |
294 TestFinished(); | 292 TestFinished(); |
295 return; | 293 return; |
296 } | 294 } |
297 base::Closure task =task_stack_.top().first; | 295 base::Closure task =task_stack_.top().first; |
298 bool immediate = task_stack_.top().second; | 296 bool immediate = task_stack_.top().second; |
299 task_stack_.pop(); | 297 task_stack_.pop(); |
300 if (immediate) | 298 if (immediate) |
301 task.Run(); | 299 task.Run(); |
302 else | 300 else |
303 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 301 base::MessageLoop::current()->PostTask(FROM_HERE, task); |
304 } | 302 } |
305 | 303 |
306 // Wrappers to call AppCacheResponseReader/Writer Read and Write methods | 304 // Wrappers to call AppCacheResponseReader/Writer Read and Write methods |
307 | 305 |
308 void WriteBasicResponse() { | 306 void WriteBasicResponse() { |
309 scoped_refptr<IOBuffer> body(new WrappedIOBuffer(kHttpBasicBody)); | 307 scoped_refptr<IOBuffer> body(new WrappedIOBuffer(kHttpBasicBody)); |
310 std::string raw_headers(kHttpBasicHeaders, arraysize(kHttpBasicHeaders)); | 308 std::string raw_headers(kHttpBasicHeaders, arraysize(kHttpBasicHeaders)); |
311 WriteResponse( | 309 WriteResponse( |
312 MakeHttpResponseInfo(raw_headers), body.get(), strlen(kHttpBasicBody)); | 310 MakeHttpResponseInfo(raw_headers), body.get(), strlen(kHttpBasicBody)); |
313 } | 311 } |
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
863 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); | 861 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); |
864 } | 862 } |
865 | 863 |
866 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { | 864 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { |
867 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); | 865 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); |
868 } | 866 } |
869 | 867 |
870 } // namespace | 868 } // namespace |
871 | 869 |
872 } // namespace content | 870 } // namespace content |
OLD | NEW |