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

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

Issue 2781473003: Add |SetImageDownloadLimit| to ImageFetcher to limit downloaded bytes (Closed)
Patch Set: Test fix: Mock pure virtual Setter in CrOS notification test Created 3 years, 8 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"
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 // multiple URLFetchers being created. 194 // multiple URLFetchers being created.
195 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); 195 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
196 ASSERT_NE(nullptr, test_url_fetcher); 196 ASSERT_NE(nullptr, test_url_fetcher);
197 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 197 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
198 198
199 test_url_fetcher = fetcher_factory_.GetFetcherByID(1); 199 test_url_fetcher = fetcher_factory_.GetFetcherByID(1);
200 ASSERT_NE(nullptr, test_url_fetcher); 200 ASSERT_NE(nullptr, test_url_fetcher);
201 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 201 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
202 } 202 }
203 203
204 TEST_F(ImageDataFetcherTest, FetchImageData_CancelFetchIfImageExceedsMaxSize) {
205 // In order to know whether the fetcher was canceled, it must notify about its
206 // deletion.
207 fetcher_factory_.set_remove_fetcher_on_delete(true);
208
209 const int64_t kMaxDownloadBytes = 1024 * 1024;
210 image_data_fetcher_.SetImageDownloadLimit(kMaxDownloadBytes);
211 image_data_fetcher_.FetchImageData(
212 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched,
213 base::Unretained(this)));
214
215 // Fetching an oversized image will behave like any other failed request.
216 // There will be exactly one call to OnImageDataFetched containing a response
217 // code that would be impossible for a completed fetch.
218 RequestMetadata expected_metadata;
219 expected_metadata.http_response_code = net::URLFetcher::RESPONSE_CODE_INVALID;
220 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata));
221
222 // Get and configure the TestURLFetcher.
223 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
224 ASSERT_NE(nullptr, test_url_fetcher);
225
226 // Create a completely valid response that is never used. This is to make sure
227 // that the answer isn't accidentally invalid but intentionally.
228 test_url_fetcher->set_status(
229 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
230 test_url_fetcher->SetResponseString(kURLResponseData);
231 test_url_fetcher->set_response_code(net::HTTP_OK);
232 std::string raw_header =
233 "HTTP/1.1 200 OK\n"
234 "Content-type: image/png\n\n";
235 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0');
236 scoped_refptr<net::HttpResponseHeaders> headers(
237 new net::HttpResponseHeaders(raw_header));
238 test_url_fetcher->set_response_headers(headers);
239
240 test_url_fetcher->delegate()->OnURLFetchDownloadProgress(
241 test_url_fetcher,
242 /*current=*/0, // Bytes received up to the call.
243 /*total=*/-1, // not determined
244 /*current_network_bytes=*/0); // not relevant
245 // The URL fetch should be running ...
246 ASSERT_NE(nullptr, fetcher_factory_.GetFetcherByID(0));
247
248 test_url_fetcher->delegate()->OnURLFetchDownloadProgress(
249 test_url_fetcher,
250 768 * 1024, // Current bytes are not exeeding the limit.
251 /*total=*/-1, /*current_network_bytes=*/0);
252 // ... and running ...
253 ASSERT_NE(nullptr, fetcher_factory_.GetFetcherByID(0));
254
255 test_url_fetcher->delegate()->OnURLFetchDownloadProgress(
256 test_url_fetcher, kMaxDownloadBytes, // Still not exeeding the limit.
257 /*total=*/-1, /*current_network_bytes=*/0);
258 // ... and running ...
259 ASSERT_NE(nullptr, fetcher_factory_.GetFetcherByID(0));
260
261 test_url_fetcher->delegate()->OnURLFetchDownloadProgress(
262 test_url_fetcher, kMaxDownloadBytes + 1, // Limits are exceeded.
263 /*total=*/-1, /*current_network_bytes=*/0);
264 // ... and be canceled.
265 EXPECT_EQ(nullptr, fetcher_factory_.GetFetcherByID(0));
266 }
267
204 } // namespace image_fetcher 268 } // 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