| 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" | |
| 15 #include "base/files/important_file_writer.h" | |
| 16 #include "base/files/scoped_temp_dir.h" | |
| 17 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 18 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 19 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 20 #include "base/optional.h" | 17 #include "base/optional.h" |
| 21 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 22 #include "base/test/scoped_feature_list.h" | 19 #include "base/test/scoped_feature_list.h" |
| 23 #include "base/test/sequenced_worker_pool_owner.h" | |
| 24 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
| 25 #include "base/values.h" | 21 #include "base/values.h" |
| 26 #include "components/ntp_tiles/constants.h" | 22 #include "components/ntp_tiles/constants.h" |
| 27 #include "components/ntp_tiles/json_unsafe_parser.h" | 23 #include "components/ntp_tiles/json_unsafe_parser.h" |
| 28 #include "components/ntp_tiles/pref_names.h" | 24 #include "components/ntp_tiles/pref_names.h" |
| 29 #include "components/ntp_tiles/switches.h" | 25 #include "components/ntp_tiles/switches.h" |
| 30 #include "components/pref_registry/pref_registry_syncable.h" | 26 #include "components/pref_registry/pref_registry_syncable.h" |
| 31 #include "components/sync_preferences/testing_pref_service_syncable.h" | 27 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 32 #include "net/http/http_status_code.h" | 28 #include "net/http/http_status_code.h" |
| 33 #include "net/url_request/test_url_fetcher_factory.h" | 29 #include "net/url_request/test_url_fetcher_factory.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 kYouTube{ | 74 kYouTube{ |
| 79 {kTitle, "YouTube"}, | 75 {kTitle, "YouTube"}, |
| 80 {kUrl, "https://m.youtube.com/"}, | 76 {kUrl, "https://m.youtube.com/"}, |
| 81 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"}, | 77 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"}, |
| 82 }, | 78 }, |
| 83 kChromium{ | 79 kChromium{ |
| 84 {kTitle, "The Chromium Project"}, | 80 {kTitle, "The Chromium Project"}, |
| 85 {kUrl, "https://www.chromium.org/"}, | 81 {kUrl, "https://www.chromium.org/"}, |
| 86 {kFaviconUrl, "https://www.chromium.org/favicon.ico"}, | 82 {kFaviconUrl, "https://www.chromium.org/favicon.ico"}, |
| 87 }, | 83 }, |
| 88 worker_pool_owner_(2, "PopularSitesTest."), | |
| 89 prefs_(new sync_preferences::TestingPrefServiceSyncable()), | 84 prefs_(new sync_preferences::TestingPrefServiceSyncable()), |
| 90 url_fetcher_factory_(nullptr) { | 85 url_fetcher_factory_(nullptr) { |
| 91 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 86 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 92 switches::kEnableNTPPopularSites); | 87 switches::kEnableNTPPopularSites); |
| 93 PopularSitesImpl::RegisterProfilePrefs(prefs_->registry()); | 88 PopularSitesImpl::RegisterProfilePrefs(prefs_->registry()); |
| 94 CHECK(scoped_cache_dir_.CreateUniqueTempDir()); | |
| 95 cache_dir_ = scoped_cache_dir_.GetPath(); | |
| 96 } | 89 } |
| 97 | 90 |
| 98 void SetCountryAndVersion(const std::string& country, | 91 void SetCountryAndVersion(const std::string& country, |
| 99 const std::string& version) { | 92 const std::string& version) { |
| 100 prefs_->SetString(prefs::kPopularSitesOverrideCountry, country); | 93 prefs_->SetString(prefs::kPopularSitesOverrideCountry, country); |
| 101 prefs_->SetString(prefs::kPopularSitesOverrideVersion, version); | 94 prefs_->SetString(prefs::kPopularSitesOverrideVersion, version); |
| 102 } | 95 } |
| 103 | 96 |
| 104 void RespondWithJSON(const std::string& url, | 97 void RespondWithJSON(const std::string& url, |
| 105 const TestPopularSiteVector& sites) { | 98 const TestPopularSiteVector& sites) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 &save_success, &loop))) { | 147 &save_success, &loop))) { |
| 155 loop.Run(); | 148 loop.Run(); |
| 156 } | 149 } |
| 157 *sites = popular_sites->sites(); | 150 *sites = popular_sites->sites(); |
| 158 return save_success; | 151 return save_success; |
| 159 } | 152 } |
| 160 | 153 |
| 161 std::unique_ptr<PopularSites> CreatePopularSites( | 154 std::unique_ptr<PopularSites> CreatePopularSites( |
| 162 net::URLRequestContextGetter* context) { | 155 net::URLRequestContextGetter* context) { |
| 163 return base::MakeUnique<PopularSitesImpl>( | 156 return base::MakeUnique<PopularSitesImpl>( |
| 164 worker_pool_owner_.pool().get(), prefs_.get(), | 157 prefs_.get(), |
| 165 /*template_url_service=*/nullptr, | 158 /*template_url_service=*/nullptr, |
| 166 /*variations_service=*/nullptr, context, cache_dir_, | 159 /*variations_service=*/nullptr, context, |
| 167 base::Bind(JsonUnsafeParser::Parse)); | 160 base::Bind(JsonUnsafeParser::Parse)); |
| 168 } | 161 } |
| 169 | 162 |
| 170 const TestPopularSite kWikipedia; | 163 const TestPopularSite kWikipedia; |
| 171 const TestPopularSite kYouTube; | 164 const TestPopularSite kYouTube; |
| 172 const TestPopularSite kChromium; | 165 const TestPopularSite kChromium; |
| 173 | 166 |
| 174 base::MessageLoopForUI ui_loop_; | 167 base::MessageLoopForUI ui_loop_; |
| 175 base::SequencedWorkerPoolOwner worker_pool_owner_; | |
| 176 base::ScopedTempDir scoped_cache_dir_; | |
| 177 base::FilePath cache_dir_; | |
| 178 std::unique_ptr<sync_preferences::TestingPrefServiceSyncable> prefs_; | 168 std::unique_ptr<sync_preferences::TestingPrefServiceSyncable> prefs_; |
| 179 net::FakeURLFetcherFactory url_fetcher_factory_; | 169 net::FakeURLFetcherFactory url_fetcher_factory_; |
| 180 }; | 170 }; |
| 181 | 171 |
| 182 TEST_F(PopularSitesTest, ContainsDefaultTilesRightAfterConstruction) { | 172 TEST_F(PopularSitesTest, ContainsDefaultTilesRightAfterConstruction) { |
| 183 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( | 173 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( |
| 184 new net::TestURLRequestContextGetter( | 174 new net::TestURLRequestContextGetter( |
| 185 base::ThreadTaskRunnerHandle::Get())); | 175 base::ThreadTaskRunnerHandle::Get())); |
| 186 | 176 |
| 187 auto popular_sites = CreatePopularSites(url_request_context.get()); | 177 auto popular_sites = CreatePopularSites(url_request_context.get()); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 EXPECT_THAT(popular_sites->sites().size(), | 292 EXPECT_THAT(popular_sites->sites().size(), |
| 303 Eq(GetNumberOfDefaultPopularSitesForPlatform())); | 293 Eq(GetNumberOfDefaultPopularSitesForPlatform())); |
| 304 | 294 |
| 305 loop.Run(); // Wait for the fetch to finish and the callback to return. | 295 loop.Run(); // Wait for the fetch to finish and the callback to return. |
| 306 | 296 |
| 307 EXPECT_TRUE(save_success.value()); | 297 EXPECT_TRUE(save_success.value()); |
| 308 // The 1 fetched site should replace the default sites. | 298 // The 1 fetched site should replace the default sites. |
| 309 EXPECT_THAT(popular_sites->sites().size(), Eq(1ul)); | 299 EXPECT_THAT(popular_sites->sites().size(), Eq(1ul)); |
| 310 } | 300 } |
| 311 | 301 |
| 312 TEST_F(PopularSitesTest, ClearsCacheFileFromOldVersions) { | |
| 313 SetCountryAndVersion("ZZ", "9"); | |
| 314 RespondWithJSON( | |
| 315 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", | |
| 316 {kWikipedia}); | |
| 317 | |
| 318 PopularSites::SitesVector sites; | |
| 319 const base::FilePath old_cache_path = | |
| 320 cache_dir_.AppendASCII("suggested_sites.json"); | |
| 321 CHECK(base::ImportantFileWriter::WriteFileAtomically(old_cache_path, | |
| 322 "Old cache")); | |
| 323 FetchPopularSites(/*force_download=*/false, &sites); | |
| 324 worker_pool_owner_.pool()->FlushForTesting(); | |
| 325 EXPECT_FALSE(base::PathExists(old_cache_path)); | |
| 326 } | |
| 327 | |
| 328 TEST_F(PopularSitesTest, UsesCachedJson) { | 302 TEST_F(PopularSitesTest, UsesCachedJson) { |
| 329 SetCountryAndVersion("ZZ", "9"); | 303 SetCountryAndVersion("ZZ", "9"); |
| 330 RespondWithJSON( | 304 RespondWithJSON( |
| 331 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", | 305 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", |
| 332 {kWikipedia}); | 306 {kWikipedia}); |
| 333 | 307 |
| 334 // First request succeeds and gets cached. | 308 // First request succeeds and gets cached. |
| 335 PopularSites::SitesVector sites; | 309 PopularSites::SitesVector sites; |
| 336 ASSERT_THAT(FetchPopularSites(/*force_download=*/false, &sites), | 310 ASSERT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
| 337 Eq(base::Optional<bool>(true))); | 311 Eq(base::Optional<bool>(true))); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 | 441 |
| 468 PopularSites::SitesVector sites; | 442 PopularSites::SitesVector sites; |
| 469 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), | 443 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
| 470 Eq(base::Optional<bool>(true))); | 444 Eq(base::Optional<bool>(true))); |
| 471 | 445 |
| 472 EXPECT_THAT(sites.size(), Eq(1u)); | 446 EXPECT_THAT(sites.size(), Eq(1u)); |
| 473 } | 447 } |
| 474 | 448 |
| 475 } // namespace | 449 } // namespace |
| 476 } // namespace ntp_tiles | 450 } // namespace ntp_tiles |
| OLD | NEW |