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