[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] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 8 | #include "base/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 9 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 11 | #include "base/path_service.h" |
[email protected] | 03b9b4e | 2012-10-22 20:01:52 | [diff] [blame] | 12 | #include "base/prefs/json_pref_store.h" |
| 13 | #include "base/prefs/public/pref_change_registrar.h" |
| 14 | #include "base/prefs/testing_pref_store.h" |
[email protected] | 1bec237 | 2011-12-07 09:19:06 | [diff] [blame] | 15 | #include "base/utf_string_conversions.h" |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 16 | #include "base/values.h" |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 17 | #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 18 | #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 19 | #include "chrome/browser/prefs/browser_prefs.h" |
| 20 | #include "chrome/browser/prefs/command_line_pref_store.h" |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 21 | #include "chrome/browser/prefs/mock_pref_change_callback.h" |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame^] | 22 | #include "chrome/browser/prefs/pref_registry_simple.h" |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 23 | #include "chrome/browser/prefs/pref_service_mock_builder.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 24 | #include "chrome/browser/prefs/pref_value_store.h" |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 25 | #include "chrome/browser/prefs/scoped_user_pref_update.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | #include "chrome/common/chrome_paths.h" |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame] | 27 | #include "chrome/common/chrome_switches.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | #include "chrome/common/pref_names.h" |
[email protected] | 1bec237 | 2011-12-07 09:19:06 | [diff] [blame] | 29 | #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
[email protected] | 8ad3636e | 2011-08-01 22:31:40 | [diff] [blame] | 30 | #include "chrome/test/base/testing_pref_service.h" |
[email protected] | 1bec237 | 2011-12-07 09:19:06 | [diff] [blame] | 31 | #include "chrome/test/base/testing_profile.h" |
[email protected] | e97882f | 2012-06-04 02:23:17 | [diff] [blame] | 32 | #include "content/public/test/test_browser_thread.h" |
[email protected] | b1e3f20 | 2012-06-04 14:45:50 | [diff] [blame] | 33 | #include "content/public/test/web_contents_tester.h" |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 34 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 41110ca | 2011-06-27 16:45:49 | [diff] [blame] | 36 | #include "ui/base/test/data/resource.h" |
[email protected] | 46a32b9 | 2012-03-22 13:04:48 | [diff] [blame] | 37 | #include "webkit/glue/webpreferences.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | |
[email protected] | 1bec237 | 2011-12-07 09:19:06 | [diff] [blame] | 39 | using content::BrowserThread; |
[email protected] | 46a32b9 | 2012-03-22 13:04:48 | [diff] [blame] | 40 | using content::WebContentsTester; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 41 | using testing::_; |
| 42 | using testing::Mock; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 43 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 44 | TEST(PrefServiceTest, NoObserverFire) { |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 45 | TestingPrefServiceSimple prefs; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 46 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 47 | const char pref_name[] = "homepage"; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame^] | 48 | prefs.registry()->RegisterStringPref(pref_name, std::string()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 49 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 50 | const char new_pref_value[] = "http://www.google.com/"; |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 51 | MockPrefChangeCallback obs(&prefs); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 52 | PrefChangeRegistrar registrar; |
| 53 | registrar.Init(&prefs); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 54 | registrar.Add(pref_name, obs.GetCallback()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 55 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 56 | // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 57 | const StringValue expected_value(new_pref_value); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 58 | obs.Expect(pref_name, &expected_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 59 | prefs.SetString(pref_name, new_pref_value); |
| 60 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 61 | |
| 62 | // Setting the pref to the same value should not set the pref value a second |
| 63 | // time. |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 64 | EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 65 | prefs.SetString(pref_name, new_pref_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 66 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 67 | |
| 68 | // Clearing the pref should cause the pref to fire. |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 69 | const StringValue expected_default_value(""); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 70 | obs.Expect(pref_name, &expected_default_value); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 71 | prefs.ClearPref(pref_name); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 72 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 73 | |
| 74 | // Clearing the pref again should not cause the pref to fire. |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 75 | EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 76 | prefs.ClearPref(pref_name); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 77 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 80 | TEST(PrefServiceTest, HasPrefPath) { |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 81 | TestingPrefServiceSimple prefs; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 82 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 83 | const char path[] = "fake.path"; |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 84 | |
| 85 | // Shouldn't initially have a path. |
| 86 | EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 87 | |
| 88 | // Register the path. This doesn't set a value, so the path still shouldn't |
| 89 | // exist. |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame^] | 90 | prefs.registry()->RegisterStringPref(path, std::string()); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 91 | EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 92 | |
| 93 | // Set a value and make sure we have a path. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 94 | prefs.SetString(path, "blah"); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 95 | EXPECT_TRUE(prefs.HasPrefPath(path)); |
| 96 | } |
| 97 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 98 | TEST(PrefServiceTest, Observers) { |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 99 | const char pref_name[] = "homepage"; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 100 | |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 101 | TestingPrefServiceSimple prefs; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 102 | prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame^] | 103 | prefs.registry()->RegisterStringPref(pref_name, std::string()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 104 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 105 | const char new_pref_value[] = "http://www.google.com/"; |
| 106 | const StringValue expected_new_pref_value(new_pref_value); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 107 | MockPrefChangeCallback obs(&prefs); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 108 | PrefChangeRegistrar registrar; |
| 109 | registrar.Init(&prefs); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 110 | registrar.Add(pref_name, obs.GetCallback()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 111 | |
[email protected] | 54ffd94a | 2012-11-12 15:29:20 | [diff] [blame] | 112 | PrefChangeRegistrar registrar_two; |
| 113 | registrar_two.Init(&prefs); |
| 114 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 115 | // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged. |
| 116 | obs.Expect(pref_name, &expected_new_pref_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 117 | prefs.SetString(pref_name, new_pref_value); |
| 118 | Mock::VerifyAndClearExpectations(&obs); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 119 | |
| 120 | // Now try adding a second pref observer. |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 121 | const char new_pref_value2[] = "http://www.youtube.com/"; |
| 122 | const StringValue expected_new_pref_value2(new_pref_value2); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 123 | MockPrefChangeCallback obs2(&prefs); |
| 124 | obs.Expect(pref_name, &expected_new_pref_value2); |
| 125 | obs2.Expect(pref_name, &expected_new_pref_value2); |
| 126 | registrar_two.Add(pref_name, obs2.GetCallback()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 127 | // This should fire the checks in obs and obs2. |
| 128 | prefs.SetString(pref_name, new_pref_value2); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 129 | Mock::VerifyAndClearExpectations(&obs); |
| 130 | Mock::VerifyAndClearExpectations(&obs2); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 131 | |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 132 | // Set a recommended value. |
| 133 | const StringValue recommended_pref_value("http://www.gmail.com/"); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 134 | obs.Expect(pref_name, &expected_new_pref_value2); |
| 135 | obs2.Expect(pref_name, &expected_new_pref_value2); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 136 | // This should fire the checks in obs and obs2 but with an unchanged value |
| 137 | // as the recommended value is being overridden by the user-set value. |
| 138 | prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy()); |
| 139 | Mock::VerifyAndClearExpectations(&obs); |
| 140 | Mock::VerifyAndClearExpectations(&obs2); |
| 141 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 142 | // Make sure obs2 still works after removing obs. |
[email protected] | 54ffd94a | 2012-11-12 15:29:20 | [diff] [blame] | 143 | registrar.Remove(pref_name); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 144 | EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
| 145 | obs2.Expect(pref_name, &expected_new_pref_value); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 146 | // This should only fire the observer in obs2. |
| 147 | prefs.SetString(pref_name, new_pref_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 148 | Mock::VerifyAndClearExpectations(&obs); |
| 149 | Mock::VerifyAndClearExpectations(&obs2); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 152 | // Make sure that if a preference changes type, so the wrong type is stored in |
| 153 | // the user pref file, it uses the correct fallback value instead. |
| 154 | TEST(PrefServiceTest, GetValueChangedType) { |
| 155 | const int kTestValue = 10; |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 156 | TestingPrefServiceSimple prefs; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame^] | 157 | prefs.registry()->RegisterIntegerPref( |
| 158 | prefs::kStabilityLaunchCount, kTestValue); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 159 | |
| 160 | // Check falling back to a recommended value. |
| 161 | prefs.SetUserPref(prefs::kStabilityLaunchCount, |
| 162 | Value::CreateStringValue("not an integer")); |
| 163 | const PrefService::Preference* pref = |
| 164 | prefs.FindPreference(prefs::kStabilityLaunchCount); |
| 165 | ASSERT_TRUE(pref); |
| 166 | const Value* value = pref->GetValue(); |
| 167 | ASSERT_TRUE(value); |
| 168 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 169 | int actual_int_value = -1; |
| 170 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 171 | EXPECT_EQ(kTestValue, actual_int_value); |
| 172 | } |
| 173 | |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 174 | TEST(PrefServiceTest, UpdateCommandLinePrefStore) { |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 175 | TestingPrefServiceSimple prefs; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame^] | 176 | prefs.registry()->RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, false); |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 177 | |
| 178 | // Check to make sure the value is as expected. |
| 179 | const PrefService::Preference* pref = |
| 180 | prefs.FindPreference(prefs::kCloudPrintProxyEnabled); |
| 181 | ASSERT_TRUE(pref); |
| 182 | const Value* value = pref->GetValue(); |
| 183 | ASSERT_TRUE(value); |
| 184 | EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); |
| 185 | bool actual_bool_value = true; |
| 186 | EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); |
[email protected] | 4eccb81a | 2012-01-26 12:01:22 | [diff] [blame] | 187 | EXPECT_FALSE(actual_bool_value); |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 188 | |
| 189 | // Change the command line. |
| 190 | CommandLine cmd_line(CommandLine::NO_PROGRAM); |
| 191 | cmd_line.AppendSwitch(switches::kEnableCloudPrintProxy); |
| 192 | |
| 193 | // Call UpdateCommandLinePrefStore and check to see if the value has changed. |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 194 | prefs.UpdateCommandLinePrefStore(new CommandLinePrefStore(&cmd_line)); |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 195 | pref = prefs.FindPreference(prefs::kCloudPrintProxyEnabled); |
| 196 | ASSERT_TRUE(pref); |
| 197 | value = pref->GetValue(); |
| 198 | ASSERT_TRUE(value); |
| 199 | EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); |
| 200 | actual_bool_value = false; |
| 201 | EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); |
[email protected] | 4eccb81a | 2012-01-26 12:01:22 | [diff] [blame] | 202 | EXPECT_TRUE(actual_bool_value); |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 205 | TEST(PrefServiceTest, GetValueAndGetRecommendedValue) { |
| 206 | const int kDefaultValue = 5; |
| 207 | const int kUserValue = 10; |
| 208 | const int kRecommendedValue = 15; |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 209 | TestingPrefServiceSimple prefs; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame^] | 210 | prefs.registry()->RegisterIntegerPref( |
| 211 | prefs::kStabilityLaunchCount, kDefaultValue); |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 212 | |
| 213 | // Create pref with a default value only. |
| 214 | const PrefService::Preference* pref = |
| 215 | prefs.FindPreference(prefs::kStabilityLaunchCount); |
| 216 | ASSERT_TRUE(pref); |
| 217 | |
| 218 | // Check that GetValue() returns the default value. |
| 219 | const Value* value = pref->GetValue(); |
| 220 | ASSERT_TRUE(value); |
| 221 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 222 | int actual_int_value = -1; |
| 223 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 224 | EXPECT_EQ(kDefaultValue, actual_int_value); |
| 225 | |
| 226 | // Check that GetRecommendedValue() returns no value. |
| 227 | value = pref->GetRecommendedValue(); |
| 228 | ASSERT_FALSE(value); |
| 229 | |
| 230 | // Set a user-set value. |
| 231 | prefs.SetUserPref(prefs::kStabilityLaunchCount, |
| 232 | Value::CreateIntegerValue(kUserValue)); |
| 233 | |
| 234 | // Check that GetValue() returns the user-set value. |
| 235 | value = pref->GetValue(); |
| 236 | ASSERT_TRUE(value); |
| 237 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 238 | actual_int_value = -1; |
| 239 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 240 | EXPECT_EQ(kUserValue, actual_int_value); |
| 241 | |
| 242 | // Check that GetRecommendedValue() returns no value. |
| 243 | value = pref->GetRecommendedValue(); |
| 244 | ASSERT_FALSE(value); |
| 245 | |
| 246 | // Set a recommended value. |
| 247 | prefs.SetRecommendedPref(prefs::kStabilityLaunchCount, |
| 248 | Value::CreateIntegerValue(kRecommendedValue)); |
| 249 | |
| 250 | // Check that GetValue() returns the user-set value. |
| 251 | value = pref->GetValue(); |
| 252 | ASSERT_TRUE(value); |
| 253 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 254 | actual_int_value = -1; |
| 255 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 256 | EXPECT_EQ(kUserValue, actual_int_value); |
| 257 | |
| 258 | // Check that GetRecommendedValue() returns the recommended value. |
| 259 | value = pref->GetRecommendedValue(); |
| 260 | ASSERT_TRUE(value); |
| 261 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 262 | actual_int_value = -1; |
| 263 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 264 | EXPECT_EQ(kRecommendedValue, actual_int_value); |
| 265 | |
| 266 | // Remove the user-set value. |
| 267 | prefs.RemoveUserPref(prefs::kStabilityLaunchCount); |
| 268 | |
| 269 | // Check that GetValue() returns the recommended value. |
| 270 | value = pref->GetValue(); |
| 271 | ASSERT_TRUE(value); |
| 272 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 273 | actual_int_value = -1; |
| 274 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 275 | EXPECT_EQ(kRecommendedValue, actual_int_value); |
| 276 | |
| 277 | // Check that GetRecommendedValue() returns the recommended value. |
| 278 | value = pref->GetRecommendedValue(); |
| 279 | ASSERT_TRUE(value); |
| 280 | EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 281 | actual_int_value = -1; |
| 282 | EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 283 | EXPECT_EQ(kRecommendedValue, actual_int_value); |
| 284 | } |
| 285 | |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 286 | class PrefServiceUserFilePrefsTest : public testing::Test { |
| 287 | protected: |
| 288 | virtual void SetUp() { |
| 289 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 290 | |
| 291 | ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); |
| 292 | data_dir_ = data_dir_.AppendASCII("pref_service"); |
| 293 | ASSERT_TRUE(file_util::PathExists(data_dir_)); |
| 294 | } |
| 295 | |
| 296 | void ClearListValue(PrefService* prefs, const char* key) { |
| 297 | ListPrefUpdate updater(prefs, key); |
| 298 | updater->Clear(); |
| 299 | } |
| 300 | |
| 301 | void ClearDictionaryValue(PrefService* prefs, const char* key) { |
| 302 | DictionaryPrefUpdate updater(prefs, key); |
| 303 | updater->Clear(); |
| 304 | } |
| 305 | |
| 306 | // The path to temporary directory used to contain the test operations. |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 307 | base::ScopedTempDir temp_dir_; |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 308 | // The path to the directory where the test data is stored. |
| 309 | FilePath data_dir_; |
| 310 | // A message loop that we can use as the file thread message loop. |
| 311 | MessageLoop message_loop_; |
| 312 | }; |
| 313 | |
| 314 | // Verifies that ListValue and DictionaryValue pref with non emtpy default |
| 315 | // preserves its empty value. |
| 316 | TEST_F(PrefServiceUserFilePrefsTest, PreserveEmptyValue) { |
| 317 | FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); |
| 318 | |
| 319 | ASSERT_TRUE(file_util::CopyFile( |
| 320 | data_dir_.AppendASCII("read.need_empty_value.json"), |
| 321 | pref_file)); |
| 322 | |
| 323 | PrefServiceMockBuilder builder; |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 324 | builder.WithUserFilePrefs(pref_file, message_loop_.message_loop_proxy()); |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 325 | scoped_ptr<PrefServiceSyncable> prefs(builder.CreateSyncable()); |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 326 | |
| 327 | // Register testing prefs. |
| 328 | prefs->RegisterListPref("list", |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 329 | PrefServiceSyncable::UNSYNCABLE_PREF); |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 330 | prefs->RegisterDictionaryPref("dict", |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 331 | PrefServiceSyncable::UNSYNCABLE_PREF); |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 332 | |
| 333 | base::ListValue* non_empty_list = new base::ListValue; |
| 334 | non_empty_list->Append(base::Value::CreateStringValue("test")); |
| 335 | prefs->RegisterListPref("list_needs_empty_value", |
| 336 | non_empty_list, |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 337 | PrefServiceSyncable::UNSYNCABLE_PREF); |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 338 | |
| 339 | base::DictionaryValue* non_empty_dict = new base::DictionaryValue; |
| 340 | non_empty_dict->SetString("dummy", "whatever"); |
| 341 | prefs->RegisterDictionaryPref("dict_needs_empty_value", |
| 342 | non_empty_dict, |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 343 | PrefServiceSyncable::UNSYNCABLE_PREF); |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 344 | |
| 345 | // Set all testing prefs to empty. |
| 346 | ClearListValue(prefs.get(), "list"); |
| 347 | ClearListValue(prefs.get(), "list_needs_empty_value"); |
| 348 | ClearDictionaryValue(prefs.get(), "dict"); |
| 349 | ClearDictionaryValue(prefs.get(), "dict_needs_empty_value"); |
| 350 | |
| 351 | // Write to file. |
| 352 | prefs->CommitPendingWrite(); |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 353 | message_loop_.RunUntilIdle(); |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 354 | |
| 355 | // Compare to expected output. |
| 356 | FilePath golden_output_file = |
| 357 | data_dir_.AppendASCII("write.golden.need_empty_value.json"); |
| 358 | ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
| 359 | EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); |
| 360 | } |
| 361 | |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 362 | class PrefServiceSetValueTest : public testing::Test { |
| 363 | protected: |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 364 | static const char kName[]; |
| 365 | static const char kValue[]; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 366 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 367 | PrefServiceSetValueTest() : observer_(&prefs_) {} |
| 368 | |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 369 | TestingPrefServiceSimple prefs_; |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 370 | MockPrefChangeCallback observer_; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 371 | }; |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 372 | |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 373 | const char PrefServiceSetValueTest::kName[] = "name"; |
| 374 | const char PrefServiceSetValueTest::kValue[] = "value"; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 375 | |
| 376 | TEST_F(PrefServiceSetValueTest, SetStringValue) { |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 377 | const char default_string[] = "default"; |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 378 | const StringValue default_value(default_string); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame^] | 379 | prefs_.registry()->RegisterStringPref(kName, default_string); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 380 | |
| 381 | PrefChangeRegistrar registrar; |
| 382 | registrar.Init(&prefs_); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 383 | registrar.Add(kName, observer_.GetCallback()); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 384 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 385 | // Changing the controlling store from default to user triggers notification. |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 386 | observer_.Expect(kName, &default_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 387 | prefs_.Set(kName, default_value); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 388 | Mock::VerifyAndClearExpectations(&observer_); |
| 389 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 390 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 391 | prefs_.Set(kName, default_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 392 | Mock::VerifyAndClearExpectations(&observer_); |
| 393 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 394 | StringValue new_value(kValue); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 395 | observer_.Expect(kName, &new_value); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 396 | prefs_.Set(kName, new_value); |
| 397 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame^] | 401 | prefs_.registry()->RegisterDictionaryPref(kName); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 402 | PrefChangeRegistrar registrar; |
| 403 | registrar.Init(&prefs_); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 404 | registrar.Add(kName, observer_.GetCallback()); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 405 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 406 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 407 | prefs_.RemoveUserPref(kName); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 408 | Mock::VerifyAndClearExpectations(&observer_); |
| 409 | |
| 410 | DictionaryValue new_value; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 411 | new_value.SetString(kName, kValue); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 412 | observer_.Expect(kName, &new_value); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 413 | prefs_.Set(kName, new_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 414 | Mock::VerifyAndClearExpectations(&observer_); |
| 415 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 416 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 417 | prefs_.Set(kName, new_value); |
| 418 | Mock::VerifyAndClearExpectations(&observer_); |
| 419 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 420 | DictionaryValue empty; |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 421 | observer_.Expect(kName, &empty); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 422 | prefs_.Set(kName, empty); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 423 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | TEST_F(PrefServiceSetValueTest, SetListValue) { |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame^] | 427 | prefs_.registry()->RegisterListPref(kName); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 428 | PrefChangeRegistrar registrar; |
| 429 | registrar.Init(&prefs_); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 430 | registrar.Add(kName, observer_.GetCallback()); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 431 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 432 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 433 | prefs_.RemoveUserPref(kName); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 434 | Mock::VerifyAndClearExpectations(&observer_); |
| 435 | |
| 436 | ListValue new_value; |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 437 | new_value.Append(Value::CreateStringValue(kValue)); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 438 | observer_.Expect(kName, &new_value); |
[email protected] | 12a3c02 | 2010-11-03 10:24:11 | [diff] [blame] | 439 | prefs_.Set(kName, new_value); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 440 | Mock::VerifyAndClearExpectations(&observer_); |
| 441 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 442 | EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 443 | prefs_.Set(kName, new_value); |
| 444 | Mock::VerifyAndClearExpectations(&observer_); |
| 445 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 446 | ListValue empty; |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 447 | observer_.Expect(kName, &empty); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 448 | prefs_.Set(kName, empty); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 449 | Mock::VerifyAndClearExpectations(&observer_); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 450 | } |
[email protected] | 1bec237 | 2011-12-07 09:19:06 | [diff] [blame] | 451 | |
| 452 | class PrefServiceWebKitPrefs : public ChromeRenderViewHostTestHarness { |
| 453 | protected: |
| 454 | PrefServiceWebKitPrefs() : ui_thread_(BrowserThread::UI, &message_loop_) { |
| 455 | } |
| 456 | |
| 457 | virtual void SetUp() { |
| 458 | ChromeRenderViewHostTestHarness::SetUp(); |
| 459 | |
| 460 | // Supply our own profile so we use the correct profile data. The test |
| 461 | // harness is not supposed to overwrite a profile if it's already created. |
| 462 | |
| 463 | // Set some (WebKit) user preferences. |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 464 | TestingPrefServiceSyncable* pref_services = |
| 465 | profile()->GetTestingPrefService(); |
[email protected] | a13283cc | 2012-04-05 00:21:22 | [diff] [blame] | 466 | #if defined(TOOLKIT_GTK) |
[email protected] | 1bec237 | 2011-12-07 09:19:06 | [diff] [blame] | 467 | pref_services->SetUserPref(prefs::kUsesSystemTheme, |
| 468 | Value::CreateBooleanValue(false)); |
| 469 | #endif |
[email protected] | ddf7214 | 2012-05-22 04:52:40 | [diff] [blame] | 470 | pref_services->SetUserPref(prefs::kDefaultCharset, |
[email protected] | 1bec237 | 2011-12-07 09:19:06 | [diff] [blame] | 471 | Value::CreateStringValue("utf8")); |
[email protected] | ddf7214 | 2012-05-22 04:52:40 | [diff] [blame] | 472 | pref_services->SetUserPref(prefs::kWebKitDefaultFontSize, |
[email protected] | 1bec237 | 2011-12-07 09:19:06 | [diff] [blame] | 473 | Value::CreateIntegerValue(20)); |
| 474 | pref_services->SetUserPref(prefs::kWebKitTextAreasAreResizable, |
| 475 | Value::CreateBooleanValue(false)); |
| 476 | pref_services->SetUserPref(prefs::kWebKitUsesUniversalDetector, |
| 477 | Value::CreateBooleanValue(true)); |
| 478 | pref_services->SetUserPref("webkit.webprefs.foo", |
| 479 | Value::CreateStringValue("bar")); |
| 480 | } |
| 481 | |
| 482 | private: |
| 483 | content::TestBrowserThread ui_thread_; |
| 484 | }; |
| 485 | |
| 486 | // Tests to see that webkit preferences are properly loaded and copied over |
| 487 | // to a WebPreferences object. |
| 488 | TEST_F(PrefServiceWebKitPrefs, PrefsCopied) { |
[email protected] | 6717bf27 | 2012-05-11 23:31:25 | [diff] [blame] | 489 | webkit_glue::WebPreferences webkit_prefs = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 490 | WebContentsTester::For(web_contents())->TestGetWebkitPrefs(); |
[email protected] | 1bec237 | 2011-12-07 09:19:06 | [diff] [blame] | 491 | |
| 492 | // These values have been overridden by the profile preferences. |
| 493 | EXPECT_EQ("UTF-8", webkit_prefs.default_encoding); |
| 494 | EXPECT_EQ(20, webkit_prefs.default_font_size); |
| 495 | EXPECT_FALSE(webkit_prefs.text_areas_are_resizable); |
| 496 | EXPECT_TRUE(webkit_prefs.uses_universal_detector); |
| 497 | |
| 498 | // These should still be the default values. |
| 499 | #if defined(OS_MACOSX) |
| 500 | const char kDefaultFont[] = "Times"; |
| 501 | #elif defined(OS_CHROMEOS) |
| 502 | const char kDefaultFont[] = "Tinos"; |
| 503 | #else |
| 504 | const char kDefaultFont[] = "Times New Roman"; |
| 505 | #endif |
[email protected] | 966d1e1 | 2012-05-18 07:20:32 | [diff] [blame] | 506 | EXPECT_EQ(ASCIIToUTF16(kDefaultFont), |
| 507 | webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]); |
[email protected] | 1bec237 | 2011-12-07 09:19:06 | [diff] [blame] | 508 | EXPECT_TRUE(webkit_prefs.javascript_enabled); |
| 509 | } |