chromium/components/plus_addresses/plus_address_preallocator_unittest.cc

// Copyright 2024 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/plus_addresses/plus_address_preallocator.h"

#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/json/values_util.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/gmock_move_support.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/affiliations/core/browser/affiliation_utils.h"
#include "components/plus_addresses/features.h"
#include "components/plus_addresses/mock_plus_address_http_client.h"
#include "components/plus_addresses/plus_address_allocator.h"
#include "components/plus_addresses/plus_address_http_client.h"
#include "components/plus_addresses/plus_address_prefs.h"
#include "components/plus_addresses/plus_address_test_utils.h"
#include "components/plus_addresses/plus_address_types.h"
#include "components/plus_addresses/settings/fake_plus_address_setting_service.h"
#include "components/prefs/testing_pref_service.h"
#include "net/http/http_status_code.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace plus_addresses {

namespace {

RunOnceCallback;
CreatePreallocatedPlusAddress;
_;
InSequence;
IsEmpty;
MockFunction;
NiceMock;
SizeIs;
UnorderedElementsAre;

PlusProfileOrError PlusProfileFromPreallocatedAddress(
    const url::Origin& origin,
    std::string plus_address) {}

MATCHER_P2(IsPreallocatedPlusAddress, end_of_life, address, "") {}

base::RepeatingCallback<bool()> AlwaysEnabled() {}

base::RepeatingCallback<bool()> NeverEnabled() {}

}  // namespace

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

// Tests that plus addresses with an end of life in the future are not pruned on
// creation of the `PlusAddressPreallocator`.
TEST_F(PlusAddressPreallocatorTest,
       PrunePreallocatedPlusAddressesWithEolInFuture) {}

// Tests that plus addresses with an end of life in the past are pruned on
// creation of the `PlusAddressPreallocator` and the index of the next plus
// address is set to 0 if no entries remain.
TEST_F(PlusAddressPreallocatorTest,
       PrunePreallocatedPlusAddressesWithEolInPast) {}

// Tests that an invalid index for the next pre-allocated plus address is fixed
// during pruning.
TEST_F(PlusAddressPreallocatorTest,
       PrunePreallocatedPlusAddressesFixesNextIndex) {}

// Tests that plus addresses with an end of life in the past are pruned on
// creation of the `PlusAddressPreallocator` and the index of the next plus
// address is in bounds.
TEST_F(PlusAddressPreallocatorTest,
       PrunePreallocatedPlusAddressesWithMixedEols) {}

// Tests that an empty cache means that the next allocation is not synchronous.
TEST_F(PlusAddressPreallocatorTest, IsAllocationSychronousNoPlusAddresses) {}

// Tests that a cache of only outdated plus addresses means that the next
// allocation is not synchronous.
TEST_F(PlusAddressPreallocatorTest,
       IsAllocationSychronousOutdatedPlusAddresses) {}

// Tests that valid plus addresses in the cache mean that the next allocation
// will be synchronous.
TEST_F(PlusAddressPreallocatorTest, IsAllocationSychronousValidPlusAddresses) {}

// Tests that preallocated plus addresses are requested on startup if there are
// fewer than the minimum size present.
TEST_F(PlusAddressPreallocatorTest, RequestPreallocatedAddressesOnStartup) {}

// Tests that no preallocated plus addresses are requested on startup if the
// "enabled check" returns false.
TEST_F(PlusAddressPreallocatorTest,
       DoNotRequestPreallocatedAddressesOnStartupWhenFeatureIsDisabled) {}

// Tests that no preallocated plus addresses are requested on startup if the
// notice screen has not yet been accepted.
TEST_F(PlusAddressPreallocatorTest,
       DoNotRequestPreallocatedAddressesOnStartupWhenNoticeNotAccepted) {}

// Tests that no preallocated plus addresses are requested on startup if the
// global toggle is off.
TEST_F(PlusAddressPreallocatorTest,
       DoNotRequestPreallocatedAddressesOnStartupWhenGlobalToggleOff) {}

// Tests that no addresses are requested on startup if there are already enough
// present.
TEST_F(PlusAddressPreallocatorTest,
       DoNotRequestPreallocatedAddressesOnStartup) {}

// Tests that errors returned from the preallocation call are handled.
TEST_F(PlusAddressPreallocatorTest, HandleNetworkError) {}

// Tests that encountering timeout errors leads to retries at appropriate
// intervals.
TEST_F(PlusAddressPreallocatorTest, RetryOnTimeout) {}

// Tests that the back-off time for failed retries is ignored if there is an
// explicit allocation call. This makes sure that if Chrome is started when
// offline, the client cannot end up in a situation where no additional
// pre-allocate calls are made even if the user is actively trying to create a
// plus address.
TEST_F(PlusAddressPreallocatorTest, NoBackoffPeriodForUserTriggeredRequests) {}

// Tests that trying to allocate a plus address for an opaque origin results in
// an error.
TEST_F(PlusAddressPreallocatorTest, AllocatePlusAddressForOpaqueOrigin) {}

// Tests that trying to allocate a plus address while the global toggle is off
// results in an error.
TEST_F(PlusAddressPreallocatorTest, AllocatePlusAddressWithToggleOff) {}

// Tests that trying to allocate a plus address while the `IsEnabledCheck` is
// false results in an error.
TEST_F(PlusAddressPreallocatorTest, AllocatePlusAddressWithServiceDisabled) {}

// Tests that allocating plus addresses returns pre-allocated plus addresses
// from the pool and cycles through them.
TEST_F(PlusAddressPreallocatorTest, AllocatePlusAddress) {}

// Tests that calling `AllocatePlusAddress` removes outdated pre-allocated
// addresses and requests new ones if the remaining ones are less than the
// minimum size of the pre-allocation pool. It also checks that there no
// additional server requests are made if one is already ongoing.
TEST_F(PlusAddressPreallocatorTest,
       AllocatePlusAddressWithPreallocationAfterSomeExpire) {}

TEST_F(PlusAddressPreallocatorTest,
       AllocatePlusAddressWithPreallocationAfterAllExpire) {}

// Tests that removing plus addresses works correctly.
TEST_F(PlusAddressPreallocatorTest, RemoveAllocatedPlusAddress) {}

// Tests that errors encountered during allocation are forward to pending
// allocation requests.
TEST_F(PlusAddressPreallocatorTest, ErrorDuringAllocationRequest) {}

}  // namespace plus_addresses