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

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

Powered by Google App Engine
This is Rietveld 408576698