1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/quick_pair/feature_status_tracker/fast_pair_enabled_provider.h"
#include <memory>
#include "ash/constants/ash_features.h"
#include "ash/quick_pair/common/fake_bluetooth_adapter.h"
#include "ash/quick_pair/feature_status_tracker/bluetooth_enabled_provider.h"
#include "ash/quick_pair/feature_status_tracker/fast_pair_pref_enabled_provider.h"
#include "ash/quick_pair/feature_status_tracker/logged_in_user_enabled_provider.h"
#include "ash/quick_pair/feature_status_tracker/mock_bluetooth_enabled_provider.h"
#include "ash/quick_pair/feature_status_tracker/mock_fast_pair_pref_enabled_provider.h"
#include "ash/quick_pair/feature_status_tracker/mock_google_api_key_availability_provider.h"
#include "ash/quick_pair/feature_status_tracker/mock_logged_in_user_enabled_provider.h"
#include "ash/quick_pair/feature_status_tracker/mock_scanning_enabled_provider.h"
#include "ash/quick_pair/feature_status_tracker/mock_screen_state_enabled_provider.h"
#include "ash/quick_pair/feature_status_tracker/screen_state_enabled_provider.h"
#include "ash/test/ash_test_base.h"
#include "base/functional/callback.h"
#include "base/memory/ptr_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "testing/gtest/include/gtest/gtest-param-test.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr int kNumFastPairEnabledProviderArgs = 6;
} // namespace
namespace ash::quick_pair {
class FastPairEnabledProviderTest : public AshTestBase {
public:
void SetUp() override {
AshTestBase::SetUp();
adapter_ = base::MakeRefCounted<FakeBluetoothAdapter>();
device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
}
protected:
scoped_refptr<FakeBluetoothAdapter> adapter_;
};
TEST_F(FastPairEnabledProviderTest, ProviderCallbackIsInvokedOnBTChanges) {
base::test::ScopedFeatureList feature_list{features::kFastPair};
base::MockCallback<base::RepeatingCallback<void(bool)>> callback;
EXPECT_CALL(callback, Run(true));
auto* fast_pair_pref_enabled_provider = new MockFastPairPrefEnabledProvider();
ON_CALL(*fast_pair_pref_enabled_provider, is_enabled)
.WillByDefault(testing::Return(true));
auto* logged_in_user_enabled_provider = new MockLoggedInUserEnabledProvider();
ON_CALL(*logged_in_user_enabled_provider, is_enabled)
.WillByDefault(testing::Return(true));
auto* screen_state_enabled_provider = new MockScreenStateEnabledProvider();
ON_CALL(*screen_state_enabled_provider, is_enabled)
.WillByDefault(testing::Return(true));
auto* google_api_key_availability_provider =
new MockGoogleApiKeyAvailabilityProvider();
ON_CALL(*google_api_key_availability_provider, is_enabled)
.WillByDefault(testing::Return(true));
auto* scanning_enabled_provider = new MockScanningEnabledProvider();
ON_CALL(*scanning_enabled_provider, is_enabled)
.WillByDefault(testing::Return(true));
auto provider = std::make_unique<FastPairEnabledProvider>(
std::make_unique<BluetoothEnabledProvider>(),
base::WrapUnique(fast_pair_pref_enabled_provider),
base::WrapUnique(logged_in_user_enabled_provider),
base::WrapUnique(screen_state_enabled_provider),
base::WrapUnique(google_api_key_availability_provider),
base::WrapUnique(scanning_enabled_provider));
provider->SetCallback(callback.Get());
adapter_->SetBluetoothIsPowered(true);
}
TEST_F(FastPairEnabledProviderTest, IsEnabledWhenExpected) {
base::test::ScopedFeatureList feature_list;
const base::flat_map<base::test::FeatureRef, bool> feature_states{
{features::kFastPair, true}};
feature_list.InitWithFeatureStates(feature_states);
auto* bluetooth_enabled_provider = new MockBluetoothEnabledProvider();
ON_CALL(*bluetooth_enabled_provider, is_enabled)
.WillByDefault(testing::Return(true));
auto* fast_pair_pref_enabled_provider = new MockFastPairPrefEnabledProvider();
ON_CALL(*fast_pair_pref_enabled_provider, is_enabled)
.WillByDefault(testing::Return(true));
auto* logged_in_user_enabled_provider = new MockLoggedInUserEnabledProvider();
ON_CALL(*logged_in_user_enabled_provider, is_enabled)
.WillByDefault(testing::Return(true));
auto* screen_state_enabled_provider = new MockScreenStateEnabledProvider();
ON_CALL(*screen_state_enabled_provider, is_enabled)
.WillByDefault(testing::Return(true));
auto* google_api_key_availability_provider =
new MockGoogleApiKeyAvailabilityProvider();
ON_CALL(*google_api_key_availability_provider, is_enabled)
.WillByDefault(testing::Return(true));
auto* scanning_enabled_provider = new MockScanningEnabledProvider();
ON_CALL(*scanning_enabled_provider, is_enabled)
.WillByDefault(testing::Return(true));
auto provider = std::make_unique<FastPairEnabledProvider>(
std::unique_ptr<BluetoothEnabledProvider>(bluetooth_enabled_provider),
std::unique_ptr<FastPairPrefEnabledProvider>(
fast_pair_pref_enabled_provider),
std::unique_ptr<LoggedInUserEnabledProvider>(
logged_in_user_enabled_provider),
std::unique_ptr<ScreenStateEnabledProvider>(
screen_state_enabled_provider),
base::WrapUnique(google_api_key_availability_provider),
base::WrapUnique(scanning_enabled_provider));
EXPECT_TRUE(provider->is_enabled());
}
class FastPairEnabledProviderTestNoCrashOnNullInputs
: public FastPairEnabledProviderTest,
public testing::WithParamInterface<size_t> {};
TEST_P(FastPairEnabledProviderTestNoCrashOnNullInputs, NoCrashOnNullInputs) {
base::test::ScopedFeatureList feature_list;
const base::flat_map<base::test::FeatureRef, bool> feature_states{
{features::kFastPair, true}};
feature_list.InitWithFeatureStates(feature_states);
size_t args_mask = GetParam();
bool is_bluetooth_provider_nonnull = args_mask & 1;
bool is_fast_pair_pref_nonnull = args_mask & 2;
bool is_user_enabled_provider_nonnull = args_mask & 4;
bool is_screen_enabled_provider_nonnull = args_mask & 8;
bool is_api_provider_nonnull = args_mask & 16;
bool is_scanning_provider_nonnull = args_mask & 32;
std::unique_ptr<BluetoothEnabledProvider> bluetooth_enabled_provider;
if (is_bluetooth_provider_nonnull) {
bluetooth_enabled_provider = std::make_unique<BluetoothEnabledProvider>();
}
MockFastPairPrefEnabledProvider* fast_pair_pref_enabled_provider = nullptr;
if (is_fast_pair_pref_nonnull) {
fast_pair_pref_enabled_provider = new MockFastPairPrefEnabledProvider();
}
MockLoggedInUserEnabledProvider* logged_in_user_enabled_provider = nullptr;
if (is_user_enabled_provider_nonnull) {
logged_in_user_enabled_provider = new MockLoggedInUserEnabledProvider();
}
MockScreenStateEnabledProvider* screen_state_enabled_provider = nullptr;
if (is_screen_enabled_provider_nonnull) {
screen_state_enabled_provider = new MockScreenStateEnabledProvider();
}
MockGoogleApiKeyAvailabilityProvider* google_api_key_availability_provider =
nullptr;
if (is_api_provider_nonnull) {
google_api_key_availability_provider =
new MockGoogleApiKeyAvailabilityProvider();
}
MockScanningEnabledProvider* scanning_enabled_provider = nullptr;
if (is_scanning_provider_nonnull) {
scanning_enabled_provider = new MockScanningEnabledProvider();
}
std::unique_ptr<FastPairEnabledProvider> fast_pair_enabled_provider =
std::make_unique<FastPairEnabledProvider>(
std::move(bluetooth_enabled_provider),
base::WrapUnique(fast_pair_pref_enabled_provider),
base::WrapUnique(logged_in_user_enabled_provider),
base::WrapUnique(screen_state_enabled_provider),
base::WrapUnique(google_api_key_availability_provider),
base::WrapUnique(scanning_enabled_provider));
fast_pair_enabled_provider->is_enabled();
}
INSTANTIATE_TEST_SUITE_P(
FastPairEnabledProviderTestNoCrashOnNullInputs,
FastPairEnabledProviderTestNoCrashOnNullInputs,
testing::Range<size_t>(0, 1 << kNumFastPairEnabledProviderArgs));
} // namespace ash::quick_pair
|