| 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" |
| 27 #include "third_party/skia/include/core/SkBitmap.h" | 28 #include "third_party/skia/include/core/SkBitmap.h" |
| 28 #include "third_party/skia/include/core/SkColor.h" | 29 #include "third_party/skia/include/core/SkColor.h" |
| 29 #include "ui/gfx/codec/png_codec.h" | 30 #include "ui/gfx/codec/png_codec.h" |
| 30 #include "ui/gfx/geometry/size.h" | 31 #include "ui/gfx/geometry/size.h" |
| 31 #include "ui/gfx/image/image.h" | 32 #include "ui/gfx/image/image.h" |
| 32 #include "ui/gfx/image/image_skia.h" | 33 #include "ui/gfx/image/image_skia.h" |
| 33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 34 | 35 |
| 35 namespace favicon { | 36 namespace favicon { |
| 36 namespace { | 37 namespace { |
| 37 | 38 |
| 39 using testing::IsEmpty; |
| 38 using testing::IsNull; | 40 using testing::IsNull; |
| 39 using testing::Eq; | 41 using testing::Eq; |
| 40 using testing::NiceMock; | 42 using testing::NiceMock; |
| 41 using testing::Return; | 43 using testing::Return; |
| 42 using testing::SaveArg; | 44 using testing::SaveArg; |
| 43 using testing::_; | 45 using testing::_; |
| 44 | 46 |
| 45 const char kDummyUrl[] = "http://www.example.com"; | 47 const char kDummyUrl[] = "http://www.example.com"; |
| 46 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png"; | 48 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png"; |
| 47 const SkColor kTestColor = SK_ColorRED; | 49 const SkColor kTestColor = SK_ColorRED; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 base::WrapUnique(mock_image_fetcher_)) {} | 123 base::WrapUnique(mock_image_fetcher_)) {} |
| 122 | 124 |
| 123 ~LargeIconServiceTest() override {} | 125 ~LargeIconServiceTest() override {} |
| 124 | 126 |
| 125 protected: | 127 protected: |
| 126 base::MessageLoopForIO loop_; | 128 base::MessageLoopForIO loop_; |
| 127 | 129 |
| 128 NiceMock<MockImageFetcher>* mock_image_fetcher_; | 130 NiceMock<MockImageFetcher>* mock_image_fetcher_; |
| 129 testing::NiceMock<MockFaviconService> mock_favicon_service_; | 131 testing::NiceMock<MockFaviconService> mock_favicon_service_; |
| 130 LargeIconService large_icon_service_; | 132 LargeIconService large_icon_service_; |
| 133 base::HistogramTester histogram_tester_; |
| 131 | 134 |
| 132 private: | 135 private: |
| 133 DISALLOW_COPY_AND_ASSIGN(LargeIconServiceTest); | 136 DISALLOW_COPY_AND_ASSIGN(LargeIconServiceTest); |
| 134 }; | 137 }; |
| 135 | 138 |
| 136 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServer) { | 139 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServer) { |
| 137 const GURL kExpectedServerUrl( | 140 const GURL kExpectedServerUrl( |
| 138 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" | 141 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" |
| 139 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE,URL" | 142 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE,URL" |
| 140 "&url=http://www.example.com/"); | 143 "&url=http://www.example.com/"); |
| 141 | 144 |
| 142 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); | 145 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); |
| 143 | 146 |
| 144 base::MockCallback<base::Callback<void(bool success)>> callback; | 147 base::MockCallback<base::Callback<void(bool success)>> callback; |
| 145 EXPECT_CALL(*mock_image_fetcher_, | 148 EXPECT_CALL(*mock_image_fetcher_, |
| 146 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) | 149 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) |
| 147 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap( | 150 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap( |
| 148 CreateTestSkBitmap(64, 64, kTestColor)))); | 151 CreateTestSkBitmap(64, 64, kTestColor)))); |
| 149 EXPECT_CALL(mock_favicon_service_, | 152 EXPECT_CALL(mock_favicon_service_, |
| 150 SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl, | 153 SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl, |
| 151 favicon_base::IconType::TOUCH_ICON, _, _)) | 154 favicon_base::IconType::TOUCH_ICON, _, _)) |
| 152 .WillOnce(PostBoolReply(true)); | 155 .WillOnce(PostBoolReply(true)); |
| 153 | 156 |
| 154 large_icon_service_ | 157 large_icon_service_ |
| 155 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( | 158 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 156 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); | 159 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); |
| 157 | 160 |
| 158 EXPECT_CALL(callback, Run(true)); | 161 EXPECT_CALL(callback, Run(true)); |
| 159 base::RunLoop().RunUntilIdle(); | 162 base::RunLoop().RunUntilIdle(); |
| 163 histogram_tester_.ExpectUniqueSample( |
| 164 "Favicons.LargeIconService.DownloadedSize", 64, /*expected_count=*/1); |
| 160 } | 165 } |
| 161 | 166 |
| 162 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServerWithOriginalUrl) { | 167 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServerWithOriginalUrl) { |
| 163 const GURL kExpectedServerUrl( | 168 const GURL kExpectedServerUrl( |
| 164 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" | 169 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" |
| 165 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE,URL" | 170 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE,URL" |
| 166 "&url=http://www.example.com/"); | 171 "&url=http://www.example.com/"); |
| 167 const GURL kExpectedOriginalUrl("http://www.example.com/favicon.png"); | 172 const GURL kExpectedOriginalUrl("http://www.example.com/favicon.png"); |
| 168 | 173 |
| 169 image_fetcher::RequestMetadata expected_metadata; | 174 image_fetcher::RequestMetadata expected_metadata; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 .Times(0); | 223 .Times(0); |
| 219 | 224 |
| 220 base::MockCallback<base::Callback<void(bool success)>> callback; | 225 base::MockCallback<base::Callback<void(bool success)>> callback; |
| 221 | 226 |
| 222 large_icon_service_ | 227 large_icon_service_ |
| 223 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( | 228 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 224 GURL(kDummyFtpUrl), /*min_source_size_in_pixel=*/42, callback.Get()); | 229 GURL(kDummyFtpUrl), /*min_source_size_in_pixel=*/42, callback.Get()); |
| 225 | 230 |
| 226 EXPECT_CALL(callback, Run(false)); | 231 EXPECT_CALL(callback, Run(false)); |
| 227 base::RunLoop().RunUntilIdle(); | 232 base::RunLoop().RunUntilIdle(); |
| 233 EXPECT_THAT(histogram_tester_.GetAllSamples( |
| 234 "Favicons.LargeIconService.DownloadedSize"), |
| 235 IsEmpty()); |
| 228 } | 236 } |
| 229 | 237 |
| 230 TEST_F(LargeIconServiceTest, ShouldReportUnavailableIfFetchFromServerFails) { | 238 TEST_F(LargeIconServiceTest, ShouldReportUnavailableIfFetchFromServerFails) { |
| 231 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1"); | 239 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1"); |
| 232 const GURL kExpectedServerUrl( | 240 const GURL kExpectedServerUrl( |
| 233 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" | 241 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" |
| 234 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE,URL" | 242 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE,URL" |
| 235 "&url=http://www.example.com/"); | 243 "&url=http://www.example.com/"); |
| 236 | 244 |
| 237 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _)) | 245 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _)) |
| 238 .Times(0); | 246 .Times(0); |
| 239 | 247 |
| 240 base::MockCallback<base::Callback<void(bool success)>> callback; | 248 base::MockCallback<base::Callback<void(bool success)>> callback; |
| 241 EXPECT_CALL(*mock_image_fetcher_, | 249 EXPECT_CALL(*mock_image_fetcher_, |
| 242 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) | 250 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) |
| 243 .WillOnce(PostFetchReply(gfx::Image())); | 251 .WillOnce(PostFetchReply(gfx::Image())); |
| 244 EXPECT_CALL(mock_favicon_service_, | 252 EXPECT_CALL(mock_favicon_service_, |
| 245 UnableToDownloadFavicon(kExpectedServerUrl)); | 253 UnableToDownloadFavicon(kExpectedServerUrl)); |
| 246 | 254 |
| 247 large_icon_service_ | 255 large_icon_service_ |
| 248 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( | 256 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 249 kDummyUrlWithQuery, /*min_source_size_in_pixel=*/42, callback.Get()); | 257 kDummyUrlWithQuery, /*min_source_size_in_pixel=*/42, callback.Get()); |
| 250 | 258 |
| 251 EXPECT_CALL(callback, Run(false)); | 259 EXPECT_CALL(callback, Run(false)); |
| 252 base::RunLoop().RunUntilIdle(); | 260 base::RunLoop().RunUntilIdle(); |
| 261 // Verify that download failure gets recorded. |
| 262 histogram_tester_.ExpectUniqueSample( |
| 263 "Favicons.LargeIconService.DownloadedSize", 0, /*expected_count=*/1); |
| 253 } | 264 } |
| 254 | 265 |
| 255 TEST_F(LargeIconServiceTest, ShoutNotGetFromGoogleServerIfUnavailable) { | 266 TEST_F(LargeIconServiceTest, ShouldNotGetFromGoogleServerIfUnavailable) { |
| 256 ON_CALL( | 267 ON_CALL( |
| 257 mock_favicon_service_, | 268 mock_favicon_service_, |
| 258 WasUnableToDownloadFavicon(GURL( | 269 WasUnableToDownloadFavicon(GURL( |
| 259 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" | 270 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" |
| 260 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE,URL" | 271 "&size=64&min_size=42&max_size=128&fallback_opts=TYPE,SIZE,URL" |
| 261 "&url=http://www.example.com/"))) | 272 "&url=http://www.example.com/"))) |
| 262 .WillByDefault(Return(true)); | 273 .WillByDefault(Return(true)); |
| 263 | 274 |
| 264 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); | 275 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); |
| 265 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _)) | 276 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _)) |
| 266 .Times(0); | 277 .Times(0); |
| 267 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _)) | 278 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _)) |
| 268 .Times(0); | 279 .Times(0); |
| 269 | 280 |
| 270 base::MockCallback<base::Callback<void(bool success)>> callback; | 281 base::MockCallback<base::Callback<void(bool success)>> callback; |
| 271 large_icon_service_ | 282 large_icon_service_ |
| 272 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( | 283 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 273 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); | 284 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); |
| 274 | 285 |
| 275 EXPECT_CALL(callback, Run(false)); | 286 EXPECT_CALL(callback, Run(false)); |
| 276 base::RunLoop().RunUntilIdle(); | 287 base::RunLoop().RunUntilIdle(); |
| 288 EXPECT_THAT(histogram_tester_.GetAllSamples( |
| 289 "Favicons.LargeIconService.DownloadedSize"), |
| 290 IsEmpty()); |
| 277 } | 291 } |
| 278 | 292 |
| 279 class LargeIconServiceGetterTest : public LargeIconServiceTest, | 293 class LargeIconServiceGetterTest : public LargeIconServiceTest, |
| 280 public ::testing::WithParamInterface<bool> { | 294 public ::testing::WithParamInterface<bool> { |
| 281 public: | 295 public: |
| 282 LargeIconServiceGetterTest() : LargeIconServiceTest() {} | 296 LargeIconServiceGetterTest() : LargeIconServiceTest() {} |
| 283 ~LargeIconServiceGetterTest() override {} | 297 ~LargeIconServiceGetterTest() override {} |
| 284 | 298 |
| 285 void GetLargeIconOrFallbackStyleAndWaitForCallback( | 299 void GetLargeIconOrFallbackStyleAndWaitForCallback( |
| 286 const GURL& page_url, | 300 const GURL& page_url, |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 | 433 |
| 420 // Every test will appear with suffix /0 (param false) and /1 (param true), e.g. | 434 // Every test will appear with suffix /0 (param false) and /1 (param true), e.g. |
| 421 // LargeIconServiceGetterTest.FallbackSinceTooPicky/0: get image. | 435 // LargeIconServiceGetterTest.FallbackSinceTooPicky/0: get image. |
| 422 // LargeIconServiceGetterTest.FallbackSinceTooPicky/1: get raw bitmap. | 436 // LargeIconServiceGetterTest.FallbackSinceTooPicky/1: get raw bitmap. |
| 423 INSTANTIATE_TEST_CASE_P(, // Empty instatiation name. | 437 INSTANTIATE_TEST_CASE_P(, // Empty instatiation name. |
| 424 LargeIconServiceGetterTest, | 438 LargeIconServiceGetterTest, |
| 425 ::testing::Values(false, true)); | 439 ::testing::Values(false, true)); |
| 426 | 440 |
| 427 } // namespace | 441 } // namespace |
| 428 } // namespace favicon | 442 } // namespace favicon |
| OLD | NEW |