blob: d62679d25d47884bfe1f1c70ca6ab94da2d0305b [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]ecde2742010-04-02 17:36:188#include "base/scoped_ptr.h"
9#include "base/values.h"
[email protected]37858e52010-08-26 00:22:0210#include "chrome/browser/prefs/dummy_pref_store.h"
[email protected]2fb7dc982010-09-29 12:24:2811#include "chrome/browser/prefs/pref_change_registrar.h"
[email protected]37858e52010-08-26 00:22:0212#include "chrome/browser/prefs/pref_value_store.h"
initial.commit09911bf2008-07-26 23:55:2913#include "chrome/common/chrome_paths.h"
[email protected]ecde2742010-04-02 17:36:1814#include "chrome/common/notification_observer_mock.h"
initial.commit09911bf2008-07-26 23:55:2915#include "chrome/common/notification_service.h"
[email protected]bfd04a62009-02-01 18:16:5616#include "chrome/common/notification_type.h"
initial.commit09911bf2008-07-26 23:55:2917#include "chrome/common/pref_names.h"
[email protected]74379bc52010-07-21 13:54:0818#include "chrome/test/testing_pref_service.h"
[email protected]ecde2742010-04-02 17:36:1819#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2920#include "testing/gtest/include/gtest/gtest.h"
21
[email protected]ecde2742010-04-02 17:36:1822using testing::_;
23using testing::Mock;
24using testing::Pointee;
25using testing::Property;
26
initial.commit09911bf2008-07-26 23:55:2927class TestPrefObserver : public NotificationObserver {
28 public:
[email protected]ddd231e2010-06-29 20:35:1929 TestPrefObserver(const PrefService* prefs,
[email protected]57ecc4b2010-08-11 03:02:5130 const std::string& pref_name,
[email protected]ddd231e2010-06-29 20:35:1931 const std::string& new_pref_value)
initial.commit09911bf2008-07-26 23:55:2932 : observer_fired_(false),
33 prefs_(prefs),
34 pref_name_(pref_name),
35 new_pref_value_(new_pref_value) {
36 }
37 virtual ~TestPrefObserver() {}
38
39 virtual void Observe(NotificationType type,
40 const NotificationSource& source,
41 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:5642 EXPECT_EQ(type.value, NotificationType::PREF_CHANGED);
initial.commit09911bf2008-07-26 23:55:2943 PrefService* prefs_in = Source<PrefService>(source).ptr();
44 EXPECT_EQ(prefs_in, prefs_);
[email protected]57ecc4b2010-08-11 03:02:5145 std::string* pref_name_in = Details<std::string>(details).ptr();
initial.commit09911bf2008-07-26 23:55:2946 EXPECT_EQ(*pref_name_in, pref_name_);
[email protected]57ecc4b2010-08-11 03:02:5147 EXPECT_EQ(new_pref_value_, prefs_in->GetString("homepage"));
initial.commit09911bf2008-07-26 23:55:2948 observer_fired_ = true;
49 }
50
51 bool observer_fired() { return observer_fired_; }
52
[email protected]ddd231e2010-06-29 20:35:1953 void Reset(const std::string& new_pref_value) {
initial.commit09911bf2008-07-26 23:55:2954 observer_fired_ = false;
55 new_pref_value_ = new_pref_value;
56 }
57
58 private:
59 bool observer_fired_;
60 const PrefService* prefs_;
[email protected]57ecc4b2010-08-11 03:02:5161 const std::string pref_name_;
[email protected]ddd231e2010-06-29 20:35:1962 std::string new_pref_value_;
initial.commit09911bf2008-07-26 23:55:2963};
64
[email protected]7aa0a962010-04-21 17:24:4265// TODO(port): port this test to POSIX.
66#if defined(OS_WIN)
[email protected]277404c22010-04-22 13:09:4567TEST(PrefServiceTest, LocalizedPrefs) {
[email protected]74379bc52010-07-21 13:54:0868 TestingPrefService prefs;
[email protected]57ecc4b2010-08-11 03:02:5169 const char kBoolean[] = "boolean";
70 const char kInteger[] = "integer";
71 const char kString[] = "string";
[email protected]7aa0a962010-04-21 17:24:4272 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL);
73 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT);
74 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING);
75
76 // The locale default should take preference over the user default.
77 EXPECT_FALSE(prefs.GetBoolean(kBoolean));
78 EXPECT_EQ(1, prefs.GetInteger(kInteger));
[email protected]ddd231e2010-06-29 20:35:1979 EXPECT_EQ("hello", prefs.GetString(kString));
[email protected]7aa0a962010-04-21 17:24:4280
81 prefs.SetBoolean(kBoolean, true);
82 EXPECT_TRUE(prefs.GetBoolean(kBoolean));
83 prefs.SetInteger(kInteger, 5);
84 EXPECT_EQ(5, prefs.GetInteger(kInteger));
[email protected]ddd231e2010-06-29 20:35:1985 prefs.SetString(kString, "foo");
86 EXPECT_EQ("foo", prefs.GetString(kString));
[email protected]7aa0a962010-04-21 17:24:4287}
88#endif
89
[email protected]277404c22010-04-22 13:09:4590TEST(PrefServiceTest, NoObserverFire) {
[email protected]74379bc52010-07-21 13:54:0891 TestingPrefService prefs;
[email protected]7aa0a962010-04-21 17:24:4292
[email protected]57ecc4b2010-08-11 03:02:5193 const char pref_name[] = "homepage";
[email protected]20ce516d2010-06-18 02:20:0494 prefs.RegisterStringPref(pref_name, "");
[email protected]7aa0a962010-04-21 17:24:4295
[email protected]ddd231e2010-06-29 20:35:1996 const std::string new_pref_value("http://www.google.com/");
[email protected]7aa0a962010-04-21 17:24:4297 TestPrefObserver obs(&prefs, pref_name, new_pref_value);
[email protected]2fb7dc982010-09-29 12:24:2898
99 PrefChangeRegistrar registrar;
100 registrar.Init(&prefs);
101 registrar.Add(pref_name, &obs);
[email protected]7aa0a962010-04-21 17:24:42102 // This should fire the checks in TestPrefObserver::Observe.
103 prefs.SetString(pref_name, new_pref_value);
104
105 // Make sure the observer was actually fired.
106 EXPECT_TRUE(obs.observer_fired());
107
108 // Setting the pref to the same value should not set the pref value a second
109 // time.
110 obs.Reset(new_pref_value);
111 prefs.SetString(pref_name, new_pref_value);
[email protected]40a47c162010-09-09 11:14:01112 EXPECT_FALSE(obs.observer_fired());
[email protected]7aa0a962010-04-21 17:24:42113
114 // Clearing the pref should cause the pref to fire.
[email protected]ddd231e2010-06-29 20:35:19115 obs.Reset("");
[email protected]7aa0a962010-04-21 17:24:42116 prefs.ClearPref(pref_name);
117 EXPECT_TRUE(obs.observer_fired());
118
119 // Clearing the pref again should not cause the pref to fire.
[email protected]ddd231e2010-06-29 20:35:19120 obs.Reset("");
[email protected]7aa0a962010-04-21 17:24:42121 prefs.ClearPref(pref_name);
[email protected]40a47c162010-09-09 11:14:01122 EXPECT_FALSE(obs.observer_fired());
[email protected]7aa0a962010-04-21 17:24:42123}
124
[email protected]277404c22010-04-22 13:09:45125TEST(PrefServiceTest, HasPrefPath) {
[email protected]74379bc52010-07-21 13:54:08126 TestingPrefService prefs;
[email protected]7aa0a962010-04-21 17:24:42127
[email protected]57ecc4b2010-08-11 03:02:51128 const char path[] = "fake.path";
[email protected]7aa0a962010-04-21 17:24:42129
130 // Shouldn't initially have a path.
131 EXPECT_FALSE(prefs.HasPrefPath(path));
132
133 // Register the path. This doesn't set a value, so the path still shouldn't
134 // exist.
[email protected]20ce516d2010-06-18 02:20:04135 prefs.RegisterStringPref(path, std::string());
[email protected]7aa0a962010-04-21 17:24:42136 EXPECT_FALSE(prefs.HasPrefPath(path));
137
138 // Set a value and make sure we have a path.
[email protected]ddd231e2010-06-29 20:35:19139 prefs.SetString(path, "blah");
[email protected]7aa0a962010-04-21 17:24:42140 EXPECT_TRUE(prefs.HasPrefPath(path));
141}
142
[email protected]277404c22010-04-22 13:09:45143TEST(PrefServiceTest, Observers) {
[email protected]57ecc4b2010-08-11 03:02:51144 const char pref_name[] = "homepage";
[email protected]277404c22010-04-22 13:09:45145
[email protected]74379bc52010-07-21 13:54:08146 TestingPrefService prefs;
[email protected]57ecc4b2010-08-11 03:02:51147 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com"));
[email protected]20ce516d2010-06-18 02:20:04148 prefs.RegisterStringPref(pref_name, "");
[email protected]277404c22010-04-22 13:09:45149
[email protected]ddd231e2010-06-29 20:35:19150 const std::string new_pref_value("http://www.google.com/");
[email protected]277404c22010-04-22 13:09:45151 TestPrefObserver obs(&prefs, pref_name, new_pref_value);
[email protected]2fb7dc982010-09-29 12:24:28152 PrefChangeRegistrar registrar;
153 registrar.Init(&prefs);
154 registrar.Add(pref_name, &obs);
[email protected]277404c22010-04-22 13:09:45155 // This should fire the checks in TestPrefObserver::Observe.
156 prefs.SetString(pref_name, new_pref_value);
157
158 // Make sure the tests were actually run.
159 EXPECT_TRUE(obs.observer_fired());
160
161 // Now try adding a second pref observer.
[email protected]ddd231e2010-06-29 20:35:19162 const std::string new_pref_value2("http://www.youtube.com/");
[email protected]277404c22010-04-22 13:09:45163 obs.Reset(new_pref_value2);
164 TestPrefObserver obs2(&prefs, pref_name, new_pref_value2);
[email protected]2fb7dc982010-09-29 12:24:28165 registrar.Add(pref_name, &obs2);
[email protected]277404c22010-04-22 13:09:45166 // This should fire the checks in obs and obs2.
167 prefs.SetString(pref_name, new_pref_value2);
168 EXPECT_TRUE(obs.observer_fired());
169 EXPECT_TRUE(obs2.observer_fired());
170
171 // Make sure obs2 still works after removing obs.
[email protected]2fb7dc982010-09-29 12:24:28172 registrar.Remove(pref_name, &obs);
[email protected]ddd231e2010-06-29 20:35:19173 obs.Reset("");
[email protected]277404c22010-04-22 13:09:45174 obs2.Reset(new_pref_value);
175 // This should only fire the observer in obs2.
176 prefs.SetString(pref_name, new_pref_value);
177 EXPECT_FALSE(obs.observer_fired());
178 EXPECT_TRUE(obs2.observer_fired());
[email protected]277404c22010-04-22 13:09:45179}
180
[email protected]ecde2742010-04-02 17:36:18181class PrefServiceSetValueTest : public testing::Test {
182 protected:
[email protected]57ecc4b2010-08-11 03:02:51183 static const char name_[];
[email protected]ddd231e2010-06-29 20:35:19184 static const char value_[];
[email protected]ecde2742010-04-02 17:36:18185
186 PrefServiceSetValueTest()
[email protected]74379bc52010-07-21 13:54:08187 : name_string_(name_),
[email protected]ecde2742010-04-02 17:36:18188 null_value_(Value::CreateNullValue()) {}
189
190 void SetExpectNoNotification() {
191 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
192 }
193
194 void SetExpectPrefChanged() {
195 EXPECT_CALL(observer_,
196 Observe(NotificationType(NotificationType::PREF_CHANGED), _,
[email protected]57ecc4b2010-08-11 03:02:51197 Property(&Details<std::string>::ptr,
[email protected]ecde2742010-04-02 17:36:18198 Pointee(name_string_))));
199 }
200
[email protected]74379bc52010-07-21 13:54:08201 TestingPrefService prefs_;
[email protected]57ecc4b2010-08-11 03:02:51202 std::string name_string_;
[email protected]ecde2742010-04-02 17:36:18203 scoped_ptr<Value> null_value_;
204 NotificationObserverMock observer_;
205};
[email protected]ddd231e2010-06-29 20:35:19206
[email protected]57ecc4b2010-08-11 03:02:51207const char PrefServiceSetValueTest::name_[] = "name";
[email protected]ddd231e2010-06-29 20:35:19208const char PrefServiceSetValueTest::value_[] = "value";
[email protected]ecde2742010-04-02 17:36:18209
210TEST_F(PrefServiceSetValueTest, SetStringValue) {
[email protected]20ce516d2010-06-18 02:20:04211 const char default_string[] = "default";
[email protected]ecde2742010-04-02 17:36:18212 scoped_ptr<Value> default_value(Value::CreateStringValue(default_string));
213 prefs_.RegisterStringPref(name_, default_string);
[email protected]2fb7dc982010-09-29 12:24:28214
215 PrefChangeRegistrar registrar;
216 registrar.Init(&prefs_);
217 registrar.Add(name_, &observer_);
218
[email protected]c3b54f372010-09-14 08:25:07219 // Changing the controlling store from default to user triggers notification.
220 SetExpectPrefChanged();
221 prefs_.Set(name_, *default_value);
222 Mock::VerifyAndClearExpectations(&observer_);
223
[email protected]40a47c162010-09-09 11:14:01224 SetExpectNoNotification();
[email protected]ecde2742010-04-02 17:36:18225 prefs_.Set(name_, *default_value);
226 Mock::VerifyAndClearExpectations(&observer_);
227
228 scoped_ptr<Value> new_value(Value::CreateStringValue(value_));
229 SetExpectPrefChanged();
230 prefs_.Set(name_, *new_value);
231 EXPECT_EQ(value_, prefs_.GetString(name_));
[email protected]ecde2742010-04-02 17:36:18232}
233
234TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
235 prefs_.RegisterDictionaryPref(name_);
[email protected]2fb7dc982010-09-29 12:24:28236 PrefChangeRegistrar registrar;
237 registrar.Init(&prefs_);
238 registrar.Add(name_, &observer_);
[email protected]ecde2742010-04-02 17:36:18239
[email protected]c3b54f372010-09-14 08:25:07240 // Dictionary values are special: setting one to NULL is the same as clearing
241 // the user value, allowing the NULL default to take (or keep) control.
[email protected]ecde2742010-04-02 17:36:18242 SetExpectNoNotification();
243 prefs_.Set(name_, *null_value_);
244 Mock::VerifyAndClearExpectations(&observer_);
245
246 DictionaryValue new_value;
247 new_value.SetString(name_, value_);
248 SetExpectPrefChanged();
249 prefs_.Set(name_, new_value);
250 Mock::VerifyAndClearExpectations(&observer_);
251 DictionaryValue* dict = prefs_.GetMutableDictionary(name_);
252 EXPECT_EQ(1U, dict->size());
[email protected]ddd231e2010-06-29 20:35:19253 std::string out_value;
[email protected]ecde2742010-04-02 17:36:18254 dict->GetString(name_, &out_value);
255 EXPECT_EQ(value_, out_value);
256
[email protected]40a47c162010-09-09 11:14:01257 SetExpectNoNotification();
[email protected]ecde2742010-04-02 17:36:18258 prefs_.Set(name_, new_value);
259 Mock::VerifyAndClearExpectations(&observer_);
260
261 SetExpectPrefChanged();
262 prefs_.Set(name_, *null_value_);
263 Mock::VerifyAndClearExpectations(&observer_);
264 dict = prefs_.GetMutableDictionary(name_);
265 EXPECT_EQ(0U, dict->size());
[email protected]ecde2742010-04-02 17:36:18266}
267
268TEST_F(PrefServiceSetValueTest, SetListValue) {
269 prefs_.RegisterListPref(name_);
[email protected]2fb7dc982010-09-29 12:24:28270 PrefChangeRegistrar registrar;
271 registrar.Init(&prefs_);
272 registrar.Add(name_, &observer_);
[email protected]ecde2742010-04-02 17:36:18273
[email protected]c3b54f372010-09-14 08:25:07274 // List values are special: setting one to NULL is the same as clearing the
275 // user value, allowing the NULL default to take (or keep) control.
[email protected]ecde2742010-04-02 17:36:18276 SetExpectNoNotification();
277 prefs_.Set(name_, *null_value_);
278 Mock::VerifyAndClearExpectations(&observer_);
279
280 ListValue new_value;
281 new_value.Append(Value::CreateStringValue(value_));
282 SetExpectPrefChanged();
283 prefs_.Set(name_, new_value);
284 Mock::VerifyAndClearExpectations(&observer_);
285 ListValue* list = prefs_.GetMutableList(name_);
286 ASSERT_EQ(1U, list->GetSize());
[email protected]ddd231e2010-06-29 20:35:19287 std::string out_value;
[email protected]ecde2742010-04-02 17:36:18288 list->GetString(0, &out_value);
289 EXPECT_EQ(value_, out_value);
290
[email protected]40a47c162010-09-09 11:14:01291 SetExpectNoNotification();
[email protected]ecde2742010-04-02 17:36:18292 prefs_.Set(name_, new_value);
293 Mock::VerifyAndClearExpectations(&observer_);
294
295 SetExpectPrefChanged();
296 prefs_.Set(name_, *null_value_);
297 Mock::VerifyAndClearExpectations(&observer_);
298 list = prefs_.GetMutableList(name_);
299 EXPECT_EQ(0U, list->GetSize());
[email protected]ecde2742010-04-02 17:36:18300}