sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 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 | |
mastiz | b06f1424 | 2016-12-15 12:32:17 | [diff] [blame] | 5 | #include "components/ntp_tiles/popular_sites_impl.h" |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 6 | |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 7 | #include <map> |
Gyuyoung Kim | cb7965e | 2018-01-25 00:39:01 | [diff] [blame] | 8 | #include <memory> |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/bind.h" |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 14 | #include "base/command_line.h" |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 15 | #include "base/json/json_writer.h" |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 16 | #include "base/run_loop.h" |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 17 | #include "base/strings/string_number_conversions.h" |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
fhorschig | f84f9f5 | 2017-03-16 12:14:06 | [diff] [blame] | 19 | #include "base/test/scoped_feature_list.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 20 | #include "base/test/task_environment.h" |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 21 | #include "base/threading/thread_task_runner_handle.h" |
| 22 | #include "base/values.h" |
Nico Weber | 356b304 | 2019-08-23 15:30:41 | [diff] [blame] | 23 | #include "build/branding_buildflags.h" |
Friedrich Horschig | 2011ecd | 2017-07-10 17:03:38 | [diff] [blame] | 24 | #include "build/build_config.h" |
Kristi Park | 01ca125 | 2019-05-10 22:39:05 | [diff] [blame] | 25 | #include "components/ntp_tiles/features.h" |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 26 | #include "components/ntp_tiles/pref_names.h" |
Friedrich Horschig | 7706ef6 | 2017-08-25 08:20:00 | [diff] [blame] | 27 | #include "components/ntp_tiles/tile_source.h" |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 28 | #include "components/pref_registry/pref_registry_syncable.h" |
a-v-y | b2cf8f8 | 2016-12-11 17:35:07 | [diff] [blame] | 29 | #include "components/sync_preferences/testing_pref_service_syncable.h" |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 30 | #include "net/http/http_status_code.h" |
Ken Rockot | 6722664 | 2019-10-31 16:40:53 | [diff] [blame] | 31 | #include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h" |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 32 | #include "services/network/public/cpp/shared_url_loader_factory.h" |
| 33 | #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h" |
| 34 | #include "services/network/test/test_url_loader_factory.h" |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 35 | #include "testing/gmock/include/gmock/gmock.h" |
| 36 | #include "testing/gtest/include/gtest/gtest.h" |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 37 | #include "third_party/abseil-cpp/absl/types/optional.h" |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 38 | |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 39 | using testing::_; |
| 40 | using testing::Contains; |
| 41 | using testing::ElementsAre; |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 42 | using testing::Eq; |
fhorschig | fed34be | 2017-03-02 23:16:09 | [diff] [blame] | 43 | using testing::Gt; |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 44 | using testing::IsEmpty; |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 45 | using testing::Not; |
| 46 | using testing::Pair; |
| 47 | using testing::SizeIs; |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 48 | |
| 49 | namespace ntp_tiles { |
| 50 | namespace { |
| 51 | |
| 52 | const char kTitle[] = "title"; |
| 53 | const char kUrl[] = "url"; |
| 54 | const char kLargeIconUrl[] = "large_icon_url"; |
| 55 | const char kFaviconUrl[] = "favicon_url"; |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 56 | const char kSection[] = "section"; |
| 57 | const char kSites[] = "sites"; |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 58 | const char kTitleSource[] = "title_source"; |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 59 | |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 60 | using TestPopularSite = std::map<std::string, std::string>; |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 61 | using TestPopularSiteVector = std::vector<TestPopularSite>; |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 62 | using TestPopularSection = std::pair<SectionType, TestPopularSiteVector>; |
| 63 | using TestPopularSectionVector = std::vector<TestPopularSection>; |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 64 | |
Jan Wilken Dörrie | fa241ba | 2021-03-11 17:57:01 | [diff] [blame] | 65 | ::testing::Matcher<const std::u16string&> Str16Eq(const std::string& s) { |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 66 | return ::testing::Eq(base::UTF8ToUTF16(s)); |
| 67 | } |
| 68 | |
| 69 | ::testing::Matcher<const GURL&> URLEq(const std::string& s) { |
| 70 | return ::testing::Eq(GURL(s)); |
| 71 | } |
| 72 | |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 73 | size_t GetNumberOfDefaultPopularSitesForPlatform() { |
Friedrich Horschig | 7f4a97f | 2017-07-10 17:50:57 | [diff] [blame] | 74 | #if defined(OS_ANDROID) || defined(OS_IOS) |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 75 | return 8ul; |
| 76 | #else |
fhorschig | f84f9f5 | 2017-03-16 12:14:06 | [diff] [blame] | 77 | return 0ul; |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 78 | #endif |
| 79 | } |
| 80 | |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 81 | class PopularSitesTest : public ::testing::Test { |
| 82 | protected: |
| 83 | PopularSitesTest() |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 84 | : kWikipedia{ |
| 85 | {kTitle, "Wikipedia, fhta Ph'nglui mglw'nafh"}, |
| 86 | {kUrl, "https://zz.m.wikipedia.org/"}, |
| 87 | {kLargeIconUrl, "https://zz.m.wikipedia.org/wikipedia.png"}, |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 88 | {kTitleSource, "3"}, // Title extracted from title tag. |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 89 | }, |
| 90 | kYouTube{ |
| 91 | {kTitle, "YouTube"}, |
| 92 | {kUrl, "https://m.youtube.com/"}, |
| 93 | {kLargeIconUrl, "https://s.ytimg.com/apple-touch-icon.png"}, |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 94 | {kTitleSource, "1"}, // Title extracted from manifest. |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 95 | }, |
| 96 | kChromium{ |
| 97 | {kTitle, "The Chromium Project"}, |
| 98 | {kUrl, "https://www.chromium.org/"}, |
| 99 | {kFaviconUrl, "https://www.chromium.org/favicon.ico"}, |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 100 | // No "title_source" (like in v5 or earlier). Defaults to TITLE_TAG. |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 101 | }, |
fhorschig | f84f9f5 | 2017-03-16 12:14:06 | [diff] [blame] | 102 | prefs_(new sync_preferences::TestingPrefServiceSyncable()), |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 103 | test_shared_loader_factory_( |
| 104 | base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>( |
| 105 | &test_url_loader_factory_)) { |
fhorschig | f84f9f5 | 2017-03-16 12:14:06 | [diff] [blame] | 106 | PopularSitesImpl::RegisterProfilePrefs(prefs_->registry()); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void SetCountryAndVersion(const std::string& country, |
| 110 | const std::string& version) { |
fhorschig | f84f9f5 | 2017-03-16 12:14:06 | [diff] [blame] | 111 | prefs_->SetString(prefs::kPopularSitesOverrideCountry, country); |
| 112 | prefs_->SetString(prefs::kPopularSitesOverrideVersion, version); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 113 | } |
| 114 | |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 115 | std::unique_ptr<base::ListValue> CreateListFromTestSites( |
| 116 | const TestPopularSiteVector& sites) { |
Gyuyoung Kim | cb7965e | 2018-01-25 00:39:01 | [diff] [blame] | 117 | auto sites_value = std::make_unique<base::ListValue>(); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 118 | for (const TestPopularSite& site : sites) { |
Gyuyoung Kim | cb7965e | 2018-01-25 00:39:01 | [diff] [blame] | 119 | auto site_value = std::make_unique<base::DictionaryValue>(); |
Nico Weber | 6dcde5b | 2020-02-22 20:49:20 | [diff] [blame] | 120 | for (const std::pair<const std::string, std::string>& kv : site) { |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 121 | if (kv.first == kTitleSource) { |
| 122 | int source; |
| 123 | bool convert_success = base::StringToInt(kv.second, &source); |
| 124 | DCHECK(convert_success); |
| 125 | site_value->SetInteger(kv.first, source); |
| 126 | continue; |
| 127 | } |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 128 | site_value->SetString(kv.first, kv.second); |
| 129 | } |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 130 | sites_value->Append(std::move(site_value)); |
| 131 | } |
| 132 | return sites_value; |
| 133 | } |
| 134 | |
| 135 | void RespondWithV5JSON(const std::string& url, |
| 136 | const TestPopularSiteVector& sites) { |
| 137 | std::string sites_string; |
| 138 | base::JSONWriter::Write(*CreateListFromTestSites(sites), &sites_string); |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 139 | test_url_loader_factory_.AddResponse(url, sites_string); |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void RespondWithV6JSON(const std::string& url, |
| 143 | const TestPopularSectionVector& sections) { |
| 144 | base::ListValue sections_value; |
| 145 | for (const TestPopularSection& section : sections) { |
Gyuyoung Kim | cb7965e | 2018-01-25 00:39:01 | [diff] [blame] | 146 | auto section_value = std::make_unique<base::DictionaryValue>(); |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 147 | section_value->SetInteger(kSection, static_cast<int>(section.first)); |
| 148 | section_value->SetList(kSites, CreateListFromTestSites(section.second)); |
| 149 | sections_value.Append(std::move(section_value)); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 150 | } |
| 151 | std::string sites_string; |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 152 | base::JSONWriter::Write(sections_value, &sites_string); |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 153 | test_url_loader_factory_.AddResponse(url, sites_string); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 154 | } |
| 155 | |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 156 | void RespondWithData(const std::string& url, const std::string& data) { |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 157 | test_url_loader_factory_.AddResponse(url, data); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 158 | } |
| 159 | |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 160 | void RespondWith404(const std::string& url) { |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 161 | test_url_loader_factory_.AddResponse(url, "", net::HTTP_NOT_FOUND); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 162 | } |
| 163 | |
fhorschig | f84f9f5 | 2017-03-16 12:14:06 | [diff] [blame] | 164 | void ReregisterProfilePrefs() { |
Gyuyoung Kim | cb7965e | 2018-01-25 00:39:01 | [diff] [blame] | 165 | prefs_ = std::make_unique<sync_preferences::TestingPrefServiceSyncable>(); |
fhorschig | f84f9f5 | 2017-03-16 12:14:06 | [diff] [blame] | 166 | PopularSitesImpl::RegisterProfilePrefs(prefs_->registry()); |
| 167 | } |
| 168 | |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 169 | // Returns an optional bool representing whether the completion callback was |
| 170 | // called at all, and if yes which was the returned bool value. |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 171 | absl::optional<bool> FetchPopularSites(bool force_download, |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 172 | PopularSites::SitesVector* sites) { |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 173 | std::map<SectionType, PopularSites::SitesVector> sections; |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 174 | absl::optional<bool> save_success = |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 175 | FetchAllSections(force_download, §ions); |
| 176 | *sites = sections.at(SectionType::PERSONALIZED); |
| 177 | return save_success; |
| 178 | } |
| 179 | |
| 180 | // Returns an optional bool representing whether the completion callback was |
| 181 | // called at all, and if yes which was the returned bool value. |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 182 | absl::optional<bool> FetchAllSections( |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 183 | bool force_download, |
| 184 | std::map<SectionType, PopularSites::SitesVector>* sections) { |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 185 | std::unique_ptr<PopularSites> popular_sites = CreatePopularSites(); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 186 | |
| 187 | base::RunLoop loop; |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 188 | absl::optional<bool> save_success; |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 189 | if (popular_sites->MaybeStartFetch( |
Ken Rockot | c24303b | 2019-12-20 17:59:32 | [diff] [blame] | 190 | force_download, base::BindOnce( |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 191 | [](absl::optional<bool>* save_success, |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 192 | base::RunLoop* loop, bool success) { |
| 193 | save_success->emplace(success); |
| 194 | loop->Quit(); |
| 195 | }, |
| 196 | &save_success, &loop))) { |
| 197 | loop.Run(); |
| 198 | } |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 199 | *sections = popular_sites->sections(); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 200 | return save_success; |
| 201 | } |
| 202 | |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 203 | std::unique_ptr<PopularSites> CreatePopularSites() { |
Ken Rockot | 6722664 | 2019-10-31 16:40:53 | [diff] [blame] | 204 | return std::make_unique<PopularSitesImpl>(prefs_.get(), |
| 205 | /*template_url_service=*/nullptr, |
| 206 | /*variations_service=*/nullptr, |
| 207 | test_shared_loader_factory_); |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 208 | } |
| 209 | |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 210 | const TestPopularSite kWikipedia; |
| 211 | const TestPopularSite kYouTube; |
| 212 | const TestPopularSite kChromium; |
| 213 | |
Gabriel Charette | 614b405 | 2019-09-07 14:29:18 | [diff] [blame] | 214 | base::test::SingleThreadTaskEnvironment task_environment_{ |
| 215 | base::test::SingleThreadTaskEnvironment::MainThreadType::UI}; |
Ken Rockot | 6722664 | 2019-10-31 16:40:53 | [diff] [blame] | 216 | data_decoder::test::InProcessDataDecoder in_process_data_decoder_; |
fhorschig | f84f9f5 | 2017-03-16 12:14:06 | [diff] [blame] | 217 | std::unique_ptr<sync_preferences::TestingPrefServiceSyncable> prefs_; |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 218 | network::TestURLLoaderFactory test_url_loader_factory_; |
| 219 | scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_; |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 220 | }; |
| 221 | |
fhorschig | fed34be | 2017-03-02 23:16:09 | [diff] [blame] | 222 | TEST_F(PopularSitesTest, ContainsDefaultTilesRightAfterConstruction) { |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 223 | auto popular_sites = CreatePopularSites(); |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 224 | EXPECT_THAT( |
| 225 | popular_sites->sections(), |
| 226 | ElementsAre(Pair(SectionType::PERSONALIZED, |
| 227 | SizeIs(GetNumberOfDefaultPopularSitesForPlatform())))); |
fhorschig | fed34be | 2017-03-02 23:16:09 | [diff] [blame] | 228 | } |
| 229 | |
fhorschig | f84f9f5 | 2017-03-16 12:14:06 | [diff] [blame] | 230 | TEST_F(PopularSitesTest, IsEmptyOnConstructionIfDisabledByTrial) { |
| 231 | base::test::ScopedFeatureList override_features; |
| 232 | override_features.InitAndDisableFeature(kPopularSitesBakedInContentFeature); |
| 233 | ReregisterProfilePrefs(); |
| 234 | |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 235 | auto popular_sites = CreatePopularSites(); |
fhorschig | f84f9f5 | 2017-03-16 12:14:06 | [diff] [blame] | 236 | |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 237 | EXPECT_THAT(popular_sites->sections(), |
| 238 | ElementsAre(Pair(SectionType::PERSONALIZED, IsEmpty()))); |
fhorschig | f84f9f5 | 2017-03-16 12:14:06 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | TEST_F(PopularSitesTest, ShouldSucceedFetching) { |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 242 | SetCountryAndVersion("ZZ", "5"); |
| 243 | RespondWithV5JSON( |
| 244 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json", |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 245 | {kWikipedia}); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 246 | |
mastiz | 78efbde | 2016-12-14 17:07:07 | [diff] [blame] | 247 | PopularSites::SitesVector sites; |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 248 | EXPECT_THAT(FetchPopularSites(/*force_download=*/true, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 249 | Eq(absl::optional<bool>(true))); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 250 | |
| 251 | ASSERT_THAT(sites.size(), Eq(1u)); |
| 252 | EXPECT_THAT(sites[0].title, Str16Eq("Wikipedia, fhta Ph'nglui mglw'nafh")); |
| 253 | EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/")); |
| 254 | EXPECT_THAT(sites[0].large_icon_url, |
| 255 | URLEq("https://zz.m.wikipedia.org/wikipedia.png")); |
| 256 | EXPECT_THAT(sites[0].favicon_url, URLEq("")); |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 257 | EXPECT_THAT(sites[0].title_source, Eq(TileTitleSource::TITLE_TAG)); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | TEST_F(PopularSitesTest, Fallback) { |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 261 | SetCountryAndVersion("ZZ", "5"); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 262 | RespondWith404( |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 263 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json"); |
| 264 | RespondWithV5JSON( |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 265 | "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json", |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 266 | {kYouTube, kChromium}); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 267 | |
mastiz | 78efbde | 2016-12-14 17:07:07 | [diff] [blame] | 268 | PopularSites::SitesVector sites; |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 269 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 270 | Eq(absl::optional<bool>(true))); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 271 | |
| 272 | ASSERT_THAT(sites.size(), Eq(2u)); |
| 273 | EXPECT_THAT(sites[0].title, Str16Eq("YouTube")); |
| 274 | EXPECT_THAT(sites[0].url, URLEq("https://m.youtube.com/")); |
| 275 | EXPECT_THAT(sites[0].large_icon_url, |
| 276 | URLEq("https://s.ytimg.com/apple-touch-icon.png")); |
| 277 | EXPECT_THAT(sites[0].favicon_url, URLEq("")); |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 278 | EXPECT_THAT(sites[0].title_source, Eq(TileTitleSource::MANIFEST)); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 279 | EXPECT_THAT(sites[1].title, Str16Eq("The Chromium Project")); |
| 280 | EXPECT_THAT(sites[1].url, URLEq("https://www.chromium.org/")); |
| 281 | EXPECT_THAT(sites[1].large_icon_url, URLEq("")); |
| 282 | EXPECT_THAT(sites[1].favicon_url, |
| 283 | URLEq("https://www.chromium.org/favicon.ico")); |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 284 | // Fall back to TITLE_TAG if there is no "title_source". Version 5 or before |
| 285 | // haven't had this property and get titles from <title> tags exclusively. |
| 286 | EXPECT_THAT(sites[1].title_source, Eq(TileTitleSource::TITLE_TAG)); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 287 | } |
| 288 | |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 289 | TEST_F(PopularSitesTest, PopulatesWithDefaultResoucesOnFailure) { |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 290 | SetCountryAndVersion("ZZ", "5"); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 291 | RespondWith404( |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 292 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json"); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 293 | RespondWith404( |
| 294 | "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json"); |
| 295 | |
mastiz | 78efbde | 2016-12-14 17:07:07 | [diff] [blame] | 296 | PopularSites::SitesVector sites; |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 297 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 298 | Eq(absl::optional<bool>(false))); |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 299 | EXPECT_THAT(sites.size(), Eq(GetNumberOfDefaultPopularSitesForPlatform())); |
| 300 | } |
| 301 | |
Friedrich Horschig | 7706ef6 | 2017-08-25 08:20:00 | [diff] [blame] | 302 | #if defined(OS_ANDROID) || defined(OS_IOS) |
fhorschig | fed34be | 2017-03-02 23:16:09 | [diff] [blame] | 303 | TEST_F(PopularSitesTest, AddsIconResourcesToDefaultPages) { |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 304 | std::unique_ptr<PopularSites> popular_sites = CreatePopularSites(); |
fhorschig | fed34be | 2017-03-02 23:16:09 | [diff] [blame] | 305 | |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 306 | const PopularSites::SitesVector& sites = |
| 307 | popular_sites->sections().at(SectionType::PERSONALIZED); |
| 308 | ASSERT_FALSE(sites.empty()); |
| 309 | for (const auto& site : sites) { |
Friedrich Horschig | 7706ef6 | 2017-08-25 08:20:00 | [diff] [blame] | 310 | EXPECT_TRUE(site.baked_in); |
Nico Weber | 356b304 | 2019-08-23 15:30:41 | [diff] [blame] | 311 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
fhorschig | fed34be | 2017-03-02 23:16:09 | [diff] [blame] | 312 | EXPECT_THAT(site.default_icon_resource, Gt(0)); |
fhorschig | fed34be | 2017-03-02 23:16:09 | [diff] [blame] | 313 | #endif |
Friedrich Horschig | 7706ef6 | 2017-08-25 08:20:00 | [diff] [blame] | 314 | } |
fhorschig | fed34be | 2017-03-02 23:16:09 | [diff] [blame] | 315 | } |
Friedrich Horschig | 7706ef6 | 2017-08-25 08:20:00 | [diff] [blame] | 316 | #endif |
fhorschig | fed34be | 2017-03-02 23:16:09 | [diff] [blame] | 317 | |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 318 | TEST_F(PopularSitesTest, ProvidesDefaultSitesUntilCallbackReturns) { |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 319 | SetCountryAndVersion("ZZ", "5"); |
| 320 | RespondWithV5JSON( |
| 321 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json", |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 322 | {kWikipedia}); |
Mark Pilgrim | 60a21d3a | 2018-05-30 15:37:40 | [diff] [blame] | 323 | std::unique_ptr<PopularSites> popular_sites = CreatePopularSites(); |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 324 | |
| 325 | base::RunLoop loop; |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 326 | absl::optional<bool> save_success = false; |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 327 | |
| 328 | bool callback_was_scheduled = popular_sites->MaybeStartFetch( |
Ken Rockot | c24303b | 2019-12-20 17:59:32 | [diff] [blame] | 329 | /*force_download=*/true, base::BindOnce( |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 330 | [](absl::optional<bool>* save_success, |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 331 | base::RunLoop* loop, bool success) { |
| 332 | save_success->emplace(success); |
| 333 | loop->Quit(); |
| 334 | }, |
| 335 | &save_success, &loop)); |
| 336 | |
| 337 | // Assert that callback was scheduled so we can wait for its completion. |
| 338 | ASSERT_TRUE(callback_was_scheduled); |
| 339 | // There should be 8 default sites as nothing was fetched yet. |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 340 | EXPECT_THAT(popular_sites->sections().at(SectionType::PERSONALIZED).size(), |
fhorschig | 7d16fb2 | 2017-02-15 08:28:08 | [diff] [blame] | 341 | Eq(GetNumberOfDefaultPopularSitesForPlatform())); |
| 342 | |
| 343 | loop.Run(); // Wait for the fetch to finish and the callback to return. |
| 344 | |
| 345 | EXPECT_TRUE(save_success.value()); |
| 346 | // The 1 fetched site should replace the default sites. |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 347 | EXPECT_THAT(popular_sites->sections(), |
| 348 | ElementsAre(Pair(SectionType::PERSONALIZED, SizeIs(1ul)))); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 349 | } |
| 350 | |
mastiz | 5c82ff8 | 2017-01-23 17:00:43 | [diff] [blame] | 351 | TEST_F(PopularSitesTest, UsesCachedJson) { |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 352 | SetCountryAndVersion("ZZ", "5"); |
| 353 | RespondWithV5JSON( |
| 354 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json", |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 355 | {kWikipedia}); |
| 356 | |
| 357 | // First request succeeds and gets cached. |
mastiz | 78efbde | 2016-12-14 17:07:07 | [diff] [blame] | 358 | PopularSites::SitesVector sites; |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 359 | ASSERT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 360 | Eq(absl::optional<bool>(true))); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 361 | |
| 362 | // File disappears from server, but we don't need it because it's cached. |
| 363 | RespondWith404( |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 364 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json"); |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 365 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 366 | Eq(absl::nullopt)); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 367 | EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/")); |
| 368 | } |
| 369 | |
| 370 | TEST_F(PopularSitesTest, CachesEmptyFile) { |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 371 | SetCountryAndVersion("ZZ", "5"); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 372 | RespondWithData( |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 373 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json", "[]"); |
| 374 | RespondWithV5JSON( |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 375 | "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json", |
| 376 | {kWikipedia}); |
| 377 | |
| 378 | // First request succeeds and caches empty suggestions list (no fallback). |
mastiz | 78efbde | 2016-12-14 17:07:07 | [diff] [blame] | 379 | PopularSites::SitesVector sites; |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 380 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 381 | Eq(absl::optional<bool>(true))); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 382 | EXPECT_THAT(sites, IsEmpty()); |
| 383 | |
| 384 | // File appears on server, but we continue to use our cached empty file. |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 385 | RespondWithV5JSON( |
| 386 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json", |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 387 | {kWikipedia}); |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 388 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 389 | Eq(absl::nullopt)); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 390 | EXPECT_THAT(sites, IsEmpty()); |
| 391 | } |
| 392 | |
| 393 | TEST_F(PopularSitesTest, DoesntUseCachedFileIfDownloadForced) { |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 394 | SetCountryAndVersion("ZZ", "5"); |
| 395 | RespondWithV5JSON( |
| 396 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json", |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 397 | {kWikipedia}); |
| 398 | |
| 399 | // First request succeeds and gets cached. |
mastiz | 78efbde | 2016-12-14 17:07:07 | [diff] [blame] | 400 | PopularSites::SitesVector sites; |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 401 | EXPECT_THAT(FetchPopularSites(/*force_download=*/true, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 402 | Eq(absl::optional<bool>(true))); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 403 | EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/")); |
| 404 | |
| 405 | // File disappears from server. Download is forced, so we get the new file. |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 406 | RespondWithV5JSON( |
| 407 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json", |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 408 | {kChromium}); |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 409 | EXPECT_THAT(FetchPopularSites(/*force_download=*/true, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 410 | Eq(absl::optional<bool>(true))); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 411 | EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); |
| 412 | } |
| 413 | |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 414 | TEST_F(PopularSitesTest, DoesntUseCacheWithDeprecatedVersion) { |
| 415 | SetCountryAndVersion("ZZ", "5"); |
| 416 | RespondWithV5JSON( |
| 417 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json", |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 418 | {kWikipedia}); |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 419 | |
| 420 | // First request succeeds and gets cached. |
| 421 | PopularSites::SitesVector sites; |
| 422 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 423 | Eq(absl::optional<bool>(true))); |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 424 | EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/")); |
| 425 | EXPECT_THAT(prefs_->GetInteger(prefs::kPopularSitesVersionPref), Eq(5)); |
| 426 | |
| 427 | // The client is updated to use V6. Drop old data and refetch. |
| 428 | SetCountryAndVersion("ZZ", "6"); |
| 429 | RespondWithV6JSON( |
| 430 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_6.json", |
| 431 | {{SectionType::PERSONALIZED, {kChromium}}}); |
| 432 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 433 | Eq(absl::optional<bool>(true))); |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 434 | EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); |
| 435 | EXPECT_THAT(prefs_->GetInteger(prefs::kPopularSitesVersionPref), Eq(6)); |
| 436 | } |
| 437 | |
Friedrich Horschig | b77dc44 | 2017-09-11 13:06:30 | [diff] [blame] | 438 | TEST_F(PopularSitesTest, FallsBackToDefaultParserIfVersionContainsNoNumber) { |
| 439 | SetCountryAndVersion("ZZ", "staging"); |
| 440 | // The version is used in the URL, as planned when setting it. |
| 441 | RespondWithV5JSON( |
| 442 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_staging.json", |
| 443 | {kChromium}); |
| 444 | PopularSites::SitesVector sites; |
| 445 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 446 | Eq(absl::optional<bool>(true))); |
Friedrich Horschig | b77dc44 | 2017-09-11 13:06:30 | [diff] [blame] | 447 | ASSERT_THAT(sites.size(), Eq(1u)); |
| 448 | EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); |
| 449 | } |
| 450 | |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 451 | TEST_F(PopularSitesTest, RefetchesAfterCountryMoved) { |
| 452 | RespondWithV5JSON( |
| 453 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json", |
| 454 | {kWikipedia}); |
| 455 | RespondWithV5JSON( |
| 456 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZX_5.json", |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 457 | {kChromium}); |
| 458 | |
mastiz | 78efbde | 2016-12-14 17:07:07 | [diff] [blame] | 459 | PopularSites::SitesVector sites; |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 460 | |
| 461 | // First request (in ZZ) saves Wikipedia. |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 462 | SetCountryAndVersion("ZZ", "5"); |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 463 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 464 | Eq(absl::optional<bool>(true))); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 465 | EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/")); |
| 466 | |
| 467 | // Second request (now in ZX) saves Chromium. |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 468 | SetCountryAndVersion("ZX", "5"); |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 469 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 470 | absl::optional<bool>(true)); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 471 | EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); |
| 472 | } |
| 473 | |
| 474 | TEST_F(PopularSitesTest, DoesntCacheInvalidFile) { |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 475 | SetCountryAndVersion("ZZ", "5"); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 476 | RespondWithData( |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 477 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json", |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 478 | "ceci n'est pas un json"); |
| 479 | RespondWith404( |
| 480 | "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json"); |
| 481 | |
| 482 | // First request falls back and gets nothing there either. |
mastiz | 78efbde | 2016-12-14 17:07:07 | [diff] [blame] | 483 | PopularSites::SitesVector sites; |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 484 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 485 | Eq(absl::optional<bool>(false))); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 486 | |
| 487 | // Second request refetches ZZ_9, which now has data. |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 488 | RespondWithV5JSON( |
| 489 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json", |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 490 | {kChromium}); |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 491 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 492 | Eq(absl::optional<bool>(true))); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 493 | ASSERT_THAT(sites.size(), Eq(1u)); |
| 494 | EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); |
| 495 | } |
| 496 | |
| 497 | TEST_F(PopularSitesTest, RefetchesAfterFallback) { |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 498 | SetCountryAndVersion("ZZ", "5"); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 499 | RespondWith404( |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 500 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json"); |
| 501 | RespondWithV5JSON( |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 502 | "https://www.gstatic.com/chrome/ntp/suggested_sites_DEFAULT_5.json", |
| 503 | {kWikipedia}); |
| 504 | |
| 505 | // First request falls back. |
mastiz | 78efbde | 2016-12-14 17:07:07 | [diff] [blame] | 506 | PopularSites::SitesVector sites; |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 507 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 508 | Eq(absl::optional<bool>(true))); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 509 | ASSERT_THAT(sites.size(), Eq(1u)); |
| 510 | EXPECT_THAT(sites[0].url, URLEq("https://zz.m.wikipedia.org/")); |
| 511 | |
| 512 | // Second request refetches ZZ_9, which now has data. |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 513 | RespondWithV5JSON( |
| 514 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_5.json", |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 515 | {kChromium}); |
mastiz | fd2c7ab | 2017-01-27 19:35:00 | [diff] [blame] | 516 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 517 | Eq(absl::optional<bool>(true))); |
sfiera | c49f0e4 | 2016-11-08 13:49:54 | [diff] [blame] | 518 | ASSERT_THAT(sites.size(), Eq(1u)); |
| 519 | EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 520 | } |
| 521 | |
mastiz | 7df710a | 2017-04-26 11:09:10 | [diff] [blame] | 522 | TEST_F(PopularSitesTest, ShouldOverrideDirectory) { |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 523 | SetCountryAndVersion("ZZ", "5"); |
mastiz | 7df710a | 2017-04-26 11:09:10 | [diff] [blame] | 524 | prefs_->SetString(prefs::kPopularSitesOverrideDirectory, "foo/bar/"); |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 525 | RespondWithV5JSON("https://www.gstatic.com/foo/bar/suggested_sites_ZZ_5.json", |
| 526 | {kWikipedia}); |
mastiz | 7df710a | 2017-04-26 11:09:10 | [diff] [blame] | 527 | |
| 528 | PopularSites::SitesVector sites; |
| 529 | EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 530 | Eq(absl::optional<bool>(true))); |
mastiz | 7df710a | 2017-04-26 11:09:10 | [diff] [blame] | 531 | |
| 532 | EXPECT_THAT(sites.size(), Eq(1u)); |
| 533 | } |
| 534 | |
Natalie Chouinard | 51f0eb209 | 2019-05-07 18:38:14 | [diff] [blame] | 535 | TEST_F(PopularSitesTest, DoesNotFetchExplorationSites) { |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 536 | SetCountryAndVersion("ZZ", "6"); |
| 537 | RespondWithV6JSON( |
| 538 | "https://www.gstatic.com/chrome/ntp/suggested_sites_ZZ_6.json", |
| 539 | {{SectionType::PERSONALIZED, {kChromium}}, |
| 540 | {SectionType::NEWS, {kYouTube}}}); |
| 541 | |
| 542 | std::map<SectionType, PopularSites::SitesVector> sections; |
| 543 | EXPECT_THAT(FetchAllSections(/*force_download=*/false, §ions), |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 544 | Eq(absl::optional<bool>(true))); |
Friedrich Horschig | f4818374 | 2017-08-08 16:53:13 | [diff] [blame] | 545 | |
| 546 | // The fetched news section should not be propagated without enabled feature. |
| 547 | EXPECT_THAT(sections, Not(Contains(Pair(SectionType::NEWS, _)))); |
| 548 | } |
| 549 | |
sfiera | acc10b6 | 2016-11-07 13:33:41 | [diff] [blame] | 550 | } // namespace |
| 551 | } // namespace ntp_tiles |