[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 5 | #include <string> |
| 6 | |
[email protected] | 03b9b4e | 2012-10-22 20:01:52 | [diff] [blame] | 7 | #include "base/prefs/json_pref_store.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 8 | #include "base/prefs/mock_pref_change_callback.h" |
| 9 | #include "base/prefs/pref_registry_simple.h" |
| 10 | #include "base/prefs/pref_value_store.h" |
[email protected] | 03b9b4e | 2012-10-22 20:01:52 | [diff] [blame] | 11 | #include "base/prefs/public/pref_change_registrar.h" |
| 12 | #include "base/prefs/testing_pref_store.h" |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 13 | #include "base/values.h" |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame^] | 14 | #include "chrome/test/base/testing_pref_service.h" |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 15 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 18 | using testing::_; |
| 19 | using testing::Mock; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 20 | |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame^] | 21 | const char kPrefName[] = "pref.name"; |
| 22 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 23 | TEST(PrefServiceTest, NoObserverFire) { |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 24 | TestingPrefServiceSimple prefs; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 25 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 26 | const char pref_name[] = "homepage"; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 27 | prefs.registry()->RegisterStringPref(pref_name, std::string()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 28 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 29 | const char new_pref_value[] = "http://www.google.com/"; |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 30 | MockPrefChangeCallback obs(&prefs); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 31 | PrefChangeRegistrar registrar; |
| 32 | registrar.Init(&prefs); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 33 | registrar.Add(pref_name, obs.GetCallback()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 34 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 35 | // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 36 | const StringValue expected_value(new_pref_value); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 37 | obs.Expect(pref_name, &expected_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 38 | prefs.SetString(pref_name, new_pref_value); |
| 39 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 40 | |
| 41 | // Setting the pref to the same value should not set the pref value a second |
| 42 | // time. |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 43 | EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 44 | prefs.SetString(pref_name, new_pref_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 45 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 46 | |
| 47 | // Clearing the pref should cause the pref to fire. |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 48 | const StringValue expected_default_value(""); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 49 | obs.Expect(pref_name, &expected_default_value); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 50 | prefs.ClearPref(pref_name); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 51 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 52 | |
| 53 | // Clearing the pref again should not cause the pref to fire. |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 54 | EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 55 | prefs.ClearPref(pref_name); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 56 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 59 | TEST(PrefServiceTest, HasPrefPath) { |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 60 | TestingPrefServiceSimple prefs; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 61 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 62 | const char path[] = "fake.path"; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 63 | |
| 64 | // Shouldn't initially have a path. |
| 65 | EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 66 | |
| 67 | // Register the path. This doesn't set a value, so the path still shouldn't |
| 68 | // exist. |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 69 | prefs.registry()->RegisterStringPref(path, std::string()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 70 | EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 71 | |
| 72 | // Set a value and make sure we have a path. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 73 | prefs.SetString(path, "blah"); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 74 | EXPECT_TRUE(prefs.HasPrefPath(path)); |
| 75 | } |
| 76 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 77 | TEST(PrefServiceTest, Observers) { |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 78 | const char pref_name[] = "homepage"; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 79 | |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 80 | TestingPrefServiceSimple prefs; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 81 | prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 82 | prefs.registry()->RegisterStringPref(pref_name, std::string()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 83 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 84 | const char new_pref_value[] = "http://www.google.com/"; |
| 85 | const StringValue expected_new_pref_value(new_pref_value); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 86 | MockPrefChangeCallback obs(&prefs); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 87 | PrefChangeRegistrar registrar; |
| 88 | registrar.Init(&prefs); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 89 | registrar.Add(pref_name, obs.GetCallback()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 90 | |
[email protected] | 54ffd94a | 2012-11-12 15:29:20 | [diff] [blame] | 91 | PrefChangeRegistrar registrar_two; |
| 92 | registrar_two.Init(&prefs); |
| 93 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 94 | // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. |
| 95 | obs.Expect(pref_name, &expected_new_pref_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 96 | prefs.SetString(pref_name, new_pref_value); |
| 97 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 98 | |
| 99 | // Now try adding a second pref observer. |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 100 | const char new_pref_value2[] = "http://www.youtube.com/"; |
| 101 | const StringValue expected_new_pref_value2(new_pref_value2); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 102 | MockPrefChangeCallback obs2(&prefs); |
| 103 | obs.Expect(pref_name, &expected_new_pref_value2); |
| 104 | obs2.Expect(pref_name, &expected_new_pref_value2); |
| 105 | registrar_two.Add(pref_name, obs2.GetCallback()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 106 | // This should fire the checks in obs and obs2. |
| 107 | prefs.SetString(pref_name, new_pref_value2); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 108 | Mock::VerifyAndClearExpectations(&obs); |
| 109 | Mock::VerifyAndClearExpectations(&obs2); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 110 | |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 111 | // Set a recommended value. |
| 112 | const StringValue recommended_pref_value("http://www.gmail.com/"); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 113 | obs.Expect(pref_name, &expected_new_pref_value2); |
| 114 | obs2.Expect(pref_name, &expected_new_pref_value2); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 115 | // This should fire the checks in obs and obs2 but with an unchanged value |
| 116 | // as the recommended value is being overridden by the user-set value. |
| 117 | prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy()); |
| 118 | Mock::VerifyAndClearExpectations(&obs); |
| 119 | Mock::VerifyAndClearExpectations(&obs2); |
| 120 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 121 | // Make sure obs2 still works after removing obs. |
[email protected] | 54ffd94a | 2012-11-12 15:29:20 | [diff] [blame] | 122 | registrar.Remove(pref_name); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 123 | EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
| 124 | obs2.Expect(pref_name, &expected_new_pref_value); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 125 | // This should only fire the observer in obs2. |
| 126 | prefs.SetString(pref_name, new_pref_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 127 | Mock::VerifyAndClearExpectations(&obs); |
| 128 | Mock::VerifyAndClearExpectations(&obs2); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 129 | } |
| 130 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 131 | // Make sure that if a preference changes type, so the wrong type is stored in |
| 132 | // the user pref file, it uses the correct fallback value instead. |
| 133 | TEST(PrefServiceTest, GetValueChangedType) { |
| 134 | const int kTestValue = 10; |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 135 | TestingPrefServiceSimple prefs; |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame^] | 136 | prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 137 | |
| 138 | // Check falling back to a recommended value. |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame^] | 139 | prefs.SetUserPref(kPrefName, Value::CreateStringValue("not an integer")); |
| 140 | const PrefService::Preference* pref = prefs.FindPreference(kPrefName); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 141 | ASSERT_TRUE(pref); |
| 142 | const Value* value = pref->GetValue(); |
| 143 | ASSERT_TRUE(value); |
| 144 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 145 | int actual_int_value = -1; |
| 146 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 147 | EXPECT_EQ(kTestValue, actual_int_value); |
| 148 | } |
| 149 | |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 150 | TEST(PrefServiceTest, GetValueAndGetRecommendedValue) { |
| 151 | const int kDefaultValue = 5; |
| 152 | const int kUserValue = 10; |
| 153 | const int kRecommendedValue = 15; |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 154 | TestingPrefServiceSimple prefs; |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame^] | 155 | prefs.registry()->RegisterIntegerPref(kPrefName, kDefaultValue); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 156 | |
| 157 | // Create pref with a default value only. |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame^] | 158 | const PrefService::Preference* pref = prefs.FindPreference(kPrefName); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 159 | ASSERT_TRUE(pref); |
| 160 | |
| 161 | // Check that GetValue() returns the default value. |
| 162 | const Value* value = pref->GetValue(); |
| 163 | ASSERT_TRUE(value); |
| 164 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 165 | int actual_int_value = -1; |
| 166 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 167 | EXPECT_EQ(kDefaultValue, actual_int_value); |
| 168 | |
| 169 | // Check that GetRecommendedValue() returns no value. |
| 170 | value = pref->GetRecommendedValue(); |
| 171 | ASSERT_FALSE(value); |
| 172 | |
| 173 | // Set a user-set value. |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame^] | 174 | prefs.SetUserPref(kPrefName, Value::CreateIntegerValue(kUserValue)); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 175 | |
| 176 | // Check that GetValue() returns the user-set value. |
| 177 | value = pref->GetValue(); |
| 178 | ASSERT_TRUE(value); |
| 179 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 180 | actual_int_value = -1; |
| 181 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 182 | EXPECT_EQ(kUserValue, actual_int_value); |
| 183 | |
| 184 | // Check that GetRecommendedValue() returns no value. |
| 185 | value = pref->GetRecommendedValue(); |
| 186 | ASSERT_FALSE(value); |
| 187 | |
| 188 | // Set a recommended value. |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame^] | 189 | prefs.SetRecommendedPref(kPrefName, |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 190 | Value::CreateIntegerValue(kRecommendedValue)); |
| 191 | |
| 192 | // Check that GetValue() returns the user-set value. |
| 193 | value = pref->GetValue(); |
| 194 | ASSERT_TRUE(value); |
| 195 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 196 | actual_int_value = -1; |
| 197 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 198 | EXPECT_EQ(kUserValue, actual_int_value); |
| 199 | |
| 200 | // Check that GetRecommendedValue() returns the recommended value. |
| 201 | value = pref->GetRecommendedValue(); |
| 202 | ASSERT_TRUE(value); |
| 203 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 204 | actual_int_value = -1; |
| 205 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 206 | EXPECT_EQ(kRecommendedValue, actual_int_value); |
| 207 | |
| 208 | // Remove the user-set value. |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame^] | 209 | prefs.RemoveUserPref(kPrefName); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 210 | |
| 211 | // Check that GetValue() returns the recommended value. |
| 212 | value = pref->GetValue(); |
| 213 | ASSERT_TRUE(value); |
| 214 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 215 | actual_int_value = -1; |
| 216 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 217 | EXPECT_EQ(kRecommendedValue, actual_int_value); |
| 218 | |
| 219 | // Check that GetRecommendedValue() returns the recommended value. |
| 220 | value = pref->GetRecommendedValue(); |
| 221 | ASSERT_TRUE(value); |
| 222 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 223 | actual_int_value = -1; |
| 224 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 225 | EXPECT_EQ(kRecommendedValue, actual_int_value); |
| 226 | } |
| 227 | |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 228 | class PrefServiceSetValueTest : public testing::Test { |
| 229 | protected: |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 230 | static const char kName[]; |
| 231 | static const char kValue[]; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 232 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 233 | PrefServiceSetValueTest() : observer_(&prefs_) {} |
| 234 | |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 235 | TestingPrefServiceSimple prefs_; |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 236 | MockPrefChangeCallback observer_; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 237 | }; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 238 | |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 239 | const char PrefServiceSetValueTest::kName[] = "name"; |
| 240 | const char PrefServiceSetValueTest::kValue[] = "value"; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 241 | |
| 242 | TEST_F(PrefServiceSetValueTest, SetStringValue) { |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 243 | const char default_string[] = "default"; |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 244 | const StringValue default_value(default_string); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 245 | prefs_.registry()->RegisterStringPref(kName, default_string); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 246 | |
| 247 | PrefChangeRegistrar registrar; |
| 248 | registrar.Init(&prefs_); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 249 | registrar.Add(kName, observer_.GetCallback()); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 250 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 251 | // Changing the controlling store from default to user triggers notification. |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 252 | observer_.Expect(kName, &default_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 253 | prefs_.Set(kName, default_value); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 254 | Mock::VerifyAndClearExpectations(&observer_); |
| 255 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 256 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 257 | prefs_.Set(kName, default_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 258 | Mock::VerifyAndClearExpectations(&observer_); |
| 259 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 260 | StringValue new_value(kValue); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 261 | observer_.Expect(kName, &new_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 262 | prefs_.Set(kName, new_value); |
| 263 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 267 | prefs_.registry()->RegisterDictionaryPref(kName); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 268 | PrefChangeRegistrar registrar; |
| 269 | registrar.Init(&prefs_); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 270 | registrar.Add(kName, observer_.GetCallback()); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 271 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 272 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 273 | prefs_.RemoveUserPref(kName); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 274 | Mock::VerifyAndClearExpectations(&observer_); |
| 275 | |
| 276 | DictionaryValue new_value; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 277 | new_value.SetString(kName, kValue); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 278 | observer_.Expect(kName, &new_value); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 279 | prefs_.Set(kName, new_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 280 | Mock::VerifyAndClearExpectations(&observer_); |
| 281 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 282 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 283 | prefs_.Set(kName, new_value); |
| 284 | Mock::VerifyAndClearExpectations(&observer_); |
| 285 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 286 | DictionaryValue empty; |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 287 | observer_.Expect(kName, &empty); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 288 | prefs_.Set(kName, empty); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 289 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | TEST_F(PrefServiceSetValueTest, SetListValue) { |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 293 | prefs_.registry()->RegisterListPref(kName); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 294 | PrefChangeRegistrar registrar; |
| 295 | registrar.Init(&prefs_); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 296 | registrar.Add(kName, observer_.GetCallback()); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 297 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 298 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 299 | prefs_.RemoveUserPref(kName); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 300 | Mock::VerifyAndClearExpectations(&observer_); |
| 301 | |
| 302 | ListValue new_value; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 303 | new_value.Append(Value::CreateStringValue(kValue)); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 304 | observer_.Expect(kName, &new_value); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 305 | prefs_.Set(kName, new_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 306 | Mock::VerifyAndClearExpectations(&observer_); |
| 307 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 308 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 309 | prefs_.Set(kName, new_value); |
| 310 | Mock::VerifyAndClearExpectations(&observer_); |
| 311 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 312 | ListValue empty; |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 313 | observer_.Expect(kName, &empty); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 314 | prefs_.Set(kName, empty); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 315 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 316 | } |