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

Side by Side Diff: components/image_fetcher/core/image_data_fetcher_unittest.cc

Issue 2794343002: Network traffic annotation added to image_data_fetcher. (Closed)
Patch Set: Comments addressed. Created 3 years, 7 months 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 #include "components/image_fetcher/core/image_data_fetcher.h" 5 #include "components/image_fetcher/core/image_data_fetcher.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "net/base/load_flags.h" 13 #include "net/base/load_flags.h"
14 #include "net/http/http_response_headers.h" 14 #include "net/http/http_response_headers.h"
15 #include "net/http/http_status_code.h" 15 #include "net/http/http_status_code.h"
16 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
16 #include "net/url_request/test_url_fetcher_factory.h" 17 #include "net/url_request/test_url_fetcher_factory.h"
17 #include "net/url_request/url_request_status.h" 18 #include "net/url_request/url_request_status.h"
18 #include "net/url_request/url_request_test_util.h" 19 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 22
22 namespace { 23 namespace {
23 24
24 const char kImageURL[] = "http://www.example.com/image"; 25 const char kImageURL[] = "http://www.example.com/image";
25 const char kURLResponseData[] = "EncodedImageData"; 26 const char kURLResponseData[] = "EncodedImageData";
(...skipping 27 matching lines...) Expand all
53 ImageDataFetcher image_data_fetcher_; 54 ImageDataFetcher image_data_fetcher_;
54 55
55 net::TestURLFetcherFactory fetcher_factory_; 56 net::TestURLFetcherFactory fetcher_factory_;
56 57
57 private: 58 private:
58 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcherTest); 59 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcherTest);
59 }; 60 };
60 61
61 TEST_F(ImageDataFetcherTest, FetchImageData) { 62 TEST_F(ImageDataFetcherTest, FetchImageData) {
62 image_data_fetcher_.FetchImageData( 63 image_data_fetcher_.FetchImageData(
63 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, 64 GURL(kImageURL),
64 base::Unretained(this))); 65 base::Bind(&ImageDataFetcherTest::OnImageDataFetched,
66 base::Unretained(this)),
67 TRAFFIC_ANNOTATION_FOR_TESTS);
65 68
66 RequestMetadata expected_metadata; 69 RequestMetadata expected_metadata;
67 expected_metadata.mime_type = std::string("image/png"); 70 expected_metadata.mime_type = std::string("image/png");
68 expected_metadata.http_response_code = net::HTTP_OK; 71 expected_metadata.http_response_code = net::HTTP_OK;
69 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData), 72 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData),
70 expected_metadata)); 73 expected_metadata));
71 74
72 // Get and configure the TestURLFetcher. 75 // Get and configure the TestURLFetcher.
73 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); 76 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
74 ASSERT_NE(nullptr, test_url_fetcher); 77 ASSERT_NE(nullptr, test_url_fetcher);
(...skipping 13 matching lines...) Expand all
88 scoped_refptr<net::HttpResponseHeaders> headers( 91 scoped_refptr<net::HttpResponseHeaders> headers(
89 new net::HttpResponseHeaders(raw_header)); 92 new net::HttpResponseHeaders(raw_header));
90 test_url_fetcher->set_response_headers(headers); 93 test_url_fetcher->set_response_headers(headers);
91 94
92 // Call the URLFetcher delegate to continue the test. 95 // Call the URLFetcher delegate to continue the test.
93 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 96 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
94 } 97 }
95 98
96 TEST_F(ImageDataFetcherTest, FetchImageData_FromCache) { 99 TEST_F(ImageDataFetcherTest, FetchImageData_FromCache) {
97 image_data_fetcher_.FetchImageData( 100 image_data_fetcher_.FetchImageData(
98 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, 101 GURL(kImageURL),
99 base::Unretained(this))); 102 base::Bind(&ImageDataFetcherTest::OnImageDataFetched,
103 base::Unretained(this)),
104 TRAFFIC_ANNOTATION_FOR_TESTS);
100 105
101 RequestMetadata expected_metadata; 106 RequestMetadata expected_metadata;
102 expected_metadata.mime_type = std::string("image/png"); 107 expected_metadata.mime_type = std::string("image/png");
103 expected_metadata.http_response_code = net::HTTP_OK; 108 expected_metadata.http_response_code = net::HTTP_OK;
104 expected_metadata.from_http_cache = true; 109 expected_metadata.from_http_cache = true;
105 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData), 110 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData),
106 expected_metadata)); 111 expected_metadata));
107 112
108 // Get and configure the TestURLFetcher. 113 // Get and configure the TestURLFetcher.
109 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); 114 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
(...skipping 11 matching lines...) Expand all
121 scoped_refptr<net::HttpResponseHeaders> headers( 126 scoped_refptr<net::HttpResponseHeaders> headers(
122 new net::HttpResponseHeaders(raw_header)); 127 new net::HttpResponseHeaders(raw_header));
123 test_url_fetcher->set_response_headers(headers); 128 test_url_fetcher->set_response_headers(headers);
124 129
125 // Call the URLFetcher delegate to continue the test. 130 // Call the URLFetcher delegate to continue the test.
126 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 131 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
127 } 132 }
128 133
129 TEST_F(ImageDataFetcherTest, FetchImageData_NotFound) { 134 TEST_F(ImageDataFetcherTest, FetchImageData_NotFound) {
130 image_data_fetcher_.FetchImageData( 135 image_data_fetcher_.FetchImageData(
131 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, 136 GURL(kImageURL),
132 base::Unretained(this))); 137 base::Bind(&ImageDataFetcherTest::OnImageDataFetched,
138 base::Unretained(this)),
139 TRAFFIC_ANNOTATION_FOR_TESTS);
133 140
134 RequestMetadata expected_metadata; 141 RequestMetadata expected_metadata;
135 expected_metadata.mime_type = std::string("image/png"); 142 expected_metadata.mime_type = std::string("image/png");
136 expected_metadata.http_response_code = net::HTTP_NOT_FOUND; 143 expected_metadata.http_response_code = net::HTTP_NOT_FOUND;
137 // For 404, expect an empty result even though correct image data is sent. 144 // For 404, expect an empty result even though correct image data is sent.
138 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata)); 145 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata));
139 146
140 // Get and configure the TestURLFetcher. 147 // Get and configure the TestURLFetcher.
141 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); 148 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
142 ASSERT_NE(nullptr, test_url_fetcher); 149 ASSERT_NE(nullptr, test_url_fetcher);
143 test_url_fetcher->set_status( 150 test_url_fetcher->set_status(
144 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); 151 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
145 test_url_fetcher->SetResponseString(kURLResponseData); 152 test_url_fetcher->SetResponseString(kURLResponseData);
146 153
147 std::string raw_header = 154 std::string raw_header =
148 "HTTP/1.1 404 Not Found\n" 155 "HTTP/1.1 404 Not Found\n"
149 "Content-type: image/png\n\n"; 156 "Content-type: image/png\n\n";
150 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0'); 157 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0');
151 scoped_refptr<net::HttpResponseHeaders> headers( 158 scoped_refptr<net::HttpResponseHeaders> headers(
152 new net::HttpResponseHeaders(raw_header)); 159 new net::HttpResponseHeaders(raw_header));
153 test_url_fetcher->set_response_headers(headers); 160 test_url_fetcher->set_response_headers(headers);
154 161
155 // Call the URLFetcher delegate to continue the test. 162 // Call the URLFetcher delegate to continue the test.
156 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 163 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
157 } 164 }
158 165
159 TEST_F(ImageDataFetcherTest, FetchImageData_WithContentLocation) { 166 TEST_F(ImageDataFetcherTest, FetchImageData_WithContentLocation) {
160 image_data_fetcher_.FetchImageData( 167 image_data_fetcher_.FetchImageData(
161 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, 168 GURL(kImageURL),
162 base::Unretained(this))); 169 base::Bind(&ImageDataFetcherTest::OnImageDataFetched,
170 base::Unretained(this)),
171 TRAFFIC_ANNOTATION_FOR_TESTS);
163 172
164 RequestMetadata expected_metadata; 173 RequestMetadata expected_metadata;
165 expected_metadata.mime_type = std::string("image/png"); 174 expected_metadata.mime_type = std::string("image/png");
166 expected_metadata.http_response_code = net::HTTP_NOT_FOUND; 175 expected_metadata.http_response_code = net::HTTP_NOT_FOUND;
167 expected_metadata.content_location_header = "http://test-location/image.png"; 176 expected_metadata.content_location_header = "http://test-location/image.png";
168 // For 404, expect an empty result even though correct image data is sent. 177 // For 404, expect an empty result even though correct image data is sent.
169 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata)); 178 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata));
170 179
171 // Get and configure the TestURLFetcher. 180 // Get and configure the TestURLFetcher.
172 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); 181 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
(...skipping 12 matching lines...) Expand all
185 test_url_fetcher->set_response_headers(headers); 194 test_url_fetcher->set_response_headers(headers);
186 195
187 // Call the URLFetcher delegate to continue the test. 196 // Call the URLFetcher delegate to continue the test.
188 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 197 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
189 } 198 }
190 199
191 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { 200 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) {
192 image_data_fetcher_.FetchImageData( 201 image_data_fetcher_.FetchImageData(
193 GURL(kImageURL), 202 GURL(kImageURL),
194 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, 203 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest,
195 base::Unretained(this))); 204 base::Unretained(this)),
205 TRAFFIC_ANNOTATION_FOR_TESTS);
196 206
197 RequestMetadata expected_metadata; 207 RequestMetadata expected_metadata;
198 expected_metadata.http_response_code = net::URLFetcher::RESPONSE_CODE_INVALID; 208 expected_metadata.http_response_code = net::URLFetcher::RESPONSE_CODE_INVALID;
199 EXPECT_CALL( 209 EXPECT_CALL(
200 *this, OnImageDataFetchedFailedRequest(std::string(), expected_metadata)); 210 *this, OnImageDataFetchedFailedRequest(std::string(), expected_metadata));
201 211
202 // Get and configure the TestURLFetcher. 212 // Get and configure the TestURLFetcher.
203 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); 213 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
204 ASSERT_NE(nullptr, test_url_fetcher); 214 ASSERT_NE(nullptr, test_url_fetcher);
205 test_url_fetcher->set_status(net::URLRequestStatus( 215 test_url_fetcher->set_status(net::URLRequestStatus(
206 net::URLRequestStatus::FAILED, net::ERR_INVALID_URL)); 216 net::URLRequestStatus::FAILED, net::ERR_INVALID_URL));
207 217
208 // Call the URLFetcher delegate to continue the test. 218 // Call the URLFetcher delegate to continue the test.
209 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 219 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
210 } 220 }
211 221
212 TEST_F(ImageDataFetcherTest, FetchImageData_MultipleRequests) { 222 TEST_F(ImageDataFetcherTest, FetchImageData_MultipleRequests) {
213 ImageDataFetcher::ImageDataFetcherCallback callback = 223 ImageDataFetcher::ImageDataFetcherCallback callback =
214 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests, 224 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests,
215 base::Unretained(this)); 225 base::Unretained(this));
216 EXPECT_CALL(*this, OnImageDataFetchedMultipleRequests(testing::_, testing::_)) 226 EXPECT_CALL(*this, OnImageDataFetchedMultipleRequests(testing::_, testing::_))
217 .Times(2); 227 .Times(2);
218 228
219 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback); 229 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback,
220 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback); 230 TRAFFIC_ANNOTATION_FOR_TESTS);
231 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback,
232 TRAFFIC_ANNOTATION_FOR_TESTS);
221 233
222 // Multiple calls to FetchImageData for the same URL will result in 234 // Multiple calls to FetchImageData for the same URL will result in
223 // multiple URLFetchers being created. 235 // multiple URLFetchers being created.
224 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); 236 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
225 ASSERT_NE(nullptr, test_url_fetcher); 237 ASSERT_NE(nullptr, test_url_fetcher);
226 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 238 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
227 239
228 test_url_fetcher = fetcher_factory_.GetFetcherByID(1); 240 test_url_fetcher = fetcher_factory_.GetFetcherByID(1);
229 ASSERT_NE(nullptr, test_url_fetcher); 241 ASSERT_NE(nullptr, test_url_fetcher);
230 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 242 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
231 } 243 }
232 244
233 TEST_F(ImageDataFetcherTest, FetchImageData_CancelFetchIfImageExceedsMaxSize) { 245 TEST_F(ImageDataFetcherTest, FetchImageData_CancelFetchIfImageExceedsMaxSize) {
234 // In order to know whether the fetcher was canceled, it must notify about its 246 // In order to know whether the fetcher was canceled, it must notify about its
235 // deletion. 247 // deletion.
236 fetcher_factory_.set_remove_fetcher_on_delete(true); 248 fetcher_factory_.set_remove_fetcher_on_delete(true);
237 249
238 const int64_t kMaxDownloadBytes = 1024 * 1024; 250 const int64_t kMaxDownloadBytes = 1024 * 1024;
239 image_data_fetcher_.SetImageDownloadLimit(kMaxDownloadBytes); 251 image_data_fetcher_.SetImageDownloadLimit(kMaxDownloadBytes);
240 image_data_fetcher_.FetchImageData( 252 image_data_fetcher_.FetchImageData(
241 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, 253 GURL(kImageURL),
242 base::Unretained(this))); 254 base::Bind(&ImageDataFetcherTest::OnImageDataFetched,
255 base::Unretained(this)),
256 TRAFFIC_ANNOTATION_FOR_TESTS);
243 257
244 // Fetching an oversized image will behave like any other failed request. 258 // Fetching an oversized image will behave like any other failed request.
245 // There will be exactly one call to OnImageDataFetched containing a response 259 // There will be exactly one call to OnImageDataFetched containing a response
246 // code that would be impossible for a completed fetch. 260 // code that would be impossible for a completed fetch.
247 RequestMetadata expected_metadata; 261 RequestMetadata expected_metadata;
248 expected_metadata.http_response_code = net::URLFetcher::RESPONSE_CODE_INVALID; 262 expected_metadata.http_response_code = net::URLFetcher::RESPONSE_CODE_INVALID;
249 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata)); 263 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata));
250 264
251 // Get and configure the TestURLFetcher. 265 // Get and configure the TestURLFetcher.
252 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); 266 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 ASSERT_NE(nullptr, fetcher_factory_.GetFetcherByID(0)); 302 ASSERT_NE(nullptr, fetcher_factory_.GetFetcherByID(0));
289 303
290 test_url_fetcher->delegate()->OnURLFetchDownloadProgress( 304 test_url_fetcher->delegate()->OnURLFetchDownloadProgress(
291 test_url_fetcher, kMaxDownloadBytes + 1, // Limits are exceeded. 305 test_url_fetcher, kMaxDownloadBytes + 1, // Limits are exceeded.
292 /*total=*/-1, /*current_network_bytes=*/0); 306 /*total=*/-1, /*current_network_bytes=*/0);
293 // ... and be canceled. 307 // ... and be canceled.
294 EXPECT_EQ(nullptr, fetcher_factory_.GetFetcherByID(0)); 308 EXPECT_EQ(nullptr, fetcher_factory_.GetFetcherByID(0));
295 } 309 }
296 310
297 } // namespace image_fetcher 311 } // namespace image_fetcher
OLDNEW
« no previous file with comments | « components/image_fetcher/core/image_data_fetcher.cc ('k') | components/image_fetcher/core/image_fetcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698