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

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

Issue 2575823002: [Popular Sites] Move PopularSitesImpl to dedicated file (Closed)
Patch Set: Added missing forward declaration. Created 4 years 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/ntp_tiles/popular_sites.h"
6
7 #include <string>
8 #include <utility>
9 #include <vector>
10
11 #include "base/bind.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/json/json_writer.h"
14 #include "base/run_loop.h"
15 #include "base/test/sequenced_worker_pool_owner.h"
16 #include "base/threading/thread_task_runner_handle.h"
17 #include "base/values.h"
18 #include "components/ntp_tiles/json_unsafe_parser.h"
19 #include "components/ntp_tiles/pref_names.h"
20 #include "components/pref_registry/pref_registry_syncable.h"
21 #include "components/sync_preferences/testing_pref_service_syncable.h"
22 #include "net/http/http_status_code.h"
23 #include "net/url_request/test_url_fetcher_factory.h"
24 #include "net/url_request/url_request_status.h"
25 #include "net/url_request/url_request_test_util.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28
29 using testing::Eq;
30 using testing::IsEmpty;
31
32 namespace ntp_tiles {
33 namespace {
34
35 const char kTitle[] = "title";
36 const char kUrl[] = "url";
37 const char kLargeIconUrl[] = "large_icon_url";
38 const char kFaviconUrl[] = "favicon_url";
39
40 using TestPopularSite = std::map<std::string, std::string>;
41 using TestPopularSiteVector = std::vector<TestPopularSite>;
42
43 ::testing::Matcher<const base::string16&> Str16Eq(const std::string& s) {
44 return ::testing::Eq(base::UTF8ToUTF16(s));
45 }
46
47 ::testing::Matcher<const GURL&> URLEq(const std::string& s) {
48 return ::testing::Eq(GURL(s));
49 }
50
51 class PopularSitesTest : public ::testing::Test {
52 protected:
53 PopularSitesTest()
54 : kWikipedia{
55 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"},
56 {kUrl, "https://zz.m.wikipedia.org/"},
57 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"},
58 },
59 kYouTube{
60 {kTitle, "YouTube"},
61 {kUrl, "https://m.youtube.com/"},
62 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"},
63 },
64 kChromium{
65 {kTitle, "The Chromium Project"},
66 {kUrl, "https://www.chromium.org/"},
67 {kFaviconUrl, "https://www.chromium.org/favicon.ico"},
68 },
69 worker_pool_owner_(2, "PopularSitesTest."),
70 url_fetcher_factory_(nullptr) {
71 PopularSitesImpl::RegisterProfilePrefs(prefs_.registry());
72 CHECK(scoped_cache_dir_.CreateUniqueTempDir());
73 cache_dir_ = scoped_cache_dir_.GetPath();
74 }
75
76 void SetCountryAndVersion(const std::string& country,
77 const std::string& version) {
78 prefs_.SetString(prefs::kPopularSitesOverrideCountry, country);
79 prefs_.SetString(prefs::kPopularSitesOverrideVersion, version);
80 }
81
82 void RespondWithJSON(const std::string& url,
83 const TestPopularSiteVector& sites) {
84 base::ListValue sites_value;
85 for (const TestPopularSite& site : sites) {
86 auto site_value = base::MakeUnique<base::DictionaryValue>();
87 for (const std::pair<std::string, std::string>& kv : site) {
88 site_value->SetString(kv.first, kv.second);
89 }
90 sites_value.Append(std::move(site_value));
91 }
92 std::string sites_string;
93 base::JSONWriter::Write(sites_value, &sites_string);
94 url_fetcher_factory_.SetFakeResponse(GURL(url), sites_string, net::HTTP_OK,
95 net::URLRequestStatus::SUCCESS);
96 }
97
98 void RespondWithData(const std::string& url, const std::string& data) {
99 url_fetcher_factory_.SetFakeResponse(GURL(url), data, net::HTTP_OK,
100 net::URLRequestStatus::SUCCESS);
101 }
102
103 void RespondWith404(const std::string& url) {
104 url_fetcher_factory_.SetFakeResponse(GURL(url), "404", net::HTTP_NOT_FOUND,
105 net::URLRequestStatus::SUCCESS);
106 }
107
108 bool FetchPopularSites(bool force_download,
109 PopularSites::SitesVector* sites) {
110 scoped_refptr<net::TestURLRequestContextGetter> url_request_context(
111 new net::TestURLRequestContextGetter(
112 base::ThreadTaskRunnerHandle::Get()));
113 PopularSitesImpl popular_sites(worker_pool_owner_.pool().get(), &prefs_,
114 /*template_url_service=*/nullptr,
115 /*variations_service=*/nullptr,
116 url_request_context.get(), cache_dir_,
117 base::Bind(JsonUnsafeParser::Parse));
118
119 base::RunLoop loop;
120 bool save_success = false;
121 popular_sites.StartFetch(
122 force_download,
123 base::Bind(
124 [](bool* save_success, base::RunLoop* loop, bool success) {
125 *save_success = success;
126 loop->Quit();
127 },
128 &save_success, &loop));
129 loop.Run();
130 *sites = popular_sites.sites();
131 return save_success;
132 }
133
134 const TestPopularSite kWikipedia;
135 const TestPopularSite kYouTube;
136 const TestPopularSite kChromium;
137
138 base::MessageLoopForUI ui_loop_;
139 base::SequencedWorkerPoolOwner worker_pool_owner_;
140 base::ScopedTempDir scoped_cache_dir_;
141 base::FilePath cache_dir_;
142 sync_preferences::TestingPrefServiceSyncable prefs_;
143 net::FakeURLFetcherFactory url_fetcher_factory_;
144 };
145
146 TEST_F(PopularSitesTest, Basic) {
147 SetCountryAndVersion("ZZ", "9");
148 RespondWithJSON(
149 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
150 {kWikipedia});
151
152 PopularSites::SitesVector sites;
153 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
154
155 ASSERT_THAT(sites.size(), Eq(1u));
156 EXPECT_THAT(sites[0].title, Str16Eq("Wikipedia, fhta Ph'nglui mglw'nafh"));
157 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/"));
158 EXPECT_THAT(sites[0].large_icon_url,
159 URLEq("https://zz.m.wikipedia.org/wikipedia.png"));
160 EXPECT_THAT(sites[0].favicon_url, URLEq(""));
161 }
162
163 TEST_F(PopularSitesTest, Fallback) {
164 SetCountryAndVersion("ZZ", "9");
165 RespondWith404(
166 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json");
167 RespondWithJSON(
168 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json",
169 {kYouTube, kChromium});
170
171 PopularSites::SitesVector sites;
172 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
173
174 ASSERT_THAT(sites.size(), Eq(2u));
175 EXPECT_THAT(sites[0].title, Str16Eq("YouTube"));
176 EXPECT_THAT(sites[0].url, URLEq("https://m.youtube.com/"));
177 EXPECT_THAT(sites[0].large_icon_url,
178 URLEq("https://s.ytimg.com/apple-touch-icon.png"));
179 EXPECT_THAT(sites[0].favicon_url, URLEq(""));
180 EXPECT_THAT(sites[1].title, Str16Eq("The Chromium Project"));
181 EXPECT_THAT(sites[1].url, URLEq("https://www.chromium.org/"));
182 EXPECT_THAT(sites[1].large_icon_url, URLEq(""));
183 EXPECT_THAT(sites[1].favicon_url,
184 URLEq("https://www.chromium.org/favicon.ico"));
185 }
186
187 TEST_F(PopularSitesTest, Failure) {
188 SetCountryAndVersion("ZZ", "9");
189 RespondWith404(
190 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json");
191 RespondWith404(
192 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json");
193
194 PopularSites::SitesVector sites;
195 EXPECT_FALSE(FetchPopularSites(/*force_download=*/false, &sites));
196 ASSERT_THAT(sites, IsEmpty());
197 }
198
199 TEST_F(PopularSitesTest, FailsWithoutFetchIfNoCacheDir) {
200 SetCountryAndVersion("ZZ", "9");
201 PopularSites::SitesVector sites;
202 cache_dir_ = base::FilePath(); // Override with invalid file path.
203 EXPECT_FALSE(FetchPopularSites(/*force_download=*/false, &sites));
204 }
205
206 TEST_F(PopularSitesTest, UsesCachedFile) {
207 SetCountryAndVersion("ZZ", "9");
208 RespondWithJSON(
209 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
210 {kWikipedia});
211
212 // First request succeeds and gets cached.
213 PopularSites::SitesVector sites;
214 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
215
216 // File disappears from server, but we don't need it because it's cached.
217 RespondWith404(
218 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json");
219 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
220 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/"));
221 }
222
223 TEST_F(PopularSitesTest, CachesEmptyFile) {
224 SetCountryAndVersion("ZZ", "9");
225 RespondWithData(
226 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", "[]");
227 RespondWithJSON(
228 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json",
229 {kWikipedia});
230
231 // First request succeeds and caches empty suggestions list (no fallback).
232 PopularSites::SitesVector sites;
233 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
234 EXPECT_THAT(sites, IsEmpty());
235
236 // File appears on server, but we continue to use our cached empty file.
237 RespondWithJSON(
238 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
239 {kWikipedia});
240 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
241 EXPECT_THAT(sites, IsEmpty());
242 }
243
244 TEST_F(PopularSitesTest, DoesntUseCachedFileIfDownloadForced) {
245 SetCountryAndVersion("ZZ", "9");
246 RespondWithJSON(
247 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
248 {kWikipedia});
249
250 // First request succeeds and gets cached.
251 PopularSites::SitesVector sites;
252 EXPECT_TRUE(FetchPopularSites(/*force_download=*/true, &sites));
253 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/"));
254
255 // File disappears from server. Download is forced, so we get the new file.
256 RespondWithJSON(
257 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
258 {kChromium});
259 EXPECT_TRUE(FetchPopularSites(/*force_download=*/true, &sites));
260 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/"));
261 }
262
263 TEST_F(PopularSitesTest, RefetchesAfterCountryMoved) {
264 RespondWithJSON(
265 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
266 {kWikipedia});
267 RespondWithJSON(
268 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZX_9.json",
269 {kChromium});
270
271 PopularSites::SitesVector sites;
272
273 // First request (in ZZ) saves Wikipedia.
274 SetCountryAndVersion("ZZ", "9");
275 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
276 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/"));
277
278 // Second request (now in ZX) saves Chromium.
279 SetCountryAndVersion("ZX", "9");
280 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
281 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/"));
282 }
283
284 TEST_F(PopularSitesTest, DoesntCacheInvalidFile) {
285 SetCountryAndVersion("ZZ", "9");
286 RespondWithData(
287 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
288 "ceci n'est pas un json");
289 RespondWith404(
290 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json");
291
292 // First request falls back and gets nothing there either.
293 PopularSites::SitesVector sites;
294 EXPECT_FALSE(FetchPopularSites(/*force_download=*/false, &sites));
295
296 // Second request refetches ZZ_9, which now has data.
297 RespondWithJSON(
298 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
299 {kChromium});
300 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
301 ASSERT_THAT(sites.size(), Eq(1u));
302 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/"));
303 }
304
305 TEST_F(PopularSitesTest, RefetchesAfterFallback) {
306 SetCountryAndVersion("ZZ", "9");
307 RespondWith404(
308 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json");
309 RespondWithJSON(
310 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json",
311 {kWikipedia});
312
313 // First request falls back.
314 PopularSites::SitesVector sites;
315 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
316 ASSERT_THAT(sites.size(), Eq(1u));
317 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/"));
318
319 // Second request refetches ZZ_9, which now has data.
320 RespondWithJSON(
321 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json",
322 {kChromium});
323 EXPECT_TRUE(FetchPopularSites(/*force_download=*/false, &sites));
324 ASSERT_THAT(sites.size(), Eq(1u));
325 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/"));
326 }
327
328 } // namespace
329 } // namespace ntp_tiles
OLDNEW
« no previous file with comments | « components/ntp_tiles/popular_sites_impl_unittest.cc ('k') | ios/chrome/browser/ntp_tiles/ios_popular_sites_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698