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

Side by Side Diff: components/favicon/core/favicon_handler_unittest.cc

Issue 2808063002: Add DownloadStatus metric to FaviconHandler (Closed)
Patch Set: Optimize UMA macro usage 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/favicon_handler.h" 5 #include "components/favicon/core/favicon_handler.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <map> 9 #include <map>
10 #include <memory> 10 #include <memory>
11 #include <set> 11 #include <set>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
17 #include "base/run_loop.h" 17 #include "base/run_loop.h"
18 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
19 #include "base/test/histogram_tester.h"
19 #include "components/favicon/core/favicon_driver.h" 20 #include "components/favicon/core/favicon_driver.h"
20 #include "components/favicon/core/test/mock_favicon_service.h" 21 #include "components/favicon/core/test/mock_favicon_service.h"
21 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/skia/include/core/SkBitmap.h" 24 #include "third_party/skia/include/core/SkBitmap.h"
24 #include "ui/base/layout.h" 25 #include "ui/base/layout.h"
25 #include "ui/gfx/codec/png_codec.h" 26 #include "ui/gfx/codec/png_codec.h"
26 #include "ui/gfx/favicon_size.h" 27 #include "ui/gfx/favicon_size.h"
27 #include "ui/gfx/image/image.h" 28 #include "ui/gfx/image/image.h"
28 29
29 namespace favicon { 30 namespace favicon {
30 namespace { 31 namespace {
31 32
32 using favicon_base::FAVICON; 33 using favicon_base::FAVICON;
33 using favicon_base::FaviconRawBitmapResult; 34 using favicon_base::FaviconRawBitmapResult;
34 using favicon_base::TOUCH_ICON; 35 using favicon_base::TOUCH_ICON;
35 using favicon_base::TOUCH_PRECOMPOSED_ICON; 36 using favicon_base::TOUCH_PRECOMPOSED_ICON;
36 using testing::Assign; 37 using testing::Assign;
37 using testing::ElementsAre; 38 using testing::ElementsAre;
38 using testing::InSequence; 39 using testing::InSequence;
39 using testing::Invoke; 40 using testing::Invoke;
40 using testing::IsEmpty; 41 using testing::IsEmpty;
41 using testing::Return; 42 using testing::Return;
42 using testing::_; 43 using testing::_;
43 44
45 using DownloadOutcome = FaviconHandler::DownloadOutcome;
44 using IntVector = std::vector<int>; 46 using IntVector = std::vector<int>;
45 using URLVector = std::vector<GURL>; 47 using URLVector = std::vector<GURL>;
46 using BitmapVector = std::vector<SkBitmap>; 48 using BitmapVector = std::vector<SkBitmap>;
47 using SizeVector = std::vector<gfx::Size>; 49 using SizeVector = std::vector<gfx::Size>;
48 50
49 MATCHER_P2(ImageSizeIs, width, height, "") { 51 MATCHER_P2(ImageSizeIs, width, height, "") {
50 *result_listener << "where size is " << arg.Width() << "x" << arg.Height(); 52 *result_listener << "where size is " << arg.Width() << "x" << arg.Height();
51 return arg.Size() == gfx::Size(width, height); 53 return arg.Size() == gfx::Size(width, height);
52 } 54 }
53 55
(...skipping 17 matching lines...) Expand all
71 std::vector<unsigned char> FillBitmapWithEdgeSize(int size) { 73 std::vector<unsigned char> FillBitmapWithEdgeSize(int size) {
72 SkBitmap bitmap = CreateBitmapWithEdgeSize(size); 74 SkBitmap bitmap = CreateBitmapWithEdgeSize(size);
73 std::vector<unsigned char> output; 75 std::vector<unsigned char> output;
74 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &output); 76 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &output);
75 return output; 77 return output;
76 } 78 }
77 79
78 std::vector<FaviconRawBitmapResult> CreateRawBitmapResult( 80 std::vector<FaviconRawBitmapResult> CreateRawBitmapResult(
79 const GURL& icon_url, 81 const GURL& icon_url,
80 favicon_base::IconType icon_type = FAVICON, 82 favicon_base::IconType icon_type = FAVICON,
81 bool expired = false) { 83 bool expired = false,
84 int edge_size = gfx::kFaviconSize) {
82 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); 85 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes());
83 data->data() = FillBitmapWithEdgeSize(gfx::kFaviconSize); 86 data->data() = FillBitmapWithEdgeSize(edge_size);
84 FaviconRawBitmapResult bitmap_result; 87 FaviconRawBitmapResult bitmap_result;
85 bitmap_result.expired = expired; 88 bitmap_result.expired = expired;
86 bitmap_result.bitmap_data = data; 89 bitmap_result.bitmap_data = data;
87 // Use a pixel size other than (0,0) as (0,0) has a special meaning. 90 // Use a pixel size other than (0,0) as (0,0) has a special meaning.
88 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize); 91 bitmap_result.pixel_size = gfx::Size(edge_size, edge_size);
89 bitmap_result.icon_type = icon_type; 92 bitmap_result.icon_type = icon_type;
90 bitmap_result.icon_url = icon_url; 93 bitmap_result.icon_url = icon_url;
91 return {bitmap_result}; 94 return {bitmap_result};
92 } 95 }
93 96
94 // Fake that implements the calls to FaviconHalder::Delegate's DownloadImage(), 97 // Fake that implements the calls to FaviconHandler::Delegate's DownloadImage(),
95 // delegated to this class through MockDelegate. 98 // delegated to this class through MockDelegate.
96 class FakeImageDownloader { 99 class FakeImageDownloader {
97 public: 100 public:
98 struct Response { 101 struct Response {
99 int http_status_code = 404; 102 int http_status_code = 404;
100 BitmapVector bitmaps; 103 BitmapVector bitmaps;
101 SizeVector original_bitmap_sizes; 104 SizeVector original_bitmap_sizes;
102 }; 105 };
103 106
104 FakeImageDownloader() : next_download_id_(1) {} 107 FakeImageDownloader() : next_download_id_(1) {}
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 } 378 }
376 379
377 base::MessageLoopForUI message_loop_; 380 base::MessageLoopForUI message_loop_;
378 std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors> 381 std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors>
379 scoped_set_supported_scale_factors_; 382 scoped_set_supported_scale_factors_;
380 testing::NiceMock<MockFaviconServiceWithFake> favicon_service_; 383 testing::NiceMock<MockFaviconServiceWithFake> favicon_service_;
381 testing::NiceMock<MockDelegate> delegate_; 384 testing::NiceMock<MockDelegate> delegate_;
382 }; 385 };
383 386
384 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) { 387 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) {
388 base::HistogramTester histogram_tester;
385 const GURL kIconURL("http://www.google.com/favicon"); 389 const GURL kIconURL("http://www.google.com/favicon");
386 390
387 favicon_service_.fake()->Store(kPageURL, kIconURL, 391 favicon_service_.fake()->Store(kPageURL, kIconURL,
388 CreateRawBitmapResult(kIconURL)); 392 CreateRawBitmapResult(kIconURL));
389 393
390 EXPECT_CALL(delegate_, OnFaviconUpdated( 394 EXPECT_CALL(delegate_, OnFaviconUpdated(
391 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP, 395 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP,
392 kIconURL, /*icon_url_changed=*/true, _)); 396 kIconURL, /*icon_url_changed=*/true, _));
393 397
394 RunHandlerWithSimpleFaviconCandidates({kIconURL}); 398 RunHandlerWithSimpleFaviconCandidates({kIconURL});
395 EXPECT_THAT(delegate_.downloads(), IsEmpty()); 399 EXPECT_THAT(delegate_.downloads(), IsEmpty());
400 EXPECT_THAT(
401 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.LargeIcons"),
402 IsEmpty());
403 EXPECT_THAT(
404 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.Favicons"),
405 IsEmpty());
396 } 406 }
397 407
398 // Test that UpdateFaviconsAndFetch() is called with the appropriate parameters 408 // Test that UpdateFaviconsAndFetch() is called with the appropriate parameters
399 // when there is data in the database for neither the page URL nor the icon URL. 409 // when there is data in the database for neither the page URL nor the icon URL.
400 TEST_F(FaviconHandlerTest, UpdateFaviconMappingsAndFetch) { 410 TEST_F(FaviconHandlerTest, UpdateFaviconMappingsAndFetch) {
401 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch( 411 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
402 kPageURL, URLVector{kIconURL16x16}, FAVICON, 412 kPageURL, URLVector{kIconURL16x16}, FAVICON,
403 /*desired_size_in_dip=*/16, _, _)); 413 /*desired_size_in_dip=*/16, _, _));
404 414
405 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16}); 415 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16});
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 538
529 RunHandlerWithSimpleFaviconCandidates({kNewIconURL}); 539 RunHandlerWithSimpleFaviconCandidates({kNewIconURL});
530 EXPECT_THAT(delegate_.downloads(), ElementsAre(kNewIconURL)); 540 EXPECT_THAT(delegate_.downloads(), ElementsAre(kNewIconURL));
531 } 541 }
532 542
533 // If there is data for the page URL in history which is invalid, test that: 543 // If there is data for the page URL in history which is invalid, test that:
534 // - The invalid data is not sent to the UI. 544 // - The invalid data is not sent to the UI.
535 // - The icon is redownloaded. 545 // - The icon is redownloaded.
536 TEST_F(FaviconHandlerTest, FaviconInHistoryInvalid) { 546 TEST_F(FaviconHandlerTest, FaviconInHistoryInvalid) {
537 // Set non empty but invalid data. 547 // Set non empty but invalid data.
538 FaviconRawBitmapResult bitmap_result; 548 std::vector<FaviconRawBitmapResult> bitmap_result =
539 bitmap_result.expired = false; 549 CreateRawBitmapResult(kIconURL16x16);
540 // Empty bitmap data is invalid. 550 // Empty bitmap data is invalid.
541 bitmap_result.bitmap_data = new base::RefCountedBytes(); 551 bitmap_result[0].bitmap_data = new base::RefCountedBytes();
542 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize);
543 bitmap_result.icon_type = FAVICON;
544 bitmap_result.icon_url = kIconURL16x16;
545 552
546 favicon_service_.fake()->Store(kPageURL, kIconURL16x16, {bitmap_result}); 553 favicon_service_.fake()->Store(kPageURL, kIconURL16x16, bitmap_result);
547 554
548 // TODO(crbug.com/700811): It would be nice if we could check the image 555 // TODO(crbug.com/700811): It would be nice if we could check the image
549 // being published to rule out invalid data. 556 // being published to rule out invalid data.
550 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _)); 557 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _));
551 558
552 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16}); 559 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16});
553 560
554 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kPageURL)); 561 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kPageURL));
555 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16)); 562 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
556 } 563 }
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after
1024 EXPECT_CALL(delegate_, 1031 EXPECT_CALL(delegate_,
1025 OnFaviconUpdated(_, _, kIconURL12x12, _, ImageSizeIs(12, 12))); 1032 OnFaviconUpdated(_, _, kIconURL12x12, _, ImageSizeIs(12, 12)));
1026 1033
1027 RunHandlerWithCandidates( 1034 RunHandlerWithCandidates(
1028 FaviconDriverObserver::NON_TOUCH_LARGEST, 1035 FaviconDriverObserver::NON_TOUCH_LARGEST,
1029 {FaviconURL(kIconURL10x10, FAVICON, SizeVector{gfx::Size(16, 16)}), 1036 {FaviconURL(kIconURL10x10, FAVICON, SizeVector{gfx::Size(16, 16)}),
1030 FaviconURL(kIconURL12x12, FAVICON, SizeVector{gfx::Size(15, 15)}), 1037 FaviconURL(kIconURL12x12, FAVICON, SizeVector{gfx::Size(15, 15)}),
1031 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}); 1038 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)});
1032 } 1039 }
1033 1040
1041 TEST_F(FaviconHandlerTest, TestRecordMultipleDownloadAttempts) {
1042 base::HistogramTester histogram_tester;
1043
1044 // Try to download the three failing icons and end up logging three attempts.
1045 RunHandlerWithCandidates(
1046 FaviconDriverObserver::NON_TOUCH_LARGEST,
1047 {FaviconURL(GURL("http://www.google.com/a"), FAVICON, kEmptySizes),
1048 FaviconURL(GURL("http://www.google.com/b"), FAVICON, kEmptySizes),
1049 FaviconURL(GURL("http://www.google.com/c"), FAVICON, kEmptySizes)});
1050
1051 EXPECT_THAT(
1052 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.LargeIcons"),
1053 ElementsAre(base::Bucket(/*sample=*/3, /*expected_count=*/1)));
1054 EXPECT_THAT(
1055 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.Favicons"),
1056 IsEmpty());
1057 EXPECT_THAT(
1058 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.TouchIcons"),
1059 IsEmpty());
1060 }
1061
1062 TEST_F(FaviconHandlerTest, TestRecordSingleFaviconDownloadAttempt) {
1063 base::HistogramTester histogram_tester;
1064
1065 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16});
1066
1067 EXPECT_THAT(
1068 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.Favicons"),
1069 ElementsAre(base::Bucket(/*sample=*/1, /*expected_count=*/1)));
1070 EXPECT_THAT(
1071 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.LargeIcons"),
1072 IsEmpty());
1073 EXPECT_THAT(
1074 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.TouchIcons"),
1075 IsEmpty());
1076 EXPECT_THAT(
1077 histogram_tester.GetAllSamples("Favicons.DownloadOutcome"),
1078 ElementsAre(base::Bucket(static_cast<int>(DownloadOutcome::SUCCEEDED),
1079 /*expected_count=*/1)));
1080 }
1081
1082 TEST_F(FaviconHandlerTest, TestRecordSingleLargeIconDownloadAttempt) {
1083 base::HistogramTester histogram_tester;
1084
1085 RunHandlerWithCandidates(FaviconDriverObserver::NON_TOUCH_LARGEST,
1086 {FaviconURL(kIconURL64x64, FAVICON, kEmptySizes)});
1087
1088 EXPECT_THAT(
1089 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.Favicons"),
1090 IsEmpty());
1091 EXPECT_THAT(
1092 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.LargeIcons"),
1093 ElementsAre(base::Bucket(/*sample=*/1, /*expected_count=*/1)));
1094 EXPECT_THAT(
1095 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.TouchIcons"),
1096 IsEmpty());
1097 EXPECT_THAT(
1098 histogram_tester.GetAllSamples("Favicons.DownloadOutcome"),
1099 ElementsAre(base::Bucket(static_cast<int>(DownloadOutcome::SUCCEEDED),
1100 /*expected_count=*/1)));
1101 }
1102
1103 TEST_F(FaviconHandlerTest, TestRecordSingleTouchIconDownloadAttempt) {
1104 base::HistogramTester histogram_tester;
1105 RunHandlerWithCandidates(
1106 FaviconDriverObserver::TOUCH_LARGEST,
1107 {FaviconURL(kIconURL64x64, TOUCH_ICON, kEmptySizes)});
1108
1109 EXPECT_THAT(
1110 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.LargeIcons"),
1111 IsEmpty());
1112 EXPECT_THAT(
1113 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.Favicons"),
1114 IsEmpty());
1115 EXPECT_THAT(
1116 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.TouchIcons"),
1117 ElementsAre(base::Bucket(/*sample=*/1, /*expected_count=*/1)));
1118 EXPECT_THAT(
1119 histogram_tester.GetAllSamples("Favicons.DownloadOutcome"),
1120 ElementsAre(base::Bucket(static_cast<int>(DownloadOutcome::SUCCEEDED),
1121 /*expected_count=*/1)));
1122 }
1123
1124 TEST_F(FaviconHandlerTest, TestRecordDownloadAttemptsFinishedByCache) {
1125 const GURL kIconURL1024x1024("http://www.google.com/a-404-ing-icon");
1126 base::HistogramTester histogram_tester;
1127 favicon_service_.fake()->Store(
1128 GURL("http://so.de"), kIconURL64x64,
1129 CreateRawBitmapResult(kIconURL64x64, FAVICON, /*expired=*/false, 64));
1130
1131 RunHandlerWithCandidates(
1132 FaviconDriverObserver::NON_TOUCH_LARGEST,
1133 {FaviconURL(kIconURL1024x1024, FAVICON, {gfx::Size(1024, 1024)}),
1134 FaviconURL(kIconURL12x12, FAVICON, {gfx::Size(12, 12)}),
1135 FaviconURL(kIconURL64x64, FAVICON, {gfx::Size(64, 64)})});
1136
1137 // Should try only the first (receive 404) and get second icon from cache.
1138 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL1024x1024));
1139
1140 EXPECT_THAT(
1141 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.LargeIcons"),
1142 ElementsAre(base::Bucket(/*sample=*/1, /*expected_count=*/1)));
1143 EXPECT_THAT(
1144 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.Favicons"),
1145 IsEmpty());
1146 EXPECT_THAT(
1147 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.TouchIcons"),
1148 IsEmpty());
1149 }
1150
1151 TEST_F(FaviconHandlerTest, TestRecordSingleDownloadAttemptForRefreshingIcons) {
1152 base::HistogramTester histogram_tester;
1153 favicon_service_.fake()->Store(
1154 kPageURL, kIconURL16x16,
1155 CreateRawBitmapResult(kIconURL16x16, FAVICON, /*expired=*/true));
1156
1157 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16});
1158
1159 EXPECT_THAT(
1160 histogram_tester.GetAllSamples("Favicons.DownloadAttempts.Favicons"),
1161 ElementsAre(base::Bucket(/*sample=*/1, /*expected_count=*/1)));
1162 }
1163
1164 TEST_F(FaviconHandlerTest, TestRecordFailingDownloadAttempt) {
1165 base::HistogramTester histogram_tester;
1166 const GURL k404IconURL("http://www.google.com/404.png");
1167
1168 delegate_.fake_downloader().AddError(k404IconURL, 404);
1169
1170 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(k404IconURL));
1171
1172 RunHandlerWithSimpleFaviconCandidates({k404IconURL});
1173
1174 EXPECT_THAT(
1175 histogram_tester.GetAllSamples("Favicons.DownloadOutcome"),
1176 ElementsAre(base::Bucket(static_cast<int>(DownloadOutcome::FAILED),
1177 /*expected_count=*/1)));
1178 }
1179
1180 TEST_F(FaviconHandlerTest, TestRecordSkippedDownloadForKnownFailingUrl) {
1181 base::HistogramTester histogram_tester;
1182 const GURL k404IconURL("http://www.google.com/404.png");
1183
1184 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(k404IconURL))
1185 .WillByDefault(Return(true));
1186
1187 RunHandlerWithSimpleFaviconCandidates({k404IconURL});
1188
1189 EXPECT_THAT(
1190 histogram_tester.GetAllSamples("Favicons.DownloadOutcome"),
1191 ElementsAre(base::Bucket(static_cast<int>(DownloadOutcome::SKIPPED),
1192 /*expected_count=*/1)));
1193 }
1194
1034 } // namespace 1195 } // namespace
1035 } // namespace favicon 1196 } // namespace favicon
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698