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

Unified Diff: components/favicon/core/large_icon_service_unittest.cc

Issue 2721363002: Extend LargeIconService to fetch missing favicons from a Google server (Closed)
Patch Set: Add fallback_opts=TYPE 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/favicon/core/large_icon_service.cc ('k') | components/favicon/core/test/mock_favicon_service.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/favicon/core/large_icon_service_unittest.cc
diff --git a/components/favicon/core/large_icon_service_unittest.cc b/components/favicon/core/large_icon_service_unittest.cc
index 7e0b7dce22f8fe4b62c1afa9432d1e814f285379..f61906bb295bd3c946cd724feadad3556a6920a6 100644
--- a/components/favicon/core/large_icon_service_unittest.cc
+++ b/components/favicon/core/large_icon_service_unittest.cc
@@ -9,15 +9,19 @@
#include "base/bind.h"
#include "base/macros.h"
+#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted_memory.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/task/cancelable_task_tracker.h"
+#include "base/test/mock_callback.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/favicon/core/favicon_client.h"
#include "components/favicon/core/test/mock_favicon_service.h"
#include "components/favicon_base/fallback_icon_style.h"
#include "components/favicon_base/favicon_types.h"
+#include "components/image_fetcher/core/image_fetcher.h"
+#include "components/image_fetcher/core/request_metadata.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
@@ -30,14 +34,35 @@
namespace favicon {
namespace {
+using testing::NiceMock;
+using testing::Return;
+using testing::SaveArg;
using testing::_;
const char kDummyUrl[] = "http://www.example.com";
const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png";
const SkColor kTestColor = SK_ColorRED;
-favicon_base::FaviconRawBitmapResult CreateTestBitmap(
- int w, int h, SkColor color) {
+ACTION_P(PostFetchReply, p0) {
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, base::Bind(arg2, arg0, p0, image_fetcher::RequestMetadata()));
+}
+
+ACTION_P(PostBoolReply, p0) {
+ base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
+ base::Bind(arg4, p0));
+}
+
+SkBitmap CreateTestSkBitmap(int w, int h, SkColor color) {
+ SkBitmap bitmap;
+ bitmap.allocN32Pixels(w, h);
+ bitmap.eraseColor(color);
+ return bitmap;
+}
+
+favicon_base::FaviconRawBitmapResult CreateTestBitmapResult(int w,
+ int h,
+ SkColor color) {
favicon_base::FaviconRawBitmapResult result;
result.expired = false;
@@ -56,11 +81,27 @@ favicon_base::FaviconRawBitmapResult CreateTestBitmap(
return result;
}
+class MockImageFetcher : public image_fetcher::ImageFetcher {
+ public:
+ MOCK_METHOD1(SetImageFetcherDelegate,
+ void(image_fetcher::ImageFetcherDelegate* delegate));
+ MOCK_METHOD1(SetDataUseServiceName,
+ void(image_fetcher::ImageFetcher::DataUseServiceName name));
+ MOCK_METHOD1(SetDesiredImageFrameSize, void(const gfx::Size& size));
+ MOCK_METHOD3(StartOrQueueNetworkRequest,
+ void(const std::string&,
+ const GURL&,
+ const ImageFetcherCallback&));
+ MOCK_METHOD0(GetImageDecoder, image_fetcher::ImageDecoder*());
+};
+
class LargeIconServiceTest : public testing::Test {
public:
LargeIconServiceTest()
- : large_icon_service_(&mock_favicon_service_,
- base::ThreadTaskRunnerHandle::Get()),
+ : mock_image_fetcher_(new NiceMock<MockImageFetcher>()),
+ large_icon_service_(&mock_favicon_service_,
+ base::ThreadTaskRunnerHandle::Get(),
+ base::WrapUnique(mock_image_fetcher_)),
is_callback_invoked_(false) {}
~LargeIconServiceTest() override {
@@ -95,7 +136,8 @@ class LargeIconServiceTest : public testing::Test {
protected:
base::MessageLoopForIO loop_;
- testing::StrictMock<MockFaviconService> mock_favicon_service_;
+ NiceMock<MockImageFetcher>* mock_image_fetcher_;
+ testing::NiceMock<MockFaviconService> mock_favicon_service_;
LargeIconService large_icon_service_;
base::CancelableTaskTracker cancelable_task_tracker_;
@@ -110,8 +152,8 @@ class LargeIconServiceTest : public testing::Test {
};
TEST_F(LargeIconServiceTest, SameSize) {
- InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(24, 24, kTestColor));
- expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor);
+ InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(24, 24, kTestColor));
+ expected_bitmap_ = CreateTestBitmapResult(24, 24, kTestColor);
large_icon_service_.GetLargeIconOrFallbackStyle(
GURL(kDummyUrl),
24, // |min_source_size_in_pixel|
@@ -123,8 +165,8 @@ TEST_F(LargeIconServiceTest, SameSize) {
}
TEST_F(LargeIconServiceTest, ScaleDown) {
- InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(32, 32, kTestColor));
- expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor);
+ InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(32, 32, kTestColor));
+ expected_bitmap_ = CreateTestBitmapResult(24, 24, kTestColor);
large_icon_service_.GetLargeIconOrFallbackStyle(
GURL(kDummyUrl), 24, 24,
base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
@@ -134,8 +176,8 @@ TEST_F(LargeIconServiceTest, ScaleDown) {
}
TEST_F(LargeIconServiceTest, ScaleUp) {
- InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(16, 16, kTestColor));
- expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor);
+ InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(16, 16, kTestColor));
+ expected_bitmap_ = CreateTestBitmapResult(24, 24, kTestColor);
large_icon_service_.GetLargeIconOrFallbackStyle(
GURL(kDummyUrl),
14, // Lowered requirement so stored bitmap is admitted.
@@ -148,8 +190,8 @@ TEST_F(LargeIconServiceTest, ScaleUp) {
// |desired_size_in_pixel| == 0 means retrieve original image without scaling.
TEST_F(LargeIconServiceTest, NoScale) {
- InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(24, 24, kTestColor));
- expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor);
+ InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(24, 24, kTestColor));
+ expected_bitmap_ = CreateTestBitmapResult(24, 24, kTestColor);
large_icon_service_.GetLargeIconOrFallbackStyle(
GURL(kDummyUrl), 16, 0,
base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
@@ -159,7 +201,7 @@ TEST_F(LargeIconServiceTest, NoScale) {
}
TEST_F(LargeIconServiceTest, FallbackSinceIconTooSmall) {
- InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(16, 16, kTestColor));
+ InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(16, 16, kTestColor));
expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
expected_fallback_icon_style_->background_color = kTestColor;
expected_fallback_icon_style_->is_default_background_color = false;
@@ -172,7 +214,7 @@ TEST_F(LargeIconServiceTest, FallbackSinceIconTooSmall) {
}
TEST_F(LargeIconServiceTest, FallbackSinceIconNotSquare) {
- InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(24, 32, kTestColor));
+ InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(24, 32, kTestColor));
expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
expected_fallback_icon_style_->background_color = kTestColor;
expected_fallback_icon_style_->is_default_background_color = false;
@@ -211,7 +253,7 @@ TEST_F(LargeIconServiceTest, FallbackSinceIconMissingNoScale) {
// Oddball case where we demand a high resolution icon to scale down. Generates
// fallback even though an icon with the final size is available.
TEST_F(LargeIconServiceTest, FallbackSinceTooPicky) {
- InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(24, 24, kTestColor));
+ InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(24, 24, kTestColor));
expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
expected_fallback_icon_style_->background_color = kTestColor;
expected_fallback_icon_style_->is_default_background_color = false;
@@ -223,5 +265,118 @@ TEST_F(LargeIconServiceTest, FallbackSinceTooPicky) {
EXPECT_TRUE(is_callback_invoked_);
}
+TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServer) {
+ const GURL kExpectedServerUrl(
+ "https://t0.gstatic.com/faviconV2?user=chrome&drop_404_icon=true"
+ "&size=192&min_size=42&max_size=256&fallback_opts=TYPE"
+ "&url=http://www.example.com/");
+
+ EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0);
+
+ base::MockCallback<base::Callback<void(bool success)>> callback;
+ EXPECT_CALL(*mock_image_fetcher_,
+ StartOrQueueNetworkRequest(_, kExpectedServerUrl, _))
+ .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap(
+ CreateTestSkBitmap(64, 64, kTestColor))));
+ EXPECT_CALL(mock_favicon_service_,
+ SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl,
+ favicon_base::IconType::TOUCH_ICON, _, _))
+ .WillOnce(PostBoolReply(true));
+
+ large_icon_service_
+ .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
+ GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get());
+
+ EXPECT_CALL(callback, Run(true));
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(LargeIconServiceTest, ShouldTrimQueryParametersForGoogleServer) {
+ const GURL kDummyUrlWithQuery("http://www.example.com?foo=1");
+ const GURL kExpectedServerUrl(
+ "https://t0.gstatic.com/faviconV2?user=chrome&drop_404_icon=true"
+ "&size=192&min_size=42&max_size=256&fallback_opts=TYPE"
+ "&url=http://www.example.com/");
+
+ EXPECT_CALL(*mock_image_fetcher_,
+ StartOrQueueNetworkRequest(_, kExpectedServerUrl, _))
+ .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap(
+ CreateTestSkBitmap(64, 64, kTestColor))));
+ // Verify that the non-trimmed page URL is used when writing to the database.
+ EXPECT_CALL(mock_favicon_service_,
+ SetLastResortFavicons(_, kExpectedServerUrl, _, _, _));
+
+ large_icon_service_
+ .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
+ GURL(kDummyUrlWithQuery), /*min_source_size_in_pixel=*/42,
+ base::Callback<void(bool success)>());
+
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(LargeIconServiceTest, ShouldNotQueryGoogleServerIfInvalidScheme) {
+ const GURL kDummyFtpUrl("ftp://www.example.com");
+
+ EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _))
+ .Times(0);
+
+ base::MockCallback<base::Callback<void(bool success)>> callback;
+
+ large_icon_service_
+ .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
+ GURL(kDummyFtpUrl), /*min_source_size_in_pixel=*/42, callback.Get());
+
+ EXPECT_CALL(callback, Run(false));
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(LargeIconServiceTest, ShouldReportUnavailableIfFetchFromServerFails) {
+ const GURL kDummyUrlWithQuery("http://www.example.com?foo=1");
+ const GURL kExpectedServerUrl(
+ "https://t0.gstatic.com/faviconV2?user=chrome&drop_404_icon=true"
+ "&size=192&min_size=42&max_size=256&fallback_opts=TYPE"
+ "&url=http://www.example.com/");
+
+ EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _))
+ .Times(0);
+
+ base::MockCallback<base::Callback<void(bool success)>> callback;
+ EXPECT_CALL(*mock_image_fetcher_,
+ StartOrQueueNetworkRequest(_, kExpectedServerUrl, _))
+ .WillOnce(PostFetchReply(gfx::Image()));
+ EXPECT_CALL(mock_favicon_service_,
+ UnableToDownloadFavicon(kExpectedServerUrl));
+
+ large_icon_service_
+ .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
+ kDummyUrlWithQuery, /*min_source_size_in_pixel=*/42, callback.Get());
+
+ EXPECT_CALL(callback, Run(false));
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(LargeIconServiceTest, ShoutNotGetFromGoogleServerIfUnavailable) {
+ ON_CALL(mock_favicon_service_,
+ WasUnableToDownloadFavicon(GURL(
+ "https://t0.gstatic.com/faviconV2?user=chrome&drop_404_icon=true"
+ "&size=192&min_size=42&max_size=256&fallback_opts=TYPE"
+ "&url=http://www.example.com/")))
+ .WillByDefault(Return(true));
+
+ EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0);
+ EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _))
+ .Times(0);
+ EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _))
+ .Times(0);
+
+ base::MockCallback<base::Callback<void(bool success)>> callback;
+ large_icon_service_
+ .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
+ GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get());
+
+ EXPECT_CALL(callback, Run(false));
+ base::RunLoop().RunUntilIdle();
+}
+
} // namespace
} // namespace favicon
« no previous file with comments | « components/favicon/core/large_icon_service.cc ('k') | components/favicon/core/test/mock_favicon_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698