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

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

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

Powered by Google App Engine
This is Rietveld 408576698