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

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

Issue 5441002: Clean up pref change notification handling. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix memory leaks in tests. Created 10 years 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 | Annotate | Revision Log
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/command_line.h" 8 #include "base/command_line.h"
9 #include "base/scoped_ptr.h" 9 #include "base/scoped_ptr.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/policy/configuration_policy_pref_store.h" 11 #include "chrome/browser/policy/configuration_policy_pref_store.h"
12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
13 #include "chrome/browser/prefs/browser_prefs.h" 13 #include "chrome/browser/prefs/browser_prefs.h"
14 #include "chrome/browser/prefs/command_line_pref_store.h" 14 #include "chrome/browser/prefs/command_line_pref_store.h"
15 #include "chrome/browser/prefs/default_pref_store.h" 15 #include "chrome/browser/prefs/default_pref_store.h"
16 #include "chrome/browser/prefs/dummy_pref_store.h" 16 #include "chrome/browser/prefs/testing_pref_store.h"
17 #include "chrome/browser/prefs/pref_change_registrar.h" 17 #include "chrome/browser/prefs/pref_change_registrar.h"
18 #include "chrome/browser/prefs/pref_observer_mock.h"
18 #include "chrome/browser/prefs/pref_value_store.h" 19 #include "chrome/browser/prefs/pref_value_store.h"
19 #include "chrome/common/chrome_paths.h" 20 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/notification_observer_mock.h"
22 #include "chrome/common/notification_service.h"
23 #include "chrome/common/notification_type.h"
24 #include "chrome/common/pref_names.h" 22 #include "chrome/common/pref_names.h"
25 #include "chrome/test/testing_pref_service.h" 23 #include "chrome/test/testing_pref_service.h"
26 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
28 26
29 using testing::_; 27 using testing::_;
30 using testing::Mock; 28 using testing::Mock;
31 using testing::Pointee;
32 using testing::Property;
33
34 namespace {
35
36 class TestPrefObserver : public NotificationObserver {
37 public:
38 TestPrefObserver(const PrefService* prefs,
39 const std::string& pref_name,
40 const std::string& new_pref_value)
41 : observer_fired_(false),
42 prefs_(prefs),
43 pref_name_(pref_name),
44 new_pref_value_(new_pref_value) {}
45 virtual ~TestPrefObserver() {}
46
47 virtual void Observe(NotificationType type,
48 const NotificationSource& source,
49 const NotificationDetails& details) {
50 EXPECT_EQ(type.value, NotificationType::PREF_CHANGED);
51 const PrefService* prefs_in = Source<PrefService>(source).ptr();
52 EXPECT_EQ(prefs_in, prefs_);
53 const std::string* pref_name_in = Details<std::string>(details).ptr();
54 EXPECT_EQ(*pref_name_in, pref_name_);
55 EXPECT_EQ(new_pref_value_, prefs_in->GetString("homepage"));
56 observer_fired_ = true;
57 }
58
59 bool observer_fired() { return observer_fired_; }
60
61 void Reset(const std::string& new_pref_value) {
62 observer_fired_ = false;
63 new_pref_value_ = new_pref_value;
64 }
65
66 private:
67 bool observer_fired_;
68 const PrefService* prefs_;
69 const std::string pref_name_;
70 std::string new_pref_value_;
71 };
72
73 } // namespace
74 29
75 // TODO(port): port this test to POSIX. 30 // TODO(port): port this test to POSIX.
76 #if defined(OS_WIN) 31 #if defined(OS_WIN)
77 TEST(PrefServiceTest, LocalizedPrefs) { 32 TEST(PrefServiceTest, LocalizedPrefs) {
78 TestingPrefService prefs; 33 TestingPrefService prefs;
79 const char kBoolean[] = "boolean"; 34 const char kBoolean[] = "boolean";
80 const char kInteger[] = "integer"; 35 const char kInteger[] = "integer";
81 const char kString[] = "string"; 36 const char kString[] = "string";
82 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL); 37 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL);
83 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT); 38 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT);
(...skipping 12 matching lines...) Expand all
96 EXPECT_EQ("foo", prefs.GetString(kString)); 51 EXPECT_EQ("foo", prefs.GetString(kString));
97 } 52 }
98 #endif 53 #endif
99 54
100 TEST(PrefServiceTest, NoObserverFire) { 55 TEST(PrefServiceTest, NoObserverFire) {
101 TestingPrefService prefs; 56 TestingPrefService prefs;
102 57
103 const char pref_name[] = "homepage"; 58 const char pref_name[] = "homepage";
104 prefs.RegisterStringPref(pref_name, std::string()); 59 prefs.RegisterStringPref(pref_name, std::string());
105 60
106 const std::string new_pref_value("http://www.google.com/"); 61 const char new_pref_value[] = "http://www.google.com/";
107 TestPrefObserver obs(&prefs, pref_name, new_pref_value); 62 PrefObserverMock obs;
108
109 PrefChangeRegistrar registrar; 63 PrefChangeRegistrar registrar;
110 registrar.Init(&prefs); 64 registrar.Init(&prefs);
111 registrar.Add(pref_name, &obs); 65 registrar.Add(pref_name, &obs);
112 // This should fire the checks in TestPrefObserver::Observe. 66
67 // This should fire the checks in PrefObserverMock::Observe.
68 StringValue expected_value(new_pref_value);
danno 2010/12/06 09:20:14 can you make this const?
Mattias Nissler (ping if slow) 2010/12/06 10:58:12 Done.
69 obs.Expect(&prefs, pref_name, &expected_value);
113 prefs.SetString(pref_name, new_pref_value); 70 prefs.SetString(pref_name, new_pref_value);
114 71 Mock::VerifyAndClearExpectations(&obs);
115 // Make sure the observer was actually fired.
116 EXPECT_TRUE(obs.observer_fired());
117 72
118 // Setting the pref to the same value should not set the pref value a second 73 // Setting the pref to the same value should not set the pref value a second
119 // time. 74 // time.
120 obs.Reset(new_pref_value); 75 EXPECT_CALL(obs, Observe(_, _, _)).Times(0);
121 prefs.SetString(pref_name, new_pref_value); 76 prefs.SetString(pref_name, new_pref_value);
122 EXPECT_FALSE(obs.observer_fired()); 77 Mock::VerifyAndClearExpectations(&obs);
123 78
124 // Clearing the pref should cause the pref to fire. 79 // Clearing the pref should cause the pref to fire.
125 obs.Reset(std::string()); 80 StringValue expected_default_value("");
danno 2010/12/06 09:20:14 this too?
Mattias Nissler (ping if slow) 2010/12/06 10:58:12 Done. And a few others as well.
81 obs.Expect(&prefs, pref_name, &expected_default_value);
126 prefs.ClearPref(pref_name); 82 prefs.ClearPref(pref_name);
127 EXPECT_TRUE(obs.observer_fired()); 83 Mock::VerifyAndClearExpectations(&obs);
128 84
129 // Clearing the pref again should not cause the pref to fire. 85 // Clearing the pref again should not cause the pref to fire.
130 obs.Reset(std::string()); 86 EXPECT_CALL(obs, Observe(_, _, _)).Times(0);
131 prefs.ClearPref(pref_name); 87 prefs.ClearPref(pref_name);
132 EXPECT_FALSE(obs.observer_fired()); 88 Mock::VerifyAndClearExpectations(&obs);
133 } 89 }
134 90
135 TEST(PrefServiceTest, HasPrefPath) { 91 TEST(PrefServiceTest, HasPrefPath) {
136 TestingPrefService prefs; 92 TestingPrefService prefs;
137 93
138 const char path[] = "fake.path"; 94 const char path[] = "fake.path";
139 95
140 // Shouldn't initially have a path. 96 // Shouldn't initially have a path.
141 EXPECT_FALSE(prefs.HasPrefPath(path)); 97 EXPECT_FALSE(prefs.HasPrefPath(path));
142 98
143 // Register the path. This doesn't set a value, so the path still shouldn't 99 // Register the path. This doesn't set a value, so the path still shouldn't
144 // exist. 100 // exist.
145 prefs.RegisterStringPref(path, std::string()); 101 prefs.RegisterStringPref(path, std::string());
146 EXPECT_FALSE(prefs.HasPrefPath(path)); 102 EXPECT_FALSE(prefs.HasPrefPath(path));
147 103
148 // Set a value and make sure we have a path. 104 // Set a value and make sure we have a path.
149 prefs.SetString(path, "blah"); 105 prefs.SetString(path, "blah");
150 EXPECT_TRUE(prefs.HasPrefPath(path)); 106 EXPECT_TRUE(prefs.HasPrefPath(path));
151 } 107 }
152 108
153 TEST(PrefServiceTest, Observers) { 109 TEST(PrefServiceTest, Observers) {
154 const char pref_name[] = "homepage"; 110 const char pref_name[] = "homepage";
155 111
156 TestingPrefService prefs; 112 TestingPrefService prefs;
157 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); 113 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com"));
158 prefs.RegisterStringPref(pref_name, std::string()); 114 prefs.RegisterStringPref(pref_name, std::string());
159 115
160 const std::string new_pref_value("http://www.google.com/"); 116 const char new_pref_value[] = "http://www.google.com/";
161 TestPrefObserver obs(&prefs, pref_name, new_pref_value); 117 StringValue expected_new_pref_value(new_pref_value);
118 PrefObserverMock obs;
162 PrefChangeRegistrar registrar; 119 PrefChangeRegistrar registrar;
163 registrar.Init(&prefs); 120 registrar.Init(&prefs);
164 registrar.Add(pref_name, &obs); 121 registrar.Add(pref_name, &obs);
165 // This should fire the checks in TestPrefObserver::Observe. 122
123 // This should fire the checks in PrefObserverMock::Observe.
124 obs.Expect(&prefs, pref_name, &expected_new_pref_value);
166 prefs.SetString(pref_name, new_pref_value); 125 prefs.SetString(pref_name, new_pref_value);
167 126 Mock::VerifyAndClearExpectations(&obs);
168 // Make sure the tests were actually run.
169 EXPECT_TRUE(obs.observer_fired());
170 127
171 // Now try adding a second pref observer. 128 // Now try adding a second pref observer.
172 const std::string new_pref_value2("http://www.youtube.com/"); 129 const char new_pref_value2[] = "http://www.youtube.com/";
173 obs.Reset(new_pref_value2); 130 StringValue expected_new_pref_value2(new_pref_value2);
174 TestPrefObserver obs2(&prefs, pref_name, new_pref_value2); 131 PrefObserverMock obs2;
132 obs.Expect(&prefs, pref_name, &expected_new_pref_value2);
133 obs2.Expect(&prefs, pref_name, &expected_new_pref_value2);
175 registrar.Add(pref_name, &obs2); 134 registrar.Add(pref_name, &obs2);
176 // This should fire the checks in obs and obs2. 135 // This should fire the checks in obs and obs2.
177 prefs.SetString(pref_name, new_pref_value2); 136 prefs.SetString(pref_name, new_pref_value2);
178 EXPECT_TRUE(obs.observer_fired()); 137 Mock::VerifyAndClearExpectations(&obs);
179 EXPECT_TRUE(obs2.observer_fired()); 138 Mock::VerifyAndClearExpectations(&obs2);
180 139
181 // Make sure obs2 still works after removing obs. 140 // Make sure obs2 still works after removing obs.
182 registrar.Remove(pref_name, &obs); 141 registrar.Remove(pref_name, &obs);
183 obs.Reset(std::string()); 142 EXPECT_CALL(obs, Observe(_, _, _)).Times(0);
184 obs2.Reset(new_pref_value); 143 obs2.Expect(&prefs, pref_name, &expected_new_pref_value);
185 // This should only fire the observer in obs2. 144 // This should only fire the observer in obs2.
186 prefs.SetString(pref_name, new_pref_value); 145 prefs.SetString(pref_name, new_pref_value);
187 EXPECT_FALSE(obs.observer_fired()); 146 Mock::VerifyAndClearExpectations(&obs);
188 EXPECT_TRUE(obs2.observer_fired()); 147 Mock::VerifyAndClearExpectations(&obs2);
189 } 148 }
190 149
191 TEST(PrefServiceTest, ProxyFromCommandLineNotPolicy) { 150 TEST(PrefServiceTest, ProxyFromCommandLineNotPolicy) {
192 CommandLine command_line(CommandLine::NO_PROGRAM); 151 CommandLine command_line(CommandLine::NO_PROGRAM);
193 command_line.AppendSwitch(switches::kProxyAutoDetect); 152 command_line.AppendSwitch(switches::kProxyAutoDetect);
194 TestingPrefService prefs(NULL, NULL, &command_line); 153 TestingPrefService prefs(NULL, NULL, &command_line);
195 browser::RegisterUserPrefs(&prefs); 154 browser::RegisterUserPrefs(&prefs);
196 EXPECT_TRUE(prefs.GetBoolean(prefs::kProxyAutoDetect)); 155 EXPECT_TRUE(prefs.GetBoolean(prefs::kProxyAutoDetect));
197 const PrefService::Preference* pref = 156 const PrefService::Preference* pref =
198 prefs.FindPreference(prefs::kProxyAutoDetect); 157 prefs.FindPreference(prefs::kProxyAutoDetect);
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyPacUrl)); 293 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyPacUrl));
335 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyBypassList)); 294 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyBypassList));
336 } 295 }
337 296
338 class PrefServiceSetValueTest : public testing::Test { 297 class PrefServiceSetValueTest : public testing::Test {
339 protected: 298 protected:
340 static const char kName[]; 299 static const char kName[];
341 static const char kValue[]; 300 static const char kValue[];
342 301
343 PrefServiceSetValueTest() 302 PrefServiceSetValueTest()
344 : name_string_(kName), 303 : null_value_(Value::CreateNullValue()) {}
345 null_value_(Value::CreateNullValue()) {}
346
347 void SetExpectNoNotification() {
348 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
349 }
350
351 void SetExpectPrefChanged() {
352 EXPECT_CALL(observer_,
353 Observe(NotificationType(NotificationType::PREF_CHANGED), _,
354 Property(&Details<std::string>::ptr,
355 Pointee(name_string_))));
356 }
357 304
358 TestingPrefService prefs_; 305 TestingPrefService prefs_;
359 std::string name_string_;
360 scoped_ptr<Value> null_value_; 306 scoped_ptr<Value> null_value_;
361 NotificationObserverMock observer_; 307 PrefObserverMock observer_;
362 }; 308 };
363 309
364 const char PrefServiceSetValueTest::kName[] = "name"; 310 const char PrefServiceSetValueTest::kName[] = "name";
365 const char PrefServiceSetValueTest::kValue[] = "value"; 311 const char PrefServiceSetValueTest::kValue[] = "value";
366 312
367 TEST_F(PrefServiceSetValueTest, SetStringValue) { 313 TEST_F(PrefServiceSetValueTest, SetStringValue) {
368 const char default_string[] = "default"; 314 const char default_string[] = "default";
369 const scoped_ptr<Value> default_value( 315 StringValue default_value(default_string);
370 Value::CreateStringValue(default_string));
371 prefs_.RegisterStringPref(kName, default_string); 316 prefs_.RegisterStringPref(kName, default_string);
372 317
373 PrefChangeRegistrar registrar; 318 PrefChangeRegistrar registrar;
374 registrar.Init(&prefs_); 319 registrar.Init(&prefs_);
375 registrar.Add(kName, &observer_); 320 registrar.Add(kName, &observer_);
376 321
377 // Changing the controlling store from default to user triggers notification. 322 // Changing the controlling store from default to user triggers notification.
378 SetExpectPrefChanged(); 323 observer_.Expect(&prefs_, kName, &default_value);
379 prefs_.Set(kName, *default_value); 324 prefs_.Set(kName, default_value);
380 Mock::VerifyAndClearExpectations(&observer_); 325 Mock::VerifyAndClearExpectations(&observer_);
381 326
382 SetExpectNoNotification(); 327 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
383 prefs_.Set(kName, *default_value); 328 prefs_.Set(kName, default_value);
384 Mock::VerifyAndClearExpectations(&observer_); 329 Mock::VerifyAndClearExpectations(&observer_);
385 330
386 const scoped_ptr<Value> new_value(Value::CreateStringValue(kValue)); 331 StringValue new_value(kValue);
387 SetExpectPrefChanged(); 332 observer_.Expect(&prefs_, kName, &new_value);
388 prefs_.Set(kName, *new_value); 333 prefs_.Set(kName, new_value);
389 EXPECT_EQ(kValue, prefs_.GetString(kName)); 334 Mock::VerifyAndClearExpectations(&observer_);
390 } 335 }
391 336
392 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { 337 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
393 prefs_.RegisterDictionaryPref(kName); 338 prefs_.RegisterDictionaryPref(kName);
394 PrefChangeRegistrar registrar; 339 PrefChangeRegistrar registrar;
395 registrar.Init(&prefs_); 340 registrar.Init(&prefs_);
396 registrar.Add(kName, &observer_); 341 registrar.Add(kName, &observer_);
397 342
398 // Dictionary values are special: setting one to NULL is the same as clearing 343 // Dictionary values are special: setting one to NULL is the same as clearing
399 // the user value, allowing the NULL default to take (or keep) control. 344 // the user value, allowing the NULL default to take (or keep) control.
400 SetExpectNoNotification(); 345 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
401 prefs_.Set(kName, *null_value_); 346 prefs_.Set(kName, *null_value_);
402 Mock::VerifyAndClearExpectations(&observer_); 347 Mock::VerifyAndClearExpectations(&observer_);
403 348
404 DictionaryValue new_value; 349 DictionaryValue new_value;
405 new_value.SetString(kName, kValue); 350 new_value.SetString(kName, kValue);
406 SetExpectPrefChanged(); 351 observer_.Expect(&prefs_, kName, &new_value);
407 prefs_.Set(kName, new_value);
408 Mock::VerifyAndClearExpectations(&observer_);
409 DictionaryValue* dict = prefs_.GetMutableDictionary(kName);
410 EXPECT_EQ(1U, dict->size());
411 std::string out_value;
412 dict->GetString(kName, &out_value);
413 EXPECT_EQ(kValue, out_value);
414
415 SetExpectNoNotification();
416 prefs_.Set(kName, new_value); 352 prefs_.Set(kName, new_value);
417 Mock::VerifyAndClearExpectations(&observer_); 353 Mock::VerifyAndClearExpectations(&observer_);
418 354
419 SetExpectPrefChanged(); 355 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
356 prefs_.Set(kName, new_value);
357 Mock::VerifyAndClearExpectations(&observer_);
358
359 observer_.Expect(&prefs_, kName, null_value_.get());
420 prefs_.Set(kName, *null_value_); 360 prefs_.Set(kName, *null_value_);
421 Mock::VerifyAndClearExpectations(&observer_); 361 Mock::VerifyAndClearExpectations(&observer_);
422 dict = prefs_.GetMutableDictionary(kName);
423 EXPECT_EQ(0U, dict->size());
424 } 362 }
425 363
426 TEST_F(PrefServiceSetValueTest, SetListValue) { 364 TEST_F(PrefServiceSetValueTest, SetListValue) {
427 prefs_.RegisterListPref(kName); 365 prefs_.RegisterListPref(kName);
428 PrefChangeRegistrar registrar; 366 PrefChangeRegistrar registrar;
429 registrar.Init(&prefs_); 367 registrar.Init(&prefs_);
430 registrar.Add(kName, &observer_); 368 registrar.Add(kName, &observer_);
431 369
432 // List values are special: setting one to NULL is the same as clearing the 370 // List values are special: setting one to NULL is the same as clearing the
433 // user value, allowing the NULL default to take (or keep) control. 371 // user value, allowing the NULL default to take (or keep) control.
434 SetExpectNoNotification(); 372 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
435 prefs_.Set(kName, *null_value_); 373 prefs_.Set(kName, *null_value_);
436 Mock::VerifyAndClearExpectations(&observer_); 374 Mock::VerifyAndClearExpectations(&observer_);
437 375
438 ListValue new_value; 376 ListValue new_value;
439 new_value.Append(Value::CreateStringValue(kValue)); 377 new_value.Append(Value::CreateStringValue(kValue));
440 SetExpectPrefChanged(); 378 observer_.Expect(&prefs_, kName, &new_value);
441 prefs_.Set(kName, new_value);
442 Mock::VerifyAndClearExpectations(&observer_);
443 const ListValue* list = prefs_.GetMutableList(kName);
444 ASSERT_EQ(1U, list->GetSize());
445 std::string out_value;
446 list->GetString(0, &out_value);
447 EXPECT_EQ(kValue, out_value);
448
449 SetExpectNoNotification();
450 prefs_.Set(kName, new_value); 379 prefs_.Set(kName, new_value);
451 Mock::VerifyAndClearExpectations(&observer_); 380 Mock::VerifyAndClearExpectations(&observer_);
452 381
453 SetExpectPrefChanged(); 382 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
383 prefs_.Set(kName, new_value);
384 Mock::VerifyAndClearExpectations(&observer_);
385
386 observer_.Expect(&prefs_, kName, null_value_.get());
454 prefs_.Set(kName, *null_value_); 387 prefs_.Set(kName, *null_value_);
455 Mock::VerifyAndClearExpectations(&observer_); 388 Mock::VerifyAndClearExpectations(&observer_);
456 list = prefs_.GetMutableList(kName);
457 EXPECT_EQ(0U, list->GetSize());
458 } 389 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698