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

Side by Side Diff: components/favicon/core/large_icon_service_unittest.cc

Issue 2784233003: [LargeIconService] Allow decoding of images in the service (Closed)
Patch Set: Peter's comments #3 Created 3 years, 8 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/favicon/core/large_icon_service.h" 5 #include "components/favicon/core/large_icon_service.h"
6 6
7 #include <deque> 7 #include <deque>
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 11 matching lines...) Expand all
22 #include "components/favicon_base/favicon_types.h" 22 #include "components/favicon_base/favicon_types.h"
23 #include "components/image_fetcher/core/image_fetcher.h" 23 #include "components/image_fetcher/core/image_fetcher.h"
24 #include "components/image_fetcher/core/request_metadata.h" 24 #include "components/image_fetcher/core/request_metadata.h"
25 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "third_party/skia/include/core/SkBitmap.h" 27 #include "third_party/skia/include/core/SkBitmap.h"
28 #include "third_party/skia/include/core/SkColor.h" 28 #include "third_party/skia/include/core/SkColor.h"
29 #include "ui/gfx/codec/png_codec.h" 29 #include "ui/gfx/codec/png_codec.h"
30 #include "ui/gfx/geometry/size.h" 30 #include "ui/gfx/geometry/size.h"
31 #include "ui/gfx/image/image.h" 31 #include "ui/gfx/image/image.h"
32 #include "ui/gfx/image/image_skia.h"
32 #include "url/gurl.h" 33 #include "url/gurl.h"
33 34
34 namespace favicon { 35 namespace favicon {
35 namespace { 36 namespace {
36 37
38 using testing::IsNull;
39 using testing::Eq;
37 using testing::NiceMock; 40 using testing::NiceMock;
38 using testing::Return; 41 using testing::Return;
39 using testing::SaveArg; 42 using testing::SaveArg;
40 using testing::_; 43 using testing::_;
41 44
42 const char kDummyUrl[] = "http://www.example.com"; 45 const char kDummyUrl[] = "http://www.example.com";
43 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png"; 46 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png";
44 const SkColor kTestColor = SK_ColorRED; 47 const SkColor kTestColor = SK_ColorRED;
45 48
46 ACTION_P(PostFetchReply, p0) { 49 ACTION_P(PostFetchReply, p0) {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 const ImageFetcherCallback&)); 99 const ImageFetcherCallback&));
97 MOCK_METHOD0(GetImageDecoder, image_fetcher::ImageDecoder*()); 100 MOCK_METHOD0(GetImageDecoder, image_fetcher::ImageDecoder*());
98 }; 101 };
99 102
100 class LargeIconServiceTest : public testing::Test { 103 class LargeIconServiceTest : public testing::Test {
101 public: 104 public:
102 LargeIconServiceTest() 105 LargeIconServiceTest()
103 : mock_image_fetcher_(new NiceMock<MockImageFetcher>()), 106 : mock_image_fetcher_(new NiceMock<MockImageFetcher>()),
104 large_icon_service_(&mock_favicon_service_, 107 large_icon_service_(&mock_favicon_service_,
105 base::ThreadTaskRunnerHandle::Get(), 108 base::ThreadTaskRunnerHandle::Get(),
106 base::WrapUnique(mock_image_fetcher_)), 109 base::WrapUnique(mock_image_fetcher_)) {}
107 is_callback_invoked_(false) {}
108 110
109 ~LargeIconServiceTest() override { 111 ~LargeIconServiceTest() override {}
110 }
111
112 void ResultCallback(const favicon_base::LargeIconResult& result) {
113 is_callback_invoked_ = true;
114
115 // Checking presence and absence of results.
116 EXPECT_EQ(expected_bitmap_.is_valid(), result.bitmap.is_valid());
117 EXPECT_EQ(expected_fallback_icon_style_ != nullptr,
118 result.fallback_icon_style != nullptr);
119
120 if (expected_bitmap_.is_valid()) {
121 EXPECT_EQ(expected_bitmap_.pixel_size, result.bitmap.pixel_size);
122 // Not actually checking bitmap content.
123 }
124 if (expected_fallback_icon_style_.get()) {
125 EXPECT_EQ(*expected_fallback_icon_style_,
126 *result.fallback_icon_style);
127 }
128 }
129
130 void InjectMockResult(
131 const GURL& page_url,
132 const favicon_base::FaviconRawBitmapResult& mock_result) {
133 EXPECT_CALL(mock_favicon_service_,
134 GetLargestRawFaviconForPageURL(page_url, _, _, _, _))
135 .WillOnce(PostReply<5>(mock_result));
136 }
137 112
138 protected: 113 protected:
139 base::MessageLoopForIO loop_; 114 base::MessageLoopForIO loop_;
140 115
141 NiceMock<MockImageFetcher>* mock_image_fetcher_; 116 NiceMock<MockImageFetcher>* mock_image_fetcher_;
142 testing::NiceMock<MockFaviconService> mock_favicon_service_; 117 testing::NiceMock<MockFaviconService> mock_favicon_service_;
143 LargeIconService large_icon_service_; 118 LargeIconService large_icon_service_;
144 base::CancelableTaskTracker cancelable_task_tracker_;
145
146 favicon_base::FaviconRawBitmapResult expected_bitmap_;
147 std::unique_ptr<favicon_base::FallbackIconStyle>
148 expected_fallback_icon_style_;
149
150 bool is_callback_invoked_;
151 119
152 private: 120 private:
153 DISALLOW_COPY_AND_ASSIGN(LargeIconServiceTest); 121 DISALLOW_COPY_AND_ASSIGN(LargeIconServiceTest);
154 }; 122 };
155 123
156 TEST_F(LargeIconServiceTest, SameSize) {
157 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(24, 24, kTestColor));
158 expected_bitmap_ = CreateTestBitmapResult(24, 24, kTestColor);
159 large_icon_service_.GetLargeIconOrFallbackStyle(
160 GURL(kDummyUrl),
161 24, // |min_source_size_in_pixel|
162 24, // |desired_size_in_pixel|
163 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
164 &cancelable_task_tracker_);
165 base::RunLoop().RunUntilIdle();
166 EXPECT_TRUE(is_callback_invoked_);
167 }
168
169 TEST_F(LargeIconServiceTest, ScaleDown) {
170 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(32, 32, kTestColor));
171 expected_bitmap_ = CreateTestBitmapResult(24, 24, kTestColor);
172 large_icon_service_.GetLargeIconOrFallbackStyle(
173 GURL(kDummyUrl), 24, 24,
174 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
175 &cancelable_task_tracker_);
176 base::RunLoop().RunUntilIdle();
177 EXPECT_TRUE(is_callback_invoked_);
178 }
179
180 TEST_F(LargeIconServiceTest, ScaleUp) {
181 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(16, 16, kTestColor));
182 expected_bitmap_ = CreateTestBitmapResult(24, 24, kTestColor);
183 large_icon_service_.GetLargeIconOrFallbackStyle(
184 GURL(kDummyUrl),
185 14, // Lowered requirement so stored bitmap is admitted.
186 24,
187 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
188 &cancelable_task_tracker_);
189 base::RunLoop().RunUntilIdle();
190 EXPECT_TRUE(is_callback_invoked_);
191 }
192
193 // |desired_size_in_pixel| == 0 means retrieve original image without scaling.
194 TEST_F(LargeIconServiceTest, NoScale) {
195 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(24, 24, kTestColor));
196 expected_bitmap_ = CreateTestBitmapResult(24, 24, kTestColor);
197 large_icon_service_.GetLargeIconOrFallbackStyle(
198 GURL(kDummyUrl), 16, 0,
199 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
200 &cancelable_task_tracker_);
201 base::RunLoop().RunUntilIdle();
202 EXPECT_TRUE(is_callback_invoked_);
203 }
204
205 TEST_F(LargeIconServiceTest, FallbackSinceIconTooSmall) {
206 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(16, 16, kTestColor));
207 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
208 expected_fallback_icon_style_->background_color = kTestColor;
209 expected_fallback_icon_style_->is_default_background_color = false;
210 large_icon_service_.GetLargeIconOrFallbackStyle(
211 GURL(kDummyUrl), 24, 24,
212 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
213 &cancelable_task_tracker_);
214 base::RunLoop().RunUntilIdle();
215 EXPECT_TRUE(is_callback_invoked_);
216 }
217
218 TEST_F(LargeIconServiceTest, FallbackSinceIconNotSquare) {
219 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(24, 32, kTestColor));
220 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
221 expected_fallback_icon_style_->background_color = kTestColor;
222 expected_fallback_icon_style_->is_default_background_color = false;
223 large_icon_service_.GetLargeIconOrFallbackStyle(
224 GURL(kDummyUrl), 24, 24,
225 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
226 &cancelable_task_tracker_);
227 base::RunLoop().RunUntilIdle();
228 EXPECT_TRUE(is_callback_invoked_);
229 }
230
231 TEST_F(LargeIconServiceTest, FallbackSinceIconMissing) {
232 InjectMockResult(GURL(kDummyUrl), favicon_base::FaviconRawBitmapResult());
233 // Expect default fallback style, including background.
234 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
235 large_icon_service_.GetLargeIconOrFallbackStyle(
236 GURL(kDummyUrl), 24, 24,
237 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
238 &cancelable_task_tracker_);
239 base::RunLoop().RunUntilIdle();
240 EXPECT_TRUE(is_callback_invoked_);
241 }
242
243 TEST_F(LargeIconServiceTest, FallbackSinceIconMissingNoScale) {
244 InjectMockResult(GURL(kDummyUrl), favicon_base::FaviconRawBitmapResult());
245 // Expect default fallback style, including background.
246 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
247 large_icon_service_.GetLargeIconOrFallbackStyle(
248 GURL(kDummyUrl), 24, 0,
249 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
250 &cancelable_task_tracker_);
251 base::RunLoop().RunUntilIdle();
252 EXPECT_TRUE(is_callback_invoked_);
253 }
254
255 // Oddball case where we demand a high resolution icon to scale down. Generates
256 // fallback even though an icon with the final size is available.
257 TEST_F(LargeIconServiceTest, FallbackSinceTooPicky) {
258 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(24, 24, kTestColor));
259 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
260 expected_fallback_icon_style_->background_color = kTestColor;
261 expected_fallback_icon_style_->is_default_background_color = false;
262 large_icon_service_.GetLargeIconOrFallbackStyle(
263 GURL(kDummyUrl), 32, 24,
264 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
265 &cancelable_task_tracker_);
266 base::RunLoop().RunUntilIdle();
267 EXPECT_TRUE(is_callback_invoked_);
268 }
269
270 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServer) { 124 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServer) {
271 const GURL kExpectedServerUrl( 125 const GURL kExpectedServerUrl(
272 "https://t0.gstatic.com/faviconV2?user=chrome&drop_404_icon=true" 126 "https://t0.gstatic.com/faviconV2?user=chrome&drop_404_icon=true"
273 "&size=192&min_size=42&max_size=256&fallback_opts=TYPE" 127 "&size=192&min_size=42&max_size=256&fallback_opts=TYPE"
274 "&url=http://www.example.com/"); 128 "&url=http://www.example.com/");
275 129
276 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); 130 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0);
277 131
278 base::MockCallback<base::Callback<void(bool success)>> callback; 132 base::MockCallback<base::Callback<void(bool success)>> callback;
279 EXPECT_CALL(*mock_image_fetcher_, 133 EXPECT_CALL(*mock_image_fetcher_,
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 227
374 base::MockCallback<base::Callback<void(bool success)>> callback; 228 base::MockCallback<base::Callback<void(bool success)>> callback;
375 large_icon_service_ 229 large_icon_service_
376 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 230 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
377 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); 231 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get());
378 232
379 EXPECT_CALL(callback, Run(false)); 233 EXPECT_CALL(callback, Run(false));
380 base::RunLoop().RunUntilIdle(); 234 base::RunLoop().RunUntilIdle();
381 } 235 }
382 236
237 class LargeIconServiceGetterTest : public LargeIconServiceTest,
238 public ::testing::WithParamInterface<bool> {
239 public:
240 LargeIconServiceGetterTest() : LargeIconServiceTest() {}
241 ~LargeIconServiceGetterTest() override {}
242
243 void GetLargeIconOrFallbackStyleAndWaitForCallback(
244 const GURL& page_url,
245 int min_source_size_in_pixel,
246 int desired_size_in_pixel) {
247 // Switch over testing two analogous functions based on the bool param.
248 if (GetParam()) {
249 large_icon_service_.GetLargeIconOrFallbackStyle(
250 page_url, min_source_size_in_pixel, desired_size_in_pixel,
251 base::Bind(&LargeIconServiceGetterTest::RawBitmapResultCallback,
252 base::Unretained(this)),
253 &cancelable_task_tracker_);
254 } else {
255 large_icon_service_.GetLargeIconImageOrFallbackStyle(
256 page_url, min_source_size_in_pixel, desired_size_in_pixel,
257 base::Bind(&LargeIconServiceGetterTest::ImageResultCallback,
258 base::Unretained(this)),
259 &cancelable_task_tracker_);
260 }
261 base::RunLoop().RunUntilIdle();
262 }
263
264 void RawBitmapResultCallback(const favicon_base::LargeIconResult& result) {
265 if (result.bitmap.is_valid()) {
266 returned_bitmap_size_ =
267 base::MakeUnique<gfx::Size>(result.bitmap.pixel_size);
268 }
269 StoreFallbackStyle(result.fallback_icon_style.get());
270 }
271
272 void ImageResultCallback(const favicon_base::LargeIconImageResult& result) {
273 if (!result.image.IsEmpty()) {
274 returned_bitmap_size_ =
275 base::MakeUnique<gfx::Size>(result.image.ToImageSkia()->size());
276 }
277 StoreFallbackStyle(result.fallback_icon_style.get());
278 }
279
280 void StoreFallbackStyle(
281 const favicon_base::FallbackIconStyle* fallback_style) {
282 if (fallback_style) {
283 returned_fallback_style_ =
284 base::MakeUnique<favicon_base::FallbackIconStyle>(*fallback_style);
285 }
286 }
287
288 void InjectMockResult(
289 const GURL& page_url,
290 const favicon_base::FaviconRawBitmapResult& mock_result) {
291 ON_CALL(mock_favicon_service_,
292 GetLargestRawFaviconForPageURL(page_url, _, _, _, _))
293 .WillByDefault(PostReply<5>(mock_result));
294 }
295
296 protected:
297 base::CancelableTaskTracker cancelable_task_tracker_;
298
299 std::unique_ptr<favicon_base::FallbackIconStyle> returned_fallback_style_;
300 std::unique_ptr<gfx::Size> returned_bitmap_size_;
301
302 private:
303 DISALLOW_COPY_AND_ASSIGN(LargeIconServiceGetterTest);
304 };
305
306 MATCHER_P(HasBackgroundColor, c, "") {
307 return arg.background_color == c && !arg.is_default_background_color;
308 }
309
310 MATCHER(HasDefaultBackgroundColor, "") {
311 return arg.is_default_background_color;
312 }
313
314 TEST_P(LargeIconServiceGetterTest, SameSize) {
315 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(24, 24, kTestColor));
316 GetLargeIconOrFallbackStyleAndWaitForCallback(
317 GURL(kDummyUrl),
318 24, // |min_source_size_in_pixel|
319 24); // |desired_size_in_pixel|
320 EXPECT_THAT(*returned_bitmap_size_, Eq(gfx::Size(24, 24)));
pkotwicz 2017/04/04 17:47:15 Nit: Can you use the more ubiquitous EXPECT_EQ(*re
jkrcal 2017/04/05 15:34:38 Done.
321 EXPECT_THAT(returned_fallback_style_, IsNull());
pkotwicz 2017/04/04 17:47:15 Nit: Can you use the more ubiquitous EXPECT_EQ(ret
jkrcal 2017/04/05 15:34:38 Done.
322 }
323
324 TEST_P(LargeIconServiceGetterTest, ScaleDown) {
325 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(32, 32, kTestColor));
326 GetLargeIconOrFallbackStyleAndWaitForCallback(GURL(kDummyUrl), 24, 24);
327 EXPECT_THAT(*returned_bitmap_size_, Eq(gfx::Size(24, 24)));
328 EXPECT_THAT(returned_fallback_style_, IsNull());
329 }
330
331 TEST_P(LargeIconServiceGetterTest, ScaleUp) {
332 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(16, 16, kTestColor));
333 GetLargeIconOrFallbackStyleAndWaitForCallback(
334 GURL(kDummyUrl),
335 14, // Lowered requirement so stored bitmap is admitted.
336 24);
337 EXPECT_THAT(*returned_bitmap_size_, Eq(gfx::Size(24, 24)));
338 EXPECT_THAT(returned_fallback_style_, IsNull());
339 }
340
341 // |desired_size_in_pixel| == 0 means retrieve original image without scaling.
342 TEST_P(LargeIconServiceGetterTest, NoScale) {
343 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(24, 24, kTestColor));
344 GetLargeIconOrFallbackStyleAndWaitForCallback(GURL(kDummyUrl), 16, 0);
345 EXPECT_THAT(*returned_bitmap_size_, Eq(gfx::Size(24, 24)));
346 EXPECT_THAT(returned_fallback_style_, IsNull());
347 }
348
349 TEST_P(LargeIconServiceGetterTest, FallbackSinceIconTooSmall) {
350 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(16, 16, kTestColor));
351 GetLargeIconOrFallbackStyleAndWaitForCallback(GURL(kDummyUrl), 24, 24);
352 EXPECT_THAT(returned_bitmap_size_, IsNull());
353 EXPECT_THAT(*returned_fallback_style_, HasBackgroundColor(kTestColor));
pkotwicz 2017/04/04 17:47:15 Can we do EXPECT_TRUE(HasNonDefaultBackgroundColo
jkrcal 2017/04/05 15:34:38 I'd rather make the color explicit. Anyway, rewrit
354 }
355
356 TEST_P(LargeIconServiceGetterTest, FallbackSinceIconNotSquare) {
357 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(24, 32, kTestColor));
358 GetLargeIconOrFallbackStyleAndWaitForCallback(GURL(kDummyUrl), 24, 24);
359 EXPECT_THAT(returned_bitmap_size_, IsNull());
360 EXPECT_THAT(*returned_fallback_style_, HasBackgroundColor(kTestColor));
361 }
362
363 TEST_P(LargeIconServiceGetterTest, FallbackSinceIconMissing) {
364 InjectMockResult(GURL(kDummyUrl), favicon_base::FaviconRawBitmapResult());
365 GetLargeIconOrFallbackStyleAndWaitForCallback(GURL(kDummyUrl), 24, 24);
366 EXPECT_THAT(returned_bitmap_size_, IsNull());
367 EXPECT_THAT(*returned_fallback_style_, HasDefaultBackgroundColor());
pkotwicz 2017/04/04 17:47:15 Can we do: EXPECT_TRUE(returned_fallback_style_->
jkrcal 2017/04/05 15:34:38 Yep, this sounds simpler!
368 }
369
370 TEST_P(LargeIconServiceGetterTest, FallbackSinceIconMissingNoScale) {
371 InjectMockResult(GURL(kDummyUrl), favicon_base::FaviconRawBitmapResult());
372 GetLargeIconOrFallbackStyleAndWaitForCallback(GURL(kDummyUrl), 24, 0);
373 EXPECT_THAT(returned_bitmap_size_, IsNull());
374 EXPECT_THAT(*returned_fallback_style_, HasDefaultBackgroundColor());
375 }
376
377 // Oddball case where we demand a high resolution icon to scale down. Generates
378 // fallback even though an icon with the final size is available.
379 TEST_P(LargeIconServiceGetterTest, FallbackSinceTooPicky) {
380 InjectMockResult(GURL(kDummyUrl), CreateTestBitmapResult(24, 24, kTestColor));
381 GetLargeIconOrFallbackStyleAndWaitForCallback(GURL(kDummyUrl), 32, 24);
382 EXPECT_THAT(returned_bitmap_size_, IsNull());
383 EXPECT_THAT(*returned_fallback_style_, HasBackgroundColor(kTestColor));
384 }
385
386 // Every test will appear with suffix /0 (param false) and /1 (param true), e.g.
387 // LargeIconServiceGetterTest.FallbackSinceTooPicky/0: get image.
388 // LargeIconServiceGetterTest.FallbackSinceTooPicky/1: get raw bitmap.
389 INSTANTIATE_TEST_CASE_P(, // Empty instatiation name.
390 LargeIconServiceGetterTest,
391 ::testing::Values(false, true));
392
383 } // namespace 393 } // namespace
384 } // namespace favicon 394 } // namespace favicon
OLDNEW
« no previous file with comments | « components/favicon/core/large_icon_service.cc ('k') | components/favicon_base/favicon_callback.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698