1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/prefs/mock_pref_change_callback.h"
6 #include "components/prefs/pref_change_registrar.h"
7 #include "components/prefs/pref_registry_simple.h"
8 #include "components/prefs/scoped_user_pref_update.h"
9 #include "components/prefs/testing_pref_service.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
16 class ScopedUserPrefUpdateTest : public testing::Test {
18 ScopedUserPrefUpdateTest()
19 : dict_observer_(&prefs_), list_observer_(&prefs_) {}
20 ~ScopedUserPrefUpdateTest() override = default;
23 void SetUp() override {
24 prefs_.registry()->RegisterDictionaryPref(kDictPref);
25 prefs_.registry()->RegisterListPref(kListPref);
26 registrar_.Init(&prefs_);
27 registrar_.Add(kDictPref, dict_observer_.GetCallback());
28 registrar_.Add(kListPref, list_observer_.GetCallback());
31 static const char kDictPref[];
32 static const char kListPref[];
33 static const char kKey[];
34 static const char kValue[];
36 TestingPrefServiceSimple prefs_;
37 MockPrefChangeCallback dict_observer_;
38 MockPrefChangeCallback list_observer_;
39 PrefChangeRegistrar registrar_;
42 const char ScopedUserPrefUpdateTest::kDictPref[] = "dict_pref_name";
43 const char ScopedUserPrefUpdateTest::kListPref[] = "list_pref_name";
44 const char ScopedUserPrefUpdateTest::kKey[] = "key";
45 const char ScopedUserPrefUpdateTest::kValue[] = "value";
47 TEST_F(ScopedUserPrefUpdateTest, ScopedDictPrefUpdateRegularUse) {
48 // Dictionary that will be expected to be set at the end.
49 base::Value expected_dictionary(base::Value::Type::DICT);
50 expected_dictionary.GetDict().Set(kKey, kValue);
53 EXPECT_CALL(dict_observer_, OnPreferenceChanged(_)).Times(0);
54 ScopedDictPrefUpdate update(&prefs_, kDictPref);
55 update->Set(kKey, kValue);
57 // The dictionary was created for us but the creation should have happened
58 // silently without notifications.
59 Mock::VerifyAndClearExpectations(&dict_observer_);
61 // Modifications happen online and are instantly visible, though.
62 EXPECT_EQ(expected_dictionary, prefs_.GetDict(kDictPref));
64 // Now we are leaving the scope of the update so we should be notified.
65 dict_observer_.Expect(kDictPref, &expected_dictionary);
67 Mock::VerifyAndClearExpectations(&dict_observer_);
69 EXPECT_EQ(expected_dictionary, prefs_.GetDict(kDictPref));
72 TEST_F(ScopedUserPrefUpdateTest, ScopedDictPrefUpdateNeverTouchAnything) {
73 const base::Value::Dict& old_value = prefs_.GetDict(kDictPref);
74 EXPECT_CALL(dict_observer_, OnPreferenceChanged(_)).Times(0);
75 { ScopedDictPrefUpdate update(&prefs_, kDictPref); }
76 const base::Value::Dict& new_value = prefs_.GetDict(kDictPref);
77 EXPECT_EQ(old_value, new_value);
78 Mock::VerifyAndClearExpectations(&dict_observer_);
81 TEST_F(ScopedUserPrefUpdateTest, ScopedDictPrefUpdateWithDefaults) {
83 base::Value::Dict().Set("firstkey", "value").Set("secondkey", "value");
85 std::string pref_name = "mypref";
86 prefs_.registry()->RegisterDictionaryPref(pref_name, std::move(defaults));
87 EXPECT_EQ(2u, prefs_.GetDict(pref_name).size());
89 ScopedDictPrefUpdate update(&prefs_, pref_name);
90 update->Set("thirdkey", "value");
91 EXPECT_EQ(3u, prefs_.GetDict(pref_name).size());
94 TEST_F(ScopedUserPrefUpdateTest, ScopedListPrefUpdateRegularUse) {
95 // List that will be expected to be set at the end.
96 base::Value expected_list(base::Value::Type::LIST);
97 expected_list.GetList().Append(kValue);
100 EXPECT_CALL(list_observer_, OnPreferenceChanged(_)).Times(0);
101 ScopedListPrefUpdate update(&prefs_, kListPref);
102 update->Append(kValue);
104 // The list was created for us but the creation should have happened
105 // silently without notifications.
106 Mock::VerifyAndClearExpectations(&list_observer_);
108 // Modifications happen online and are instantly visible, though.
109 EXPECT_EQ(expected_list, prefs_.GetList(kListPref));
111 // Now we are leaving the scope of the update so we should be notified.
112 list_observer_.Expect(kListPref, &expected_list);
114 Mock::VerifyAndClearExpectations(&list_observer_);
116 EXPECT_EQ(expected_list.GetList(), prefs_.GetList(kListPref));
119 TEST_F(ScopedUserPrefUpdateTest, ScopedListPrefUpdateNeverTouchAnything) {
120 const base::Value::List& old_value = prefs_.GetList(kListPref);
121 EXPECT_CALL(dict_observer_, OnPreferenceChanged(_)).Times(0);
122 { ScopedListPrefUpdate update(&prefs_, kListPref); }
123 const base::Value::List& new_value = prefs_.GetList(kListPref);
124 EXPECT_EQ(old_value, new_value);
125 Mock::VerifyAndClearExpectations(&dict_observer_);
128 TEST_F(ScopedUserPrefUpdateTest, ScopedListPrefUpdateWithDefaults) {
129 base::Value::List defaults;
130 defaults.Append("firstvalue");
131 defaults.Append("secondvalue");
133 std::string pref_name = "mypref";
134 prefs_.registry()->RegisterListPref(pref_name, std::move(defaults));
135 EXPECT_EQ(2u, prefs_.GetList(pref_name).size());
137 ScopedListPrefUpdate update(&prefs_, pref_name);
138 update->Append("thirdvalue");
139 EXPECT_EQ(3u, prefs_.GetList(pref_name).size());