chromium/components/optimization_guide/core/hints_manager_unittest.cc

// Copyright 2019 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/optimization_guide/core/hints_manager.h"

#include <optional>
#include <string>
#include <utility>

#include "base/base64.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/functional/callback_helpers.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/gtest_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/optimization_guide/core/bloom_filter.h"
#include "components/optimization_guide/core/hint_cache.h"
#include "components/optimization_guide/core/hints_component_util.h"
#include "components/optimization_guide/core/hints_fetcher.h"
#include "components/optimization_guide/core/hints_fetcher_factory.h"
#include "components/optimization_guide/core/optimization_guide_constants.h"
#include "components/optimization_guide/core/optimization_guide_enums.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/core/optimization_guide_logger.h"
#include "components/optimization_guide/core/optimization_guide_navigation_data.h"
#include "components/optimization_guide/core/optimization_guide_prefs.h"
#include "components/optimization_guide/core/optimization_guide_store.h"
#include "components/optimization_guide/core/optimization_guide_switches.h"
#include "components/optimization_guide/core/proto_database_provider_test_base.h"
#include "components/optimization_guide/core/tab_url_provider.h"
#include "components/optimization_guide/core/top_host_provider.h"
#include "components/optimization_guide/proto/hints.pb.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/signin/public/identity_manager/test_identity_manager_observer.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/ukm/test_ukm_recorder.h"
#include "components/unified_consent/unified_consent_service.h"
#include "components/variations/scoped_variations_ids_provider.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_source.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/gtest/include/gtest/gtest.h"

