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

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

Issue 2668943002: provide static popular sites for first run (Closed)
Patch Set: Move default site definition into resource file Created 3 years, 10 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>
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 net::URLRequestStatus::SUCCESS); 110 net::URLRequestStatus::SUCCESS);
111 } 111 }
112 112
113 // Returns an optional bool representing whether the completion callback was 113 // Returns an optional bool representing whether the completion callback was
114 // called at all, and if yes which was the returned bool value. 114 // called at all, and if yes which was the returned bool value.
115 base::Optional<bool> FetchPopularSites(bool force_download, 115 base::Optional<bool> FetchPopularSites(bool force_download,
116 PopularSites::SitesVector* sites) { 116 PopularSites::SitesVector* sites) {
117 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( 117 scoped_refptr<net::TestURLRequestContextGetter> url_request_context(
118 new net::TestURLRequestContextGetter( 118 new net::TestURLRequestContextGetter(
119 base::ThreadTaskRunnerHandle::Get())); 119 base::ThreadTaskRunnerHandle::Get()));
120 PopularSitesImpl popular_sites(worker_pool_owner_.pool().get(), &prefs_, 120 std::unique_ptr<PopularSites> popular_sites =
121 /*template_url_service=*/nullptr, 121 CreatePopularSites(url_request_context.get());
122 /*variations_service=*/nullptr,
123 url_request_context.get(), cache_dir_,
124 base::Bind(JsonUnsafeParser::Parse));
125 122
126 base::RunLoop loop; 123 base::RunLoop loop;
127 base::Optional<bool> save_success; 124 base::Optional<bool> save_success;
128 if (popular_sites.MaybeStartFetch( 125 if (popular_sites->MaybeStartFetch(
129 force_download, base::Bind( 126 force_download, base::Bind(
130 [](base::Optional<bool>* save_success, 127 [](base::Optional<bool>* save_success,
131 base::RunLoop* loop, bool success) { 128 base::RunLoop* loop, bool success) {
132 save_success->emplace(success); 129 save_success->emplace(success);
133 loop->Quit(); 130 loop->Quit();
134 }, 131 },
135 &save_success, &loop))) { 132 &save_success, &loop))) {
136 loop.Run(); 133 loop.Run();
137 } 134 }
138 *sites = popular_sites.sites(); 135 *sites = popular_sites->sites();
139 return save_success; 136 return save_success;
140 } 137 }
141 138
139 std::unique_ptr<PopularSites> CreatePopularSites(
140 net::URLRequestContextGetter* context) {
141 return base::MakeUnique<PopularSitesImpl>(
142 worker_pool_owner_.pool().get(), &prefs_,
143 /*template_url_service=*/nullptr,
144 /*variations_service=*/nullptr, context, cache_dir_,
145 base::Bind(JsonUnsafeParser::Parse));
146 }
147
142 const TestPopularSite kWikipedia; 148 const TestPopularSite kWikipedia;
143 const TestPopularSite kYouTube; 149 const TestPopularSite kYouTube;
144 const TestPopularSite kChromium; 150 const TestPopularSite kChromium;
145 151
146 base::MessageLoopForUI ui_loop_; 152 base::MessageLoopForUI ui_loop_;
147 base::SequencedWorkerPoolOwner worker_pool_owner_; 153 base::SequencedWorkerPoolOwner worker_pool_owner_;
148 base::ScopedTempDir scoped_cache_dir_; 154 base::ScopedTempDir scoped_cache_dir_;
149 base::FilePath cache_dir_; 155 base::FilePath cache_dir_;
150 sync_preferences::TestingPrefServiceSyncable prefs_; 156 sync_preferences::TestingPrefServiceSyncable prefs_;
151 net::FakeURLFetcherFactory url_fetcher_factory_; 157 net::FakeURLFetcherFactory url_fetcher_factory_;
152 }; 158 };
153 159
sfiera 2017/02/09 12:39:06 I think I'd like to see a test of what sites() ret
fhorschig 2017/02/09 15:30:18 Good idea, especially when reading prefs in the ct
154 TEST_F(PopularSitesTest, Basic) { 160 TEST_F(PopularSitesTest, Basic) {
155 SetCountryAndVersion("ZZ", "9"); 161 SetCountryAndVersion("ZZ", "9");
156 RespondWithJSON( 162 RespondWithJSON(
157 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", 163 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
158 {kWikipedia}); 164 {kWikipedia});
159 165
160 PopularSites::SitesVector sites; 166 PopularSites::SitesVector sites;
161 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), 167 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites),
162 Eq(base::Optional<bool>(true))); 168 Eq(base::Optional<bool>(true)));
163 169
(...skipping 23 matching lines...) Expand all
187 EXPECT_THAT(sites[0].large_icon_url, 193 EXPECT_THAT(sites[0].large_icon_url,
188 URLEq("https://s.ytimg.com/apple-touch-icon.png")); 194 URLEq("https://s.ytimg.com/apple-touch-icon.png"));
189 EXPECT_THAT(sites[0].favicon_url, URLEq("")); 195 EXPECT_THAT(sites[0].favicon_url, URLEq(""));
190 EXPECT_THAT(sites[1].title, Str16Eq("The Chromium Project")); 196 EXPECT_THAT(sites[1].title, Str16Eq("The Chromium Project"));
191 EXPECT_THAT(sites[1].url, URLEq("https://www.chromium.org/")); 197 EXPECT_THAT(sites[1].url, URLEq("https://www.chromium.org/"));
192 EXPECT_THAT(sites[1].large_icon_url, URLEq("")); 198 EXPECT_THAT(sites[1].large_icon_url, URLEq(""));
193 EXPECT_THAT(sites[1].favicon_url, 199 EXPECT_THAT(sites[1].favicon_url,
194 URLEq("https://www.chromium.org/favicon.ico")); 200 URLEq("https://www.chromium.org/favicon.ico"));
195 } 201 }
196 202
197 TEST_F(PopularSitesTest, Failure) { 203 TEST_F(PopularSitesTest, PopulatesWithDefaultResoucesOnFailure) {
198 SetCountryAndVersion("ZZ", "9"); 204 SetCountryAndVersion("ZZ", "9");
199 RespondWith404( 205 RespondWith404(
200 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json"); 206 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json");
201 RespondWith404( 207 RespondWith404(
202 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json"); 208 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json");
203 209
204 PopularSites::SitesVector sites; 210 PopularSites::SitesVector sites;
205 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), 211 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites),
206 Eq(base::Optional<bool>(false))); 212 Eq(base::Optional<bool>(false)));
207 ASSERT_THAT(sites, IsEmpty()); 213 EXPECT_THAT(sites.size(), Eq(8ul));
214 }
215
216 TEST_F(PopularSitesTest, ProvidesDefaultSitesUntilCallbackReturns) {
217 SetCountryAndVersion("ZZ", "9");
218 RespondWithJSON(
219 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
220 {kWikipedia});
221 scoped_refptr<net::TestURLRequestContextGetter> url_request_context(
222 new net::TestURLRequestContextGetter(
223 base::ThreadTaskRunnerHandle::Get()));
224 std::unique_ptr<PopularSites> popular_sites =
225 CreatePopularSites(url_request_context.get());
226
227 base::RunLoop loop;
228 base::Optional<bool> save_success;
229
230 bool callback_was_scheduled = popular_sites->MaybeStartFetch(
231 /*force_download=*/true, base::Bind(
232 [](base::Optional<bool>* save_success,
233 base::RunLoop* loop, bool success) {
234 save_success->emplace(success);
235 loop->Quit();
236 },
237 &save_success, &loop));
238
239 // Assert that callback was scheduled so we can wait for its completion.
240 ASSERT_TRUE(callback_was_scheduled);
241 // There should be 8 default sites as nothing was fetched yet.
242 EXPECT_THAT(popular_sites->sites().size(), Eq(8ul));
243
244 loop.Run(); // Wait for the fetch to finish and the callback to return.
245
246 ASSERT_TRUE(save_success.has_value());
247 EXPECT_TRUE(save_success.value());
248 // The 1 fetched site should replace the default sites.
249 EXPECT_THAT(popular_sites->sites().size(), Eq(1ul));
208 } 250 }
209 251
210 TEST_F(PopularSitesTest, ClearsCacheFileFromOldVersions) { 252 TEST_F(PopularSitesTest, ClearsCacheFileFromOldVersions) {
211 SetCountryAndVersion("ZZ", "9"); 253 SetCountryAndVersion("ZZ", "9");
212 RespondWithJSON( 254 RespondWithJSON(
213 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", 255 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
214 {kWikipedia}); 256 {kWikipedia});
215 257
216 PopularSites::SitesVector sites; 258 PopularSites::SitesVector sites;
217 const base::FilePath old_cache_path = 259 const base::FilePath old_cache_path =
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", 394 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
353 {kChromium}); 395 {kChromium});
354 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), 396 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites),
355 Eq(base::Optional<bool>(true))); 397 Eq(base::Optional<bool>(true)));
356 ASSERT_THAT(sites.size(), Eq(1u)); 398 ASSERT_THAT(sites.size(), Eq(1u));
357 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); 399 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/"));
358 } 400 }
359 401
360 } // namespace 402 } // namespace
361 } // namespace ntp_tiles 403 } // namespace ntp_tiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698