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

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: Drop bool from callback 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 unified diff | Download patch
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"
13 #include "base/test/test_simple_task_runner.h"
12 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
13 #include "components/favicon/core/favicon_client.h" 15 #include "components/favicon/core/favicon_client.h"
14 #include "components/favicon/core/favicon_service_impl.h" 16 #include "components/favicon/core/favicon_service_impl.h"
15 #include "components/favicon/core/favicon_util.h" 17 #include "components/favicon/core/favicon_util.h"
16 #include "components/history/core/browser/history_database_params.h" 18 #include "components/history/core/browser/history_database_params.h"
17 #include "components/history/core/browser/history_service.h" 19 #include "components/history/core/browser/history_service.h"
18 #include "components/image_fetcher/image_fetcher.h" 20 #include "components/image_fetcher/image_fetcher.h"
19 #include "testing/gmock/include/gmock/gmock.h" 21 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/gfx/image/image_unittest_util.h" 24 #include "ui/gfx/image/image_unittest_util.h"
22 25
23 using ::testing::_; 26 using ::testing::_;
27 using ::testing::Eq;
24 using ::testing::InSequence; 28 using ::testing::InSequence;
25 using ::testing::MockFunction; 29 using ::testing::MockFunction;
26 using ::testing::Return; 30 using ::testing::Return;
27 31
28 namespace ntp_tiles { 32 namespace ntp_tiles {
29 namespace { 33 namespace {
30 34
31 class MockImageFetcher : public image_fetcher::ImageFetcher { 35 class MockImageFetcher : public image_fetcher::ImageFetcher {
32 public: 36 public:
33 MOCK_METHOD1(SetImageFetcherDelegate, 37 MOCK_METHOD1(SetImageFetcherDelegate,
34 void(image_fetcher::ImageFetcherDelegate* delegate)); 38 void(image_fetcher::ImageFetcherDelegate* delegate));
35 MOCK_METHOD1(SetDataUseServiceName, 39 MOCK_METHOD1(SetDataUseServiceName,
36 void(image_fetcher::ImageFetcher::DataUseServiceName name)); 40 void(image_fetcher::ImageFetcher::DataUseServiceName name));
37 MOCK_METHOD3(StartOrQueueNetworkRequest, 41 MOCK_METHOD3(StartOrQueueNetworkRequest,
38 void(const std::string& id, 42 void(const std::string& id,
39 const GURL& image_url, 43 const GURL& image_url,
40 base::Callback<void(const std::string& id, 44 base::Callback<void(const std::string& id,
41 const gfx::Image& image)> callback)); 45 const gfx::Image& image)> callback));
42 }; 46 };
43 47
48 // This class provides methods to inject an image resource where a real resource
49 // would be necessary otherwise. All other methods have return values that allow
50 // the normal implementation to proceed.
51 class FakeResourceDelegate : public ui::ResourceBundle::Delegate {
52 public:
53 ~FakeResourceDelegate() override {}
54 base::FilePath GetPathForResourcePack(const base::FilePath& pack_path,
55 ui::ScaleFactor scale_factor) override {
56 return pack_path; // Continue Loading pack file.
mastiz 2017/03/01 08:48:12 Nit: lowercase "loading". Similar occurrences foll
fhorschig 2017/03/01 20:57:35 Done.
57 }
58
59 base::FilePath GetPathForLocalePack(const base::FilePath& pack_path,
60 const std::string& locale) override {
61 return pack_path; // Continue Loading pack file.
62 }
63
64 // Returns a non-empty image for any resource and logs the resource id.
65 gfx::Image GetImageNamed(int resource_id) override {
66 requested_resources.insert(resource_id);
67 return gfx::test::CreateImage(64, 64);
68 }
69
70 gfx::Image GetNativeImageNamed(int resource_id) override {
71 return gfx::Image(); // Attempt retrieval of the default resource.
72 }
73
74 base::RefCountedMemory* LoadDataResourceBytes(
75 int resource_id,
76 ui::ScaleFactor scale_factor) override {
77 return nullptr; // Attempt retrieval of the default resource.
78 }
79
80 bool GetRawDataResource(int resource_id,
81 ui::ScaleFactor scale_factor,
82 base::StringPiece* value) override {
83 return false; // Attempt retrieval of the default resource.
84 }
85
86 bool GetLocalizedString(int message_id, base::string16* value) override {
87 return false; // Attempt retrieval of the default resource.
88 }
89
90 // Returns whether a resource was requested at least once.
91 bool HasProvidedResource(int resource_id) {
mastiz 2017/03/01 08:48:12 Have you considered using a mock instead? This w
fhorschig 2017/03/01 20:57:35 I didn't. But now I did. I tried a complete Mock a
mastiz 2017/03/02 11:05:39 I can help you with that debugging, it could save
fhorschig 2017/03/02 11:56:24 Thanks, it did.
92 return requested_resources.find(resource_id) != requested_resources.end();
93 }
94
95 private:
96 std::set<int> requested_resources;
97 };
98
44 class IconCacherTest : public ::testing::Test { 99 class IconCacherTest : public ::testing::Test {
45 protected: 100 protected:
46 IconCacherTest() 101 IconCacherTest()
47 : site_(base::string16(), // title, unused 102 : site_(base::string16(), // title, unused
48 GURL("http://url.google/"), 103 GURL("http://url.google/"),
49 GURL("http://url.google/icon.png"), 104 GURL("http://url.google/icon.png"),
50 GURL("http://url.google/favicon.ico"), 105 GURL("http://url.google/favicon.ico"),
51 GURL()), // thumbnail, unused 106 GURL()), // thumbnail, unused
52 image_fetcher_(new ::testing::StrictMock<MockImageFetcher>), 107 image_fetcher_(new ::testing::StrictMock<MockImageFetcher>),
53 favicon_service_(/*favicon_client=*/nullptr, &history_service_) { 108 favicon_service_(/*favicon_client=*/nullptr, &history_service_),
109 task_runner_(new base::TestSimpleTaskRunner()) {
54 CHECK(history_dir_.CreateUniqueTempDir()); 110 CHECK(history_dir_.CreateUniqueTempDir());
55 CHECK(history_service_.Init( 111 CHECK(history_service_.Init(
56 history::HistoryDatabaseParams(history_dir_.GetPath(), 0, 0))); 112 history::HistoryDatabaseParams(history_dir_.GetPath(), 0, 0)));
113 if (ui::ResourceBundle::HasSharedInstance()) {
114 ui::ResourceBundle::CleanupSharedInstance();
115 }
57 } 116 }
58 117
59 void PreloadIcon(const GURL& url, 118 void PreloadIcon(const GURL& url,
60 const GURL& icon_url, 119 const GURL& icon_url,
61 favicon_base::IconType icon_type, 120 favicon_base::IconType icon_type,
62 int width, 121 int width,
63 int height) { 122 int height) {
64 favicon_service_.SetFavicons(url, icon_url, icon_type, 123 favicon_service_.SetFavicons(url, icon_url, icon_type,
65 gfx::test::CreateImage(width, height)); 124 gfx::test::CreateImage(width, height));
66 } 125 }
67 126
68 bool IconIsCachedFor(const GURL& url, favicon_base::IconType icon_type) { 127 bool IconIsCachedFor(const GURL& url, favicon_base::IconType icon_type) {
128 return !GetCachedIconFor(url, icon_type).IsEmpty();
129 }
130
131 gfx::Image GetCachedIconFor(const GURL& url,
132 favicon_base::IconType icon_type) {
69 base::CancelableTaskTracker tracker; 133 base::CancelableTaskTracker tracker;
70 bool is_cached; 134 gfx::Image image;
71 base::RunLoop loop; 135 base::RunLoop loop;
72 favicon::GetFaviconImageForPageURL( 136 favicon::GetFaviconImageForPageURL(
73 &favicon_service_, url, icon_type, 137 &favicon_service_, url, icon_type,
74 base::Bind( 138 base::Bind(
75 [](bool* is_cached, base::RunLoop* loop, 139 [](gfx::Image* image, base::RunLoop* loop,
76 const favicon_base::FaviconImageResult& result) { 140 const favicon_base::FaviconImageResult& result) {
77 *is_cached = !result.image.IsEmpty(); 141 *image = result.image;
78 loop->Quit(); 142 loop->Quit();
79 }, 143 },
80 &is_cached, &loop), 144 &image, &loop),
81 &tracker); 145 &tracker);
82 loop.Run(); 146 loop.Run();
83 return is_cached; 147 return image;
84 } 148 }
85 149
150 void WaitForTasksToFinish() { task_runner_->RunUntilIdle(); }
151
86 base::MessageLoop message_loop_; 152 base::MessageLoop message_loop_;
87 PopularSites::Site site_; 153 PopularSites::Site site_;
88 std::unique_ptr<MockImageFetcher> image_fetcher_; 154 std::unique_ptr<MockImageFetcher> image_fetcher_;
89 base::ScopedTempDir history_dir_; 155 base::ScopedTempDir history_dir_;
90 history::HistoryService history_service_; 156 history::HistoryService history_service_;
91 favicon::FaviconServiceImpl favicon_service_; 157 favicon::FaviconServiceImpl favicon_service_;
158 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
92 }; 159 };
93 160
94 template <typename Fn> 161 template <typename Fn>
95 base::Callback<Fn> BindMockFunction(MockFunction<Fn>* function) { 162 base::Callback<Fn> BindMockFunction(MockFunction<Fn>* function) {
mastiz 2017/03/02 11:05:39 Optional: there's MockCallback which is probably w
fhorschig 2017/03/02 11:56:24 Done.
96 return base::Bind(&MockFunction<Fn>::Call, base::Unretained(function)); 163 return base::Bind(&MockFunction<Fn>::Call, base::Unretained(function));
97 } 164 }
98 165
99 ACTION(FailFetch) { 166 ACTION(FailFetch) {
100 base::ThreadTaskRunnerHandle::Get()->PostTask( 167 base::ThreadTaskRunnerHandle::Get()->PostTask(
101 FROM_HERE, base::Bind(arg2, arg0, gfx::Image())); 168 FROM_HERE, base::Bind(arg2, arg0, gfx::Image()));
102 } 169 }
103 170
104 ACTION_P2(PassFetch, width, height) { 171 ACTION_P2(PassFetch, width, height) {
105 base::ThreadTaskRunnerHandle::Get()->PostTask( 172 base::ThreadTaskRunnerHandle::Get()->PostTask(
106 FROM_HERE, base::Bind(arg2, arg0, gfx::test::CreateImage(width, height))); 173 FROM_HERE, base::Bind(arg2, arg0, gfx::test::CreateImage(width, height)));
107 } 174 }
108 175
109 ACTION_P(Quit, run_loop) { 176 ACTION_P(Quit, run_loop) {
110 run_loop->Quit(); 177 run_loop->Quit();
111 } 178 }
112 179
113 TEST_F(IconCacherTest, LargeCached) { 180 TEST_F(IconCacherTest, LargeCached) {
114 MockFunction<void(bool)> done; 181 MockFunction<void()> done;
182 EXPECT_CALL(done, Call()).Times(0);
115 base::RunLoop loop; 183 base::RunLoop loop;
116 { 184 {
117 InSequence s; 185 InSequence s;
118 EXPECT_CALL(*image_fetcher_, 186 EXPECT_CALL(*image_fetcher_,
119 SetDataUseServiceName( 187 SetDataUseServiceName(
120 data_use_measurement::DataUseUserData::NTP_TILES)); 188 data_use_measurement::DataUseUserData::NTP_TILES));
121 EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop));
122 } 189 }
123 PreloadIcon(site_.url, site_.large_icon_url, favicon_base::TOUCH_ICON, 128, 190 PreloadIcon(site_.url, site_.large_icon_url, favicon_base::TOUCH_ICON, 128,
124 128); 191 128);
125
126 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); 192 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
127 cacher.StartFetch(site_, BindMockFunction(&done)); 193 cacher.StartFetch(site_, BindMockFunction(&done));
128 loop.Run(); 194 WaitForTasksToFinish();
129 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 195 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
130 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 196 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
131 } 197 }
132 198
133 TEST_F(IconCacherTest, LargeNotCachedAndFetchSucceeded) { 199 TEST_F(IconCacherTest, LargeNotCachedAndFetchSucceeded) {
134 MockFunction<void(bool)> done; 200 MockFunction<void()> done;
135 base::RunLoop loop; 201 base::RunLoop loop;
136 { 202 {
137 InSequence s; 203 InSequence s;
138 EXPECT_CALL(*image_fetcher_, 204 EXPECT_CALL(*image_fetcher_,
139 SetDataUseServiceName( 205 SetDataUseServiceName(
140 data_use_measurement::DataUseUserData::NTP_TILES)); 206 data_use_measurement::DataUseUserData::NTP_TILES));
141 EXPECT_CALL(*image_fetcher_, 207 EXPECT_CALL(*image_fetcher_,
142 StartOrQueueNetworkRequest(_, site_.large_icon_url, _)) 208 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
143 .WillOnce(PassFetch(128, 128)); 209 .WillOnce(PassFetch(128, 128));
144 EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop)); 210 EXPECT_CALL(done, Call()).WillOnce(Quit(&loop));
145 } 211 }
146 212
147 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); 213 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
148 cacher.StartFetch(site_, BindMockFunction(&done)); 214 cacher.StartFetch(site_, BindMockFunction(&done));
149 loop.Run(); 215 loop.Run();
150 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 216 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
151 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 217 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
152 } 218 }
153 219
154 TEST_F(IconCacherTest, SmallNotCachedAndFetchSucceeded) { 220 TEST_F(IconCacherTest, SmallNotCachedAndFetchSucceeded) {
155 site_.large_icon_url = GURL(); 221 site_.large_icon_url = GURL();
156 222
157 MockFunction<void(bool)> done; 223 MockFunction<void()> done;
158 base::RunLoop loop; 224 base::RunLoop loop;
159 { 225 {
160 InSequence s; 226 InSequence s;
161 EXPECT_CALL(*image_fetcher_, 227 EXPECT_CALL(*image_fetcher_,
162 SetDataUseServiceName( 228 SetDataUseServiceName(
163 data_use_measurement::DataUseUserData::NTP_TILES)); 229 data_use_measurement::DataUseUserData::NTP_TILES));
164 EXPECT_CALL(*image_fetcher_, 230 EXPECT_CALL(*image_fetcher_,
165 StartOrQueueNetworkRequest(_, site_.favicon_url, _)) 231 StartOrQueueNetworkRequest(_, site_.favicon_url, _))
166 .WillOnce(PassFetch(128, 128)); 232 .WillOnce(PassFetch(128, 128));
167 EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop)); 233 EXPECT_CALL(done, Call()).WillOnce(Quit(&loop));
168 } 234 }
169 235
170 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); 236 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
171 cacher.StartFetch(site_, BindMockFunction(&done)); 237 cacher.StartFetch(site_, BindMockFunction(&done));
172 loop.Run(); 238 loop.Run();
173 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 239 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
174 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 240 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
175 } 241 }
176 242
177 TEST_F(IconCacherTest, LargeNotCachedAndFetchFailed) { 243 TEST_F(IconCacherTest, LargeNotCachedAndFetchFailed) {
178 MockFunction<void(bool)> done; 244 MockFunction<void()> done;
245 EXPECT_CALL(done, Call()).Times(0);
179 base::RunLoop loop; 246 base::RunLoop loop;
180 { 247 {
181 InSequence s; 248 InSequence s;
182 EXPECT_CALL(*image_fetcher_, 249 EXPECT_CALL(*image_fetcher_,
183 SetDataUseServiceName( 250 SetDataUseServiceName(
184 data_use_measurement::DataUseUserData::NTP_TILES)); 251 data_use_measurement::DataUseUserData::NTP_TILES));
185 EXPECT_CALL(*image_fetcher_, 252 EXPECT_CALL(*image_fetcher_,
186 StartOrQueueNetworkRequest(_, site_.large_icon_url, _)) 253 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
187 .WillOnce(FailFetch()); 254 .WillOnce(FailFetch());
188 EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop));
189 } 255 }
190 256
191 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); 257 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
192 cacher.StartFetch(site_, BindMockFunction(&done)); 258 cacher.StartFetch(site_, BindMockFunction(&done));
193 loop.Run(); 259 WaitForTasksToFinish();
194 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 260 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
195 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 261 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
196 } 262 }
197 263
264 TEST_F(IconCacherTest, ProvidesDefaultIconAndSucceedsWithFetching) {
265 FakeResourceDelegate fake_resource;
266 ui::ResourceBundle::InitSharedInstanceWithLocale(
267 "en-US", &fake_resource, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
268 MockFunction<void()> icon_available;
269 base::RunLoop loop_for_static;
270 base::RunLoop loop_for_fetch;
271 {
272 InSequence s;
273 EXPECT_CALL(*image_fetcher_,
274 SetDataUseServiceName(
275 data_use_measurement::DataUseUserData::NTP_TILES));
276 EXPECT_CALL(icon_available, Call()).WillOnce(Quit(&loop_for_static));
277 EXPECT_CALL(*image_fetcher_,
278 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
279 .WillOnce(PassFetch(128, 128));
280 EXPECT_CALL(icon_available, Call()).WillOnce(Quit(&loop_for_fetch));
281 }
282
283 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
284 site_.default_resource_id = 12345;
285 cacher.StartFetch(site_, BindMockFunction(&icon_available));
286
287 loop_for_static.Run(); // Wait for the default image.
288 EXPECT_TRUE(fake_resource.HasProvidedResource(12345));
289 EXPECT_THAT(GetCachedIconFor(site_.url, favicon_base::TOUCH_ICON).Size(),
290 Eq(gfx::Size(64, 64))); // Compares dimensions, not objects.
291
292 // Let the fetcher continue and wait for the second call of the callback.
293 loop_for_fetch.Run(); // Wait for the updated image.
294 EXPECT_THAT(GetCachedIconFor(site_.url, favicon_base::TOUCH_ICON).Size(),
295 Eq(gfx::Size(128, 128))); // Compares dimensions, not objects.
296 }
297
198 } // namespace 298 } // namespace
199 } // namespace ntp_tiles 299 } // namespace ntp_tiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698