namespace optimization_guide {
namespace {

// Allows for default hour to pass + random delay between 30 and 60 seconds.
constexpr int kUpdateFetchHintsTimeSecs =;  // 1 hours and 1 minutes.

const int kDefaultHostBloomFilterNumHashFunctions =;
const int kDefaultHostBloomFilterNumBits =;

void PopulateBloomFilterWithDefaultHost(BloomFilter* bloom_filter) {}

void AddBloomFilterToConfig(proto::OptimizationType optimization_type,
                            const BloomFilter& bloom_filter,
                            int num_hash_functions,
                            int num_bits,
                            bool is_allowlist,
                            proto::Configuration* config) {}

std::unique_ptr<proto::GetHintsResponse> BuildHintsResponse(
    const std::vector<std::string>& hosts,
    const std::vector<std::string>& urls) {}

void RunHintsFetchedCallbackWithResponse(
    HintsFetchedCallback hints_fetched_callback,
    std::unique_ptr<proto::GetHintsResponse> response) {}

// Returns the default params used for the kOptimizationHints feature.
base::FieldTrialParams GetOptimizationHintsDefaultFeatureParams() {}

std::unique_ptr<base::test::ScopedFeatureList>
SetUpDeferStartupActiveTabsHintsFetch(bool is_enabled) {}

}  // namespace

// A mock class implementation of TopHostProvider.
class FakeTopHostProvider : public TopHostProvider {};

// A mock class implementation of TabUrlProvider.
class FakeTabUrlProvider : public TabUrlProvider {};

enum class HintsFetcherEndState {};

// A mock class implementation of HintsFetcher. It will iterate through the
// provided fetch states each time it is called. If it reaches the end of the
// loop, it will just continue using the last fetch state.
class TestHintsFetcher : public HintsFetcher {};

// A mock class of HintsFetcherFactory that returns instances of
// TestHintsFetchers with the provided fetch state.
class TestHintsFetcherFactory : public HintsFetcherFactory {};

class HintsManagerTest : public ProtoDatabaseProviderTestBase {};

TEST_F(HintsManagerTest, ProcessHintsWithValidCommandLineOverride) {}

TEST_F(HintsManagerTest, ProcessHintsWithInvalidCommandLineOverride) {}

TEST_F(HintsManagerTest,
       ProcessHintsWithCommandLineOverrideShouldNotBeOverriddenByNewComponent) {}

TEST_F(HintsManagerTest, ParseTwoConfigVersions) {}

TEST_F(HintsManagerTest, ParseInvalidConfigVersions) {}

TEST_F(HintsManagerTest, ComponentProcessingWhileShutdown) {}

TEST_F(HintsManagerTest, ParseOlderConfigVersions) {}

TEST_F(HintsManagerTest, ParseDuplicateConfigVersions) {}

TEST_F(HintsManagerTest, ComponentInfoDidNotContainConfig) {}

TEST_F(HintsManagerTest, ProcessHintsWithExistingPref) {}

TEST_F(HintsManagerTest,
       ProcessHintsWithExistingPrefDoesNotClearOrCountAsMidProcessing) {}

TEST_F(HintsManagerTest, ProcessHintsWithInvalidPref) {}

TEST_F(HintsManagerTest, ProcessHintsUpdatePreviousOptTypesWithFilter) {}

TEST_F(HintsManagerTest,
       OnNavigationStartOrRedirectNoTypesRegisteredShouldNotLoadHint) {}

TEST_F(HintsManagerTest, OnNavigationStartOrRedirectWithHint) {}

TEST_F(HintsManagerTest, OnNavigationStartOrRedirectNoHint) {}

TEST_F(HintsManagerTest, OnNavigationStartOrRedirectNoHost) {}

TEST_F(HintsManagerTest, OptimizationFiltersAreOnlyLoadedIfTypeIsRegistered) {}

TEST_F(HintsManagerTest, OptimizationFiltersOnlyLoadOncePerType) {}

TEST_F(HintsManagerTest, InvalidOptimizationFilterNotLoaded) {}

TEST_F(HintsManagerTest, CanApplyOptimizationUrlWithNoHost) {}

TEST_F(HintsManagerTest,
       CanApplyOptimizationHasFilterForTypeButNotLoadedYet_ComponentReady) {}

TEST_F(HintsManagerTest,
       CanApplyOptimizationHasFilterForTypeButNotLoadedYet_ComponentNotReady) {}

TEST_F(HintsManagerTest,
       CanApplyOptimizationHasLoadedFilterForTypeUrlInAllowlist) {}

TEST_F(HintsManagerTest,
       CanApplyOptimizationHasLoadedFilterForTypeUrlInBlocklist) {}

TEST_F(HintsManagerTest,
       CanApplyOptimizationHasLoadedFilterForTypeUrlNotInAllowlistFilter) {}

TEST_F(HintsManagerTest,
       CanApplyOptimizationHasLoadedFilterForTypeUrlNotInBlocklistFilter) {}

TEST_F(HintsManagerTest,
       CanApplyOptimizationOptimizationTypeAllowlistedAtTopLevel) {}

TEST_F(HintsManagerTest, CanApplyOptimizationHasPageHintButNoMatchingOptType) {}

TEST_F(HintsManagerTest,
       CanApplyOptimizationAndPopulatesLoadingPredictorMetadata) {}

TEST_F(HintsManagerTest, CanApplyOptimizationAndPopulatesAnyMetadata) {}

TEST_F(HintsManagerTest, CanApplyOptimizationNoMatchingPageHint) {}

TEST_F(HintsManagerTest,
       CanApplyOptimizationNoHintForNavigationMetadataClearedAnyway) {}

TEST_F(HintsManagerTest, CanApplyOptimizationHasHintInCacheButNotLoaded) {}

TEST_F(HintsManagerTest, CanApplyOptimizationFilterTakesPrecedence) {}

TEST_F(HintsManagerTest,
       CanApplyOptimizationFilterTakesPrecedenceMatchesFilter) {}

class HintsManagerFetchingDisabledTest : public HintsManagerTest {};

TEST_F(HintsManagerFetchingDisabledTest,
       HintsFetchNotAllowedIfFeatureIsNotEnabled) {}

TEST_F(HintsManagerTest,
       CanApplyOptimizationAsyncReturnsRightAwayIfNotAllowedToFetch) {}

TEST_F(
    HintsManagerTest,
    CanApplyOptimizationAsyncReturnsRightAwayIfNotAllowedToFetchAndNotAllowlistedByAvailableHint) {}

TEST_F(HintsManagerTest, RemoveFetchedEntriesByHintKeys_Host) {}

TEST_F(HintsManagerTest, RemoveFetchedEntriesByHintKeys_URL) {}

TEST_F(HintsManagerTest, HintFetcherPrefUpdated_URL) {}

TEST_F(HintsManagerTest, HintFetcherPrefUpdated_Hosts) {}

class HintsManagerFetchingTest : public HintsManagerTest {};

TEST_F(HintsManagerFetchingTest, BatchUpdateFetcherCleanup) {}

TEST_F(HintsManagerFetchingTest,
       HintsFetchNotAllowedIfFeatureIsEnabledButUserNotAllowed) {}

TEST_F(HintsManagerFetchingTest,
       NoRegisteredOptimizationTypesAndHintsFetchNotAttempted) {}

TEST_F(HintsManagerFetchingTest,
       OnlyFilterTypesRegisteredHintsFetchNotAttempted) {}

TEST_F(HintsManagerFetchingTest, HintsFetcherEnabledNoHostsOrUrlsToFetch) {}

TEST_F(HintsManagerFetchingTest, HintsFetcherEnabledNoHostsButHasUrlsToFetch) {}

// Verifies hints for active tab URLs is not fetched immediately on startup. It
// should be fetched after a random delay for the first time, and then continue
// to be fetched.
TEST_F(HintsManagerFetchingTest, HintsFetcherTimerFetchOnStartup) {}

// Verifies the deferred startup mode that fetches hints for active tab URLs on
// deferred startup (but not on immediate startup). It should continue to be
// fetched after a refresh duration.
TEST_F(HintsManagerFetchingTest, HintsFetcherDeferredStartup) {}

TEST_F(HintsManagerFetchingTest,
       HintsFetched_RegisteredOptimizationTypes_AllWithOptFilter) {}

TEST_F(HintsManagerFetchingTest, HintsFetchedAtNavigationTime) {}

TEST_F(HintsManagerFetchingTest,
       HintsFetchedAtNavigationTime_FetchNotAttempted) {}

TEST_F(HintsManagerFetchingTest,
       HintsFetchedAtNavigationTime_HasComponentHintButNotFetched) {}

TEST_F(HintsManagerFetchingTest,
       HintsFetchedAtNavigationTime_DoesNotRemoveManualOverride) {}

TEST_F(HintsManagerFetchingTest, URLHintsNotFetchedAtNavigationTime) {}

TEST_F(HintsManagerFetchingTest, URLWithNoHintsNotRefetchedAtNavigationTime) {}

TEST_F(HintsManagerFetchingTest, CanApplyOptimizationCalledMidFetch) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationCalledPostFetchButNoHintsCameBack) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationCalledPostFetchButFetchFailed) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationWithURLKeyedHintApplicableForOptimizationType) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationNotAllowedByURLButAllowedByHostKeyedHint) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationNotAllowedByURLOrHostKeyedHint) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationNoURLKeyedHintOrHostKeyedHint) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationCalledMidFetchForURLKeyedOptimization) {}

