| 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/popular_sites_impl.h" | 5 #include "components/ntp_tiles/popular_sites_impl.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <unordered_set> |
| 9 #include <utility> | 10 #include <utility> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/bind.h" | 13 #include "base/bind.h" |
| 13 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 14 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 15 #include "base/files/important_file_writer.h" | 16 #include "base/files/important_file_writer.h" |
| 16 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/json/json_writer.h" | 18 #include "base/json/json_writer.h" |
| 18 #include "base/memory/ptr_util.h" | 19 #include "base/memory/ptr_util.h" |
| 19 #include "base/optional.h" | 20 #include "base/optional.h" |
| 20 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
| 21 #include "base/test/sequenced_worker_pool_owner.h" | 22 #include "base/test/sequenced_worker_pool_owner.h" |
| 22 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "base/values.h" | 24 #include "base/values.h" |
| 25 #include "components/ntp_tiles/icon_cacher.h" |
| 24 #include "components/ntp_tiles/json_unsafe_parser.h" | 26 #include "components/ntp_tiles/json_unsafe_parser.h" |
| 25 #include "components/ntp_tiles/pref_names.h" | 27 #include "components/ntp_tiles/pref_names.h" |
| 26 #include "components/ntp_tiles/switches.h" | 28 #include "components/ntp_tiles/switches.h" |
| 27 #include "components/pref_registry/pref_registry_syncable.h" | 29 #include "components/pref_registry/pref_registry_syncable.h" |
| 28 #include "components/sync_preferences/testing_pref_service_syncable.h" | 30 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 29 #include "net/http/http_status_code.h" | 31 #include "net/http/http_status_code.h" |
| 30 #include "net/url_request/test_url_fetcher_factory.h" | 32 #include "net/url_request/test_url_fetcher_factory.h" |
| 31 #include "net/url_request/url_request_status.h" | 33 #include "net/url_request/url_request_status.h" |
| 32 #include "net/url_request/url_request_test_util.h" | 34 #include "net/url_request/url_request_test_util.h" |
| 33 #include "testing/gmock/include/gmock/gmock.h" | 35 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 56 } | 58 } |
| 57 | 59 |
| 58 size_t GetNumberOfDefaultPopularSitesForPlatform() { | 60 size_t GetNumberOfDefaultPopularSitesForPlatform() { |
| 59 #if defined(OS_ANDROID) || defined(OS_IOS) | 61 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 60 return 8ul; | 62 return 8ul; |
| 61 #else | 63 #else |
| 62 return 0; | 64 return 0; |
| 63 #endif | 65 #endif |
| 64 } | 66 } |
| 65 | 67 |
| 68 class FakeIconCacher : public IconCacher { |
| 69 public: |
| 70 FakeIconCacher() {} |
| 71 ~FakeIconCacher() override {} |
| 72 |
| 73 void StartFetch(PopularSites::Site site, |
| 74 const base::Callback<void(bool)>& done) override {} |
| 75 |
| 76 void AddDefaultImage(const PopularSites::Site& site, |
| 77 int image_resource_id) override { |
| 78 registered_sites_.insert(site.url.spec()); |
| 79 } |
| 80 |
| 81 size_t GetRegisteredSitesCount() const { return registered_sites_.size(); } |
| 82 |
| 83 private: |
| 84 std::unordered_set<std::string> registered_sites_; |
| 85 }; |
| 86 |
| 66 class PopularSitesTest : public ::testing::Test { | 87 class PopularSitesTest : public ::testing::Test { |
| 67 protected: | 88 protected: |
| 68 PopularSitesTest() | 89 PopularSitesTest() |
| 69 : kWikipedia{ | 90 : kWikipedia{ |
| 70 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"}, | 91 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"}, |
| 71 {kUrl, "https://zz.m.wikipedia.org/"}, | 92 {kUrl, "https://zz.m.wikipedia.org/"}, |
| 72 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"}, | 93 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"}, |
| 73 }, | 94 }, |
| 74 kYouTube{ | 95 kYouTube{ |
| 75 {kTitle, "YouTube"}, | 96 {kTitle, "YouTube"}, |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json"); | 248 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json"); |
| 228 RespondWith404( | 249 RespondWith404( |
| 229 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json"); | 250 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json"); |
| 230 | 251 |
| 231 PopularSites::SitesVector sites; | 252 PopularSites::SitesVector sites; |
| 232 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), | 253 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
| 233 Eq(base::Optional<bool>(false))); | 254 Eq(base::Optional<bool>(false))); |
| 234 EXPECT_THAT(sites.size(), Eq(GetNumberOfDefaultPopularSitesForPlatform())); | 255 EXPECT_THAT(sites.size(), Eq(GetNumberOfDefaultPopularSitesForPlatform())); |
| 235 } | 256 } |
| 236 | 257 |
| 258 TEST_F(PopularSitesTest, AddsIconUrlsToIconCacher) { |
| 259 auto icon_cacher = base::MakeUnique<FakeIconCacher>(); |
| 260 PopularSitesImpl::AddIconsForDefaultPagesToCacher(prefs_, icon_cacher.get()); |
| 261 EXPECT_THAT(icon_cacher->GetRegisteredSitesCount(), |
| 262 Eq(GetNumberOfDefaultPopularSitesForPlatform())); |
| 263 } |
| 264 |
| 237 TEST_F(PopularSitesTest, ProvidesDefaultSitesUntilCallbackReturns) { | 265 TEST_F(PopularSitesTest, ProvidesDefaultSitesUntilCallbackReturns) { |
| 238 SetCountryAndVersion("ZZ", "9"); | 266 SetCountryAndVersion("ZZ", "9"); |
| 239 RespondWithJSON( | 267 RespondWithJSON( |
| 240 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", | 268 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", |
| 241 {kWikipedia}); | 269 {kWikipedia}); |
| 242 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( | 270 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( |
| 243 new net::TestURLRequestContextGetter( | 271 new net::TestURLRequestContextGetter( |
| 244 base::ThreadTaskRunnerHandle::Get())); | 272 base::ThreadTaskRunnerHandle::Get())); |
| 245 std::unique_ptr<PopularSites> popular_sites = | 273 std::unique_ptr<PopularSites> popular_sites = |
| 246 CreatePopularSites(url_request_context.get()); | 274 CreatePopularSites(url_request_context.get()); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", | 443 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", |
| 416 {kChromium}); | 444 {kChromium}); |
| 417 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), | 445 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
| 418 Eq(base::Optional<bool>(true))); | 446 Eq(base::Optional<bool>(true))); |
| 419 ASSERT_THAT(sites.size(), Eq(1u)); | 447 ASSERT_THAT(sites.size(), Eq(1u)); |
| 420 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); | 448 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); |
| 421 } | 449 } |
| 422 | 450 |
| 423 } // namespace | 451 } // namespace |
| 424 } // namespace ntp_tiles | 452 } // namespace ntp_tiles |
| OLD | NEW |