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

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: Split test into new CL 2725923003 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
« no previous file with comments | « components/ntp_tiles/icon_cacher_impl.cc ('k') | components/ntp_tiles/most_visited_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"
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.
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) {
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 }
116 ui::ResourceBundle::InitSharedInstanceWithLocale(
117 "en-US", &fake_resource_, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
57 } 118 }
58 119
59 void PreloadIcon(const GURL& url, 120 void PreloadIcon(const GURL& url,
60 const GURL& icon_url, 121 const GURL& icon_url,
61 favicon_base::IconType icon_type, 122 favicon_base::IconType icon_type,
62 int width, 123 int width,
63 int height) { 124 int height) {
64 favicon_service_.SetFavicons(url, icon_url, icon_type, 125 favicon_service_.SetFavicons(url, icon_url, icon_type,
65 gfx::test::CreateImage(width, height)); 126 gfx::test::CreateImage(width, height));
66 } 127 }
67 128
68 bool IconIsCachedFor(const GURL& url, favicon_base::IconType icon_type) { 129 bool IconIsCachedFor(const GURL& url, favicon_base::IconType icon_type) {
130 return !GetCachedIconFor(url, icon_type).IsEmpty();
131 }
132
133 gfx::Image GetCachedIconFor(const GURL& url,
134 favicon_base::IconType icon_type) {
69 base::CancelableTaskTracker tracker; 135 base::CancelableTaskTracker tracker;
70 bool is_cached; 136 gfx::Image image;
71 base::RunLoop loop; 137 base::RunLoop loop;
72 favicon::GetFaviconImageForPageURL( 138 favicon::GetFaviconImageForPageURL(
73 &favicon_service_, url, icon_type, 139 &favicon_service_, url, icon_type,
74 base::Bind( 140 base::Bind(
75 [](bool* is_cached, base::RunLoop* loop, 141 [](gfx::Image* image, base::RunLoop* loop,
76 const favicon_base::FaviconImageResult& result) { 142 const favicon_base::FaviconImageResult& result) {
77 *is_cached = !result.image.IsEmpty(); 143 *image = result.image;
78 loop->Quit(); 144 loop->Quit();
79 }, 145 },
80 &is_cached, &loop), 146 &image, &loop),
81 &tracker); 147 &tracker);
82 loop.Run(); 148 loop.Run();
83 return is_cached; 149 return image;
84 } 150 }
85 151
152 void WaitForTasksToFinish() { task_runner_->RunUntilIdle(); }
153
86 base::MessageLoop message_loop_; 154 base::MessageLoop message_loop_;
87 PopularSites::Site site_; 155 PopularSites::Site site_;
88 std::unique_ptr<MockImageFetcher> image_fetcher_; 156 std::unique_ptr<MockImageFetcher> image_fetcher_;
89 base::ScopedTempDir history_dir_; 157 base::ScopedTempDir history_dir_;
90 history::HistoryService history_service_; 158 history::HistoryService history_service_;
91 favicon::FaviconServiceImpl favicon_service_; 159 favicon::FaviconServiceImpl favicon_service_;
160 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
161 FakeResourceDelegate fake_resource_;
92 }; 162 };
93 163
94 template <typename Fn> 164 template <typename Fn>
95 base::Callback<Fn> BindMockFunction(MockFunction<Fn>* function) { 165 base::Callback<Fn> BindMockFunction(MockFunction<Fn>* function) {
96 return base::Bind(&MockFunction<Fn>::Call, base::Unretained(function)); 166 return base::Bind(&MockFunction<Fn>::Call, base::Unretained(function));
97 } 167 }
98 168
99 ACTION(FailFetch) { 169 ACTION(FailFetch) {
100 base::ThreadTaskRunnerHandle::Get()->PostTask( 170 base::ThreadTaskRunnerHandle::Get()->PostTask(
101 FROM_HERE, base::Bind(arg2, arg0, gfx::Image())); 171 FROM_HERE, base::Bind(arg2, arg0, gfx::Image()));
102 } 172 }
103 173
104 ACTION_P2(PassFetch, width, height) { 174 ACTION_P2(PassFetch, width, height) {
105 base::ThreadTaskRunnerHandle::Get()->PostTask( 175 base::ThreadTaskRunnerHandle::Get()->PostTask(
106 FROM_HERE, base::Bind(arg2, arg0, gfx::test::CreateImage(width, height))); 176 FROM_HERE, base::Bind(arg2, arg0, gfx::test::CreateImage(width, height)));
107 } 177 }
108 178
109 ACTION_P(Quit, run_loop) { 179 ACTION_P(Quit, run_loop) {
110 run_loop->Quit(); 180 run_loop->Quit();
111 } 181 }
112 182
113 TEST_F(IconCacherTest, LargeCached) { 183 TEST_F(IconCacherTest, LargeCached) {
114 MockFunction<void(bool)> done; 184 MockFunction<void()> done;
115 base::RunLoop loop; 185 EXPECT_CALL(done, Call()).Times(0);
116 { 186 EXPECT_CALL(
117 InSequence s; 187 *image_fetcher_,
118 EXPECT_CALL(*image_fetcher_, 188 SetDataUseServiceName(data_use_measurement::DataUseUserData::NTP_TILES));
119 SetDataUseServiceName(
120 data_use_measurement::DataUseUserData::NTP_TILES));
121 EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop));
122 }
123 PreloadIcon(site_.url, site_.large_icon_url, favicon_base::TOUCH_ICON, 128, 189 PreloadIcon(site_.url, site_.large_icon_url, favicon_base::TOUCH_ICON, 128,
124 128); 190 128);
125
126 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); 191 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
127 cacher.StartFetch(site_, BindMockFunction(&done)); 192 cacher.StartFetch(site_, BindMockFunction(&done), BindMockFunction(&done));
128 loop.Run(); 193 WaitForTasksToFinish();
129 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 194 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
130 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 195 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
131 } 196 }
132 197
133 TEST_F(IconCacherTest, LargeNotCachedAndFetchSucceeded) { 198 TEST_F(IconCacherTest, LargeNotCachedAndFetchSucceeded) {
134 MockFunction<void(bool)> done; 199 MockFunction<void()> done;
135 base::RunLoop loop; 200 base::RunLoop loop;
136 { 201 {
137 InSequence s; 202 InSequence s;
138 EXPECT_CALL(*image_fetcher_, 203 EXPECT_CALL(*image_fetcher_,
139 SetDataUseServiceName( 204 SetDataUseServiceName(
140 data_use_measurement::DataUseUserData::NTP_TILES)); 205 data_use_measurement::DataUseUserData::NTP_TILES));
141 EXPECT_CALL(*image_fetcher_, 206 EXPECT_CALL(*image_fetcher_,
142 StartOrQueueNetworkRequest(_, site_.large_icon_url, _)) 207 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
143 .WillOnce(PassFetch(128, 128)); 208 .WillOnce(PassFetch(128, 128));
144 EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop)); 209 EXPECT_CALL(done, Call()).WillOnce(Quit(&loop));
145 } 210 }
146 211
147 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); 212 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
148 cacher.StartFetch(site_, BindMockFunction(&done)); 213 cacher.StartFetch(site_, BindMockFunction(&done), BindMockFunction(&done));
149 loop.Run(); 214 loop.Run();
150 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 215 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
151 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 216 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
152 } 217 }
153 218
154 TEST_F(IconCacherTest, SmallNotCachedAndFetchSucceeded) { 219 TEST_F(IconCacherTest, SmallNotCachedAndFetchSucceeded) {
155 site_.large_icon_url = GURL(); 220 site_.large_icon_url = GURL();
156 221
157 MockFunction<void(bool)> done; 222 MockFunction<void()> done;
158 base::RunLoop loop; 223 base::RunLoop loop;
159 { 224 {
160 InSequence s; 225 InSequence s;
161 EXPECT_CALL(*image_fetcher_, 226 EXPECT_CALL(*image_fetcher_,
162 SetDataUseServiceName( 227 SetDataUseServiceName(
163 data_use_measurement::DataUseUserData::NTP_TILES)); 228 data_use_measurement::DataUseUserData::NTP_TILES));
164 EXPECT_CALL(*image_fetcher_, 229 EXPECT_CALL(*image_fetcher_,
165 StartOrQueueNetworkRequest(_, site_.favicon_url, _)) 230 StartOrQueueNetworkRequest(_, site_.favicon_url, _))
166 .WillOnce(PassFetch(128, 128)); 231 .WillOnce(PassFetch(128, 128));
167 EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop)); 232 EXPECT_CALL(done, Call()).WillOnce(Quit(&loop));
168 } 233 }
169 234
170 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); 235 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
171 cacher.StartFetch(site_, BindMockFunction(&done)); 236 cacher.StartFetch(site_, BindMockFunction(&done), BindMockFunction(&done));
172 loop.Run(); 237 loop.Run();
173 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 238 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
174 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 239 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
175 } 240 }
176 241
177 TEST_F(IconCacherTest, LargeNotCachedAndFetchFailed) { 242 TEST_F(IconCacherTest, LargeNotCachedAndFetchFailed) {
178 MockFunction<void(bool)> done; 243 MockFunction<void()> done;
179 base::RunLoop loop; 244 EXPECT_CALL(done, Call()).Times(0);
180 { 245 {
181 InSequence s; 246 InSequence s;
182 EXPECT_CALL(*image_fetcher_, 247 EXPECT_CALL(*image_fetcher_,
183 SetDataUseServiceName( 248 SetDataUseServiceName(
184 data_use_measurement::DataUseUserData::NTP_TILES)); 249 data_use_measurement::DataUseUserData::NTP_TILES));
185 EXPECT_CALL(*image_fetcher_, 250 EXPECT_CALL(*image_fetcher_,
186 StartOrQueueNetworkRequest(_, site_.large_icon_url, _)) 251 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
187 .WillOnce(FailFetch()); 252 .WillOnce(FailFetch());
188 EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop));
189 } 253 }
190 254
191 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); 255 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
192 cacher.StartFetch(site_, BindMockFunction(&done)); 256 cacher.StartFetch(site_, BindMockFunction(&done), BindMockFunction(&done));
193 loop.Run(); 257 WaitForTasksToFinish();
194 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); 258 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
195 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); 259 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
196 } 260 }
197 261
262 TEST_F(IconCacherTest, ProvidesDefaultIconAndSucceedsWithFetching) {
263 MockFunction<void()> preliminary_icon_available;
264 MockFunction<void()> icon_available;
265 base::RunLoop default_loop;
266 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
267 {
268 InSequence s;
269 EXPECT_CALL(*image_fetcher_,
270 SetDataUseServiceName(
271 data_use_measurement::DataUseUserData::NTP_TILES));
272 EXPECT_CALL(preliminary_icon_available, Call())
273 .WillOnce(Quit(&default_loop));
274 EXPECT_CALL(*image_fetcher_,
275 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
276 .WillOnce(PassFetch(128, 128));
277 EXPECT_CALL(icon_available, Call()).WillOnce(Quit(&fetch_loop));
278 }
279
280 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_));
281 site_.default_icon_resource = 12345;
282 cacher.StartFetch(site_, BindMockFunction(&icon_available),
283 BindMockFunction(&preliminary_icon_available));
284
285 default_loop.Run(); // Wait for the default image.
286 EXPECT_TRUE(fake_resource_.HasProvidedResource(12345));
287 EXPECT_THAT(GetCachedIconFor(site_.url, favicon_base::TOUCH_ICON).Size(),
288 Eq(gfx::Size(64, 64))); // Compares dimensions, not objects.
289
290 // Let the fetcher continue and wait for the second call of the callback.
291 fetch_loop.Run(); // Wait for the updated image.
292 EXPECT_THAT(GetCachedIconFor(site_.url, favicon_base::TOUCH_ICON).Size(),
293 Eq(gfx::Size(128, 128))); // Compares dimensions, not objects.
294 }
295
198 } // namespace 296 } // namespace
199 } // namespace ntp_tiles 297 } // namespace ntp_tiles
OLDNEW
« no previous file with comments | « components/ntp_tiles/icon_cacher_impl.cc ('k') | components/ntp_tiles/most_visited_sites.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698