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

Side by Side Diff: components/favicon/core/large_icon_service_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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/favicon/core/large_icon_service.h" 5 #include "components/favicon/core/large_icon_service.h"
6 6
7 #include <deque> 7 #include <deque>
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 101
102 class MockImageFetcher : public image_fetcher::ImageFetcher { 102 class MockImageFetcher : public image_fetcher::ImageFetcher {
103 public: 103 public:
104 MOCK_METHOD1(SetImageFetcherDelegate, 104 MOCK_METHOD1(SetImageFetcherDelegate,
105 void(image_fetcher::ImageFetcherDelegate* delegate)); 105 void(image_fetcher::ImageFetcherDelegate* delegate));
106 MOCK_METHOD1(SetDataUseServiceName, 106 MOCK_METHOD1(SetDataUseServiceName,
107 void(image_fetcher::ImageFetcher::DataUseServiceName name)); 107 void(image_fetcher::ImageFetcher::DataUseServiceName name));
108 MOCK_METHOD1(SetImageDownloadLimit, 108 MOCK_METHOD1(SetImageDownloadLimit,
109 void(base::Optional<int64_t> max_download_bytes)); 109 void(base::Optional<int64_t> max_download_bytes));
110 MOCK_METHOD1(SetDesiredImageFrameSize, void(const gfx::Size& size)); 110 MOCK_METHOD1(SetDesiredImageFrameSize, void(const gfx::Size& size));
111 MOCK_METHOD3(StartOrQueueNetworkRequest, 111 MOCK_METHOD4(StartOrQueueNetworkRequest,
112 void(const std::string&, 112 void(const std::string&,
113 const GURL&, 113 const GURL&,
114 const ImageFetcherCallback&)); 114 const ImageFetcherCallback&,
115 const net::NetworkTrafficAnnotationTag&));
115 MOCK_METHOD0(GetImageDecoder, image_fetcher::ImageDecoder*()); 116 MOCK_METHOD0(GetImageDecoder, image_fetcher::ImageDecoder*());
116 }; 117 };
117 118
118 class LargeIconServiceTest : public testing::Test { 119 class LargeIconServiceTest : public testing::Test {
119 public: 120 public:
120 LargeIconServiceTest() 121 LargeIconServiceTest()
121 : mock_image_fetcher_(new NiceMock<MockImageFetcher>()), 122 : mock_image_fetcher_(new NiceMock<MockImageFetcher>()),
122 large_icon_service_(&mock_favicon_service_, 123 large_icon_service_(&mock_favicon_service_,
123 base::ThreadTaskRunnerHandle::Get(), 124 base::ThreadTaskRunnerHandle::Get(),
124 base::WrapUnique(mock_image_fetcher_)) {} 125 base::WrapUnique(mock_image_fetcher_)) {}
(...skipping 15 matching lines...) Expand all
140 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServer) { 141 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServer) {
141 const GURL kExpectedServerUrl( 142 const GURL kExpectedServerUrl(
142 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" 143 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true"
143 "&check_seen=true&size=61&min_size=42&max_size=122" 144 "&check_seen=true&size=61&min_size=42&max_size=122"
144 "&fallback_opts=TYPE,SIZE,URL&url=http://www.example.com/"); 145 "&fallback_opts=TYPE,SIZE,URL&url=http://www.example.com/");
145 146
146 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); 147 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0);
147 148
148 base::MockCallback<base::Callback<void(bool success)>> callback; 149 base::MockCallback<base::Callback<void(bool success)>> callback;
149 EXPECT_CALL(*mock_image_fetcher_, 150 EXPECT_CALL(*mock_image_fetcher_,
150 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) 151 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _, _))
151 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap( 152 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap(
152 CreateTestSkBitmap(64, 64, kTestColor)))); 153 CreateTestSkBitmap(64, 64, kTestColor))));
153 EXPECT_CALL(mock_favicon_service_, 154 EXPECT_CALL(mock_favicon_service_,
154 SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl, 155 SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl,
155 favicon_base::IconType::TOUCH_ICON, _, _)) 156 favicon_base::IconType::TOUCH_ICON, _, _))
156 .WillOnce(PostBoolReply(true)); 157 .WillOnce(PostBoolReply(true));
157 158
158 large_icon_service_ 159 large_icon_service_
159 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 160 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
160 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, 161 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42,
(...skipping 15 matching lines...) Expand all
176 {"desired_to_max_size_factor", "1.5"}}, 177 {"desired_to_max_size_factor", "1.5"}},
177 {"LargeIconServiceFetching"}); 178 {"LargeIconServiceFetching"});
178 const GURL kExpectedServerUrl( 179 const GURL kExpectedServerUrl(
179 "https://t0.gstatic.com/" 180 "https://t0.gstatic.com/"
180 "faviconV2?size=61&min_size=43&max_size=91&url=http://www.example.com/"); 181 "faviconV2?size=61&min_size=43&max_size=91&url=http://www.example.com/");
181 182
182 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); 183 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0);
183 184
184 base::MockCallback<base::Callback<void(bool success)>> callback; 185 base::MockCallback<base::Callback<void(bool success)>> callback;
185 EXPECT_CALL(*mock_image_fetcher_, 186 EXPECT_CALL(*mock_image_fetcher_,
186 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) 187 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _, _))
187 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap( 188 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap(
188 CreateTestSkBitmap(64, 64, kTestColor)))); 189 CreateTestSkBitmap(64, 64, kTestColor))));
189 EXPECT_CALL(mock_favicon_service_, 190 EXPECT_CALL(mock_favicon_service_,
190 SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl, 191 SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl,
191 favicon_base::IconType::TOUCH_ICON, _, _)) 192 favicon_base::IconType::TOUCH_ICON, _, _))
192 .WillOnce(PostBoolReply(true)); 193 .WillOnce(PostBoolReply(true));
193 194
194 large_icon_service_ 195 large_icon_service_
195 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 196 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
196 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, 197 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42,
197 /*desired_size_in_pixel=*/61, callback.Get()); 198 /*desired_size_in_pixel=*/61, callback.Get());
198 199
199 EXPECT_CALL(callback, Run(true)); 200 EXPECT_CALL(callback, Run(true));
200 base::RunLoop().RunUntilIdle(); 201 base::RunLoop().RunUntilIdle();
201 } 202 }
202 203
203 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServerWithOriginalUrl) { 204 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServerWithOriginalUrl) {
204 const GURL kExpectedServerUrl( 205 const GURL kExpectedServerUrl(
205 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" 206 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true"
206 "&check_seen=true&size=61&min_size=42&max_size=122" 207 "&check_seen=true&size=61&min_size=42&max_size=122"
207 "&fallback_opts=TYPE,SIZE,URL&url=http://www.example.com/"); 208 "&fallback_opts=TYPE,SIZE,URL&url=http://www.example.com/");
208 const GURL kExpectedOriginalUrl("http://www.example.com/favicon.png"); 209 const GURL kExpectedOriginalUrl("http://www.example.com/favicon.png");
209 210
210 image_fetcher::RequestMetadata expected_metadata; 211 image_fetcher::RequestMetadata expected_metadata;
211 expected_metadata.content_location_header = kExpectedOriginalUrl.spec(); 212 expected_metadata.content_location_header = kExpectedOriginalUrl.spec();
212 EXPECT_CALL(*mock_image_fetcher_, 213 EXPECT_CALL(*mock_image_fetcher_,
213 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) 214 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _, _))
214 .WillOnce(PostFetchReplyWithMetadata( 215 .WillOnce(PostFetchReplyWithMetadata(
215 gfx::Image::CreateFrom1xBitmap( 216 gfx::Image::CreateFrom1xBitmap(
216 CreateTestSkBitmap(64, 64, kTestColor)), 217 CreateTestSkBitmap(64, 64, kTestColor)),
217 expected_metadata)); 218 expected_metadata));
218 EXPECT_CALL(mock_favicon_service_, 219 EXPECT_CALL(mock_favicon_service_,
219 SetLastResortFavicons(GURL(kDummyUrl), kExpectedOriginalUrl, 220 SetLastResortFavicons(GURL(kDummyUrl), kExpectedOriginalUrl,
220 favicon_base::IconType::TOUCH_ICON, _, _)) 221 favicon_base::IconType::TOUCH_ICON, _, _))
221 .WillOnce(PostBoolReply(true)); 222 .WillOnce(PostBoolReply(true));
222 223
223 base::MockCallback<base::Callback<void(bool success)>> callback; 224 base::MockCallback<base::Callback<void(bool success)>> callback;
224 large_icon_service_ 225 large_icon_service_
225 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 226 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
226 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, 227 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42,
227 /*desired_size_in_pixel=*/61, callback.Get()); 228 /*desired_size_in_pixel=*/61, callback.Get());
228 229
229 EXPECT_CALL(callback, Run(true)); 230 EXPECT_CALL(callback, Run(true));
230 base::RunLoop().RunUntilIdle(); 231 base::RunLoop().RunUntilIdle();
231 } 232 }
232 233
233 TEST_F(LargeIconServiceTest, ShouldTrimQueryParametersForGoogleServer) { 234 TEST_F(LargeIconServiceTest, ShouldTrimQueryParametersForGoogleServer) {
234 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1"); 235 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1");
235 const GURL kExpectedServerUrl( 236 const GURL kExpectedServerUrl(
236 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" 237 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true"
237 "&check_seen=true&size=61&min_size=42&max_size=122" 238 "&check_seen=true&size=61&min_size=42&max_size=122"
238 "&fallback_opts=TYPE,SIZE,URL&url=http://www.example.com/"); 239 "&fallback_opts=TYPE,SIZE,URL&url=http://www.example.com/");
239 240
240 EXPECT_CALL(*mock_image_fetcher_, 241 EXPECT_CALL(*mock_image_fetcher_,
241 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) 242 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _, _))
242 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap( 243 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap(
243 CreateTestSkBitmap(64, 64, kTestColor)))); 244 CreateTestSkBitmap(64, 64, kTestColor))));
244 // Verify that the non-trimmed page URL is used when writing to the database. 245 // Verify that the non-trimmed page URL is used when writing to the database.
245 EXPECT_CALL(mock_favicon_service_, 246 EXPECT_CALL(mock_favicon_service_,
246 SetLastResortFavicons(_, kExpectedServerUrl, _, _, _)); 247 SetLastResortFavicons(_, kExpectedServerUrl, _, _, _));
247 248
248 large_icon_service_ 249 large_icon_service_
249 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 250 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
250 GURL(kDummyUrlWithQuery), /*min_source_size_in_pixel=*/42, 251 GURL(kDummyUrlWithQuery), /*min_source_size_in_pixel=*/42,
251 /*desired_size_in_pixel=*/61, base::Callback<void(bool success)>()); 252 /*desired_size_in_pixel=*/61, base::Callback<void(bool success)>());
252 253
253 base::RunLoop().RunUntilIdle(); 254 base::RunLoop().RunUntilIdle();
254 } 255 }
255 256
256 TEST_F(LargeIconServiceTest, ShouldNotQueryGoogleServerIfInvalidScheme) { 257 TEST_F(LargeIconServiceTest, ShouldNotQueryGoogleServerIfInvalidScheme) {
257 const GURL kDummyFtpUrl("ftp://www.example.com"); 258 const GURL kDummyFtpUrl("ftp://www.example.com");
258 259
259 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _)) 260 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _, _))
260 .Times(0); 261 .Times(0);
261 262
262 base::MockCallback<base::Callback<void(bool success)>> callback; 263 base::MockCallback<base::Callback<void(bool success)>> callback;
263 264
264 large_icon_service_ 265 large_icon_service_
265 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 266 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
266 GURL(kDummyFtpUrl), /*min_source_size_in_pixel=*/42, 267 GURL(kDummyFtpUrl), /*min_source_size_in_pixel=*/42,
267 /*desired_size_in_pixel=*/61, callback.Get()); 268 /*desired_size_in_pixel=*/61, callback.Get());
268 269
269 EXPECT_CALL(callback, Run(false)); 270 EXPECT_CALL(callback, Run(false));
270 base::RunLoop().RunUntilIdle(); 271 base::RunLoop().RunUntilIdle();
271 EXPECT_THAT(histogram_tester_.GetAllSamples( 272 EXPECT_THAT(histogram_tester_.GetAllSamples(
272 "Favicons.LargeIconService.DownloadedSize"), 273 "Favicons.LargeIconService.DownloadedSize"),
273 IsEmpty()); 274 IsEmpty());
274 } 275 }
275 276
276 TEST_F(LargeIconServiceTest, ShouldReportUnavailableIfFetchFromServerFails) { 277 TEST_F(LargeIconServiceTest, ShouldReportUnavailableIfFetchFromServerFails) {
277 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1"); 278 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1");
278 const GURL kExpectedServerUrl( 279 const GURL kExpectedServerUrl(
279 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" 280 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true"
280 "&check_seen=true&size=61&min_size=42&max_size=122" 281 "&check_seen=true&size=61&min_size=42&max_size=122"
281 "&fallback_opts=TYPE,SIZE,URL&url=http://www.example.com/"); 282 "&fallback_opts=TYPE,SIZE,URL&url=http://www.example.com/");
282 283
283 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _)) 284 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _))
284 .Times(0); 285 .Times(0);
285 286
286 base::MockCallback<base::Callback<void(bool success)>> callback; 287 base::MockCallback<base::Callback<void(bool success)>> callback;
287 EXPECT_CALL(*mock_image_fetcher_, 288 EXPECT_CALL(*mock_image_fetcher_,
288 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) 289 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _, _))
289 .WillOnce(PostFetchReply(gfx::Image())); 290 .WillOnce(PostFetchReply(gfx::Image()));
290 EXPECT_CALL(mock_favicon_service_, 291 EXPECT_CALL(mock_favicon_service_,
291 UnableToDownloadFavicon(kExpectedServerUrl)); 292 UnableToDownloadFavicon(kExpectedServerUrl));
292 293
293 large_icon_service_ 294 large_icon_service_
294 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 295 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
295 kDummyUrlWithQuery, /*min_source_size_in_pixel=*/42, 296 kDummyUrlWithQuery, /*min_source_size_in_pixel=*/42,
296 /*desired_size_in_pixel=*/61, callback.Get()); 297 /*desired_size_in_pixel=*/61, callback.Get());
297 298
298 EXPECT_CALL(callback, Run(false)); 299 EXPECT_CALL(callback, Run(false));
299 base::RunLoop().RunUntilIdle(); 300 base::RunLoop().RunUntilIdle();
300 // Verify that download failure gets recorded. 301 // Verify that download failure gets recorded.
301 histogram_tester_.ExpectUniqueSample( 302 histogram_tester_.ExpectUniqueSample(
302 "Favicons.LargeIconService.DownloadedSize", 0, /*expected_count=*/1); 303 "Favicons.LargeIconService.DownloadedSize", 0, /*expected_count=*/1);
303 } 304 }
304 305
305 TEST_F(LargeIconServiceTest, ShouldNotGetFromGoogleServerIfUnavailable) { 306 TEST_F(LargeIconServiceTest, ShouldNotGetFromGoogleServerIfUnavailable) {
306 ON_CALL( 307 ON_CALL(
307 mock_favicon_service_, 308 mock_favicon_service_,
308 WasUnableToDownloadFavicon(GURL( 309 WasUnableToDownloadFavicon(GURL(
309 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" 310 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true"
310 "&check_seen=true&size=61&min_size=42&max_size=122" 311 "&check_seen=true&size=61&min_size=42&max_size=122"
311 "&fallback_opts=TYPE,SIZE,URL&url=http://www.example.com/"))) 312 "&fallback_opts=TYPE,SIZE,URL&url=http://www.example.com/")))
312 .WillByDefault(Return(true)); 313 .WillByDefault(Return(true));
313 314
314 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); 315 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0);
315 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _)) 316 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _, _))
316 .Times(0); 317 .Times(0);
317 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _)) 318 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _))
318 .Times(0); 319 .Times(0);
319 320
320 base::MockCallback<base::Callback<void(bool success)>> callback; 321 base::MockCallback<base::Callback<void(bool success)>> callback;
321 large_icon_service_ 322 large_icon_service_
322 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 323 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
323 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, 324 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42,
324 /*desired_size_in_pixel=*/61, callback.Get()); 325 /*desired_size_in_pixel=*/61, callback.Get());
325 326
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 484
484 // Every test will appear with suffix /0 (param false) and /1 (param true), e.g. 485 // Every test will appear with suffix /0 (param false) and /1 (param true), e.g.
485 // LargeIconServiceGetterTest.FallbackSinceTooPicky/0: get image. 486 // LargeIconServiceGetterTest.FallbackSinceTooPicky/0: get image.
486 // LargeIconServiceGetterTest.FallbackSinceTooPicky/1: get raw bitmap. 487 // LargeIconServiceGetterTest.FallbackSinceTooPicky/1: get raw bitmap.
487 INSTANTIATE_TEST_CASE_P(, // Empty instatiation name. 488 INSTANTIATE_TEST_CASE_P(, // Empty instatiation name.
488 LargeIconServiceGetterTest, 489 LargeIconServiceGetterTest,
489 ::testing::Values(false, true)); 490 ::testing::Values(false, true));
490 491
491 } // namespace 492 } // namespace
492 } // namespace favicon 493 } // namespace favicon
OLDNEW
« no previous file with comments | « components/favicon/core/large_icon_service.cc ('k') | components/image_fetcher/core/image_data_fetcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698