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

Side by Side Diff: content/browser/loader/test_resource_handler.h

Issue 2542843006: ResourceLoader: Fix a bunch of double-cancellation/double-error notification cases. (Closed)
Patch Set: Fix 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
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 #ifndef CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_ 5 #ifndef CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_
6 #define CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_ 6 #define CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_
7 7
8 #include <memory>
8 #include <string> 9 #include <string>
9 10
10 #include "base/macros.h" 11 #include "base/macros.h"
11 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/run_loop.h"
12 #include "content/browser/loader/resource_handler.h" 14 #include "content/browser/loader/resource_handler.h"
13 #include "net/base/io_buffer.h" 15 #include "net/base/io_buffer.h"
14 16 #include "net/base/net_errors.h"
15 class GURL; 17 #include "net/url_request/url_request_status.h"
18 #include "url/gurl.h"
16 19
17 namespace net { 20 namespace net {
18 class URLRequestStatus; 21 class URLRequestStatus;
19 } 22 }
20 23
21 namespace content { 24 namespace content {
22 25
26 class ResourceController;
23 class ResourceHandler; 27 class ResourceHandler;
24 struct ResourceResponse; 28 struct ResourceResponse;
25 29
26 // A test version of a ResourceHandler. It returns a configurable buffer in 30 // A test version of a ResourceHandler. It returns a configurable buffer in
27 // response to OnWillStart. It records what ResourceHandler methods are called, 31 // response to OnWillStart. It records what ResourceHandler methods are called,
28 // and verifies that they are called in the correct order. It can optionally 32 // and verifies that they are called in the correct order. It can optionally
29 // defer or fail the request at any stage, and record the response body and 33 // defer or fail the request at any stage, and record the response body and
30 // final status it sees. Redirects currently not supported. 34 // final status it sees. Redirects currently not supported.
31 class TestResourceHandler : public ResourceHandler { 35 class TestResourceHandler : public ResourceHandler {
32 public: 36 public:
(...skipping 12 matching lines...) Expand all
45 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; 49 bool OnResponseStarted(ResourceResponse* response, bool* defer) override;
46 bool OnWillStart(const GURL& url, bool* defer) override; 50 bool OnWillStart(const GURL& url, bool* defer) override;
47 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, 51 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
48 int* buf_size, 52 int* buf_size,
49 int min_size) override; 53 int min_size) override;
50 bool OnReadCompleted(int bytes_read, bool* defer) override; 54 bool OnReadCompleted(int bytes_read, bool* defer) override;
51 void OnResponseCompleted(const net::URLRequestStatus& status, 55 void OnResponseCompleted(const net::URLRequestStatus& status,
52 bool* defer) override; 56 bool* defer) override;
53 void OnDataDownloaded(int bytes_downloaded) override; 57 void OnDataDownloaded(int bytes_downloaded) override;
54 58
59 // Invoke the corresponding methods on the ResourceHandler's
60 // ResourceController.
61 void Resume();
62 void CancelWithError(net::Error net_error);
63
55 // Sets the size of the read buffer returned by OnWillRead. Releases reference 64 // Sets the size of the read buffer returned by OnWillRead. Releases reference
56 // to previous read buffer. Default size is 2048 bytes. 65 // to previous read buffer. Default size is 2048 bytes.
57 void SetBufferSize(int buffer_size); 66 void SetBufferSize(int buffer_size);
58 67
59 scoped_refptr<net::IOBuffer> buffer() const { return buffer_; } 68 scoped_refptr<net::IOBuffer> buffer() const { return buffer_; }
60 69
61 // Sets the result returned by each method. All default to returning true. 70 // Sets the result returned by each method. All default to returning true.
62 void set_on_will_start_result(bool on_will_start_result) { 71 void set_on_will_start_result(bool on_will_start_result) {
63 on_will_start_result_ = on_will_start_result; 72 on_will_start_result_ = on_will_start_result;
64 } 73 }
74 void set_on_request_redirected_result(bool on_request_redirected_result) {
75 on_request_redirected_result_ = on_request_redirected_result;
76 }
65 void set_on_response_started_result(bool on_response_started_result) { 77 void set_on_response_started_result(bool on_response_started_result) {
66 on_response_started_result_ = on_response_started_result; 78 on_response_started_result_ = on_response_started_result;
67 } 79 }
68 void set_on_will_read_result(bool on_will_read_result) { 80 void set_on_will_read_result(bool on_will_read_result) {
69 on_will_read_result_ = on_will_read_result; 81 on_will_read_result_ = on_will_read_result;
70 } 82 }
71 void set_on_read_completed_result(bool on_read_completed_result) { 83 void set_on_read_completed_result(bool on_read_completed_result) {
72 on_read_completed_result_ = on_read_completed_result; 84 on_read_completed_result_ = on_read_completed_result;
73 } 85 }
86 void set_on_on_read_eof_result(bool on_on_read_eof_result) {
87 on_on_read_eof_result_ = on_on_read_eof_result;
88 }
74 89
75 // Cause |defer| to be set to true when the specified method is invoked. The 90 // Cause |defer| to be set to true when the specified method is invoked. The
76 // test itself is responsible for resuming the request after deferral. 91 // test itself is responsible for resuming the request after deferral.
77 92
78 void set_defer_on_will_start(bool defer_on_will_start) { 93 void set_defer_on_will_start(bool defer_on_will_start) {
79 defer_on_will_start_ = defer_on_will_start; 94 defer_on_will_start_ = defer_on_will_start;
80 } 95 }
96 void set_defer_on_request_redirected(bool defer_on_request_redirected) {
97 defer_on_request_redirected_ = defer_on_request_redirected;
98 }
81 void set_defer_on_response_started(bool defer_on_response_started) { 99 void set_defer_on_response_started(bool defer_on_response_started) {
82 defer_on_response_started_ = defer_on_response_started; 100 defer_on_response_started_ = defer_on_response_started;
83 } 101 }
84 // Only the next OnReadCompleted call will set |defer| to true. 102 // Only the next OnReadCompleted call will set |defer| to true.
85 void set_defer_on_read_completed(bool defer_on_read_completed) { 103 void set_defer_on_read_completed(bool defer_on_read_completed) {
86 defer_on_read_completed_ = defer_on_read_completed; 104 defer_on_read_completed_ = defer_on_read_completed;
87 } 105 }
106 // The final-byte read will set |defer| to true.
107 void set_defer_on_read_eof(bool defer_on_read_eof) {
108 defer_on_read_eof_ = defer_on_read_eof;
109 }
88 void set_defer_on_response_completed(bool defer_on_response_completed) { 110 void set_defer_on_response_completed(bool defer_on_response_completed) {
89 defer_on_response_completed_ = defer_on_response_completed; 111 defer_on_response_completed_ = defer_on_response_completed;
90 } 112 }
91 113
114 // Set if OnDataDownloaded calls are expected instead of
115 // OnWillRead/OnReadCompleted.
116 void set_expect_on_data_downloaded(bool expect_on_data_downloaded) {
117 expect_on_data_downloaded_ = expect_on_data_downloaded;
118 }
119
120 // Sets whether to expect a final 0-byte read on success. Defaults to true.
121 void set_expect_eof_read(bool expect_eof_read) {
122 expect_eof_read_ = expect_eof_read;
123 }
124
92 // Return the number of times the corresponding method was invoked. 125 // Return the number of times the corresponding method was invoked.
93 126
94 int on_will_start_called() const { return on_will_start_called_; } 127 int on_will_start_called() const { return on_will_start_called_; }
95 // Redirection currently not supported. 128 int on_request_redirected_called() const {
96 int on_request_redirected_called() const { return 0; } 129 return on_request_redirected_called_;
130 }
97 int on_response_started_called() const { return on_response_started_called_; } 131 int on_response_started_called() const { return on_response_started_called_; }
98 int on_will_read_called() const { return on_will_read_called_; } 132 int on_will_read_called() const { return on_will_read_called_; }
99 int on_read_completed_called() const { return on_read_completed_called_; } 133 int on_read_completed_called() const { return on_read_completed_called_; }
134 int on_read_eof() const { return on_read_eof_; }
100 int on_response_completed_called() const { 135 int on_response_completed_called() const {
101 return on_response_completed_called_; 136 return on_response_completed_called_;
102 } 137 }
103 138
139 // URL passed to OnResponseStarted, if it was called.
140 const GURL& start_url() const { return start_url_; }
141
142 ResourceResponse* resource_response() { return resource_response_.get(); };
143
144 int total_bytes_downloaded() const { return total_bytes_downloaded_; }
145
146 const std::string& body() const { return body_; }
147 net::URLRequestStatus final_status() const { return final_status_; }
148
149 // Spins the message loop until the request is deferred. Using this is
150 // optional, but if used, must use it exclusively to wait for the request. If
151 // the request was deferred and then resumed/canceled without calling this
152 // method, behavior is undefined.
153 void WaitUntilDeferred();
154
155 void WaitUntilResponseComplete();
156
104 private: 157 private:
105 net::URLRequestStatus* request_status_; 158 // TODO(mmenke): Remove these, in favor of final_status_ and body_.
106 std::string* body_; 159 net::URLRequestStatus* request_status_ptr_;
160 std::string* body_ptr_;
161
107 scoped_refptr<net::IOBuffer> buffer_; 162 scoped_refptr<net::IOBuffer> buffer_;
108 size_t buffer_size_; 163 size_t buffer_size_;
109 164
165 ResourceController* controller_;
166
110 bool on_will_start_result_ = true; 167 bool on_will_start_result_ = true;
168 bool on_request_redirected_result_ = true;
111 bool on_response_started_result_ = true; 169 bool on_response_started_result_ = true;
112 bool on_will_read_result_ = true; 170 bool on_will_read_result_ = true;
113 bool on_read_completed_result_ = true; 171 bool on_read_completed_result_ = true;
172 bool on_on_read_eof_result_ = true;
114 173
115 bool defer_on_will_start_ = false; 174 bool defer_on_will_start_ = false;
175 bool defer_on_request_redirected_ = false;
116 bool defer_on_response_started_ = false; 176 bool defer_on_response_started_ = false;
117 bool defer_on_read_completed_ = false; 177 bool defer_on_read_completed_ = false;
178 bool defer_on_read_eof_ = false;
118 bool defer_on_response_completed_ = false; 179 bool defer_on_response_completed_ = false;
119 180
181 bool expect_on_data_downloaded_ = false;
182
183 bool expect_eof_read_ = true;
184
120 int on_will_start_called_ = 0; 185 int on_will_start_called_ = 0;
186 int on_request_redirected_called_ = 0;
121 int on_response_started_called_ = 0; 187 int on_response_started_called_ = 0;
122 int on_will_read_called_ = 0; 188 int on_will_read_called_ = 0;
123 int on_read_completed_called_ = 0; 189 int on_read_completed_called_ = 0;
190 int on_read_eof_ = 0;
124 int on_response_completed_called_ = 0; 191 int on_response_completed_called_ = 0;
125 192
193 GURL start_url_;
194 scoped_refptr<ResourceResponse> resource_response_;
195 int total_bytes_downloaded_ = 0;
196 std::string body_;
197 net::URLRequestStatus final_status_ =
198 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED);
199 bool canceled_ = false;
200
201 std::unique_ptr<base::RunLoop> deferred_run_loop_;
202
203 base::RunLoop response_complete_run_loop_;
204
126 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); 205 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler);
127 }; 206 };
128 207
129 } // namespace content 208 } // namespace content
130 209
131 #endif // CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_ 210 #endif // CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_
OLDNEW
« no previous file with comments | « content/browser/loader/resource_loader_unittest.cc ('k') | content/browser/loader/test_resource_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698