chromium/components/affiliations/core/browser/affiliation_backend_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/affiliations/core/browser/affiliation_backend.h"

#include <stddef.h>

#include <memory>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "base/test/test_mock_time_task_runner.h"
#include "base/test/test_simple_task_runner.h"
#include "base/time/clock.h"
#include "base/time/tick_clock.h"
#include "components/affiliations/core/browser/affiliation_database.h"
#include "components/affiliations/core/browser/affiliation_fetch_throttler.h"
#include "components/affiliations/core/browser/affiliation_fetch_throttler_delegate.h"
#include "components/affiliations/core/browser/affiliation_utils.h"
#include "components/affiliations/core/browser/facet_manager.h"
#include "components/affiliations/core/browser/fake_affiliation_api.h"
#include "components/affiliations/core/browser/mock_affiliation_consumer.h"
#include "components/variations/scoped_variations_ids_provider.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_network_connection_tracker.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace affiliations {

namespace {

StrategyOnCacheMiss;

// Mock fetch throttler that has some extra logic to accurately portray the real
// AffiliationFetchThrottler in how it ignores SignalNetworkRequestNeeded()
// requests when a request is already known to be needed or one is already in
// flight, and in how it goes back to the idle state afterwards.
class MockAffiliationFetchThrottler : public AffiliationFetchThrottler {};

const char kTestFacetURIAlpha1[] =;
const char kTestFacetURIAlpha2[] =;
const char kTestFacetURIAlpha3[] =;
const char kTestFacetURIAlpha4[] =;
const char kTestFacetNameAlpha4[] =;
const char kTestFacetIconURLAlpha4[] =;
const char kTestFacetURIBeta1[] =;
const char kTestFacetURIBeta2[] =;
const char kTestFacetURIGamma1[] =;

AffiliatedFacets GetTestEquivalenceClassAlpha() {}

AffiliatedFacets GetTestEquivalenceClassBeta() {}

AffiliatedFacets GetTestEquivalenceClassGamma() {}

GroupedFacets GetTestGropingAlpha() {}

GroupedFacets GetTestGropingBeta() {}

base::TimeDelta GetCacheHardExpiryPeriod() {}

base::TimeDelta GetCacheSoftExpiryPeriod() {}

base::TimeDelta GetShortTestPeriod() {}

// Returns a smallest time difference that this test cares about.
base::TimeDelta Epsilon() {}

}  // namespace

class AffiliationBackendTest : public testing::Test {};

TEST_F(AffiliationBackendTest, OnDemandRequestSucceedsWithFetch) {}

// This test also verifies that the FacetManager is immediately discarded.
TEST_F(AffiliationBackendTest, CachedOnlyRequestFailsDueToCacheMiss) {}

TEST_F(AffiliationBackendTest, PrefetchTriggersInitialFetch) {}

// This test also verifies that the FacetManager is immediately discarded.
TEST_F(AffiliationBackendTest, ExpiredPrefetchTriggersNoInitialFetch) {}

// One additional GetAffiliationsAndBranding() and one Prefetch() request come
// in, both for unrelated facets, shortly after an initial
// GetAffiliationsAndBranding() request.
//
// Suppose that the network request triggered by the first
// GetAffiliationsAndBranding() request has already been initiated when the
// other requests arrive. As there should be no simultaneous requests, the
// additional facets should be queried together in a second fetch after the
// first fetch completes.
TEST_F(AffiliationBackendTest, ConcurrentUnrelatedRequests) {}

// Now suppose that the first fetch is somewhat delayed (e.g., because network
// requests are throttled), so the other requests arrive before it is actually
// issued. In this case, all facet URIs should be queried together in one fetch.
TEST_F(AffiliationBackendTest, ConcurrentUnrelatedRequests2) {}

TEST_F(AffiliationBackendTest, RetryIsMadeOnFailedFetch) {}

// The Prefetch() request expires before fetching corresponding affiliation
// information would be allowed. The fetch should be abandoned.
TEST_F(AffiliationBackendTest, FetchIsNoLongerNeededOnceAllowed) {}

TEST_F(AffiliationBackendTest, CacheServesSubsequentRequestForSameFacet) {}

