OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <string> | 6 #include <string> |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 // Helper callback to run a test on our io_thread. The io_thread is spun up | 56 // Helper callback to run a test on our io_thread. The io_thread is spun up |
57 // once and reused for all tests. | 57 // once and reused for all tests. |
58 template <class Method> | 58 template <class Method> |
59 void MethodWrapper(Method method) { | 59 void MethodWrapper(Method method) { |
60 SetUpTest(); | 60 SetUpTest(); |
61 (this->*method)(); | 61 (this->*method)(); |
62 } | 62 } |
63 | 63 |
64 static void SetUpTestCase() { | 64 static void SetUpTestCase() { |
65 io_thread_.reset(new base::Thread("AppCacheResponseTest Thread")); | 65 io_thread_.reset(new base::Thread("AppCacheResponseTest Thread")); |
66 base::Thread::Options options(MessageLoop::TYPE_IO, 0); | 66 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); |
67 io_thread_->StartWithOptions(options); | 67 io_thread_->StartWithOptions(options); |
68 } | 68 } |
69 | 69 |
70 static void TearDownTestCase() { | 70 static void TearDownTestCase() { |
71 io_thread_.reset(NULL); | 71 io_thread_.reset(NULL); |
72 } | 72 } |
73 | 73 |
74 AppCacheResponseTest() {} | 74 AppCacheResponseTest() {} |
75 | 75 |
76 template <class Method> | 76 template <class Method> |
77 void RunTestOnIOThread(Method method) { | 77 void RunTestOnIOThread(Method method) { |
78 test_finished_event_ .reset(new base::WaitableEvent(false, false)); | 78 test_finished_event_ .reset(new base::WaitableEvent(false, false)); |
79 io_thread_->message_loop()->PostTask( | 79 io_thread_->message_loop()->PostTask( |
80 FROM_HERE, base::Bind(&AppCacheResponseTest::MethodWrapper<Method>, | 80 FROM_HERE, base::Bind(&AppCacheResponseTest::MethodWrapper<Method>, |
81 base::Unretained(this), method)); | 81 base::Unretained(this), method)); |
82 test_finished_event_->Wait(); | 82 test_finished_event_->Wait(); |
83 } | 83 } |
84 | 84 |
85 void SetUpTest() { | 85 void SetUpTest() { |
86 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 86 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); |
87 DCHECK(task_stack_.empty()); | 87 DCHECK(task_stack_.empty()); |
88 storage_delegate_.reset(new MockStorageDelegate(this)); | 88 storage_delegate_.reset(new MockStorageDelegate(this)); |
89 service_.reset(new MockAppCacheService()); | 89 service_.reset(new MockAppCacheService()); |
90 expected_read_result_ = 0; | 90 expected_read_result_ = 0; |
91 expected_write_result_ = 0; | 91 expected_write_result_ = 0; |
92 written_response_id_ = 0; | 92 written_response_id_ = 0; |
93 should_delete_reader_in_completion_callback_ = false; | 93 should_delete_reader_in_completion_callback_ = false; |
94 should_delete_writer_in_completion_callback_ = false; | 94 should_delete_writer_in_completion_callback_ = false; |
95 reader_deletion_count_down_ = 0; | 95 reader_deletion_count_down_ = 0; |
96 writer_deletion_count_down_ = 0; | 96 writer_deletion_count_down_ = 0; |
97 read_callback_was_called_ = false; | 97 read_callback_was_called_ = false; |
98 write_callback_was_called_ = false; | 98 write_callback_was_called_ = false; |
99 } | 99 } |
100 | 100 |
101 void TearDownTest() { | 101 void TearDownTest() { |
102 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 102 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); |
103 while (!task_stack_.empty()) | 103 while (!task_stack_.empty()) |
104 task_stack_.pop(); | 104 task_stack_.pop(); |
105 | 105 |
106 reader_.reset(); | 106 reader_.reset(); |
107 read_buffer_ = NULL; | 107 read_buffer_ = NULL; |
108 read_info_buffer_ = NULL; | 108 read_info_buffer_ = NULL; |
109 writer_.reset(); | 109 writer_.reset(); |
110 write_buffer_ = NULL; | 110 write_buffer_ = NULL; |
111 write_info_buffer_ = NULL; | 111 write_info_buffer_ = NULL; |
112 storage_delegate_.reset(); | 112 storage_delegate_.reset(); |
113 service_.reset(); | 113 service_.reset(); |
114 } | 114 } |
115 | 115 |
116 void TestFinished() { | 116 void TestFinished() { |
117 // We unwind the stack prior to finishing up to let stack | 117 // We unwind the stack prior to finishing up to let stack |
118 // based objects get deleted. | 118 // based objects get deleted. |
119 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 119 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); |
120 MessageLoop::current()->PostTask( | 120 base::MessageLoop::current()->PostTask( |
121 FROM_HERE, base::Bind(&AppCacheResponseTest::TestFinishedUnwound, | 121 FROM_HERE, |
brettw
2013/04/30 21:56:47
"
xhwang
2013/05/06 16:33:12
Done.
| |
122 base::Unretained(this))); | 122 base::Bind(&AppCacheResponseTest::TestFinishedUnwound, |
123 base::Unretained(this))); | |
123 } | 124 } |
124 | 125 |
125 void TestFinishedUnwound() { | 126 void TestFinishedUnwound() { |
126 TearDownTest(); | 127 TearDownTest(); |
127 test_finished_event_->Signal(); | 128 test_finished_event_->Signal(); |
128 } | 129 } |
129 | 130 |
130 void PushNextTask(const base::Closure& task) { | 131 void PushNextTask(const base::Closure& task) { |
131 task_stack_.push(std::pair<base::Closure, bool>(task, false)); | 132 task_stack_.push(std::pair<base::Closure, bool>(task, false)); |
132 } | 133 } |
133 | 134 |
134 void PushNextTaskAsImmediate(const base::Closure& task) { | 135 void PushNextTaskAsImmediate(const base::Closure& task) { |
135 task_stack_.push(std::pair<base::Closure, bool>(task, true)); | 136 task_stack_.push(std::pair<base::Closure, bool>(task, true)); |
136 } | 137 } |
137 | 138 |
138 void ScheduleNextTask() { | 139 void ScheduleNextTask() { |
139 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 140 DCHECK(base::MessageLoop::current() == io_thread_->message_loop()); |
140 if (task_stack_.empty()) { | 141 if (task_stack_.empty()) { |
141 TestFinished(); | 142 TestFinished(); |
142 return; | 143 return; |
143 } | 144 } |
144 base::Closure task = task_stack_.top().first; | 145 base::Closure task = task_stack_.top().first; |
145 bool immediate = task_stack_.top().second; | 146 bool immediate = task_stack_.top().second; |
146 task_stack_.pop(); | 147 task_stack_.pop(); |
147 if (immediate) | 148 if (immediate) |
148 task.Run(); | 149 task.Run(); |
149 else | 150 else |
150 MessageLoop::current()->PostTask(FROM_HERE, task); | 151 base::MessageLoop::current()->PostTask(FROM_HERE, task); |
151 } | 152 } |
152 | 153 |
153 // Wrappers to call AppCacheResponseReader/Writer Read and Write methods | 154 // Wrappers to call AppCacheResponseReader/Writer Read and Write methods |
154 | 155 |
155 void WriteBasicResponse() { | 156 void WriteBasicResponse() { |
156 static const char kHttpHeaders[] = | 157 static const char kHttpHeaders[] = |
157 "HTTP/1.0 200 OK\0Content-Length: 5\0\0"; | 158 "HTTP/1.0 200 OK\0Content-Length: 5\0\0"; |
158 static const char* kHttpBody = "Hello"; | 159 static const char* kHttpBody = "Hello"; |
159 scoped_refptr<IOBuffer> body(new WrappedIOBuffer(kHttpBody)); | 160 scoped_refptr<IOBuffer> body(new WrappedIOBuffer(kHttpBody)); |
160 std::string raw_headers(kHttpHeaders, arraysize(kHttpHeaders)); | 161 std::string raw_headers(kHttpHeaders, arraysize(kHttpHeaders)); |
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
634 | 635 |
635 void ReadThenDelete() { | 636 void ReadThenDelete() { |
636 read_callback_was_called_ = false; | 637 read_callback_was_called_ = false; |
637 reader_.reset(service_->storage()->CreateResponseReader( | 638 reader_.reset(service_->storage()->CreateResponseReader( |
638 GURL(), 0, written_response_id_)); | 639 GURL(), 0, written_response_id_)); |
639 ReadResponseBody(new IOBuffer(kBlockSize), kBlockSize); | 640 ReadResponseBody(new IOBuffer(kBlockSize), kBlockSize); |
640 EXPECT_TRUE(reader_->IsReadPending()); | 641 EXPECT_TRUE(reader_->IsReadPending()); |
641 reader_.reset(); | 642 reader_.reset(); |
642 | 643 |
643 // Wait a moment to verify no callbacks. | 644 // Wait a moment to verify no callbacks. |
644 MessageLoop::current()->PostDelayedTask( | 645 base::MessageLoop::current()->PostDelayedTask( |
645 FROM_HERE, base::Bind(&AppCacheResponseTest::VerifyNoCallbacks, | 646 FROM_HERE, |
brettw
2013/04/30 21:56:47
"
xhwang
2013/05/06 16:33:12
Done.
| |
646 base::Unretained(this)), | 647 base::Bind(&AppCacheResponseTest::VerifyNoCallbacks, |
648 base::Unretained(this)), | |
647 base::TimeDelta::FromMilliseconds(10)); | 649 base::TimeDelta::FromMilliseconds(10)); |
648 } | 650 } |
649 | 651 |
650 void VerifyNoCallbacks() { | 652 void VerifyNoCallbacks() { |
651 EXPECT_TRUE(!write_callback_was_called_); | 653 EXPECT_TRUE(!write_callback_was_called_); |
652 EXPECT_TRUE(!read_callback_was_called_); | 654 EXPECT_TRUE(!read_callback_was_called_); |
653 TestFinished(); | 655 TestFinished(); |
654 } | 656 } |
655 | 657 |
656 // Data members | 658 // Data members |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
709 | 711 |
710 TEST_F(AppCacheResponseTest, DeleteWithinCallbacks) { | 712 TEST_F(AppCacheResponseTest, DeleteWithinCallbacks) { |
711 RunTestOnIOThread(&AppCacheResponseTest::DeleteWithinCallbacks); | 713 RunTestOnIOThread(&AppCacheResponseTest::DeleteWithinCallbacks); |
712 } | 714 } |
713 | 715 |
714 TEST_F(AppCacheResponseTest, DeleteWithIOPending) { | 716 TEST_F(AppCacheResponseTest, DeleteWithIOPending) { |
715 RunTestOnIOThread(&AppCacheResponseTest::DeleteWithIOPending); | 717 RunTestOnIOThread(&AppCacheResponseTest::DeleteWithIOPending); |
716 } | 718 } |
717 | 719 |
718 } // namespace appcache | 720 } // namespace appcache |
OLD | NEW |