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) {
82 base::Value::Dict defaults;
83 defaults.Set("firstkey", "value");
84 defaults.Set("secondkey", "value");
86 std::string pref_name = "mypref";
87 prefs_.registry()->RegisterDictionaryPref(pref_name, std::move(defaults));
88 EXPECT_EQ(2u, prefs_.GetDict(pref_name).size());
90 ScopedDictPrefUpdate update(&prefs_, pref_name);
91 update->Set("thirdkey", "value");
92 EXPECT_EQ(3u, prefs_.GetDict(pref_name).size());
95 TEST_F(ScopedUserPrefUpdateTest, ScopedListPrefUpdateRegularUse) {
96 // List that will be expected to be set at the end.
97 base::Value expected_list(base::Value::Type::LIST);
98 expected_list.GetList().Append(kValue);
101 EXPECT_CALL(list_observer_, OnPreferenceChanged(_)).Times(0);
102 ScopedListPrefUpdate update(&prefs_, kListPref);
103 update->Append(kValue);
105 // The list was created for us but the creation should have happened
106 // silently without notifications.
107 Mock::VerifyAndClearExpectations(&list_observer_);
109 // Modifications happen online and are instantly visible, though.
110 EXPECT_EQ(expected_list, prefs_.GetList(kListPref));
112 // Now we are leaving the scope of the update so we should be notified.
113 list_observer_.Expect(kListPref, &expected_list);
115 Mock::VerifyAndClearExpectations(&list_observer_);
117 EXPECT_EQ(expected_list.GetList(), prefs_.GetList(kListPref));
120 TEST_F(ScopedUserPrefUpdateTest, ScopedListPrefUpdateNeverTouchAnything) {
121 const base::Value::List& old_value = prefs_.GetList(kListPref);
122 EXPECT_CALL(dict_observer_, OnPreferenceChanged(_)).Times(0);
123 { ScopedListPrefUpdate update(&prefs_, kListPref); }
124 const base::Value::List& new_value = prefs_.GetList(kListPref);
125 EXPECT_EQ(old_value, new_value);
126 Mock::VerifyAndClearExpectations(&dict_observer_);
129 TEST_F(ScopedUserPrefUpdateTest, ScopedListPrefUpdateWithDefaults) {
130 base::Value::List defaults;
131 defaults.Append("firstvalue");
132 defaults.Append("secondvalue");
134 std::string pref_name = "mypref";
135 prefs_.registry()->RegisterListPref(pref_name, std::move(defaults));
136 EXPECT_EQ(2u, prefs_.GetList(pref_name).size());
138 ScopedListPrefUpdate update(&prefs_, pref_name);
139 update->Append("thirdvalue");
140 EXPECT_EQ(3u, prefs_.GetList(pref_name).size());