OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/ntp_tiles/icon_cacher_impl.h" | 5 #include "components/ntp_tiles/icon_cacher_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "components/favicon/core/favicon_client.h" | 13 #include "components/favicon/core/favicon_client.h" |
14 #include "components/favicon/core/favicon_service.h" | 14 #include "components/favicon/core/favicon_service.h" |
15 #include "components/favicon/core/favicon_util.h" | 15 #include "components/favicon/core/favicon_util.h" |
16 #include "components/history/core/browser/history_database_params.h" | 16 #include "components/history/core/browser/history_database_params.h" |
17 #include "components/history/core/browser/history_service.h" | 17 #include "components/history/core/browser/history_service.h" |
18 #include "components/image_fetcher/image_fetcher.h" | 18 #include "components/image_fetcher/image_fetcher.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "ui/base/resource/resource_bundle.h" | |
21 #include "ui/gfx/image/image_unittest_util.h" | 22 #include "ui/gfx/image/image_unittest_util.h" |
22 | 23 |
23 using ::testing::_; | 24 using ::testing::_; |
24 using ::testing::InSequence; | 25 using ::testing::InSequence; |
25 using ::testing::MockFunction; | 26 using ::testing::MockFunction; |
26 using ::testing::Return; | 27 using ::testing::Return; |
27 | 28 |
28 namespace ntp_tiles { | 29 namespace ntp_tiles { |
29 namespace { | 30 namespace { |
30 | 31 |
31 class MockImageFetcher : public image_fetcher::ImageFetcher { | 32 class MockImageFetcher : public image_fetcher::ImageFetcher { |
32 public: | 33 public: |
33 MOCK_METHOD1(SetImageFetcherDelegate, | 34 MOCK_METHOD1(SetImageFetcherDelegate, |
34 void(image_fetcher::ImageFetcherDelegate* delegate)); | 35 void(image_fetcher::ImageFetcherDelegate* delegate)); |
35 MOCK_METHOD1(SetDataUseServiceName, | 36 MOCK_METHOD1(SetDataUseServiceName, |
36 void(image_fetcher::ImageFetcher::DataUseServiceName name)); | 37 void(image_fetcher::ImageFetcher::DataUseServiceName name)); |
37 MOCK_METHOD3(StartOrQueueNetworkRequest, | 38 MOCK_METHOD3(StartOrQueueNetworkRequest, |
38 void(const std::string& id, | 39 void(const std::string& id, |
39 const GURL& image_url, | 40 const GURL& image_url, |
40 base::Callback<void(const std::string& id, | 41 base::Callback<void(const std::string& id, |
41 const gfx::Image& image)> callback)); | 42 const gfx::Image& image)> callback)); |
42 }; | 43 }; |
43 | 44 |
45 // This class provides methods to inject an image resource where a real resource | |
46 // would be necessary otherwise. All other methods have return values that allow | |
47 // the normal implementation to proceed. | |
48 class MockResourceDelegate : public ui::ResourceBundle::Delegate { | |
sfiera
2017/02/16 18:54:16
This class is (mostly) not a mock—please don't mix
fhorschig
2017/02/17 16:24:03
Done.
| |
49 public: | |
50 ~MockResourceDelegate() override {} | |
51 base::FilePath GetPathForResourcePack(const base::FilePath& pack_path, | |
52 ui::ScaleFactor scale_factor) override { | |
53 return pack_path; // Continue Loading pack file. | |
54 } | |
55 | |
56 base::FilePath GetPathForLocalePack(const base::FilePath& pack_path, | |
57 const std::string& locale) override { | |
58 return pack_path; // Continue Loading pack file. | |
59 } | |
60 | |
61 MOCK_METHOD1(GetImageNamed, gfx::Image(int)); | |
62 | |
63 gfx::Image GetNativeImageNamed(int resource_id) override { | |
64 return gfx::Image(); // Attempt retrieval of the default resource. | |
65 } | |
66 | |
67 base::RefCountedMemory* LoadDataResourceBytes( | |
68 int resource_id, | |
69 ui::ScaleFactor scale_factor) override { | |
70 return nullptr; // Attempt retrieval of the default resource. | |
71 } | |
72 | |
73 bool GetRawDataResource(int resource_id, | |
74 ui::ScaleFactor scale_factor, | |
75 base::StringPiece* value) override { | |
76 return false; // Attempt retrieval of the default resource. | |
77 } | |
78 | |
79 bool GetLocalizedString(int message_id, base::string16* value) override { | |
80 return false; // Attempt retrieval of the default resource. | |
81 } | |
82 }; | |
83 | |
44 class IconCacherTest : public ::testing::Test { | 84 class IconCacherTest : public ::testing::Test { |
45 protected: | 85 protected: |
46 IconCacherTest() | 86 IconCacherTest() |
47 : site_(base::string16(), // title, unused | 87 : site_(base::string16(), // title, unused |
48 GURL("http://url.google/"), | 88 GURL("http://url.google/"), |
49 GURL("http://url.google/icon.png"), | 89 GURL("http://url.google/icon.png"), |
50 GURL("http://url.google/favicon.ico"), | 90 GURL("http://url.google/favicon.ico"), |
51 GURL()), // thumbnail, unused | 91 GURL()), // thumbnail, unused |
52 image_fetcher_(new ::testing::StrictMock<MockImageFetcher>), | 92 image_fetcher_(new ::testing::StrictMock<MockImageFetcher>), |
53 favicon_service_(/*favicon_client=*/nullptr, &history_service_) { | 93 favicon_service_(/*favicon_client=*/nullptr, &history_service_) { |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop)); | 228 EXPECT_CALL(done, Call(false)).WillOnce(Quit(&loop)); |
189 } | 229 } |
190 | 230 |
191 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); | 231 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); |
192 cacher.StartFetch(site_, BindMockFunction(&done)); | 232 cacher.StartFetch(site_, BindMockFunction(&done)); |
193 loop.Run(); | 233 loop.Run(); |
194 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); | 234 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::FAVICON)); |
195 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); | 235 EXPECT_FALSE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); |
196 } | 236 } |
197 | 237 |
238 TEST_F(IconCacherTest, ProvidesDefaultIconAndSucceedsWithFetching) { | |
239 if (ui::ResourceBundle::HasSharedInstance()) { | |
sfiera
2017/02/16 18:54:16
I'd put this as setup for IconCacherTest. Even if
fhorschig
2017/02/17 16:24:03
Done.
| |
240 ui::ResourceBundle::CleanupSharedInstance(); | |
241 } | |
242 | |
243 // Returns true after the global resource loader instance has been created. | |
244 MockResourceDelegate mock_resource; | |
245 ui::ResourceBundle::InitSharedInstanceWithLocale( | |
246 "en-US", &mock_resource, ui::ResourceBundle::LOAD_COMMON_RESOURCES); | |
247 MockFunction<void(bool)> done; | |
248 base::RunLoop loop_for_static; | |
249 base::RunLoop loop_for_fetch; | |
250 { | |
251 InSequence s; | |
252 EXPECT_CALL(*image_fetcher_, | |
253 SetDataUseServiceName( | |
254 data_use_measurement::DataUseUserData::NTP_TILES)); | |
255 EXPECT_CALL(mock_resource, GetImageNamed(12345)) | |
256 .WillOnce(Return(gfx::test::CreateImage(64, 64))); | |
257 EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop_for_static)); | |
258 EXPECT_CALL(*image_fetcher_, | |
259 StartOrQueueNetworkRequest(_, site_.large_icon_url, _)) | |
260 .WillOnce(PassFetch(128, 128)); | |
261 EXPECT_CALL(done, Call(true)).WillOnce(Quit(&loop_for_fetch)); | |
262 } | |
263 | |
264 IconCacherImpl cacher(&favicon_service_, std::move(image_fetcher_)); | |
265 site_.default_resource_id = 12345; | |
266 cacher.StartFetch(site_, BindMockFunction(&done)); | |
267 | |
268 loop_for_static.Run(); // Wait for the default image. | |
269 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); | |
sfiera
2017/02/16 18:54:16
Can you add a check for the size here, so that we
fhorschig
2017/02/17 16:24:03
Done.
| |
270 | |
271 // Let the fetcher continue and wait for the second call of the callback. | |
272 loop_for_fetch.Run(); // Wait for the updated image. | |
273 EXPECT_TRUE(IconIsCachedFor(site_.url, favicon_base::TOUCH_ICON)); | |
274 } | |
275 | |
198 } // namespace | 276 } // namespace |
199 } // namespace ntp_tiles | 277 } // namespace ntp_tiles |
OLD | NEW |