OLD | NEW |
| (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_observer.h" | |
8 #include "base/prefs/pref_registry_simple.h" | |
9 #include "base/prefs/public/pref_change_registrar.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, | |
31 void(const char*, PrefObserver*)); | |
32 MOCK_METHOD2(RemovePrefObserver, | |
33 void(const char*, PrefObserver*)); | |
34 }; | |
35 | |
36 } // namespace | |
37 | |
38 class PrefChangeRegistrarTest : public testing::Test { | |
39 public: | |
40 PrefChangeRegistrarTest() {} | |
41 virtual ~PrefChangeRegistrarTest() {} | |
42 | |
43 protected: | |
44 virtual void SetUp() OVERRIDE; | |
45 | |
46 base::Closure observer() const { | |
47 return base::Bind(&base::DoNothing); | |
48 } | |
49 | |
50 MockPrefService* service() const { return service_.get(); } | |
51 | |
52 private: | |
53 scoped_ptr<MockPrefService> service_; | |
54 }; | |
55 | |
56 void PrefChangeRegistrarTest::SetUp() { | |
57 service_.reset(new MockPrefService()); | |
58 } | |
59 | |
60 TEST_F(PrefChangeRegistrarTest, AddAndRemove) { | |
61 PrefChangeRegistrar registrar; | |
62 registrar.Init(service()); | |
63 | |
64 // Test adding. | |
65 EXPECT_CALL(*service(), | |
66 AddPrefObserver(Eq(std::string("test.pref.1")), ®istrar)); | |
67 EXPECT_CALL(*service(), | |
68 AddPrefObserver(Eq(std::string("test.pref.2")), ®istrar)); | |
69 registrar.Add("test.pref.1", observer()); | |
70 registrar.Add("test.pref.2", observer()); | |
71 EXPECT_FALSE(registrar.IsEmpty()); | |
72 | |
73 // Test removing. | |
74 Mock::VerifyAndClearExpectations(service()); | |
75 EXPECT_CALL(*service(), | |
76 RemovePrefObserver(Eq(std::string("test.pref.1")), ®istrar)); | |
77 EXPECT_CALL(*service(), | |
78 RemovePrefObserver(Eq(std::string("test.pref.2")), ®istrar)); | |
79 registrar.Remove("test.pref.1"); | |
80 registrar.Remove("test.pref.2"); | |
81 EXPECT_TRUE(registrar.IsEmpty()); | |
82 | |
83 // Explicitly check the expectations now to make sure that the Removes | |
84 // worked (rather than the registrar destructor doing the work). | |
85 Mock::VerifyAndClearExpectations(service()); | |
86 } | |
87 | |
88 TEST_F(PrefChangeRegistrarTest, AutoRemove) { | |
89 PrefChangeRegistrar registrar; | |
90 registrar.Init(service()); | |
91 | |
92 // Setup of auto-remove. | |
93 EXPECT_CALL(*service(), | |
94 AddPrefObserver(Eq(std::string("test.pref.1")), ®istrar)); | |
95 registrar.Add("test.pref.1", observer()); | |
96 Mock::VerifyAndClearExpectations(service()); | |
97 EXPECT_FALSE(registrar.IsEmpty()); | |
98 | |
99 // Test auto-removing. | |
100 EXPECT_CALL(*service(), | |
101 RemovePrefObserver(Eq(std::string("test.pref.1")), ®istrar)); | |
102 } | |
103 | |
104 TEST_F(PrefChangeRegistrarTest, RemoveAll) { | |
105 PrefChangeRegistrar registrar; | |
106 registrar.Init(service()); | |
107 | |
108 EXPECT_CALL(*service(), | |
109 AddPrefObserver(Eq(std::string("test.pref.1")), ®istrar)); | |
110 EXPECT_CALL(*service(), | |
111 AddPrefObserver(Eq(std::string("test.pref.2")), ®istrar)); | |
112 registrar.Add("test.pref.1", observer()); | |
113 registrar.Add("test.pref.2", observer()); | |
114 Mock::VerifyAndClearExpectations(service()); | |
115 | |
116 EXPECT_CALL(*service(), | |
117 RemovePrefObserver(Eq(std::string("test.pref.1")), ®istrar)); | |
118 EXPECT_CALL(*service(), | |
119 RemovePrefObserver(Eq(std::string("test.pref.2")), ®istrar)); | |
120 registrar.RemoveAll(); | |
121 EXPECT_TRUE(registrar.IsEmpty()); | |
122 | |
123 // Explicitly check the expectations now to make sure that the RemoveAll | |
124 // worked (rather than the registrar destructor doing the work). | |
125 Mock::VerifyAndClearExpectations(service()); | |
126 } | |
127 | |
128 class ObserveSetOfPreferencesTest : public testing::Test { | |
129 public: | |
130 virtual void SetUp() { | |
131 pref_service_.reset(new TestingPrefServiceSimple); | |
132 PrefRegistrySimple* registry = pref_service_->registry(); | |
133 registry->RegisterStringPref(kHomePage, "http://google.com"); | |
134 registry->RegisterBooleanPref(kHomePageIsNewTabPage, false); | |
135 registry->RegisterStringPref(kApplicationLocale, ""); | |
136 } | |
137 | |
138 PrefChangeRegistrar* CreatePrefChangeRegistrar() { | |
139 PrefChangeRegistrar* pref_set = new PrefChangeRegistrar(); | |
140 base::Closure callback = base::Bind(&base::DoNothing); | |
141 pref_set->Init(pref_service_.get()); | |
142 pref_set->Add(kHomePage, callback); | |
143 pref_set->Add(kHomePageIsNewTabPage, callback); | |
144 return pref_set; | |
145 } | |
146 | |
147 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&)); | |
148 | |
149 scoped_ptr<TestingPrefServiceSimple> pref_service_; | |
150 }; | |
151 | |
152 TEST_F(ObserveSetOfPreferencesTest, IsObserved) { | |
153 scoped_ptr<PrefChangeRegistrar> pref_set(CreatePrefChangeRegistrar()); | |
154 EXPECT_TRUE(pref_set->IsObserved(kHomePage)); | |
155 EXPECT_TRUE(pref_set->IsObserved(kHomePageIsNewTabPage)); | |
156 EXPECT_FALSE(pref_set->IsObserved(kApplicationLocale)); | |
157 } | |
158 | |
159 TEST_F(ObserveSetOfPreferencesTest, IsManaged) { | |
160 scoped_ptr<PrefChangeRegistrar> pref_set(CreatePrefChangeRegistrar()); | |
161 EXPECT_FALSE(pref_set->IsManaged()); | |
162 pref_service_->SetManagedPref(kHomePage, | |
163 new StringValue("http://crbug.com")); | |
164 EXPECT_TRUE(pref_set->IsManaged()); | |
165 pref_service_->SetManagedPref(kHomePageIsNewTabPage, | |
166 new FundamentalValue(true)); | |
167 EXPECT_TRUE(pref_set->IsManaged()); | |
168 pref_service_->RemoveManagedPref(kHomePage); | |
169 EXPECT_TRUE(pref_set->IsManaged()); | |
170 pref_service_->RemoveManagedPref(kHomePageIsNewTabPage); | |
171 EXPECT_FALSE(pref_set->IsManaged()); | |
172 } | |
173 | |
174 TEST_F(ObserveSetOfPreferencesTest, Observe) { | |
175 using testing::_; | |
176 using testing::Mock; | |
177 | |
178 PrefChangeRegistrar pref_set; | |
179 PrefChangeRegistrar::NamedChangeCallback callback = base::Bind( | |
180 &ObserveSetOfPreferencesTest::OnPreferenceChanged, | |
181 base::Unretained(this)); | |
182 pref_set.Init(pref_service_.get()); | |
183 pref_set.Add(kHomePage, callback); | |
184 pref_set.Add(kHomePageIsNewTabPage, callback); | |
185 | |
186 EXPECT_CALL(*this, OnPreferenceChanged(kHomePage)); | |
187 pref_service_->SetUserPref(kHomePage, new StringValue("http://crbug.com")); | |
188 Mock::VerifyAndClearExpectations(this); | |
189 | |
190 EXPECT_CALL(*this, OnPreferenceChanged(kHomePageIsNewTabPage)); | |
191 pref_service_->SetUserPref(kHomePageIsNewTabPage, | |
192 new FundamentalValue(true)); | |
193 Mock::VerifyAndClearExpectations(this); | |
194 | |
195 EXPECT_CALL(*this, OnPreferenceChanged(_)).Times(0); | |
196 pref_service_->SetUserPref(kApplicationLocale, new StringValue("en_US.utf8")); | |
197 Mock::VerifyAndClearExpectations(this); | |
198 } | |
199 | |
200 } // namespace base | |
OLD | NEW |