Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(412)

Side by Side Diff: content/browser/loader/mock_resource_loader.cc

Issue 2581393002: Update MojoAsyncResourceHandler tests to use MockResourceLoader. (Closed)
Patch Set: Oops, had another local change I forgot about Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 } else if (defer) { 89 } else if (defer) {
90 status_ = Status::CALLBACK_PENDING; 90 status_ = Status::CALLBACK_PENDING;
91 } else { 91 } else {
92 status_ = Status::IDLE; 92 status_ = Status::IDLE;
93 } 93 }
94 return status_; 94 return status_;
95 } 95 }
96 96
97 MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) { 97 MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) {
98 EXPECT_EQ(Status::IDLE, status_); 98 EXPECT_EQ(Status::IDLE, status_);
99 EXPECT_FALSE(io_buffer_);
100 EXPECT_EQ(0, io_buffer_size_);
101
99 status_ = Status::CALLING_HANDLER; 102 status_ = Status::CALLING_HANDLER;
100 103
101 scoped_refptr<net::IOBuffer> buf; 104 bool result =
102 int buf_size; 105 resource_handler_->OnWillRead(&io_buffer_, &io_buffer_size_, min_size);
103 bool result = resource_handler_->OnWillRead(&buf, &buf_size, min_size);
104 // The second case isn't really allowed, but a number of classes do it 106 // The second case isn't really allowed, but a number of classes do it
105 // anyways. 107 // anyways.
106 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || 108 EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
107 (result == false && status_ == Status::CANCELED)); 109 (result == false && status_ == Status::CANCELED));
108 if (!result) { 110 if (!result) {
111 EXPECT_EQ(0, io_buffer_size_);
112 EXPECT_FALSE(io_buffer_);
109 status_ = Status::CANCELED; 113 status_ = Status::CANCELED;
110 } else { 114 } else {
111 EXPECT_LE(min_size, buf_size); 115 EXPECT_LE(min_size, io_buffer_size_);
116 EXPECT_LT(0, io_buffer_size_);
117 EXPECT_TRUE(io_buffer_);
112 status_ = Status::IDLE; 118 status_ = Status::IDLE;
113 } 119 }
114 return status_; 120 return status_;
115 }; 121 };
116 122
117 MockResourceLoader::Status MockResourceLoader::OnReadCompleted(int bytes_read) { 123 MockResourceLoader::Status MockResourceLoader::OnReadCompleted(
124 base::StringPiece bytes) {
118 EXPECT_EQ(Status::IDLE, status_); 125 EXPECT_EQ(Status::IDLE, status_);
126 EXPECT_LE(bytes.size(), static_cast<size_t>(io_buffer_size_));
127
128 status_ = Status::CALLING_HANDLER;
129 std::copy(bytes.begin(), bytes.end(), io_buffer_->data());
130 io_buffer_ = nullptr;
131 io_buffer_size_ = 0;
119 status_ = Status::CALLING_HANDLER; 132 status_ = Status::CALLING_HANDLER;
120 133
121 bool defer = false; 134 bool defer = false;
122 bool result = resource_handler_->OnReadCompleted(bytes_read, &defer); 135 bool result = resource_handler_->OnReadCompleted(bytes.size(), &defer);
123 // The second case isn't really allowed, but a number of classes do it 136 // The second case isn't really allowed, but a number of classes do it
124 // anyways. 137 // anyways.
125 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || 138 EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
126 (result == false && status_ == Status::CANCELED)); 139 (result == false && status_ == Status::CANCELED));
127 if (!result) { 140 if (!result) {
128 status_ = Status::CANCELED; 141 status_ = Status::CANCELED;
129 } else if (defer) { 142 } else if (defer) {
130 status_ = Status::CALLBACK_PENDING; 143 status_ = Status::CALLBACK_PENDING;
131 } else { 144 } else {
132 status_ = Status::IDLE; 145 status_ = Status::IDLE;
133 } 146 }
134 return status_; 147 return status_;
135 } 148 }
136 149
137 MockResourceLoader::Status MockResourceLoader::OnResponseCompleted( 150 MockResourceLoader::Status MockResourceLoader::OnResponseCompleted(
138 const net::URLRequestStatus& status) { 151 const net::URLRequestStatus& status) {
139 // This should only happen while the ResourceLoader is idle or the request was 152 // This should only happen while the ResourceLoader is idle or the request was
140 // canceled. 153 // canceled.
141 EXPECT_TRUE(status_ == Status::IDLE || 154 EXPECT_TRUE(status_ == Status::IDLE ||
142 (!status.is_success() && status_ == Status::CANCELED && 155 (!status.is_success() && status_ == Status::CANCELED &&
143 error_code_ == status.error())); 156 error_code_ == status.error()));
144 157
158 io_buffer_ = nullptr;
159 io_buffer_size_ = 0;
145 status_ = Status::CALLING_HANDLER; 160 status_ = Status::CALLING_HANDLER;
146 161
147 bool defer = false; 162 bool defer = false;
148 resource_handler_->OnResponseCompleted(status, &defer); 163 resource_handler_->OnResponseCompleted(status, &defer);
149 EXPECT_EQ(Status::CALLING_HANDLER, status_); 164 EXPECT_EQ(Status::CALLING_HANDLER, status_);
150 if (defer) { 165 if (defer) {
151 status_ = Status::CALLBACK_PENDING; 166 status_ = Status::CALLBACK_PENDING;
152 } else { 167 } else {
153 status_ = Status::IDLE; 168 status_ = Status::IDLE;
154 } 169 }
155 return status_; 170 return status_;
156 } 171 }
157 172
173 MockResourceLoader::Status
174 MockResourceLoader::OnResponseCompletedFromExternalOutOfBandCancel(
175 const net::URLRequestStatus& url_request_status) {
176 // This can happen at any point, except from a recursive call from
177 // ResourceHandler.
178 EXPECT_NE(Status::CALLING_HANDLER, status_);
179
180 io_buffer_ = nullptr;
181 io_buffer_size_ = 0;
182 status_ = Status::CALLING_HANDLER;
183
184 bool defer = false;
185 resource_handler_->OnResponseCompleted(url_request_status, &defer);
186 EXPECT_EQ(Status::CALLING_HANDLER, status_);
187 if (defer) {
188 status_ = Status::CALLBACK_PENDING;
189 } else {
190 status_ = Status::IDLE;
191 }
192 return status_;
193 }
194
195 void MockResourceLoader::WaitUntilIdleOrCanceled() {
196 if (status_ == Status::IDLE || status_ == Status::CANCELED)
197 return;
198 EXPECT_FALSE(canceled_or_idle_run_loop_);
199 canceled_or_idle_run_loop_.reset(new base::RunLoop());
200 canceled_or_idle_run_loop_->Run();
201 EXPECT_TRUE(status_ == Status::IDLE || status_ == Status::CANCELED);
202 }
203
158 void MockResourceLoader::Cancel() { 204 void MockResourceLoader::Cancel() {
159 CancelWithError(net::ERR_ABORTED); 205 CancelWithError(net::ERR_ABORTED);
160 } 206 }
161 207
162 void MockResourceLoader::CancelAndIgnore() { 208 void MockResourceLoader::CancelAndIgnore() {
163 NOTREACHED(); 209 NOTREACHED();
164 } 210 }
165 211
166 void MockResourceLoader::CancelWithError(int error_code) { 212 void MockResourceLoader::CancelWithError(int error_code) {
167 EXPECT_LT(error_code, 0); 213 EXPECT_LT(error_code, 0);
168 // Ignore double cancels. Classes shouldn't be doing this, as 214 // Ignore double cancels. Classes shouldn't be doing this, as
169 // ResourceLoader doesn't really support it, but they do anywways. :( 215 // ResourceLoader doesn't really support it, but they do anywways. :(
170 // TODO(mmenke): Remove this check. 216 // TODO(mmenke): Remove this check.
171 if (error_code_ != net::OK) 217 if (error_code_ != net::OK)
172 return; 218 return;
173 219
174 // ResourceLoader really expects canceled not to be called synchronously, but 220 // ResourceLoader really expects canceled not to be called synchronously, but
175 // a lot of code does it, so allow it. 221 // a lot of code does it, so allow it.
176 // TODO(mmenke): Remove CALLING_HANDLER. 222 // TODO(mmenke): Remove CALLING_HANDLER.
177 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING || 223 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING ||
178 status_ == Status::CALLING_HANDLER || status_ == Status::IDLE); 224 status_ == Status::CALLING_HANDLER || status_ == Status::IDLE);
179 status_ = Status::CANCELED; 225 status_ = Status::CANCELED;
180 error_code_ = error_code; 226 error_code_ = error_code;
227 if (canceled_or_idle_run_loop_)
228 canceled_or_idle_run_loop_->Quit();
181 } 229 }
182 230
183 void MockResourceLoader::Resume() { 231 void MockResourceLoader::Resume() {
184 EXPECT_EQ(Status::CALLBACK_PENDING, status_); 232 EXPECT_EQ(Status::CALLBACK_PENDING, status_);
185 status_ = Status::IDLE; 233 status_ = Status::IDLE;
234 if (canceled_or_idle_run_loop_)
235 canceled_or_idle_run_loop_->Quit();
186 } 236 }
187 237
188 } // namespace content 238 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/mock_resource_loader.h ('k') | content/browser/loader/mojo_async_resource_handler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698