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