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.
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() : observer_(&prefs_) {}
19 ~ScopedUserPrefUpdateTest() override {}
22 void SetUp() override {
23 prefs_.registry()->RegisterDictionaryPref(kPref);
24 registrar_.Init(&prefs_);
25 registrar_.Add(kPref, observer_.GetCallback());
28 static const char kPref[];
29 static const char kKey[];
30 static const char kValue[];
32 TestingPrefServiceSimple prefs_;
33 MockPrefChangeCallback observer_;
34 PrefChangeRegistrar registrar_;
37 const char ScopedUserPrefUpdateTest::kPref[] = "name";
38 const char ScopedUserPrefUpdateTest::kKey[] = "key";
39 const char ScopedUserPrefUpdateTest::kValue[] = "value";
41 TEST_F(ScopedUserPrefUpdateTest, RegularUse) {
42 // Dictionary that will be expected to be set at the end.
43 base::DictionaryValue expected_dictionary;
44 expected_dictionary.SetString(kKey, kValue);
47 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
48 DictionaryPrefUpdate update(&prefs_, kPref);
49 base::DictionaryValue* value = update.Get();
51 value->SetString(kKey, kValue);
53 // The dictionary was created for us but the creation should have happened
54 // silently without notifications.
55 Mock::VerifyAndClearExpectations(&observer_);
57 // Modifications happen online and are instantly visible, though.
58 const base::DictionaryValue* current_value = prefs_.GetDictionary(kPref);
59 ASSERT_TRUE(current_value);
60 EXPECT_TRUE(expected_dictionary.Equals(current_value));
62 // Now we are leaving the scope of the update so we should be notified.
63 observer_.Expect(kPref, &expected_dictionary);
65 Mock::VerifyAndClearExpectations(&observer_);
67 const base::DictionaryValue* current_value = prefs_.GetDictionary(kPref);
68 ASSERT_TRUE(current_value);
69 EXPECT_TRUE(expected_dictionary.Equals(current_value));
72 TEST_F(ScopedUserPrefUpdateTest, NeverTouchAnything) {
73 const base::DictionaryValue* old_value = prefs_.GetDictionary(kPref);
74 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
76 DictionaryPrefUpdate update(&prefs_, kPref);
78 const base::DictionaryValue* new_value = prefs_.GetDictionary(kPref);
79 EXPECT_EQ(old_value, new_value);
80 Mock::VerifyAndClearExpectations(&observer_);
83 TEST_F(ScopedUserPrefUpdateTest, UpdatingListPrefWithDefaults) {
84 base::Value::ListStorage defaults;
85 defaults.emplace_back("firstvalue");
86 defaults.emplace_back("secondvalue");
88 std::string pref_name = "mypref";
89 prefs_.registry()->RegisterListPref(pref_name,
90 base::Value(std::move(defaults)));
91 EXPECT_EQ(2u, prefs_.GetList(pref_name)->GetList().size());
93 ListPrefUpdate update(&prefs_, pref_name);
94 update->AppendString("thirdvalue");
95 EXPECT_EQ(3u, prefs_.GetList(pref_name)->GetList().size());
98 TEST_F(ScopedUserPrefUpdateTest, UpdatingDictionaryPrefWithDefaults) {
99 base::Value defaults(base::Value::Type::DICTIONARY);
100 defaults.SetKey("firstkey", base::Value("value"));
101 defaults.SetKey("secondkey", base::Value("value"));
103 std::string pref_name = "mypref";
104 prefs_.registry()->RegisterDictionaryPref(pref_name, std::move(defaults));
105 EXPECT_EQ(2u, prefs_.GetDictionary(pref_name)->DictSize());
107 DictionaryPrefUpdate update(&prefs_, pref_name);
108 update->SetKey("thirdkey", base::Value("value"));
109 EXPECT_EQ(3u, prefs_.GetDictionary(pref_name)->DictSize());