TEST_F(AffiliationBackendTest, CacheServesSubsequentRequestForAffiliatedFacet) {}

TEST_F(AffiliationBackendTest, CacheServesRequestsForPrefetchedFacets) {}

TEST_F(AffiliationBackendTest,
       CacheServesRequestsForFacetsAffiliatedWithPrefetchedFacets) {}

// A second GetAffiliationsAndBranding() request for the same facet and a third
// request for an affiliated facet comes in while the network fetch triggered by
// the first request is in flight.
//
// There should be no simultaneous requests, and once the fetch completes, all
// three requests should be served without further fetches (they have the data).
TEST_F(AffiliationBackendTest,
       CacheServesConcurrentRequestsForAffiliatedFacets) {}

// A second Prefetch() request for the same facet and a third request for an
// affiliated facet comes in while the initial fetch triggered by the first
// request is in flight.
//
// There should be no simultaneous requests, and once the fetch completes, there
// should be no further initial fetches as the data needed is already there.
TEST_F(AffiliationBackendTest,
       CacheServesConcurrentPrefetchesForAffiliatedFacets) {}

TEST_F(AffiliationBackendTest, SimpleCacheExpiryWithoutPrefetches) {}

// A Prefetch() request for a finite period. It should trigger an initial fetch
// and exactly one refetch, as the Prefetch() request expires exactly when the
// cached data obtained with the refetch expires.
TEST_F(AffiliationBackendTest,
       PrefetchTriggersOneInitialFetchAndOneRefetchBeforeExpiring) {}

// Affiliation data for prefetched facets should be automatically refetched once
// every 23 hours, and GetAffiliationsAndBranding() requests regarding
// affiliated facets should be continuously served from cache.
TEST_F(AffiliationBackendTest, PrefetchTriggersPeriodicRefetch) {}

TEST_F(AffiliationBackendTest,
       PrefetchTriggersNoInitialFetchIfDataIsAlreadyFresh) {}

TEST_F(AffiliationBackendTest, CancelPrefetch) {}

TEST_F(AffiliationBackendTest, CancelDuplicatePrefetch) {}

// Canceling a non-existing prefetch request for a non-prefetched facet.
TEST_F(AffiliationBackendTest, CancelingNonExistingPrefetchIsSilentlyIgnored) {}

// Verify that TrimCacheForFacetURI() only removes the equivalence class for the
// given facet, and preserves others (even if they could be discarded).
TEST_F(AffiliationBackendTest,
       TrimCacheForFacetURIOnlyRemovesDataForTheGivenFacet) {}

TEST_F(AffiliationBackendTest, NothingExplodesWhenShutDownDuringFetch) {}

TEST_F(AffiliationBackendTest,
       FailureCallbacksAreCalledIfBackendIsDestroyedWithPendingRequest) {}

TEST_F(AffiliationBackendTest, DeleteCache) {}

TEST_F(AffiliationBackendTest, KeepPrefetchForFacets) {}

TEST_F(AffiliationBackendTest, GetGroupingWith) {}

TEST_F(AffiliationBackendTest, SingleGroupForAffiliatedFacets) {}

TEST_F(AffiliationBackendTest, UpdateAffiliationsAndBrandingClearsOldCache) {}

TEST_F(AffiliationBackendTest, UpdateAffiliationsAndBrandingSuccess) {}

TEST_F(AffiliationBackendTest, UpdateAffiliationsAndBrandingFailure) {}

TEST_F(AffiliationBackendTest, UpdateAffiliationsAndBrandingFailsIfNoInternet) {}

TEST_F(AffiliationBackendTest, GetGroupingInfoInjectsGroupsForMissingFacets) {}

TEST_F(AffiliationBackendTest, GetGroupingInfoWithDuplicates) {}

TEST_F(AffiliationBackendTest, GetGroupingInfoForInvalidFacet) {}

TEST_F(AffiliationBackendTest,
       UpdateAffiliationsAndBrandingSkipsInvalidFacets) {}

TEST_F(AffiliationBackendTest, GroupsUpdatedByMainDomain) {}

}  // namespace affiliations