OLD | NEW |
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/content/content_favicon_driver.h" | 5 #include "components/favicon/content/content_favicon_driver.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 | 71 |
72 testing::NiceMock<MockFaviconService> favicon_service_; | 72 testing::NiceMock<MockFaviconService> favicon_service_; |
73 }; | 73 }; |
74 | 74 |
75 // Test that a download is initiated when there isn't a favicon in the database | 75 // Test that a download is initiated when there isn't a favicon in the database |
76 // for either the page URL or the icon URL. | 76 // for either the page URL or the icon URL. |
77 TEST_F(ContentFaviconDriverTest, ShouldCauseImageDownload) { | 77 TEST_F(ContentFaviconDriverTest, ShouldCauseImageDownload) { |
78 // Mimic a page load. | 78 // Mimic a page load. |
79 TestFetchFaviconForPage( | 79 TestFetchFaviconForPage( |
80 kPageURL, | 80 kPageURL, |
81 {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, | 81 {content::FaviconURL(kIconURL, content::FaviconURL::IconType::kFavicon, |
82 kEmptyIconSizes)}); | 82 kEmptyIconSizes)}); |
83 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( | 83 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( |
84 kIconURL, 200, kEmptyIcons, kEmptyIconSizes)); | 84 kIconURL, 200, kEmptyIcons, kEmptyIconSizes)); |
85 } | 85 } |
86 | 86 |
87 // Test that Favicon is not requested repeatedly during the same session if | 87 // Test that Favicon is not requested repeatedly during the same session if |
88 // the favicon is known to be unavailable (e.g. due to HTTP 404 status). | 88 // the favicon is known to be unavailable (e.g. due to HTTP 404 status). |
89 TEST_F(ContentFaviconDriverTest, ShouldNotRequestRepeatedlyIfUnavailable) { | 89 TEST_F(ContentFaviconDriverTest, ShouldNotRequestRepeatedlyIfUnavailable) { |
90 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL)) | 90 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL)) |
91 .WillByDefault(Return(true)); | 91 .WillByDefault(Return(true)); |
92 // Mimic a page load. | 92 // Mimic a page load. |
93 TestFetchFaviconForPage( | 93 TestFetchFaviconForPage( |
94 kPageURL, | 94 kPageURL, |
95 {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, | 95 {content::FaviconURL(kIconURL, content::FaviconURL::IconType::kFavicon, |
96 kEmptyIconSizes)}); | 96 kEmptyIconSizes)}); |
97 // Verify that no download request is pending for the image. | 97 // Verify that no download request is pending for the image. |
98 EXPECT_FALSE(web_contents_tester()->HasPendingDownloadImage(kIconURL)); | 98 EXPECT_FALSE(web_contents_tester()->HasPendingDownloadImage(kIconURL)); |
99 } | 99 } |
100 | 100 |
101 TEST_F(ContentFaviconDriverTest, ShouldDownloadSecondIfFirstUnavailable) { | 101 TEST_F(ContentFaviconDriverTest, ShouldDownloadSecondIfFirstUnavailable) { |
102 const GURL kOtherIconURL = GURL("http://www.google.com/other-favicon.ico"); | 102 const GURL kOtherIconURL = GURL("http://www.google.com/other-favicon.ico"); |
103 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL)) | 103 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL)) |
104 .WillByDefault(Return(true)); | 104 .WillByDefault(Return(true)); |
105 // Mimic a page load. | 105 // Mimic a page load. |
106 TestFetchFaviconForPage( | 106 TestFetchFaviconForPage( |
107 kPageURL, | 107 kPageURL, |
108 {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, | 108 {content::FaviconURL(kIconURL, content::FaviconURL::IconType::kFavicon, |
109 kEmptyIconSizes), | 109 kEmptyIconSizes), |
110 content::FaviconURL(kOtherIconURL, content::FaviconURL::FAVICON, | 110 content::FaviconURL(kOtherIconURL, |
| 111 content::FaviconURL::IconType::kFavicon, |
111 kEmptyIconSizes)}); | 112 kEmptyIconSizes)}); |
112 // Verify a download request is pending for the second image. | 113 // Verify a download request is pending for the second image. |
113 EXPECT_FALSE(web_contents_tester()->HasPendingDownloadImage(kIconURL)); | 114 EXPECT_FALSE(web_contents_tester()->HasPendingDownloadImage(kIconURL)); |
114 EXPECT_TRUE(web_contents_tester()->HasPendingDownloadImage(kOtherIconURL)); | 115 EXPECT_TRUE(web_contents_tester()->HasPendingDownloadImage(kOtherIconURL)); |
115 } | 116 } |
116 | 117 |
117 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no | 118 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no |
118 // last committed entry. This occurs when script is injected in about:blank. | 119 // last committed entry. This occurs when script is injected in about:blank. |
119 // See crbug.com/520759 for more details | 120 // See crbug.com/520759 for more details |
120 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { | 121 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { |
121 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); | 122 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); |
122 | 123 |
123 std::vector<content::FaviconURL> favicon_urls; | 124 std::vector<content::FaviconURL> favicon_urls; |
124 favicon_urls.push_back( | 125 favicon_urls.push_back(content::FaviconURL( |
125 content::FaviconURL(GURL("http://www.google.ca/favicon.ico"), | 126 GURL("http://www.google.ca/favicon.ico"), |
126 content::FaviconURL::FAVICON, kEmptyIconSizes)); | 127 content::FaviconURL::IconType::kFavicon, kEmptyIconSizes)); |
127 favicon::ContentFaviconDriver* driver = | 128 favicon::ContentFaviconDriver* driver = |
128 favicon::ContentFaviconDriver::FromWebContents(web_contents()); | 129 favicon::ContentFaviconDriver::FromWebContents(web_contents()); |
129 static_cast<content::WebContentsObserver*>(driver) | 130 static_cast<content::WebContentsObserver*>(driver) |
130 ->DidUpdateFaviconURL(favicon_urls); | 131 ->DidUpdateFaviconURL(favicon_urls); |
131 | 132 |
132 // Test that ContentFaviconDriver ignored the favicon url update. | 133 // Test that ContentFaviconDriver ignored the favicon url update. |
133 EXPECT_TRUE(driver->favicon_urls().empty()); | 134 EXPECT_TRUE(driver->favicon_urls().empty()); |
134 } | 135 } |
135 | 136 |
136 TEST_F(ContentFaviconDriverTest, RecordsHistorgramsForCandidates) { | 137 TEST_F(ContentFaviconDriverTest, RecordsHistorgramsForCandidates) { |
137 const std::vector<gfx::Size> kSizes16x16and32x32({{16, 16}, {32, 32}}); | 138 const std::vector<gfx::Size> kSizes16x16and32x32({{16, 16}, {32, 32}}); |
138 base::HistogramTester tester; | 139 base::HistogramTester tester; |
139 content::WebContentsObserver* driver_as_observer = | 140 content::WebContentsObserver* driver_as_observer = |
140 ContentFaviconDriver::FromWebContents(web_contents()); | 141 ContentFaviconDriver::FromWebContents(web_contents()); |
141 | 142 |
142 // Navigation to a page updating one icon. | 143 // Navigation to a page updating one icon. |
143 NavigateAndCommit(GURL("http://www.youtube.com")); | 144 NavigateAndCommit(GURL("http://www.youtube.com")); |
144 driver_as_observer->DidUpdateFaviconURL( | 145 driver_as_observer->DidUpdateFaviconURL({content::FaviconURL( |
145 {content::FaviconURL(GURL("http://www.youtube.com/favicon.ico"), | 146 GURL("http://www.youtube.com/favicon.ico"), |
146 content::FaviconURL::FAVICON, kSizes16x16and32x32)}); | 147 content::FaviconURL::IconType::kFavicon, kSizes16x16and32x32)}); |
147 | 148 |
148 EXPECT_THAT(tester.GetAllSamples("Favicons.CandidatesCount"), | 149 EXPECT_THAT(tester.GetAllSamples("Favicons.CandidatesCount"), |
149 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); | 150 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); |
150 EXPECT_THAT(tester.GetAllSamples("Favicons.CandidatesWithDefinedSizesCount"), | 151 EXPECT_THAT(tester.GetAllSamples("Favicons.CandidatesWithDefinedSizesCount"), |
151 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); | 152 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); |
152 EXPECT_THAT(tester.GetAllSamples("Favicons.CandidatesWithTouchIconsCount"), | 153 EXPECT_THAT(tester.GetAllSamples("Favicons.CandidatesWithTouchIconsCount"), |
153 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | 154 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
154 | 155 |
155 std::vector<content::FaviconURL> favicon_urls = { | 156 std::vector<content::FaviconURL> favicon_urls = { |
156 content::FaviconURL(GURL("http://www.google.ca/favicon.ico"), | 157 content::FaviconURL(GURL("http://www.google.ca/favicon.ico"), |
157 content::FaviconURL::FAVICON, kSizes16x16and32x32), | 158 content::FaviconURL::IconType::kFavicon, |
| 159 kSizes16x16and32x32), |
158 content::FaviconURL(GURL("http://www.google.ca/precomposed_icon.png"), | 160 content::FaviconURL(GURL("http://www.google.ca/precomposed_icon.png"), |
159 content::FaviconURL::TOUCH_PRECOMPOSED_ICON, | 161 content::FaviconURL::IconType::kTouchPrecomposedIcon, |
160 kEmptyIconSizes), | 162 kEmptyIconSizes), |
161 content::FaviconURL(GURL("http://www.google.ca/touch_icon.png"), | 163 content::FaviconURL(GURL("http://www.google.ca/touch_icon.png"), |
162 content::FaviconURL::TOUCH_ICON, kEmptyIconSizes)}; | 164 content::FaviconURL::IconType::kTouchIcon, |
| 165 kEmptyIconSizes)}; |
163 | 166 |
164 // Double navigation to a page with 3 different icons. | 167 // Double navigation to a page with 3 different icons. |
165 NavigateAndCommit(GURL("http://www.google.ca")); | 168 NavigateAndCommit(GURL("http://www.google.ca")); |
166 driver_as_observer->DidUpdateFaviconURL(favicon_urls); | 169 driver_as_observer->DidUpdateFaviconURL(favicon_urls); |
167 NavigateAndCommit(GURL("http://www.google.ca")); | 170 NavigateAndCommit(GURL("http://www.google.ca")); |
168 driver_as_observer->DidUpdateFaviconURL(favicon_urls); | 171 driver_as_observer->DidUpdateFaviconURL(favicon_urls); |
169 | 172 |
170 EXPECT_THAT(tester.GetAllSamples("Favicons.CandidatesCount"), | 173 EXPECT_THAT(tester.GetAllSamples("Favicons.CandidatesCount"), |
171 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1), | 174 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1), |
172 base::Bucket(/*min=*/3, /*count=*/2))); | 175 base::Bucket(/*min=*/3, /*count=*/2))); |
173 EXPECT_THAT(tester.GetAllSamples("Favicons.CandidatesWithDefinedSizesCount"), | 176 EXPECT_THAT(tester.GetAllSamples("Favicons.CandidatesWithDefinedSizesCount"), |
174 ElementsAre(base::Bucket(/*min=*/1, /*count=*/3))); | 177 ElementsAre(base::Bucket(/*min=*/1, /*count=*/3))); |
175 EXPECT_THAT(tester.GetAllSamples("Favicons.CandidatesWithTouchIconsCount"), | 178 EXPECT_THAT(tester.GetAllSamples("Favicons.CandidatesWithTouchIconsCount"), |
176 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), | 179 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), |
177 base::Bucket(/*min=*/2, /*count=*/2))); | 180 base::Bucket(/*min=*/2, /*count=*/2))); |
178 } | 181 } |
179 | 182 |
180 } // namespace | 183 } // namespace |
181 } // namespace favicon | 184 } // namespace favicon |
OLD | NEW |