fast_pair_enabled_provider_unittest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/quick_pair/feature_status_tracker/fast_pair_enabled_provider.h"
  5. #include <memory>
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/quick_pair/feature_status_tracker/bluetooth_enabled_provider.h"
  8. #include "ash/quick_pair/feature_status_tracker/fake_bluetooth_adapter.h"
  9. #include "ash/quick_pair/feature_status_tracker/fast_pair_pref_enabled_provider.h"
  10. #include "ash/quick_pair/feature_status_tracker/logged_in_user_enabled_provider.h"
  11. #include "ash/quick_pair/feature_status_tracker/mock_bluetooth_enabled_provider.h"
  12. #include "ash/quick_pair/feature_status_tracker/mock_fast_pair_pref_enabled_provider.h"
  13. #include "ash/quick_pair/feature_status_tracker/mock_google_api_key_availability_provider.h"
  14. #include "ash/quick_pair/feature_status_tracker/mock_logged_in_user_enabled_provider.h"
  15. #include "ash/quick_pair/feature_status_tracker/mock_screen_state_enabled_provider.h"
  16. #include "ash/quick_pair/feature_status_tracker/screen_state_enabled_provider.h"
  17. #include "ash/test/ash_test_base.h"
  18. #include "base/callback.h"
  19. #include "base/memory/ptr_util.h"
  20. #include "base/memory/scoped_refptr.h"
  21. #include "base/test/mock_callback.h"
  22. #include "base/test/scoped_feature_list.h"
  23. #include "device/bluetooth/bluetooth_adapter_factory.h"
  24. #include "testing/gtest/include/gtest/gtest-param-test.h"
  25. #include "testing/gtest/include/gtest/gtest.h"
  26. namespace ash {
  27. namespace quick_pair {
  28. class FastPairEnabledProviderTest : public AshTestBase {
  29. public:
  30. void SetUp() override {
  31. AshTestBase::SetUp();
  32. adapter_ = base::MakeRefCounted<FakeBluetoothAdapter>();
  33. device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
  34. }
  35. protected:
  36. scoped_refptr<FakeBluetoothAdapter> adapter_;
  37. };
  38. TEST_F(FastPairEnabledProviderTest, ProviderCallbackIsInvokedOnBTChanges) {
  39. base::test::ScopedFeatureList feature_list{features::kFastPair};
  40. base::MockCallback<base::RepeatingCallback<void(bool)>> callback;
  41. EXPECT_CALL(callback, Run(true));
  42. auto* fast_pair_pref_enabled_provider = new MockFastPairPrefEnabledProvider();
  43. ON_CALL(*fast_pair_pref_enabled_provider, is_enabled)
  44. .WillByDefault(testing::Return(true));
  45. auto* logged_in_user_enabled_provider = new MockLoggedInUserEnabledProvider();
  46. ON_CALL(*logged_in_user_enabled_provider, is_enabled)
  47. .WillByDefault(testing::Return(true));
  48. auto* screen_state_enabled_provider = new MockScreenStateEnabledProvider();
  49. ON_CALL(*screen_state_enabled_provider, is_enabled)
  50. .WillByDefault(testing::Return(true));
  51. auto* google_api_key_availability_provider =
  52. new MockGoogleApiKeyAvailabilityProvider();
  53. ON_CALL(*google_api_key_availability_provider, is_enabled)
  54. .WillByDefault(testing::Return(true));
  55. auto provider = std::make_unique<FastPairEnabledProvider>(
  56. std::make_unique<BluetoothEnabledProvider>(),
  57. base::WrapUnique(fast_pair_pref_enabled_provider),
  58. base::WrapUnique(logged_in_user_enabled_provider),
  59. base::WrapUnique(screen_state_enabled_provider),
  60. base::WrapUnique(google_api_key_availability_provider));
  61. provider->SetCallback(callback.Get());
  62. adapter_->SetBluetoothIsPowered(true);
  63. }
  64. // Represents: <is_flag_enabled, is_bt_enabled, is_pref_enabled,
  65. // is_user_logged_in, is_screen_state_on,
  66. // is_google_api_keys_available>
  67. using TestParam = std::tuple<bool, bool, bool, bool, bool, bool>;
  68. class FastPairEnabledProviderTestWithParams
  69. : public FastPairEnabledProviderTest,
  70. public testing::WithParamInterface<TestParam> {};
  71. TEST_P(FastPairEnabledProviderTestWithParams, IsEnabledWhenExpected) {
  72. bool is_flag_enabled = std::get<0>(GetParam());
  73. bool is_bt_enabled = std::get<1>(GetParam());
  74. bool is_pref_enabled = std::get<2>(GetParam());
  75. bool is_user_logged_in = std::get<3>(GetParam());
  76. bool is_screen_state_on = std::get<4>(GetParam());
  77. bool is_google_api_keys_available = std::get<5>(GetParam());
  78. base::test::ScopedFeatureList feature_list;
  79. feature_list.InitWithFeatureState(features::kFastPair, is_flag_enabled);
  80. auto* bluetooth_enabled_provider = new MockBluetoothEnabledProvider();
  81. ON_CALL(*bluetooth_enabled_provider, is_enabled)
  82. .WillByDefault(testing::Return(is_bt_enabled));
  83. auto* fast_pair_pref_enabled_provider = new MockFastPairPrefEnabledProvider();
  84. ON_CALL(*fast_pair_pref_enabled_provider, is_enabled)
  85. .WillByDefault(testing::Return(is_pref_enabled));
  86. auto* logged_in_user_enabled_provider = new MockLoggedInUserEnabledProvider();
  87. ON_CALL(*logged_in_user_enabled_provider, is_enabled)
  88. .WillByDefault(testing::Return(is_user_logged_in));
  89. auto* screen_state_enabled_provider = new MockScreenStateEnabledProvider();
  90. ON_CALL(*screen_state_enabled_provider, is_enabled)
  91. .WillByDefault(testing::Return(is_screen_state_on));
  92. auto* google_api_key_availability_provider =
  93. new MockGoogleApiKeyAvailabilityProvider();
  94. ON_CALL(*google_api_key_availability_provider, is_enabled)
  95. .WillByDefault(testing::Return(is_google_api_keys_available));
  96. auto provider = std::make_unique<FastPairEnabledProvider>(
  97. std::unique_ptr<BluetoothEnabledProvider>(bluetooth_enabled_provider),
  98. std::unique_ptr<FastPairPrefEnabledProvider>(
  99. fast_pair_pref_enabled_provider),
  100. std::unique_ptr<LoggedInUserEnabledProvider>(
  101. logged_in_user_enabled_provider),
  102. std::unique_ptr<ScreenStateEnabledProvider>(
  103. screen_state_enabled_provider),
  104. base::WrapUnique(google_api_key_availability_provider));
  105. bool all_are_enabled = is_flag_enabled && is_bt_enabled && is_pref_enabled &&
  106. is_user_logged_in && is_screen_state_on &&
  107. is_google_api_keys_available;
  108. EXPECT_EQ(provider->is_enabled(), all_are_enabled);
  109. }
  110. INSTANTIATE_TEST_SUITE_P(FastPairEnabledProviderTestWithParams,
  111. FastPairEnabledProviderTestWithParams,
  112. testing::Combine(testing::Bool(),
  113. testing::Bool(),
  114. testing::Bool(),
  115. testing::Bool(),
  116. testing::Bool(),
  117. testing::Bool()));
  118. } // namespace quick_pair
  119. } // namespace ash