| 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 <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/files/important_file_writer.h" | 15 #include "base/files/important_file_writer.h" |
| 16 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/json/json_writer.h" | 17 #include "base/json/json_writer.h" |
| 18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 19 #include "base/optional.h" | 19 #include "base/optional.h" |
| 20 #include "base/run_loop.h" | 20 #include "base/run_loop.h" |
| 21 #include "base/test/scoped_feature_list.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/constants.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 19 matching lines...) Expand all Loading... |
| 53 } | 55 } |
| 54 | 56 |
| 55 ::testing::Matcher<const GURL&> URLEq(const std::string& s) { | 57 ::testing::Matcher<const GURL&> URLEq(const std::string& s) { |
| 56 return ::testing::Eq(GURL(s)); | 58 return ::testing::Eq(GURL(s)); |
| 57 } | 59 } |
| 58 | 60 |
| 59 size_t GetNumberOfDefaultPopularSitesForPlatform() { | 61 size_t GetNumberOfDefaultPopularSitesForPlatform() { |
| 60 #if defined(OS_ANDROID) || defined(OS_IOS) | 62 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 61 return 8ul; | 63 return 8ul; |
| 62 #else | 64 #else |
| 63 return 0; | 65 return 0ul; |
| 64 #endif | 66 #endif |
| 65 } | 67 } |
| 66 | 68 |
| 67 class PopularSitesTest : public ::testing::Test { | 69 class PopularSitesTest : public ::testing::Test { |
| 68 protected: | 70 protected: |
| 69 PopularSitesTest() | 71 PopularSitesTest() |
| 70 : kWikipedia{ | 72 : kWikipedia{ |
| 71 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"}, | 73 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"}, |
| 72 {kUrl, "https://zz.m.wikipedia.org/"}, | 74 {kUrl, "https://zz.m.wikipedia.org/"}, |
| 73 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"}, | 75 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"}, |
| 74 }, | 76 }, |
| 75 kYouTube{ | 77 kYouTube{ |
| 76 {kTitle, "YouTube"}, | 78 {kTitle, "YouTube"}, |
| 77 {kUrl, "https://m.youtube.com/"}, | 79 {kUrl, "https://m.youtube.com/"}, |
| 78 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"}, | 80 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"}, |
| 79 }, | 81 }, |
| 80 kChromium{ | 82 kChromium{ |
| 81 {kTitle, "The Chromium Project"}, | 83 {kTitle, "The Chromium Project"}, |
| 82 {kUrl, "https://www.chromium.org/"}, | 84 {kUrl, "https://www.chromium.org/"}, |
| 83 {kFaviconUrl, "https://www.chromium.org/favicon.ico"}, | 85 {kFaviconUrl, "https://www.chromium.org/favicon.ico"}, |
| 84 }, | 86 }, |
| 85 worker_pool_owner_(2, "PopularSitesTest."), | 87 worker_pool_owner_(2, "PopularSitesTest."), |
| 88 prefs_(new sync_preferences::TestingPrefServiceSyncable()), |
| 86 url_fetcher_factory_(nullptr) { | 89 url_fetcher_factory_(nullptr) { |
| 87 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 90 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 88 switches::kEnableNTPPopularSites); | 91 switches::kEnableNTPPopularSites); |
| 89 PopularSitesImpl::RegisterProfilePrefs(prefs_.registry()); | 92 PopularSitesImpl::RegisterProfilePrefs(prefs_->registry()); |
| 90 CHECK(scoped_cache_dir_.CreateUniqueTempDir()); | 93 CHECK(scoped_cache_dir_.CreateUniqueTempDir()); |
| 91 cache_dir_ = scoped_cache_dir_.GetPath(); | 94 cache_dir_ = scoped_cache_dir_.GetPath(); |
| 92 } | 95 } |
| 93 | 96 |
| 94 void SetCountryAndVersion(const std::string& country, | 97 void SetCountryAndVersion(const std::string& country, |
| 95 const std::string& version) { | 98 const std::string& version) { |
| 96 prefs_.SetString(prefs::kPopularSitesOverrideCountry, country); | 99 prefs_->SetString(prefs::kPopularSitesOverrideCountry, country); |
| 97 prefs_.SetString(prefs::kPopularSitesOverrideVersion, version); | 100 prefs_->SetString(prefs::kPopularSitesOverrideVersion, version); |
| 98 } | 101 } |
| 99 | 102 |
| 100 void RespondWithJSON(const std::string& url, | 103 void RespondWithJSON(const std::string& url, |
| 101 const TestPopularSiteVector& sites) { | 104 const TestPopularSiteVector& sites) { |
| 102 base::ListValue sites_value; | 105 base::ListValue sites_value; |
| 103 for (const TestPopularSite& site : sites) { | 106 for (const TestPopularSite& site : sites) { |
| 104 auto site_value = base::MakeUnique<base::DictionaryValue>(); | 107 auto site_value = base::MakeUnique<base::DictionaryValue>(); |
| 105 for (const std::pair<std::string, std::string>& kv : site) { | 108 for (const std::pair<std::string, std::string>& kv : site) { |
| 106 site_value->SetString(kv.first, kv.second); | 109 site_value->SetString(kv.first, kv.second); |
| 107 } | 110 } |
| 108 sites_value.Append(std::move(site_value)); | 111 sites_value.Append(std::move(site_value)); |
| 109 } | 112 } |
| 110 std::string sites_string; | 113 std::string sites_string; |
| 111 base::JSONWriter::Write(sites_value, &sites_string); | 114 base::JSONWriter::Write(sites_value, &sites_string); |
| 112 url_fetcher_factory_.SetFakeResponse(GURL(url), sites_string, net::HTTP_OK, | 115 url_fetcher_factory_.SetFakeResponse(GURL(url), sites_string, net::HTTP_OK, |
| 113 net::URLRequestStatus::SUCCESS); | 116 net::URLRequestStatus::SUCCESS); |
| 114 } | 117 } |
| 115 | 118 |
| 116 void RespondWithData(const std::string& url, const std::string& data) { | 119 void RespondWithData(const std::string& url, const std::string& data) { |
| 117 url_fetcher_factory_.SetFakeResponse(GURL(url), data, net::HTTP_OK, | 120 url_fetcher_factory_.SetFakeResponse(GURL(url), data, net::HTTP_OK, |
| 118 net::URLRequestStatus::SUCCESS); | 121 net::URLRequestStatus::SUCCESS); |
| 119 } | 122 } |
| 120 | 123 |
| 121 void RespondWith404(const std::string& url) { | 124 void RespondWith404(const std::string& url) { |
| 122 url_fetcher_factory_.SetFakeResponse(GURL(url), "404", net::HTTP_NOT_FOUND, | 125 url_fetcher_factory_.SetFakeResponse(GURL(url), "404", net::HTTP_NOT_FOUND, |
| 123 net::URLRequestStatus::SUCCESS); | 126 net::URLRequestStatus::SUCCESS); |
| 124 } | 127 } |
| 125 | 128 |
| 129 void ReregisterProfilePrefs() { |
| 130 prefs_ = base::MakeUnique<sync_preferences::TestingPrefServiceSyncable>(); |
| 131 PopularSitesImpl::RegisterProfilePrefs(prefs_->registry()); |
| 132 } |
| 133 |
| 126 // Returns an optional bool representing whether the completion callback was | 134 // Returns an optional bool representing whether the completion callback was |
| 127 // called at all, and if yes which was the returned bool value. | 135 // called at all, and if yes which was the returned bool value. |
| 128 base::Optional<bool> FetchPopularSites(bool force_download, | 136 base::Optional<bool> FetchPopularSites(bool force_download, |
| 129 PopularSites::SitesVector* sites) { | 137 PopularSites::SitesVector* sites) { |
| 130 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( | 138 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( |
| 131 new net::TestURLRequestContextGetter( | 139 new net::TestURLRequestContextGetter( |
| 132 base::ThreadTaskRunnerHandle::Get())); | 140 base::ThreadTaskRunnerHandle::Get())); |
| 133 std::unique_ptr<PopularSites> popular_sites = | 141 std::unique_ptr<PopularSites> popular_sites = |
| 134 CreatePopularSites(url_request_context.get()); | 142 CreatePopularSites(url_request_context.get()); |
| 135 | 143 |
| 136 base::RunLoop loop; | 144 base::RunLoop loop; |
| 137 base::Optional<bool> save_success; | 145 base::Optional<bool> save_success; |
| 138 if (popular_sites->MaybeStartFetch( | 146 if (popular_sites->MaybeStartFetch( |
| 139 force_download, base::Bind( | 147 force_download, base::Bind( |
| 140 [](base::Optional<bool>* save_success, | 148 [](base::Optional<bool>* save_success, |
| 141 base::RunLoop* loop, bool success) { | 149 base::RunLoop* loop, bool success) { |
| 142 save_success->emplace(success); | 150 save_success->emplace(success); |
| 143 loop->Quit(); | 151 loop->Quit(); |
| 144 }, | 152 }, |
| 145 &save_success, &loop))) { | 153 &save_success, &loop))) { |
| 146 loop.Run(); | 154 loop.Run(); |
| 147 } | 155 } |
| 148 *sites = popular_sites->sites(); | 156 *sites = popular_sites->sites(); |
| 149 return save_success; | 157 return save_success; |
| 150 } | 158 } |
| 151 | 159 |
| 152 std::unique_ptr<PopularSites> CreatePopularSites( | 160 std::unique_ptr<PopularSites> CreatePopularSites( |
| 153 net::URLRequestContextGetter* context) { | 161 net::URLRequestContextGetter* context) { |
| 154 return base::MakeUnique<PopularSitesImpl>( | 162 return base::MakeUnique<PopularSitesImpl>( |
| 155 worker_pool_owner_.pool().get(), &prefs_, | 163 worker_pool_owner_.pool().get(), prefs_.get(), |
| 156 /*template_url_service=*/nullptr, | 164 /*template_url_service=*/nullptr, |
| 157 /*variations_service=*/nullptr, context, cache_dir_, | 165 /*variations_service=*/nullptr, context, cache_dir_, |
| 158 base::Bind(JsonUnsafeParser::Parse)); | 166 base::Bind(JsonUnsafeParser::Parse)); |
| 159 } | 167 } |
| 160 | 168 |
| 161 const TestPopularSite kWikipedia; | 169 const TestPopularSite kWikipedia; |
| 162 const TestPopularSite kYouTube; | 170 const TestPopularSite kYouTube; |
| 163 const TestPopularSite kChromium; | 171 const TestPopularSite kChromium; |
| 164 | 172 |
| 165 base::MessageLoopForUI ui_loop_; | 173 base::MessageLoopForUI ui_loop_; |
| 166 base::SequencedWorkerPoolOwner worker_pool_owner_; | 174 base::SequencedWorkerPoolOwner worker_pool_owner_; |
| 167 base::ScopedTempDir scoped_cache_dir_; | 175 base::ScopedTempDir scoped_cache_dir_; |
| 168 base::FilePath cache_dir_; | 176 base::FilePath cache_dir_; |
| 169 sync_preferences::TestingPrefServiceSyncable prefs_; | 177 std::unique_ptr<sync_preferences::TestingPrefServiceSyncable> prefs_; |
| 170 net::FakeURLFetcherFactory url_fetcher_factory_; | 178 net::FakeURLFetcherFactory url_fetcher_factory_; |
| 171 }; | 179 }; |
| 172 | 180 |
| 173 TEST_F(PopularSitesTest, ContainsDefaultTilesRightAfterConstruction) { | 181 TEST_F(PopularSitesTest, ContainsDefaultTilesRightAfterConstruction) { |
| 174 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( | 182 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( |
| 175 new net::TestURLRequestContextGetter( | 183 new net::TestURLRequestContextGetter( |
| 176 base::ThreadTaskRunnerHandle::Get())); | 184 base::ThreadTaskRunnerHandle::Get())); |
| 177 | 185 |
| 178 auto popular_sites = CreatePopularSites(url_request_context.get()); | 186 auto popular_sites = CreatePopularSites(url_request_context.get()); |
| 179 EXPECT_THAT(popular_sites->sites().size(), | 187 EXPECT_THAT(popular_sites->sites().size(), |
| 180 Eq(GetNumberOfDefaultPopularSitesForPlatform())); | 188 Eq(GetNumberOfDefaultPopularSitesForPlatform())); |
| 181 } | 189 } |
| 182 | 190 |
| 183 TEST_F(PopularSitesTest, Zasic) { | 191 TEST_F(PopularSitesTest, IsEmptyOnConstructionIfDisabledByTrial) { |
| 192 base::test::ScopedFeatureList override_features; |
| 193 override_features.InitAndDisableFeature(kPopularSitesBakedInContentFeature); |
| 194 ReregisterProfilePrefs(); |
| 195 |
| 196 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( |
| 197 new net::TestURLRequestContextGetter( |
| 198 base::ThreadTaskRunnerHandle::Get())); |
| 199 auto popular_sites = CreatePopularSites(url_request_context.get()); |
| 200 |
| 201 EXPECT_THAT(popular_sites->sites().size(), Eq(0ul)); |
| 202 } |
| 203 |
| 204 TEST_F(PopularSitesTest, ShouldSucceedFetching) { |
| 184 SetCountryAndVersion("ZZ", "9"); | 205 SetCountryAndVersion("ZZ", "9"); |
| 185 RespondWithJSON( | 206 RespondWithJSON( |
| 186 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", | 207 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", |
| 187 {kWikipedia}); | 208 {kWikipedia}); |
| 188 | 209 |
| 189 PopularSites::SitesVector sites; | 210 PopularSites::SitesVector sites; |
| 190 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), | 211 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
| 191 Eq(base::Optional<bool>(true))); | 212 Eq(base::Optional<bool>(true))); |
| 192 | 213 |
| 193 ASSERT_THAT(sites.size(), Eq(1u)); | 214 ASSERT_THAT(sites.size(), Eq(1u)); |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", | 453 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", |
| 433 {kChromium}); | 454 {kChromium}); |
| 434 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), | 455 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
| 435 Eq(base::Optional<bool>(true))); | 456 Eq(base::Optional<bool>(true))); |
| 436 ASSERT_THAT(sites.size(), Eq(1u)); | 457 ASSERT_THAT(sites.size(), Eq(1u)); |
| 437 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); | 458 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); |
| 438 } | 459 } |
| 439 | 460 |
| 440 } // namespace | 461 } // namespace |
| 441 } // namespace ntp_tiles | 462 } // namespace ntp_tiles |
| OLD | NEW |