| OLD | NEW |
| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 const char pref_name[] = "homepage"; | 31 const char pref_name[] = "homepage"; |
| 32 prefs.registry()->RegisterStringPref(pref_name, std::string()); | 32 prefs.registry()->RegisterStringPref(pref_name, std::string()); |
| 33 | 33 |
| 34 const char new_pref_value[] = "http://www.google.com/"; | 34 const char new_pref_value[] = "http://www.google.com/"; |
| 35 MockPrefChangeCallback obs(&prefs); | 35 MockPrefChangeCallback obs(&prefs); |
| 36 PrefChangeRegistrar registrar; | 36 PrefChangeRegistrar registrar; |
| 37 registrar.Init(&prefs); | 37 registrar.Init(&prefs); |
| 38 registrar.Add(pref_name, obs.GetCallback()); | 38 registrar.Add(pref_name, obs.GetCallback()); |
| 39 | 39 |
| 40 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. | 40 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. |
| 41 const base::StringValue expected_value(new_pref_value); | 41 const base::Value expected_value(new_pref_value); |
| 42 obs.Expect(pref_name, &expected_value); | 42 obs.Expect(pref_name, &expected_value); |
| 43 prefs.SetString(pref_name, new_pref_value); | 43 prefs.SetString(pref_name, new_pref_value); |
| 44 Mock::VerifyAndClearExpectations(&obs); | 44 Mock::VerifyAndClearExpectations(&obs); |
| 45 | 45 |
| 46 // Setting the pref to the same value should not set the pref value a second | 46 // Setting the pref to the same value should not set the pref value a second |
| 47 // time. | 47 // time. |
| 48 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); | 48 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
| 49 prefs.SetString(pref_name, new_pref_value); | 49 prefs.SetString(pref_name, new_pref_value); |
| 50 Mock::VerifyAndClearExpectations(&obs); | 50 Mock::VerifyAndClearExpectations(&obs); |
| 51 | 51 |
| 52 // Clearing the pref should cause the pref to fire. | 52 // Clearing the pref should cause the pref to fire. |
| 53 const base::StringValue expected_default_value((std::string())); | 53 const base::Value expected_default_value((std::string())); |
| 54 obs.Expect(pref_name, &expected_default_value); | 54 obs.Expect(pref_name, &expected_default_value); |
| 55 prefs.ClearPref(pref_name); | 55 prefs.ClearPref(pref_name); |
| 56 Mock::VerifyAndClearExpectations(&obs); | 56 Mock::VerifyAndClearExpectations(&obs); |
| 57 | 57 |
| 58 // Clearing the pref again should not cause the pref to fire. | 58 // Clearing the pref again should not cause the pref to fire. |
| 59 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); | 59 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
| 60 prefs.ClearPref(pref_name); | 60 prefs.ClearPref(pref_name); |
| 61 Mock::VerifyAndClearExpectations(&obs); | 61 Mock::VerifyAndClearExpectations(&obs); |
| 62 } | 62 } |
| 63 | 63 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 76 | 76 |
| 77 // Set a value and make sure we have a path. | 77 // Set a value and make sure we have a path. |
| 78 prefs.SetString(path, "blah"); | 78 prefs.SetString(path, "blah"); |
| 79 EXPECT_TRUE(prefs.HasPrefPath(path)); | 79 EXPECT_TRUE(prefs.HasPrefPath(path)); |
| 80 } | 80 } |
| 81 | 81 |
| 82 TEST(PrefServiceTest, Observers) { | 82 TEST(PrefServiceTest, Observers) { |
| 83 const char pref_name[] = "homepage"; | 83 const char pref_name[] = "homepage"; |
| 84 | 84 |
| 85 TestingPrefServiceSimple prefs; | 85 TestingPrefServiceSimple prefs; |
| 86 prefs.SetUserPref(pref_name, | 86 prefs.SetUserPref(pref_name, new base::Value("http://www.cnn.com")); |
| 87 new base::StringValue("http://www.cnn.com")); | |
| 88 prefs.registry()->RegisterStringPref(pref_name, std::string()); | 87 prefs.registry()->RegisterStringPref(pref_name, std::string()); |
| 89 | 88 |
| 90 const char new_pref_value[] = "http://www.google.com/"; | 89 const char new_pref_value[] = "http://www.google.com/"; |
| 91 const base::StringValue expected_new_pref_value(new_pref_value); | 90 const base::Value expected_new_pref_value(new_pref_value); |
| 92 MockPrefChangeCallback obs(&prefs); | 91 MockPrefChangeCallback obs(&prefs); |
| 93 PrefChangeRegistrar registrar; | 92 PrefChangeRegistrar registrar; |
| 94 registrar.Init(&prefs); | 93 registrar.Init(&prefs); |
| 95 registrar.Add(pref_name, obs.GetCallback()); | 94 registrar.Add(pref_name, obs.GetCallback()); |
| 96 | 95 |
| 97 PrefChangeRegistrar registrar_two; | 96 PrefChangeRegistrar registrar_two; |
| 98 registrar_two.Init(&prefs); | 97 registrar_two.Init(&prefs); |
| 99 | 98 |
| 100 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. | 99 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. |
| 101 obs.Expect(pref_name, &expected_new_pref_value); | 100 obs.Expect(pref_name, &expected_new_pref_value); |
| 102 prefs.SetString(pref_name, new_pref_value); | 101 prefs.SetString(pref_name, new_pref_value); |
| 103 Mock::VerifyAndClearExpectations(&obs); | 102 Mock::VerifyAndClearExpectations(&obs); |
| 104 | 103 |
| 105 // Now try adding a second pref observer. | 104 // Now try adding a second pref observer. |
| 106 const char new_pref_value2[] = "http://www.youtube.com/"; | 105 const char new_pref_value2[] = "http://www.youtube.com/"; |
| 107 const base::StringValue expected_new_pref_value2(new_pref_value2); | 106 const base::Value expected_new_pref_value2(new_pref_value2); |
| 108 MockPrefChangeCallback obs2(&prefs); | 107 MockPrefChangeCallback obs2(&prefs); |
| 109 obs.Expect(pref_name, &expected_new_pref_value2); | 108 obs.Expect(pref_name, &expected_new_pref_value2); |
| 110 obs2.Expect(pref_name, &expected_new_pref_value2); | 109 obs2.Expect(pref_name, &expected_new_pref_value2); |
| 111 registrar_two.Add(pref_name, obs2.GetCallback()); | 110 registrar_two.Add(pref_name, obs2.GetCallback()); |
| 112 // This should fire the checks in obs and obs2. | 111 // This should fire the checks in obs and obs2. |
| 113 prefs.SetString(pref_name, new_pref_value2); | 112 prefs.SetString(pref_name, new_pref_value2); |
| 114 Mock::VerifyAndClearExpectations(&obs); | 113 Mock::VerifyAndClearExpectations(&obs); |
| 115 Mock::VerifyAndClearExpectations(&obs2); | 114 Mock::VerifyAndClearExpectations(&obs2); |
| 116 | 115 |
| 117 // Set a recommended value. | 116 // Set a recommended value. |
| 118 const base::StringValue recommended_pref_value("http://www.gmail.com/"); | 117 const base::Value recommended_pref_value("http://www.gmail.com/"); |
| 119 obs.Expect(pref_name, &expected_new_pref_value2); | 118 obs.Expect(pref_name, &expected_new_pref_value2); |
| 120 obs2.Expect(pref_name, &expected_new_pref_value2); | 119 obs2.Expect(pref_name, &expected_new_pref_value2); |
| 121 // This should fire the checks in obs and obs2 but with an unchanged value | 120 // This should fire the checks in obs and obs2 but with an unchanged value |
| 122 // as the recommended value is being overridden by the user-set value. | 121 // as the recommended value is being overridden by the user-set value. |
| 123 prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy()); | 122 prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy()); |
| 124 Mock::VerifyAndClearExpectations(&obs); | 123 Mock::VerifyAndClearExpectations(&obs); |
| 125 Mock::VerifyAndClearExpectations(&obs2); | 124 Mock::VerifyAndClearExpectations(&obs2); |
| 126 | 125 |
| 127 // Make sure obs2 still works after removing obs. | 126 // Make sure obs2 still works after removing obs. |
| 128 registrar.Remove(pref_name); | 127 registrar.Remove(pref_name); |
| 129 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); | 128 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
| 130 obs2.Expect(pref_name, &expected_new_pref_value); | 129 obs2.Expect(pref_name, &expected_new_pref_value); |
| 131 // This should only fire the observer in obs2. | 130 // This should only fire the observer in obs2. |
| 132 prefs.SetString(pref_name, new_pref_value); | 131 prefs.SetString(pref_name, new_pref_value); |
| 133 Mock::VerifyAndClearExpectations(&obs); | 132 Mock::VerifyAndClearExpectations(&obs); |
| 134 Mock::VerifyAndClearExpectations(&obs2); | 133 Mock::VerifyAndClearExpectations(&obs2); |
| 135 } | 134 } |
| 136 | 135 |
| 137 // Make sure that if a preference changes type, so the wrong type is stored in | 136 // Make sure that if a preference changes type, so the wrong type is stored in |
| 138 // the user pref file, it uses the correct fallback value instead. | 137 // the user pref file, it uses the correct fallback value instead. |
| 139 TEST(PrefServiceTest, GetValueChangedType) { | 138 TEST(PrefServiceTest, GetValueChangedType) { |
| 140 const int kTestValue = 10; | 139 const int kTestValue = 10; |
| 141 TestingPrefServiceSimple prefs; | 140 TestingPrefServiceSimple prefs; |
| 142 prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue); | 141 prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue); |
| 143 | 142 |
| 144 // Check falling back to a recommended value. | 143 // Check falling back to a recommended value. |
| 145 prefs.SetUserPref(kPrefName, | 144 prefs.SetUserPref(kPrefName, new base::Value("not an integer")); |
| 146 new base::StringValue("not an integer")); | |
| 147 const PrefService::Preference* pref = prefs.FindPreference(kPrefName); | 145 const PrefService::Preference* pref = prefs.FindPreference(kPrefName); |
| 148 ASSERT_TRUE(pref); | 146 ASSERT_TRUE(pref); |
| 149 const base::Value* value = pref->GetValue(); | 147 const base::Value* value = pref->GetValue(); |
| 150 ASSERT_TRUE(value); | 148 ASSERT_TRUE(value); |
| 151 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); | 149 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); |
| 152 int actual_int_value = -1; | 150 int actual_int_value = -1; |
| 153 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 151 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 154 EXPECT_EQ(kTestValue, actual_int_value); | 152 EXPECT_EQ(kTestValue, actual_int_value); |
| 155 } | 153 } |
| 156 | 154 |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 | 345 |
| 348 TestingPrefServiceSimple prefs_; | 346 TestingPrefServiceSimple prefs_; |
| 349 MockPrefChangeCallback observer_; | 347 MockPrefChangeCallback observer_; |
| 350 }; | 348 }; |
| 351 | 349 |
| 352 const char PrefServiceSetValueTest::kName[] = "name"; | 350 const char PrefServiceSetValueTest::kName[] = "name"; |
| 353 const char PrefServiceSetValueTest::kValue[] = "value"; | 351 const char PrefServiceSetValueTest::kValue[] = "value"; |
| 354 | 352 |
| 355 TEST_F(PrefServiceSetValueTest, SetStringValue) { | 353 TEST_F(PrefServiceSetValueTest, SetStringValue) { |
| 356 const char default_string[] = "default"; | 354 const char default_string[] = "default"; |
| 357 const base::StringValue default_value(default_string); | 355 const base::Value default_value(default_string); |
| 358 prefs_.registry()->RegisterStringPref(kName, default_string); | 356 prefs_.registry()->RegisterStringPref(kName, default_string); |
| 359 | 357 |
| 360 PrefChangeRegistrar registrar; | 358 PrefChangeRegistrar registrar; |
| 361 registrar.Init(&prefs_); | 359 registrar.Init(&prefs_); |
| 362 registrar.Add(kName, observer_.GetCallback()); | 360 registrar.Add(kName, observer_.GetCallback()); |
| 363 | 361 |
| 364 // Changing the controlling store from default to user triggers notification. | 362 // Changing the controlling store from default to user triggers notification. |
| 365 observer_.Expect(kName, &default_value); | 363 observer_.Expect(kName, &default_value); |
| 366 prefs_.Set(kName, default_value); | 364 prefs_.Set(kName, default_value); |
| 367 Mock::VerifyAndClearExpectations(&observer_); | 365 Mock::VerifyAndClearExpectations(&observer_); |
| 368 | 366 |
| 369 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); | 367 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
| 370 prefs_.Set(kName, default_value); | 368 prefs_.Set(kName, default_value); |
| 371 Mock::VerifyAndClearExpectations(&observer_); | 369 Mock::VerifyAndClearExpectations(&observer_); |
| 372 | 370 |
| 373 base::StringValue new_value(kValue); | 371 base::Value new_value(kValue); |
| 374 observer_.Expect(kName, &new_value); | 372 observer_.Expect(kName, &new_value); |
| 375 prefs_.Set(kName, new_value); | 373 prefs_.Set(kName, new_value); |
| 376 Mock::VerifyAndClearExpectations(&observer_); | 374 Mock::VerifyAndClearExpectations(&observer_); |
| 377 } | 375 } |
| 378 | 376 |
| 379 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { | 377 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { |
| 380 prefs_.registry()->RegisterDictionaryPref(kName); | 378 prefs_.registry()->RegisterDictionaryPref(kName); |
| 381 PrefChangeRegistrar registrar; | 379 PrefChangeRegistrar registrar; |
| 382 registrar.Init(&prefs_); | 380 registrar.Init(&prefs_); |
| 383 registrar.Add(kName, observer_.GetCallback()); | 381 registrar.Add(kName, observer_.GetCallback()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 | 418 |
| 421 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); | 419 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
| 422 prefs_.Set(kName, new_value); | 420 prefs_.Set(kName, new_value); |
| 423 Mock::VerifyAndClearExpectations(&observer_); | 421 Mock::VerifyAndClearExpectations(&observer_); |
| 424 | 422 |
| 425 base::ListValue empty; | 423 base::ListValue empty; |
| 426 observer_.Expect(kName, &empty); | 424 observer_.Expect(kName, &empty); |
| 427 prefs_.Set(kName, empty); | 425 prefs_.Set(kName, empty); |
| 428 Mock::VerifyAndClearExpectations(&observer_); | 426 Mock::VerifyAndClearExpectations(&observer_); |
| 429 } | 427 } |
| OLD | NEW |