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

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

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

Powered by Google App Engine
This is Rietveld 408576698