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 bool result = resource_handler_->OnRequestRedirected(redirect_info, | |
55 response.get(), &defer); | |
Randy Smith (Not in Mondays)
2017/01/11 19:57:15
Ok, I'm sure there's something subtle I'm missing
mmenke
2017/01/11 20:39:14
Great question! This is indeed rather unclear.
I
Randy Smith (Not in Mondays)
2017/01/11 21:11:02
Shouldn't there be a comment to this effect in res
mmenke
2017/01/11 21:16:37
SGTM. I'll update the RH interface update CL with
Randy Smith (Not in Mondays)
2017/01/11 21:35:53
FWIW, I'd support the interface change (at some ti
| |
56 // The second case isn't really allowed, but a number of classes do it | |
57 // anyways. | |
58 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | |
59 (result == false && status_ == Status::CANCELED)); | |
60 if (!result) { | |
61 status_ = Status::CANCELED; | |
62 } else if (defer) { | |
63 status_ = Status::CALLBACK_PENDING; | |
64 } else { | |
65 status_ = Status::IDLE; | |
66 } | |
67 return status_; | |
68 } | |
69 | |
70 MockResourceLoader::Status MockResourceLoader::OnResponseStarted( | |
71 scoped_refptr<ResourceResponse> response) { | |
72 EXPECT_EQ(Status::IDLE, status_); | |
73 status_ = Status::CALLING_HANDLER; | |
74 | |
75 bool defer = false; | |
76 bool result = resource_handler_->OnResponseStarted(response.get(), &defer); | |
77 // The second case isn't really allowed, but a number of classes do it | |
78 // anyways. | |
79 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | |
80 (result == false && status_ == Status::CANCELED)); | |
81 if (!result) { | |
82 status_ = Status::CANCELED; | |
83 } else if (defer) { | |
84 status_ = Status::CALLBACK_PENDING; | |
85 } else { | |
86 status_ = Status::IDLE; | |
87 } | |
88 return status_; | |
89 } | |
90 | |
91 MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) { | |
92 EXPECT_EQ(Status::IDLE, status_); | |
93 status_ = Status::CALLING_HANDLER; | |
94 | |
95 scoped_refptr<net::IOBuffer> buf; | |
96 int buf_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 | |
99 // anyways. | |
100 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | |
101 (result == false && status_ == Status::CANCELED)); | |
102 if (!result) { | |
103 status_ = Status::CANCELED; | |
104 } else { | |
105 EXPECT_LE(min_size, buf_size); | |
106 status_ = Status::IDLE; | |
107 } | |
108 return status_; | |
109 }; | |
110 | |
111 MockResourceLoader::Status MockResourceLoader::OnReadCompleted(int bytes_read) { | |
112 EXPECT_EQ(Status::IDLE, status_); | |
113 status_ = Status::CALLING_HANDLER; | |
114 | |
115 bool defer = false; | |
116 bool result = resource_handler_->OnReadCompleted(bytes_read, &defer); | |
117 // The second case isn't really allowed, but a number of classes do it | |
118 // anyways. | |
119 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | |
120 (result == false && status_ == Status::CANCELED)); | |
121 if (!result) { | |
122 status_ = Status::CANCELED; | |
123 } else if (defer) { | |
124 status_ = Status::CALLBACK_PENDING; | |
125 } else { | |
126 status_ = Status::IDLE; | |
127 } | |
128 return status_; | |
129 } | |
130 | |
131 MockResourceLoader::Status MockResourceLoader::OnResponseCompleted( | |
132 const net::URLRequestStatus& status) { | |
133 // This should only happen while the ResourceLoader is idle or the request was | |
134 // canceled. | |
135 EXPECT_TRUE(status_ == Status::IDLE || | |
136 (!status.is_success() && status_ == Status::CANCELED && | |
137 error_code_ == status.error())); | |
138 | |
139 status_ = Status::CALLING_HANDLER; | |
140 | |
141 bool defer = false; | |
142 resource_handler_->OnResponseCompleted(status, &defer); | |
143 EXPECT_EQ(Status::CALLING_HANDLER, status_); | |
144 if (defer) { | |
145 status_ = Status::CALLBACK_PENDING; | |
146 } else { | |
147 status_ = Status::IDLE; | |
148 } | |
149 return status_; | |
150 } | |
151 | |
152 void MockResourceLoader::Cancel() { | |
153 CancelWithError(net::ERR_ABORTED); | |
154 } | |
155 | |
156 void MockResourceLoader::CancelAndIgnore() { | |
157 NOTREACHED(); | |
158 } | |
159 | |
160 void MockResourceLoader::CancelWithError(int error_code) { | |
161 EXPECT_LT(error_code, 0); | |
162 // Ignore double cancels. Classes shouldn't be doing this, as | |
163 // ResourceLoader doesn't really support it, but they do anywways. :( | |
164 // TODO(mmenke): Remove this check. | |
165 if (error_code_ != net::OK) | |
166 return; | |
167 | |
168 // ResourceLoader really expects canceled not to be called synchronously, but | |
169 // a lot of code does it, so allow it. | |
170 // TODO(mmenke): Remove CALLING_HANDLER. | |
171 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING || | |
172 status_ == Status::CALLING_HANDLER || status_ == Status::IDLE); | |
173 status_ = Status::CANCELED; | |
174 error_code_ = error_code; | |
175 } | |
176 | |
177 void MockResourceLoader::Resume() { | |
178 EXPECT_EQ(Status::CALLBACK_PENDING, status_); | |
179 status_ = Status::IDLE; | |
180 } | |
181 | |
182 } // namespace content | |
OLD | NEW |