TEST_F(HintsManagerFetchingTest,
       OnNavigationStartOrRedirectWontInitiateFetchIfAlreadyStartedForTheURL) {}

TEST_F(HintsManagerFetchingTest,
       PageNavigationHintsFetcherGetsCleanedUpOnceHintsAreStored) {}

TEST_F(HintsManagerFetchingTest,
       PageNavigationHintsFetcherCanFetchMultipleThingsConcurrently) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationNewAPIDecisionComesFromInFlightURLHint) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationNewAPIRequestFailsBeforeFetch) {}

TEST_F(HintsManagerFetchingTest, CanApplyOptimizationNewAPICalledPostFetch) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationAsyncDecisionComesFromInFlightURLHint) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationAsyncMultipleCallbacksRegisteredForSameTypeAndURL) {}

TEST_F(
    HintsManagerFetchingTest,
    CanApplyOptimizationAsyncDecisionComesFromInFlightURLHintNotAllowlisted) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationAsyncFetchFailsDoesNotStrandCallbacks) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationAsyncInfoAlreadyInPriorToCall) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationAsyncInfoAlreadyInPriorToCallAndNotAllowlisted) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationAsyncHintComesInAndNotAllowlisted) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationAsyncDoesNotStrandCallbacksAtBeginningOfChain) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationAsyncDoesNotStrandCallbacksIfFetchNotPending) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationAsyncWithDecisionFromAllowlistReturnsRightAway) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationAsyncWithDecisionFromBlocklistReturnsRightAway) {}

