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

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

Issue 2668943002: provide static popular sites for first run (Closed)
Patch Set: Revert test changes. Construction of PopularSites is always safe. 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 using TestPopularSiteVector = std::vector<TestPopularSite>; 46 using TestPopularSiteVector = std::vector<TestPopularSite>;
47 47
48 ::testing::Matcher<const base::string16&> Str16Eq(const std::string& s) { 48 ::testing::Matcher<const base::string16&> Str16Eq(const std::string& s) {
49 return ::testing::Eq(base::UTF8ToUTF16(s)); 49 return ::testing::Eq(base::UTF8ToUTF16(s));
50 } 50 }
51 51
52 ::testing::Matcher<const GURL&> URLEq(const std::string& s) { 52 ::testing::Matcher<const GURL&> URLEq(const std::string& s) {
53 return ::testing::Eq(GURL(s)); 53 return ::testing::Eq(GURL(s));
54 } 54 }
55 55
56 size_t GetNumberOfDefaultPopularSitesForPlatform() {
57 #if defined(OS_IOS) || defined(OS_ANDROID)
58 return 8ul;
59 #endif
sfiera 2017/02/13 10:58:01 I'd put "return 0" in an #else because it would ot
fhorschig 2017/02/13 14:57:00 Done.
60 return 0;
61 }
62
56 class PopularSitesTest : public ::testing::Test { 63 class PopularSitesTest : public ::testing::Test {
57 protected: 64 protected:
58 PopularSitesTest() 65 PopularSitesTest()
59 : kWikipedia{ 66 : kWikipedia{
60 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"}, 67 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"},
61 {kUrl, "https://zz.m.wikipedia.org/"}, 68 {kUrl, "https://zz.m.wikipedia.org/"},
62 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"}, 69 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"},
63 }, 70 },
64 kYouTube{ 71 kYouTube{
65 {kTitle, "YouTube"}, 72 {kTitle, "YouTube"},
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 net::URLRequestStatus::SUCCESS); 117 net::URLRequestStatus::SUCCESS);
111 } 118 }
112 119
113 // Returns an optional bool representing whether the completion callback was 120 // Returns an optional bool representing whether the completion callback was
114 // called at all, and if yes which was the returned bool value. 121 // called at all, and if yes which was the returned bool value.
115 base::Optional<bool> FetchPopularSites(bool force_download, 122 base::Optional<bool> FetchPopularSites(bool force_download,
116 PopularSites::SitesVector* sites) { 123 PopularSites::SitesVector* sites) {
117 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( 124 scoped_refptr<net::TestURLRequestContextGetter> url_request_context(
118 new net::TestURLRequestContextGetter( 125 new net::TestURLRequestContextGetter(
119 base::ThreadTaskRunnerHandle::Get())); 126 base::ThreadTaskRunnerHandle::Get()));
120 PopularSitesImpl popular_sites(worker_pool_owner_.pool().get(), &prefs_, 127 std::unique_ptr<PopularSites> popular_sites =
121 /*template_url_service=*/nullptr, 128 CreatePopularSites(url_request_context.get());
122 /*variations_service=*/nullptr,
123 url_request_context.get(), cache_dir_,
124 base::Bind(JsonUnsafeParser::Parse));
125 129
126 base::RunLoop loop; 130 base::RunLoop loop;
127 base::Optional<bool> save_success; 131 base::Optional<bool> save_success;
128 if (popular_sites.MaybeStartFetch( 132 if (popular_sites->MaybeStartFetch(
129 force_download, base::Bind( 133 force_download, base::Bind(
130 [](base::Optional<bool>* save_success, 134 [](base::Optional<bool>* save_success,
131 base::RunLoop* loop, bool success) { 135 base::RunLoop* loop, bool success) {
132 save_success->emplace(success); 136 save_success->emplace(success);
133 loop->Quit(); 137 loop->Quit();
134 }, 138 },
135 &save_success, &loop))) { 139 &save_success, &loop))) {
136 loop.Run(); 140 loop.Run();
137 } 141 }
138 *sites = popular_sites.sites(); 142 *sites = popular_sites->sites();
139 return save_success; 143 return save_success;
140 } 144 }
141 145
146 std::unique_ptr<PopularSites> CreatePopularSites(
147 net::URLRequestContextGetter* context) {
148 return base::MakeUnique<PopularSitesImpl>(
149 worker_pool_owner_.pool().get(), &prefs_,
150 /*template_url_service=*/nullptr,
151 /*variations_service=*/nullptr, context, cache_dir_,
152 base::Bind(JsonUnsafeParser::Parse));
153 }
154
142 const TestPopularSite kWikipedia; 155 const TestPopularSite kWikipedia;
143 const TestPopularSite kYouTube; 156 const TestPopularSite kYouTube;
144 const TestPopularSite kChromium; 157 const TestPopularSite kChromium;
145 158
146 base::MessageLoopForUI ui_loop_; 159 base::MessageLoopForUI ui_loop_;
147 base::SequencedWorkerPoolOwner worker_pool_owner_; 160 base::SequencedWorkerPoolOwner worker_pool_owner_;
148 base::ScopedTempDir scoped_cache_dir_; 161 base::ScopedTempDir scoped_cache_dir_;
149 base::FilePath cache_dir_; 162 base::FilePath cache_dir_;
150 sync_preferences::TestingPrefServiceSyncable prefs_; 163 sync_preferences::TestingPrefServiceSyncable prefs_;
151 net::FakeURLFetcherFactory url_fetcher_factory_; 164 net::FakeURLFetcherFactory url_fetcher_factory_;
(...skipping 10 matching lines...) Expand all
162 Eq(base::Optional<bool>(true))); 175 Eq(base::Optional<bool>(true)));
163 176
164 ASSERT_THAT(sites.size(), Eq(1u)); 177 ASSERT_THAT(sites.size(), Eq(1u));
165 EXPECT_THAT(sites[0].title, Str16Eq("Wikipedia, fhta Ph'nglui mglw'nafh")); 178 EXPECT_THAT(sites[0].title, Str16Eq("Wikipedia, fhta Ph'nglui mglw'nafh"));
166 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/")); 179 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/"));
167 EXPECT_THAT(sites[0].large_icon_url, 180 EXPECT_THAT(sites[0].large_icon_url,
168 URLEq("https://zz.m.wikipedia.org/wikipedia.png")); 181 URLEq("https://zz.m.wikipedia.org/wikipedia.png"));
169 EXPECT_THAT(sites[0].favicon_url, URLEq("")); 182 EXPECT_THAT(sites[0].favicon_url, URLEq(""));
170 } 183 }
171 184
185 TEST_F(PopularSitesTest, ContainsDefaultTilesRightAfterConstruction) {
186 scoped_refptr<net::TestURLRequestContextGetter> url_request_context(
187 new net::TestURLRequestContextGetter(
188 base::ThreadTaskRunnerHandle::Get()));
189
190 EXPECT_THAT(CreatePopularSites(url_request_context.get())->sites().size(),
191 Eq(GetNumberOfDefaultPopularSitesForPlatform()));
192 }
193
172 TEST_F(PopularSitesTest, Fallback) { 194 TEST_F(PopularSitesTest, Fallback) {
173 SetCountryAndVersion("ZZ", "9"); 195 SetCountryAndVersion("ZZ", "9");
174 RespondWith404( 196 RespondWith404(
175 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json"); 197 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json");
176 RespondWithJSON( 198 RespondWithJSON(
177 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json", 199 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json",
178 {kYouTube, kChromium}); 200 {kYouTube, kChromium});
179 201
180 PopularSites::SitesVector sites; 202 PopularSites::SitesVector sites;
181 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), 203 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites),
182 Eq(base::Optional<bool>(true))); 204 Eq(base::Optional<bool>(true)));
183 205
184 ASSERT_THAT(sites.size(), Eq(2u)); 206 ASSERT_THAT(sites.size(), Eq(2u));
185 EXPECT_THAT(sites[0].title, Str16Eq("YouTube")); 207 EXPECT_THAT(sites[0].title, Str16Eq("YouTube"));
186 EXPECT_THAT(sites[0].url, URLEq("https://m.youtube.com/")); 208 EXPECT_THAT(sites[0].url, URLEq("https://m.youtube.com/"));
187 EXPECT_THAT(sites[0].large_icon_url, 209 EXPECT_THAT(sites[0].large_icon_url,
188 URLEq("https://s.ytimg.com/apple-touch-icon.png")); 210 URLEq("https://s.ytimg.com/apple-touch-icon.png"));
189 EXPECT_THAT(sites[0].favicon_url, URLEq("")); 211 EXPECT_THAT(sites[0].favicon_url, URLEq(""));
190 EXPECT_THAT(sites[1].title, Str16Eq("The Chromium Project")); 212 EXPECT_THAT(sites[1].title, Str16Eq("The Chromium Project"));
191 EXPECT_THAT(sites[1].url, URLEq("https://www.chromium.org/")); 213 EXPECT_THAT(sites[1].url, URLEq("https://www.chromium.org/"));
192 EXPECT_THAT(sites[1].large_icon_url, URLEq("")); 214 EXPECT_THAT(sites[1].large_icon_url, URLEq(""));
193 EXPECT_THAT(sites[1].favicon_url, 215 EXPECT_THAT(sites[1].favicon_url,
194 URLEq("https://www.chromium.org/favicon.ico")); 216 URLEq("https://www.chromium.org/favicon.ico"));
195 } 217 }
196 218
197 TEST_F(PopularSitesTest, Failure) { 219 TEST_F(PopularSitesTest, PopulatesWithDefaultResoucesOnFailure) {
198 SetCountryAndVersion("ZZ", "9"); 220 SetCountryAndVersion("ZZ", "9");
199 RespondWith404( 221 RespondWith404(
200 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json"); 222 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json");
201 RespondWith404( 223 RespondWith404(
202 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json"); 224 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json");
203 225
204 PopularSites::SitesVector sites; 226 PopularSites::SitesVector sites;
205 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), 227 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites),
206 Eq(base::Optional<bool>(false))); 228 Eq(base::Optional<bool>(false)));
207 ASSERT_THAT(sites, IsEmpty()); 229 EXPECT_THAT(sites.size(), Eq(GetNumberOfDefaultPopularSitesForPlatform()));
230 }
231
232 TEST_F(PopularSitesTest, ProvidesDefaultSitesUntilCallbackReturns) {
233 SetCountryAndVersion("ZZ", "9");
234 RespondWithJSON(
235 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
236 {kWikipedia});
237 scoped_refptr<net::TestURLRequestContextGetter> url_request_context(
238 new net::TestURLRequestContextGetter(
239 base::ThreadTaskRunnerHandle::Get()));
240 std::unique_ptr<PopularSites> popular_sites =
241 CreatePopularSites(url_request_context.get());
242
243 base::RunLoop loop;
244 base::Optional<bool> save_success = false;
245
246 bool callback_was_scheduled = popular_sites->MaybeStartFetch(
247 /*force_download=*/true, base::Bind(
248 [](base::Optional<bool>* save_success,
249 base::RunLoop* loop, bool success) {
250 save_success->emplace(success);
251 loop->Quit();
252 },
253 &save_success, &loop));
254
255 // Assert that callback was scheduled so we can wait for its completion.
256 ASSERT_TRUE(callback_was_scheduled);
257 // There should be 8 default sites as nothing was fetched yet.
258 EXPECT_THAT(popular_sites->sites().size(),
259 Eq(GetNumberOfDefaultPopularSitesForPlatform()));
260
261 loop.Run(); // Wait for the fetch to finish and the callback to return.
262
263 EXPECT_TRUE(save_success.value());
264 // The 1 fetched site should replace the default sites.
265 EXPECT_THAT(popular_sites->sites().size(), Eq(1ul));
208 } 266 }
209 267
210 TEST_F(PopularSitesTest, ClearsCacheFileFromOldVersions) { 268 TEST_F(PopularSitesTest, ClearsCacheFileFromOldVersions) {
211 SetCountryAndVersion("ZZ", "9"); 269 SetCountryAndVersion("ZZ", "9");
212 RespondWithJSON( 270 RespondWithJSON(
213 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", 271 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
214 {kWikipedia}); 272 {kWikipedia});
215 273
216 PopularSites::SitesVector sites; 274 PopularSites::SitesVector sites;
217 const base::FilePath old_cache_path = 275 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", 410 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
353 {kChromium}); 411 {kChromium});
354 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), 412 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites),
355 Eq(base::Optional<bool>(true))); 413 Eq(base::Optional<bool>(true)));
356 ASSERT_THAT(sites.size(), Eq(1u)); 414 ASSERT_THAT(sites.size(), Eq(1u));
357 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); 415 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/"));
358 } 416 }
359 417
360 } // namespace 418 } // namespace
361 } // namespace ntp_tiles 419 } // namespace ntp_tiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698