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" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 } else if (defer) { | 83 } else if (defer) { |
84 status_ = Status::CALLBACK_PENDING; | 84 status_ = Status::CALLBACK_PENDING; |
85 } else { | 85 } else { |
86 status_ = Status::IDLE; | 86 status_ = Status::IDLE; |
87 } | 87 } |
88 return status_; | 88 return status_; |
89 } | 89 } |
90 | 90 |
91 MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) { | 91 MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) { |
92 EXPECT_EQ(Status::IDLE, status_); | 92 EXPECT_EQ(Status::IDLE, status_); |
| 93 EXPECT_FALSE(io_buffer_); |
| 94 EXPECT_EQ(0, io_buffer_size_); |
| 95 |
93 status_ = Status::CALLING_HANDLER; | 96 status_ = Status::CALLING_HANDLER; |
94 | 97 |
95 scoped_refptr<net::IOBuffer> buf; | 98 bool result = |
96 int buf_size; | 99 resource_handler_->OnWillRead(&io_buffer_, &io_buffer_size_, min_size); |
97 bool result = resource_handler_->OnWillRead(&buf, &buf_size, min_size); | |
98 // The second case isn't really allowed, but a number of classes do it | 100 // The second case isn't really allowed, but a number of classes do it |
99 // anyways. | 101 // anyways. |
100 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | 102 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || |
101 (result == false && status_ == Status::CANCELED)); | 103 (result == false && status_ == Status::CANCELED)); |
102 if (!result) { | 104 if (!result) { |
| 105 EXPECT_EQ(0, io_buffer_size_); |
| 106 EXPECT_FALSE(io_buffer_); |
103 status_ = Status::CANCELED; | 107 status_ = Status::CANCELED; |
104 } else { | 108 } else { |
105 EXPECT_LE(min_size, buf_size); | 109 EXPECT_LE(min_size, io_buffer_size_); |
| 110 EXPECT_LT(0, io_buffer_size_); |
| 111 EXPECT_TRUE(io_buffer_); |
106 status_ = Status::IDLE; | 112 status_ = Status::IDLE; |
107 } | 113 } |
108 return status_; | 114 return status_; |
109 }; | 115 }; |
110 | 116 |
111 MockResourceLoader::Status MockResourceLoader::OnReadCompleted(int bytes_read) { | 117 MockResourceLoader::Status MockResourceLoader::OnReadCompleted( |
| 118 base::StringPiece bytes) { |
112 EXPECT_EQ(Status::IDLE, status_); | 119 EXPECT_EQ(Status::IDLE, status_); |
| 120 EXPECT_LE(bytes.size(), static_cast<size_t>(io_buffer_size_)); |
| 121 |
| 122 status_ = Status::CALLING_HANDLER; |
| 123 std::copy(bytes.begin(), bytes.end(), io_buffer_->data()); |
| 124 io_buffer_ = nullptr; |
| 125 io_buffer_size_ = 0; |
113 status_ = Status::CALLING_HANDLER; | 126 status_ = Status::CALLING_HANDLER; |
114 | 127 |
115 bool defer = false; | 128 bool defer = false; |
116 bool result = resource_handler_->OnReadCompleted(bytes_read, &defer); | 129 bool result = resource_handler_->OnReadCompleted(bytes.size(), &defer); |
117 // The second case isn't really allowed, but a number of classes do it | 130 // The second case isn't really allowed, but a number of classes do it |
118 // anyways. | 131 // anyways. |
119 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | 132 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || |
120 (result == false && status_ == Status::CANCELED)); | 133 (result == false && status_ == Status::CANCELED)); |
121 if (!result) { | 134 if (!result) { |
122 status_ = Status::CANCELED; | 135 status_ = Status::CANCELED; |
123 } else if (defer) { | 136 } else if (defer) { |
124 status_ = Status::CALLBACK_PENDING; | 137 status_ = Status::CALLBACK_PENDING; |
125 } else { | 138 } else { |
126 status_ = Status::IDLE; | 139 status_ = Status::IDLE; |
(...skipping 15 matching lines...) Expand all Loading... |
142 resource_handler_->OnResponseCompleted(status, &defer); | 155 resource_handler_->OnResponseCompleted(status, &defer); |
143 EXPECT_EQ(Status::CALLING_HANDLER, status_); | 156 EXPECT_EQ(Status::CALLING_HANDLER, status_); |
144 if (defer) { | 157 if (defer) { |
145 status_ = Status::CALLBACK_PENDING; | 158 status_ = Status::CALLBACK_PENDING; |
146 } else { | 159 } else { |
147 status_ = Status::IDLE; | 160 status_ = Status::IDLE; |
148 } | 161 } |
149 return status_; | 162 return status_; |
150 } | 163 } |
151 | 164 |
| 165 MockResourceLoader::Status |
| 166 MockResourceLoader::OnResponseCompletedFromExternalOutOfBandCancel( |
| 167 const net::URLRequestStatus& url_request_status) { |
| 168 // This can happen at any point, except from a recursive call from |
| 169 // ResourceHandler. |
| 170 EXPECT_NE(Status::CALLING_HANDLER, status_); |
| 171 |
| 172 status_ = Status::CALLING_HANDLER; |
| 173 |
| 174 bool defer = false; |
| 175 resource_handler_->OnResponseCompleted(url_request_status, &defer); |
| 176 EXPECT_EQ(Status::CALLING_HANDLER, status_); |
| 177 if (defer) { |
| 178 status_ = Status::CALLBACK_PENDING; |
| 179 } else { |
| 180 status_ = Status::IDLE; |
| 181 } |
| 182 return status_; |
| 183 } |
| 184 |
| 185 void MockResourceLoader::WaitUntilIdleOrCanceled() { |
| 186 if (status_ != Status::IDLE && status_ != Status::CANCELED) { |
| 187 EXPECT_FALSE(canceled_or_idle_run_loop_); |
| 188 canceled_or_idle_run_loop_.reset(new base::RunLoop()); |
| 189 canceled_or_idle_run_loop_->Run(); |
| 190 EXPECT_TRUE(status_ == Status::IDLE || status_ == Status::CANCELED); |
| 191 } |
| 192 } |
| 193 |
| 194 void MockResourceLoader::ReleaseIOBuffer() { |
| 195 io_buffer_ = nullptr; |
| 196 } |
| 197 |
152 void MockResourceLoader::Cancel() { | 198 void MockResourceLoader::Cancel() { |
153 CancelWithError(net::ERR_ABORTED); | 199 CancelWithError(net::ERR_ABORTED); |
154 } | 200 } |
155 | 201 |
156 void MockResourceLoader::CancelAndIgnore() { | 202 void MockResourceLoader::CancelAndIgnore() { |
157 NOTREACHED(); | 203 NOTREACHED(); |
158 } | 204 } |
159 | 205 |
160 void MockResourceLoader::CancelWithError(int error_code) { | 206 void MockResourceLoader::CancelWithError(int error_code) { |
161 EXPECT_LT(error_code, 0); | 207 EXPECT_LT(error_code, 0); |
162 // Ignore double cancels. Classes shouldn't be doing this, as | 208 // Ignore double cancels. Classes shouldn't be doing this, as |
163 // ResourceLoader doesn't really support it, but they do anywways. :( | 209 // ResourceLoader doesn't really support it, but they do anywways. :( |
164 // TODO(mmenke): Remove this check. | 210 // TODO(mmenke): Remove this check. |
165 if (error_code_ != net::OK) | 211 if (error_code_ != net::OK) |
166 return; | 212 return; |
167 | 213 |
168 // ResourceLoader really expects canceled not to be called synchronously, but | 214 // ResourceLoader really expects canceled not to be called synchronously, but |
169 // a lot of code does it, so allow it. | 215 // a lot of code does it, so allow it. |
170 // TODO(mmenke): Remove CALLING_HANDLER. | 216 // TODO(mmenke): Remove CALLING_HANDLER. |
171 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING || | 217 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING || |
172 status_ == Status::CALLING_HANDLER || status_ == Status::IDLE); | 218 status_ == Status::CALLING_HANDLER || status_ == Status::IDLE); |
173 status_ = Status::CANCELED; | 219 status_ = Status::CANCELED; |
174 error_code_ = error_code; | 220 error_code_ = error_code; |
| 221 if (canceled_or_idle_run_loop_) |
| 222 canceled_or_idle_run_loop_->Quit(); |
175 } | 223 } |
176 | 224 |
177 void MockResourceLoader::Resume() { | 225 void MockResourceLoader::Resume() { |
178 EXPECT_EQ(Status::CALLBACK_PENDING, status_); | 226 EXPECT_EQ(Status::CALLBACK_PENDING, status_); |
179 status_ = Status::IDLE; | 227 status_ = Status::IDLE; |
| 228 if (canceled_or_idle_run_loop_) |
| 229 canceled_or_idle_run_loop_->Quit(); |
180 } | 230 } |
181 | 231 |
182 } // namespace content | 232 } // namespace content |
OLD | NEW |