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

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: 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/enums.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 "testing/gmock/include/gmock/gmock.h" 28 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
29 #include "ui/base/resource/resource_bundle.h" 30 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/base/ui_base_paths.h" 31 #include "ui/base/ui_base_paths.h"
31 #include "ui/gfx/image/image_unittest_util.h" 32 #include "ui/gfx/image/image_unittest_util.h"
32 33
34 using base::Bucket;
33 using ::testing::_; 35 using ::testing::_;
36 using ::testing::ElementsAre;
34 using ::testing::Eq; 37 using ::testing::Eq;
35 using ::testing::Invoke; 38 using ::testing::Invoke;
36 using ::testing::InSequence; 39 using ::testing::InSequence;
37 using ::testing::NiceMock; 40 using ::testing::NiceMock;
38 using ::testing::Return; 41 using ::testing::Return;
39 using ::testing::ReturnArg; 42 using ::testing::ReturnArg;
40 43
41 namespace ntp_tiles { 44 namespace ntp_tiles {
42 namespace { 45 namespace {
43 46
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 } 397 }
395 398
396 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_)); 399 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_));
397 cacher.StartFetchPopularSites(site_, done.Get(), done.Get()); 400 cacher.StartFetchPopularSites(site_, done.Get(), done.Get());
398 cacher.StartFetchPopularSites(site_, done.Get(), done.Get()); 401 cacher.StartFetchPopularSites(site_, done.Get(), done.Get());
399 loop.Run(); 402 loop.Run();
400 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 403 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
401 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 404 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
402 } 405 }
403 406
407 TEST_F(IconCacherTestPopularSites, LargeNotCachedAndFetchSuccessReported) {
408 base::HistogramTester histogram_tester;
409 base::MockCallback<base::Closure> done;
410 base::RunLoop loop;
411 {
412 InSequence s;
413 EXPECT_CALL(*image_fetcher_,
414 SetDataUseServiceName(
415 data_use_measurement::DataUseUserData::NTP_TILES));
416 EXPECT_CALL(*image_fetcher_, SetDesiredImageFrameSize(gfx::Size(128, 128)));
417 EXPECT_CALL(*image_fetcher_,
418 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
419 .WillOnce(PassFetch(128, 128));
420 EXPECT_CALL(done, Run()).WillOnce(Quit(&loop));
421 }
422
423 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_));
424 cacher.StartFetchPopularSites(site_, done.Get(), done.Get());
425 loop.Run();
426 EXPECT_THAT(
427 histogram_tester.GetAllSamples("NewTabPage.TileFaviconFetched.popular"),
428 ElementsAre(Bucket(/*bucket=*/0, /*count=*/1))); // bucket 0 = SUCCESS.
429 }
430
431 TEST_F(IconCacherTestPopularSites, LargeNotCachedAndFetchFailureReported) {
432 base::HistogramTester histogram_tester;
433 base::MockCallback<base::Closure> done;
434 EXPECT_CALL(done, Run()).Times(0);
435 {
436 InSequence s;
437 EXPECT_CALL(*image_fetcher_,
438 SetDataUseServiceName(
439 data_use_measurement::DataUseUserData::NTP_TILES));
440 EXPECT_CALL(*image_fetcher_, SetDesiredImageFrameSize(gfx::Size(128, 128)));
441 EXPECT_CALL(*image_fetcher_,
442 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
443 .WillOnce(FailFetch());
444 }
445
446 IconCacherImpl cacher(&favicon_service_, nullptr, std::move(image_fetcher_));
447 cacher.StartFetchPopularSites(site_, done.Get(), done.Get());
448 WaitForHistoryThreadTasksToFinish();
449 WaitForMainThreadTasksToFinish();
450 EXPECT_THAT(
451 histogram_tester.GetAllSamples("NewTabPage.TileFaviconFetched.popular"),
452 ElementsAre(Bucket(/*bucket=*/1, /*count=*/1))); // bucket 1 = FAILURE.
453 }
454
sfiera 2017/05/18 13:40:32 Could you also please test that cached and baked-i
jkrcal 2017/05/18 14:09:54 Done. I've merged histograms expects back into th
404 class IconCacherTestMostLikely : public IconCacherTestBase { 455 class IconCacherTestMostLikely : public IconCacherTestBase {
405 protected: 456 protected:
406 IconCacherTestMostLikely() 457 IconCacherTestMostLikely()
407 : large_icon_service_background_task_runner_( 458 : large_icon_service_background_task_runner_(
408 new base::TestSimpleTaskRunner()), 459 new base::TestSimpleTaskRunner()),
409 fetcher_for_large_icon_service_( 460 fetcher_for_large_icon_service_(
410 base::MakeUnique<::testing::StrictMock<MockImageFetcher>>()), 461 base::MakeUnique<::testing::StrictMock<MockImageFetcher>>()),
411 fetcher_for_icon_cacher_( 462 fetcher_for_icon_cacher_(
412 base::MakeUnique<::testing::StrictMock<MockImageFetcher>>()) { 463 base::MakeUnique<::testing::StrictMock<MockImageFetcher>>()) {
413 // Expect uninteresting calls here, |fetcher_for_icon_cacher_| is not 464 // Expect uninteresting calls here, |fetcher_for_icon_cacher_| is not
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 // Both these task runners need to be flushed in order to get |done| called by 617 // Both these task runners need to be flushed in order to get |done| called by
567 // running the main loop. 618 // running the main loop.
568 WaitForHistoryThreadTasksToFinish(); 619 WaitForHistoryThreadTasksToFinish();
569 large_icon_service_background_task_runner_->RunUntilIdle(); 620 large_icon_service_background_task_runner_->RunUntilIdle();
570 621
571 loop.Run(); 622 loop.Run();
572 EXPECT_FALSE(IconIsCachedFor(page_url, favicon_base::FAVICON)); 623 EXPECT_FALSE(IconIsCachedFor(page_url, favicon_base::FAVICON));
573 EXPECT_TRUE(IconIsCachedFor(page_url, favicon_base::TOUCH_ICON)); 624 EXPECT_TRUE(IconIsCachedFor(page_url, favicon_base::TOUCH_ICON));
574 } 625 }
575 626
627 TEST_F(IconCacherTestMostLikely, NotCachedAndFetchSuccessReported) {
628 base::HistogramTester histogram_tester;
629 GURL page_url("http://www.site.com");
630
631 base::MockCallback<base::Closure> done;
632 base::RunLoop loop;
633 {
634 InSequence s;
635 EXPECT_CALL(*fetcher_for_large_icon_service_,
636 SetDataUseServiceName(
637 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE));
638 EXPECT_CALL(*fetcher_for_large_icon_service_,
639 StartOrQueueNetworkRequest(_, _, _))
640 .WillOnce(PassFetch(128, 128));
641 EXPECT_CALL(done, Run()).WillOnce(Quit(&loop));
642 }
643
644 favicon::LargeIconService large_icon_service(
645 &favicon_service_, large_icon_service_background_task_runner_,
646 std::move(fetcher_for_large_icon_service_));
647 IconCacherImpl cacher(&favicon_service_, &large_icon_service,
648 std::move(fetcher_for_icon_cacher_));
649
650 cacher.StartFetchMostLikely(page_url, done.Get());
651 // Both these task runners need to be flushed in order to get |done| called by
652 // running the main loop.
653 WaitForHistoryThreadTasksToFinish();
654 large_icon_service_background_task_runner_->RunUntilIdle();
655
656 loop.Run();
657 EXPECT_THAT(
658 histogram_tester.GetAllSamples("NewTabPage.TileFaviconFetched.server"),
659 ElementsAre(Bucket(/*bucket=*/0, /*count=*/1))); // bucket 0 = SUCCESS.
660 }
661
662 TEST_F(IconCacherTestMostLikely, NotCachedAndFetchFailureReported) {
663 base::HistogramTester histogram_tester;
664 GURL page_url("http://www.site.com");
665
666 base::MockCallback<base::Closure> done;
667 {
668 InSequence s;
669 EXPECT_CALL(*fetcher_for_large_icon_service_,
670 SetDataUseServiceName(
671 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE));
672 EXPECT_CALL(*fetcher_for_large_icon_service_,
673 StartOrQueueNetworkRequest(_, _, _))
674 .WillOnce(FailFetch());
675 EXPECT_CALL(done, Run()).Times(0);
676 }
677
678 favicon::LargeIconService large_icon_service(
679 &favicon_service_, large_icon_service_background_task_runner_,
680 std::move(fetcher_for_large_icon_service_));
681 IconCacherImpl cacher(&favicon_service_, &large_icon_service,
682 std::move(fetcher_for_icon_cacher_));
683
684 cacher.StartFetchMostLikely(page_url, done.Get());
685 // Both these task runners need to be flushed before flushing the main thread
686 // queue in order to finish the work.
687 WaitForHistoryThreadTasksToFinish();
688 large_icon_service_background_task_runner_->RunUntilIdle();
689 WaitForMainThreadTasksToFinish();
690
691 EXPECT_THAT(
692 histogram_tester.GetAllSamples("NewTabPage.TileFaviconFetched.server"),
693 ElementsAre(Bucket(/*bucket=*/1, /*count=*/1))); // bucket 1 = FAILURE.
694 }
695
576 } // namespace 696 } // namespace
577 } // namespace ntp_tiles 697 } // namespace ntp_tiles
OLDNEW
« no previous file with comments | « components/ntp_tiles/icon_cacher_impl.cc ('k') | tools/metrics/histograms/enums.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698