Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: base/prefs/pref_change_registrar_unittest.cc

Issue 1645073005: Revert of Move base/prefs to components/prefs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/prefs/pref_change_registrar.cc ('k') | base/prefs/pref_filter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 "base/bind.h"
6 #include "base/bind_helpers.h"
7 #include "base/prefs/pref_change_registrar.h"
8 #include "base/prefs/pref_observer.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using testing::Mock;
15 using testing::Eq;
16
17 namespace base {
18 namespace {
19
20 const char kHomePage[] = "homepage";
21 const char kHomePageIsNewTabPage[] = "homepage_is_newtabpage";
22 const char kApplicationLocale[] = "intl.app_locale";
23
24 // A mock provider that allows us to capture pref observer changes.
25 class MockPrefService : public TestingPrefServiceSimple {
26 public:
27 MockPrefService() {}
28 virtual ~MockPrefService() {}
29
30 MOCK_METHOD2(AddPrefObserver, void(const std::string&, PrefObserver*));
31 MOCK_METHOD2(RemovePrefObserver, void(const std::string&, PrefObserver*));
32 };
33
34 } // namespace
35
36 class PrefChangeRegistrarTest : public testing::Test {
37 public:
38 PrefChangeRegistrarTest() {}
39 ~PrefChangeRegistrarTest() override {}
40
41 protected:
42 void SetUp() override;
43
44 base::Closure observer() const {
45 return base::Bind(&base::DoNothing);
46 }
47
48 MockPrefService* service() const { return service_.get(); }
49
50 private:
51 scoped_ptr<MockPrefService> service_;
52 };
53
54 void PrefChangeRegistrarTest::SetUp() {
55 service_.reset(new MockPrefService());
56 }
57
58 TEST_F(PrefChangeRegistrarTest, AddAndRemove) {
59 PrefChangeRegistrar registrar;
60 registrar.Init(service());
61
62 // Test adding.
63 EXPECT_CALL(*service(),
64 AddPrefObserver(Eq(std::string("test.pref.1")), &registrar));
65 EXPECT_CALL(*service(),
66 AddPrefObserver(Eq(std::string("test.pref.2")), &registrar));
67 registrar.Add("test.pref.1", observer());
68 registrar.Add("test.pref.2", observer());
69 EXPECT_FALSE(registrar.IsEmpty());
70
71 // Test removing.
72 Mock::VerifyAndClearExpectations(service());
73 EXPECT_CALL(*service(),
74 RemovePrefObserver(Eq(std::string("test.pref.1")), &registrar));
75 EXPECT_CALL(*service(),
76 RemovePrefObserver(Eq(std::string("test.pref.2")), &registrar));
77 registrar.Remove("test.pref.1");
78 registrar.Remove("test.pref.2");
79 EXPECT_TRUE(registrar.IsEmpty());
80
81 // Explicitly check the expectations now to make sure that the Removes
82 // worked (rather than the registrar destructor doing the work).
83 Mock::VerifyAndClearExpectations(service());
84 }
85
86 TEST_F(PrefChangeRegistrarTest, AutoRemove) {
87 PrefChangeRegistrar registrar;
88 registrar.Init(service());
89
90 // Setup of auto-remove.
91 EXPECT_CALL(*service(),
92 AddPrefObserver(Eq(std::string("test.pref.1")), &registrar));
93 registrar.Add("test.pref.1", observer());
94 Mock::VerifyAndClearExpectations(service());
95 EXPECT_FALSE(registrar.IsEmpty());
96
97 // Test auto-removing.
98 EXPECT_CALL(*service(),
99 RemovePrefObserver(Eq(std::string("test.pref.1")), &registrar));
100 }
101
102 TEST_F(PrefChangeRegistrarTest, RemoveAll) {
103 PrefChangeRegistrar registrar;
104 registrar.Init(service());
105
106 EXPECT_CALL(*service(),
107 AddPrefObserver(Eq(std::string("test.pref.1")), &registrar));
108 EXPECT_CALL(*service(),
109 AddPrefObserver(Eq(std::string("test.pref.2")), &registrar));
110 registrar.Add("test.pref.1", observer());
111 registrar.Add("test.pref.2", observer());
112 Mock::VerifyAndClearExpectations(service());
113
114 EXPECT_CALL(*service(),
115 RemovePrefObserver(Eq(std::string("test.pref.1")), &registrar));
116 EXPECT_CALL(*service(),
117 RemovePrefObserver(Eq(std::string("test.pref.2")), &registrar));
118 registrar.RemoveAll();
119 EXPECT_TRUE(registrar.IsEmpty());
120
121 // Explicitly check the expectations now to make sure that the RemoveAll
122 // worked (rather than the registrar destructor doing the work).
123 Mock::VerifyAndClearExpectations(service());
124 }
125
126 class ObserveSetOfPreferencesTest : public testing::Test {
127 public:
128 virtual void SetUp() {
129 pref_service_.reset(new TestingPrefServiceSimple);
130 PrefRegistrySimple* registry = pref_service_->registry();
131 registry->RegisterStringPref(kHomePage, "http://google.com");
132 registry->RegisterBooleanPref(kHomePageIsNewTabPage, false);
133 registry->RegisterStringPref(kApplicationLocale, std::string());
134 }
135
136 PrefChangeRegistrar* CreatePrefChangeRegistrar() {
137 PrefChangeRegistrar* pref_set = new PrefChangeRegistrar();
138 base::Closure callback = base::Bind(&base::DoNothing);
139 pref_set->Init(pref_service_.get());
140 pref_set->Add(kHomePage, callback);
141 pref_set->Add(kHomePageIsNewTabPage, callback);
142 return pref_set;
143 }
144
145 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
146
147 scoped_ptr<TestingPrefServiceSimple> pref_service_;
148 };
149
150 TEST_F(ObserveSetOfPreferencesTest, IsObserved) {
151 scoped_ptr<PrefChangeRegistrar> pref_set(CreatePrefChangeRegistrar());
152 EXPECT_TRUE(pref_set->IsObserved(kHomePage));
153 EXPECT_TRUE(pref_set->IsObserved(kHomePageIsNewTabPage));
154 EXPECT_FALSE(pref_set->IsObserved(kApplicationLocale));
155 }
156
157 TEST_F(ObserveSetOfPreferencesTest, IsManaged) {
158 scoped_ptr<PrefChangeRegistrar> pref_set(CreatePrefChangeRegistrar());
159 EXPECT_FALSE(pref_set->IsManaged());
160 pref_service_->SetManagedPref(kHomePage,
161 new StringValue("http://crbug.com"));
162 EXPECT_TRUE(pref_set->IsManaged());
163 pref_service_->SetManagedPref(kHomePageIsNewTabPage,
164 new FundamentalValue(true));
165 EXPECT_TRUE(pref_set->IsManaged());
166 pref_service_->RemoveManagedPref(kHomePage);
167 EXPECT_TRUE(pref_set->IsManaged());
168 pref_service_->RemoveManagedPref(kHomePageIsNewTabPage);
169 EXPECT_FALSE(pref_set->IsManaged());
170 }
171
172 TEST_F(ObserveSetOfPreferencesTest, Observe) {
173 using testing::_;
174 using testing::Mock;
175
176 PrefChangeRegistrar pref_set;
177 PrefChangeRegistrar::NamedChangeCallback callback = base::Bind(
178 &ObserveSetOfPreferencesTest::OnPreferenceChanged,
179 base::Unretained(this));
180 pref_set.Init(pref_service_.get());
181 pref_set.Add(kHomePage, callback);
182 pref_set.Add(kHomePageIsNewTabPage, callback);
183
184 EXPECT_CALL(*this, OnPreferenceChanged(kHomePage));
185 pref_service_->SetUserPref(kHomePage, new StringValue("http://crbug.com"));
186 Mock::VerifyAndClearExpectations(this);
187
188 EXPECT_CALL(*this, OnPreferenceChanged(kHomePageIsNewTabPage));
189 pref_service_->SetUserPref(kHomePageIsNewTabPage,
190 new FundamentalValue(true));
191 Mock::VerifyAndClearExpectations(this);
192
193 EXPECT_CALL(*this, OnPreferenceChanged(_)).Times(0);
194 pref_service_->SetUserPref(kApplicationLocale, new StringValue("en_US.utf8"));
195 Mock::VerifyAndClearExpectations(this);
196 }
197
198 } // namespace base
OLDNEW
« no previous file with comments | « base/prefs/pref_change_registrar.cc ('k') | base/prefs/pref_filter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698