OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/browser/loader/mock_resource_loader.h" | 5 #include "content/browser/loader/mock_resource_loader.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "content/browser/loader/resource_controller.h" | 11 #include "content/browser/loader/resource_controller.h" |
12 #include "content/browser/loader/resource_handler.h" | 12 #include "content/browser/loader/resource_handler.h" |
13 #include "net/base/io_buffer.h" | |
14 #include "net/url_request/url_request_status.h" | 13 #include "net/url_request/url_request_status.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
16 | 15 |
17 namespace content { | 16 namespace content { |
18 | 17 |
19 MockResourceLoader::MockResourceLoader(ResourceHandler* resource_handler) | 18 MockResourceLoader::MockResourceLoader(ResourceHandler* resource_handler) |
20 : resource_handler_(resource_handler) { | 19 : resource_handler_(resource_handler) { |
21 resource_handler_->SetController(this); | 20 resource_handler_->SetController(this); |
22 } | 21 } |
23 | 22 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 } else if (defer) { | 81 } else if (defer) { |
83 status_ = Status::CALLBACK_PENDING; | 82 status_ = Status::CALLBACK_PENDING; |
84 } else { | 83 } else { |
85 status_ = Status::IDLE; | 84 status_ = Status::IDLE; |
86 } | 85 } |
87 return status_; | 86 return status_; |
88 } | 87 } |
89 | 88 |
90 MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) { | 89 MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) { |
91 EXPECT_EQ(Status::IDLE, status_); | 90 EXPECT_EQ(Status::IDLE, status_); |
91 EXPECT_FALSE(io_buffer_); | |
92 EXPECT_EQ(0, io_buffer_size_); | |
93 | |
92 status_ = Status::CALLING_HANDLER; | 94 status_ = Status::CALLING_HANDLER; |
93 | 95 |
94 scoped_refptr<net::IOBuffer> buf; | 96 bool result = |
95 int buf_size; | 97 resource_handler_->OnWillRead(&io_buffer_, &io_buffer_size_, min_size); |
96 bool result = resource_handler_->OnWillRead(&buf, &buf_size, min_size); | |
97 // The second case isn't really allowed, but a number of classes do it | 98 // The second case isn't really allowed, but a number of classes do it |
98 // anyways. | 99 // anyways. |
99 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | 100 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || |
100 (result == false && status_ == Status::CANCELED)); | 101 (result == false && status_ == Status::CANCELED)); |
101 if (!result) { | 102 if (!result) { |
103 EXPECT_EQ(0, io_buffer_size_); | |
104 EXPECT_FALSE(io_buffer_); | |
102 status_ = Status::CANCELED; | 105 status_ = Status::CANCELED; |
103 } else { | 106 } else { |
104 EXPECT_LE(min_size, buf_size); | 107 EXPECT_LE(min_size, io_buffer_size_); |
108 EXPECT_LT(0, io_buffer_size_); | |
109 EXPECT_TRUE(io_buffer_); | |
105 status_ = Status::IDLE; | 110 status_ = Status::IDLE; |
106 } | 111 } |
107 return status_; | 112 return status_; |
108 }; | 113 }; |
109 | 114 |
110 MockResourceLoader::Status MockResourceLoader::OnReadCompleted(int bytes_read) { | 115 MockResourceLoader::Status MockResourceLoader::OnReadCompleted( |
116 base::StringPiece bytes) { | |
111 EXPECT_EQ(Status::IDLE, status_); | 117 EXPECT_EQ(Status::IDLE, status_); |
118 EXPECT_LE(bytes.size(), io_buffer_size_); | |
119 | |
120 status_ = Status::CALLING_HANDLER; | |
121 std::copy(bytes.begin(), bytes.end(), io_buffer_->data()); | |
122 io_buffer_ = nullptr; | |
123 io_buffer_size_ = 0; | |
112 status_ = Status::CALLING_HANDLER; | 124 status_ = Status::CALLING_HANDLER; |
113 | 125 |
114 bool defer = false; | 126 bool defer = false; |
115 bool result = resource_handler_->OnReadCompleted(bytes_read, &defer); | 127 bool result = resource_handler_->OnReadCompleted(bytes.size(), &defer); |
116 // The second case isn't really allowed, but a number of classes do it | 128 // The second case isn't really allowed, but a number of classes do it |
117 // anyways. | 129 // anyways. |
118 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | 130 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || |
119 (result == false && status_ == Status::CANCELED)); | 131 (result == false && status_ == Status::CANCELED)); |
120 if (!result) { | 132 if (!result) { |
121 status_ = Status::CANCELED; | 133 status_ = Status::CANCELED; |
122 } else if (defer) { | 134 } else if (defer) { |
123 status_ = Status::CALLBACK_PENDING; | 135 status_ = Status::CALLBACK_PENDING; |
124 } else { | 136 } else { |
125 status_ = Status::IDLE; | 137 status_ = Status::IDLE; |
(...skipping 15 matching lines...) Expand all Loading... | |
141 resource_handler_->OnResponseCompleted(status, &defer); | 153 resource_handler_->OnResponseCompleted(status, &defer); |
142 EXPECT_EQ(Status::CALLING_HANDLER, status_); | 154 EXPECT_EQ(Status::CALLING_HANDLER, status_); |
143 if (defer) { | 155 if (defer) { |
144 status_ = Status::CALLBACK_PENDING; | 156 status_ = Status::CALLBACK_PENDING; |
145 } else { | 157 } else { |
146 status_ = Status::IDLE; | 158 status_ = Status::IDLE; |
147 } | 159 } |
148 return status_; | 160 return status_; |
149 } | 161 } |
150 | 162 |
163 MockResourceLoader::Status | |
164 MockResourceLoader::OnResponseCompletedFromExternalOutOfBandCancel( | |
165 const net::URLRequestStatus& status) { | |
Randy Smith (Not in Mondays)
2016/12/21 16:47:37
nit, suggestion: This routine (and possibly other
mmenke
2017/01/05 18:50:25
Done.
| |
166 // This can happen at any point, except from a recursive call from | |
167 // ResourceHandler. | |
168 EXPECT_NE(Status::CALLING_HANDLER, status_); | |
169 | |
170 status_ = Status::CALLING_HANDLER; | |
171 | |
172 bool defer = false; | |
173 resource_handler_->OnResponseCompleted(status, &defer); | |
174 EXPECT_EQ(Status::CALLING_HANDLER, status_); | |
175 if (defer) { | |
176 status_ = Status::CALLBACK_PENDING; | |
177 } else { | |
178 status_ = Status::IDLE; | |
179 } | |
180 return status_; | |
181 } | |
182 | |
183 void MockResourceLoader::WaitUntilIdleOrCanceled() { | |
184 if (status_ != Status::IDLE && status_ != Status::CANCELED) { | |
185 EXPECT_FALSE(canceled_or_idle_run_loop_); | |
186 canceled_or_idle_run_loop_.reset(new base::RunLoop()); | |
187 canceled_or_idle_run_loop_->Run(); | |
188 EXPECT_TRUE(status_ == Status::IDLE || status_ == Status::CANCELED); | |
189 } | |
190 } | |
191 | |
192 void MockResourceLoader::ReleaseIOBuffer() { | |
Randy Smith (Not in Mondays)
2016/12/21 16:47:37
Is there a reason this can't be done automatically
mmenke
2017/01/05 18:50:25
MojoAsyncResourceHandlerTest.IOBufferFromOnWillRea
Randy Smith (Not in Mondays)
2017/01/10 23:07:47
If I understand this use case correctly, it could
| |
193 io_buffer_ = nullptr; | |
194 } | |
195 | |
151 void MockResourceLoader::Cancel() { | 196 void MockResourceLoader::Cancel() { |
152 CancelWithError(net::ERR_ABORTED); | 197 CancelWithError(net::ERR_ABORTED); |
153 } | 198 } |
154 | 199 |
155 void MockResourceLoader::CancelAndIgnore() { | 200 void MockResourceLoader::CancelAndIgnore() { |
156 NOTREACHED(); | 201 NOTREACHED(); |
157 } | 202 } |
158 | 203 |
159 void MockResourceLoader::CancelWithError(int error_code) { | 204 void MockResourceLoader::CancelWithError(int error_code) { |
160 EXPECT_LT(error_code, 0); | 205 EXPECT_LT(error_code, 0); |
161 // Ignore double cancels. Classes shouldn't be doing this, as | 206 // Ignore double cancels. Classes shouldn't be doing this, as |
162 // ResourceLoader doesn't really support it, but they do anywways. :( | 207 // ResourceLoader doesn't really support it, but they do anywways. :( |
163 // TODO(mmenke): Remove this check. | 208 // TODO(mmenke): Remove this check. |
164 if (error_code_ != net::OK) | 209 if (error_code_ != net::OK) |
165 return; | 210 return; |
166 | 211 |
167 // ResourceLoader really expects canceled not to be called synchronously, but | 212 // ResourceLoader really expects canceled not to be called synchronously, but |
168 // a lot of code does it, so allow it. | 213 // a lot of code does it, so allow it. |
169 // TODO(mmenke): Remove CALLING_HANDLER. | 214 // TODO(mmenke): Remove CALLING_HANDLER. |
170 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING || | 215 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING || |
171 status_ == Status::CALLING_HANDLER || status_ == Status::IDLE); | 216 status_ == Status::CALLING_HANDLER || status_ == Status::IDLE); |
172 status_ = Status::CANCELED; | 217 status_ = Status::CANCELED; |
173 error_code_ = error_code; | 218 error_code_ = error_code; |
219 if (canceled_or_idle_run_loop_) | |
220 canceled_or_idle_run_loop_->Quit(); | |
174 } | 221 } |
175 | 222 |
176 void MockResourceLoader::Resume() { | 223 void MockResourceLoader::Resume() { |
177 EXPECT_EQ(Status::CALLBACK_PENDING, status_); | 224 EXPECT_EQ(Status::CALLBACK_PENDING, status_); |
178 status_ = Status::IDLE; | 225 status_ = Status::IDLE; |
226 if (canceled_or_idle_run_loop_) | |
227 canceled_or_idle_run_loop_->Quit(); | |
179 } | 228 } |
180 | 229 |
181 } // namespace content | 230 } // namespace content |
OLD | NEW |