Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: components/ntp_tiles/popular_sites_impl_unittest.cc

Issue 2946143002: Remove legacy popular sites cache cleaning (Closed)
Patch Set: Fix factory on iOS Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
mastiz 2017/06/21 04:44:22 Same for the two above.
fhorschig 2017/06/23 18:11:23 Gone.
16 #include "base/files/scoped_temp_dir.h"
17 #include "base/json/json_writer.h" 16 #include "base/json/json_writer.h"
18 #include "base/memory/ptr_util.h" 17 #include "base/memory/ptr_util.h"
19 #include "base/message_loop/message_loop.h" 18 #include "base/message_loop/message_loop.h"
20 #include "base/optional.h" 19 #include "base/optional.h"
21 #include "base/run_loop.h" 20 #include "base/run_loop.h"
22 #include "base/test/scoped_feature_list.h" 21 #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" 22 #include "base/threading/thread_task_runner_handle.h"
25 #include "base/values.h" 23 #include "base/values.h"
26 #include "components/ntp_tiles/constants.h" 24 #include "components/ntp_tiles/constants.h"
27 #include "components/ntp_tiles/json_unsafe_parser.h" 25 #include "components/ntp_tiles/json_unsafe_parser.h"
28 #include "components/ntp_tiles/pref_names.h" 26 #include "components/ntp_tiles/pref_names.h"
29 #include "components/ntp_tiles/switches.h" 27 #include "components/ntp_tiles/switches.h"
30 #include "components/pref_registry/pref_registry_syncable.h" 28 #include "components/pref_registry/pref_registry_syncable.h"
31 #include "components/sync_preferences/testing_pref_service_syncable.h" 29 #include "components/sync_preferences/testing_pref_service_syncable.h"
32 #include "net/http/http_status_code.h" 30 #include "net/http/http_status_code.h"
33 #include "net/url_request/test_url_fetcher_factory.h" 31 #include "net/url_request/test_url_fetcher_factory.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 kYouTube{ 76 kYouTube{
79 {kTitle, "YouTube"}, 77 {kTitle, "YouTube"},
80 {kUrl, "https://m.youtube.com/"}, 78 {kUrl, "https://m.youtube.com/"},
81 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"}, 79 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"},
82 }, 80 },
83 kChromium{ 81 kChromium{
84 {kTitle, "The Chromium Project"}, 82 {kTitle, "The Chromium Project"},
85 {kUrl, "https://www.chromium.org/"}, 83 {kUrl, "https://www.chromium.org/"},
86 {kFaviconUrl, "https://www.chromium.org/favicon.ico"}, 84 {kFaviconUrl, "https://www.chromium.org/favicon.ico"},
87 }, 85 },
88 worker_pool_owner_(2, "PopularSitesTest."),
89 prefs_(new sync_preferences::TestingPrefServiceSyncable()), 86 prefs_(new sync_preferences::TestingPrefServiceSyncable()),
90 url_fetcher_factory_(nullptr) { 87 url_fetcher_factory_(nullptr) {
91 base::CommandLine::ForCurrentProcess()->AppendSwitch( 88 base::CommandLine::ForCurrentProcess()->AppendSwitch(
92 switches::kEnableNTPPopularSites); 89 switches::kEnableNTPPopularSites);
93 PopularSitesImpl::RegisterProfilePrefs(prefs_->registry()); 90 PopularSitesImpl::RegisterProfilePrefs(prefs_->registry());
94 CHECK(scoped_cache_dir_.CreateUniqueTempDir());
95 cache_dir_ = scoped_cache_dir_.GetPath();
96 } 91 }
97 92
98 void SetCountryAndVersion(const std::string& country, 93 void SetCountryAndVersion(const std::string& country,
99 const std::string& version) { 94 const std::string& version) {
100 prefs_->SetString(prefs::kPopularSitesOverrideCountry, country); 95 prefs_->SetString(prefs::kPopularSitesOverrideCountry, country);
101 prefs_->SetString(prefs::kPopularSitesOverrideVersion, version); 96 prefs_->SetString(prefs::kPopularSitesOverrideVersion, version);
102 } 97 }
103 98
104 void RespondWithJSON(const std::string& url, 99 void RespondWithJSON(const std::string& url,
105 const TestPopularSiteVector& sites) { 100 const TestPopularSiteVector& sites) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 &save_success, &loop))) { 149 &save_success, &loop))) {
155 loop.Run(); 150 loop.Run();
156 } 151 }
157 *sites = popular_sites->sites(); 152 *sites = popular_sites->sites();
158 return save_success; 153 return save_success;
159 } 154 }
160 155
161 std::unique_ptr<PopularSites> CreatePopularSites( 156 std::unique_ptr<PopularSites> CreatePopularSites(
162 net::URLRequestContextGetter* context) { 157 net::URLRequestContextGetter* context) {
163 return base::MakeUnique<PopularSitesImpl>( 158 return base::MakeUnique<PopularSitesImpl>(
164 worker_pool_owner_.pool().get(), prefs_.get(), 159 prefs_.get(),
165 /*template_url_service=*/nullptr, 160 /*template_url_service=*/nullptr,
166 /*variations_service=*/nullptr, context, cache_dir_, 161 /*variations_service=*/nullptr, context,
167 base::Bind(JsonUnsafeParser::Parse)); 162 base::Bind(JsonUnsafeParser::Parse));
168 } 163 }
169 164
170 const TestPopularSite kWikipedia; 165 const TestPopularSite kWikipedia;
171 const TestPopularSite kYouTube; 166 const TestPopularSite kYouTube;
172 const TestPopularSite kChromium; 167 const TestPopularSite kChromium;
173 168
174 base::MessageLoopForUI ui_loop_; 169 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_; 170 std::unique_ptr<sync_preferences::TestingPrefServiceSyncable> prefs_;
179 net::FakeURLFetcherFactory url_fetcher_factory_; 171 net::FakeURLFetcherFactory url_fetcher_factory_;
180 }; 172 };
181 173
182 TEST_F(PopularSitesTest, ContainsDefaultTilesRightAfterConstruction) { 174 TEST_F(PopularSitesTest, ContainsDefaultTilesRightAfterConstruction) {
183 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( 175 scoped_refptr<net::TestURLRequestContextGetter> url_request_context(
184 new net::TestURLRequestContextGetter( 176 new net::TestURLRequestContextGetter(
185 base::ThreadTaskRunnerHandle::Get())); 177 base::ThreadTaskRunnerHandle::Get()));
186 178
187 auto popular_sites = CreatePopularSites(url_request_context.get()); 179 auto popular_sites = CreatePopularSites(url_request_context.get());
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 EXPECT_THAT(popular_sites->sites().size(), 294 EXPECT_THAT(popular_sites->sites().size(),
303 Eq(GetNumberOfDefaultPopularSitesForPlatform())); 295 Eq(GetNumberOfDefaultPopularSitesForPlatform()));
304 296
305 loop.Run(); // Wait for the fetch to finish and the callback to return. 297 loop.Run(); // Wait for the fetch to finish and the callback to return.
306 298
307 EXPECT_TRUE(save_success.value()); 299 EXPECT_TRUE(save_success.value());
308 // The 1 fetched site should replace the default sites. 300 // The 1 fetched site should replace the default sites.
309 EXPECT_THAT(popular_sites->sites().size(), Eq(1ul)); 301 EXPECT_THAT(popular_sites->sites().size(), Eq(1ul));
310 } 302 }
311 303
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) { 304 TEST_F(PopularSitesTest, UsesCachedJson) {
329 SetCountryAndVersion("ZZ", "9"); 305 SetCountryAndVersion("ZZ", "9");
330 RespondWithJSON( 306 RespondWithJSON(
331 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", 307 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
332 {kWikipedia}); 308 {kWikipedia});
333 309
334 // First request succeeds and gets cached. 310 // First request succeeds and gets cached.
335 PopularSites::SitesVector sites; 311 PopularSites::SitesVector sites;
336 ASSERT_THAT(FetchPopularSites(/*force_download=*/false, &sites), 312 ASSERT_THAT(FetchPopularSites(/*force_download=*/false, &sites),
337 Eq(base::Optional<bool>(true))); 313 Eq(base::Optional<bool>(true)));
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 443
468 PopularSites::SitesVector sites; 444 PopularSites::SitesVector sites;
469 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), 445 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites),
470 Eq(base::Optional<bool>(true))); 446 Eq(base::Optional<bool>(true)));
471 447
472 EXPECT_THAT(sites.size(), Eq(1u)); 448 EXPECT_THAT(sites.size(), Eq(1u));
473 } 449 }
474 450
475 } // namespace 451 } // namespace
476 } // namespace ntp_tiles 452 } // namespace ntp_tiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698