Commit 5159f105 authored by mmenke's avatar mmenke Committed by Commit bot

Update MojoAsyncResourceHandler tests to use MockResourceLoader.

TBR=clamy@chromium.org
BUG=659317

Review-Url: https://codereview.chromium.org/2581393002
Cr-Commit-Position: refs/heads/master@{#443263}
parent 063f0c9d
...@@ -104,9 +104,9 @@ specific_include_rules = { ...@@ -104,9 +104,9 @@ specific_include_rules = {
], ],
"mojo_async_resource_handler_unittest\.cc": [ "mojo_async_resource_handler_unittest\.cc": [
"-content", "-content",
"+content/browser/loader/mock_resource_loader.h",
"+content/browser/loader/mojo_async_resource_handler.h", "+content/browser/loader/mojo_async_resource_handler.h",
"+content/browser/loader/test_url_loader_client.h", "+content/browser/loader/test_url_loader_client.h",
"+content/public/browser/navigation_data.h",
"+content/browser/loader/resource_controller.h", "+content/browser/loader/resource_controller.h",
"+content/browser/loader/resource_dispatcher_host_impl.h", "+content/browser/loader/resource_dispatcher_host_impl.h",
"+content/browser/loader/resource_request_info_impl.h", "+content/browser/loader/resource_request_info_impl.h",
...@@ -114,6 +114,7 @@ specific_include_rules = { ...@@ -114,6 +114,7 @@ specific_include_rules = {
"+content/common/resource_request_completion_status.h", "+content/common/resource_request_completion_status.h",
"+content/common/url_loader.mojom.h", "+content/common/url_loader.mojom.h",
"+content/public/browser/appcache_service.h", "+content/public/browser/appcache_service.h",
"+content/public/browser/navigation_data.h",
"+content/public/browser/resource_context.h", "+content/public/browser/resource_context.h",
"+content/public/browser/resource_dispatcher_host_delegate.h", "+content/public/browser/resource_dispatcher_host_delegate.h",
"+content/public/browser/resource_throttle.h", "+content/public/browser/resource_throttle.h",
......
...@@ -96,30 +96,43 @@ MockResourceLoader::Status MockResourceLoader::OnResponseStarted( ...@@ -96,30 +96,43 @@ MockResourceLoader::Status MockResourceLoader::OnResponseStarted(
MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) { MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) {
EXPECT_EQ(Status::IDLE, status_); EXPECT_EQ(Status::IDLE, status_);
EXPECT_FALSE(io_buffer_);
EXPECT_EQ(0, io_buffer_size_);
status_ = Status::CALLING_HANDLER; status_ = Status::CALLING_HANDLER;
scoped_refptr<net::IOBuffer> buf; bool result =
int buf_size; resource_handler_->OnWillRead(&io_buffer_, &io_buffer_size_, min_size);
bool result = resource_handler_->OnWillRead(&buf, &buf_size, min_size);
// The second case isn't really allowed, but a number of classes do it // The second case isn't really allowed, but a number of classes do it
// anyways. // anyways.
EXPECT_TRUE(status_ == Status::CALLING_HANDLER || EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
(result == false && status_ == Status::CANCELED)); (result == false && status_ == Status::CANCELED));
if (!result) { if (!result) {
EXPECT_EQ(0, io_buffer_size_);
EXPECT_FALSE(io_buffer_);
status_ = Status::CANCELED; status_ = Status::CANCELED;
} else { } else {
EXPECT_LE(min_size, buf_size); EXPECT_LE(min_size, io_buffer_size_);
EXPECT_LT(0, io_buffer_size_);
EXPECT_TRUE(io_buffer_);
status_ = Status::IDLE; status_ = Status::IDLE;
} }
return status_; return status_;
}; };
MockResourceLoader::Status MockResourceLoader::OnReadCompleted(int bytes_read) { MockResourceLoader::Status MockResourceLoader::OnReadCompleted(
base::StringPiece bytes) {
EXPECT_EQ(Status::IDLE, status_); EXPECT_EQ(Status::IDLE, status_);
EXPECT_LE(bytes.size(), static_cast<size_t>(io_buffer_size_));
status_ = Status::CALLING_HANDLER;
std::copy(bytes.begin(), bytes.end(), io_buffer_->data());
io_buffer_ = nullptr;
io_buffer_size_ = 0;
status_ = Status::CALLING_HANDLER; status_ = Status::CALLING_HANDLER;
bool defer = false; bool defer = false;
bool result = resource_handler_->OnReadCompleted(bytes_read, &defer); bool result = resource_handler_->OnReadCompleted(bytes.size(), &defer);
// The second case isn't really allowed, but a number of classes do it // The second case isn't really allowed, but a number of classes do it
// anyways. // anyways.
EXPECT_TRUE(status_ == Status::CALLING_HANDLER || EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
...@@ -142,6 +155,8 @@ MockResourceLoader::Status MockResourceLoader::OnResponseCompleted( ...@@ -142,6 +155,8 @@ MockResourceLoader::Status MockResourceLoader::OnResponseCompleted(
(!status.is_success() && status_ == Status::CANCELED && (!status.is_success() && status_ == Status::CANCELED &&
error_code_ == status.error())); error_code_ == status.error()));
io_buffer_ = nullptr;
io_buffer_size_ = 0;
status_ = Status::CALLING_HANDLER; status_ = Status::CALLING_HANDLER;
bool defer = false; bool defer = false;
...@@ -155,6 +170,37 @@ MockResourceLoader::Status MockResourceLoader::OnResponseCompleted( ...@@ -155,6 +170,37 @@ MockResourceLoader::Status MockResourceLoader::OnResponseCompleted(
return status_; return status_;
} }
MockResourceLoader::Status
MockResourceLoader::OnResponseCompletedFromExternalOutOfBandCancel(
const net::URLRequestStatus& url_request_status) {
// This can happen at any point, except from a recursive call from
// ResourceHandler.
EXPECT_NE(Status::CALLING_HANDLER, status_);
io_buffer_ = nullptr;
io_buffer_size_ = 0;
status_ = Status::CALLING_HANDLER;
bool defer = false;
resource_handler_->OnResponseCompleted(url_request_status, &defer);
EXPECT_EQ(Status::CALLING_HANDLER, status_);
if (defer) {
status_ = Status::CALLBACK_PENDING;
} else {
status_ = Status::IDLE;
}
return status_;
}
void MockResourceLoader::WaitUntilIdleOrCanceled() {
if (status_ == Status::IDLE || status_ == Status::CANCELED)
return;
EXPECT_FALSE(canceled_or_idle_run_loop_);
canceled_or_idle_run_loop_.reset(new base::RunLoop());
canceled_or_idle_run_loop_->Run();
EXPECT_TRUE(status_ == Status::IDLE || status_ == Status::CANCELED);
}
void MockResourceLoader::Cancel() { void MockResourceLoader::Cancel() {
CancelWithError(net::ERR_ABORTED); CancelWithError(net::ERR_ABORTED);
} }
...@@ -178,11 +224,15 @@ void MockResourceLoader::CancelWithError(int error_code) { ...@@ -178,11 +224,15 @@ void MockResourceLoader::CancelWithError(int error_code) {
status_ == Status::CALLING_HANDLER || status_ == Status::IDLE); status_ == Status::CALLING_HANDLER || status_ == Status::IDLE);
status_ = Status::CANCELED; status_ = Status::CANCELED;
error_code_ = error_code; error_code_ = error_code;
if (canceled_or_idle_run_loop_)
canceled_or_idle_run_loop_->Quit();
} }
void MockResourceLoader::Resume() { void MockResourceLoader::Resume() {
EXPECT_EQ(Status::CALLBACK_PENDING, status_); EXPECT_EQ(Status::CALLBACK_PENDING, status_);
status_ = Status::IDLE; status_ = Status::IDLE;
if (canceled_or_idle_run_loop_)
canceled_or_idle_run_loop_->Quit();
} }
} // namespace content } // namespace content
...@@ -10,7 +10,10 @@ ...@@ -10,7 +10,10 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/strings/string_piece.h"
#include "content/browser/loader/resource_controller.h" #include "content/browser/loader/resource_controller.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
class GURL; class GURL;
...@@ -56,15 +59,31 @@ class MockResourceLoader : public ResourceController { ...@@ -56,15 +59,31 @@ class MockResourceLoader : public ResourceController {
scoped_refptr<ResourceResponse> response); scoped_refptr<ResourceResponse> response);
Status OnResponseStarted(scoped_refptr<ResourceResponse> response); Status OnResponseStarted(scoped_refptr<ResourceResponse> response);
Status OnWillRead(int min_size); Status OnWillRead(int min_size);
Status OnReadCompleted(int bytes_read); Status OnReadCompleted(base::StringPiece bytes);
Status OnResponseCompleted(const net::URLRequestStatus& status); Status OnResponseCompleted(const net::URLRequestStatus& status);
// Simulates an out-of-band cancel from some source other than the
// ResourceHandler |this| was created with (like another ResourceHandler). The
// difference between this and OnResponseCompleted() is that this has fewer
// sanity checks to validate the cancel was in-band.
Status OnResponseCompletedFromExternalOutOfBandCancel(
const net::URLRequestStatus& url_request_status);
// Waits until status() is IDLE or CANCELED. If that's already the case, does
// nothing.
void WaitUntilIdleOrCanceled();
Status status() const { return status_; } Status status() const { return status_; }
// Network error passed to the first CancelWithError() / Cancel() call, which // Network error passed to the first CancelWithError() / Cancel() call, which
// is the one the real code uses in the case of multiple cancels. // is the one the real code uses in the case of multiple cancels.
int error_code() const { return error_code_; } int error_code() const { return error_code_; }
// Returns IOBuffer returned by last call to OnWillRead. The IOBuffer is
// release on read complete, on response complete, and on cancel.
net::IOBuffer* io_buffer() const { return io_buffer_.get(); }
int io_buffer_size() const { return io_buffer_size_; }
private: private:
// ResourceController implementation. // ResourceController implementation.
void Cancel() override; void Cancel() override;
...@@ -77,6 +96,11 @@ class MockResourceLoader : public ResourceController { ...@@ -77,6 +96,11 @@ class MockResourceLoader : public ResourceController {
Status status_ = Status::IDLE; Status status_ = Status::IDLE;
int error_code_ = net::OK; int error_code_ = net::OK;
scoped_refptr<net::IOBuffer> io_buffer_;
int io_buffer_size_ = 0;
std::unique_ptr<base::RunLoop> canceled_or_idle_run_loop_;
DISALLOW_COPY_AND_ASSIGN(MockResourceLoader); DISALLOW_COPY_AND_ASSIGN(MockResourceLoader);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment