proximity_auth_profile_pref_manager_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2017 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/components/proximity_auth/proximity_auth_profile_pref_manager.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "ash/components/proximity_auth/proximity_auth_local_state_pref_manager.h"
  10. #include "ash/components/proximity_auth/proximity_auth_pref_names.h"
  11. #include "ash/services/multidevice_setup/public/cpp/fake_multidevice_setup_client.h"
  12. #include "ash/services/multidevice_setup/public/cpp/prefs.h"
  13. #include "components/prefs/testing_pref_service.h"
  14. #include "components/sync_preferences/testing_pref_service_syncable.h"
  15. #include "testing/gmock/include/gmock/gmock.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. namespace proximity_auth {
  18. namespace {
  19. const char kUserEmail[] = "testuser@example.com";
  20. const int64_t kPromotionCheckTimestampMs1 = 1111111111L;
  21. const int64_t kPromotionCheckTimestampMs2 = 2222222222L;
  22. } // namespace
  23. class ProximityAuthProfilePrefManagerTest : public testing::Test {
  24. public:
  25. ProximityAuthProfilePrefManagerTest(
  26. const ProximityAuthProfilePrefManagerTest&) = delete;
  27. ProximityAuthProfilePrefManagerTest& operator=(
  28. const ProximityAuthProfilePrefManagerTest&) = delete;
  29. protected:
  30. ProximityAuthProfilePrefManagerTest() = default;
  31. void SetUp() override {
  32. fake_multidevice_setup_client_ =
  33. std::make_unique<ash::multidevice_setup::FakeMultiDeviceSetupClient>();
  34. ProximityAuthProfilePrefManager::RegisterPrefs(pref_service_.registry());
  35. ash::multidevice_setup::RegisterFeaturePrefs(pref_service_.registry());
  36. pref_manager_ = std::make_unique<ProximityAuthProfilePrefManager>(
  37. &pref_service_, fake_multidevice_setup_client_.get());
  38. }
  39. std::unique_ptr<ash::multidevice_setup::FakeMultiDeviceSetupClient>
  40. fake_multidevice_setup_client_;
  41. sync_preferences::TestingPrefServiceSyncable pref_service_;
  42. std::unique_ptr<ProximityAuthProfilePrefManager> pref_manager_;
  43. };
  44. // TODO(crbug/904005): Investigate using Unified Setup API instead of directly
  45. // manipulating the pref.
  46. TEST_F(ProximityAuthProfilePrefManagerTest, IsEasyUnlockAllowed) {
  47. EXPECT_TRUE(pref_manager_->IsEasyUnlockAllowed());
  48. // Simulating setting kEasyUnlockAllowed pref through enterprise policy.
  49. pref_service_.SetBoolean(ash::multidevice_setup::kSmartLockAllowedPrefName,
  50. false);
  51. EXPECT_FALSE(pref_manager_->IsEasyUnlockAllowed());
  52. }
  53. TEST_F(ProximityAuthProfilePrefManagerTest, LastPromotionCheckTimestamp) {
  54. EXPECT_EQ(0L, pref_manager_->GetLastPromotionCheckTimestampMs());
  55. pref_manager_->SetLastPromotionCheckTimestampMs(kPromotionCheckTimestampMs1);
  56. EXPECT_EQ(kPromotionCheckTimestampMs1,
  57. pref_manager_->GetLastPromotionCheckTimestampMs());
  58. pref_manager_->SetLastPromotionCheckTimestampMs(kPromotionCheckTimestampMs2);
  59. EXPECT_EQ(kPromotionCheckTimestampMs2,
  60. pref_manager_->GetLastPromotionCheckTimestampMs());
  61. }
  62. TEST_F(ProximityAuthProfilePrefManagerTest, PromotionShownCount) {
  63. EXPECT_EQ(0, pref_manager_->GetPromotionShownCount());
  64. pref_manager_->SetPromotionShownCount(1);
  65. EXPECT_EQ(1, pref_manager_->GetPromotionShownCount());
  66. pref_manager_->SetPromotionShownCount(2);
  67. EXPECT_EQ(2, pref_manager_->GetPromotionShownCount());
  68. }
  69. TEST_F(ProximityAuthProfilePrefManagerTest, IsChromeOSLoginEnabled) {
  70. EXPECT_FALSE(pref_manager_->IsChromeOSLoginEnabled());
  71. pref_manager_->SetIsChromeOSLoginEnabled(true);
  72. EXPECT_TRUE(pref_manager_->IsChromeOSLoginEnabled());
  73. pref_manager_->SetIsChromeOSLoginEnabled(false);
  74. EXPECT_FALSE(pref_manager_->IsChromeOSLoginEnabled());
  75. }
  76. TEST_F(ProximityAuthProfilePrefManagerTest, SyncsToLocalPrefOnChange) {
  77. // Use a local variable to ensure that the PrefRegistrar adds and removes
  78. // observers on the same thread.
  79. ProximityAuthProfilePrefManager profile_pref_manager(
  80. &pref_service_, fake_multidevice_setup_client_.get());
  81. TestingPrefServiceSimple local_state;
  82. AccountId account_id = AccountId::FromUserEmail(kUserEmail);
  83. ProximityAuthLocalStatePrefManager::RegisterPrefs(local_state.registry());
  84. profile_pref_manager.StartSyncingToLocalState(&local_state, account_id);
  85. // Use the local state pref manager to verify that prefs are synced correctly.
  86. ProximityAuthLocalStatePrefManager local_pref_manager(&local_state);
  87. local_pref_manager.SetActiveUser(account_id);
  88. profile_pref_manager.SetIsChromeOSLoginEnabled(true);
  89. profile_pref_manager.SetIsEasyUnlockEnabled(true);
  90. EXPECT_TRUE(local_pref_manager.IsChromeOSLoginEnabled());
  91. profile_pref_manager.SetIsChromeOSLoginEnabled(false);
  92. profile_pref_manager.SetIsEasyUnlockEnabled(false);
  93. EXPECT_FALSE(local_pref_manager.IsChromeOSLoginEnabled());
  94. EXPECT_FALSE(local_pref_manager.IsEasyUnlockEnabled());
  95. // Test changing the kEasyUnlockAllowed pref value directly (e.g. through
  96. // enterprise policy).
  97. EXPECT_TRUE(local_pref_manager.IsEasyUnlockAllowed());
  98. pref_service_.SetBoolean(ash::multidevice_setup::kSmartLockAllowedPrefName,
  99. false);
  100. EXPECT_FALSE(profile_pref_manager.IsEasyUnlockAllowed());
  101. EXPECT_FALSE(local_pref_manager.IsEasyUnlockAllowed());
  102. }
  103. } // namespace proximity_auth