TEST_F(HintsManagerFetchingTest,
       OnNavigationFinishDoesNotPrematurelyInvokeRegisteredCallbacks) {}

TEST_F(HintsManagerFetchingTest,
       OnNavigationFinishDoesNotCrashWithoutAnyCallbacksRegistered) {}

TEST_F(HintsManagerFetchingTest, NewOptTypeRegisteredClearsHintCache) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationOnDemandDecisionMetadataComesFromFetch) {}

TEST_F(HintsManagerFetchingTest, BatchUpdateCalledMoreThanMaxConcurrent) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationOnDemandNoRegistrationAlwaysFetches) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationOnDemandNoRegistrationFetchFailure) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationOnDemandMixedRegistrations) {}

TEST_F(
    HintsManagerFetchingTest,
    CanApplyOptimizationOnDemandDecisionMultipleTypesBothHostAndURLKeyedMixedFetch) {}

TEST_F(HintsManagerFetchingTest,
       CanApplyOptimizationOnDemandDecisionFailedFetchDoesNotStrandCallback) {}

// RequestContextMetadata will be sent in fetcher only for appropriate request
// context.
TEST_F(HintsManagerFetchingTest,
       PageInsightsHubContextRequestContextMetadataPihSentGetHintsRequest) {}

// RequestContextMetadata will not be sent in fetcher when the request context
// is not enabled for it.
TEST_F(
    HintsManagerFetchingTest,
    PageInsightsHubContextNotSentRequestContextMetadataPihSentGetHintsRequest) {}

// Tests the null RequestContextMetadata case.
TEST_F(HintsManagerFetchingTest,
       PageInsightsHubContextRequestContextMetadataPihNotSentGetHintsRequest) {}

class HintsManagerFetchingNoBatchUpdateTest : public HintsManagerTest {};

TEST_F(HintsManagerFetchingNoBatchUpdateTest,
       BatchUpdateHintsFetchNotScheduledIfNotAllowed) {}

class HintsManagerComponentSkipProcessingTest : public HintsManagerTest {};

TEST_F(HintsManagerComponentSkipProcessingTest, ProcessHintsWithExistingPref) {}

class HintsManagerPersonalizedFetchingTest : public HintsManagerFetchingTest {};

// TODO(crbug.com/41482478): test is failing on iPhone device.
#if TARGET_OS_IOS && !TARGET_IPHONE_SIMULATOR
#define MAYBE_SuccessfulPersonalizedHintsFetching
#else
#define MAYBE_SuccessfulPersonalizedHintsFetching
#endif
TEST_F(HintsManagerPersonalizedFetchingTest,
       MAYBE_SuccessfulPersonalizedHintsFetching) {}

TEST_F(HintsManagerPersonalizedFetchingTest, TokenFailure) {}

TEST_F(HintsManagerPersonalizedFetchingTest, NoUserSignIn) {}

}  // namespace optimization_guide