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

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

Issue 2485923002: Add some more tests of PopularSites. (Closed)
Patch Set: Created 4 years, 1 month 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.h" 5 #include "components/ntp_tiles/popular_sites.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 20 matching lines...) Expand all
31 using testing::StrEq; 31 using testing::StrEq;
32 32
33 namespace ntp_tiles { 33 namespace ntp_tiles {
34 namespace { 34 namespace {
35 35
36 const char kTitle[] = "title"; 36 const char kTitle[] = "title";
37 const char kUrl[] = "url"; 37 const char kUrl[] = "url";
38 const char kLargeIconUrl[] = "large_icon_url"; 38 const char kLargeIconUrl[] = "large_icon_url";
39 const char kFaviconUrl[] = "favicon_url"; 39 const char kFaviconUrl[] = "favicon_url";
40 40
41 using TestPopularSite = std::vector<std::pair<std::string, std::string>>; 41 using TestPopularSite = std::map<std::string, std::string>;
42 using TestPopularSiteVector = std::vector<TestPopularSite>; 42 using TestPopularSiteVector = std::vector<TestPopularSite>;
43 43
44 ::testing::Matcher<const base::string16&> Str16Eq(const std::string& s) { 44 ::testing::Matcher<const base::string16&> Str16Eq(const std::string& s) {
45 return ::testing::Eq(base::UTF8ToUTF16(s)); 45 return ::testing::Eq(base::UTF8ToUTF16(s));
46 } 46 }
47 47
48 ::testing::Matcher<const GURL&> URLEq(const std::string& s) { 48 ::testing::Matcher<const GURL&> URLEq(const std::string& s) {
49 return ::testing::Eq(GURL(s)); 49 return ::testing::Eq(GURL(s));
50 } 50 }
51 51
(...skipping 29 matching lines...) Expand all
81 }, 81 },
82 unsafe_json, success_callback, error_callback)); 82 unsafe_json, success_callback, error_callback));
83 } 83 }
84 84
85 JsonUnsafeParser() = delete; 85 JsonUnsafeParser() = delete;
86 }; 86 };
87 87
88 class PopularSitesTest : public ::testing::Test { 88 class PopularSitesTest : public ::testing::Test {
89 protected: 89 protected:
90 PopularSitesTest() 90 PopularSitesTest()
91 : worker_pool_owner_(2, "PopularSitesTest."), 91 : kWikipedia{
92 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"},
Marc Treib 2016/11/08 11:27:30 I feel like I'm missing a reference here...? :D
sfiera 2016/11/08 13:00:35 I'm using the country code "ZZ" which is guarantee
93 {kUrl, "https://zz.m.wikipedia.org/"},
94 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"},
95 },
96 kYouTube{
97 {kTitle, "YouTube"},
98 {kUrl, "https://m.youtube.com/"},
99 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"},
100 },
101 kChromium{
102 {kTitle, "The Chromium Project"},
103 {kUrl, "https://www.chromium.org/"},
104 {kFaviconUrl, "https://www.chromium.org/favicon.ico"},
105 },
106 worker_pool_owner_(2, "PopularSitesTest."),
92 url_fetcher_factory_(nullptr) { 107 url_fetcher_factory_(nullptr) {
93 PopularSites::RegisterProfilePrefs(prefs_.registry()); 108 PopularSites::RegisterProfilePrefs(prefs_.registry());
94 CHECK(cache_dir_.CreateUniqueTempDir()); 109 CHECK(scoped_cache_dir_.CreateUniqueTempDir());
110 cache_dir_ = scoped_cache_dir_.GetPath();
95 } 111 }
96 112
97 void SetCountryAndVersion(const std::string& country, 113 void SetCountryAndVersion(const std::string& country,
98 const std::string& version) { 114 const std::string& version) {
99 prefs_.SetString(prefs::kPopularSitesOverrideCountry, country); 115 prefs_.SetString(prefs::kPopularSitesOverrideCountry, country);
100 prefs_.SetString(prefs::kPopularSitesOverrideVersion, version); 116 prefs_.SetString(prefs::kPopularSitesOverrideVersion, version);
101 } 117 }
102 118
103 void RespondWithJSON(const std::string& url, 119 void RespondWithJSON(const std::string& url,
104 const TestPopularSiteVector& sites) { 120 const TestPopularSiteVector& sites) {
105 base::ListValue sites_value; 121 base::ListValue sites_value;
106 for (const TestPopularSite& site : sites) { 122 for (const TestPopularSite& site : sites) {
107 auto site_value = base::MakeUnique<base::DictionaryValue>(); 123 auto site_value = base::MakeUnique<base::DictionaryValue>();
108 for (const std::pair<std::string, std::string>& kv : site) { 124 for (const std::pair<std::string, std::string>& kv : site) {
109 site_value->SetString(kv.first, kv.second); 125 site_value->SetString(kv.first, kv.second);
110 } 126 }
111 sites_value.Append(std::move(site_value)); 127 sites_value.Append(std::move(site_value));
112 } 128 }
113 std::string sites_string; 129 std::string sites_string;
114 base::JSONWriter::Write(sites_value, &sites_string); 130 base::JSONWriter::Write(sites_value, &sites_string);
115 url_fetcher_factory_.SetFakeResponse(GURL(url), sites_string, net::HTTP_OK, 131 url_fetcher_factory_.SetFakeResponse(GURL(url), sites_string, net::HTTP_OK,
116 net::URLRequestStatus::SUCCESS); 132 net::URLRequestStatus::SUCCESS);
117 } 133 }
118 134
135 void RespondWithData(const std::string& url, const std::string& data) {
136 url_fetcher_factory_.SetFakeResponse(GURL(url), data, net::HTTP_OK,
137 net::URLRequestStatus::SUCCESS);
138 }
139
119 void RespondWith404(const std::string& url) { 140 void RespondWith404(const std::string& url) {
120 url_fetcher_factory_.SetFakeResponse(GURL(url), "404", net::HTTP_NOT_FOUND, 141 url_fetcher_factory_.SetFakeResponse(GURL(url), "404", net::HTTP_NOT_FOUND,
121 net::URLRequestStatus::SUCCESS); 142 net::URLRequestStatus::SUCCESS);
122 } 143 }
123 144
124 bool FetchPopularSites(bool force_download, 145 bool FetchPopularSites(bool force_download,
125 std::vector<PopularSites::Site>* sites) { 146 std::vector<PopularSites::Site>* sites) {
Marc Treib 2016/11/08 11:27:30 optional nit: You could return a base::Optional<ve
126 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( 147 scoped_refptr<net::TestURLRequestContextGetter> url_request_context(
127 new net::TestURLRequestContextGetter( 148 new net::TestURLRequestContextGetter(
128 base::ThreadTaskRunnerHandle::Get())); 149 base::ThreadTaskRunnerHandle::Get()));
129 PopularSites popular_sites(worker_pool_owner_.pool().get(), &prefs_, 150 PopularSites popular_sites(worker_pool_owner_.pool().get(), &prefs_,
130 /*template_url_service=*/nullptr, 151 /*template_url_service=*/nullptr,
131 /*variations_service=*/nullptr, 152 /*variations_service=*/nullptr,
132 url_request_context.get(), cache_dir_.GetPath(), 153 url_request_context.get(), cache_dir_,
133 base::Bind(JsonUnsafeParser::Parse)); 154 base::Bind(JsonUnsafeParser::Parse));
134 155
135 base::RunLoop loop; 156 base::RunLoop loop;
136 bool save_success = false; 157 bool save_success = false;
137 popular_sites.StartFetch( 158 popular_sites.StartFetch(
138 force_download, 159 force_download,
139 base::Bind( 160 base::Bind(
140 [](bool* save_success, base::RunLoop* loop, bool success) { 161 [](bool* save_success, base::RunLoop* loop, bool success) {
141 *save_success = success; 162 *save_success = success;
142 loop->Quit(); 163 loop->Quit();
143 }, 164 },
144 &save_success, &loop)); 165 &save_success, &loop));
145 loop.Run(); 166 loop.Run();
146 *sites = popular_sites.sites(); 167 *sites = popular_sites.sites();
147 return save_success; 168 return save_success;
148 } 169 }
149 170
171 const TestPopularSite kWikipedia;
172 const TestPopularSite kYouTube;
173 const TestPopularSite kChromium;
174
150 base::MessageLoopForUI ui_loop_; 175 base::MessageLoopForUI ui_loop_;
151 base::SequencedWorkerPoolOwner worker_pool_owner_; 176 base::SequencedWorkerPoolOwner worker_pool_owner_;
152 base::ScopedTempDir cache_dir_; 177 base::ScopedTempDir scoped_cache_dir_;
178 base::FilePath cache_dir_;
Marc Treib 2016/11/08 11:27:30 nit: cache_path_ ?
sfiera 2016/11/08 13:00:35 I would say that the cache path is "/…whatever…/su
153 user_prefs::TestingPrefServiceSyncable prefs_; 179 user_prefs::TestingPrefServiceSyncable prefs_;
154 net::FakeURLFetcherFactory url_fetcher_factory_; 180 net::FakeURLFetcherFactory url_fetcher_factory_;
155 }; 181 };
156 182
157 TEST_F(PopularSitesTest, Basic) { 183 TEST_F(PopularSitesTest, Basic) {
158 SetCountryAndVersion("ZZ", "9"); 184 SetCountryAndVersion("ZZ", "9");
159 RespondWithJSON( 185 RespondWithJSON(
160 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", 186 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
161 {{ 187 {kWikipedia});
162 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"},
163 {kUrl, "https://zz.m.wikipedia.org/"},
164 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"},
165 }});
166 188
167 std::vector<PopularSites::Site> sites; 189 std::vector<PopularSites::Site> sites;
168 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites)); 190 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
169 191
170 ASSERT_THAT(sites.size(), Eq(1u)); 192 ASSERT_THAT(sites.size(), Eq(1u));
171 EXPECT_THAT(sites[0].title, Str16Eq("Wikipedia, fhta Ph'nglui mglw'nafh")); 193 EXPECT_THAT(sites[0].title, Str16Eq("Wikipedia, fhta Ph'nglui mglw'nafh"));
172 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/")); 194 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/"));
173 EXPECT_THAT(sites[0].large_icon_url, 195 EXPECT_THAT(sites[0].large_icon_url,
174 URLEq("https://zz.m.wikipedia.org/wikipedia.png")); 196 URLEq("https://zz.m.wikipedia.org/wikipedia.png"));
175 EXPECT_THAT(sites[0].favicon_url, URLEq("")); 197 EXPECT_THAT(sites[0].favicon_url, URLEq(""));
176 } 198 }
177 199
178 TEST_F(PopularSitesTest, Fallback) { 200 TEST_F(PopularSitesTest, Fallback) {
179 SetCountryAndVersion("ZZ", "9"); 201 SetCountryAndVersion("ZZ", "9");
180 RespondWith404( 202 RespondWith404(
181 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json"); 203 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json");
182 RespondWithJSON( 204 RespondWithJSON(
183 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json", 205 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json",
184 {{ 206 {kYouTube, kChromium});
185 {kTitle, "YouTube"},
186 {kUrl, "https://m.youtube.com/"},
187 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"},
188 },
189 {
190 {kTitle, "The Chromium Project"},
191 {kUrl, "https://www.chromium.org/"},
192 {kFaviconUrl, "https://www.chromium.org/favicon.ico"},
193 }});
194 207
195 std::vector<PopularSites::Site> sites; 208 std::vector<PopularSites::Site> sites;
196 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites)); 209 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
197 210
198 ASSERT_THAT(sites.size(), Eq(2u)); 211 ASSERT_THAT(sites.size(), Eq(2u));
199 EXPECT_THAT(sites[0].title, Str16Eq("YouTube")); 212 EXPECT_THAT(sites[0].title, Str16Eq("YouTube"));
200 EXPECT_THAT(sites[0].url, URLEq("https://m.youtube.com/")); 213 EXPECT_THAT(sites[0].url, URLEq("https://m.youtube.com/"));
201 EXPECT_THAT(sites[0].large_icon_url, 214 EXPECT_THAT(sites[0].large_icon_url,
202 URLEq("https://s.ytimg.com/apple-touch-icon.png")); 215 URLEq("https://s.ytimg.com/apple-touch-icon.png"));
203 EXPECT_THAT(sites[0].favicon_url, URLEq("")); 216 EXPECT_THAT(sites[0].favicon_url, URLEq(""));
204 EXPECT_THAT(sites[1].title, Str16Eq("The Chromium Project")); 217 EXPECT_THAT(sites[1].title, Str16Eq("The Chromium Project"));
205 EXPECT_THAT(sites[1].url, URLEq("https://www.chromium.org/")); 218 EXPECT_THAT(sites[1].url, URLEq("https://www.chromium.org/"));
206 EXPECT_THAT(sites[1].large_icon_url, URLEq("")); 219 EXPECT_THAT(sites[1].large_icon_url, URLEq(""));
207 EXPECT_THAT(sites[1].favicon_url, 220 EXPECT_THAT(sites[1].favicon_url,
208 URLEq("https://www.chromium.org/favicon.ico")); 221 URLEq("https://www.chromium.org/favicon.ico"));
209 } 222 }
210 223
211 TEST_F(PopularSitesTest, Failure) { 224 TEST_F(PopularSitesTest, Failure) {
212 SetCountryAndVersion("ZZ", "9"); 225 SetCountryAndVersion("ZZ", "9");
213 RespondWith404( 226 RespondWith404(
214 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json"); 227 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json");
215 RespondWith404( 228 RespondWith404(
216 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json"); 229 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json");
217 230
218 std::vector<PopularSites::Site> sites; 231 std::vector<PopularSites::Site> sites;
219 EXPECT_FALSE(FetchPopularSites(/*force_download=*/false, &sites)); 232 EXPECT_FALSE(FetchPopularSites(/*force_download=*/false, &sites));
220 ASSERT_THAT(sites.size(), Eq(0u)); 233 ASSERT_THAT(sites.size(), Eq(0u));
221 } 234 }
222 235
236 TEST_F(PopularSitesTest, FailsWithoutFetchIfNoCacheDir) {
237 SetCountryAndVersion("ZZ", "9");
238 std::vector<PopularSites::Site> sites;
239 cache_dir_ = base::FilePath(); // Override with invalid file path.
240 EXPECT_FALSE(FetchPopularSites(/*force_download=*/false, &sites));
241 }
242
243 TEST_F(PopularSitesTest, UsesCachedFile) {
244 SetCountryAndVersion("ZZ", "9");
245 RespondWithJSON(
246 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
247 {kWikipedia});
248
249 // First request succeeds and gets cached.
250 std::vector<PopularSites::Site> sites;
251 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
252
253 // File disappears from server, but we don't need it because it's cached.
254 RespondWith404(
255 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json");
256 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
257 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/"));
258 }
259
260 TEST_F(PopularSitesTest, DoesntUseCachedFileIfDownloadForced) {
261 SetCountryAndVersion("ZZ", "9");
262 RespondWithJSON(
263 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
264 {kWikipedia});
265
266 // First request succeeds and gets cached.
267 std::vector<PopularSites::Site> sites;
268 EXPECT_TRUE(FetchPopularSites(/*force_download=*/true, &sites));
269 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/"));
270
271 // File disappears from server. Download is forced, so we get the new file.
272 RespondWithJSON(
273 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
274 {kChromium});
275 EXPECT_TRUE(FetchPopularSites(/*force_download=*/true, &sites));
276 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/"));
277 }
278
279 TEST_F(PopularSitesTest, RefetchesAfterCountryMoved) {
280 RespondWithJSON(
281 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
282 {kWikipedia});
283 RespondWithJSON(
284 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZX_9.json",
285 {kChromium});
286
287 std::vector<PopularSites::Site> sites;
288
289 // First request (in ZZ) saves Wikipedia.
290 SetCountryAndVersion("ZZ", "9");
291 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
292 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/"));
293
294 // Second request (now in ZX) saves Chromium.
295 SetCountryAndVersion("ZX", "9");
296 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
297 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/"));
298 }
299
300 TEST_F(PopularSitesTest, DoesntCacheInvalidFile) {
301 SetCountryAndVersion("ZZ", "9");
302 RespondWithData(
303 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
304 "ceci n'est pas un json");
305 RespondWithJSON(
306 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json", {});
Marc Treib 2016/11/08 11:27:30 Is this actually needed? The same thing would happ
sfiera 2016/11/08 13:00:35 Hmm. Well, the fetcher will hit the fallback URL h
307
308 // First request falls back and gets nothing.
309 std::vector<PopularSites::Site> sites;
310 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
311 EXPECT_THAT(sites.size(), Eq(0u));
Marc Treib 2016/11/08 11:27:30 nit: IsEmpty?
sfiera 2016/11/08 13:00:35 Done.
312
313 // Second request refetches ZZ_9, which now has data.
314 RespondWithJSON(
315 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
316 {kChromium});
317 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
318 ASSERT_THAT(sites.size(), Eq(1u));
319 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/"));
320 }
321
223 } // namespace 322 } // namespace
224 } // namespace ntp_tiles 323 } // namespace ntp_tiles
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698