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

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

Issue 2698473004: Split FaviconService and FaviconServiceImpl. (Closed)
Patch Set: Revert requiring non-null service for FaviconHandler. Created 3 years, 10 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"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ref_counted_memory.h" 12 #include "base/memory/ref_counted_memory.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/task/cancelable_task_tracker.h" 15 #include "base/task/cancelable_task_tracker.h"
16 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
17 #include "components/favicon/core/favicon_client.h" 17 #include "components/favicon/core/favicon_client.h"
18 #include "components/favicon/core/favicon_service.h" 18 #include "components/favicon/core/test/mock_favicon_service.h"
19 #include "components/favicon_base/fallback_icon_style.h" 19 #include "components/favicon_base/fallback_icon_style.h"
20 #include "components/favicon_base/favicon_types.h" 20 #include "components/favicon_base/favicon_types.h"
21 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/skia/include/core/SkBitmap.h" 23 #include "third_party/skia/include/core/SkBitmap.h"
23 #include "third_party/skia/include/core/SkColor.h" 24 #include "third_party/skia/include/core/SkColor.h"
24 #include "ui/gfx/codec/png_codec.h" 25 #include "ui/gfx/codec/png_codec.h"
25 #include "ui/gfx/geometry/size.h" 26 #include "ui/gfx/geometry/size.h"
26 #include "ui/gfx/image/image.h" 27 #include "ui/gfx/image/image.h"
27 #include "url/gurl.h" 28 #include "url/gurl.h"
28 29
29 namespace favicon { 30 namespace favicon {
30 namespace { 31 namespace {
31 32
33 using testing::WithArg;
34 using testing::_;
35
32 const char kDummyUrl[] = "http://www.example.com"; 36 const char kDummyUrl[] = "http://www.example.com";
33 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png"; 37 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png";
34
35 const SkColor kTestColor = SK_ColorRED; 38 const SkColor kTestColor = SK_ColorRED;
36 39
37 favicon_base::FaviconRawBitmapResult CreateTestBitmap( 40 favicon_base::FaviconRawBitmapResult CreateTestBitmap(
38 int w, int h, SkColor color) { 41 int w, int h, SkColor color) {
39 favicon_base::FaviconRawBitmapResult result; 42 favicon_base::FaviconRawBitmapResult result;
40 result.expired = false; 43 result.expired = false;
41 44
42 // Create bitmap and fill with |color|. 45 // Create bitmap and fill with |color|.
43 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); 46 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes());
44 SkBitmap bitmap; 47 SkBitmap bitmap;
45 bitmap.allocN32Pixels(w, h); 48 bitmap.allocN32Pixels(w, h);
46 bitmap.eraseColor(color); 49 bitmap.eraseColor(color);
47 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &data->data()); 50 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &data->data());
48 result.bitmap_data = data; 51 result.bitmap_data = data;
49 52
50 result.pixel_size = gfx::Size(w, h); 53 result.pixel_size = gfx::Size(w, h);
51 result.icon_url = GURL(kDummyIconUrl); 54 result.icon_url = GURL(kDummyIconUrl);
52 result.icon_type = favicon_base::TOUCH_ICON; 55 result.icon_type = favicon_base::TOUCH_ICON;
53 CHECK(result.is_valid()); 56 CHECK(result.is_valid());
54 return result; 57 return result;
55 } 58 }
56 59
57 // A mock FaviconService that emits pre-programmed response.
58 class MockFaviconService : public FaviconService {
59 public:
60 MockFaviconService() : FaviconService(nullptr, nullptr) {
61 }
62
63 ~MockFaviconService() override {
64 }
65
66 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForPageURL(
67 const GURL& page_url,
68 const std::vector<int>& icon_types,
69 int minimum_size_in_pixels,
70 const favicon_base::FaviconRawBitmapCallback& callback,
71 base::CancelableTaskTracker* tracker) override {
72 favicon_base::FaviconRawBitmapResult mock_result =
73 mock_result_queue_.front();
74 mock_result_queue_.pop_front();
75 return tracker->PostTask(base::ThreadTaskRunnerHandle::Get().get(),
76 FROM_HERE, base::Bind(callback, mock_result));
77 }
78
79 void InjectResult(const favicon_base::FaviconRawBitmapResult& mock_result) {
80 mock_result_queue_.push_back(mock_result);
81 }
82
83 bool HasUnusedResults() {
84 return !mock_result_queue_.empty();
85 }
86
87 private:
88 std::deque<favicon_base::FaviconRawBitmapResult> mock_result_queue_;
89
90 DISALLOW_COPY_AND_ASSIGN(MockFaviconService);
91 };
92
93 // This class provides access to LargeIconService internals, using the current
94 // thread's task runner for testing.
95 class TestLargeIconService : public LargeIconService {
96 public:
97 explicit TestLargeIconService(MockFaviconService* mock_favicon_service)
98 : LargeIconService(mock_favicon_service,
99 base::ThreadTaskRunnerHandle::Get()) {}
100 ~TestLargeIconService() override {
101 }
102
103 private:
104 DISALLOW_COPY_AND_ASSIGN(TestLargeIconService);
105 };
106
107 class LargeIconServiceTest : public testing::Test { 60 class LargeIconServiceTest : public testing::Test {
108 public: 61 public:
109 LargeIconServiceTest() : is_callback_invoked_(false) { 62 LargeIconServiceTest()
110 } 63 : large_icon_service_(&mock_favicon_service_,
64 base::ThreadTaskRunnerHandle::Get()),
65 is_callback_invoked_(false) {}
111 66
112 ~LargeIconServiceTest() override { 67 ~LargeIconServiceTest() override {
113 } 68 }
114 69
115 void SetUp() override {
116 testing::Test::SetUp();
117 mock_favicon_service_.reset(new MockFaviconService());
118 large_icon_service_.reset(
119 new TestLargeIconService(mock_favicon_service_.get()));
120 }
121
122 void ResultCallback(const favicon_base::LargeIconResult& result) { 70 void ResultCallback(const favicon_base::LargeIconResult& result) {
123 is_callback_invoked_ = true; 71 is_callback_invoked_ = true;
124 72
125 // Checking presence and absence of results. 73 // Checking presence and absence of results.
126 EXPECT_EQ(expected_bitmap_.is_valid(), result.bitmap.is_valid()); 74 EXPECT_EQ(expected_bitmap_.is_valid(), result.bitmap.is_valid());
127 EXPECT_EQ(expected_fallback_icon_style_ != nullptr, 75 EXPECT_EQ(expected_fallback_icon_style_ != nullptr,
128 result.fallback_icon_style != nullptr); 76 result.fallback_icon_style != nullptr);
129 77
130 if (expected_bitmap_.is_valid()) { 78 if (expected_bitmap_.is_valid()) {
131 EXPECT_EQ(expected_bitmap_.pixel_size, result.bitmap.pixel_size); 79 EXPECT_EQ(expected_bitmap_.pixel_size, result.bitmap.pixel_size);
132 // Not actually checking bitmap content. 80 // Not actually checking bitmap content.
133 } 81 }
134 if (expected_fallback_icon_style_.get()) { 82 if (expected_fallback_icon_style_.get()) {
135 EXPECT_EQ(*expected_fallback_icon_style_, 83 EXPECT_EQ(*expected_fallback_icon_style_,
136 *result.fallback_icon_style); 84 *result.fallback_icon_style);
137 } 85 }
138 // Ensure all mock results have been consumed. 86 }
139 EXPECT_FALSE(mock_favicon_service_->HasUnusedResults()); 87
88 void InjectMockResult(
89 const GURL& page_url,
90 const favicon_base::FaviconRawBitmapResult& mock_result) {
91 EXPECT_CALL(mock_favicon_service_,
92 GetLargestRawFaviconForPageURL(page_url, _, _, _, _))
93 .WillOnce(WithArg<3>(PostCallbackWithResult(mock_result)));
140 } 94 }
141 95
142 protected: 96 protected:
143 base::MessageLoopForIO loop_; 97 base::MessageLoopForIO loop_;
144 98
145 std::unique_ptr<MockFaviconService> mock_favicon_service_; 99 testing::StrictMock<MockFaviconService> mock_favicon_service_;
146 std::unique_ptr<TestLargeIconService> large_icon_service_; 100 LargeIconService large_icon_service_;
147 base::CancelableTaskTracker cancelable_task_tracker_; 101 base::CancelableTaskTracker cancelable_task_tracker_;
148 102
149 favicon_base::FaviconRawBitmapResult expected_bitmap_; 103 favicon_base::FaviconRawBitmapResult expected_bitmap_;
150 std::unique_ptr<favicon_base::FallbackIconStyle> 104 std::unique_ptr<favicon_base::FallbackIconStyle>
151 expected_fallback_icon_style_; 105 expected_fallback_icon_style_;
152 106
153 bool is_callback_invoked_; 107 bool is_callback_invoked_;
154 108
155 private: 109 private:
156 DISALLOW_COPY_AND_ASSIGN(LargeIconServiceTest); 110 DISALLOW_COPY_AND_ASSIGN(LargeIconServiceTest);
157 }; 111 };
158 112
159 TEST_F(LargeIconServiceTest, SameSize) { 113 TEST_F(LargeIconServiceTest, SameSize) {
160 mock_favicon_service_->InjectResult(CreateTestBitmap(24, 24, kTestColor)); 114 InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(24, 24, kTestColor));
161 expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor); 115 expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor);
162 large_icon_service_->GetLargeIconOrFallbackStyle( 116 large_icon_service_.GetLargeIconOrFallbackStyle(
163 GURL(kDummyUrl), 117 GURL(kDummyUrl),
164 24, // |min_source_size_in_pixel| 118 24, // |min_source_size_in_pixel|
165 24, // |desired_size_in_pixel| 119 24, // |desired_size_in_pixel|
166 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)), 120 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
167 &cancelable_task_tracker_); 121 &cancelable_task_tracker_);
168 base::RunLoop().RunUntilIdle(); 122 base::RunLoop().RunUntilIdle();
169 EXPECT_TRUE(is_callback_invoked_); 123 EXPECT_TRUE(is_callback_invoked_);
170 } 124 }
171 125
172 TEST_F(LargeIconServiceTest, ScaleDown) { 126 TEST_F(LargeIconServiceTest, ScaleDown) {
173 mock_favicon_service_->InjectResult(CreateTestBitmap(32, 32, kTestColor)); 127 InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(32, 32, kTestColor));
174 expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor); 128 expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor);
175 large_icon_service_->GetLargeIconOrFallbackStyle( 129 large_icon_service_.GetLargeIconOrFallbackStyle(
176 GURL(kDummyUrl), 130 GURL(kDummyUrl), 24, 24,
177 24,
178 24,
179 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)), 131 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
180 &cancelable_task_tracker_); 132 &cancelable_task_tracker_);
181 base::RunLoop().RunUntilIdle(); 133 base::RunLoop().RunUntilIdle();
182 EXPECT_TRUE(is_callback_invoked_); 134 EXPECT_TRUE(is_callback_invoked_);
183 } 135 }
184 136
185 TEST_F(LargeIconServiceTest, ScaleUp) { 137 TEST_F(LargeIconServiceTest, ScaleUp) {
186 mock_favicon_service_->InjectResult(CreateTestBitmap(16, 16, kTestColor)); 138 InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(16, 16, kTestColor));
187 expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor); 139 expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor);
188 large_icon_service_->GetLargeIconOrFallbackStyle( 140 large_icon_service_.GetLargeIconOrFallbackStyle(
189 GURL(kDummyUrl), 141 GURL(kDummyUrl),
190 14, // Lowered requirement so stored bitmap is admitted. 142 14, // Lowered requirement so stored bitmap is admitted.
191 24, 143 24,
192 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)), 144 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
193 &cancelable_task_tracker_); 145 &cancelable_task_tracker_);
194 base::RunLoop().RunUntilIdle(); 146 base::RunLoop().RunUntilIdle();
195 EXPECT_TRUE(is_callback_invoked_); 147 EXPECT_TRUE(is_callback_invoked_);
196 } 148 }
197 149
198 // |desired_size_in_pixel| == 0 means retrieve original image without scaling. 150 // |desired_size_in_pixel| == 0 means retrieve original image without scaling.
199 TEST_F(LargeIconServiceTest, NoScale) { 151 TEST_F(LargeIconServiceTest, NoScale) {
200 mock_favicon_service_->InjectResult(CreateTestBitmap(24, 24, kTestColor)); 152 InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(24, 24, kTestColor));
201 expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor); 153 expected_bitmap_ = CreateTestBitmap(24, 24, kTestColor);
202 large_icon_service_->GetLargeIconOrFallbackStyle( 154 large_icon_service_.GetLargeIconOrFallbackStyle(
203 GURL(kDummyUrl), 155 GURL(kDummyUrl), 16, 0,
204 16,
205 0,
206 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)), 156 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
207 &cancelable_task_tracker_); 157 &cancelable_task_tracker_);
208 base::RunLoop().RunUntilIdle(); 158 base::RunLoop().RunUntilIdle();
209 EXPECT_TRUE(is_callback_invoked_); 159 EXPECT_TRUE(is_callback_invoked_);
210 } 160 }
211 161
212 TEST_F(LargeIconServiceTest, FallbackSinceIconTooSmall) { 162 TEST_F(LargeIconServiceTest, FallbackSinceIconTooSmall) {
213 mock_favicon_service_->InjectResult(CreateTestBitmap(16, 16, kTestColor)); 163 InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(16, 16, kTestColor));
214 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle); 164 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
215 expected_fallback_icon_style_->background_color = kTestColor; 165 expected_fallback_icon_style_->background_color = kTestColor;
216 expected_fallback_icon_style_->is_default_background_color = false; 166 expected_fallback_icon_style_->is_default_background_color = false;
217 large_icon_service_->GetLargeIconOrFallbackStyle( 167 large_icon_service_.GetLargeIconOrFallbackStyle(
218 GURL(kDummyUrl), 168 GURL(kDummyUrl), 24, 24,
219 24,
220 24,
221 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)), 169 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
222 &cancelable_task_tracker_); 170 &cancelable_task_tracker_);
223 base::RunLoop().RunUntilIdle(); 171 base::RunLoop().RunUntilIdle();
224 EXPECT_TRUE(is_callback_invoked_); 172 EXPECT_TRUE(is_callback_invoked_);
225 } 173 }
226 174
227 TEST_F(LargeIconServiceTest, FallbackSinceIconNotSquare) { 175 TEST_F(LargeIconServiceTest, FallbackSinceIconNotSquare) {
228 mock_favicon_service_->InjectResult(CreateTestBitmap(24, 32, kTestColor)); 176 InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(24, 32, kTestColor));
229 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle); 177 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
230 expected_fallback_icon_style_->background_color = kTestColor; 178 expected_fallback_icon_style_->background_color = kTestColor;
231 expected_fallback_icon_style_->is_default_background_color = false; 179 expected_fallback_icon_style_->is_default_background_color = false;
232 large_icon_service_->GetLargeIconOrFallbackStyle( 180 large_icon_service_.GetLargeIconOrFallbackStyle(
233 GURL(kDummyUrl), 181 GURL(kDummyUrl), 24, 24,
234 24,
235 24,
236 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)), 182 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
237 &cancelable_task_tracker_); 183 &cancelable_task_tracker_);
238 base::RunLoop().RunUntilIdle(); 184 base::RunLoop().RunUntilIdle();
239 EXPECT_TRUE(is_callback_invoked_); 185 EXPECT_TRUE(is_callback_invoked_);
240 } 186 }
241 187
242 TEST_F(LargeIconServiceTest, FallbackSinceIconMissing) { 188 TEST_F(LargeIconServiceTest, FallbackSinceIconMissing) {
243 mock_favicon_service_->InjectResult(favicon_base::FaviconRawBitmapResult()); 189 InjectMockResult(GURL(kDummyUrl), favicon_base::FaviconRawBitmapResult());
244 // Expect default fallback style, including background. 190 // Expect default fallback style, including background.
245 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle); 191 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
246 large_icon_service_->GetLargeIconOrFallbackStyle( 192 large_icon_service_.GetLargeIconOrFallbackStyle(
247 GURL(kDummyUrl), 193 GURL(kDummyUrl), 24, 24,
248 24,
249 24,
250 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)), 194 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
251 &cancelable_task_tracker_); 195 &cancelable_task_tracker_);
252 base::RunLoop().RunUntilIdle(); 196 base::RunLoop().RunUntilIdle();
253 EXPECT_TRUE(is_callback_invoked_); 197 EXPECT_TRUE(is_callback_invoked_);
254 } 198 }
255 199
256 TEST_F(LargeIconServiceTest, FallbackSinceIconMissingNoScale) { 200 TEST_F(LargeIconServiceTest, FallbackSinceIconMissingNoScale) {
257 mock_favicon_service_->InjectResult(favicon_base::FaviconRawBitmapResult()); 201 InjectMockResult(GURL(kDummyUrl), favicon_base::FaviconRawBitmapResult());
258 // Expect default fallback style, including background. 202 // Expect default fallback style, including background.
259 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle); 203 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
260 large_icon_service_->GetLargeIconOrFallbackStyle( 204 large_icon_service_.GetLargeIconOrFallbackStyle(
261 GURL(kDummyUrl), 205 GURL(kDummyUrl), 24, 0,
262 24,
263 0,
264 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)), 206 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
265 &cancelable_task_tracker_); 207 &cancelable_task_tracker_);
266 base::RunLoop().RunUntilIdle(); 208 base::RunLoop().RunUntilIdle();
267 EXPECT_TRUE(is_callback_invoked_); 209 EXPECT_TRUE(is_callback_invoked_);
268 } 210 }
269 211
270 // Oddball case where we demand a high resolution icon to scale down. Generates 212 // Oddball case where we demand a high resolution icon to scale down. Generates
271 // fallback even though an icon with the final size is available. 213 // fallback even though an icon with the final size is available.
272 TEST_F(LargeIconServiceTest, FallbackSinceTooPicky) { 214 TEST_F(LargeIconServiceTest, FallbackSinceTooPicky) {
273 mock_favicon_service_->InjectResult(CreateTestBitmap(24, 24, kTestColor)); 215 InjectMockResult(GURL(kDummyUrl), CreateTestBitmap(24, 24, kTestColor));
274 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle); 216 expected_fallback_icon_style_.reset(new favicon_base::FallbackIconStyle);
275 expected_fallback_icon_style_->background_color = kTestColor; 217 expected_fallback_icon_style_->background_color = kTestColor;
276 expected_fallback_icon_style_->is_default_background_color = false; 218 expected_fallback_icon_style_->is_default_background_color = false;
277 large_icon_service_->GetLargeIconOrFallbackStyle( 219 large_icon_service_.GetLargeIconOrFallbackStyle(
278 GURL(kDummyUrl), 220 GURL(kDummyUrl), 32, 24,
279 32,
280 24,
281 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)), 221 base::Bind(&LargeIconServiceTest::ResultCallback, base::Unretained(this)),
282 &cancelable_task_tracker_); 222 &cancelable_task_tracker_);
283 base::RunLoop().RunUntilIdle(); 223 base::RunLoop().RunUntilIdle();
284 EXPECT_TRUE(is_callback_invoked_); 224 EXPECT_TRUE(is_callback_invoked_);
285 } 225 }
286 226
287 } // namespace 227 } // namespace
288 } // namespace favicon 228 } // namespace favicon
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698