blob: 54cc3ddec87cf65b8b643c8a732fb019379255cd [file] [log] [blame]
[email protected]74379bc52010-07-21 13:54:081// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]ecde2742010-04-02 17:36:185#include <string>
6
[email protected]3184cba42009-05-15 01:25:297#include "app/test/data/resource.h"
[email protected]ce1850e92010-10-15 08:40:588#include "base/command_line.h"
[email protected]ecde2742010-04-02 17:36:189#include "base/scoped_ptr.h"
10#include "base/values.h"
[email protected]ce1850e92010-10-15 08:40:5811#include "chrome/browser/policy/configuration_policy_pref_store.h"
12#include "chrome/browser/policy/mock_configuration_policy_provider.h"
13#include "chrome/browser/prefs/browser_prefs.h"
14#include "chrome/browser/prefs/command_line_pref_store.h"
15#include "chrome/browser/prefs/default_pref_store.h"
[email protected]37858e52010-08-26 00:22:0216#include "chrome/browser/prefs/dummy_pref_store.h"
[email protected]2fb7dc982010-09-29 12:24:2817#include "chrome/browser/prefs/pref_change_registrar.h"
[email protected]37858e52010-08-26 00:22:0218#include "chrome/browser/prefs/pref_value_store.h"
initial.commit09911bf2008-07-26 23:55:2919#include "chrome/common/chrome_paths.h"
[email protected]ce1850e92010-10-15 08:40:5820#include "chrome/common/chrome_switches.h"
[email protected]ecde2742010-04-02 17:36:1821#include "chrome/common/notification_observer_mock.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/common/notification_service.h"
[email protected]bfd04a62009-02-01 18:16:5623#include "chrome/common/notification_type.h"
initial.commit09911bf2008-07-26 23:55:2924#include "chrome/common/pref_names.h"
[email protected]74379bc52010-07-21 13:54:0825#include "chrome/test/testing_pref_service.h"
[email protected]ecde2742010-04-02 17:36:1826#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2927#include "testing/gtest/include/gtest/gtest.h"
28
[email protected]ecde2742010-04-02 17:36:1829using testing::_;
30using testing::Mock;
31using testing::Pointee;
32using testing::Property;
33
[email protected]12a3c022010-11-03 10:24:1134namespace {
35
initial.commit09911bf2008-07-26 23:55:2936class TestPrefObserver : public NotificationObserver {
37 public:
[email protected]ddd231e2010-06-29 20:35:1938 TestPrefObserver(const PrefService* prefs,
[email protected]57ecc4b2010-08-11 03:02:5139 const std::string& pref_name,
[email protected]ddd231e2010-06-29 20:35:1940 const std::string& new_pref_value)
initial.commit09911bf2008-07-26 23:55:2941 : observer_fired_(false),
42 prefs_(prefs),
43 pref_name_(pref_name),
[email protected]12a3c022010-11-03 10:24:1144 new_pref_value_(new_pref_value) {}
initial.commit09911bf2008-07-26 23:55:2945 virtual ~TestPrefObserver() {}
46
47 virtual void Observe(NotificationType type,
48 const NotificationSource& source,
49 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:5650 EXPECT_EQ(type.value, NotificationType::PREF_CHANGED);
[email protected]12a3c022010-11-03 10:24:1151 const PrefService* prefs_in = Source<PrefService>(source).ptr();
initial.commit09911bf2008-07-26 23:55:2952 EXPECT_EQ(prefs_in, prefs_);
[email protected]12a3c022010-11-03 10:24:1153 const std::string* pref_name_in = Details<std::string>(details).ptr();
initial.commit09911bf2008-07-26 23:55:2954 EXPECT_EQ(*pref_name_in, pref_name_);
[email protected]57ecc4b2010-08-11 03:02:5155 EXPECT_EQ(new_pref_value_, prefs_in->GetString("homepage"));
initial.commit09911bf2008-07-26 23:55:2956 observer_fired_ = true;
57 }
58
59 bool observer_fired() { return observer_fired_; }
60
[email protected]ddd231e2010-06-29 20:35:1961 void Reset(const std::string& new_pref_value) {
initial.commit09911bf2008-07-26 23:55:2962 observer_fired_ = false;
63 new_pref_value_ = new_pref_value;
64 }
65
66 private:
67 bool observer_fired_;
68 const PrefService* prefs_;
[email protected]57ecc4b2010-08-11 03:02:5169 const std::string pref_name_;
[email protected]ddd231e2010-06-29 20:35:1970 std::string new_pref_value_;
initial.commit09911bf2008-07-26 23:55:2971};
72
[email protected]12a3c022010-11-03 10:24:1173} // namespace
74
[email protected]7aa0a962010-04-21 17:24:4275// TODO(port): port this test to POSIX.
76#if defined(OS_WIN)
[email protected]277404c22010-04-22 13:09:4577TEST(PrefServiceTest, LocalizedPrefs) {
[email protected]74379bc52010-07-21 13:54:0878 TestingPrefService prefs;
[email protected]57ecc4b2010-08-11 03:02:5179 const char kBoolean[] = "boolean";
80 const char kInteger[] = "integer";
81 const char kString[] = "string";
[email protected]7aa0a962010-04-21 17:24:4282 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL);
83 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT);
84 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING);
85
86 // The locale default should take preference over the user default.
87 EXPECT_FALSE(prefs.GetBoolean(kBoolean));
88 EXPECT_EQ(1, prefs.GetInteger(kInteger));
[email protected]ddd231e2010-06-29 20:35:1989 EXPECT_EQ("hello", prefs.GetString(kString));
[email protected]7aa0a962010-04-21 17:24:4290
91 prefs.SetBoolean(kBoolean, true);
92 EXPECT_TRUE(prefs.GetBoolean(kBoolean));
93 prefs.SetInteger(kInteger, 5);
94 EXPECT_EQ(5, prefs.GetInteger(kInteger));
[email protected]ddd231e2010-06-29 20:35:1995 prefs.SetString(kString, "foo");
96 EXPECT_EQ("foo", prefs.GetString(kString));
[email protected]7aa0a962010-04-21 17:24:4297}
98#endif
99
[email protected]277404c22010-04-22 13:09:45100TEST(PrefServiceTest, NoObserverFire) {
[email protected]74379bc52010-07-21 13:54:08101 TestingPrefService prefs;
[email protected]7aa0a962010-04-21 17:24:42102
[email protected]57ecc4b2010-08-11 03:02:51103 const char pref_name[] = "homepage";
[email protected]12a3c022010-11-03 10:24:11104 prefs.RegisterStringPref(pref_name, std::string());
[email protected]7aa0a962010-04-21 17:24:42105
[email protected]ddd231e2010-06-29 20:35:19106 const std::string new_pref_value("http://www.google.com/");
[email protected]7aa0a962010-04-21 17:24:42107 TestPrefObserver obs(&prefs, pref_name, new_pref_value);
[email protected]2fb7dc982010-09-29 12:24:28108
109 PrefChangeRegistrar registrar;
110 registrar.Init(&prefs);
111 registrar.Add(pref_name, &obs);
[email protected]7aa0a962010-04-21 17:24:42112 // This should fire the checks in TestPrefObserver::Observe.
113 prefs.SetString(pref_name, new_pref_value);
114
115 // Make sure the observer was actually fired.
116 EXPECT_TRUE(obs.observer_fired());
117
118 // Setting the pref to the same value should not set the pref value a second
119 // time.
120 obs.Reset(new_pref_value);
121 prefs.SetString(pref_name, new_pref_value);
[email protected]40a47c162010-09-09 11:14:01122 EXPECT_FALSE(obs.observer_fired());
[email protected]7aa0a962010-04-21 17:24:42123
124 // Clearing the pref should cause the pref to fire.
[email protected]12a3c022010-11-03 10:24:11125 obs.Reset(std::string());
[email protected]7aa0a962010-04-21 17:24:42126 prefs.ClearPref(pref_name);
127 EXPECT_TRUE(obs.observer_fired());
128
129 // Clearing the pref again should not cause the pref to fire.
[email protected]12a3c022010-11-03 10:24:11130 obs.Reset(std::string());
[email protected]7aa0a962010-04-21 17:24:42131 prefs.ClearPref(pref_name);
[email protected]40a47c162010-09-09 11:14:01132 EXPECT_FALSE(obs.observer_fired());
[email protected]7aa0a962010-04-21 17:24:42133}
134
[email protected]277404c22010-04-22 13:09:45135TEST(PrefServiceTest, HasPrefPath) {
[email protected]74379bc52010-07-21 13:54:08136 TestingPrefService prefs;
[email protected]7aa0a962010-04-21 17:24:42137
[email protected]57ecc4b2010-08-11 03:02:51138 const char path[] = "fake.path";
[email protected]7aa0a962010-04-21 17:24:42139
140 // Shouldn't initially have a path.
141 EXPECT_FALSE(prefs.HasPrefPath(path));
142
143 // Register the path. This doesn't set a value, so the path still shouldn't
144 // exist.
[email protected]20ce516d2010-06-18 02:20:04145 prefs.RegisterStringPref(path, std::string());
[email protected]7aa0a962010-04-21 17:24:42146 EXPECT_FALSE(prefs.HasPrefPath(path));
147
148 // Set a value and make sure we have a path.
[email protected]ddd231e2010-06-29 20:35:19149 prefs.SetString(path, "blah");
[email protected]7aa0a962010-04-21 17:24:42150 EXPECT_TRUE(prefs.HasPrefPath(path));
151}
152
[email protected]277404c22010-04-22 13:09:45153TEST(PrefServiceTest, Observers) {
[email protected]57ecc4b2010-08-11 03:02:51154 const char pref_name[] = "homepage";
[email protected]277404c22010-04-22 13:09:45155
[email protected]74379bc52010-07-21 13:54:08156 TestingPrefService prefs;
[email protected]57ecc4b2010-08-11 03:02:51157 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com"));
[email protected]12a3c022010-11-03 10:24:11158 prefs.RegisterStringPref(pref_name, std::string());
[email protected]277404c22010-04-22 13:09:45159
[email protected]ddd231e2010-06-29 20:35:19160 const std::string new_pref_value("http://www.google.com/");
[email protected]277404c22010-04-22 13:09:45161 TestPrefObserver obs(&prefs, pref_name, new_pref_value);
[email protected]2fb7dc982010-09-29 12:24:28162 PrefChangeRegistrar registrar;
163 registrar.Init(&prefs);
164 registrar.Add(pref_name, &obs);
[email protected]277404c22010-04-22 13:09:45165 // This should fire the checks in TestPrefObserver::Observe.
166 prefs.SetString(pref_name, new_pref_value);
167
168 // Make sure the tests were actually run.
169 EXPECT_TRUE(obs.observer_fired());
170
171 // Now try adding a second pref observer.
[email protected]ddd231e2010-06-29 20:35:19172 const std::string new_pref_value2("http://www.youtube.com/");
[email protected]277404c22010-04-22 13:09:45173 obs.Reset(new_pref_value2);
174 TestPrefObserver obs2(&prefs, pref_name, new_pref_value2);
[email protected]2fb7dc982010-09-29 12:24:28175 registrar.Add(pref_name, &obs2);
[email protected]277404c22010-04-22 13:09:45176 // This should fire the checks in obs and obs2.
177 prefs.SetString(pref_name, new_pref_value2);
178 EXPECT_TRUE(obs.observer_fired());
179 EXPECT_TRUE(obs2.observer_fired());
180
181 // Make sure obs2 still works after removing obs.
[email protected]2fb7dc982010-09-29 12:24:28182 registrar.Remove(pref_name, &obs);
[email protected]12a3c022010-11-03 10:24:11183 obs.Reset(std::string());
[email protected]277404c22010-04-22 13:09:45184 obs2.Reset(new_pref_value);
185 // This should only fire the observer in obs2.
186 prefs.SetString(pref_name, new_pref_value);
187 EXPECT_FALSE(obs.observer_fired());
188 EXPECT_TRUE(obs2.observer_fired());
[email protected]277404c22010-04-22 13:09:45189}
190
[email protected]ce1850e92010-10-15 08:40:58191TEST(PrefServiceTest, ProxyFromCommandLineNotPolicy) {
[email protected]947446b2010-10-21 03:36:31192 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]ce1850e92010-10-15 08:40:58193 command_line.AppendSwitch(switches::kProxyAutoDetect);
194 TestingPrefService prefs(NULL, &command_line);
195 browser::RegisterUserPrefs(&prefs);
196 EXPECT_TRUE(prefs.GetBoolean(prefs::kProxyAutoDetect));
197 const PrefService::Preference* pref =
198 prefs.FindPreference(prefs::kProxyAutoDetect);
199 ASSERT_TRUE(pref);
200 EXPECT_FALSE(pref->IsManaged());
201}
202
203TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineOptions) {
[email protected]947446b2010-10-21 03:36:31204 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]ce1850e92010-10-15 08:40:58205 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123");
206 command_line.AppendSwitchASCII(switches::kProxyPacUrl, "456");
207 command_line.AppendSwitchASCII(switches::kProxyServer, "789");
208 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
209 new policy::MockConfigurationPolicyProvider());
210 Value* mode_value = Value::CreateIntegerValue(
[email protected]12a3c022010-11-03 10:24:11211 policy::kPolicyManuallyConfiguredProxyMode);
212 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value);
213 provider->AddPolicy(policy::kPolicyProxyBypassList,
214 Value::CreateStringValue("abc"));
215 provider->AddPolicy(policy::kPolicyProxyPacUrl,
216 Value::CreateStringValue("def"));
217 provider->AddPolicy(policy::kPolicyProxyServer,
218 Value::CreateStringValue("ghi"));
[email protected]ce1850e92010-10-15 08:40:58219
220 // First verify that command-line options are set correctly when
221 // there is no policy in effect.
222 TestingPrefService prefs(NULL, &command_line);
223 browser::RegisterUserPrefs(&prefs);
224 EXPECT_FALSE(prefs.GetBoolean(prefs::kProxyAutoDetect));
225 EXPECT_FALSE(prefs.GetBoolean(prefs::kNoProxyServer));
226 EXPECT_EQ("789", prefs.GetString(prefs::kProxyServer));
227 EXPECT_EQ("456", prefs.GetString(prefs::kProxyPacUrl));
228 EXPECT_EQ("123", prefs.GetString(prefs::kProxyBypassList));
229
230 // Try a second time time with the managed PrefStore in place, the
231 // manual proxy policy should have removed all traces of the command
232 // line and replaced them with the policy versions.
233 TestingPrefService prefs2(provider.get(), &command_line);
234 browser::RegisterUserPrefs(&prefs2);
235 EXPECT_FALSE(prefs2.GetBoolean(prefs::kProxyAutoDetect));
236 EXPECT_FALSE(prefs2.GetBoolean(prefs::kNoProxyServer));
237 EXPECT_EQ("ghi", prefs2.GetString(prefs::kProxyServer));
238 EXPECT_EQ("def", prefs2.GetString(prefs::kProxyPacUrl));
239 EXPECT_EQ("abc", prefs2.GetString(prefs::kProxyBypassList));
240}
241
242TEST(PrefServiceTest, ProxyPolicyOverridesUnrelatedCommandLineOptions) {
[email protected]947446b2010-10-21 03:36:31243 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]ce1850e92010-10-15 08:40:58244 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123");
245 command_line.AppendSwitchASCII(switches::kProxyPacUrl, "456");
246 command_line.AppendSwitchASCII(switches::kProxyServer, "789");
247 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
248 new policy::MockConfigurationPolicyProvider());
249 Value* mode_value = Value::CreateIntegerValue(
[email protected]12a3c022010-11-03 10:24:11250 policy::kPolicyUseSystemProxyMode);
251 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value);
[email protected]ce1850e92010-10-15 08:40:58252
253 // First verify that command-line options are set correctly when
254 // there is no policy in effect.
255 TestingPrefService prefs(NULL, &command_line);
256 browser::RegisterUserPrefs(&prefs);
257 EXPECT_FALSE(prefs.GetBoolean(prefs::kProxyAutoDetect));
258 EXPECT_FALSE(prefs.GetBoolean(prefs::kNoProxyServer));
259 EXPECT_EQ("789", prefs.GetString(prefs::kProxyServer));
260 EXPECT_EQ("456", prefs.GetString(prefs::kProxyPacUrl));
261 EXPECT_EQ("123", prefs.GetString(prefs::kProxyBypassList));
262
263 // Try a second time time with the managed PrefStore in place, the
264 // no proxy policy should have removed all traces of the command
265 // line proxy settings, even though they were not the specific one
266 // set in policy.
267 TestingPrefService prefs2(provider.get(), &command_line);
268 browser::RegisterUserPrefs(&prefs2);
269 EXPECT_FALSE(prefs2.GetBoolean(prefs::kProxyAutoDetect));
270 EXPECT_FALSE(prefs2.GetBoolean(prefs::kNoProxyServer));
[email protected]12a3c022010-11-03 10:24:11271 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyServer));
272 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyPacUrl));
273 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyBypassList));
[email protected]ce1850e92010-10-15 08:40:58274}
275
276TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineNoProxy) {
[email protected]947446b2010-10-21 03:36:31277 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]ce1850e92010-10-15 08:40:58278 command_line.AppendSwitch(switches::kNoProxyServer);
279 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
280 new policy::MockConfigurationPolicyProvider());
281 Value* mode_value = Value::CreateIntegerValue(
[email protected]12a3c022010-11-03 10:24:11282 policy::kPolicyAutoDetectProxyMode);
283 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value);
[email protected]ce1850e92010-10-15 08:40:58284
285 // First verify that command-line options are set correctly when
286 // there is no policy in effect.
287 TestingPrefService prefs(NULL, &command_line);
288 browser::RegisterUserPrefs(&prefs);
289 EXPECT_FALSE(prefs.GetBoolean(prefs::kProxyAutoDetect));
290 EXPECT_TRUE(prefs.GetBoolean(prefs::kNoProxyServer));
[email protected]12a3c022010-11-03 10:24:11291 EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyServer));
292 EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyPacUrl));
293 EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyBypassList));
[email protected]ce1850e92010-10-15 08:40:58294
295 // Try a second time time with the managed PrefStore in place, the
296 // auto-detect should be overridden. The default pref store must be
297 // in place with the appropriate default value for this to work.
298 TestingPrefService prefs2(provider.get(), &command_line);
299 browser::RegisterUserPrefs(&prefs2);
300 EXPECT_TRUE(prefs2.GetBoolean(prefs::kProxyAutoDetect));
301 EXPECT_FALSE(prefs2.GetBoolean(prefs::kNoProxyServer));
[email protected]12a3c022010-11-03 10:24:11302 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyServer));
303 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyPacUrl));
304 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyBypassList));
[email protected]ce1850e92010-10-15 08:40:58305}
306
307TEST(PrefServiceTest, ProxyPolicyOverridesCommandLineAutoDetect) {
[email protected]947446b2010-10-21 03:36:31308 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]ce1850e92010-10-15 08:40:58309 command_line.AppendSwitch(switches::kProxyAutoDetect);
310 scoped_ptr<policy::MockConfigurationPolicyProvider> provider(
311 new policy::MockConfigurationPolicyProvider());
312 Value* mode_value = Value::CreateIntegerValue(
[email protected]12a3c022010-11-03 10:24:11313 policy::kPolicyNoProxyServerMode);
314 provider->AddPolicy(policy::kPolicyProxyServerMode, mode_value);
[email protected]ce1850e92010-10-15 08:40:58315
316 // First verify that the auto-detect is set if there is no managed
317 // PrefStore.
318 TestingPrefService prefs(NULL, &command_line);
319 browser::RegisterUserPrefs(&prefs);
320 EXPECT_TRUE(prefs.GetBoolean(prefs::kProxyAutoDetect));
321 EXPECT_FALSE(prefs.GetBoolean(prefs::kNoProxyServer));
[email protected]12a3c022010-11-03 10:24:11322 EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyServer));
323 EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyPacUrl));
324 EXPECT_EQ(std::string(), prefs.GetString(prefs::kProxyBypassList));
[email protected]ce1850e92010-10-15 08:40:58325
326 // Try a second time time with the managed PrefStore in place, the
327 // auto-detect should be overridden. The default pref store must be
328 // in place with the appropriate default value for this to work.
329 TestingPrefService prefs2(provider.get(), &command_line);
330 browser::RegisterUserPrefs(&prefs2);
331 EXPECT_FALSE(prefs2.GetBoolean(prefs::kProxyAutoDetect));
332 EXPECT_TRUE(prefs2.GetBoolean(prefs::kNoProxyServer));
[email protected]12a3c022010-11-03 10:24:11333 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyServer));
334 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyPacUrl));
335 EXPECT_EQ(std::string(), prefs2.GetString(prefs::kProxyBypassList));
[email protected]ce1850e92010-10-15 08:40:58336}
337
[email protected]ecde2742010-04-02 17:36:18338class PrefServiceSetValueTest : public testing::Test {
339 protected:
[email protected]12a3c022010-11-03 10:24:11340 static const char kName[];
341 static const char kValue[];
[email protected]ecde2742010-04-02 17:36:18342
343 PrefServiceSetValueTest()
[email protected]12a3c022010-11-03 10:24:11344 : name_string_(kName),
[email protected]ecde2742010-04-02 17:36:18345 null_value_(Value::CreateNullValue()) {}
346
347 void SetExpectNoNotification() {
348 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
349 }
350
351 void SetExpectPrefChanged() {
352 EXPECT_CALL(observer_,
353 Observe(NotificationType(NotificationType::PREF_CHANGED), _,
[email protected]57ecc4b2010-08-11 03:02:51354 Property(&Details<std::string>::ptr,
[email protected]ecde2742010-04-02 17:36:18355 Pointee(name_string_))));
356 }
357
[email protected]74379bc52010-07-21 13:54:08358 TestingPrefService prefs_;
[email protected]57ecc4b2010-08-11 03:02:51359 std::string name_string_;
[email protected]ecde2742010-04-02 17:36:18360 scoped_ptr<Value> null_value_;
361 NotificationObserverMock observer_;
362};
[email protected]ddd231e2010-06-29 20:35:19363
[email protected]12a3c022010-11-03 10:24:11364const char PrefServiceSetValueTest::kName[] = "name";
365const char PrefServiceSetValueTest::kValue[] = "value";
[email protected]ecde2742010-04-02 17:36:18366
367TEST_F(PrefServiceSetValueTest, SetStringValue) {
[email protected]20ce516d2010-06-18 02:20:04368 const char default_string[] = "default";
[email protected]12a3c022010-11-03 10:24:11369 const scoped_ptr<Value> default_value(
370 Value::CreateStringValue(default_string));
371 prefs_.RegisterStringPref(kName, default_string);
[email protected]2fb7dc982010-09-29 12:24:28372
373 PrefChangeRegistrar registrar;
374 registrar.Init(&prefs_);
[email protected]12a3c022010-11-03 10:24:11375 registrar.Add(kName, &observer_);
[email protected]2fb7dc982010-09-29 12:24:28376
[email protected]c3b54f372010-09-14 08:25:07377 // Changing the controlling store from default to user triggers notification.
378 SetExpectPrefChanged();
[email protected]12a3c022010-11-03 10:24:11379 prefs_.Set(kName, *default_value);
[email protected]c3b54f372010-09-14 08:25:07380 Mock::VerifyAndClearExpectations(&observer_);
381
[email protected]40a47c162010-09-09 11:14:01382 SetExpectNoNotification();
[email protected]12a3c022010-11-03 10:24:11383 prefs_.Set(kName, *default_value);
[email protected]ecde2742010-04-02 17:36:18384 Mock::VerifyAndClearExpectations(&observer_);
385
[email protected]12a3c022010-11-03 10:24:11386 const scoped_ptr<Value> new_value(Value::CreateStringValue(kValue));
[email protected]ecde2742010-04-02 17:36:18387 SetExpectPrefChanged();
[email protected]12a3c022010-11-03 10:24:11388 prefs_.Set(kName, *new_value);
389 EXPECT_EQ(kValue, prefs_.GetString(kName));
[email protected]ecde2742010-04-02 17:36:18390}
391
392TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
[email protected]12a3c022010-11-03 10:24:11393 prefs_.RegisterDictionaryPref(kName);
[email protected]2fb7dc982010-09-29 12:24:28394 PrefChangeRegistrar registrar;
395 registrar.Init(&prefs_);
[email protected]12a3c022010-11-03 10:24:11396 registrar.Add(kName, &observer_);
[email protected]ecde2742010-04-02 17:36:18397
[email protected]c3b54f372010-09-14 08:25:07398 // Dictionary values are special: setting one to NULL is the same as clearing
399 // the user value, allowing the NULL default to take (or keep) control.
[email protected]ecde2742010-04-02 17:36:18400 SetExpectNoNotification();
[email protected]12a3c022010-11-03 10:24:11401 prefs_.Set(kName, *null_value_);
[email protected]ecde2742010-04-02 17:36:18402 Mock::VerifyAndClearExpectations(&observer_);
403
404 DictionaryValue new_value;
[email protected]12a3c022010-11-03 10:24:11405 new_value.SetString(kName, kValue);
[email protected]ecde2742010-04-02 17:36:18406 SetExpectPrefChanged();
[email protected]12a3c022010-11-03 10:24:11407 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18408 Mock::VerifyAndClearExpectations(&observer_);
[email protected]12a3c022010-11-03 10:24:11409 DictionaryValue* dict = prefs_.GetMutableDictionary(kName);
[email protected]ecde2742010-04-02 17:36:18410 EXPECT_EQ(1U, dict->size());
[email protected]ddd231e2010-06-29 20:35:19411 std::string out_value;
[email protected]12a3c022010-11-03 10:24:11412 dict->GetString(kName, &out_value);
413 EXPECT_EQ(kValue, out_value);
[email protected]ecde2742010-04-02 17:36:18414
[email protected]40a47c162010-09-09 11:14:01415 SetExpectNoNotification();
[email protected]12a3c022010-11-03 10:24:11416 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18417 Mock::VerifyAndClearExpectations(&observer_);
418
419 SetExpectPrefChanged();
[email protected]12a3c022010-11-03 10:24:11420 prefs_.Set(kName, *null_value_);
[email protected]ecde2742010-04-02 17:36:18421 Mock::VerifyAndClearExpectations(&observer_);
[email protected]12a3c022010-11-03 10:24:11422 dict = prefs_.GetMutableDictionary(kName);
[email protected]ecde2742010-04-02 17:36:18423 EXPECT_EQ(0U, dict->size());
[email protected]ecde2742010-04-02 17:36:18424}
425
426TEST_F(PrefServiceSetValueTest, SetListValue) {
[email protected]12a3c022010-11-03 10:24:11427 prefs_.RegisterListPref(kName);
[email protected]2fb7dc982010-09-29 12:24:28428 PrefChangeRegistrar registrar;
429 registrar.Init(&prefs_);
[email protected]12a3c022010-11-03 10:24:11430 registrar.Add(kName, &observer_);
[email protected]ecde2742010-04-02 17:36:18431
[email protected]c3b54f372010-09-14 08:25:07432 // List values are special: setting one to NULL is the same as clearing the
433 // user value, allowing the NULL default to take (or keep) control.
[email protected]ecde2742010-04-02 17:36:18434 SetExpectNoNotification();
[email protected]12a3c022010-11-03 10:24:11435 prefs_.Set(kName, *null_value_);
[email protected]ecde2742010-04-02 17:36:18436 Mock::VerifyAndClearExpectations(&observer_);
437
438 ListValue new_value;
[email protected]12a3c022010-11-03 10:24:11439 new_value.Append(Value::CreateStringValue(kValue));
[email protected]ecde2742010-04-02 17:36:18440 SetExpectPrefChanged();
[email protected]12a3c022010-11-03 10:24:11441 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18442 Mock::VerifyAndClearExpectations(&observer_);
[email protected]12a3c022010-11-03 10:24:11443 const ListValue* list = prefs_.GetMutableList(kName);
[email protected]ecde2742010-04-02 17:36:18444 ASSERT_EQ(1U, list->GetSize());
[email protected]ddd231e2010-06-29 20:35:19445 std::string out_value;
[email protected]ecde2742010-04-02 17:36:18446 list->GetString(0, &out_value);
[email protected]12a3c022010-11-03 10:24:11447 EXPECT_EQ(kValue, out_value);
[email protected]ecde2742010-04-02 17:36:18448
[email protected]40a47c162010-09-09 11:14:01449 SetExpectNoNotification();
[email protected]12a3c022010-11-03 10:24:11450 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18451 Mock::VerifyAndClearExpectations(&observer_);
452
453 SetExpectPrefChanged();
[email protected]12a3c022010-11-03 10:24:11454 prefs_.Set(kName, *null_value_);
[email protected]ecde2742010-04-02 17:36:18455 Mock::VerifyAndClearExpectations(&observer_);
[email protected]12a3c022010-11-03 10:24:11456 list = prefs_.GetMutableList(kName);
[email protected]ecde2742010-04-02 17:36:18457 EXPECT_EQ(0U, list->GetSize());
[email protected]ecde2742010-04-02 17:36:18458}