Chromium Code Reviews| 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 12dcf815e12018d121d9299f7b9830ad40b23bfb..ff08c63e3bac227baff847e1a521844d0341ceb4 100644 |
| --- a/components/ntp_tiles/icon_cacher_impl_unittest.cc |
| +++ b/components/ntp_tiles/icon_cacher_impl_unittest.cc |
| @@ -4,11 +4,13 @@ |
| #include "components/ntp_tiles/icon_cacher_impl.h" |
| +#include <set> |
| #include <utility> |
| #include "base/files/scoped_temp_dir.h" |
| #include "base/memory/ptr_util.h" |
| #include "base/run_loop.h" |
| +#include "base/test/test_simple_task_runner.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "components/favicon/core/favicon_client.h" |
| #include "components/favicon/core/favicon_service_impl.h" |
| @@ -18,9 +20,11 @@ |
| #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::_; |
| +using ::testing::Eq; |
| using ::testing::InSequence; |
| using ::testing::MockFunction; |
| using ::testing::Return; |
| @@ -41,6 +45,57 @@ 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 FakeResourceDelegate : public ui::ResourceBundle::Delegate { |
| + public: |
| + ~FakeResourceDelegate() 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. |
| + } |
| + |
| + // Returns a non-empty image for any resource and logs the resource id. |
| + gfx::Image GetImageNamed(int resource_id) override { |
| + requested_resources.insert(resource_id); |
| + return gfx::test::CreateImage(64, 64); |
| + } |
| + |
| + 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. |
| + } |
| + |
| + // Returns whether a resource was requested at least once. |
| + bool HasProvidedResource(int resource_id) { |
| + return requested_resources.find(resource_id) != requested_resources.end(); |
| + } |
| + |
| + private: |
| + std::set<int> requested_resources; |
| +}; |
| + |
| class IconCacherTest : public ::testing::Test { |
| protected: |
| IconCacherTest() |
| @@ -50,10 +105,16 @@ class IconCacherTest : public ::testing::Test { |
| GURL("http://url.google/favicon.ico"), |
| GURL()), // thumbnail, unused |
| image_fetcher_(new ::testing::StrictMock<MockImageFetcher>), |
| - favicon_service_(/*favicon_client=*/nullptr, &history_service_) { |
| + favicon_service_(/*favicon_client=*/nullptr, &history_service_), |
| + task_runner_(new base::TestSimpleTaskRunner()) { |
| CHECK(history_dir_.CreateUniqueTempDir()); |
| CHECK(history_service_.Init( |
| history::HistoryDatabaseParams(history_dir_.GetPath(), 0, 0))); |
| + if (ui::ResourceBundle::HasSharedInstance()) { |
| + ui::ResourceBundle::CleanupSharedInstance(); |
| + } |
| + ui::ResourceBundle::InitSharedInstanceWithLocale( |
| + "en-US", &fake_resource_, ui::ResourceBundle::LOAD_COMMON_RESOURCES); |
| } |
| void PreloadIcon(const GURL& url, |
| @@ -66,29 +127,38 @@ class IconCacherTest : public ::testing::Test { |
| } |
| bool IconIsCachedFor(const GURL& url, favicon_base::IconType icon_type) { |
| + return !GetCachedIconFor(url, icon_type).IsEmpty(); |
| + } |
| + |
| + gfx::Image GetCachedIconFor(const GURL& url, |
| + favicon_base::IconType icon_type) { |
| base::CancelableTaskTracker tracker; |
| - bool is_cached; |
| + gfx::Image image; |
| base::RunLoop loop; |
| favicon::GetFaviconImageForPageURL( |
| &favicon_service_, url, icon_type, |
| base::Bind( |
| - [](bool* is_cached, base::RunLoop* loop, |
| + [](gfx::Image* image, base::RunLoop* loop, |
| const favicon_base::FaviconImageResult& result) { |
| - *is_cached = !result.image.IsEmpty(); |
| + *image = result.image; |
| loop->Quit(); |
| }, |
| - &is_cached, &loop), |
| + &image, &loop), |
| &tracker); |
| loop.Run(); |
| - return is_cached; |
| + return image; |
| } |
| + void WaitForTasksToFinish() { task_runner_->RunUntilIdle(); } |
| + |
| base::MessageLoop message_loop_; |
| PopularSites::Site site_; |
| std::unique_ptr<MockImageFetcher> image_fetcher_; |
| base::ScopedTempDir history_dir_; |
| history::HistoryService history_service_; |
| favicon::FaviconServiceImpl favicon_service_; |
| + scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| + FakeResourceDelegate fake_resource_; |
| }; |
| template <typename Fn> |
| @@ -111,27 +181,22 @@ ACTION_P(Quit, run_loop) { |
| } |
| TEST_F(IconCacherTest, LargeCached) { |
| - MockFunction<void(bool)> done; |
| - base::RunLoop loop; |
| - { |
| - InSequence s; |
| - EXPECT_CALL(*image_fetcher_, |
| - SetDataUseServiceName( |
| - data_use_measurement::DataUseUserData::NTP_TILES)); |
| - EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop)); |
| - } |
| + MockFunction<void()> done; |
| + EXPECT_CALL(done, Call()).Times(0); |
| + EXPECT_CALL( |
| + *image_fetcher_, |
| + SetDataUseServiceName(data_use_measurement::DataUseUserData::NTP_TILES)); |
| PreloadIcon(site_.url, site_.large_icon_url, favicon_base::TOUCH_ICON, 128, |
| 128); |
| - |
| IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); |
| - cacher.StartFetch(site_, BindMockFunction(&done)); |
| - loop.Run(); |
| + cacher.StartFetch(site_, BindMockFunction(&done), BindMockFunction(&done)); |
| + WaitForTasksToFinish(); |
| EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); |
| EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); |
| } |
| TEST_F(IconCacherTest, LargeNotCachedAndFetchSucceeded) { |
| - MockFunction<void(bool)> done; |
| + MockFunction<void()> done; |
| base::RunLoop loop; |
| { |
| InSequence s; |
| @@ -141,11 +206,11 @@ TEST_F(IconCacherTest, LargeNotCachedAndFetchSucceeded) { |
| EXPECT_CALL(*image_fetcher_, |
| StartOrQueueNetworkRequest(_, site_.large_icon_url, _)) |
| .WillOnce(PassFetch(128, 128)); |
| - EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop)); |
| + EXPECT_CALL(done, Call()).WillOnce(Quit(&loop)); |
| } |
| IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); |
| - cacher.StartFetch(site_, BindMockFunction(&done)); |
| + cacher.StartFetch(site_, BindMockFunction(&done), BindMockFunction(&done)); |
| loop.Run(); |
| EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); |
| EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); |
| @@ -154,7 +219,7 @@ TEST_F(IconCacherTest, LargeNotCachedAndFetchSucceeded) { |
| TEST_F(IconCacherTest, SmallNotCachedAndFetchSucceeded) { |
| site_.large_icon_url = GURL(); |
| - MockFunction<void(bool)> done; |
| + MockFunction<void()> done; |
| base::RunLoop loop; |
| { |
| InSequence s; |
| @@ -164,19 +229,19 @@ TEST_F(IconCacherTest, SmallNotCachedAndFetchSucceeded) { |
| EXPECT_CALL(*image_fetcher_, |
| StartOrQueueNetworkRequest(_, site_.favicon_url, _)) |
| .WillOnce(PassFetch(128, 128)); |
| - EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop)); |
| + EXPECT_CALL(done, Call()).WillOnce(Quit(&loop)); |
| } |
| IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); |
| - cacher.StartFetch(site_, BindMockFunction(&done)); |
| + cacher.StartFetch(site_, BindMockFunction(&done), BindMockFunction(&done)); |
| loop.Run(); |
| EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); |
| EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); |
| } |
| TEST_F(IconCacherTest, LargeNotCachedAndFetchFailed) { |
| - MockFunction<void(bool)> done; |
| - base::RunLoop loop; |
| + MockFunction<void()> done; |
| + EXPECT_CALL(done, Call()).Times(0); |
| { |
| InSequence s; |
| EXPECT_CALL(*image_fetcher_, |
| @@ -185,15 +250,48 @@ TEST_F(IconCacherTest, LargeNotCachedAndFetchFailed) { |
| EXPECT_CALL(*image_fetcher_, |
| StartOrQueueNetworkRequest(_, site_.large_icon_url, _)) |
| .WillOnce(FailFetch()); |
| - EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop)); |
| } |
| IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); |
| - cacher.StartFetch(site_, BindMockFunction(&done)); |
| - loop.Run(); |
| + cacher.StartFetch(site_, BindMockFunction(&done), BindMockFunction(&done)); |
| + WaitForTasksToFinish(); |
| EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); |
| EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); |
| } |
| +TEST_F(IconCacherTest, ProvidesDefaultIconAndSucceedsWithFetching) { |
| + MockFunction<void()> preliminary_icon_available; |
| + MockFunction<void()> icon_available; |
| + base::RunLoop default_loop; |
| + base::RunLoop fetch_loop; |
|
mastiz
2017/03/02 11:05:39
With the adoption of a mock delegate, I believe yo
fhorschig
2017/03/02 11:56:24
As we discussed, more elegant solutions would requ
|
| + { |
| + InSequence s; |
| + EXPECT_CALL(*image_fetcher_, |
| + SetDataUseServiceName( |
| + data_use_measurement::DataUseUserData::NTP_TILES)); |
| + EXPECT_CALL(preliminary_icon_available, Call()) |
| + .WillOnce(Quit(&default_loop)); |
| + EXPECT_CALL(*image_fetcher_, |
| + StartOrQueueNetworkRequest(_, site_.large_icon_url, _)) |
| + .WillOnce(PassFetch(128, 128)); |
| + EXPECT_CALL(icon_available, Call()).WillOnce(Quit(&fetch_loop)); |
| + } |
| + |
| + IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); |
| + site_.default_icon_resource = 12345; |
| + cacher.StartFetch(site_, BindMockFunction(&icon_available), |
| + BindMockFunction(&preliminary_icon_available)); |
| + |
| + default_loop.Run(); // Wait for the default image. |
| + EXPECT_TRUE(fake_resource_.HasProvidedResource(12345)); |
| + EXPECT_THAT(GetCachedIconFor(site_.url, favicon_base::TOUCH_ICON).Size(), |
| + Eq(gfx::Size(64, 64))); // Compares dimensions, not objects. |
| + |
| + // Let the fetcher continue and wait for the second call of the callback. |
| + fetch_loop.Run(); // Wait for the updated image. |
| + EXPECT_THAT(GetCachedIconFor(site_.url, favicon_base::TOUCH_ICON).Size(), |
| + Eq(gfx::Size(128, 128))); // Compares dimensions, not objects. |
| +} |
| + |
| } // namespace |
| } // namespace ntp_tiles |