| 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_command_line.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" |
| 31 #include "components/variations/variations_params_manager.h" |
| 29 #include "net/http/http_status_code.h" | 32 #include "net/http/http_status_code.h" |
| 30 #include "net/url_request/test_url_fetcher_factory.h" | 33 #include "net/url_request/test_url_fetcher_factory.h" |
| 31 #include "net/url_request/url_request_status.h" | 34 #include "net/url_request/url_request_status.h" |
| 32 #include "net/url_request/url_request_test_util.h" | 35 #include "net/url_request/url_request_test_util.h" |
| 33 #include "testing/gmock/include/gmock/gmock.h" | 36 #include "testing/gmock/include/gmock/gmock.h" |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 35 | 38 |
| 36 using testing::Eq; | 39 using testing::Eq; |
| 37 using testing::Gt; | 40 using testing::Gt; |
| 38 using testing::IsEmpty; | 41 using testing::IsEmpty; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 53 } | 56 } |
| 54 | 57 |
| 55 ::testing::Matcher<const GURL&> URLEq(const std::string& s) { | 58 ::testing::Matcher<const GURL&> URLEq(const std::string& s) { |
| 56 return ::testing::Eq(GURL(s)); | 59 return ::testing::Eq(GURL(s)); |
| 57 } | 60 } |
| 58 | 61 |
| 59 size_t GetNumberOfDefaultPopularSitesForPlatform() { | 62 size_t GetNumberOfDefaultPopularSitesForPlatform() { |
| 60 #if defined(OS_ANDROID) || defined(OS_IOS) | 63 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 61 return 8ul; | 64 return 8ul; |
| 62 #else | 65 #else |
| 63 return 0; | 66 return 0ul; |
| 64 #endif | 67 #endif |
| 65 } | 68 } |
| 66 | 69 |
| 67 class PopularSitesTest : public ::testing::Test { | 70 class PopularSitesTest : public ::testing::Test { |
| 68 protected: | 71 protected: |
| 69 PopularSitesTest() | 72 PopularSitesTest() |
| 70 : kWikipedia{ | 73 : kWikipedia{ |
| 71 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"}, | 74 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"}, |
| 72 {kUrl, "https://zz.m.wikipedia.org/"}, | 75 {kUrl, "https://zz.m.wikipedia.org/"}, |
| 73 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"}, | 76 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"}, |
| 74 }, | 77 }, |
| 75 kYouTube{ | 78 kYouTube{ |
| 76 {kTitle, "YouTube"}, | 79 {kTitle, "YouTube"}, |
| 77 {kUrl, "https://m.youtube.com/"}, | 80 {kUrl, "https://m.youtube.com/"}, |
| 78 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"}, | 81 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"}, |
| 79 }, | 82 }, |
| 80 kChromium{ | 83 kChromium{ |
| 81 {kTitle, "The Chromium Project"}, | 84 {kTitle, "The Chromium Project"}, |
| 82 {kUrl, "https://www.chromium.org/"}, | 85 {kUrl, "https://www.chromium.org/"}, |
| 83 {kFaviconUrl, "https://www.chromium.org/favicon.ico"}, | 86 {kFaviconUrl, "https://www.chromium.org/favicon.ico"}, |
| 84 }, | 87 }, |
| 85 worker_pool_owner_(2, "PopularSitesTest."), | 88 worker_pool_owner_(2, "PopularSitesTest."), |
| 89 prefs_(new sync_preferences::TestingPrefServiceSyncable()), |
| 86 url_fetcher_factory_(nullptr) { | 90 url_fetcher_factory_(nullptr) { |
| 87 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 91 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 88 switches::kEnableNTPPopularSites); | 92 switches::kEnableNTPPopularSites); |
| 89 PopularSitesImpl::RegisterProfilePrefs(prefs_.registry()); | 93 PopularSitesImpl::RegisterProfilePrefs(prefs_->registry()); |
| 90 CHECK(scoped_cache_dir_.CreateUniqueTempDir()); | 94 CHECK(scoped_cache_dir_.CreateUniqueTempDir()); |
| 91 cache_dir_ = scoped_cache_dir_.GetPath(); | 95 cache_dir_ = scoped_cache_dir_.GetPath(); |
| 92 } | 96 } |
| 93 | 97 |
| 94 void SetCountryAndVersion(const std::string& country, | 98 void SetCountryAndVersion(const std::string& country, |
| 95 const std::string& version) { | 99 const std::string& version) { |
| 96 prefs_.SetString(prefs::kPopularSitesOverrideCountry, country); | 100 prefs_->SetString(prefs::kPopularSitesOverrideCountry, country); |
| 97 prefs_.SetString(prefs::kPopularSitesOverrideVersion, version); | 101 prefs_->SetString(prefs::kPopularSitesOverrideVersion, version); |
| 98 } | 102 } |
| 99 | 103 |
| 100 void RespondWithJSON(const std::string& url, | 104 void RespondWithJSON(const std::string& url, |
| 101 const TestPopularSiteVector& sites) { | 105 const TestPopularSiteVector& sites) { |
| 102 base::ListValue sites_value; | 106 base::ListValue sites_value; |
| 103 for (const TestPopularSite& site : sites) { | 107 for (const TestPopularSite& site : sites) { |
| 104 auto site_value = base::MakeUnique<base::DictionaryValue>(); | 108 auto site_value = base::MakeUnique<base::DictionaryValue>(); |
| 105 for (const std::pair<std::string, std::string>& kv : site) { | 109 for (const std::pair<std::string, std::string>& kv : site) { |
| 106 site_value->SetString(kv.first, kv.second); | 110 site_value->SetString(kv.first, kv.second); |
| 107 } | 111 } |
| 108 sites_value.Append(std::move(site_value)); | 112 sites_value.Append(std::move(site_value)); |
| 109 } | 113 } |
| 110 std::string sites_string; | 114 std::string sites_string; |
| 111 base::JSONWriter::Write(sites_value, &sites_string); | 115 base::JSONWriter::Write(sites_value, &sites_string); |
| 112 url_fetcher_factory_.SetFakeResponse(GURL(url), sites_string, net::HTTP_OK, | 116 url_fetcher_factory_.SetFakeResponse(GURL(url), sites_string, net::HTTP_OK, |
| 113 net::URLRequestStatus::SUCCESS); | 117 net::URLRequestStatus::SUCCESS); |
| 114 } | 118 } |
| 115 | 119 |
| 116 void RespondWithData(const std::string& url, const std::string& data) { | 120 void RespondWithData(const std::string& url, const std::string& data) { |
| 117 url_fetcher_factory_.SetFakeResponse(GURL(url), data, net::HTTP_OK, | 121 url_fetcher_factory_.SetFakeResponse(GURL(url), data, net::HTTP_OK, |
| 118 net::URLRequestStatus::SUCCESS); | 122 net::URLRequestStatus::SUCCESS); |
| 119 } | 123 } |
| 120 | 124 |
| 121 void RespondWith404(const std::string& url) { | 125 void RespondWith404(const std::string& url) { |
| 122 url_fetcher_factory_.SetFakeResponse(GURL(url), "404", net::HTTP_NOT_FOUND, | 126 url_fetcher_factory_.SetFakeResponse(GURL(url), "404", net::HTTP_NOT_FOUND, |
| 123 net::URLRequestStatus::SUCCESS); | 127 net::URLRequestStatus::SUCCESS); |
| 124 } | 128 } |
| 125 | 129 |
| 130 void ReregisterProfilePrefs() { |
| 131 prefs_ = base::MakeUnique<sync_preferences::TestingPrefServiceSyncable>(); |
| 132 PopularSitesImpl::RegisterProfilePrefs(prefs_->registry()); |
| 133 } |
| 134 |
| 126 // Returns an optional bool representing whether the completion callback was | 135 // Returns an optional bool representing whether the completion callback was |
| 127 // called at all, and if yes which was the returned bool value. | 136 // called at all, and if yes which was the returned bool value. |
| 128 base::Optional<bool> FetchPopularSites(bool force_download, | 137 base::Optional<bool> FetchPopularSites(bool force_download, |
| 129 PopularSites::SitesVector* sites) { | 138 PopularSites::SitesVector* sites) { |
| 130 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( | 139 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( |
| 131 new net::TestURLRequestContextGetter( | 140 new net::TestURLRequestContextGetter( |
| 132 base::ThreadTaskRunnerHandle::Get())); | 141 base::ThreadTaskRunnerHandle::Get())); |
| 133 std::unique_ptr<PopularSites> popular_sites = | 142 std::unique_ptr<PopularSites> popular_sites = |
| 134 CreatePopularSites(url_request_context.get()); | 143 CreatePopularSites(url_request_context.get()); |
| 135 | 144 |
| 136 base::RunLoop loop; | 145 base::RunLoop loop; |
| 137 base::Optional<bool> save_success; | 146 base::Optional<bool> save_success; |
| 138 if (popular_sites->MaybeStartFetch( | 147 if (popular_sites->MaybeStartFetch( |
| 139 force_download, base::Bind( | 148 force_download, base::Bind( |
| 140 [](base::Optional<bool>* save_success, | 149 [](base::Optional<bool>* save_success, |
| 141 base::RunLoop* loop, bool success) { | 150 base::RunLoop* loop, bool success) { |
| 142 save_success->emplace(success); | 151 save_success->emplace(success); |
| 143 loop->Quit(); | 152 loop->Quit(); |
| 144 }, | 153 }, |
| 145 &save_success, &loop))) { | 154 &save_success, &loop))) { |
| 146 loop.Run(); | 155 loop.Run(); |
| 147 } | 156 } |
| 148 *sites = popular_sites->sites(); | 157 *sites = popular_sites->sites(); |
| 149 return save_success; | 158 return save_success; |
| 150 } | 159 } |
| 151 | 160 |
| 152 std::unique_ptr<PopularSites> CreatePopularSites( | 161 std::unique_ptr<PopularSites> CreatePopularSites( |
| 153 net::URLRequestContextGetter* context) { | 162 net::URLRequestContextGetter* context) { |
| 154 return base::MakeUnique<PopularSitesImpl>( | 163 return base::MakeUnique<PopularSitesImpl>( |
| 155 worker_pool_owner_.pool().get(), &prefs_, | 164 worker_pool_owner_.pool().get(), prefs_.get(), |
| 156 /*template_url_service=*/nullptr, | 165 /*template_url_service=*/nullptr, |
| 157 /*variations_service=*/nullptr, context, cache_dir_, | 166 /*variations_service=*/nullptr, context, cache_dir_, |
| 158 base::Bind(JsonUnsafeParser::Parse)); | 167 base::Bind(JsonUnsafeParser::Parse)); |
| 159 } | 168 } |
| 160 | 169 |
| 161 const TestPopularSite kWikipedia; | 170 const TestPopularSite kWikipedia; |
| 162 const TestPopularSite kYouTube; | 171 const TestPopularSite kYouTube; |
| 163 const TestPopularSite kChromium; | 172 const TestPopularSite kChromium; |
| 164 | 173 |
| 165 base::MessageLoopForUI ui_loop_; | 174 base::MessageLoopForUI ui_loop_; |
| 166 base::SequencedWorkerPoolOwner worker_pool_owner_; | 175 base::SequencedWorkerPoolOwner worker_pool_owner_; |
| 167 base::ScopedTempDir scoped_cache_dir_; | 176 base::ScopedTempDir scoped_cache_dir_; |
| 168 base::FilePath cache_dir_; | 177 base::FilePath cache_dir_; |
| 169 sync_preferences::TestingPrefServiceSyncable prefs_; | 178 std::unique_ptr<sync_preferences::TestingPrefServiceSyncable> prefs_; |
| 170 net::FakeURLFetcherFactory url_fetcher_factory_; | 179 net::FakeURLFetcherFactory url_fetcher_factory_; |
| 171 }; | 180 }; |
| 172 | 181 |
| 173 TEST_F(PopularSitesTest, ContainsDefaultTilesRightAfterConstruction) { | 182 TEST_F(PopularSitesTest, ContainsDefaultTilesRightAfterConstruction) { |
| 174 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( | 183 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( |
| 175 new net::TestURLRequestContextGetter( | 184 new net::TestURLRequestContextGetter( |
| 176 base::ThreadTaskRunnerHandle::Get())); | 185 base::ThreadTaskRunnerHandle::Get())); |
| 177 | 186 |
| 178 auto popular_sites = CreatePopularSites(url_request_context.get()); | 187 auto popular_sites = CreatePopularSites(url_request_context.get()); |
| 179 EXPECT_THAT(popular_sites->sites().size(), | 188 EXPECT_THAT(popular_sites->sites().size(), |
| 180 Eq(GetNumberOfDefaultPopularSitesForPlatform())); | 189 Eq(GetNumberOfDefaultPopularSitesForPlatform())); |
| 181 } | 190 } |
| 182 | 191 |
| 183 TEST_F(PopularSitesTest, Zasic) { | 192 TEST_F(PopularSitesTest, IsEmptyOnConstructionIfDisabledByFlag) { |
| 193 // If both switches are present, the disable flag overrides the enable flag. |
| 194 base::test::ScopedCommandLine command_line; |
| 195 command_line.GetProcessCommandLine()->AppendSwitch( |
| 196 switches::kEnableNTPBakedInSites); |
| 197 command_line.GetProcessCommandLine()->AppendSwitch( |
| 198 switches::kDisableNTPBakedInSites); |
| 199 ReregisterProfilePrefs(); |
| 200 |
| 201 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( |
| 202 new net::TestURLRequestContextGetter( |
| 203 base::ThreadTaskRunnerHandle::Get())); |
| 204 auto popular_sites = CreatePopularSites(url_request_context.get()); |
| 205 |
| 206 EXPECT_THAT(popular_sites->sites().size(), Eq(0ul)); |
| 207 } |
| 208 |
| 209 TEST_F(PopularSitesTest, IsEmptyOnConstructionIfDisabledByTrial) { |
| 210 variations::testing::VariationParamsManager variations_params( |
| 211 ntp_tiles::kPopularSitesFieldTrialName, |
| 212 {{ntp_tiles::kEnableBakedInSitesParamName, "false"}}); |
| 213 ReregisterProfilePrefs(); |
| 214 |
| 215 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( |
| 216 new net::TestURLRequestContextGetter( |
| 217 base::ThreadTaskRunnerHandle::Get())); |
| 218 auto popular_sites = CreatePopularSites(url_request_context.get()); |
| 219 |
| 220 EXPECT_THAT(popular_sites->sites().size(), Eq(0ul)); |
| 221 } |
| 222 |
| 223 TEST_F(PopularSitesTest, ShouldSucceedFetching) { |
| 184 SetCountryAndVersion("ZZ", "9"); | 224 SetCountryAndVersion("ZZ", "9"); |
| 185 RespondWithJSON( | 225 RespondWithJSON( |
| 186 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", | 226 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", |
| 187 {kWikipedia}); | 227 {kWikipedia}); |
| 188 | 228 |
| 189 PopularSites::SitesVector sites; | 229 PopularSites::SitesVector sites; |
| 190 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), | 230 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
| 191 Eq(base::Optional<bool>(true))); | 231 Eq(base::Optional<bool>(true))); |
| 192 | 232 |
| 193 ASSERT_THAT(sites.size(), Eq(1u)); | 233 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", | 472 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", |
| 433 {kChromium}); | 473 {kChromium}); |
| 434 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), | 474 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
| 435 Eq(base::Optional<bool>(true))); | 475 Eq(base::Optional<bool>(true))); |
| 436 ASSERT_THAT(sites.size(), Eq(1u)); | 476 ASSERT_THAT(sites.size(), Eq(1u)); |
| 437 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); | 477 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); |
| 438 } | 478 } |
| 439 | 479 |
| 440 } // namespace | 480 } // namespace |
| 441 } // namespace ntp_tiles | 481 } // namespace ntp_tiles |
| OLD | NEW |