xref: /aosp_15_r20/external/cronet/components/prefs/scoped_user_pref_update_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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.
4 
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"
12 
13 using testing::_;
14 using testing::Mock;
15 
16 class ScopedUserPrefUpdateTest : public testing::Test {
17  public:
ScopedUserPrefUpdateTest()18   ScopedUserPrefUpdateTest()
19       : dict_observer_(&prefs_), list_observer_(&prefs_) {}
20   ~ScopedUserPrefUpdateTest() override = default;
21 
22  protected:
SetUp()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());
29   }
30 
31   static const char kDictPref[];
32   static const char kListPref[];
33   static const char kKey[];
34   static const char kValue[];
35 
36   TestingPrefServiceSimple prefs_;
37   MockPrefChangeCallback dict_observer_;
38   MockPrefChangeCallback list_observer_;
39   PrefChangeRegistrar registrar_;
40 };
41 
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";
46 
TEST_F(ScopedUserPrefUpdateTest,ScopedDictPrefUpdateRegularUse)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);
51 
52   {
53     EXPECT_CALL(dict_observer_, OnPreferenceChanged(_)).Times(0);
54     ScopedDictPrefUpdate update(&prefs_, kDictPref);
55     update->Set(kKey, kValue);
56 
57     // The dictionary was created for us but the creation should have happened
58     // silently without notifications.
59     Mock::VerifyAndClearExpectations(&dict_observer_);
60 
61     // Modifications happen online and are instantly visible, though.
62     EXPECT_EQ(expected_dictionary, prefs_.GetDict(kDictPref));
63 
64     // Now we are leaving the scope of the update so we should be notified.
65     dict_observer_.Expect(kDictPref, &expected_dictionary);
66   }
67   Mock::VerifyAndClearExpectations(&dict_observer_);
68 
69   EXPECT_EQ(expected_dictionary, prefs_.GetDict(kDictPref));
70 }
71 
TEST_F(ScopedUserPrefUpdateTest,ScopedDictPrefUpdateNeverTouchAnything)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_);
79 }
80 
TEST_F(ScopedUserPrefUpdateTest,ScopedDictPrefUpdateWithDefaults)81 TEST_F(ScopedUserPrefUpdateTest, ScopedDictPrefUpdateWithDefaults) {
82   auto defaults =
83       base::Value::Dict().Set("firstkey", "value").Set("secondkey", "value");
84 
85   std::string pref_name = "mypref";
86   prefs_.registry()->RegisterDictionaryPref(pref_name, std::move(defaults));
87   EXPECT_EQ(2u, prefs_.GetDict(pref_name).size());
88 
89   ScopedDictPrefUpdate update(&prefs_, pref_name);
90   update->Set("thirdkey", "value");
91   EXPECT_EQ(3u, prefs_.GetDict(pref_name).size());
92 }
93 
TEST_F(ScopedUserPrefUpdateTest,ScopedListPrefUpdateRegularUse)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);
98 
99   {
100     EXPECT_CALL(list_observer_, OnPreferenceChanged(_)).Times(0);
101     ScopedListPrefUpdate update(&prefs_, kListPref);
102     update->Append(kValue);
103 
104     // The list was created for us but the creation should have happened
105     // silently without notifications.
106     Mock::VerifyAndClearExpectations(&list_observer_);
107 
108     // Modifications happen online and are instantly visible, though.
109     EXPECT_EQ(expected_list, prefs_.GetList(kListPref));
110 
111     // Now we are leaving the scope of the update so we should be notified.
112     list_observer_.Expect(kListPref, &expected_list);
113   }
114   Mock::VerifyAndClearExpectations(&list_observer_);
115 
116   EXPECT_EQ(expected_list.GetList(), prefs_.GetList(kListPref));
117 }
118 
TEST_F(ScopedUserPrefUpdateTest,ScopedListPrefUpdateNeverTouchAnything)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_);
126 }
127 
TEST_F(ScopedUserPrefUpdateTest,ScopedListPrefUpdateWithDefaults)128 TEST_F(ScopedUserPrefUpdateTest, ScopedListPrefUpdateWithDefaults) {
129   base::Value::List defaults;
130   defaults.Append("firstvalue");
131   defaults.Append("secondvalue");
132 
133   std::string pref_name = "mypref";
134   prefs_.registry()->RegisterListPref(pref_name, std::move(defaults));
135   EXPECT_EQ(2u, prefs_.GetList(pref_name).size());
136 
137   ScopedListPrefUpdate update(&prefs_, pref_name);
138   update->Append("thirdvalue");
139   EXPECT_EQ(3u, prefs_.GetList(pref_name).size());
140 }
141