proximity_auth_local_state_pref_manager_unittest.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_local_state_pref_manager.h"
  5. #include <memory>
  6. #include <string>
  7. #include <vector>
  8. #include "ash/components/proximity_auth/proximity_auth_pref_names.h"
  9. #include "ash/services/multidevice_setup/public/cpp/prefs.h"
  10. #include "base/json/json_reader.h"
  11. #include "components/prefs/scoped_user_pref_update.h"
  12. #include "components/prefs/testing_pref_service.h"
  13. #include "testing/gmock/include/gmock/gmock.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. namespace proximity_auth {
  16. namespace {
  17. const char kUser1[] = "songttim@gmail.com";
  18. const bool kIsChromeOSLoginEnabled1 = true;
  19. const bool kIsEasyUnlockAllowed1 = true;
  20. const bool kIsEasyUnlockEnabled1 = true;
  21. const char kUser2[] = "tengs@google.com";
  22. const bool kIsChromeOSLoginEnabled2 = false;
  23. const bool kIsEasyUnlockAllowed2 = false;
  24. const bool kIsEasyUnlockEnabled2 = false;
  25. const char kUnknownUser[] = "tengs@chromium.org";
  26. const bool kIsChromeOSLoginEnabledDefault = false;
  27. const bool kIsEasyUnlockAllowedDefault = true;
  28. const bool kIsEasyUnlockEnabledDefault = false;
  29. } // namespace
  30. class ProximityAuthLocalStatePrefManagerTest : public testing::Test {
  31. public:
  32. ProximityAuthLocalStatePrefManagerTest(
  33. const ProximityAuthLocalStatePrefManagerTest&) = delete;
  34. ProximityAuthLocalStatePrefManagerTest& operator=(
  35. const ProximityAuthLocalStatePrefManagerTest&) = delete;
  36. protected:
  37. ProximityAuthLocalStatePrefManagerTest()
  38. : user1_(AccountId::FromUserEmail(kUser1)),
  39. user2_(AccountId::FromUserEmail(kUser2)),
  40. unknown_user_(AccountId::FromUserEmail(kUnknownUser)) {}
  41. ~ProximityAuthLocalStatePrefManagerTest() override {}
  42. void SetUp() override {
  43. ProximityAuthLocalStatePrefManager::RegisterPrefs(local_state_.registry());
  44. // Note: in normal circumstances, these prefs are synced to local state in
  45. // ProximityAuthProfilePrefService.
  46. base::Value user1_prefs(base::Value::Type::DICTIONARY);
  47. user1_prefs.SetBoolKey(
  48. proximity_auth::prefs::kProximityAuthIsChromeOSLoginEnabled,
  49. kIsChromeOSLoginEnabled1);
  50. user1_prefs.SetBoolKey(ash::multidevice_setup::kSmartLockAllowedPrefName,
  51. kIsEasyUnlockAllowed1);
  52. user1_prefs.SetBoolKey(ash::multidevice_setup::kSmartLockEnabledPrefName,
  53. kIsEasyUnlockEnabled1);
  54. DictionaryPrefUpdate update1(&local_state_,
  55. prefs::kEasyUnlockLocalStateUserPrefs);
  56. update1->SetKey(user1_.GetUserEmail(), std::move(user1_prefs));
  57. base::Value user2_prefs(base::Value::Type::DICTIONARY);
  58. user2_prefs.SetBoolKey(
  59. proximity_auth::prefs::kProximityAuthIsChromeOSLoginEnabled,
  60. kIsChromeOSLoginEnabled2);
  61. user2_prefs.SetBoolKey(ash::multidevice_setup::kSmartLockAllowedPrefName,
  62. kIsEasyUnlockAllowed2);
  63. user2_prefs.SetBoolKey(ash::multidevice_setup::kSmartLockEnabledPrefName,
  64. kIsEasyUnlockEnabled2);
  65. DictionaryPrefUpdate update2(&local_state_,
  66. prefs::kEasyUnlockLocalStateUserPrefs);
  67. update2->SetKey(user2_.GetUserEmail(), std::move(user2_prefs));
  68. }
  69. AccountId user1_;
  70. AccountId user2_;
  71. AccountId unknown_user_;
  72. TestingPrefServiceSimple local_state_;
  73. };
  74. TEST_F(ProximityAuthLocalStatePrefManagerTest, RegisterPrefs) {
  75. EXPECT_TRUE(
  76. local_state_.FindPreference(prefs::kEasyUnlockLocalStateUserPrefs));
  77. }
  78. TEST_F(ProximityAuthLocalStatePrefManagerTest, IsEasyUnlockAllowed) {
  79. ProximityAuthLocalStatePrefManager pref_manager(&local_state_);
  80. // If no active user is set, return the default value.
  81. EXPECT_EQ(kIsEasyUnlockAllowedDefault, pref_manager.IsEasyUnlockAllowed());
  82. // Unknown users should return the default value.
  83. pref_manager.SetActiveUser(unknown_user_);
  84. EXPECT_EQ(kIsEasyUnlockAllowedDefault, pref_manager.IsEasyUnlockAllowed());
  85. // Test users with set values.
  86. pref_manager.SetActiveUser(user1_);
  87. EXPECT_EQ(kIsEasyUnlockAllowed1, pref_manager.IsEasyUnlockAllowed());
  88. pref_manager.SetActiveUser(user2_);
  89. EXPECT_EQ(kIsEasyUnlockAllowed2, pref_manager.IsEasyUnlockAllowed());
  90. }
  91. TEST_F(ProximityAuthLocalStatePrefManagerTest, IsEasyUnlockEnabled) {
  92. ProximityAuthLocalStatePrefManager pref_manager(&local_state_);
  93. // If no active user is set, return the default value.
  94. EXPECT_EQ(kIsEasyUnlockEnabledDefault, pref_manager.IsEasyUnlockEnabled());
  95. // Unknown users should return the default value.
  96. pref_manager.SetActiveUser(unknown_user_);
  97. EXPECT_EQ(kIsEasyUnlockEnabledDefault, pref_manager.IsEasyUnlockEnabled());
  98. // Test users with set values.
  99. pref_manager.SetActiveUser(user1_);
  100. EXPECT_EQ(kIsEasyUnlockEnabled1, pref_manager.IsEasyUnlockEnabled());
  101. pref_manager.SetActiveUser(user2_);
  102. EXPECT_EQ(kIsEasyUnlockEnabled2, pref_manager.IsEasyUnlockEnabled());
  103. }
  104. TEST_F(ProximityAuthLocalStatePrefManagerTest, IsChromeOSLoginEnabled) {
  105. ProximityAuthLocalStatePrefManager pref_manager(&local_state_);
  106. // If no active user is set, return the default value.
  107. EXPECT_EQ(kIsChromeOSLoginEnabledDefault,
  108. pref_manager.IsChromeOSLoginEnabled());
  109. // Unknown users should return the default value.
  110. pref_manager.SetActiveUser(unknown_user_);
  111. EXPECT_EQ(kIsChromeOSLoginEnabledDefault,
  112. pref_manager.IsChromeOSLoginEnabled());
  113. // Test users with set values.
  114. pref_manager.SetActiveUser(user1_);
  115. EXPECT_EQ(kIsChromeOSLoginEnabled1, pref_manager.IsChromeOSLoginEnabled());
  116. pref_manager.SetActiveUser(user2_);
  117. EXPECT_EQ(kIsChromeOSLoginEnabled2, pref_manager.IsChromeOSLoginEnabled());
  118. }
  119. } // namespace proximity_auth