| 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/core/large_icon_service.h" | 5 #include "components/favicon/core/large_icon_service.h" |
| 6 | 6 |
| 7 #include <deque> | 7 #include <deque> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/task/cancelable_task_tracker.h" | 16 #include "base/task/cancelable_task_tracker.h" |
| 17 #include "base/test/histogram_tester.h" |
| 17 #include "base/test/mock_callback.h" | 18 #include "base/test/mock_callback.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "components/favicon/core/favicon_client.h" | 20 #include "components/favicon/core/favicon_client.h" |
| 20 #include "components/favicon/core/test/mock_favicon_service.h" | 21 #include "components/favicon/core/test/mock_favicon_service.h" |
| 21 #include "components/favicon_base/fallback_icon_style.h" | 22 #include "components/favicon_base/fallback_icon_style.h" |
| 22 #include "components/favicon_base/favicon_types.h" | 23 #include "components/favicon_base/favicon_types.h" |
| 23 #include "components/image_fetcher/core/image_fetcher.h" | 24 #include "components/image_fetcher/core/image_fetcher.h" |
| 24 #include "components/image_fetcher/core/request_metadata.h" | 25 #include "components/image_fetcher/core/request_metadata.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 base::WrapUnique(mock_image_fetcher_)) {} | 117 base::WrapUnique(mock_image_fetcher_)) {} |
| 117 | 118 |
| 118 ~LargeIconServiceTest() override {} | 119 ~LargeIconServiceTest() override {} |
| 119 | 120 |
| 120 protected: | 121 protected: |
| 121 base::MessageLoopForIO loop_; | 122 base::MessageLoopForIO loop_; |
| 122 | 123 |
| 123 NiceMock<MockImageFetcher>* mock_image_fetcher_; | 124 NiceMock<MockImageFetcher>* mock_image_fetcher_; |
| 124 testing::NiceMock<MockFaviconService> mock_favicon_service_; | 125 testing::NiceMock<MockFaviconService> mock_favicon_service_; |
| 125 LargeIconService large_icon_service_; | 126 LargeIconService large_icon_service_; |
| 127 base::HistogramTester histogram_tester_; |
| 126 | 128 |
| 127 private: | 129 private: |
| 128 DISALLOW_COPY_AND_ASSIGN(LargeIconServiceTest); | 130 DISALLOW_COPY_AND_ASSIGN(LargeIconServiceTest); |
| 129 }; | 131 }; |
| 130 | 132 |
| 131 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServer) { | 133 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServer) { |
| 132 const GURL kExpectedServerUrl( | 134 const GURL kExpectedServerUrl( |
| 133 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" | 135 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" |
| 134 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE" | 136 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE" |
| 135 "&url=http://www.example.com/"); | 137 "&url=http://www.example.com/"); |
| 136 | 138 |
| 137 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); | 139 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); |
| 138 | 140 |
| 139 base::MockCallback<base::Callback<void(bool success)>> callback; | 141 base::MockCallback<base::Callback<void(bool success)>> callback; |
| 140 EXPECT_CALL(*mock_image_fetcher_, | 142 EXPECT_CALL(*mock_image_fetcher_, |
| 141 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) | 143 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) |
| 142 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap( | 144 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap( |
| 143 CreateTestSkBitmap(64, 64, kTestColor)))); | 145 CreateTestSkBitmap(64, 64, kTestColor)))); |
| 144 EXPECT_CALL(mock_favicon_service_, | 146 EXPECT_CALL(mock_favicon_service_, |
| 145 SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl, | 147 SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl, |
| 146 favicon_base::IconType::TOUCH_ICON, _, _)) | 148 favicon_base::IconType::TOUCH_ICON, _, _)) |
| 147 .WillOnce(PostBoolReply(true)); | 149 .WillOnce(PostBoolReply(true)); |
| 148 | 150 |
| 149 large_icon_service_ | 151 large_icon_service_ |
| 150 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( | 152 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 151 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); | 153 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); |
| 152 | 154 |
| 153 EXPECT_CALL(callback, Run(true)); | 155 EXPECT_CALL(callback, Run(true)); |
| 154 base::RunLoop().RunUntilIdle(); | 156 base::RunLoop().RunUntilIdle(); |
| 157 histogram_tester_.ExpectUniqueSample( |
| 158 "Favicons.LargeIconService.DownloadedSize", 64, /*expected_count=*/1); |
| 155 } | 159 } |
| 156 | 160 |
| 157 TEST_F(LargeIconServiceTest, ShouldTrimQueryParametersForGoogleServer) { | 161 TEST_F(LargeIconServiceTest, ShouldTrimQueryParametersForGoogleServer) { |
| 158 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1"); | 162 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1"); |
| 159 const GURL kExpectedServerUrl( | 163 const GURL kExpectedServerUrl( |
| 160 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" | 164 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" |
| 161 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE" | 165 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE" |
| 162 "&url=http://www.example.com/"); | 166 "&url=http://www.example.com/"); |
| 163 | 167 |
| 164 EXPECT_CALL(*mock_image_fetcher_, | 168 EXPECT_CALL(*mock_image_fetcher_, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 184 .Times(0); | 188 .Times(0); |
| 185 | 189 |
| 186 base::MockCallback<base::Callback<void(bool success)>> callback; | 190 base::MockCallback<base::Callback<void(bool success)>> callback; |
| 187 | 191 |
| 188 large_icon_service_ | 192 large_icon_service_ |
| 189 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( | 193 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 190 GURL(kDummyFtpUrl), /*min_source_size_in_pixel=*/42, callback.Get()); | 194 GURL(kDummyFtpUrl), /*min_source_size_in_pixel=*/42, callback.Get()); |
| 191 | 195 |
| 192 EXPECT_CALL(callback, Run(false)); | 196 EXPECT_CALL(callback, Run(false)); |
| 193 base::RunLoop().RunUntilIdle(); | 197 base::RunLoop().RunUntilIdle(); |
| 198 // No download histogram recorded. |
| 199 histogram_tester_.ExpectUniqueSample( |
| 200 "Favicons.LargeIconService.DownloadedSize", 0, /*expected_count=*/0); |
| 194 } | 201 } |
| 195 | 202 |
| 196 TEST_F(LargeIconServiceTest, ShouldReportUnavailableIfFetchFromServerFails) { | 203 TEST_F(LargeIconServiceTest, ShouldReportUnavailableIfFetchFromServerFails) { |
| 197 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1"); | 204 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1"); |
| 198 const GURL kExpectedServerUrl( | 205 const GURL kExpectedServerUrl( |
| 199 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" | 206 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" |
| 200 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE" | 207 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE" |
| 201 "&url=http://www.example.com/"); | 208 "&url=http://www.example.com/"); |
| 202 | 209 |
| 203 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _)) | 210 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _)) |
| 204 .Times(0); | 211 .Times(0); |
| 205 | 212 |
| 206 base::MockCallback<base::Callback<void(bool success)>> callback; | 213 base::MockCallback<base::Callback<void(bool success)>> callback; |
| 207 EXPECT_CALL(*mock_image_fetcher_, | 214 EXPECT_CALL(*mock_image_fetcher_, |
| 208 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) | 215 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) |
| 209 .WillOnce(PostFetchReply(gfx::Image())); | 216 .WillOnce(PostFetchReply(gfx::Image())); |
| 210 EXPECT_CALL(mock_favicon_service_, | 217 EXPECT_CALL(mock_favicon_service_, |
| 211 UnableToDownloadFavicon(kExpectedServerUrl)); | 218 UnableToDownloadFavicon(kExpectedServerUrl)); |
| 212 | 219 |
| 213 large_icon_service_ | 220 large_icon_service_ |
| 214 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( | 221 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 215 kDummyUrlWithQuery, /*min_source_size_in_pixel=*/42, callback.Get()); | 222 kDummyUrlWithQuery, /*min_source_size_in_pixel=*/42, callback.Get()); |
| 216 | 223 |
| 217 EXPECT_CALL(callback, Run(false)); | 224 EXPECT_CALL(callback, Run(false)); |
| 218 base::RunLoop().RunUntilIdle(); | 225 base::RunLoop().RunUntilIdle(); |
| 226 histogram_tester_.ExpectUniqueSample( |
| 227 "Favicons.LargeIconService.DownloadedSize", 0, /*expected_count=*/1); |
| 219 } | 228 } |
| 220 | 229 |
| 221 TEST_F(LargeIconServiceTest, ShoutNotGetFromGoogleServerIfUnavailable) { | 230 TEST_F(LargeIconServiceTest, ShoutNotGetFromGoogleServerIfUnavailable) { |
| 222 ON_CALL( | 231 ON_CALL( |
| 223 mock_favicon_service_, | 232 mock_favicon_service_, |
| 224 WasUnableToDownloadFavicon(GURL( | 233 WasUnableToDownloadFavicon(GURL( |
| 225 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" | 234 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" |
| 226 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE" | 235 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE" |
| 227 "&url=http://www.example.com/"))) | 236 "&url=http://www.example.com/"))) |
| 228 .WillByDefault(Return(true)); | 237 .WillByDefault(Return(true)); |
| 229 | 238 |
| 230 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); | 239 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); |
| 231 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _)) | 240 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _)) |
| 232 .Times(0); | 241 .Times(0); |
| 233 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _)) | 242 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _)) |
| 234 .Times(0); | 243 .Times(0); |
| 235 | 244 |
| 236 base::MockCallback<base::Callback<void(bool success)>> callback; | 245 base::MockCallback<base::Callback<void(bool success)>> callback; |
| 237 large_icon_service_ | 246 large_icon_service_ |
| 238 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( | 247 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 239 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); | 248 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); |
| 240 | 249 |
| 241 EXPECT_CALL(callback, Run(false)); | 250 EXPECT_CALL(callback, Run(false)); |
| 242 base::RunLoop().RunUntilIdle(); | 251 base::RunLoop().RunUntilIdle(); |
| 252 // No download histogram recorded. |
| 253 histogram_tester_.ExpectUniqueSample( |
| 254 "Favicons.LargeIconService.DownloadedSize", 0, /*expected_count=*/0); |
| 243 } | 255 } |
| 244 | 256 |
| 245 class LargeIconServiceGetterTest : public LargeIconServiceTest, | 257 class LargeIconServiceGetterTest : public LargeIconServiceTest, |
| 246 public ::testing::WithParamInterface<bool> { | 258 public ::testing::WithParamInterface<bool> { |
| 247 public: | 259 public: |
| 248 LargeIconServiceGetterTest() : LargeIconServiceTest() {} | 260 LargeIconServiceGetterTest() : LargeIconServiceTest() {} |
| 249 ~LargeIconServiceGetterTest() override {} | 261 ~LargeIconServiceGetterTest() override {} |
| 250 | 262 |
| 251 void GetLargeIconOrFallbackStyleAndWaitForCallback( | 263 void GetLargeIconOrFallbackStyleAndWaitForCallback( |
| 252 const GURL& page_url, | 264 const GURL& page_url, |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 | 397 |
| 386 // Every test will appear with suffix /0 (param false) and /1 (param true), e.g. | 398 // Every test will appear with suffix /0 (param false) and /1 (param true), e.g. |
| 387 // LargeIconServiceGetterTest.FallbackSinceTooPicky/0: get image. | 399 // LargeIconServiceGetterTest.FallbackSinceTooPicky/0: get image. |
| 388 // LargeIconServiceGetterTest.FallbackSinceTooPicky/1: get raw bitmap. | 400 // LargeIconServiceGetterTest.FallbackSinceTooPicky/1: get raw bitmap. |
| 389 INSTANTIATE_TEST_CASE_P(, // Empty instatiation name. | 401 INSTANTIATE_TEST_CASE_P(, // Empty instatiation name. |
| 390 LargeIconServiceGetterTest, | 402 LargeIconServiceGetterTest, |
| 391 ::testing::Values(false, true)); | 403 ::testing::Values(false, true)); |
| 392 | 404 |
| 393 } // namespace | 405 } // namespace |
| 394 } // namespace favicon | 406 } // namespace favicon |
| OLD | NEW |