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

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

Issue 2388783004: Ensure PopularSite icon availability in ntp_tiles. (Closed)
Patch Set: Merge branch 'master' into icons Created 4 years, 1 month 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.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
(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/favicon/core/favicon_service.h"
13 #include "components/favicon/core/favicon_util.h"
14 #include "components/history/core/browser/history_database_params.h"
15 #include "components/history/core/browser/history_service.h"
16 #include "components/image_fetcher/image_fetcher.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/gfx/image/image_unittest_util.h"
20
21 using ::testing::_;
22 using ::testing::InSequence;
23 using ::testing::MockFunction;
24 using ::testing::Return;
25
26 namespace ntp_tiles {
27 namespace {
28
29 class MockImageFetcher : public image_fetcher::ImageFetcher {
30 public:
31 MOCK_METHOD1(SetImageFetcherDelegate,
32 void(image_fetcher::ImageFetcherDelegate* delegate));
33 MOCK_METHOD1(SetDataUseServiceName,
34 void(image_fetcher::ImageFetcher::DataUseServiceName name));
35 MOCK_METHOD3(StartOrQueueNetworkRequest,
36 void(const std::string& id,
37 const GURL& image_url,
38 base::Callback<void(const std::string& id,
39 const gfx::Image& image)> callback));
40 };
41
42 class IconCacherTest : public ::testing::Test {
43 protected:
44 IconCacherTest()
45 : site_(base::string16(), // title, unused
46 GURL("http://url.google/"),
47 GURL("http://url.google/icon.png"),
48 GURL("http://url.google/favicon.ico"),
49 GURL()), // thumbnail, unused
50 image_fetcher_(new ::testing::StrictMock<MockImageFetcher>),
51 favicon_service_(/*favicon_client=*/nullptr, &history_service_) {
52 CHECK(history_dir_.CreateUniqueTempDir());
53 CHECK(history_service_.Init(
54 history::HistoryDatabaseParams(history_dir_.GetPath(), 0, 0)));
55 }
56
57 void PreloadIcon(const GURL& url,
58 const GURL& icon_url,
59 favicon_base::IconType icon_type,
60 int width,
61 int height) {
62 favicon_service_.SetFavicons(url, icon_url, icon_type,
63 gfx::test::CreateImage(width, height));
64 }
65
66 bool IconIsCachedFor(const GURL& url, favicon_base::IconType icon_type) {
67 base::CancelableTaskTracker tracker;
68 bool is_cached;
69 base::RunLoop loop;
70 favicon::GetFaviconImageForPageURL(
71 &favicon_service_, url, icon_type,
72 base::Bind(
73 [](bool* is_cached, base::RunLoop* loop,
74 const favicon_base::FaviconImageResult& result) {
75 *is_cached = !result.image.IsEmpty();
76 loop->Quit();
77 },
78 &is_cached, &loop),
79 &tracker);
80 loop.Run();
81 return is_cached;
82 }
83
84 base::MessageLoop message_loop_;
85 PopularSites::Site site_;
86 std::unique_ptr<MockImageFetcher> image_fetcher_;
87 base::ScopedTempDir history_dir_;
88 history::HistoryService history_service_;
89 favicon::FaviconService favicon_service_;
90 };
91
92 template <typename Fn>
93 base::Callback<Fn> BindMockFunction(MockFunction<Fn>* function) {
94 return base::Bind(&MockFunction<Fn>::Call, base::Unretained(function));
95 }
96
97 ACTION(FailFetch) {
98 base::ThreadTaskRunnerHandle::Get()->PostTask(
99 FROM_HERE, base::Bind(arg2, arg0, gfx::Image()));
100 }
101
102 ACTION_P2(PassFetch, width, height) {
103 base::ThreadTaskRunnerHandle::Get()->PostTask(
104 FROM_HERE, base::Bind(arg2, arg0, gfx::test::CreateImage(width, height)));
105 }
106
107 ACTION_P(Quit, run_loop) {
108 run_loop->Quit();
109 }
110
111 TEST_F(IconCacherTest, LargeCached) {
112 MockFunction<void(bool)> done;
113 base::RunLoop loop;
114 {
115 InSequence s;
116 EXPECT_CALL(*image_fetcher_,
117 SetDataUseServiceName(
118 data_use_measurement::DataUseUserData::NTP_TILES));
119 EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop));
120 }
121 PreloadIcon(site_.url, site_.large_icon_url, favicon_base::TOUCH_ICON, 128,
122 128);
123
124 IconCacher cacher(&favicon_service_, std::move(image_fetcher_));
125 cacher.StartFetch(site_, BindMockFunction(&done));
126 loop.Run();
127 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
128 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
129 }
130
131 TEST_F(IconCacherTest, LargeNotCachedAndFetchSucceeded) {
132 MockFunction<void(bool)> done;
133 base::RunLoop loop;
134 {
135 InSequence s;
136 EXPECT_CALL(*image_fetcher_,
137 SetDataUseServiceName(
138 data_use_measurement::DataUseUserData::NTP_TILES));
139 EXPECT_CALL(*image_fetcher_,
140 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
141 .WillOnce(PassFetch(128, 128));
142 EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop));
143 }
144
145 IconCacher cacher(&favicon_service_, std::move(image_fetcher_));
146 cacher.StartFetch(site_, BindMockFunction(&done));
147 loop.Run();
148 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
149 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
150 }
151
152 TEST_F(IconCacherTest, SmallNotCachedAndFetchSucceeded) {
153 site_.large_icon_url = GURL();
154
155 MockFunction<void(bool)> done;
156 base::RunLoop loop;
157 {
158 InSequence s;
159 EXPECT_CALL(*image_fetcher_,
160 SetDataUseServiceName(
161 data_use_measurement::DataUseUserData::NTP_TILES));
162 EXPECT_CALL(*image_fetcher_,
163 StartOrQueueNetworkRequest(_, site_.favicon_url, _))
164 .WillOnce(PassFetch(128, 128));
165 EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop));
166 }
167
168 IconCacher cacher(&favicon_service_, std::move(image_fetcher_));
169 cacher.StartFetch(site_, BindMockFunction(&done));
170 loop.Run();
171 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
172 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
173 }
174
175 TEST_F(IconCacherTest, LargeNotCachedAndFetchFailed) {
176 MockFunction<void(bool)> done;
177 base::RunLoop loop;
178 {
179 InSequence s;
180 EXPECT_CALL(*image_fetcher_,
181 SetDataUseServiceName(
182 data_use_measurement::DataUseUserData::NTP_TILES));
183 EXPECT_CALL(*image_fetcher_,
184 StartOrQueueNetworkRequest(_, site_.large_icon_url, _))
185 .WillOnce(FailFetch());
186 EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop));
187 }
188
189 IconCacher cacher(&favicon_service_, std::move(image_fetcher_));
190 cacher.StartFetch(site_, BindMockFunction(&done));
191 loop.Run();
192 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON));
193 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON));
194 }
195
196 } // namespace
197 } // namespace ntp_tiles
OLDNEW
« no previous file with comments | « components/ntp_tiles/icon_cacher.cc ('k') | components/ntp_tiles/most_visited_sites.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698