OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/loader/mock_resource_loader.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "content/browser/loader/resource_controller.h" |
| 12 #include "content/browser/loader/resource_handler.h" |
| 13 #include "content/public/common/resource_response.h" |
| 14 #include "net/base/io_buffer.h" |
| 15 #include "net/url_request/url_request_status.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 MockResourceLoader::MockResourceLoader(ResourceHandler* resource_handler) |
| 21 : resource_handler_(resource_handler) { |
| 22 resource_handler_->SetController(this); |
| 23 } |
| 24 |
| 25 MockResourceLoader::~MockResourceLoader() {} |
| 26 |
| 27 MockResourceLoader::Status MockResourceLoader::OnWillStart(const GURL& url) { |
| 28 EXPECT_EQ(Status::IDLE, status_); |
| 29 status_ = Status::CALLING_HANDLER; |
| 30 |
| 31 bool defer = false; |
| 32 bool result = resource_handler_->OnWillStart(url, &defer); |
| 33 // The second case isn't really allowed, but a number of classes do it |
| 34 // anyways. |
| 35 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || |
| 36 (result == false && status_ == Status::CANCELED)); |
| 37 if (!result) { |
| 38 status_ = Status::CANCELED; |
| 39 } else if (defer) { |
| 40 status_ = Status::CALLBACK_PENDING; |
| 41 } else { |
| 42 status_ = Status::IDLE; |
| 43 } |
| 44 return status_; |
| 45 } |
| 46 |
| 47 MockResourceLoader::Status MockResourceLoader::OnRequestRedirected( |
| 48 const net::RedirectInfo& redirect_info, |
| 49 scoped_refptr<ResourceResponse> response) { |
| 50 EXPECT_EQ(Status::IDLE, status_); |
| 51 status_ = Status::CALLING_HANDLER; |
| 52 |
| 53 bool defer = false; |
| 54 // Note that |this| does not hold onto |response|, to match ResourceLoader's |
| 55 // behavior. If |resource_handler_| wants to use |response| asynchronously, it |
| 56 // needs to hold onto its own pointer to it. |
| 57 bool result = resource_handler_->OnRequestRedirected(redirect_info, |
| 58 response.get(), &defer); |
| 59 // The second case isn't really allowed, but a number of classes do it |
| 60 // anyways. |
| 61 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || |
| 62 (result == false && status_ == Status::CANCELED)); |
| 63 if (!result) { |
| 64 status_ = Status::CANCELED; |
| 65 } else if (defer) { |
| 66 status_ = Status::CALLBACK_PENDING; |
| 67 } else { |
| 68 status_ = Status::IDLE; |
| 69 } |
| 70 return status_; |
| 71 } |
| 72 |
| 73 MockResourceLoader::Status MockResourceLoader::OnResponseStarted( |
| 74 scoped_refptr<ResourceResponse> response) { |
| 75 EXPECT_EQ(Status::IDLE, status_); |
| 76 status_ = Status::CALLING_HANDLER; |
| 77 |
| 78 bool defer = false; |
| 79 // Note that |this| does not hold onto |response|, to match ResourceLoader's |
| 80 // behavior. If |resource_handler_| wants to use |response| asynchronously, it |
| 81 // needs to hold onto its own pointer to it. |
| 82 bool result = resource_handler_->OnResponseStarted(response.get(), &defer); |
| 83 // The second case isn't really allowed, but a number of classes do it |
| 84 // anyways. |
| 85 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || |
| 86 (result == false && status_ == Status::CANCELED)); |
| 87 if (!result) { |
| 88 status_ = Status::CANCELED; |
| 89 } else if (defer) { |
| 90 status_ = Status::CALLBACK_PENDING; |
| 91 } else { |
| 92 status_ = Status::IDLE; |
| 93 } |
| 94 return status_; |
| 95 } |
| 96 |
| 97 MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) { |
| 98 EXPECT_EQ(Status::IDLE, status_); |
| 99 status_ = Status::CALLING_HANDLER; |
| 100 |
| 101 scoped_refptr<net::IOBuffer> buf; |
| 102 int buf_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 |
| 105 // anyways. |
| 106 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || |
| 107 (result == false && status_ == Status::CANCELED)); |
| 108 if (!result) { |
| 109 status_ = Status::CANCELED; |
| 110 } else { |
| 111 EXPECT_LE(min_size, buf_size); |
| 112 status_ = Status::IDLE; |
| 113 } |
| 114 return status_; |
| 115 }; |
| 116 |
| 117 MockResourceLoader::Status MockResourceLoader::OnReadCompleted(int bytes_read) { |
| 118 EXPECT_EQ(Status::IDLE, status_); |
| 119 status_ = Status::CALLING_HANDLER; |
| 120 |
| 121 bool defer = false; |
| 122 bool result = resource_handler_->OnReadCompleted(bytes_read, &defer); |
| 123 // The second case isn't really allowed, but a number of classes do it |
| 124 // anyways. |
| 125 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || |
| 126 (result == false && status_ == Status::CANCELED)); |
| 127 if (!result) { |
| 128 status_ = Status::CANCELED; |
| 129 } else if (defer) { |
| 130 status_ = Status::CALLBACK_PENDING; |
| 131 } else { |
| 132 status_ = Status::IDLE; |
| 133 } |
| 134 return status_; |
| 135 } |
| 136 |
| 137 MockResourceLoader::Status MockResourceLoader::OnResponseCompleted( |
| 138 const net::URLRequestStatus& status) { |
| 139 // This should only happen while the ResourceLoader is idle or the request was |
| 140 // canceled. |
| 141 EXPECT_TRUE(status_ == Status::IDLE || |
| 142 (!status.is_success() && status_ == Status::CANCELED && |
| 143 error_code_ == status.error())); |
| 144 |
| 145 status_ = Status::CALLING_HANDLER; |
| 146 |
| 147 bool defer = false; |
| 148 resource_handler_->OnResponseCompleted(status, &defer); |
| 149 EXPECT_EQ(Status::CALLING_HANDLER, status_); |
| 150 if (defer) { |
| 151 status_ = Status::CALLBACK_PENDING; |
| 152 } else { |
| 153 status_ = Status::IDLE; |
| 154 } |
| 155 return status_; |
| 156 } |
| 157 |
| 158 void MockResourceLoader::Cancel() { |
| 159 CancelWithError(net::ERR_ABORTED); |
| 160 } |
| 161 |
| 162 void MockResourceLoader::CancelAndIgnore() { |
| 163 NOTREACHED(); |
| 164 } |
| 165 |
| 166 void MockResourceLoader::CancelWithError(int error_code) { |
| 167 EXPECT_LT(error_code, 0); |
| 168 // Ignore double cancels. Classes shouldn't be doing this, as |
| 169 // ResourceLoader doesn't really support it, but they do anywways. :( |
| 170 // TODO(mmenke): Remove this check. |
| 171 if (error_code_ != net::OK) |
| 172 return; |
| 173 |
| 174 // ResourceLoader really expects canceled not to be called synchronously, but |
| 175 // a lot of code does it, so allow it. |
| 176 // TODO(mmenke): Remove CALLING_HANDLER. |
| 177 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING || |
| 178 status_ == Status::CALLING_HANDLER || status_ == Status::IDLE); |
| 179 status_ = Status::CANCELED; |
| 180 error_code_ = error_code; |
| 181 } |
| 182 |
| 183 void MockResourceLoader::Resume() { |
| 184 EXPECT_EQ(Status::CALLBACK_PENDING, status_); |
| 185 status_ = Status::IDLE; |
| 186 } |
| 187 |
| 188 } // namespace content |
OLD | NEW |