chromium/components/ntp_tiles/most_visited_sites_unittest.cc

// Copyright 2015 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/most_visited_sites.h"

#include <stddef.h>

#include <map>
#include <memory>
#include <ostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#include "base/callback_list.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "components/history/core/browser/top_sites.h"
#include "components/history/core/browser/top_sites_observer.h"
#include "components/ntp_tiles/custom_links_manager.h"
#include "components/ntp_tiles/features.h"
#include "components/ntp_tiles/icon_cacher.h"
#include "components/ntp_tiles/popular_sites_impl.h"
#include "components/ntp_tiles/pref_names.h"
#include "components/ntp_tiles/section_type.h"
#include "components/ntp_tiles/switches.h"
#include "components/supervised_user/core/common/buildflags.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/webapps/common/constants.h"
#include "extensions/buildflags/buildflags.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"

#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
#include "components/supervised_user/core/browser/supervised_user_preferences.h"
#endif

namespace ntp_tiles {

// Defined for googletest. Must be defined in the same namespace.
void PrintTo(const NTPTile& tile, std::ostream* os) {}

namespace {

MostVisitedURL;
MostVisitedURLList;
TopSites;
_;
AllOf;
AnyNumber;
AtLeast;
ByMove;
Contains;
DoAll;
ElementsAre;
Eq;
Ge;
InSequence;
Invoke;
IsEmpty;
Key;
Mock;
Not;
Pair;
Return;
ReturnRef;
SaveArg;
SizeIs;
StrictMock;

const char kHomepageUrl[] =;
const char16_t kHomepageTitle[] =;

std::string PrintTile(const std::u16string& title,
                      const std::string& url,
                      TileSource source) {}

MATCHER_P3(MatchesTile, title, url, source, PrintTile(title, url, source)) {}

MATCHER_P3(LastTileIs,
           title,
           url,
           source,
           std::string("last tile ") + PrintTile(title, url, source)) {}

MATCHER_P3(FirstPersonalizedTileIs,
           title,
           url,
           source,
           std::string("first tile ") + PrintTile(title, url, source)) {}

NTPTile MakeTile(const std::u16string& title,
                 const std::string& url,
                 TileSource source) {}

MostVisitedURL MakeMostVisitedURL(const std::u16string& title,
                                  const std::string& url) {}

class MockTopSites : public TopSites {};

class MockMostVisitedSitesObserver : public MostVisitedSites::Observer {};

class FakeHomepageClient : public MostVisitedSites::HomepageClient {};

class MockIconCacher : public IconCacher {};

class MockCustomLinksManager : public CustomLinksManager {};

class PopularSitesFactoryForTest {};

}  // namespace

// Param specifies whether Popular Sites is enabled via variations.
class MostVisitedSitesTest : public ::testing::TestWithParam<bool> {};

TEST_P(MostVisitedSitesTest, ShouldStartNoCallInConstructor) {}

TEST_P(MostVisitedSitesTest, ShouldRefreshBackends) {}

TEST_P(MostVisitedSitesTest, ShouldIncludeTileForHomepage) {}

TEST_P(MostVisitedSitesTest, ShouldNotIncludeHomepageWithoutClient) {}

TEST_P(MostVisitedSitesTest, ShouldIncludeHomeTileWithUrlBeforeQueryingName) {}

TEST_P(MostVisitedSitesTest, ShouldUpdateHomepageTileWhenRefreshHomepageTile) {}

TEST_P(MostVisitedSitesTest, ShouldNotIncludeHomepageIfNoTileRequested) {}

TEST_P(MostVisitedSitesTest, ShouldReturnHomepageIfOneTileRequested) {}

TEST_P(MostVisitedSitesTest, ShouldHaveHomepageFirstInListWhenFull) {}

// The following test exercises behavior with a preinstalled chrome app; this
// is only relevant if extensions and apps are enabled.
#if BUILDFLAG(ENABLE_EXTENSIONS)
TEST_P(MostVisitedSitesTest, ShouldNotContainDefaultPreinstalledApp) {}
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

TEST_P(MostVisitedSitesTest, ShouldHaveHomepageFirstInListWhenNotFull) {}

TEST_P(MostVisitedSitesTest, ShouldDeduplicateHomepageWithTopSites) {}

TEST_P(MostVisitedSitesTest, ShouldNotIncludeHomepageIfThereIsNone) {}

TEST_P(MostVisitedSitesTest, ShouldNotIncludeHomepageIfEmptyUrl) {}

TEST_P(MostVisitedSitesTest, ShouldNotIncludeHomepageIfBlocked) {}

#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
TEST_P(MostVisitedSitesTest, ShouldPinHomepageAgainIfBlockedUndone) {}
#endif

TEST_P(MostVisitedSitesTest, ShouldInformSuggestionSourcesWhenBlocked) {}

TEST_P(MostVisitedSitesTest,
       ShouldDeduplicatePopularSitesWithMostVisitedIffHostAndTitleMatches) {}

TEST_P(MostVisitedSitesTest, ShouldHandleTopSitesCacheHit) {}

// Tests that multiple observers can be added to the MostVisitedSites.
TEST_P(MostVisitedSitesTest, MultipleObservers) {}

INSTANTIATE_TEST_SUITE_P();

TEST(MostVisitedSitesTest, ShouldDeduplicateDomainWithNoWwwDomain) {}

TEST(MostVisitedSitesTest, ShouldDeduplicateDomainByRemovingMobilePrefixes) {}

TEST(MostVisitedSitesTest, ShouldDeduplicateDomainByReplacingMobilePrefixes) {}

#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
class MostVisitedSitesWithCustomLinksTest : public MostVisitedSitesTest {};

TEST_P(MostVisitedSitesWithCustomLinksTest, ChangeVisibility) {}

TEST_P(MostVisitedSitesWithCustomLinksTest,
       ShouldOnlyBuildCustomLinksWhenInitialized) {}

TEST_P(MostVisitedSitesWithCustomLinksTest,
       ShouldFavorCustomLinksOverTopSites) {}

TEST_P(MostVisitedSitesWithCustomLinksTest,
       DisableCustomLinksWhenNotInitialized) {}

TEST_P(MostVisitedSitesWithCustomLinksTest, DisableCustomLinksWhenInitialized) {}

TEST_P(MostVisitedSitesWithCustomLinksTest,
       ShouldGenerateShortTitleForTopSites) {}

// Test all delimiters
TEST_P(MostVisitedSitesWithCustomLinksTest,
       ShouldSplitTitleWithSpaceAfterDelimiter) {}

TEST_P(MostVisitedSitesWithCustomLinksTest,
       ShouldUseFullTitleIfTitleDoesNotContainDelimiterFollowedBySpace) {}

TEST_P(MostVisitedSitesWithCustomLinksTest,
       ShouldNotCrashIfReceiveAnEmptyTitle) {}

TEST_P(MostVisitedSitesWithCustomLinksTest,
       UninitializeCustomLinksOnUndoAfterFirstAction) {}

TEST_P(MostVisitedSitesWithCustomLinksTest,
       DontUninitializeCustomLinksOnUndoAfterMultipleActions) {}

TEST_P(MostVisitedSitesWithCustomLinksTest,
       UninitializeCustomLinksIfFirstActionFails) {}

TEST_P(MostVisitedSitesWithCustomLinksTest, RebuildTilesOnCustomLinksChanged) {}

// These exclude Android and iOS.
INSTANTIATE_TEST_SUITE_P();
#endif

// This a test for MostVisitedSites::MergeTiles(...) method, and thus has the
// same scope as the method itself. This tests merging popular sites with
// personal tiles.
// More important things out of the scope of testing presently:
// - Removing blocked tiles.
// - Correct host extraction from the URL.
// - Ensuring personal tiles are not duplicated in popular tiles.
TEST(MostVisitedSitesMergeTest, ShouldMergeTilesWithPersonalOnly) {}

TEST(MostVisitedSitesMergeTest, ShouldMergeTilesWithPopularOnly) {}

TEST(MostVisitedSitesMergeTest, ShouldMergeTilesFavoringPersonalOverPopular) {}

}  // namespace ntp_tiles