OLD | NEW |
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/files/file_util.h" | 14 #include "base/files/file_util.h" |
14 #include "base/files/important_file_writer.h" | 15 #include "base/files/important_file_writer.h" |
15 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
16 #include "base/json/json_writer.h" | 17 #include "base/json/json_writer.h" |
17 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
18 #include "base/optional.h" | 19 #include "base/optional.h" |
19 #include "base/run_loop.h" | 20 #include "base/run_loop.h" |
20 #include "base/test/sequenced_worker_pool_owner.h" | 21 #include "base/test/sequenced_worker_pool_owner.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
22 #include "base/values.h" | 23 #include "base/values.h" |
23 #include "components/ntp_tiles/json_unsafe_parser.h" | 24 #include "components/ntp_tiles/json_unsafe_parser.h" |
24 #include "components/ntp_tiles/pref_names.h" | 25 #include "components/ntp_tiles/pref_names.h" |
| 26 #include "components/ntp_tiles/switches.h" |
25 #include "components/pref_registry/pref_registry_syncable.h" | 27 #include "components/pref_registry/pref_registry_syncable.h" |
26 #include "components/sync_preferences/testing_pref_service_syncable.h" | 28 #include "components/sync_preferences/testing_pref_service_syncable.h" |
27 #include "net/http/http_status_code.h" | 29 #include "net/http/http_status_code.h" |
28 #include "net/url_request/test_url_fetcher_factory.h" | 30 #include "net/url_request/test_url_fetcher_factory.h" |
29 #include "net/url_request/url_request_status.h" | 31 #include "net/url_request/url_request_status.h" |
30 #include "net/url_request/url_request_test_util.h" | 32 #include "net/url_request/url_request_test_util.h" |
31 #include "testing/gmock/include/gmock/gmock.h" | 33 #include "testing/gmock/include/gmock/gmock.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
33 | 35 |
34 using testing::Eq; | 36 using testing::Eq; |
(...skipping 11 matching lines...) Expand all Loading... |
46 using TestPopularSiteVector = std::vector<TestPopularSite>; | 48 using TestPopularSiteVector = std::vector<TestPopularSite>; |
47 | 49 |
48 ::testing::Matcher<const base::string16&> Str16Eq(const std::string& s) { | 50 ::testing::Matcher<const base::string16&> Str16Eq(const std::string& s) { |
49 return ::testing::Eq(base::UTF8ToUTF16(s)); | 51 return ::testing::Eq(base::UTF8ToUTF16(s)); |
50 } | 52 } |
51 | 53 |
52 ::testing::Matcher<const GURL&> URLEq(const std::string& s) { | 54 ::testing::Matcher<const GURL&> URLEq(const std::string& s) { |
53 return ::testing::Eq(GURL(s)); | 55 return ::testing::Eq(GURL(s)); |
54 } | 56 } |
55 | 57 |
| 58 size_t GetNumberOfDefaultPopularSitesForPlatform() { |
| 59 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 60 return 8ul; |
| 61 #else |
| 62 return 0; |
| 63 #endif |
| 64 } |
| 65 |
56 class PopularSitesTest : public ::testing::Test { | 66 class PopularSitesTest : public ::testing::Test { |
57 protected: | 67 protected: |
58 PopularSitesTest() | 68 PopularSitesTest() |
59 : kWikipedia{ | 69 : kWikipedia{ |
60 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"}, | 70 {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"}, |
61 {kUrl, "https://zz.m.wikipedia.org/"}, | 71 {kUrl, "https://zz.m.wikipedia.org/"}, |
62 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"}, | 72 {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"}, |
63 }, | 73 }, |
64 kYouTube{ | 74 kYouTube{ |
65 {kTitle, "YouTube"}, | 75 {kTitle, "YouTube"}, |
66 {kUrl, "https://m.youtube.com/"}, | 76 {kUrl, "https://m.youtube.com/"}, |
67 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"}, | 77 {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"}, |
68 }, | 78 }, |
69 kChromium{ | 79 kChromium{ |
70 {kTitle, "The Chromium Project"}, | 80 {kTitle, "The Chromium Project"}, |
71 {kUrl, "https://www.chromium.org/"}, | 81 {kUrl, "https://www.chromium.org/"}, |
72 {kFaviconUrl, "https://www.chromium.org/favicon.ico"}, | 82 {kFaviconUrl, "https://www.chromium.org/favicon.ico"}, |
73 }, | 83 }, |
74 worker_pool_owner_(2, "PopularSitesTest."), | 84 worker_pool_owner_(2, "PopularSitesTest."), |
75 url_fetcher_factory_(nullptr) { | 85 url_fetcher_factory_(nullptr) { |
| 86 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 87 switches::kEnableNTPPopularSites); |
76 PopularSitesImpl::RegisterProfilePrefs(prefs_.registry()); | 88 PopularSitesImpl::RegisterProfilePrefs(prefs_.registry()); |
77 CHECK(scoped_cache_dir_.CreateUniqueTempDir()); | 89 CHECK(scoped_cache_dir_.CreateUniqueTempDir()); |
78 cache_dir_ = scoped_cache_dir_.GetPath(); | 90 cache_dir_ = scoped_cache_dir_.GetPath(); |
79 } | 91 } |
80 | 92 |
81 void SetCountryAndVersion(const std::string& country, | 93 void SetCountryAndVersion(const std::string& country, |
82 const std::string& version) { | 94 const std::string& version) { |
83 prefs_.SetString(prefs::kPopularSitesOverrideCountry, country); | 95 prefs_.SetString(prefs::kPopularSitesOverrideCountry, country); |
84 prefs_.SetString(prefs::kPopularSitesOverrideVersion, version); | 96 prefs_.SetString(prefs::kPopularSitesOverrideVersion, version); |
85 } | 97 } |
(...skipping 24 matching lines...) Expand all Loading... |
110 net::URLRequestStatus::SUCCESS); | 122 net::URLRequestStatus::SUCCESS); |
111 } | 123 } |
112 | 124 |
113 // Returns an optional bool representing whether the completion callback was | 125 // Returns an optional bool representing whether the completion callback was |
114 // called at all, and if yes which was the returned bool value. | 126 // called at all, and if yes which was the returned bool value. |
115 base::Optional<bool> FetchPopularSites(bool force_download, | 127 base::Optional<bool> FetchPopularSites(bool force_download, |
116 PopularSites::SitesVector* sites) { | 128 PopularSites::SitesVector* sites) { |
117 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( | 129 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( |
118 new net::TestURLRequestContextGetter( | 130 new net::TestURLRequestContextGetter( |
119 base::ThreadTaskRunnerHandle::Get())); | 131 base::ThreadTaskRunnerHandle::Get())); |
120 PopularSitesImpl popular_sites(worker_pool_owner_.pool().get(), &prefs_, | 132 std::unique_ptr<PopularSites> popular_sites = |
121 /*template_url_service=*/nullptr, | 133 CreatePopularSites(url_request_context.get()); |
122 /*variations_service=*/nullptr, | |
123 url_request_context.get(), cache_dir_, | |
124 base::Bind(JsonUnsafeParser::Parse)); | |
125 | 134 |
126 base::RunLoop loop; | 135 base::RunLoop loop; |
127 base::Optional<bool> save_success; | 136 base::Optional<bool> save_success; |
128 if (popular_sites.MaybeStartFetch( | 137 if (popular_sites->MaybeStartFetch( |
129 force_download, base::Bind( | 138 force_download, base::Bind( |
130 [](base::Optional<bool>* save_success, | 139 [](base::Optional<bool>* save_success, |
131 base::RunLoop* loop, bool success) { | 140 base::RunLoop* loop, bool success) { |
132 save_success->emplace(success); | 141 save_success->emplace(success); |
133 loop->Quit(); | 142 loop->Quit(); |
134 }, | 143 }, |
135 &save_success, &loop))) { | 144 &save_success, &loop))) { |
136 loop.Run(); | 145 loop.Run(); |
137 } | 146 } |
138 *sites = popular_sites.sites(); | 147 *sites = popular_sites->sites(); |
139 return save_success; | 148 return save_success; |
140 } | 149 } |
141 | 150 |
| 151 std::unique_ptr<PopularSites> CreatePopularSites( |
| 152 net::URLRequestContextGetter* context) { |
| 153 return base::MakeUnique<PopularSitesImpl>( |
| 154 worker_pool_owner_.pool().get(), &prefs_, |
| 155 /*template_url_service=*/nullptr, |
| 156 /*variations_service=*/nullptr, context, cache_dir_, |
| 157 base::Bind(JsonUnsafeParser::Parse)); |
| 158 } |
| 159 |
142 const TestPopularSite kWikipedia; | 160 const TestPopularSite kWikipedia; |
143 const TestPopularSite kYouTube; | 161 const TestPopularSite kYouTube; |
144 const TestPopularSite kChromium; | 162 const TestPopularSite kChromium; |
145 | 163 |
146 base::MessageLoopForUI ui_loop_; | 164 base::MessageLoopForUI ui_loop_; |
147 base::SequencedWorkerPoolOwner worker_pool_owner_; | 165 base::SequencedWorkerPoolOwner worker_pool_owner_; |
148 base::ScopedTempDir scoped_cache_dir_; | 166 base::ScopedTempDir scoped_cache_dir_; |
149 base::FilePath cache_dir_; | 167 base::FilePath cache_dir_; |
150 sync_preferences::TestingPrefServiceSyncable prefs_; | 168 sync_preferences::TestingPrefServiceSyncable prefs_; |
151 net::FakeURLFetcherFactory url_fetcher_factory_; | 169 net::FakeURLFetcherFactory url_fetcher_factory_; |
(...skipping 10 matching lines...) Expand all Loading... |
162 Eq(base::Optional<bool>(true))); | 180 Eq(base::Optional<bool>(true))); |
163 | 181 |
164 ASSERT_THAT(sites.size(), Eq(1u)); | 182 ASSERT_THAT(sites.size(), Eq(1u)); |
165 EXPECT_THAT(sites[0].title, Str16Eq("Wikipedia, fhta Ph'nglui mglw'nafh")); | 183 EXPECT_THAT(sites[0].title, Str16Eq("Wikipedia, fhta Ph'nglui mglw'nafh")); |
166 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/")); | 184 EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/")); |
167 EXPECT_THAT(sites[0].large_icon_url, | 185 EXPECT_THAT(sites[0].large_icon_url, |
168 URLEq("https://zz.m.wikipedia.org/wikipedia.png")); | 186 URLEq("https://zz.m.wikipedia.org/wikipedia.png")); |
169 EXPECT_THAT(sites[0].favicon_url, URLEq("")); | 187 EXPECT_THAT(sites[0].favicon_url, URLEq("")); |
170 } | 188 } |
171 | 189 |
| 190 TEST_F(PopularSitesTest, ContainsDefaultTilesRightAfterConstruction) { |
| 191 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( |
| 192 new net::TestURLRequestContextGetter( |
| 193 base::ThreadTaskRunnerHandle::Get())); |
| 194 |
| 195 EXPECT_THAT(CreatePopularSites(url_request_context.get())->sites().size(), |
| 196 Eq(GetNumberOfDefaultPopularSitesForPlatform())); |
| 197 } |
| 198 |
172 TEST_F(PopularSitesTest, Fallback) { | 199 TEST_F(PopularSitesTest, Fallback) { |
173 SetCountryAndVersion("ZZ", "9"); | 200 SetCountryAndVersion("ZZ", "9"); |
174 RespondWith404( | 201 RespondWith404( |
175 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json"); | 202 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json"); |
176 RespondWithJSON( | 203 RespondWithJSON( |
177 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json", | 204 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json", |
178 {kYouTube, kChromium}); | 205 {kYouTube, kChromium}); |
179 | 206 |
180 PopularSites::SitesVector sites; | 207 PopularSites::SitesVector sites; |
181 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), | 208 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
182 Eq(base::Optional<bool>(true))); | 209 Eq(base::Optional<bool>(true))); |
183 | 210 |
184 ASSERT_THAT(sites.size(), Eq(2u)); | 211 ASSERT_THAT(sites.size(), Eq(2u)); |
185 EXPECT_THAT(sites[0].title, Str16Eq("YouTube")); | 212 EXPECT_THAT(sites[0].title, Str16Eq("YouTube")); |
186 EXPECT_THAT(sites[0].url, URLEq("https://m.youtube.com/")); | 213 EXPECT_THAT(sites[0].url, URLEq("https://m.youtube.com/")); |
187 EXPECT_THAT(sites[0].large_icon_url, | 214 EXPECT_THAT(sites[0].large_icon_url, |
188 URLEq("https://s.ytimg.com/apple-touch-icon.png")); | 215 URLEq("https://s.ytimg.com/apple-touch-icon.png")); |
189 EXPECT_THAT(sites[0].favicon_url, URLEq("")); | 216 EXPECT_THAT(sites[0].favicon_url, URLEq("")); |
190 EXPECT_THAT(sites[1].title, Str16Eq("The Chromium Project")); | 217 EXPECT_THAT(sites[1].title, Str16Eq("The Chromium Project")); |
191 EXPECT_THAT(sites[1].url, URLEq("https://www.chromium.org/")); | 218 EXPECT_THAT(sites[1].url, URLEq("https://www.chromium.org/")); |
192 EXPECT_THAT(sites[1].large_icon_url, URLEq("")); | 219 EXPECT_THAT(sites[1].large_icon_url, URLEq("")); |
193 EXPECT_THAT(sites[1].favicon_url, | 220 EXPECT_THAT(sites[1].favicon_url, |
194 URLEq("https://www.chromium.org/favicon.ico")); | 221 URLEq("https://www.chromium.org/favicon.ico")); |
195 } | 222 } |
196 | 223 |
197 TEST_F(PopularSitesTest, Failure) { | 224 TEST_F(PopularSitesTest, PopulatesWithDefaultResoucesOnFailure) { |
198 SetCountryAndVersion("ZZ", "9"); | 225 SetCountryAndVersion("ZZ", "9"); |
199 RespondWith404( | 226 RespondWith404( |
200 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json"); | 227 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json"); |
201 RespondWith404( | 228 RespondWith404( |
202 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json"); | 229 "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json"); |
203 | 230 |
204 PopularSites::SitesVector sites; | 231 PopularSites::SitesVector sites; |
205 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), | 232 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
206 Eq(base::Optional<bool>(false))); | 233 Eq(base::Optional<bool>(false))); |
207 ASSERT_THAT(sites, IsEmpty()); | 234 EXPECT_THAT(sites.size(), Eq(GetNumberOfDefaultPopularSitesForPlatform())); |
| 235 } |
| 236 |
| 237 TEST_F(PopularSitesTest, ProvidesDefaultSitesUntilCallbackReturns) { |
| 238 SetCountryAndVersion("ZZ", "9"); |
| 239 RespondWithJSON( |
| 240 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", |
| 241 {kWikipedia}); |
| 242 scoped_refptr<net::TestURLRequestContextGetter> url_request_context( |
| 243 new net::TestURLRequestContextGetter( |
| 244 base::ThreadTaskRunnerHandle::Get())); |
| 245 std::unique_ptr<PopularSites> popular_sites = |
| 246 CreatePopularSites(url_request_context.get()); |
| 247 |
| 248 base::RunLoop loop; |
| 249 base::Optional<bool> save_success = false; |
| 250 |
| 251 bool callback_was_scheduled = popular_sites->MaybeStartFetch( |
| 252 /*force_download=*/true, base::Bind( |
| 253 [](base::Optional<bool>* save_success, |
| 254 base::RunLoop* loop, bool success) { |
| 255 save_success->emplace(success); |
| 256 loop->Quit(); |
| 257 }, |
| 258 &save_success, &loop)); |
| 259 |
| 260 // Assert that callback was scheduled so we can wait for its completion. |
| 261 ASSERT_TRUE(callback_was_scheduled); |
| 262 // There should be 8 default sites as nothing was fetched yet. |
| 263 EXPECT_THAT(popular_sites->sites().size(), |
| 264 Eq(GetNumberOfDefaultPopularSitesForPlatform())); |
| 265 |
| 266 loop.Run(); // Wait for the fetch to finish and the callback to return. |
| 267 |
| 268 EXPECT_TRUE(save_success.value()); |
| 269 // The 1 fetched site should replace the default sites. |
| 270 EXPECT_THAT(popular_sites->sites().size(), Eq(1ul)); |
208 } | 271 } |
209 | 272 |
210 TEST_F(PopularSitesTest, ClearsCacheFileFromOldVersions) { | 273 TEST_F(PopularSitesTest, ClearsCacheFileFromOldVersions) { |
211 SetCountryAndVersion("ZZ", "9"); | 274 SetCountryAndVersion("ZZ", "9"); |
212 RespondWithJSON( | 275 RespondWithJSON( |
213 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", | 276 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", |
214 {kWikipedia}); | 277 {kWikipedia}); |
215 | 278 |
216 PopularSites::SitesVector sites; | 279 PopularSites::SitesVector sites; |
217 const base::FilePath old_cache_path = | 280 const base::FilePath old_cache_path = |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", | 415 "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_9.json", |
353 {kChromium}); | 416 {kChromium}); |
354 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), | 417 EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
355 Eq(base::Optional<bool>(true))); | 418 Eq(base::Optional<bool>(true))); |
356 ASSERT_THAT(sites.size(), Eq(1u)); | 419 ASSERT_THAT(sites.size(), Eq(1u)); |
357 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); | 420 EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); |
358 } | 421 } |
359 | 422 |
360 } // namespace | 423 } // namespace |
361 } // namespace ntp_tiles | 424 } // namespace ntp_tiles |
OLD | NEW |