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

Side by Side Diff: components/ntp_tiles/icon_cacher_impl_unittest.cc

Issue 2888393002: [Icon cacher] Add metrics for downloading favicons for NTP Tiles (Closed)
Patch Set: Robert's comments 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
« no previous file with comments | « components/ntp_tiles/icon_cacher_impl.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/ntp_tiles/icon_cacher_impl.h" 5 #include "components/ntp_tiles/icon_cacher_impl.h"
6 6
7 #include <set> 7 #include <set>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/path_service.h" 12 #include "base/path_service.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/test/histogram_tester.h"
14 #include "base/test/mock_callback.h" 15 #include "base/test/mock_callback.h"
15 #include "base/test/scoped_task_environment.h" 16 #include "base/test/scoped_task_environment.h"
16 #include "base/test/test_simple_task_runner.h" 17 #include "base/test/test_simple_task_runner.h"
17 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
18 #include "components/favicon/core/favicon_client.h" 19 #include "components/favicon/core/favicon_client.h"
19 #include "components/favicon/core/favicon_service_impl.h" 20 #include "components/favicon/core/favicon_service_impl.h"
20 #include "components/favicon/core/favicon_util.h" 21 #include "components/favicon/core/favicon_util.h"
21 #include "components/favicon/core/large_icon_service.h" 22 #include "components/favicon/core/large_icon_service.h"
22 #include "components/history/core/browser/history_database_params.h" 23 #include "components/history/core/browser/history_database_params.h"
23 #include "components/history/core/browser/history_service.h" 24 #include "components/history/core/browser/history_service.h"
24 #include "components/image_fetcher/core/image_decoder.h" 25 #include "components/image_fetcher/core/image_decoder.h"
25 #include "components/image_fetcher/core/image_fetcher.h" 26 #include "components/image_fetcher/core/image_fetcher.h"
26 #include "components/image_fetcher/core/request_metadata.h" 27 #include "components/image_fetcher/core/request_metadata.h"
27 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" 28 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
28 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
30 #include "ui/base/resource/resource_bundle.h" 31 #include "ui/base/resource/resource_bundle.h"
31 #include "ui/base/ui_base_paths.h" 32 #include "ui/base/ui_base_paths.h"
32 #include "ui/gfx/image/image_unittest_util.h" 33 #include "ui/gfx/image/image_unittest_util.h"
33 34
35 using base::Bucket;
34 using ::testing::_; 36 using ::testing::_;
37 using ::testing::ElementsAre;
35 using ::testing::Eq; 38 using ::testing::Eq;
36 using ::testing::Invoke; 39 using ::testing::Invoke;
37 using ::testing::InSequence; 40 using ::testing::InSequence;
41 using ::testing::IsEmpty;
38 using ::testing::NiceMock; 42 using ::testing::NiceMock;
39 using ::testing::Return; 43 using ::testing::Return;
40 using ::testing::ReturnArg; 44 using ::testing::ReturnArg;
41 45
42 namespace ntp_tiles { 46 namespace ntp_tiles {
43 namespace { 47 namespace {
44 48
45 class MockImageFetcher : public image_fetcher::ImageFetcher { 49 class MockImageFetcher : public image_fetcher::ImageFetcher {
46 public: 50 public:
47 MOCK_METHOD1(SetImageFetcherDelegate, 51 MOCK_METHOD1(SetImageFetcherDelegate,
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 ui::SCALE_FACTOR_NONE); 229 ui::SCALE_FACTOR_NONE);
226 } 230 }
227 231
228 PopularSites::Site site_; 232 PopularSites::Site site_;
229 std::unique_ptr<MockImageFetcher> image_fetcher_; 233 std::unique_ptr<MockImageFetcher> image_fetcher_;
230 std::unique_ptr<MockImageDecoder> image_decoder_; 234 std::unique_ptr<MockImageDecoder> image_decoder_;
231 NiceMock<MockResourceDelegate> mock_resource_delegate_; 235 NiceMock<MockResourceDelegate> mock_resource_delegate_;
232 }; 236 };
233 237
234 TEST_F(IconCacherTestPopularSites, LargeCached) { 238 TEST_F(IconCacherTestPopularSites, LargeCached) {
239 base::HistogramTester histogram_tester;
235 base::MockCallback<base::Closure> done; 240 base::MockCallback<base::Closure> done;
236 EXPECT_CALL(done, Run()).Times(0); 241 EXPECT_CALL(done, Run()).Times(0);
237 base::RunLoop loop; 242 base::RunLoop loop;
238 { 243 {
239 InSequence s; 244 InSequence s;
240 EXPECT_CALL(*image_fetcher_, 245 EXPECT_CALL(*image_fetcher_,
241 SetDataUseServiceName( 246 SetDataUseServiceName(
242 data_use_measurement::DataUseUserData::NTP_TILES)); 247 data_use_measurement::DataUseUserData::NTP_TILES));
243 EXPECT_CALL(*image_fetcher_, SetDesiredImageFrameSize(gfx::Size(128, 128))); 248 EXPECT_CALL(*image_fetcher_, SetDesiredImageFrameSize(gfx::Size(128, 128)));
244 } 249 }
245 PreloadIcon(site_.url, site_.large_icon_url, favicon_base::TOUCH_ICON, 128, 250 PreloadIcon(site_.url, site_.large_icon_url, favicon_base::TOUCH_ICON, 128,
246 128); 251 128);
247 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_)); 252 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_));
248 cacher.StartFetchPopularSites(site_, done.Get(), done.Get()); 253 cacher.StartFetchPopularSites(site_, done.Get(), done.Get());
249 WaitForMainThreadTasksToFinish(); 254 WaitForMainThreadTasksToFinish();
250 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 255 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
251 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 256 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
257 EXPECT_THAT(histogram_tester.GetAllSamples(
258 "NewTabPage.TileFaviconFetchSuccess.Popular"),
259 IsEmpty());
252 } 260 }
253 261
254 TEST_F(IconCacherTestPopularSites, LargeNotCachedAndFetchSucceeded) { 262 TEST_F(IconCacherTestPopularSites, LargeNotCachedAndFetchSucceeded) {
263 base::HistogramTester histogram_tester;
255 base::MockCallback<base::Closure> done; 264 base::MockCallback<base::Closure> done;
256 base::RunLoop loop; 265 base::RunLoop loop;
257 { 266 {
258 InSequence s; 267 InSequence s;
259 EXPECT_CALL(*image_fetcher_, 268 EXPECT_CALL(*image_fetcher_,
260 SetDataUseServiceName( 269 SetDataUseServiceName(
261 data_use_measurement::DataUseUserData::NTP_TILES)); 270 data_use_measurement::DataUseUserData::NTP_TILES));
262 EXPECT_CALL(*image_fetcher_, SetDesiredImageFrameSize(gfx::Size(128, 128))); 271 EXPECT_CALL(*image_fetcher_, SetDesiredImageFrameSize(gfx::Size(128, 128)));
263 EXPECT_CALL(*image_fetcher_, 272 EXPECT_CALL(*image_fetcher_,
264 StartOrQueueNetworkRequest(_, site_.large_icon_url, _, _)) 273 StartOrQueueNetworkRequest(_, site_.large_icon_url, _, _))
265 .WillOnce(PassFetch(128, 128)); 274 .WillOnce(PassFetch(128, 128));
266 EXPECT_CALL(done, Run()).WillOnce(Quit(&loop)); 275 EXPECT_CALL(done, Run()).WillOnce(Quit(&loop));
267 } 276 }
268 277
269 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_)); 278 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_));
270 cacher.StartFetchPopularSites(site_, done.Get(), done.Get()); 279 cacher.StartFetchPopularSites(site_, done.Get(), done.Get());
271 loop.Run(); 280 loop.Run();
272 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 281 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
273 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 282 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
283 EXPECT_THAT(histogram_tester.GetAllSamples(
284 "NewTabPage.TileFaviconFetchSuccess.Popular"),
285 ElementsAre(Bucket(/*bucket=*/true, /*count=*/1)));
274 } 286 }
275 287
276 TEST_F(IconCacherTestPopularSites, SmallNotCachedAndFetchSucceeded) { 288 TEST_F(IconCacherTestPopularSites, SmallNotCachedAndFetchSucceeded) {
277 site_.large_icon_url = GURL(); 289 site_.large_icon_url = GURL();
278 290
279 base::MockCallback<base::Closure> done; 291 base::MockCallback<base::Closure> done;
280 base::RunLoop loop; 292 base::RunLoop loop;
281 { 293 {
282 InSequence s; 294 InSequence s;
283 EXPECT_CALL(*image_fetcher_, 295 EXPECT_CALL(*image_fetcher_,
284 SetDataUseServiceName( 296 SetDataUseServiceName(
285 data_use_measurement::DataUseUserData::NTP_TILES)); 297 data_use_measurement::DataUseUserData::NTP_TILES));
286 EXPECT_CALL(*image_fetcher_, SetDesiredImageFrameSize(gfx::Size(128, 128))); 298 EXPECT_CALL(*image_fetcher_, SetDesiredImageFrameSize(gfx::Size(128, 128)));
287 EXPECT_CALL(*image_fetcher_, 299 EXPECT_CALL(*image_fetcher_,
288 StartOrQueueNetworkRequest(_, site_.favicon_url, _, _)) 300 StartOrQueueNetworkRequest(_, site_.favicon_url, _, _))
289 .WillOnce(PassFetch(128, 128)); 301 .WillOnce(PassFetch(128, 128));
290 EXPECT_CALL(done, Run()).WillOnce(Quit(&loop)); 302 EXPECT_CALL(done, Run()).WillOnce(Quit(&loop));
291 } 303 }
292 304
293 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_)); 305 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_));
294 cacher.StartFetchPopularSites(site_, done.Get(), done.Get()); 306 cacher.StartFetchPopularSites(site_, done.Get(), done.Get());
295 loop.Run(); 307 loop.Run();
296 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 308 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
297 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 309 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
298 } 310 }
299 311
300 TEST_F(IconCacherTestPopularSites, LargeNotCachedAndFetchFailed) { 312 TEST_F(IconCacherTestPopularSites, LargeNotCachedAndFetchFailed) {
313 base::HistogramTester histogram_tester;
301 base::MockCallback<base::Closure> done; 314 base::MockCallback<base::Closure> done;
302 EXPECT_CALL(done, Run()).Times(0); 315 EXPECT_CALL(done, Run()).Times(0);
303 { 316 {
304 InSequence s; 317 InSequence s;
305 EXPECT_CALL(*image_fetcher_, 318 EXPECT_CALL(*image_fetcher_,
306 SetDataUseServiceName( 319 SetDataUseServiceName(
307 data_use_measurement::DataUseUserData::NTP_TILES)); 320 data_use_measurement::DataUseUserData::NTP_TILES));
308 EXPECT_CALL(*image_fetcher_, SetDesiredImageFrameSize(gfx::Size(128, 128))); 321 EXPECT_CALL(*image_fetcher_, SetDesiredImageFrameSize(gfx::Size(128, 128)));
309 EXPECT_CALL(*image_fetcher_, 322 EXPECT_CALL(*image_fetcher_,
310 StartOrQueueNetworkRequest(_, site_.large_icon_url, _, _)) 323 StartOrQueueNetworkRequest(_, site_.large_icon_url, _, _))
311 .WillOnce(FailFetch()); 324 .WillOnce(FailFetch());
312 } 325 }
313 326
314 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_)); 327 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_));
315 cacher.StartFetchPopularSites(site_, done.Get(), done.Get()); 328 cacher.StartFetchPopularSites(site_, done.Get(), done.Get());
316 WaitForMainThreadTasksToFinish(); 329 WaitForMainThreadTasksToFinish();
317 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 330 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
318 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 331 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
332 EXPECT_THAT(
333 histogram_tester.GetAllSamples(
334 "NewTabPage.TileFaviconFetchSuccess.Popular"),
335 ElementsAre(Bucket(/*bucket=*/false, /*count=*/1)));
319 } 336 }
320 337
321 TEST_F(IconCacherTestPopularSites, HandlesEmptyCallbacksNicely) { 338 TEST_F(IconCacherTestPopularSites, HandlesEmptyCallbacksNicely) {
339 base::HistogramTester histogram_tester;
322 EXPECT_CALL(*image_fetcher_, SetDataUseServiceName(_)); 340 EXPECT_CALL(*image_fetcher_, SetDataUseServiceName(_));
323 EXPECT_CALL(*image_fetcher_, SetDesiredImageFrameSize(_)); 341 EXPECT_CALL(*image_fetcher_, SetDesiredImageFrameSize(_));
324 EXPECT_CALL(*image_fetcher_, StartOrQueueNetworkRequest(_, _, _, _)) 342 EXPECT_CALL(*image_fetcher_, StartOrQueueNetworkRequest(_, _, _, _))
325 .WillOnce(PassFetch(128, 128)); 343 .WillOnce(PassFetch(128, 128));
326 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_)); 344 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_));
327 cacher.StartFetchPopularSites(site_, base::Closure(), base::Closure()); 345 cacher.StartFetchPopularSites(site_, base::Closure(), base::Closure());
328 WaitForHistoryThreadTasksToFinish(); // Writing the icon into the DB. 346 WaitForHistoryThreadTasksToFinish(); // Writing the icon into the DB.
329 WaitForMainThreadTasksToFinish(); // Finishing tasks after the DB write. 347 WaitForMainThreadTasksToFinish(); // Finishing tasks after the DB write.
330 // Even though the callbacks are not called, the icon gets written out. 348 // Even though the callbacks are not called, the icon gets written out.
331 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 349 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
332 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 350 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
351 // The histogram gets reported despite empty callbacks.
352 EXPECT_THAT(
353 histogram_tester.GetAllSamples(
354 "NewTabPage.TileFaviconFetchSuccess.Popular"),
355 ElementsAre(Bucket(/*bucket=*/true, /*count=*/1)));
333 } 356 }
334 357
335 TEST_F(IconCacherTestPopularSites, ProvidesDefaultIconAndSucceedsWithFetching) { 358 TEST_F(IconCacherTestPopularSites, ProvidesDefaultIconAndSucceedsWithFetching) {
359 base::HistogramTester histogram_tester;
336 // The returned data string is not used by the mocked decoder. 360 // The returned data string is not used by the mocked decoder.
337 ON_CALL(mock_resource_delegate_, GetRawDataResource(12345, _, _)) 361 ON_CALL(mock_resource_delegate_, GetRawDataResource(12345, _, _))
338 .WillByDefault(Return("")); 362 .WillByDefault(Return(""));
339 // It's not important when the image_fetcher's decoder is used to decode the 363 // It's not important when the image_fetcher's decoder is used to decode the
340 // image but it must happen at some point. 364 // image but it must happen at some point.
341 EXPECT_CALL(*image_fetcher_, GetImageDecoder()) 365 EXPECT_CALL(*image_fetcher_, GetImageDecoder())
342 .WillOnce(Return(image_decoder_.get())); 366 .WillOnce(Return(image_decoder_.get()));
343 EXPECT_CALL(*image_decoder_, DecodeImage(_, gfx::Size(128, 128), _)) 367 EXPECT_CALL(*image_decoder_, DecodeImage(_, gfx::Size(128, 128), _))
344 .WillOnce(DecodeSuccessfully(64, 64)); 368 .WillOnce(DecodeSuccessfully(64, 64));
345 base::MockCallback<base::Closure> preliminary_icon_available; 369 base::MockCallback<base::Closure> preliminary_icon_available;
(...skipping 23 matching lines...) Expand all
369 preliminary_icon_available.Get()); 393 preliminary_icon_available.Get());
370 394
371 default_loop.Run(); // Wait for the default image. 395 default_loop.Run(); // Wait for the default image.
372 EXPECT_THAT(GetCachedIconFor(site_.url, favicon_base::TOUCH_ICON).Size(), 396 EXPECT_THAT(GetCachedIconFor(site_.url, favicon_base::TOUCH_ICON).Size(),
373 Eq(gfx::Size(64, 64))); // Compares dimensions, not objects. 397 Eq(gfx::Size(64, 64))); // Compares dimensions, not objects.
374 398
375 // Let the fetcher continue and wait for the second call of the callback. 399 // Let the fetcher continue and wait for the second call of the callback.
376 fetch_loop.Run(); // Wait for the updated image. 400 fetch_loop.Run(); // Wait for the updated image.
377 EXPECT_THAT(GetCachedIconFor(site_.url, favicon_base::TOUCH_ICON).Size(), 401 EXPECT_THAT(GetCachedIconFor(site_.url, favicon_base::TOUCH_ICON).Size(),
378 Eq(gfx::Size(128, 128))); // Compares dimensions, not objects. 402 Eq(gfx::Size(128, 128))); // Compares dimensions, not objects.
403 // The histogram gets reported only once (for the downloaded icon, not for the
404 // default one).
405 EXPECT_THAT(
406 histogram_tester.GetAllSamples(
407 "NewTabPage.TileFaviconFetchSuccess.Popular"),
408 ElementsAre(Bucket(/*bucket=*/true, /*count=*/1)));
379 } 409 }
380 410
381 TEST_F(IconCacherTestPopularSites, LargeNotCachedAndFetchPerformedOnlyOnce) { 411 TEST_F(IconCacherTestPopularSites, LargeNotCachedAndFetchPerformedOnlyOnce) {
382 base::MockCallback<base::Closure> done; 412 base::MockCallback<base::Closure> done;
383 base::RunLoop loop; 413 base::RunLoop loop;
384 { 414 {
385 InSequence s; 415 InSequence s;
386 // Image fetcher is used only once. 416 // Image fetcher is used only once.
387 EXPECT_CALL(*image_fetcher_, 417 EXPECT_CALL(*image_fetcher_,
388 SetDataUseServiceName( 418 SetDataUseServiceName(
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 } 453 }
424 454
425 scoped_refptr<base::TestSimpleTaskRunner> 455 scoped_refptr<base::TestSimpleTaskRunner>
426 large_icon_service_background_task_runner_; 456 large_icon_service_background_task_runner_;
427 std::unique_ptr<MockImageFetcher> fetcher_for_large_icon_service_; 457 std::unique_ptr<MockImageFetcher> fetcher_for_large_icon_service_;
428 std::unique_ptr<MockImageFetcher> fetcher_for_icon_cacher_; 458 std::unique_ptr<MockImageFetcher> fetcher_for_icon_cacher_;
429 }; 459 };
430 460
431 TEST_F(IconCacherTestMostLikely, Cached) { 461 TEST_F(IconCacherTestMostLikely, Cached) {
432 GURL page_url("http://www.site.com"); 462 GURL page_url("http://www.site.com");
463 base::HistogramTester histogram_tester;
464
433 GURL icon_url("http://www.site.com/favicon.png"); 465 GURL icon_url("http://www.site.com/favicon.png");
434 PreloadIcon(page_url, icon_url, favicon_base::TOUCH_ICON, 128, 128); 466 PreloadIcon(page_url, icon_url, favicon_base::TOUCH_ICON, 128, 128);
435 467
436 favicon::LargeIconService large_icon_service( 468 favicon::LargeIconService large_icon_service(
437 &favicon_service_, large_icon_service_background_task_runner_, 469 &favicon_service_, large_icon_service_background_task_runner_,
438 std::move(fetcher_for_large_icon_service_)); 470 std::move(fetcher_for_large_icon_service_));
439 IconCacherImpl cacher(&favicon_service_, &large_icon_service, 471 IconCacherImpl cacher(&favicon_service_, &large_icon_service,
440 std::move(fetcher_for_icon_cacher_)); 472 std::move(fetcher_for_icon_cacher_));
441 473
442 base::MockCallback<base::Closure> done; 474 base::MockCallback<base::Closure> done;
443 EXPECT_CALL(done, Run()).Times(0); 475 EXPECT_CALL(done, Run()).Times(0);
444 cacher.StartFetchMostLikely(page_url, done.Get()); 476 cacher.StartFetchMostLikely(page_url, done.Get());
445 WaitForMainThreadTasksToFinish(); 477 WaitForMainThreadTasksToFinish();
446 478
447 EXPECT_FALSE(IconIsCachedFor(page_url, favicon_base::FAVICON)); 479 EXPECT_FALSE(IconIsCachedFor(page_url, favicon_base::FAVICON));
448 EXPECT_TRUE(IconIsCachedFor(page_url, favicon_base::TOUCH_ICON)); 480 EXPECT_TRUE(IconIsCachedFor(page_url, favicon_base::TOUCH_ICON));
481 EXPECT_THAT(histogram_tester.GetAllSamples(
482 "NewTabPage.TileFaviconFetchSuccess.Server"),
483 IsEmpty());
449 } 484 }
450 485
451 TEST_F(IconCacherTestMostLikely, NotCachedAndFetchSucceeded) { 486 TEST_F(IconCacherTestMostLikely, NotCachedAndFetchSucceeded) {
452 GURL page_url("http://www.site.com"); 487 GURL page_url("http://www.site.com");
488 base::HistogramTester histogram_tester;
453 489
454 base::MockCallback<base::Closure> done; 490 base::MockCallback<base::Closure> done;
455 base::RunLoop loop; 491 base::RunLoop loop;
456 { 492 {
457 InSequence s; 493 InSequence s;
458 EXPECT_CALL(*fetcher_for_large_icon_service_, 494 EXPECT_CALL(*fetcher_for_large_icon_service_,
459 SetDataUseServiceName( 495 SetDataUseServiceName(
460 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE)); 496 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE));
461 EXPECT_CALL(*fetcher_for_large_icon_service_, 497 EXPECT_CALL(*fetcher_for_large_icon_service_,
462 StartOrQueueNetworkRequest(_, _, _, _)) 498 StartOrQueueNetworkRequest(_, _, _, _))
463 .WillOnce(PassFetch(128, 128)); 499 .WillOnce(PassFetch(128, 128));
464 EXPECT_CALL(done, Run()).WillOnce(Quit(&loop)); 500 EXPECT_CALL(done, Run()).WillOnce(Quit(&loop));
465 } 501 }
466 502
467 favicon::LargeIconService large_icon_service( 503 favicon::LargeIconService large_icon_service(
468 &favicon_service_, large_icon_service_background_task_runner_, 504 &favicon_service_, large_icon_service_background_task_runner_,
469 std::move(fetcher_for_large_icon_service_)); 505 std::move(fetcher_for_large_icon_service_));
470 IconCacherImpl cacher(&favicon_service_, &large_icon_service, 506 IconCacherImpl cacher(&favicon_service_, &large_icon_service,
471 std::move(fetcher_for_icon_cacher_)); 507 std::move(fetcher_for_icon_cacher_));
472 508
473 cacher.StartFetchMostLikely(page_url, done.Get()); 509 cacher.StartFetchMostLikely(page_url, done.Get());
474 // Both these task runners need to be flushed in order to get |done| called by 510 // Both these task runners need to be flushed in order to get |done| called by
475 // running the main loop. 511 // running the main loop.
476 WaitForHistoryThreadTasksToFinish(); 512 WaitForHistoryThreadTasksToFinish();
477 large_icon_service_background_task_runner_->RunUntilIdle(); 513 large_icon_service_background_task_runner_->RunUntilIdle();
478 514
479 loop.Run(); 515 loop.Run();
480 EXPECT_FALSE(IconIsCachedFor(page_url, favicon_base::FAVICON)); 516 EXPECT_FALSE(IconIsCachedFor(page_url, favicon_base::FAVICON));
481 EXPECT_TRUE(IconIsCachedFor(page_url, favicon_base::TOUCH_ICON)); 517 EXPECT_TRUE(IconIsCachedFor(page_url, favicon_base::TOUCH_ICON));
518 EXPECT_THAT(
519 histogram_tester.GetAllSamples(
520 "NewTabPage.TileFaviconFetchSuccess.Server"),
521 ElementsAre(Bucket(/*bucket=*/true, /*count=*/1)));
482 } 522 }
483 523
484 TEST_F(IconCacherTestMostLikely, NotCachedAndFetchFailed) { 524 TEST_F(IconCacherTestMostLikely, NotCachedAndFetchFailed) {
485 GURL page_url("http://www.site.com"); 525 GURL page_url("http://www.site.com");
526 base::HistogramTester histogram_tester;
486 527
487 base::MockCallback<base::Closure> done; 528 base::MockCallback<base::Closure> done;
488 { 529 {
489 InSequence s; 530 InSequence s;
490 EXPECT_CALL(*fetcher_for_large_icon_service_, 531 EXPECT_CALL(*fetcher_for_large_icon_service_,
491 SetDataUseServiceName( 532 SetDataUseServiceName(
492 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE)); 533 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE));
493 EXPECT_CALL(*fetcher_for_large_icon_service_, 534 EXPECT_CALL(*fetcher_for_large_icon_service_,
494 StartOrQueueNetworkRequest(_, _, _, _)) 535 StartOrQueueNetworkRequest(_, _, _, _))
495 .WillOnce(FailFetch()); 536 .WillOnce(FailFetch());
496 EXPECT_CALL(done, Run()).Times(0); 537 EXPECT_CALL(done, Run()).Times(0);
497 } 538 }
498 539
499 favicon::LargeIconService large_icon_service( 540 favicon::LargeIconService large_icon_service(
500 &favicon_service_, large_icon_service_background_task_runner_, 541 &favicon_service_, large_icon_service_background_task_runner_,
501 std::move(fetcher_for_large_icon_service_)); 542 std::move(fetcher_for_large_icon_service_));
502 IconCacherImpl cacher(&favicon_service_, &large_icon_service, 543 IconCacherImpl cacher(&favicon_service_, &large_icon_service,
503 std::move(fetcher_for_icon_cacher_)); 544 std::move(fetcher_for_icon_cacher_));
504 545
505 cacher.StartFetchMostLikely(page_url, done.Get()); 546 cacher.StartFetchMostLikely(page_url, done.Get());
506 // Both these task runners need to be flushed before flushing the main thread 547 // Both these task runners need to be flushed before flushing the main thread
507 // queue in order to finish the work. 548 // queue in order to finish the work.
508 WaitForHistoryThreadTasksToFinish(); 549 WaitForHistoryThreadTasksToFinish();
509 large_icon_service_background_task_runner_->RunUntilIdle(); 550 large_icon_service_background_task_runner_->RunUntilIdle();
510 WaitForMainThreadTasksToFinish(); 551 WaitForMainThreadTasksToFinish();
511 552
512 EXPECT_FALSE(IconIsCachedFor(page_url, favicon_base::FAVICON)); 553 EXPECT_FALSE(IconIsCachedFor(page_url, favicon_base::FAVICON));
513 EXPECT_FALSE(IconIsCachedFor(page_url, favicon_base::TOUCH_ICON)); 554 EXPECT_FALSE(IconIsCachedFor(page_url, favicon_base::TOUCH_ICON));
555 EXPECT_THAT(
556 histogram_tester.GetAllSamples(
557 "NewTabPage.TileFaviconFetchSuccess.Server"),
558 ElementsAre(Bucket(/*bucket=*/false, /*count=*/1)));
514 } 559 }
515 560
516 TEST_F(IconCacherTestMostLikely, HandlesEmptyCallbacksNicely) { 561 TEST_F(IconCacherTestMostLikely, HandlesEmptyCallbacksNicely) {
517 GURL page_url("http://www.site.com"); 562 GURL page_url("http://www.site.com");
518 563
519 EXPECT_CALL(*fetcher_for_large_icon_service_, SetDataUseServiceName(_)); 564 EXPECT_CALL(*fetcher_for_large_icon_service_, SetDataUseServiceName(_));
520 EXPECT_CALL(*fetcher_for_large_icon_service_, 565 EXPECT_CALL(*fetcher_for_large_icon_service_,
521 StartOrQueueNetworkRequest(_, _, _, _)) 566 StartOrQueueNetworkRequest(_, _, _, _))
522 .WillOnce(PassFetch(128, 128)); 567 .WillOnce(PassFetch(128, 128));
523 568
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 WaitForHistoryThreadTasksToFinish(); 615 WaitForHistoryThreadTasksToFinish();
571 large_icon_service_background_task_runner_->RunUntilIdle(); 616 large_icon_service_background_task_runner_->RunUntilIdle();
572 617
573 loop.Run(); 618 loop.Run();
574 EXPECT_FALSE(IconIsCachedFor(page_url, favicon_base::FAVICON)); 619 EXPECT_FALSE(IconIsCachedFor(page_url, favicon_base::FAVICON));
575 EXPECT_TRUE(IconIsCachedFor(page_url, favicon_base::TOUCH_ICON)); 620 EXPECT_TRUE(IconIsCachedFor(page_url, favicon_base::TOUCH_ICON));
576 } 621 }
577 622
578 } // namespace 623 } // namespace
579 } // namespace ntp_tiles 624 } // namespace ntp_tiles
OLDNEW
« no previous file with comments | « components/ntp_tiles/icon_cacher_impl.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698