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

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

Issue 2739173002: Always select best favicon bitmap (Closed)
Patch Set: Rebased. Created 3 years, 9 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 <memory> 9 #include <memory>
10 #include <set> 10 #include <set>
(...skipping 768 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 // - On a page with <link rel="icon"> tags with no "sizes" information. 779 // - On a page with <link rel="icon"> tags with no "sizes" information.
780 // Selects the largest exact match. Note that a 32x32 PNG image is not a "true 780 // Selects the largest exact match. Note that a 32x32 PNG image is not a "true
781 // exact match" on an OS which supports an 1x and 2x. A "true exact match" is 781 // exact match" on an OS which supports an 1x and 2x. A "true exact match" is
782 // a .ico file with 16x16 and 32x32 bitmaps. 782 // a .ico file with 16x16 and 32x32 bitmaps.
783 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseLargestExactMatch) { 783 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseLargestExactMatch) {
784 EXPECT_EQ(32, 784 EXPECT_EQ(32,
785 DownloadTillDoneIgnoringHistory(IntVector{16, 24, 32, 48, 256})); 785 DownloadTillDoneIgnoringHistory(IntVector{16, 24, 32, 48, 256}));
786 } 786 }
787 787
788 // Test that if there are several single resolution favicons to choose 788 // Test that if there are several single resolution favicons to choose
789 // from, the exact match is preferred even if it results in upsampling. 789 // from, the exact match is not preferred if it results in upsampling.
790 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseExactMatchDespiteUpsampling) { 790 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseBestDespiteSmallerExactMatch) {
791 EXPECT_EQ(16, DownloadTillDoneIgnoringHistory(IntVector{16, 24, 48, 256})); 791 EXPECT_EQ(48, DownloadTillDoneIgnoringHistory(IntVector{16, 24, 48, 256}));
792 } 792 }
793 793
794 // Test that favicons which need to be upsampled a little or downsampled 794 // Test that favicons which need to be upsampled a little or downsampled
795 // a little are preferred over huge favicons. 795 // a little are preferred over huge favicons.
796 TEST_F(FaviconHandlerMultipleFaviconsTest, 796 TEST_F(FaviconHandlerMultipleFaviconsTest,
797 ChooseMinorDownsamplingOverHugeIcon) { 797 ChooseMinorDownsamplingOverHugeIcon) {
798 EXPECT_EQ(48, DownloadTillDoneIgnoringHistory(IntVector{256, 48})); 798 EXPECT_EQ(48, DownloadTillDoneIgnoringHistory(IntVector{256, 48}));
799 } 799 }
800 800
801 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseMinorUpsamplingOverHugeIcon) { 801 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseMinorUpsamplingOverHugeIcon) {
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 TEST_F(FaviconHandlerTest, FaviconInvalidURL) { 880 TEST_F(FaviconHandlerTest, FaviconInvalidURL) {
881 const GURL kInvalidFormatURL("invalid"); 881 const GURL kInvalidFormatURL("invalid");
882 ASSERT_TRUE(kInvalidFormatURL.is_empty()); 882 ASSERT_TRUE(kInvalidFormatURL.is_empty());
883 883
884 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0); 884 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0);
885 885
886 RunHandlerWithSimpleFaviconCandidates({kInvalidFormatURL}); 886 RunHandlerWithSimpleFaviconCandidates({kInvalidFormatURL});
887 EXPECT_THAT(delegate_.downloads(), IsEmpty()); 887 EXPECT_THAT(delegate_.downloads(), IsEmpty());
888 } 888 }
889 889
890 TEST_F(FaviconHandlerTest, TestSortFavicon) {
891 const std::vector<favicon::FaviconURL> kSourceIconURLs{
892 FaviconURL(GURL("http://www.google.com/a"), FAVICON,
893 {gfx::Size(1, 1), gfx::Size(17, 17)}),
894 FaviconURL(GURL("http://www.google.com/b"), FAVICON,
895 {gfx::Size(1024, 1024), gfx::Size(512, 512)}),
896 FaviconURL(GURL("http://www.google.com/c"), FAVICON,
897 {gfx::Size(16, 16), gfx::Size(14, 14)}),
898 FaviconURL(GURL("http://www.google.com/d"), FAVICON, kEmptySizes),
899 FaviconURL(GURL("http://www.google.com/e"), FAVICON, kEmptySizes)};
900
901 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates(
902 FaviconDriverObserver::NON_TOUCH_LARGEST, kSourceIconURLs);
903
904 struct ExpectedResult {
905 // The favicon's index in kSourceIconURLs.
906 size_t favicon_index;
907 // Width of largest bitmap.
908 int width;
909 } results[] = {
910 // First is icon2, though its size larger than maximal.
911 {1, 1024},
912 // Second is icon1
913 // The 17x17 is largest.
914 {0, 17},
915 // Third is icon3.
916 // The 16x16 is largest.
917 {2, 16},
918 // The rest of bitmaps come in order, there is no "sizes" attribute.
919 {3, -1},
920 {4, -1},
921 };
922 const std::vector<FaviconURL>& icons = handler->image_urls();
923 ASSERT_EQ(5u, icons.size());
924 for (size_t i = 0; i < icons.size(); ++i) {
925 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url,
926 icons[i].icon_url);
927 if (results[i].width != -1)
928 EXPECT_EQ(results[i].width, icons[i].icon_sizes[0].width());
929 }
930 }
931
932 TEST_F(FaviconHandlerTest, TestDownloadLargestFavicon) { 890 TEST_F(FaviconHandlerTest, TestDownloadLargestFavicon) {
933 // Names represent the bitmap sizes per icon. 891 // Names represent the bitmap sizes per icon.
934 const GURL kIconURL1024_512("http://www.google.com/a"); 892 const GURL kIconURL1024_512("http://www.google.com/a");
935 const GURL kIconURL15_14("http://www.google.com/b"); 893 const GURL kIconURL15_14("http://www.google.com/b");
936 const GURL kIconURL16_512("http://www.google.com/c"); 894 const GURL kIconURL16_512("http://www.google.com/c");
937 const GURL kIconURLWithoutSize1("http://www.google.com/d"); 895 const GURL kIconURLWithoutSize1("http://www.google.com/d");
938 const GURL kIconURLWithoutSize2("http://www.google.com/e"); 896 const GURL kIconURLWithoutSize2("http://www.google.com/e");
939 897
940 RunHandlerWithCandidates( 898 RunHandlerWithCandidates(
941 FaviconDriverObserver::NON_TOUCH_LARGEST, 899 FaviconDriverObserver::NON_TOUCH_LARGEST,
(...skipping 30 matching lines...) Expand all
972 930
973 RunHandlerWithCandidates( 931 RunHandlerWithCandidates(
974 FaviconDriverObserver::NON_TOUCH_LARGEST, 932 FaviconDriverObserver::NON_TOUCH_LARGEST,
975 {FaviconURL(kIconURL1, FAVICON, {gfx::Size(15, 15)}), 933 {FaviconURL(kIconURL1, FAVICON, {gfx::Size(15, 15)}),
976 FaviconURL(kIconURL2, FAVICON, {gfx::Size(14, 14), gfx::Size(16, 16)})}); 934 FaviconURL(kIconURL2, FAVICON, {gfx::Size(14, 14), gfx::Size(16, 16)})});
977 935
978 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL2)); 936 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL2));
979 } 937 }
980 938
981 TEST_F(FaviconHandlerTest, TestFaviconWasScaledAfterDownload) { 939 TEST_F(FaviconHandlerTest, TestFaviconWasScaledAfterDownload) {
982 const int kMaximalSize = FaviconHandler::GetMaximalIconSize(FAVICON); 940 #if defined(OS_IOS)
941 const int kMaximalSize = 144;
942 #else
943 const int kMaximalSize = 192;
944 #endif
983 945
984 const GURL kIconURL1("http://www.google.com/b"); 946 const GURL kIconURL1("http://www.google.com/b");
985 const GURL kIconURL2("http://www.google.com/c"); 947 const GURL kIconURL2("http://www.google.com/c");
986 948
987 const int kOriginalSize1 = kMaximalSize + 1; 949 const int kOriginalSize1 = kMaximalSize + 1;
988 const int kOriginalSize2 = kMaximalSize + 2; 950 const int kOriginalSize2 = kMaximalSize + 2;
989 951
990 delegate_.fake_downloader().AddWithOriginalSizes( 952 delegate_.fake_downloader().AddWithOriginalSizes(
991 kIconURL1, IntVector{kMaximalSize}, IntVector{kOriginalSize1}); 953 kIconURL1, IntVector{kMaximalSize}, IntVector{kOriginalSize1});
992 delegate_.fake_downloader().AddWithOriginalSizes( 954 delegate_.fake_downloader().AddWithOriginalSizes(
993 kIconURL2, IntVector{kMaximalSize}, IntVector{kOriginalSize2}); 955 kIconURL2, IntVector{kMaximalSize}, IntVector{kOriginalSize2});
994 956
995 // Verify the largest bitmap was selected although it was scaled down to 957 // Verify the best bitmap was selected although it was scaled down to maximal
996 // maximal size and smaller than |kIconURL1| now. 958 // size and smaller than |kIconURL2| now.
997 EXPECT_CALL(delegate_, 959 EXPECT_CALL(delegate_,
998 OnFaviconUpdated(_, _, kIconURL2, _, 960 OnFaviconUpdated(_, _, kIconURL1, _,
999 ImageSizeIs(kMaximalSize, kMaximalSize))); 961 ImageSizeIs(kMaximalSize, kMaximalSize)));
1000 962
1001 RunHandlerWithCandidates( 963 RunHandlerWithCandidates(
1002 FaviconDriverObserver::NON_TOUCH_LARGEST, 964 FaviconDriverObserver::NON_TOUCH_LARGEST,
1003 {FaviconURL(kIconURL1, FAVICON, 965 {FaviconURL(kIconURL1, FAVICON,
1004 SizeVector{gfx::Size(kOriginalSize1, kOriginalSize1)}), 966 SizeVector{gfx::Size(kOriginalSize1, kOriginalSize1)}),
1005 FaviconURL(kIconURL2, FAVICON, 967 FaviconURL(kIconURL2, FAVICON,
1006 SizeVector{gfx::Size(kOriginalSize2, kOriginalSize2)})}); 968 SizeVector{gfx::Size(kOriginalSize2, kOriginalSize2)})});
1007 969
1008 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL2)); 970 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL1));
1009 } 971 }
1010 972
1011 // Test that if several icons are downloaded because the icons are smaller than 973 // Test that if several icons are downloaded because the icons are smaller than
1012 // expected that OnFaviconUpdated() is called with the largest downloaded 974 // expected that OnFaviconUpdated() is called with the largest downloaded
1013 // bitmap. 975 // bitmap.
1014 TEST_F(FaviconHandlerTest, TestKeepDownloadedLargestFavicon) { 976 TEST_F(FaviconHandlerTest, TestKeepDownloadedLargestFavicon) {
1015 EXPECT_CALL(delegate_, 977 EXPECT_CALL(delegate_,
1016 OnFaviconUpdated(_, _, kIconURL12x12, _, ImageSizeIs(12, 12))); 978 OnFaviconUpdated(_, _, kIconURL12x12, _, ImageSizeIs(12, 12)));
1017 979
1018 RunHandlerWithCandidates( 980 RunHandlerWithCandidates(
1019 FaviconDriverObserver::NON_TOUCH_LARGEST, 981 FaviconDriverObserver::NON_TOUCH_LARGEST,
1020 {FaviconURL(kIconURL10x10, FAVICON, SizeVector{gfx::Size(16, 16)}), 982 {FaviconURL(kIconURL10x10, FAVICON, SizeVector{gfx::Size(16, 16)}),
1021 FaviconURL(kIconURL12x12, FAVICON, SizeVector{gfx::Size(15, 15)}), 983 FaviconURL(kIconURL12x12, FAVICON, SizeVector{gfx::Size(15, 15)}),
1022 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}); 984 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)});
1023 } 985 }
1024 986
1025 } // namespace 987 } // namespace
1026 } // namespace favicon 988 } // namespace favicon
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698