[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 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 8 | #include <string> |
| 9 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 10 | #include "base/macros.h" |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 11 | #include "base/values.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 12 | #include "components/prefs/json_pref_store.h" |
| 13 | #include "components/prefs/mock_pref_change_callback.h" |
| 14 | #include "components/prefs/pref_change_registrar.h" |
| 15 | #include "components/prefs/pref_registry_simple.h" |
| 16 | #include "components/prefs/pref_service_factory.h" |
| 17 | #include "components/prefs/pref_value_store.h" |
| 18 | #include "components/prefs/testing_pref_service.h" |
| 19 | #include "components/prefs/testing_pref_store.h" |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 20 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 23 | using testing::_; |
| 24 | using testing::Mock; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 25 | |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame] | 26 | const char kPrefName[] = "pref.name"; |
| 27 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 28 | TEST(PrefServiceTest, NoObserverFire) { |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 29 | TestingPrefServiceSimple prefs; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 30 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 31 | const char pref_name[] = "homepage"; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 32 | prefs.registry()->RegisterStringPref(pref_name, std::string()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 33 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 34 | const char new_pref_value[] = "http://www.google.com/"; |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 35 | MockPrefChangeCallback obs(&prefs); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 36 | PrefChangeRegistrar registrar; |
| 37 | registrar.Init(&prefs); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 38 | registrar.Add(pref_name, obs.GetCallback()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 39 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 40 | // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame^] | 41 | const base::Value expected_value(new_pref_value); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 42 | obs.Expect(pref_name, &expected_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 43 | prefs.SetString(pref_name, new_pref_value); |
| 44 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 45 | |
| 46 | // Setting the pref to the same value should not set the pref value a second |
| 47 | // time. |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 48 | EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 49 | prefs.SetString(pref_name, new_pref_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 50 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 51 | |
| 52 | // Clearing the pref should cause the pref to fire. |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame^] | 53 | const base::Value expected_default_value((std::string())); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 54 | obs.Expect(pref_name, &expected_default_value); |
[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 | // Clearing the pref again should not cause the pref to fire. |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 59 | EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 60 | prefs.ClearPref(pref_name); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 61 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 64 | TEST(PrefServiceTest, HasPrefPath) { |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 65 | TestingPrefServiceSimple prefs; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 66 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 67 | const char path[] = "fake.path"; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 68 | |
| 69 | // Shouldn't initially have a path. |
| 70 | EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 71 | |
| 72 | // Register the path. This doesn't set a value, so the path still shouldn't |
| 73 | // exist. |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 74 | prefs.registry()->RegisterStringPref(path, std::string()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 75 | EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 76 | |
| 77 | // Set a value and make sure we have a path. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 78 | prefs.SetString(path, "blah"); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 79 | EXPECT_TRUE(prefs.HasPrefPath(path)); |
| 80 | } |
| 81 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 82 | TEST(PrefServiceTest, Observers) { |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 83 | const char pref_name[] = "homepage"; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 84 | |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 85 | TestingPrefServiceSimple prefs; |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame^] | 86 | prefs.SetUserPref(pref_name, new base::Value("http://www.cnn.com")); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 87 | prefs.registry()->RegisterStringPref(pref_name, std::string()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 88 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 89 | const char new_pref_value[] = "http://www.google.com/"; |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame^] | 90 | const base::Value expected_new_pref_value(new_pref_value); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 91 | MockPrefChangeCallback obs(&prefs); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 92 | PrefChangeRegistrar registrar; |
| 93 | registrar.Init(&prefs); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 94 | registrar.Add(pref_name, obs.GetCallback()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 95 | |
[email protected] | 54ffd94a | 2012-11-12 15:29:20 | [diff] [blame] | 96 | PrefChangeRegistrar registrar_two; |
| 97 | registrar_two.Init(&prefs); |
| 98 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 99 | // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. |
| 100 | obs.Expect(pref_name, &expected_new_pref_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 101 | prefs.SetString(pref_name, new_pref_value); |
| 102 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 103 | |
| 104 | // Now try adding a second pref observer. |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 105 | const char new_pref_value2[] = "http://www.youtube.com/"; |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame^] | 106 | const base::Value expected_new_pref_value2(new_pref_value2); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 107 | MockPrefChangeCallback obs2(&prefs); |
| 108 | obs.Expect(pref_name, &expected_new_pref_value2); |
| 109 | obs2.Expect(pref_name, &expected_new_pref_value2); |
| 110 | registrar_two.Add(pref_name, obs2.GetCallback()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 111 | // This should fire the checks in obs and obs2. |
| 112 | prefs.SetString(pref_name, new_pref_value2); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 113 | Mock::VerifyAndClearExpectations(&obs); |
| 114 | Mock::VerifyAndClearExpectations(&obs2); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 115 | |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 116 | // Set a recommended value. |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame^] | 117 | const base::Value recommended_pref_value("http://www.gmail.com/"); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 118 | obs.Expect(pref_name, &expected_new_pref_value2); |
| 119 | obs2.Expect(pref_name, &expected_new_pref_value2); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 120 | // This should fire the checks in obs and obs2 but with an unchanged value |
| 121 | // as the recommended value is being overridden by the user-set value. |
| 122 | prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy()); |
| 123 | Mock::VerifyAndClearExpectations(&obs); |
| 124 | Mock::VerifyAndClearExpectations(&obs2); |
| 125 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 126 | // Make sure obs2 still works after removing obs. |
[email protected] | 54ffd94a | 2012-11-12 15:29:20 | [diff] [blame] | 127 | registrar.Remove(pref_name); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 128 | EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
| 129 | obs2.Expect(pref_name, &expected_new_pref_value); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 130 | // This should only fire the observer in obs2. |
| 131 | prefs.SetString(pref_name, new_pref_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 132 | Mock::VerifyAndClearExpectations(&obs); |
| 133 | Mock::VerifyAndClearExpectations(&obs2); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 134 | } |
| 135 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 136 | // Make sure that if a preference changes type, so the wrong type is stored in |
| 137 | // the user pref file, it uses the correct fallback value instead. |
| 138 | TEST(PrefServiceTest, GetValueChangedType) { |
| 139 | const int kTestValue = 10; |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 140 | TestingPrefServiceSimple prefs; |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame] | 141 | prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 142 | |
| 143 | // Check falling back to a recommended value. |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame^] | 144 | prefs.SetUserPref(kPrefName, new base::Value("not an integer")); |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame] | 145 | const PrefService::Preference* pref = prefs.FindPreference(kPrefName); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 146 | ASSERT_TRUE(pref); |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 147 | const base::Value* value = pref->GetValue(); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 148 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 149 | EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 150 | int actual_int_value = -1; |
| 151 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 152 | EXPECT_EQ(kTestValue, actual_int_value); |
| 153 | } |
| 154 | |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 155 | TEST(PrefServiceTest, GetValueAndGetRecommendedValue) { |
| 156 | const int kDefaultValue = 5; |
| 157 | const int kUserValue = 10; |
| 158 | const int kRecommendedValue = 15; |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 159 | TestingPrefServiceSimple prefs; |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame] | 160 | prefs.registry()->RegisterIntegerPref(kPrefName, kDefaultValue); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 161 | |
| 162 | // Create pref with a default value only. |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame] | 163 | const PrefService::Preference* pref = prefs.FindPreference(kPrefName); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 164 | ASSERT_TRUE(pref); |
| 165 | |
| 166 | // Check that GetValue() returns the default value. |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 167 | const base::Value* value = pref->GetValue(); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 168 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 169 | EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 170 | int actual_int_value = -1; |
| 171 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 172 | EXPECT_EQ(kDefaultValue, actual_int_value); |
| 173 | |
| 174 | // Check that GetRecommendedValue() returns no value. |
| 175 | value = pref->GetRecommendedValue(); |
| 176 | ASSERT_FALSE(value); |
| 177 | |
| 178 | // Set a user-set value. |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 179 | prefs.SetUserPref(kPrefName, new base::Value(kUserValue)); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 180 | |
| 181 | // Check that GetValue() returns the user-set value. |
| 182 | value = pref->GetValue(); |
| 183 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 184 | EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 185 | actual_int_value = -1; |
| 186 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 187 | EXPECT_EQ(kUserValue, actual_int_value); |
| 188 | |
| 189 | // Check that GetRecommendedValue() returns no value. |
| 190 | value = pref->GetRecommendedValue(); |
| 191 | ASSERT_FALSE(value); |
| 192 | |
| 193 | // Set a recommended value. |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 194 | prefs.SetRecommendedPref(kPrefName, new base::Value(kRecommendedValue)); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 195 | |
| 196 | // Check that GetValue() returns the user-set value. |
| 197 | value = pref->GetValue(); |
| 198 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 199 | EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 200 | actual_int_value = -1; |
| 201 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 202 | EXPECT_EQ(kUserValue, actual_int_value); |
| 203 | |
| 204 | // Check that GetRecommendedValue() returns the recommended value. |
| 205 | value = pref->GetRecommendedValue(); |
| 206 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 207 | EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 208 | actual_int_value = -1; |
| 209 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 210 | EXPECT_EQ(kRecommendedValue, actual_int_value); |
| 211 | |
| 212 | // Remove the user-set value. |
[email protected] | aaa55231 | 2013-02-13 16:25:40 | [diff] [blame] | 213 | prefs.RemoveUserPref(kPrefName); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 214 | |
| 215 | // Check that GetValue() returns the recommended value. |
| 216 | value = pref->GetValue(); |
| 217 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 218 | EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 219 | actual_int_value = -1; |
| 220 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 221 | EXPECT_EQ(kRecommendedValue, actual_int_value); |
| 222 | |
| 223 | // Check that GetRecommendedValue() returns the recommended value. |
| 224 | value = pref->GetRecommendedValue(); |
| 225 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 226 | EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 227 | actual_int_value = -1; |
| 228 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 229 | EXPECT_EQ(kRecommendedValue, actual_int_value); |
| 230 | } |
| 231 | |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 232 | // A PrefStore which just stores the last write flags that were used to write |
| 233 | // values to it. |
| 234 | class WriteFlagChecker : public TestingPrefStore { |
| 235 | public: |
| 236 | WriteFlagChecker() {} |
| 237 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 238 | void ReportValueChanged(const std::string& key, uint32_t flags) override { |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 239 | SetLastWriteFlags(flags); |
| 240 | } |
| 241 | |
| 242 | void SetValue(const std::string& key, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 243 | std::unique_ptr<base::Value> value, |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 244 | uint32_t flags) override { |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 245 | SetLastWriteFlags(flags); |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void SetValueSilently(const std::string& key, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 249 | std::unique_ptr<base::Value> value, |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 250 | uint32_t flags) override { |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 251 | SetLastWriteFlags(flags); |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 252 | } |
| 253 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 254 | void RemoveValue(const std::string& key, uint32_t flags) override { |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 255 | SetLastWriteFlags(flags); |
| 256 | } |
| 257 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 258 | uint32_t GetLastFlagsAndClear() { |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 259 | CHECK(last_write_flags_set_); |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 260 | uint32_t result = last_write_flags_; |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 261 | last_write_flags_set_ = false; |
| 262 | last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS; |
| 263 | return result; |
| 264 | } |
| 265 | |
| 266 | bool last_write_flags_set() { return last_write_flags_set_; } |
| 267 | |
| 268 | private: |
| 269 | ~WriteFlagChecker() override {} |
| 270 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 271 | void SetLastWriteFlags(uint32_t flags) { |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 272 | CHECK(!last_write_flags_set_); |
| 273 | last_write_flags_set_ = true; |
| 274 | last_write_flags_ = flags; |
| 275 | } |
| 276 | |
| 277 | bool last_write_flags_set_ = false; |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 278 | uint32_t last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS; |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | TEST(PrefServiceTest, WriteablePrefStoreFlags) { |
| 282 | scoped_refptr<WriteFlagChecker> flag_checker(new WriteFlagChecker); |
| 283 | scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple); |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 284 | PrefServiceFactory factory; |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 285 | factory.set_user_prefs(flag_checker); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 286 | std::unique_ptr<PrefService> prefs(factory.Create(registry.get())); |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 287 | |
| 288 | // The first 8 bits of write flags are reserved for subclasses. Create a |
| 289 | // custom flag in this range |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 290 | uint32_t kCustomRegistrationFlag = 1 << 2; |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 291 | |
| 292 | // A map of the registration flags that will be tested and the write flags |
| 293 | // they are expected to convert to. |
| 294 | struct RegistrationToWriteFlags { |
| 295 | const char* pref_name; |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 296 | uint32_t registration_flags; |
| 297 | uint32_t write_flags; |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 298 | }; |
| 299 | const RegistrationToWriteFlags kRegistrationToWriteFlags[] = { |
| 300 | {"none", |
| 301 | PrefRegistry::NO_REGISTRATION_FLAGS, |
| 302 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS}, |
| 303 | {"lossy", |
| 304 | PrefRegistry::LOSSY_PREF, |
| 305 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}, |
| 306 | {"custom", |
| 307 | kCustomRegistrationFlag, |
| 308 | WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS}, |
| 309 | {"lossyandcustom", |
| 310 | PrefRegistry::LOSSY_PREF | kCustomRegistrationFlag, |
| 311 | WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}}; |
| 312 | |
| 313 | for (size_t i = 0; i < arraysize(kRegistrationToWriteFlags); ++i) { |
| 314 | RegistrationToWriteFlags entry = kRegistrationToWriteFlags[i]; |
| 315 | registry->RegisterDictionaryPref( |
| 316 | entry.pref_name, new base::DictionaryValue(), entry.registration_flags); |
| 317 | |
| 318 | SCOPED_TRACE("Currently testing pref with name: " + |
| 319 | std::string(entry.pref_name)); |
| 320 | |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 321 | prefs->GetMutableUserPref(entry.pref_name, base::Value::Type::DICTIONARY); |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 322 | EXPECT_TRUE(flag_checker->last_write_flags_set()); |
| 323 | EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear()); |
| 324 | |
| 325 | prefs->ReportUserPrefChanged(entry.pref_name); |
| 326 | EXPECT_TRUE(flag_checker->last_write_flags_set()); |
| 327 | EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear()); |
| 328 | |
| 329 | prefs->ClearPref(entry.pref_name); |
| 330 | EXPECT_TRUE(flag_checker->last_write_flags_set()); |
| 331 | EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear()); |
| 332 | |
| 333 | prefs->SetUserPrefValue(entry.pref_name, new base::DictionaryValue()); |
| 334 | EXPECT_TRUE(flag_checker->last_write_flags_set()); |
| 335 | EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear()); |
| 336 | } |
| 337 | } |
| 338 | |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 339 | class PrefServiceSetValueTest : public testing::Test { |
| 340 | protected: |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 341 | static const char kName[]; |
| 342 | static const char kValue[]; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 343 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 344 | PrefServiceSetValueTest() : observer_(&prefs_) {} |
| 345 | |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 346 | TestingPrefServiceSimple prefs_; |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 347 | MockPrefChangeCallback observer_; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 348 | }; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 349 | |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 350 | const char PrefServiceSetValueTest::kName[] = "name"; |
| 351 | const char PrefServiceSetValueTest::kValue[] = "value"; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 352 | |
| 353 | TEST_F(PrefServiceSetValueTest, SetStringValue) { |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 354 | const char default_string[] = "default"; |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame^] | 355 | const base::Value default_value(default_string); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 356 | prefs_.registry()->RegisterStringPref(kName, default_string); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 357 | |
| 358 | PrefChangeRegistrar registrar; |
| 359 | registrar.Init(&prefs_); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 360 | registrar.Add(kName, observer_.GetCallback()); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 361 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 362 | // Changing the controlling store from default to user triggers notification. |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 363 | observer_.Expect(kName, &default_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 364 | prefs_.Set(kName, default_value); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 365 | Mock::VerifyAndClearExpectations(&observer_); |
| 366 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 367 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 368 | prefs_.Set(kName, default_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 369 | Mock::VerifyAndClearExpectations(&observer_); |
| 370 | |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame^] | 371 | base::Value new_value(kValue); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 372 | observer_.Expect(kName, &new_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 373 | prefs_.Set(kName, new_value); |
| 374 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 378 | prefs_.registry()->RegisterDictionaryPref(kName); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 379 | PrefChangeRegistrar registrar; |
| 380 | registrar.Init(&prefs_); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 381 | registrar.Add(kName, observer_.GetCallback()); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 382 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 383 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 384 | prefs_.RemoveUserPref(kName); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 385 | Mock::VerifyAndClearExpectations(&observer_); |
| 386 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 387 | base::DictionaryValue new_value; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 388 | new_value.SetString(kName, kValue); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 389 | observer_.Expect(kName, &new_value); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 390 | prefs_.Set(kName, new_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 391 | Mock::VerifyAndClearExpectations(&observer_); |
| 392 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 393 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 394 | prefs_.Set(kName, new_value); |
| 395 | Mock::VerifyAndClearExpectations(&observer_); |
| 396 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 397 | base::DictionaryValue empty; |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 398 | observer_.Expect(kName, &empty); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 399 | prefs_.Set(kName, empty); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 400 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | TEST_F(PrefServiceSetValueTest, SetListValue) { |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 404 | prefs_.registry()->RegisterListPref(kName); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 405 | PrefChangeRegistrar registrar; |
| 406 | registrar.Init(&prefs_); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 407 | registrar.Add(kName, observer_.GetCallback()); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 408 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 409 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 410 | prefs_.RemoveUserPref(kName); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 411 | Mock::VerifyAndClearExpectations(&observer_); |
| 412 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 413 | base::ListValue new_value; |
dcheng | 58241a81 | 2016-06-03 18:18:42 | [diff] [blame] | 414 | new_value.AppendString(kValue); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 415 | observer_.Expect(kName, &new_value); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 416 | prefs_.Set(kName, new_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 417 | Mock::VerifyAndClearExpectations(&observer_); |
| 418 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 419 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 420 | prefs_.Set(kName, new_value); |
| 421 | Mock::VerifyAndClearExpectations(&observer_); |
| 422 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 423 | base::ListValue empty; |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 424 | observer_.Expect(kName, &empty); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 425 | prefs_.Set(kName, empty); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 426 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 427 | } |