scoped_user_pref_update_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2013 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 "components/prefs/mock_pref_change_callback.h"
  5. #include "components/prefs/pref_change_registrar.h"
  6. #include "components/prefs/pref_registry_simple.h"
  7. #include "components/prefs/scoped_user_pref_update.h"
  8. #include "components/prefs/testing_pref_service.h"
  9. #include "testing/gmock/include/gmock/gmock.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. using testing::_;
  12. using testing::Mock;
  13. class ScopedUserPrefUpdateTest : public testing::Test {
  14. public:
  15. ScopedUserPrefUpdateTest() : observer_(&prefs_) {}
  16. ~ScopedUserPrefUpdateTest() override {}
  17. protected:
  18. void SetUp() override {
  19. prefs_.registry()->RegisterDictionaryPref(kPref);
  20. registrar_.Init(&prefs_);
  21. registrar_.Add(kPref, observer_.GetCallback());
  22. }
  23. static const char kPref[];
  24. static const char kKey[];
  25. static const char kValue[];
  26. TestingPrefServiceSimple prefs_;
  27. MockPrefChangeCallback observer_;
  28. PrefChangeRegistrar registrar_;
  29. };
  30. const char ScopedUserPrefUpdateTest::kPref[] = "name";
  31. const char ScopedUserPrefUpdateTest::kKey[] = "key";
  32. const char ScopedUserPrefUpdateTest::kValue[] = "value";
  33. TEST_F(ScopedUserPrefUpdateTest, RegularUse) {
  34. // Dictionary that will be expected to be set at the end.
  35. base::Value expected_dictionary(base::Value::Type::DICTIONARY);
  36. expected_dictionary.SetStringKey(kKey, kValue);
  37. {
  38. EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
  39. DictionaryPrefUpdate update(&prefs_, kPref);
  40. base::Value* value = update.Get();
  41. ASSERT_TRUE(value);
  42. value->SetStringKey(kKey, kValue);
  43. // The dictionary was created for us but the creation should have happened
  44. // silently without notifications.
  45. Mock::VerifyAndClearExpectations(&observer_);
  46. // Modifications happen online and are instantly visible, though.
  47. EXPECT_EQ(expected_dictionary, prefs_.GetValueDict(kPref));
  48. // Now we are leaving the scope of the update so we should be notified.
  49. observer_.Expect(kPref, &expected_dictionary);
  50. }
  51. Mock::VerifyAndClearExpectations(&observer_);
  52. EXPECT_EQ(expected_dictionary, prefs_.GetValueDict(kPref));
  53. }
  54. TEST_F(ScopedUserPrefUpdateTest, NeverTouchAnything) {
  55. const base::Value::Dict& old_value = prefs_.GetValueDict(kPref);
  56. EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
  57. { DictionaryPrefUpdate update(&prefs_, kPref); }
  58. const base::Value::Dict& new_value = prefs_.GetValueDict(kPref);
  59. EXPECT_EQ(old_value, new_value);
  60. Mock::VerifyAndClearExpectations(&observer_);
  61. }
  62. TEST_F(ScopedUserPrefUpdateTest, UpdatingListPrefWithDefaults) {
  63. base::Value::List defaults;
  64. defaults.Append("firstvalue");
  65. defaults.Append("secondvalue");
  66. std::string pref_name = "mypref";
  67. prefs_.registry()->RegisterListPref(pref_name, std::move(defaults));
  68. EXPECT_EQ(2u, prefs_.GetValueList(pref_name).size());
  69. ListPrefUpdate update(&prefs_, pref_name);
  70. update->Append("thirdvalue");
  71. EXPECT_EQ(3u, prefs_.GetValueList(pref_name).size());
  72. }
  73. TEST_F(ScopedUserPrefUpdateTest, UpdatingDictionaryPrefWithDefaults) {
  74. base::Value::Dict defaults;
  75. defaults.Set("firstkey", "value");
  76. defaults.Set("secondkey", "value");
  77. std::string pref_name = "mypref";
  78. prefs_.registry()->RegisterDictionaryPref(pref_name, std::move(defaults));
  79. EXPECT_EQ(2u, prefs_.GetValueDict(pref_name).size());
  80. DictionaryPrefUpdate update(&prefs_, pref_name);
  81. update->SetStringKey("thirdkey", "value");
  82. EXPECT_EQ(3u, prefs_.GetValueDict(pref_name).size());
  83. }