chromium/components/ntp_tiles/popular_sites_impl_unittest.cc

// 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"

_;
Contains;
ElementsAre;
Eq;
Gt;
IsEmpty;
Not;
Pair;
SizeIs;

namespace ntp_tiles {
namespace {

const char kTitle[] =;
const char kUrl[] =;
const char kLargeIconUrl[] =;
const char kFaviconUrl[] =;
const char kSection[] =;
const char kSites[] =;
const char kTitleSource[] =;
#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

TestPopularSite;
TestPopularSiteVector;
TestPopularSection;
TestPopularSectionVector;

::testing::Matcher<const std::u16string&> Str16Eq(const std::string& s) {}

::testing::Matcher<const GURL&> URLEq(const std::string& s) {}

size_t GetNumberOfDefaultPopularSitesForPlatform() {}

class PopularSitesTest : public ::testing::Test {};

TEST_F(PopularSitesTest, ContainsDefaultTilesRightAfterConstruction) {}

TEST_F(PopularSitesTest, IsEmptyOnConstructionIfDisabledByTrial) {}

TEST_F(PopularSitesTest, ShouldSucceedFetching) {}

#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) {}

TEST_F(PopularSitesTest, PopulatesWithDefaultResoucesOnFailure) {}

#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) {}

TEST_F(PopularSitesTest, UsesCachedJson) {}

TEST_F(PopularSitesTest, CachesEmptyFile) {}

TEST_F(PopularSitesTest, DoesntUseCachedFileIfDownloadForced) {}

TEST_F(PopularSitesTest, DoesntUseCacheWithDeprecatedVersion) {}

TEST_F(PopularSitesTest, FallsBackToDefaultParserIfVersionContainsNoNumber) {}

TEST_F(PopularSitesTest, RefetchesAfterCountryMoved) {}

TEST_F(PopularSitesTest, DoesntCacheInvalidFile) {}

TEST_F(PopularSitesTest, RefetchesAfterFallback) {}

TEST_F(PopularSitesTest, ShouldOverrideDirectory) {}

TEST_F(PopularSitesTest, DoesNotFetchExplorationSites) {}

}  // namespace
}  // namespace ntp_tiles