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

Side by Side Diff: components/ntp_tiles/icon_cacher_unittest.cc

Issue 2388783004: Ensure PopularSite icon availability in ntp_tiles. (Closed)
Patch Set: Fix comments, variable names. Created 4 years, 2 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/ntp_tiles/icon_cacher.h"
6
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/run_loop.h"
10 #include "base/threading/thread_task_runner_handle.h"
11 #include "components/favicon/core/favicon_client.h"
12 #include "components/history/core/browser/history_database_params.h"
13 #include "components/history/core/browser/history_service.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/gfx/image/image_unittest_util.h"
17
18 using ::testing::_;
19 using ::testing::InSequence;
20 using ::testing::MockFunction;
21 using ::testing::Return;
22
23 namespace ntp_tiles {
24 namespace {
25
26 class MockImageFetcher : public image_fetcher::ImageFetcher {
27 public:
28 MOCK_METHOD1(SetImageFetcherDelegate,
29 void(image_fetcher::ImageFetcherDelegate* delegate));
30 MOCK_METHOD1(SetDataUseServiceName,
31 void(image_fetcher::ImageFetcher::DataUseServiceName name));
32 MOCK_METHOD3(StartOrQueueNetworkRequest,
33 void(const std::string& id,
34 const GURL& image_url,
35 base::Callback<void(const std::string& id,
36 const gfx::Image& image)> callback));
37 };
38
39 class IconCacherTest : public ::testing::Test {
40 protected:
41 IconCacherTest()
42 : site_(base::string16(), // title, unused
43 GURL("http://url.google/"),
44 GURL("http://url.google/icon.png"),
45 GURL("http://url.google/favicon.ico"),
46 GURL()), // thumbnail, unused
47 image_fetcher_(new ::testing::StrictMock<MockImageFetcher>),
48 favicon_service_(nullptr, &history_service_) {
Marc Treib 2016/10/12 09:11:28 /*favicon_client=*/nullptr
sfiera 2016/10/13 09:06:46 Done.
49 CHECK(history_dir_.CreateUniqueTempDir());
Marc Treib 2016/10/12 09:11:28 Can this be ASSERT_TRUE rather than CHECK, so the
sfiera 2016/10/13 09:06:46 Not easily; ASSERT_TRUE() doesn't work well outsid
Marc Treib 2016/10/13 12:31:16 Alright, fair enough.
Bernhard Bauer 2016/10/13 13:20:09 I'm not 100% sure -- ASSERT not only returns from
sfiera 2016/10/14 06:27:32 Tried it. Apparently ASSERT_* does return somethin
50 CHECK(history_service_.Init(
51 history::HistoryDatabaseParams(history_dir_.GetPath(), 0, 0)));
Marc Treib 2016/10/12 09:11:28 misaligned?
sfiera 2016/10/13 09:06:46 Indentation, you mean? I've run git cl format, so
Marc Treib 2016/10/13 12:31:16 Yes, I meant indentation, which looks a bit weird
Bernhard Bauer 2016/10/13 13:20:09 Just FTR, that is not actually how it works: clang
sfiera 2016/10/14 06:27:32 No, that is how it works: https://chromium.googles
52 }
53
54 void PreloadIcon(const GURL& url,
55 const GURL& icon_url,
56 favicon_base::IconType icon_type,
57 int width,
58 int height) {
59 favicon_service_.SetFavicons(url, icon_url, icon_type,
60 gfx::test::CreateImage(width, height));
61 }
62
63 bool IconIsCachedFor(const GURL& url, favicon_base::IconType icon_type) {
64 base::CancelableTaskTracker tracker;
65 bool is_cached;
66 base::RunLoop loop;
67 favicon::GetFaviconImageForPageURL(
68 &favicon_service_, url, icon_type,
69 base::Bind(
70 [](bool* is_cached, base::RunLoop* loop,
71 const favicon_base::FaviconImageResult& result) {
72 *is_cached = !result.image.IsEmpty();
73 loop->Quit();
74 },
75 &is_cached, &loop),
76 &tracker);
77 loop.Run();
78 return is_cached;
79 }
80
81 base::MessageLoop message_loop_;
82 PopularSites::Site site_;
83 MockImageFetcher* const image_fetcher_;
84 base::ScopedTempDir history_dir_;
85 history::HistoryService history_service_;
86 favicon::FaviconService favicon_service_;
87 };
88
89 template <typename Fn>
90 base::Callback<Fn> BindMockFunction(MockFunction<Fn>* function) {
91 return base::Bind(&MockFunction<Fn>::Call, base::Unretained(function));
92 }
93
94 ACTION(FailFetch) {
95 base::ThreadTaskRunnerHandle::Get()->PostTask(
96 FROM_HERE, base::Bind(arg2, arg0, gfx::Image()));
97 }
98
99 ACTION_P2(PassFetch, width, height) {
100 base::ThreadTaskRunnerHandle::Get()->PostTask(
101 FROM_HERE, base::Bind(arg2, arg0, gfx::test::CreateImage(width, height)));
102 }
103
104 ACTION_P(Quit, run_loop) {
105 run_loop->Quit();
106 }
107
108 TEST_F(IconCacherTest, LargeCached) {
109 MockFunction<void(bool)> done;
110 base::RunLoop loop;
111 {
112 InSequence s;
113 EXPECT_CALL(*image_fetcher_,
114 SetDataUseServiceName(
115 data_use_measurement::DataUseUserData::NTP_TILES));
116 EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop));
117 }
118 PreloadIcon(site_.url, site_.large_icon_url, favicon_base::TOUCH_ICON, 128,
119 128);
120
121 IconCacher cacher(&favicon_service_, base::WrapUnique(image_fetcher_));
Marc Treib 2016/10/12 09:11:28 The WrapUnique is quite unsafe - it needs to happe
sfiera 2016/10/13 09:06:46 Switched to a std::unique_ptr<> in the fixture and
122 cacher.StartFetch(site_, BindMockFunction(&done));
123 loop.Run();
124 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
125 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
126 }
127
128 TEST_F(IconCacherTest, LargeNotCachedAndFetchSucceeded) {
129 MockFunction<void(bool)> done;
130 base::RunLoop loop;
131 {
132 InSequence s;
133 EXPECT_CALL(*image_fetcher_,
134 SetDataUseServiceName(
135 data_use_measurement::DataUseUserData::NTP_TILES));
136 EXPECT_CALL(*image_fetcher_,
137 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
138 .WillOnce(PassFetch(128, 128));
139 EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop));
140 }
141
142 IconCacher cacher(&favicon_service_, base::WrapUnique(image_fetcher_));
143 cacher.StartFetch(site_, BindMockFunction(&done));
144 loop.Run();
145 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
146 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
147 }
148
149 TEST_F(IconCacherTest, SmallNotCachedAndFetchSucceeded) {
150 site_.large_icon_url = GURL();
151
152 MockFunction<void(bool)> done;
153 base::RunLoop loop;
154 {
155 InSequence s;
156 EXPECT_CALL(*image_fetcher_,
157 SetDataUseServiceName(
158 data_use_measurement::DataUseUserData::NTP_TILES));
159 EXPECT_CALL(*image_fetcher_,
160 StartOrQueueNetworkRequest(_, site_.favicon_url, _))
161 .WillOnce(PassFetch(128, 128));
162 EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop));
163 }
164
165 IconCacher cacher(&favicon_service_, base::WrapUnique(image_fetcher_));
166 cacher.StartFetch(site_, BindMockFunction(&done));
167 loop.Run();
168 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
169 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
170 }
171
172 TEST_F(IconCacherTest, LargeNotCachedAndFetchFailed) {
173 MockFunction<void(bool)> done;
174 base::RunLoop loop;
175 {
176 InSequence s;
177 EXPECT_CALL(*image_fetcher_,
178 SetDataUseServiceName(
179 data_use_measurement::DataUseUserData::NTP_TILES));
180 EXPECT_CALL(*image_fetcher_,
181 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
182 .WillOnce(FailFetch());
183 EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop));
184 }
185
186 IconCacher cacher(&favicon_service_, base::WrapUnique(image_fetcher_));
187 cacher.StartFetch(site_, BindMockFunction(&done));
188 loop.Run();
189 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
190 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
191 }
192
193 } // namespace
194 } // namespace ntp_tiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698