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

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

Issue 11741003: Remove PrefServiceSimple, replacing it with PrefService and PrefRegistrySimple. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 26 matching lines...) Expand all
37 37
38 using content::BrowserThread; 38 using content::BrowserThread;
39 using content::WebContentsTester; 39 using content::WebContentsTester;
40 using testing::_; 40 using testing::_;
41 using testing::Mock; 41 using testing::Mock;
42 42
43 TEST(PrefServiceTest, NoObserverFire) { 43 TEST(PrefServiceTest, NoObserverFire) {
44 TestingPrefServiceSimple prefs; 44 TestingPrefServiceSimple prefs;
45 45
46 const char pref_name[] = "homepage"; 46 const char pref_name[] = "homepage";
47 prefs.RegisterStringPref(pref_name, std::string()); 47 prefs.registrar()->RegisterStringPref(pref_name, std::string());
48 48
49 const char new_pref_value[] = "http://www.google.com/"; 49 const char new_pref_value[] = "http://www.google.com/";
50 MockPrefChangeCallback obs(&prefs); 50 MockPrefChangeCallback obs(&prefs);
51 PrefChangeRegistrar registrar; 51 PrefChangeRegistrar registrar;
52 registrar.Init(&prefs); 52 registrar.Init(&prefs);
53 registrar.Add(pref_name, obs.GetCallback()); 53 registrar.Add(pref_name, obs.GetCallback());
54 54
55 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. 55 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
56 const StringValue expected_value(new_pref_value); 56 const StringValue expected_value(new_pref_value);
57 obs.Expect(pref_name, &expected_value); 57 obs.Expect(pref_name, &expected_value);
(...skipping 21 matching lines...) Expand all
79 TEST(PrefServiceTest, HasPrefPath) { 79 TEST(PrefServiceTest, HasPrefPath) {
80 TestingPrefServiceSimple prefs; 80 TestingPrefServiceSimple prefs;
81 81
82 const char path[] = "fake.path"; 82 const char path[] = "fake.path";
83 83
84 // Shouldn't initially have a path. 84 // Shouldn't initially have a path.
85 EXPECT_FALSE(prefs.HasPrefPath(path)); 85 EXPECT_FALSE(prefs.HasPrefPath(path));
86 86
87 // Register the path. This doesn't set a value, so the path still shouldn't 87 // Register the path. This doesn't set a value, so the path still shouldn't
88 // exist. 88 // exist.
89 prefs.RegisterStringPref(path, std::string()); 89 prefs.registrar()->RegisterStringPref(path, std::string());
90 EXPECT_FALSE(prefs.HasPrefPath(path)); 90 EXPECT_FALSE(prefs.HasPrefPath(path));
91 91
92 // Set a value and make sure we have a path. 92 // Set a value and make sure we have a path.
93 prefs.SetString(path, "blah"); 93 prefs.SetString(path, "blah");
94 EXPECT_TRUE(prefs.HasPrefPath(path)); 94 EXPECT_TRUE(prefs.HasPrefPath(path));
95 } 95 }
96 96
97 TEST(PrefServiceTest, Observers) { 97 TEST(PrefServiceTest, Observers) {
98 const char pref_name[] = "homepage"; 98 const char pref_name[] = "homepage";
99 99
100 TestingPrefServiceSimple prefs; 100 TestingPrefServiceSimple prefs;
101 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); 101 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com"));
102 prefs.RegisterStringPref(pref_name, std::string()); 102 prefs.registrar()->RegisterStringPref(pref_name, std::string());
103 103
104 const char new_pref_value[] = "http://www.google.com/"; 104 const char new_pref_value[] = "http://www.google.com/";
105 const StringValue expected_new_pref_value(new_pref_value); 105 const StringValue expected_new_pref_value(new_pref_value);
106 MockPrefChangeCallback obs(&prefs); 106 MockPrefChangeCallback obs(&prefs);
107 PrefChangeRegistrar registrar; 107 PrefChangeRegistrar registrar;
108 registrar.Init(&prefs); 108 registrar.Init(&prefs);
109 registrar.Add(pref_name, obs.GetCallback()); 109 registrar.Add(pref_name, obs.GetCallback());
110 110
111 PrefChangeRegistrar registrar_two; 111 PrefChangeRegistrar registrar_two;
112 registrar_two.Init(&prefs); 112 registrar_two.Init(&prefs);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 prefs.SetString(pref_name, new_pref_value); 146 prefs.SetString(pref_name, new_pref_value);
147 Mock::VerifyAndClearExpectations(&obs); 147 Mock::VerifyAndClearExpectations(&obs);
148 Mock::VerifyAndClearExpectations(&obs2); 148 Mock::VerifyAndClearExpectations(&obs2);
149 } 149 }
150 150
151 // Make sure that if a preference changes type, so the wrong type is stored in 151 // Make sure that if a preference changes type, so the wrong type is stored in
152 // the user pref file, it uses the correct fallback value instead. 152 // the user pref file, it uses the correct fallback value instead.
153 TEST(PrefServiceTest, GetValueChangedType) { 153 TEST(PrefServiceTest, GetValueChangedType) {
154 const int kTestValue = 10; 154 const int kTestValue = 10;
155 TestingPrefServiceSimple prefs; 155 TestingPrefServiceSimple prefs;
156 prefs.RegisterIntegerPref(prefs::kStabilityLaunchCount, kTestValue); 156 prefs.registrar()->RegisterIntegerPref(
157 prefs::kStabilityLaunchCount, kTestValue);
157 158
158 // Check falling back to a recommended value. 159 // Check falling back to a recommended value.
159 prefs.SetUserPref(prefs::kStabilityLaunchCount, 160 prefs.SetUserPref(prefs::kStabilityLaunchCount,
160 Value::CreateStringValue("not an integer")); 161 Value::CreateStringValue("not an integer"));
161 const PrefService::Preference* pref = 162 const PrefService::Preference* pref =
162 prefs.FindPreference(prefs::kStabilityLaunchCount); 163 prefs.FindPreference(prefs::kStabilityLaunchCount);
163 ASSERT_TRUE(pref); 164 ASSERT_TRUE(pref);
164 const Value* value = pref->GetValue(); 165 const Value* value = pref->GetValue();
165 ASSERT_TRUE(value); 166 ASSERT_TRUE(value);
166 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); 167 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
167 int actual_int_value = -1; 168 int actual_int_value = -1;
168 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); 169 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
169 EXPECT_EQ(kTestValue, actual_int_value); 170 EXPECT_EQ(kTestValue, actual_int_value);
170 } 171 }
171 172
172 TEST(PrefServiceTest, UpdateCommandLinePrefStore) { 173 TEST(PrefServiceTest, UpdateCommandLinePrefStore) {
173 TestingPrefServiceSimple prefs; 174 TestingPrefServiceSimple prefs;
174 prefs.RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, false); 175 prefs.registrar()->RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, false);
175 176
176 // Check to make sure the value is as expected. 177 // Check to make sure the value is as expected.
177 const PrefService::Preference* pref = 178 const PrefService::Preference* pref =
178 prefs.FindPreference(prefs::kCloudPrintProxyEnabled); 179 prefs.FindPreference(prefs::kCloudPrintProxyEnabled);
179 ASSERT_TRUE(pref); 180 ASSERT_TRUE(pref);
180 const Value* value = pref->GetValue(); 181 const Value* value = pref->GetValue();
181 ASSERT_TRUE(value); 182 ASSERT_TRUE(value);
182 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); 183 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType());
183 bool actual_bool_value = true; 184 bool actual_bool_value = true;
184 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); 185 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value));
(...skipping 13 matching lines...) Expand all
198 actual_bool_value = false; 199 actual_bool_value = false;
199 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); 200 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value));
200 EXPECT_TRUE(actual_bool_value); 201 EXPECT_TRUE(actual_bool_value);
201 } 202 }
202 203
203 TEST(PrefServiceTest, GetValueAndGetRecommendedValue) { 204 TEST(PrefServiceTest, GetValueAndGetRecommendedValue) {
204 const int kDefaultValue = 5; 205 const int kDefaultValue = 5;
205 const int kUserValue = 10; 206 const int kUserValue = 10;
206 const int kRecommendedValue = 15; 207 const int kRecommendedValue = 15;
207 TestingPrefServiceSimple prefs; 208 TestingPrefServiceSimple prefs;
208 prefs.RegisterIntegerPref(prefs::kStabilityLaunchCount, kDefaultValue); 209 prefs.registrar()->RegisterIntegerPref(
210 prefs::kStabilityLaunchCount, kDefaultValue);
209 211
210 // Create pref with a default value only. 212 // Create pref with a default value only.
211 const PrefService::Preference* pref = 213 const PrefService::Preference* pref =
212 prefs.FindPreference(prefs::kStabilityLaunchCount); 214 prefs.FindPreference(prefs::kStabilityLaunchCount);
213 ASSERT_TRUE(pref); 215 ASSERT_TRUE(pref);
214 216
215 // Check that GetValue() returns the default value. 217 // Check that GetValue() returns the default value.
216 const Value* value = pref->GetValue(); 218 const Value* value = pref->GetValue();
217 ASSERT_TRUE(value); 219 ASSERT_TRUE(value);
218 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); 220 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 TestingPrefServiceSimple prefs_; 368 TestingPrefServiceSimple prefs_;
367 MockPrefChangeCallback observer_; 369 MockPrefChangeCallback observer_;
368 }; 370 };
369 371
370 const char PrefServiceSetValueTest::kName[] = "name"; 372 const char PrefServiceSetValueTest::kName[] = "name";
371 const char PrefServiceSetValueTest::kValue[] = "value"; 373 const char PrefServiceSetValueTest::kValue[] = "value";
372 374
373 TEST_F(PrefServiceSetValueTest, SetStringValue) { 375 TEST_F(PrefServiceSetValueTest, SetStringValue) {
374 const char default_string[] = "default"; 376 const char default_string[] = "default";
375 const StringValue default_value(default_string); 377 const StringValue default_value(default_string);
376 prefs_.RegisterStringPref(kName, default_string); 378 prefs_.registrar()->RegisterStringPref(kName, default_string);
377 379
378 PrefChangeRegistrar registrar; 380 PrefChangeRegistrar registrar;
379 registrar.Init(&prefs_); 381 registrar.Init(&prefs_);
380 registrar.Add(kName, observer_.GetCallback()); 382 registrar.Add(kName, observer_.GetCallback());
381 383
382 // Changing the controlling store from default to user triggers notification. 384 // Changing the controlling store from default to user triggers notification.
383 observer_.Expect(kName, &default_value); 385 observer_.Expect(kName, &default_value);
384 prefs_.Set(kName, default_value); 386 prefs_.Set(kName, default_value);
385 Mock::VerifyAndClearExpectations(&observer_); 387 Mock::VerifyAndClearExpectations(&observer_);
386 388
387 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); 389 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
388 prefs_.Set(kName, default_value); 390 prefs_.Set(kName, default_value);
389 Mock::VerifyAndClearExpectations(&observer_); 391 Mock::VerifyAndClearExpectations(&observer_);
390 392
391 StringValue new_value(kValue); 393 StringValue new_value(kValue);
392 observer_.Expect(kName, &new_value); 394 observer_.Expect(kName, &new_value);
393 prefs_.Set(kName, new_value); 395 prefs_.Set(kName, new_value);
394 Mock::VerifyAndClearExpectations(&observer_); 396 Mock::VerifyAndClearExpectations(&observer_);
395 } 397 }
396 398
397 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { 399 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
398 prefs_.RegisterDictionaryPref(kName); 400 prefs_.registrar()->RegisterDictionaryPref(kName);
399 PrefChangeRegistrar registrar; 401 PrefChangeRegistrar registrar;
400 registrar.Init(&prefs_); 402 registrar.Init(&prefs_);
401 registrar.Add(kName, observer_.GetCallback()); 403 registrar.Add(kName, observer_.GetCallback());
402 404
403 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); 405 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
404 prefs_.RemoveUserPref(kName); 406 prefs_.RemoveUserPref(kName);
405 Mock::VerifyAndClearExpectations(&observer_); 407 Mock::VerifyAndClearExpectations(&observer_);
406 408
407 DictionaryValue new_value; 409 DictionaryValue new_value;
408 new_value.SetString(kName, kValue); 410 new_value.SetString(kName, kValue);
409 observer_.Expect(kName, &new_value); 411 observer_.Expect(kName, &new_value);
410 prefs_.Set(kName, new_value); 412 prefs_.Set(kName, new_value);
411 Mock::VerifyAndClearExpectations(&observer_); 413 Mock::VerifyAndClearExpectations(&observer_);
412 414
413 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); 415 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
414 prefs_.Set(kName, new_value); 416 prefs_.Set(kName, new_value);
415 Mock::VerifyAndClearExpectations(&observer_); 417 Mock::VerifyAndClearExpectations(&observer_);
416 418
417 DictionaryValue empty; 419 DictionaryValue empty;
418 observer_.Expect(kName, &empty); 420 observer_.Expect(kName, &empty);
419 prefs_.Set(kName, empty); 421 prefs_.Set(kName, empty);
420 Mock::VerifyAndClearExpectations(&observer_); 422 Mock::VerifyAndClearExpectations(&observer_);
421 } 423 }
422 424
423 TEST_F(PrefServiceSetValueTest, SetListValue) { 425 TEST_F(PrefServiceSetValueTest, SetListValue) {
424 prefs_.RegisterListPref(kName); 426 prefs_.registrar()->RegisterListPref(kName);
425 PrefChangeRegistrar registrar; 427 PrefChangeRegistrar registrar;
426 registrar.Init(&prefs_); 428 registrar.Init(&prefs_);
427 registrar.Add(kName, observer_.GetCallback()); 429 registrar.Add(kName, observer_.GetCallback());
428 430
429 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); 431 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
430 prefs_.RemoveUserPref(kName); 432 prefs_.RemoveUserPref(kName);
431 Mock::VerifyAndClearExpectations(&observer_); 433 Mock::VerifyAndClearExpectations(&observer_);
432 434
433 ListValue new_value; 435 ListValue new_value;
434 new_value.Append(Value::CreateStringValue(kValue)); 436 new_value.Append(Value::CreateStringValue(kValue));
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 const char kDefaultFont[] = "Times"; 499 const char kDefaultFont[] = "Times";
498 #elif defined(OS_CHROMEOS) 500 #elif defined(OS_CHROMEOS)
499 const char kDefaultFont[] = "Tinos"; 501 const char kDefaultFont[] = "Tinos";
500 #else 502 #else
501 const char kDefaultFont[] = "Times New Roman"; 503 const char kDefaultFont[] = "Times New Roman";
502 #endif 504 #endif
503 EXPECT_EQ(ASCIIToUTF16(kDefaultFont), 505 EXPECT_EQ(ASCIIToUTF16(kDefaultFont),
504 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]); 506 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]);
505 EXPECT_TRUE(webkit_prefs.javascript_enabled); 507 EXPECT_TRUE(webkit_prefs.javascript_enabled);
506 } 508 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698