Index: components/ntp_tiles/icon_cacher_impl_unittest.cc |
diff --git a/components/ntp_tiles/icon_cacher_impl_unittest.cc b/components/ntp_tiles/icon_cacher_impl_unittest.cc |
index f80289c8ae14b2fb75deb14249304bdab0ab18c4..20f7c3237ee75e1281471ce73fc7c1a1ca75446f 100644 |
--- a/components/ntp_tiles/icon_cacher_impl_unittest.cc |
+++ b/components/ntp_tiles/icon_cacher_impl_unittest.cc |
@@ -18,6 +18,7 @@ |
#include "components/image_fetcher/image_fetcher.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/base/resource/resource_bundle.h" |
#include "ui/gfx/image/image_unittest_util.h" |
using ::testing::_; |
@@ -41,6 +42,45 @@ class MockImageFetcher : public image_fetcher::ImageFetcher { |
const gfx::Image& image)> callback)); |
}; |
+// This class provides methods to inject an image resource where a real resource |
+// would be necessary otherwise. All other methods have return values that allow |
+// the normal implementation to proceed. |
+class MockResourceDelegate : public ui::ResourceBundle::Delegate { |
sfiera
2017/02/16 18:54:16
This class is (mostly) not a mock—please don't mix
fhorschig
2017/02/17 16:24:03
Done.
|
+ public: |
+ ~MockResourceDelegate() override {} |
+ base::FilePath GetPathForResourcePack(const base::FilePath& pack_path, |
+ ui::ScaleFactor scale_factor) override { |
+ return pack_path; // Continue Loading pack file. |
+ } |
+ |
+ base::FilePath GetPathForLocalePack(const base::FilePath& pack_path, |
+ const std::string& locale) override { |
+ return pack_path; // Continue Loading pack file. |
+ } |
+ |
+ MOCK_METHOD1(GetImageNamed, gfx::Image(int)); |
+ |
+ gfx::Image GetNativeImageNamed(int resource_id) override { |
+ return gfx::Image(); // Attempt retrieval of the default resource. |
+ } |
+ |
+ base::RefCountedMemory* LoadDataResourceBytes( |
+ int resource_id, |
+ ui::ScaleFactor scale_factor) override { |
+ return nullptr; // Attempt retrieval of the default resource. |
+ } |
+ |
+ bool GetRawDataResource(int resource_id, |
+ ui::ScaleFactor scale_factor, |
+ base::StringPiece* value) override { |
+ return false; // Attempt retrieval of the default resource. |
+ } |
+ |
+ bool GetLocalizedString(int message_id, base::string16* value) override { |
+ return false; // Attempt retrieval of the default resource. |
+ } |
+}; |
+ |
class IconCacherTest : public ::testing::Test { |
protected: |
IconCacherTest() |
@@ -195,5 +235,43 @@ TEST_F(IconCacherTest, LargeNotCachedAndFetchFailed) { |
EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); |
} |
+TEST_F(IconCacherTest, ProvidesDefaultIconAndSucceedsWithFetching) { |
+ if (ui::ResourceBundle::HasSharedInstance()) { |
sfiera
2017/02/16 18:54:16
I'd put this as setup for IconCacherTest. Even if
fhorschig
2017/02/17 16:24:03
Done.
|
+ ui::ResourceBundle::CleanupSharedInstance(); |
+ } |
+ |
+ // Returns true after the global resource loader instance has been created. |
+ MockResourceDelegate mock_resource; |
+ ui::ResourceBundle::InitSharedInstanceWithLocale( |
+ "en-US", &mock_resource, ui::ResourceBundle::LOAD_COMMON_RESOURCES); |
+ MockFunction<void(bool)> done; |
+ base::RunLoop loop_for_static; |
+ base::RunLoop loop_for_fetch; |
+ { |
+ InSequence s; |
+ EXPECT_CALL(*image_fetcher_, |
+ SetDataUseServiceName( |
+ data_use_measurement::DataUseUserData::NTP_TILES)); |
+ EXPECT_CALL(mock_resource, GetImageNamed(12345)) |
+ .WillOnce(Return(gfx::test::CreateImage(64, 64))); |
+ EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop_for_static)); |
+ EXPECT_CALL(*image_fetcher_, |
+ StartOrQueueNetworkRequest(_, site_.large_icon_url, _)) |
+ .WillOnce(PassFetch(128, 128)); |
+ EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop_for_fetch)); |
+ } |
+ |
+ IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); |
+ site_.default_resource_id = 12345; |
+ cacher.StartFetch(site_, BindMockFunction(&done)); |
+ |
+ loop_for_static.Run(); // Wait for the default image. |
+ EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); |
sfiera
2017/02/16 18:54:16
Can you add a check for the size here, so that we
fhorschig
2017/02/17 16:24:03
Done.
|
+ |
+ // Let the fetcher continue and wait for the second call of the callback. |
+ loop_for_fetch.Run(); // Wait for the updated image. |
+ EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); |
+} |
+ |
} // namespace |
} // namespace ntp_tiles |