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

Unified Diff: components/ntp_tiles/icon_cacher_impl_unittest.cc

Issue 2695713004: Add baked-in favicons for default popular sites on NTP (Closed)
Patch Set: Fix iOS images and resize only if necessary. Created 3 years, 10 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/ntp_tiles/icon_cacher_impl.cc ('k') | components/ntp_tiles/popular_sites.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..fa0bf94cfc20215d59a3d5cbb08c4b73c6a6ce84 100644
--- a/components/ntp_tiles/icon_cacher_impl_unittest.cc
+++ b/components/ntp_tiles/icon_cacher_impl_unittest.cc
@@ -4,6 +4,7 @@
#include "components/ntp_tiles/icon_cacher_impl.h"
+#include <set>
#include <utility>
#include "base/files/scoped_temp_dir.h"
@@ -18,9 +19,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 +44,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()
@@ -54,6 +108,9 @@ class IconCacherTest : public ::testing::Test {
CHECK(history_dir_.CreateUniqueTempDir());
CHECK(history_service_.Init(
history::HistoryDatabaseParams(history_dir_.GetPath(), 0, 0)));
+ if (ui::ResourceBundle::HasSharedInstance()) {
+ ui::ResourceBundle::CleanupSharedInstance();
+ }
}
void PreloadIcon(const GURL& url,
@@ -66,21 +123,26 @@ 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;
}
base::MessageLoop message_loop_;
@@ -195,5 +257,39 @@ TEST_F(IconCacherTest, LargeNotCachedAndFetchFailed) {
EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
}
+TEST_F(IconCacherTest, ProvidesDefaultIconAndSucceedsWithFetching) {
+ FakeResourceDelegate fake_resource;
+ ui::ResourceBundle::InitSharedInstanceWithLocale(
+ "en-US", &fake_resource, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
+ MockFunction<void(bool)> icon_available;
+ 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(icon_available, Call(true)).WillOnce(Quit(&loop_for_static));
+ EXPECT_CALL(*image_fetcher_,
+ StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
+ .WillOnce(PassFetch(128, 128));
+ EXPECT_CALL(icon_available, Call(true)).WillOnce(Quit(&loop_for_fetch));
+ }
+
+ IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
+ site_.default_resource_id = 12345;
+ cacher.StartFetch(site_, BindMockFunction(&icon_available));
+
+ loop_for_static.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.
+ loop_for_fetch.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
« no previous file with comments | « components/ntp_tiles/icon_cacher_impl.cc ('k') | components/ntp_tiles/popular_sites.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698