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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/ntp_tiles/icon_cacher_impl.h" 5 #include "components/ntp_tiles/icon_cacher_impl.h"
6 6
7 #include <set>
7 #include <utility> 8 #include <utility>
8 9
9 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
11 #include "base/run_loop.h" 12 #include "base/run_loop.h"
12 #include "base/threading/thread_task_runner_handle.h" 13 #include "base/threading/thread_task_runner_handle.h"
13 #include "components/favicon/core/favicon_client.h" 14 #include "components/favicon/core/favicon_client.h"
14 #include "components/favicon/core/favicon_service.h" 15 #include "components/favicon/core/favicon_service.h"
15 #include "components/favicon/core/favicon_util.h" 16 #include "components/favicon/core/favicon_util.h"
16 #include "components/history/core/browser/history_database_params.h" 17 #include "components/history/core/browser/history_database_params.h"
17 #include "components/history/core/browser/history_service.h" 18 #include "components/history/core/browser/history_service.h"
18 #include "components/image_fetcher/image_fetcher.h" 19 #include "components/image_fetcher/image_fetcher.h"
19 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/gfx/image/image_unittest_util.h" 23 #include "ui/gfx/image/image_unittest_util.h"
22 24
23 using ::testing::_; 25 using ::testing::_;
26 using ::testing::Eq;
24 using ::testing::InSequence; 27 using ::testing::InSequence;
25 using ::testing::MockFunction; 28 using ::testing::MockFunction;
26 using ::testing::Return; 29 using ::testing::Return;
27 30
28 namespace ntp_tiles { 31 namespace ntp_tiles {
29 namespace { 32 namespace {
30 33
31 class MockImageFetcher : public image_fetcher::ImageFetcher { 34 class MockImageFetcher : public image_fetcher::ImageFetcher {
32 public: 35 public:
33 MOCK_METHOD1(SetImageFetcherDelegate, 36 MOCK_METHOD1(SetImageFetcherDelegate,
34 void(image_fetcher::ImageFetcherDelegate* delegate)); 37 void(image_fetcher::ImageFetcherDelegate* delegate));
35 MOCK_METHOD1(SetDataUseServiceName, 38 MOCK_METHOD1(SetDataUseServiceName,
36 void(image_fetcher::ImageFetcher::DataUseServiceName name)); 39 void(image_fetcher::ImageFetcher::DataUseServiceName name));
37 MOCK_METHOD3(StartOrQueueNetworkRequest, 40 MOCK_METHOD3(StartOrQueueNetworkRequest,
38 void(const std::string& id, 41 void(const std::string& id,
39 const GURL& image_url, 42 const GURL& image_url,
40 base::Callback<void(const std::string& id, 43 base::Callback<void(const std::string& id,
41 const gfx::Image& image)> callback)); 44 const gfx::Image& image)> callback));
42 }; 45 };
43 46
47 // This class provides methods to inject an image resource where a real resource
48 // would be necessary otherwise. All other methods have return values that allow
49 // the normal implementation to proceed.
50 class FakeResourceDelegate : public ui::ResourceBundle::Delegate {
51 public:
52 ~FakeResourceDelegate() override {}
53 base::FilePath GetPathForResourcePack(const base::FilePath& pack_path,
54 ui::ScaleFactor scale_factor) override {
55 return pack_path; // Continue Loading pack file.
56 }
57
58 base::FilePath GetPathForLocalePack(const base::FilePath& pack_path,
59 const std::string& locale) override {
60 return pack_path; // Continue Loading pack file.
61 }
62
63 // Returns a non-empty image for any resource and logs the resource id.
64 gfx::Image GetImageNamed(int resource_id) override {
65 requested_resources.insert(resource_id);
66 return gfx::test::CreateImage(64, 64);
67 }
68
69 gfx::Image GetNativeImageNamed(int resource_id) override {
70 return gfx::Image(); // Attempt retrieval of the default resource.
71 }
72
73 base::RefCountedMemory* LoadDataResourceBytes(
74 int resource_id,
75 ui::ScaleFactor scale_factor) override {
76 return nullptr; // Attempt retrieval of the default resource.
77 }
78
79 bool GetRawDataResource(int resource_id,
80 ui::ScaleFactor scale_factor,
81 base::StringPiece* value) override {
82 return false; // Attempt retrieval of the default resource.
83 }
84
85 bool GetLocalizedString(int message_id, base::string16* value) override {
86 return false; // Attempt retrieval of the default resource.
87 }
88
89 // Returns whether a resource was requested at least once.
90 bool HasProvidedResource(int resource_id) {
91 return requested_resources.find(resource_id) != requested_resources.end();
92 }
93
94 private:
95 std::set<int> requested_resources;
96 };
97
44 class IconCacherTest : public ::testing::Test { 98 class IconCacherTest : public ::testing::Test {
45 protected: 99 protected:
46 IconCacherTest() 100 IconCacherTest()
47 : site_(base::string16(), // title, unused 101 : site_(base::string16(), // title, unused
48 GURL("http://url.google/"), 102 GURL("http://url.google/"),
49 GURL("http://url.google/icon.png"), 103 GURL("http://url.google/icon.png"),
50 GURL("http://url.google/favicon.ico"), 104 GURL("http://url.google/favicon.ico"),
51 GURL()), // thumbnail, unused 105 GURL()), // thumbnail, unused
52 image_fetcher_(new ::testing::StrictMock<MockImageFetcher>), 106 image_fetcher_(new ::testing::StrictMock<MockImageFetcher>),
53 favicon_service_(/*favicon_client=*/nullptr, &history_service_) { 107 favicon_service_(/*favicon_client=*/nullptr, &history_service_) {
54 CHECK(history_dir_.CreateUniqueTempDir()); 108 CHECK(history_dir_.CreateUniqueTempDir());
55 CHECK(history_service_.Init( 109 CHECK(history_service_.Init(
56 history::HistoryDatabaseParams(history_dir_.GetPath(), 0, 0))); 110 history::HistoryDatabaseParams(history_dir_.GetPath(), 0, 0)));
111 if (ui::ResourceBundle::HasSharedInstance()) {
112 ui::ResourceBundle::CleanupSharedInstance();
113 }
57 } 114 }
58 115
59 void PreloadIcon(const GURL& url, 116 void PreloadIcon(const GURL& url,
60 const GURL& icon_url, 117 const GURL& icon_url,
61 favicon_base::IconType icon_type, 118 favicon_base::IconType icon_type,
62 int width, 119 int width,
63 int height) { 120 int height) {
64 favicon_service_.SetFavicons(url, icon_url, icon_type, 121 favicon_service_.SetFavicons(url, icon_url, icon_type,
65 gfx::test::CreateImage(width, height)); 122 gfx::test::CreateImage(width, height));
66 } 123 }
67 124
68 bool IconIsCachedFor(const GURL& url, favicon_base::IconType icon_type) { 125 bool IconIsCachedFor(const GURL& url, favicon_base::IconType icon_type) {
126 return !GetCachedIconFor(url, icon_type).IsEmpty();
127 }
128
129 gfx::Image GetCachedIconFor(const GURL& url,
130 favicon_base::IconType icon_type) {
69 base::CancelableTaskTracker tracker; 131 base::CancelableTaskTracker tracker;
70 bool is_cached; 132 gfx::Image image;
71 base::RunLoop loop; 133 base::RunLoop loop;
72 favicon::GetFaviconImageForPageURL( 134 favicon::GetFaviconImageForPageURL(
73 &favicon_service_, url, icon_type, 135 &favicon_service_, url, icon_type,
74 base::Bind( 136 base::Bind(
75 [](bool* is_cached, base::RunLoop* loop, 137 [](gfx::Image* image, base::RunLoop* loop,
76 const favicon_base::FaviconImageResult& result) { 138 const favicon_base::FaviconImageResult& result) {
77 *is_cached = !result.image.IsEmpty(); 139 *image = result.image;
78 loop->Quit(); 140 loop->Quit();
79 }, 141 },
80 &is_cached, &loop), 142 &image, &loop),
81 &tracker); 143 &tracker);
82 loop.Run(); 144 loop.Run();
83 return is_cached; 145 return image;
84 } 146 }
85 147
86 base::MessageLoop message_loop_; 148 base::MessageLoop message_loop_;
87 PopularSites::Site site_; 149 PopularSites::Site site_;
88 std::unique_ptr<MockImageFetcher> image_fetcher_; 150 std::unique_ptr<MockImageFetcher> image_fetcher_;
89 base::ScopedTempDir history_dir_; 151 base::ScopedTempDir history_dir_;
90 history::HistoryService history_service_; 152 history::HistoryService history_service_;
91 favicon::FaviconService favicon_service_; 153 favicon::FaviconService favicon_service_;
92 }; 154 };
93 155
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop)); 250 EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop));
189 } 251 }
190 252
191 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); 253 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
192 cacher.StartFetch(site_, BindMockFunction(&done)); 254 cacher.StartFetch(site_, BindMockFunction(&done));
193 loop.Run(); 255 loop.Run();
194 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 256 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
195 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 257 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
196 } 258 }
197 259
260 TEST_F(IconCacherTest, ProvidesDefaultIconAndSucceedsWithFetching) {
261 FakeResourceDelegate fake_resource;
262 ui::ResourceBundle::InitSharedInstanceWithLocale(
263 "en-US", &fake_resource, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
264 MockFunction<void(bool)> icon_available;
265 base::RunLoop loop_for_static;
266 base::RunLoop loop_for_fetch;
267 {
268 InSequence s;
269 EXPECT_CALL(*image_fetcher_,
270 SetDataUseServiceName(
271 data_use_measurement::DataUseUserData::NTP_TILES));
272 EXPECT_CALL(icon_available, Call(true)).WillOnce(Quit(&loop_for_static));
273 EXPECT_CALL(*image_fetcher_,
274 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
275 .WillOnce(PassFetch(128, 128));
276 EXPECT_CALL(icon_available, Call(true)).WillOnce(Quit(&loop_for_fetch));
277 }
278
279 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
280 site_.default_resource_id = 12345;
281 cacher.StartFetch(site_, BindMockFunction(&icon_available));
282
283 loop_for_static.Run(); // Wait for the default image.
284 EXPECT_TRUE(fake_resource.HasProvidedResource(12345));
285 EXPECT_THAT(GetCachedIconFor(site_.url, favicon_base::TOUCH_ICON).Size(),
286 Eq(gfx::Size(64, 64))); // Compares dimensions, not objects.
287
288 // Let the fetcher continue and wait for the second call of the callback.
289 loop_for_fetch.Run(); // Wait for the updated image.
290 EXPECT_THAT(GetCachedIconFor(site_.url, favicon_base::TOUCH_ICON).Size(),
291 Eq(gfx::Size(128, 128))); // Compares dimensions, not objects.
292 }
293
198 } // namespace 294 } // namespace
199 } // namespace ntp_tiles 295 } // namespace ntp_tiles
OLDNEW
« 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