Upload upstream chromium 94.0.4606.31
[platform/framework/web/chromium-efl.git] / components / prefs / scoped_user_pref_update_unittest.cc
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
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:
18   ScopedUserPrefUpdateTest() : observer_(&prefs_) {}
19   ~ScopedUserPrefUpdateTest() override {}
20
21  protected:
22   void SetUp() override {
23     prefs_.registry()->RegisterDictionaryPref(kPref);
24     registrar_.Init(&prefs_);
25     registrar_.Add(kPref, observer_.GetCallback());
26   }
27
28   static const char kPref[];
29   static const char kKey[];
30   static const char kValue[];
31
32   TestingPrefServiceSimple prefs_;
33   MockPrefChangeCallback observer_;
34   PrefChangeRegistrar registrar_;
35 };
36
37 const char ScopedUserPrefUpdateTest::kPref[] = "name";
38 const char ScopedUserPrefUpdateTest::kKey[] = "key";
39 const char ScopedUserPrefUpdateTest::kValue[] = "value";
40
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);
45
46   {
47     EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
48     DictionaryPrefUpdate update(&prefs_, kPref);
49     base::DictionaryValue* value = update.Get();
50     ASSERT_TRUE(value);
51     value->SetString(kKey, kValue);
52
53     // The dictionary was created for us but the creation should have happened
54     // silently without notifications.
55     Mock::VerifyAndClearExpectations(&observer_);
56
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));
61
62     // Now we are leaving the scope of the update so we should be notified.
63     observer_.Expect(kPref, &expected_dictionary);
64   }
65   Mock::VerifyAndClearExpectations(&observer_);
66
67   const base::DictionaryValue* current_value = prefs_.GetDictionary(kPref);
68   ASSERT_TRUE(current_value);
69   EXPECT_TRUE(expected_dictionary.Equals(current_value));
70 }
71
72 TEST_F(ScopedUserPrefUpdateTest, NeverTouchAnything) {
73   const base::DictionaryValue* old_value = prefs_.GetDictionary(kPref);
74   EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
75   {
76     DictionaryPrefUpdate update(&prefs_, kPref);
77   }
78   const base::DictionaryValue* new_value = prefs_.GetDictionary(kPref);
79   EXPECT_EQ(old_value, new_value);
80   Mock::VerifyAndClearExpectations(&observer_);
81 }
82
83 TEST_F(ScopedUserPrefUpdateTest, UpdatingListPrefWithDefaults) {
84   base::Value::ListStorage defaults;
85   defaults.emplace_back("firstvalue");
86   defaults.emplace_back("secondvalue");
87
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());
92
93   ListPrefUpdate update(&prefs_, pref_name);
94   update->AppendString("thirdvalue");
95   EXPECT_EQ(3u, prefs_.GetList(pref_name)->GetList().size());
96 }
97
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"));
102
103   std::string pref_name = "mypref";
104   prefs_.registry()->RegisterDictionaryPref(pref_name, std::move(defaults));
105   EXPECT_EQ(2u, prefs_.GetDictionary(pref_name)->DictSize());
106
107   DictionaryPrefUpdate update(&prefs_, pref_name);
108   update->SetKey("thirdkey", base::Value("value"));
109   EXPECT_EQ(3u, prefs_.GetDictionary(pref_name)->DictSize());
110 }