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

Side by Side Diff: chrome/browser/prefs/pref_service_unittest.cc

Issue 3304015: Use PrefChangeRegistrar everywhere (Closed)
Patch Set: final version for commit Created 10 years, 2 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 | « chrome/browser/prefs/pref_service.h ('k') | chrome/browser/prefs/pref_set_observer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 6
7 #include "app/test/data/resource.h" 7 #include "app/test/data/resource.h"
8 #include "base/scoped_ptr.h" 8 #include "base/scoped_ptr.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/browser/prefs/dummy_pref_store.h" 10 #include "chrome/browser/prefs/dummy_pref_store.h"
11 #include "chrome/browser/prefs/pref_change_registrar.h"
11 #include "chrome/browser/prefs/pref_value_store.h" 12 #include "chrome/browser/prefs/pref_value_store.h"
12 #include "chrome/common/chrome_paths.h" 13 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/notification_observer_mock.h" 14 #include "chrome/common/notification_observer_mock.h"
14 #include "chrome/common/notification_service.h" 15 #include "chrome/common/notification_service.h"
15 #include "chrome/common/notification_type.h" 16 #include "chrome/common/notification_type.h"
16 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
17 #include "chrome/test/testing_pref_service.h" 18 #include "chrome/test/testing_pref_service.h"
18 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 21
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 #endif 88 #endif
88 89
89 TEST(PrefServiceTest, NoObserverFire) { 90 TEST(PrefServiceTest, NoObserverFire) {
90 TestingPrefService prefs; 91 TestingPrefService prefs;
91 92
92 const char pref_name[] = "homepage"; 93 const char pref_name[] = "homepage";
93 prefs.RegisterStringPref(pref_name, ""); 94 prefs.RegisterStringPref(pref_name, "");
94 95
95 const std::string new_pref_value("http://www.google.com/"); 96 const std::string new_pref_value("http://www.google.com/");
96 TestPrefObserver obs(&prefs, pref_name, new_pref_value); 97 TestPrefObserver obs(&prefs, pref_name, new_pref_value);
97 prefs.AddPrefObserver(pref_name, &obs); 98
99 PrefChangeRegistrar registrar;
100 registrar.Init(&prefs);
101 registrar.Add(pref_name, &obs);
98 // This should fire the checks in TestPrefObserver::Observe. 102 // This should fire the checks in TestPrefObserver::Observe.
99 prefs.SetString(pref_name, new_pref_value); 103 prefs.SetString(pref_name, new_pref_value);
100 104
101 // Make sure the observer was actually fired. 105 // Make sure the observer was actually fired.
102 EXPECT_TRUE(obs.observer_fired()); 106 EXPECT_TRUE(obs.observer_fired());
103 107
104 // Setting the pref to the same value should not set the pref value a second 108 // Setting the pref to the same value should not set the pref value a second
105 // time. 109 // time.
106 obs.Reset(new_pref_value); 110 obs.Reset(new_pref_value);
107 prefs.SetString(pref_name, new_pref_value); 111 prefs.SetString(pref_name, new_pref_value);
108 EXPECT_FALSE(obs.observer_fired()); 112 EXPECT_FALSE(obs.observer_fired());
109 113
110 // Clearing the pref should cause the pref to fire. 114 // Clearing the pref should cause the pref to fire.
111 obs.Reset(""); 115 obs.Reset("");
112 prefs.ClearPref(pref_name); 116 prefs.ClearPref(pref_name);
113 EXPECT_TRUE(obs.observer_fired()); 117 EXPECT_TRUE(obs.observer_fired());
114 118
115 // Clearing the pref again should not cause the pref to fire. 119 // Clearing the pref again should not cause the pref to fire.
116 obs.Reset(""); 120 obs.Reset("");
117 prefs.ClearPref(pref_name); 121 prefs.ClearPref(pref_name);
118 EXPECT_FALSE(obs.observer_fired()); 122 EXPECT_FALSE(obs.observer_fired());
119
120 // Ok, clean up.
121 prefs.RemovePrefObserver(pref_name, &obs);
122 } 123 }
123 124
124 TEST(PrefServiceTest, HasPrefPath) { 125 TEST(PrefServiceTest, HasPrefPath) {
125 TestingPrefService prefs; 126 TestingPrefService prefs;
126 127
127 const char path[] = "fake.path"; 128 const char path[] = "fake.path";
128 129
129 // Shouldn't initially have a path. 130 // Shouldn't initially have a path.
130 EXPECT_FALSE(prefs.HasPrefPath(path)); 131 EXPECT_FALSE(prefs.HasPrefPath(path));
131 132
132 // Register the path. This doesn't set a value, so the path still shouldn't 133 // Register the path. This doesn't set a value, so the path still shouldn't
133 // exist. 134 // exist.
134 prefs.RegisterStringPref(path, std::string()); 135 prefs.RegisterStringPref(path, std::string());
135 EXPECT_FALSE(prefs.HasPrefPath(path)); 136 EXPECT_FALSE(prefs.HasPrefPath(path));
136 137
137 // Set a value and make sure we have a path. 138 // Set a value and make sure we have a path.
138 prefs.SetString(path, "blah"); 139 prefs.SetString(path, "blah");
139 EXPECT_TRUE(prefs.HasPrefPath(path)); 140 EXPECT_TRUE(prefs.HasPrefPath(path));
140 } 141 }
141 142
142 TEST(PrefServiceTest, Observers) { 143 TEST(PrefServiceTest, Observers) {
143 const char pref_name[] = "homepage"; 144 const char pref_name[] = "homepage";
144 145
145 TestingPrefService prefs; 146 TestingPrefService prefs;
146 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); 147 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com"));
147 prefs.RegisterStringPref(pref_name, ""); 148 prefs.RegisterStringPref(pref_name, "");
148 149
149 const std::string new_pref_value("http://www.google.com/"); 150 const std::string new_pref_value("http://www.google.com/");
150 TestPrefObserver obs(&prefs, pref_name, new_pref_value); 151 TestPrefObserver obs(&prefs, pref_name, new_pref_value);
151 prefs.AddPrefObserver(pref_name, &obs); 152 PrefChangeRegistrar registrar;
153 registrar.Init(&prefs);
154 registrar.Add(pref_name, &obs);
152 // This should fire the checks in TestPrefObserver::Observe. 155 // This should fire the checks in TestPrefObserver::Observe.
153 prefs.SetString(pref_name, new_pref_value); 156 prefs.SetString(pref_name, new_pref_value);
154 157
155 // Make sure the tests were actually run. 158 // Make sure the tests were actually run.
156 EXPECT_TRUE(obs.observer_fired()); 159 EXPECT_TRUE(obs.observer_fired());
157 160
158 // Now try adding a second pref observer. 161 // Now try adding a second pref observer.
159 const std::string new_pref_value2("http://www.youtube.com/"); 162 const std::string new_pref_value2("http://www.youtube.com/");
160 obs.Reset(new_pref_value2); 163 obs.Reset(new_pref_value2);
161 TestPrefObserver obs2(&prefs, pref_name, new_pref_value2); 164 TestPrefObserver obs2(&prefs, pref_name, new_pref_value2);
162 prefs.AddPrefObserver(pref_name, &obs2); 165 registrar.Add(pref_name, &obs2);
163 // This should fire the checks in obs and obs2. 166 // This should fire the checks in obs and obs2.
164 prefs.SetString(pref_name, new_pref_value2); 167 prefs.SetString(pref_name, new_pref_value2);
165 EXPECT_TRUE(obs.observer_fired()); 168 EXPECT_TRUE(obs.observer_fired());
166 EXPECT_TRUE(obs2.observer_fired()); 169 EXPECT_TRUE(obs2.observer_fired());
167 170
168 // Make sure obs2 still works after removing obs. 171 // Make sure obs2 still works after removing obs.
169 prefs.RemovePrefObserver(pref_name, &obs); 172 registrar.Remove(pref_name, &obs);
170 obs.Reset(""); 173 obs.Reset("");
171 obs2.Reset(new_pref_value); 174 obs2.Reset(new_pref_value);
172 // This should only fire the observer in obs2. 175 // This should only fire the observer in obs2.
173 prefs.SetString(pref_name, new_pref_value); 176 prefs.SetString(pref_name, new_pref_value);
174 EXPECT_FALSE(obs.observer_fired()); 177 EXPECT_FALSE(obs.observer_fired());
175 EXPECT_TRUE(obs2.observer_fired()); 178 EXPECT_TRUE(obs2.observer_fired());
176
177 // Ok, clean up.
178 prefs.RemovePrefObserver(pref_name, &obs2);
179 } 179 }
180 180
181 class PrefServiceSetValueTest : public testing::Test { 181 class PrefServiceSetValueTest : public testing::Test {
182 protected: 182 protected:
183 static const char name_[]; 183 static const char name_[];
184 static const char value_[]; 184 static const char value_[];
185 185
186 PrefServiceSetValueTest() 186 PrefServiceSetValueTest()
187 : name_string_(name_), 187 : name_string_(name_),
188 null_value_(Value::CreateNullValue()) {} 188 null_value_(Value::CreateNullValue()) {}
(...skipping 15 matching lines...) Expand all
204 NotificationObserverMock observer_; 204 NotificationObserverMock observer_;
205 }; 205 };
206 206
207 const char PrefServiceSetValueTest::name_[] = "name"; 207 const char PrefServiceSetValueTest::name_[] = "name";
208 const char PrefServiceSetValueTest::value_[] = "value"; 208 const char PrefServiceSetValueTest::value_[] = "value";
209 209
210 TEST_F(PrefServiceSetValueTest, SetStringValue) { 210 TEST_F(PrefServiceSetValueTest, SetStringValue) {
211 const char default_string[] = "default"; 211 const char default_string[] = "default";
212 scoped_ptr<Value> default_value(Value::CreateStringValue(default_string)); 212 scoped_ptr<Value> default_value(Value::CreateStringValue(default_string));
213 prefs_.RegisterStringPref(name_, default_string); 213 prefs_.RegisterStringPref(name_, default_string);
214 prefs_.AddPrefObserver(name_, &observer_); 214
215 PrefChangeRegistrar registrar;
216 registrar.Init(&prefs_);
217 registrar.Add(name_, &observer_);
218
215 // Changing the controlling store from default to user triggers notification. 219 // Changing the controlling store from default to user triggers notification.
216 SetExpectPrefChanged(); 220 SetExpectPrefChanged();
217 prefs_.Set(name_, *default_value); 221 prefs_.Set(name_, *default_value);
218 Mock::VerifyAndClearExpectations(&observer_); 222 Mock::VerifyAndClearExpectations(&observer_);
219 223
220 SetExpectNoNotification(); 224 SetExpectNoNotification();
221 prefs_.Set(name_, *default_value); 225 prefs_.Set(name_, *default_value);
222 Mock::VerifyAndClearExpectations(&observer_); 226 Mock::VerifyAndClearExpectations(&observer_);
223 227
224 scoped_ptr<Value> new_value(Value::CreateStringValue(value_)); 228 scoped_ptr<Value> new_value(Value::CreateStringValue(value_));
225 SetExpectPrefChanged(); 229 SetExpectPrefChanged();
226 prefs_.Set(name_, *new_value); 230 prefs_.Set(name_, *new_value);
227 EXPECT_EQ(value_, prefs_.GetString(name_)); 231 EXPECT_EQ(value_, prefs_.GetString(name_));
228
229 prefs_.RemovePrefObserver(name_, &observer_);
230 } 232 }
231 233
232 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { 234 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
233 prefs_.RegisterDictionaryPref(name_); 235 prefs_.RegisterDictionaryPref(name_);
234 prefs_.AddPrefObserver(name_, &observer_); 236 PrefChangeRegistrar registrar;
237 registrar.Init(&prefs_);
238 registrar.Add(name_, &observer_);
235 239
236 // Dictionary values are special: setting one to NULL is the same as clearing 240 // Dictionary values are special: setting one to NULL is the same as clearing
237 // the user value, allowing the NULL default to take (or keep) control. 241 // the user value, allowing the NULL default to take (or keep) control.
238 SetExpectNoNotification(); 242 SetExpectNoNotification();
239 prefs_.Set(name_, *null_value_); 243 prefs_.Set(name_, *null_value_);
240 Mock::VerifyAndClearExpectations(&observer_); 244 Mock::VerifyAndClearExpectations(&observer_);
241 245
242 DictionaryValue new_value; 246 DictionaryValue new_value;
243 new_value.SetString(name_, value_); 247 new_value.SetString(name_, value_);
244 SetExpectPrefChanged(); 248 SetExpectPrefChanged();
245 prefs_.Set(name_, new_value); 249 prefs_.Set(name_, new_value);
246 Mock::VerifyAndClearExpectations(&observer_); 250 Mock::VerifyAndClearExpectations(&observer_);
247 DictionaryValue* dict = prefs_.GetMutableDictionary(name_); 251 DictionaryValue* dict = prefs_.GetMutableDictionary(name_);
248 EXPECT_EQ(1U, dict->size()); 252 EXPECT_EQ(1U, dict->size());
249 std::string out_value; 253 std::string out_value;
250 dict->GetString(name_, &out_value); 254 dict->GetString(name_, &out_value);
251 EXPECT_EQ(value_, out_value); 255 EXPECT_EQ(value_, out_value);
252 256
253 SetExpectNoNotification(); 257 SetExpectNoNotification();
254 prefs_.Set(name_, new_value); 258 prefs_.Set(name_, new_value);
255 Mock::VerifyAndClearExpectations(&observer_); 259 Mock::VerifyAndClearExpectations(&observer_);
256 260
257 SetExpectPrefChanged(); 261 SetExpectPrefChanged();
258 prefs_.Set(name_, *null_value_); 262 prefs_.Set(name_, *null_value_);
259 Mock::VerifyAndClearExpectations(&observer_); 263 Mock::VerifyAndClearExpectations(&observer_);
260 dict = prefs_.GetMutableDictionary(name_); 264 dict = prefs_.GetMutableDictionary(name_);
261 EXPECT_EQ(0U, dict->size()); 265 EXPECT_EQ(0U, dict->size());
262
263 prefs_.RemovePrefObserver(name_, &observer_);
264 } 266 }
265 267
266 TEST_F(PrefServiceSetValueTest, SetListValue) { 268 TEST_F(PrefServiceSetValueTest, SetListValue) {
267 prefs_.RegisterListPref(name_); 269 prefs_.RegisterListPref(name_);
268 prefs_.AddPrefObserver(name_, &observer_); 270 PrefChangeRegistrar registrar;
271 registrar.Init(&prefs_);
272 registrar.Add(name_, &observer_);
269 273
270 // List values are special: setting one to NULL is the same as clearing the 274 // List values are special: setting one to NULL is the same as clearing the
271 // user value, allowing the NULL default to take (or keep) control. 275 // user value, allowing the NULL default to take (or keep) control.
272 SetExpectNoNotification(); 276 SetExpectNoNotification();
273 prefs_.Set(name_, *null_value_); 277 prefs_.Set(name_, *null_value_);
274 Mock::VerifyAndClearExpectations(&observer_); 278 Mock::VerifyAndClearExpectations(&observer_);
275 279
276 ListValue new_value; 280 ListValue new_value;
277 new_value.Append(Value::CreateStringValue(value_)); 281 new_value.Append(Value::CreateStringValue(value_));
278 SetExpectPrefChanged(); 282 SetExpectPrefChanged();
279 prefs_.Set(name_, new_value); 283 prefs_.Set(name_, new_value);
280 Mock::VerifyAndClearExpectations(&observer_); 284 Mock::VerifyAndClearExpectations(&observer_);
281 ListValue* list = prefs_.GetMutableList(name_); 285 ListValue* list = prefs_.GetMutableList(name_);
282 ASSERT_EQ(1U, list->GetSize()); 286 ASSERT_EQ(1U, list->GetSize());
283 std::string out_value; 287 std::string out_value;
284 list->GetString(0, &out_value); 288 list->GetString(0, &out_value);
285 EXPECT_EQ(value_, out_value); 289 EXPECT_EQ(value_, out_value);
286 290
287 SetExpectNoNotification(); 291 SetExpectNoNotification();
288 prefs_.Set(name_, new_value); 292 prefs_.Set(name_, new_value);
289 Mock::VerifyAndClearExpectations(&observer_); 293 Mock::VerifyAndClearExpectations(&observer_);
290 294
291 SetExpectPrefChanged(); 295 SetExpectPrefChanged();
292 prefs_.Set(name_, *null_value_); 296 prefs_.Set(name_, *null_value_);
293 Mock::VerifyAndClearExpectations(&observer_); 297 Mock::VerifyAndClearExpectations(&observer_);
294 list = prefs_.GetMutableList(name_); 298 list = prefs_.GetMutableList(name_);
295 EXPECT_EQ(0U, list->GetSize()); 299 EXPECT_EQ(0U, list->GetSize());
296
297 prefs_.RemovePrefObserver(name_, &observer_);
298 } 300 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_service.h ('k') | chrome/browser/prefs/pref_set_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698