OLD | NEW |
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" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "chrome/test/testing_pref_service.h" | 23 #include "chrome/test/testing_pref_service.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 | 26 |
27 using testing::_; | 27 using testing::_; |
28 using testing::Mock; | 28 using testing::Mock; |
29 | 29 |
30 // TODO(port): port this test to POSIX. | 30 // TODO(port): port this test to POSIX. |
31 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
32 TEST(PrefServiceTest, LocalizedPrefs) { | 32 TEST(PrefServiceTest, LocalizedPrefs) { |
33 TestingPrefService prefs; | 33 scoped_ptr<TestingPrefService> prefs( |
| 34 TestingPrefService::CreateTestingPrefService()); |
34 const char kBoolean[] = "boolean"; | 35 const char kBoolean[] = "boolean"; |
35 const char kInteger[] = "integer"; | 36 const char kInteger[] = "integer"; |
36 const char kString[] = "string"; | 37 const char kString[] = "string"; |
37 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL); | 38 prefs->RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL); |
38 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT); | 39 prefs->RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT); |
39 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING); | 40 prefs->RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING); |
40 | 41 |
41 // The locale default should take preference over the user default. | 42 // The locale default should take preference over the user default. |
42 EXPECT_FALSE(prefs.GetBoolean(kBoolean)); | 43 EXPECT_FALSE(prefs->GetBoolean(kBoolean)); |
43 EXPECT_EQ(1, prefs.GetInteger(kInteger)); | 44 EXPECT_EQ(1, prefs->GetInteger(kInteger)); |
44 EXPECT_EQ("hello", prefs.GetString(kString)); | 45 EXPECT_EQ("hello", prefs->GetString(kString)); |
45 | 46 |
46 prefs.SetBoolean(kBoolean, true); | 47 prefs->SetBoolean(kBoolean, true); |
47 EXPECT_TRUE(prefs.GetBoolean(kBoolean)); | 48 EXPECT_TRUE(prefs->GetBoolean(kBoolean)); |
48 prefs.SetInteger(kInteger, 5); | 49 prefs->SetInteger(kInteger, 5); |
49 EXPECT_EQ(5, prefs.GetInteger(kInteger)); | 50 EXPECT_EQ(5, prefs->GetInteger(kInteger)); |
50 prefs.SetString(kString, "foo"); | 51 prefs->SetString(kString, "foo"); |
51 EXPECT_EQ("foo", prefs.GetString(kString)); | 52 EXPECT_EQ("foo", prefs->GetString(kString)); |
52 } | 53 } |
53 #endif | 54 #endif |
54 | 55 |
55 TEST(PrefServiceTest, NoObserverFire) { | 56 TEST(PrefServiceTest, NoObserverFire) { |
56 TestingPrefService prefs; | 57 scoped_ptr<TestingPrefService> prefs( |
| 58 TestingPrefService::CreateTestingPrefService()); |
57 | 59 |
58 const char pref_name[] = "homepage"; | 60 const char pref_name[] = "homepage"; |
59 prefs.RegisterStringPref(pref_name, std::string()); | 61 prefs->RegisterStringPref(pref_name, std::string()); |
60 | 62 |
61 const char new_pref_value[] = "http://www.google.com/"; | 63 const char new_pref_value[] = "http://www.google.com/"; |
62 PrefObserverMock obs; | 64 PrefObserverMock obs; |
63 PrefChangeRegistrar registrar; | 65 PrefChangeRegistrar registrar; |
64 registrar.Init(&prefs); | 66 registrar.Init(prefs.get()); |
65 registrar.Add(pref_name, &obs); | 67 registrar.Add(pref_name, &obs); |
66 | 68 |
67 // This should fire the checks in PrefObserverMock::Observe. | 69 // This should fire the checks in PrefObserverMock::Observe. |
68 const StringValue expected_value(new_pref_value); | 70 const StringValue expected_value(new_pref_value); |
69 obs.Expect(&prefs, pref_name, &expected_value); | 71 obs.Expect(prefs.get(), pref_name, &expected_value); |
70 prefs.SetString(pref_name, new_pref_value); | 72 prefs->SetString(pref_name, new_pref_value); |
71 Mock::VerifyAndClearExpectations(&obs); | 73 Mock::VerifyAndClearExpectations(&obs); |
72 | 74 |
73 // Setting the pref to the same value should not set the pref value a second | 75 // Setting the pref to the same value should not set the pref value a second |
74 // time. | 76 // time. |
75 EXPECT_CALL(obs, Observe(_, _, _)).Times(0); | 77 EXPECT_CALL(obs, Observe(_, _, _)).Times(0); |
76 prefs.SetString(pref_name, new_pref_value); | 78 prefs->SetString(pref_name, new_pref_value); |
77 Mock::VerifyAndClearExpectations(&obs); | 79 Mock::VerifyAndClearExpectations(&obs); |
78 | 80 |
79 // Clearing the pref should cause the pref to fire. | 81 // Clearing the pref should cause the pref to fire. |
80 const StringValue expected_default_value(""); | 82 const StringValue expected_default_value(""); |
81 obs.Expect(&prefs, pref_name, &expected_default_value); | 83 obs.Expect(prefs.get(), pref_name, &expected_default_value); |
82 prefs.ClearPref(pref_name); | 84 prefs->ClearPref(pref_name); |
83 Mock::VerifyAndClearExpectations(&obs); | 85 Mock::VerifyAndClearExpectations(&obs); |
84 | 86 |
85 // Clearing the pref again should not cause the pref to fire. | 87 // Clearing the pref again should not cause the pref to fire. |
86 EXPECT_CALL(obs, Observe(_, _, _)).Times(0); | 88 EXPECT_CALL(obs, Observe(_, _, _)).Times(0); |
87 prefs.ClearPref(pref_name); | 89 prefs->ClearPref(pref_name); |
88 Mock::VerifyAndClearExpectations(&obs); | 90 Mock::VerifyAndClearExpectations(&obs); |
89 } | 91 } |
90 | 92 |
91 TEST(PrefServiceTest, HasPrefPath) { | 93 TEST(PrefServiceTest, HasPrefPath) { |
92 TestingPrefService prefs; | 94 scoped_ptr<TestingPrefService> prefs( |
| 95 TestingPrefService::CreateTestingPrefService()); |
93 | 96 |
94 const char path[] = "fake.path"; | 97 const char path[] = "fake.path"; |
95 | 98 |
96 // Shouldn't initially have a path. | 99 // Shouldn't initially have a path. |
97 EXPECT_FALSE(prefs.HasPrefPath(path)); | 100 EXPECT_FALSE(prefs->HasPrefPath(path)); |
98 | 101 |
99 // Register the path. This doesn't set a value, so the path still shouldn't | 102 // Register the path. This doesn't set a value, so the path still shouldn't |
100 // exist. | 103 // exist. |
101 prefs.RegisterStringPref(path, std::string()); | 104 prefs->RegisterStringPref(path, std::string()); |
102 EXPECT_FALSE(prefs.HasPrefPath(path)); | 105 EXPECT_FALSE(prefs->HasPrefPath(path)); |
103 | 106 |
104 // Set a value and make sure we have a path. | 107 // Set a value and make sure we have a path. |
105 prefs.SetString(path, "blah"); | 108 prefs->SetString(path, "blah"); |
106 EXPECT_TRUE(prefs.HasPrefPath(path)); | 109 EXPECT_TRUE(prefs->HasPrefPath(path)); |
107 } | 110 } |
108 | 111 |
109 TEST(PrefServiceTest, Observers) { | 112 TEST(PrefServiceTest, Observers) { |
110 const char pref_name[] = "homepage"; | 113 const char pref_name[] = "homepage"; |
111 | 114 |
112 TestingPrefService prefs; | 115 scoped_ptr<TestingPrefService> prefs( |
113 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); | 116 TestingPrefService::CreateTestingPrefService()); |
114 prefs.RegisterStringPref(pref_name, std::string()); | 117 prefs->SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); |
| 118 prefs->RegisterStringPref(pref_name, std::string()); |
115 | 119 |
116 const char new_pref_value[] = "http://www.google.com/"; | 120 const char new_pref_value[] = "http://www.google.com/"; |
117 const StringValue expected_new_pref_value(new_pref_value); | 121 const StringValue expected_new_pref_value(new_pref_value); |
118 PrefObserverMock obs; | 122 PrefObserverMock obs; |
119 PrefChangeRegistrar registrar; | 123 PrefChangeRegistrar registrar; |
120 registrar.Init(&prefs); | 124 registrar.Init(prefs.get()); |
121 registrar.Add(pref_name, &obs); | 125 registrar.Add(pref_name, &obs); |
122 | 126 |
123 // This should fire the checks in PrefObserverMock::Observe. | 127 // This should fire the checks in PrefObserverMock::Observe. |
124 obs.Expect(&prefs, pref_name, &expected_new_pref_value); | 128 obs.Expect(prefs.get(), pref_name, &expected_new_pref_value); |
125 prefs.SetString(pref_name, new_pref_value); | 129 prefs->SetString(pref_name, new_pref_value); |
126 Mock::VerifyAndClearExpectations(&obs); | 130 Mock::VerifyAndClearExpectations(&obs); |
127 | 131 |
128 // Now try adding a second pref observer. | 132 // Now try adding a second pref observer. |
129 const char new_pref_value2[] = "http://www.youtube.com/"; | 133 const char new_pref_value2[] = "http://www.youtube.com/"; |
130 const StringValue expected_new_pref_value2(new_pref_value2); | 134 const StringValue expected_new_pref_value2(new_pref_value2); |
131 PrefObserverMock obs2; | 135 PrefObserverMock obs2; |
132 obs.Expect(&prefs, pref_name, &expected_new_pref_value2); | 136 obs.Expect(prefs.get(), pref_name, &expected_new_pref_value2); |
133 obs2.Expect(&prefs, pref_name, &expected_new_pref_value2); | 137 obs2.Expect(prefs.get(), pref_name, &expected_new_pref_value2); |
134 registrar.Add(pref_name, &obs2); | 138 registrar.Add(pref_name, &obs2); |
135 // This should fire the checks in obs and obs2. | 139 // This should fire the checks in obs and obs2. |
136 prefs.SetString(pref_name, new_pref_value2); | 140 prefs->SetString(pref_name, new_pref_value2); |
137 Mock::VerifyAndClearExpectations(&obs); | 141 Mock::VerifyAndClearExpectations(&obs); |
138 Mock::VerifyAndClearExpectations(&obs2); | 142 Mock::VerifyAndClearExpectations(&obs2); |
139 | 143 |
140 // Make sure obs2 still works after removing obs. | 144 // Make sure obs2 still works after removing obs. |
141 registrar.Remove(pref_name, &obs); | 145 registrar.Remove(pref_name, &obs); |
142 EXPECT_CALL(obs, Observe(_, _, _)).Times(0); | 146 EXPECT_CALL(obs, Observe(_, _, _)).Times(0); |
143 obs2.Expect(&prefs, pref_name, &expected_new_pref_value); | 147 obs2.Expect(prefs.get(), pref_name, &expected_new_pref_value); |
144 // This should only fire the observer in obs2. | 148 // This should only fire the observer in obs2. |
145 prefs.SetString(pref_name, new_pref_value); | 149 prefs->SetString(pref_name, new_pref_value); |
146 Mock::VerifyAndClearExpectations(&obs); | 150 Mock::VerifyAndClearExpectations(&obs); |
147 Mock::VerifyAndClearExpectations(&obs2); | 151 Mock::VerifyAndClearExpectations(&obs2); |
148 } | 152 } |
149 | 153 |
| 154 // Make sure that if a preference changes type, so the wrong type is stored in |
| 155 // the user pref file, it uses the correct fallback value instead. |
| 156 TEST(PrefServiceTest, GetValueChangedType) { |
| 157 const int kTestValue = 10; |
| 158 scoped_ptr<TestingPrefService> prefs( |
| 159 TestingPrefService::CreateTestingPrefService()); |
| 160 prefs->RegisterIntegerPref(prefs::kStabilityLaunchCount, kTestValue); |
| 161 |
| 162 // Check falling back to a recommended value. |
| 163 prefs->SetUserPref(prefs::kStabilityLaunchCount, |
| 164 Value::CreateStringValue("not an integer")); |
| 165 const PrefService::Preference* pref = |
| 166 prefs->FindPreference(prefs::kStabilityLaunchCount); |
| 167 ASSERT_TRUE(pref); |
| 168 const Value* value = pref->GetValue(); |
| 169 ASSERT_TRUE(value); |
| 170 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 171 int actual_int_value = -1; |
| 172 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 173 EXPECT_EQ(kTestValue, actual_int_value); |
| 174 } |
| 175 |
150 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineOptions) { | 176 TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineOptions) { |
151 CommandLine command_line(CommandLine::NO_PROGRAM); | 177 CommandLine command_line(CommandLine::NO_PROGRAM); |
152 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); | 178 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); |
153 command_line.AppendSwitchASCII(switches::kProxyPacUrl, "456"); | 179 command_line.AppendSwitchASCII(switches::kProxyPacUrl, "456"); |
154 command_line.AppendSwitchASCII(switches::kProxyServer, "789"); | 180 command_line.AppendSwitchASCII(switches::kProxyServer, "789"); |
155 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( | 181 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( |
156 new policy::MockConfigurationPolicyProvider()); | 182 new policy::MockConfigurationPolicyProvider()); |
157 Value* mode_value = Value::CreateIntegerValue( | 183 Value* mode_value = Value::CreateIntegerValue( |
158 policy::kPolicyManuallyConfiguredProxyMode); | 184 policy::kPolicyManuallyConfiguredProxyMode); |
159 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value); | 185 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyPacUrl)); | 323 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyPacUrl)); |
298 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyBypassList)); | 324 EXPECT_EQ(std::string(), prefs2->GetString(prefs::kProxyBypassList)); |
299 } | 325 } |
300 | 326 |
301 class PrefServiceSetValueTest : public testing::Test { | 327 class PrefServiceSetValueTest : public testing::Test { |
302 protected: | 328 protected: |
303 static const char kName[]; | 329 static const char kName[]; |
304 static const char kValue[]; | 330 static const char kValue[]; |
305 | 331 |
306 PrefServiceSetValueTest() | 332 PrefServiceSetValueTest() |
307 : null_value_(Value::CreateNullValue()) {} | 333 : prefs_(TestingPrefService::CreateTestingPrefService()) { |
| 334 } |
308 | 335 |
309 TestingPrefService prefs_; | 336 scoped_ptr<TestingPrefService> prefs_; |
310 scoped_ptr<Value> null_value_; | |
311 PrefObserverMock observer_; | 337 PrefObserverMock observer_; |
312 }; | 338 }; |
313 | 339 |
314 const char PrefServiceSetValueTest::kName[] = "name"; | 340 const char PrefServiceSetValueTest::kName[] = "name"; |
315 const char PrefServiceSetValueTest::kValue[] = "value"; | 341 const char PrefServiceSetValueTest::kValue[] = "value"; |
316 | 342 |
317 TEST_F(PrefServiceSetValueTest, SetStringValue) { | 343 TEST_F(PrefServiceSetValueTest, SetStringValue) { |
318 const char default_string[] = "default"; | 344 const char default_string[] = "default"; |
319 const StringValue default_value(default_string); | 345 const StringValue default_value(default_string); |
320 prefs_.RegisterStringPref(kName, default_string); | 346 prefs_->RegisterStringPref(kName, default_string); |
321 | 347 |
322 PrefChangeRegistrar registrar; | 348 PrefChangeRegistrar registrar; |
323 registrar.Init(&prefs_); | 349 registrar.Init(prefs_.get()); |
324 registrar.Add(kName, &observer_); | 350 registrar.Add(kName, &observer_); |
325 | 351 |
326 // Changing the controlling store from default to user triggers notification. | 352 // Changing the controlling store from default to user triggers notification. |
327 observer_.Expect(&prefs_, kName, &default_value); | 353 observer_.Expect(prefs_.get(), kName, &default_value); |
328 prefs_.Set(kName, default_value); | 354 prefs_->Set(kName, default_value); |
329 Mock::VerifyAndClearExpectations(&observer_); | 355 Mock::VerifyAndClearExpectations(&observer_); |
330 | 356 |
331 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); | 357 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
332 prefs_.Set(kName, default_value); | 358 prefs_->Set(kName, default_value); |
333 Mock::VerifyAndClearExpectations(&observer_); | 359 Mock::VerifyAndClearExpectations(&observer_); |
334 | 360 |
335 StringValue new_value(kValue); | 361 StringValue new_value(kValue); |
336 observer_.Expect(&prefs_, kName, &new_value); | 362 observer_.Expect(prefs_.get(), kName, &new_value); |
337 prefs_.Set(kName, new_value); | 363 prefs_->Set(kName, new_value); |
338 Mock::VerifyAndClearExpectations(&observer_); | 364 Mock::VerifyAndClearExpectations(&observer_); |
339 } | 365 } |
340 | 366 |
341 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { | 367 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { |
342 prefs_.RegisterDictionaryPref(kName); | 368 prefs_->RegisterDictionaryPref(kName); |
343 PrefChangeRegistrar registrar; | 369 PrefChangeRegistrar registrar; |
344 registrar.Init(&prefs_); | 370 registrar.Init(prefs_.get()); |
345 registrar.Add(kName, &observer_); | 371 registrar.Add(kName, &observer_); |
346 | 372 |
347 // Dictionary values are special: setting one to NULL is the same as clearing | |
348 // the user value, allowing the NULL default to take (or keep) control. | |
349 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); | 373 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
350 prefs_.Set(kName, *null_value_); | 374 prefs_->RemoveUserPref(kName); |
351 Mock::VerifyAndClearExpectations(&observer_); | 375 Mock::VerifyAndClearExpectations(&observer_); |
352 | 376 |
353 DictionaryValue new_value; | 377 DictionaryValue new_value; |
354 new_value.SetString(kName, kValue); | 378 new_value.SetString(kName, kValue); |
355 observer_.Expect(&prefs_, kName, &new_value); | 379 observer_.Expect(prefs_.get(), kName, &new_value); |
356 prefs_.Set(kName, new_value); | 380 prefs_->Set(kName, new_value); |
357 Mock::VerifyAndClearExpectations(&observer_); | 381 Mock::VerifyAndClearExpectations(&observer_); |
358 | 382 |
359 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); | 383 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
360 prefs_.Set(kName, new_value); | 384 prefs_->Set(kName, new_value); |
361 Mock::VerifyAndClearExpectations(&observer_); | 385 Mock::VerifyAndClearExpectations(&observer_); |
362 | 386 |
363 observer_.Expect(&prefs_, kName, null_value_.get()); | 387 DictionaryValue empty; |
364 prefs_.Set(kName, *null_value_); | 388 observer_.Expect(prefs_.get(), kName, &empty); |
| 389 prefs_->Set(kName, empty); |
365 Mock::VerifyAndClearExpectations(&observer_); | 390 Mock::VerifyAndClearExpectations(&observer_); |
366 } | 391 } |
367 | 392 |
368 TEST_F(PrefServiceSetValueTest, SetListValue) { | 393 TEST_F(PrefServiceSetValueTest, SetListValue) { |
369 prefs_.RegisterListPref(kName); | 394 prefs_->RegisterListPref(kName); |
370 PrefChangeRegistrar registrar; | 395 PrefChangeRegistrar registrar; |
371 registrar.Init(&prefs_); | 396 registrar.Init(prefs_.get()); |
372 registrar.Add(kName, &observer_); | 397 registrar.Add(kName, &observer_); |
373 | 398 |
374 // List values are special: setting one to NULL is the same as clearing the | |
375 // user value, allowing the NULL default to take (or keep) control. | |
376 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); | 399 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
377 prefs_.Set(kName, *null_value_); | 400 prefs_->RemoveUserPref(kName); |
378 Mock::VerifyAndClearExpectations(&observer_); | 401 Mock::VerifyAndClearExpectations(&observer_); |
379 | 402 |
380 ListValue new_value; | 403 ListValue new_value; |
381 new_value.Append(Value::CreateStringValue(kValue)); | 404 new_value.Append(Value::CreateStringValue(kValue)); |
382 observer_.Expect(&prefs_, kName, &new_value); | 405 observer_.Expect(prefs_.get(), kName, &new_value); |
383 prefs_.Set(kName, new_value); | 406 prefs_->Set(kName, new_value); |
384 Mock::VerifyAndClearExpectations(&observer_); | 407 Mock::VerifyAndClearExpectations(&observer_); |
385 | 408 |
386 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); | 409 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
387 prefs_.Set(kName, new_value); | 410 prefs_->Set(kName, new_value); |
388 Mock::VerifyAndClearExpectations(&observer_); | 411 Mock::VerifyAndClearExpectations(&observer_); |
389 | 412 |
390 observer_.Expect(&prefs_, kName, null_value_.get()); | 413 ListValue empty; |
391 prefs_.Set(kName, *null_value_); | 414 observer_.Expect(prefs_.get(), kName, &empty); |
| 415 prefs_->Set(kName, empty); |
392 Mock::VerifyAndClearExpectations(&observer_); | 416 Mock::VerifyAndClearExpectations(&observer_); |
393 } | 417 } |
OLD | NEW |