blob: 461f1083b055cb58064f2b35c1a0fed6149bd31d [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"
11#include "chrome/browser/prefs/pref_value_store.h"
initial.commit09911bf2008-07-26 23:55:2912#include "chrome/common/chrome_paths.h"
[email protected]ecde2742010-04-02 17:36:1813#include "chrome/common/notification_observer_mock.h"
initial.commit09911bf2008-07-26 23:55:2914#include "chrome/common/notification_service.h"
[email protected]bfd04a62009-02-01 18:16:5615#include "chrome/common/notification_type.h"
initial.commit09911bf2008-07-26 23:55:2916#include "chrome/common/pref_names.h"
[email protected]74379bc52010-07-21 13:54:0817#include "chrome/test/testing_pref_service.h"
[email protected]ecde2742010-04-02 17:36:1818#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2919#include "testing/gtest/include/gtest/gtest.h"
20
[email protected]ecde2742010-04-02 17:36:1821using testing::_;
22using testing::Mock;
23using testing::Pointee;
24using testing::Property;
25
initial.commit09911bf2008-07-26 23:55:2926class TestPrefObserver : public NotificationObserver {
27 public:
[email protected]ddd231e2010-06-29 20:35:1928 TestPrefObserver(const PrefService* prefs,
[email protected]57ecc4b2010-08-11 03:02:5129 const std::string& pref_name,
[email protected]ddd231e2010-06-29 20:35:1930 const std::string& new_pref_value)
initial.commit09911bf2008-07-26 23:55:2931 : observer_fired_(false),
32 prefs_(prefs),
33 pref_name_(pref_name),
34 new_pref_value_(new_pref_value) {
35 }
36 virtual ~TestPrefObserver() {}
37
38 virtual void Observe(NotificationType type,
39 const NotificationSource& source,
40 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:5641 EXPECT_EQ(type.value, NotificationType::PREF_CHANGED);
initial.commit09911bf2008-07-26 23:55:2942 PrefService* prefs_in = Source<PrefService>(source).ptr();
43 EXPECT_EQ(prefs_in, prefs_);
[email protected]57ecc4b2010-08-11 03:02:5144 std::string* pref_name_in = Details<std::string>(details).ptr();
initial.commit09911bf2008-07-26 23:55:2945 EXPECT_EQ(*pref_name_in, pref_name_);
[email protected]57ecc4b2010-08-11 03:02:5146 EXPECT_EQ(new_pref_value_, prefs_in->GetString("homepage"));
initial.commit09911bf2008-07-26 23:55:2947 observer_fired_ = true;
48 }
49
50 bool observer_fired() { return observer_fired_; }
51
[email protected]ddd231e2010-06-29 20:35:1952 void Reset(const std::string& new_pref_value) {
initial.commit09911bf2008-07-26 23:55:2953 observer_fired_ = false;
54 new_pref_value_ = new_pref_value;
55 }
56
57 private:
58 bool observer_fired_;
59 const PrefService* prefs_;
[email protected]57ecc4b2010-08-11 03:02:5160 const std::string pref_name_;
[email protected]ddd231e2010-06-29 20:35:1961 std::string new_pref_value_;
initial.commit09911bf2008-07-26 23:55:2962};
63
[email protected]7aa0a962010-04-21 17:24:4264// TODO(port): port this test to POSIX.
65#if defined(OS_WIN)
[email protected]277404c22010-04-22 13:09:4566TEST(PrefServiceTest, LocalizedPrefs) {
[email protected]74379bc52010-07-21 13:54:0867 TestingPrefService prefs;
[email protected]57ecc4b2010-08-11 03:02:5168 const char kBoolean[] = "boolean";
69 const char kInteger[] = "integer";
70 const char kString[] = "string";
[email protected]7aa0a962010-04-21 17:24:4271 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL);
72 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT);
73 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING);
74
75 // The locale default should take preference over the user default.
76 EXPECT_FALSE(prefs.GetBoolean(kBoolean));
77 EXPECT_EQ(1, prefs.GetInteger(kInteger));
[email protected]ddd231e2010-06-29 20:35:1978 EXPECT_EQ("hello", prefs.GetString(kString));
[email protected]7aa0a962010-04-21 17:24:4279
80 prefs.SetBoolean(kBoolean, true);
81 EXPECT_TRUE(prefs.GetBoolean(kBoolean));
82 prefs.SetInteger(kInteger, 5);
83 EXPECT_EQ(5, prefs.GetInteger(kInteger));
[email protected]ddd231e2010-06-29 20:35:1984 prefs.SetString(kString, "foo");
85 EXPECT_EQ("foo", prefs.GetString(kString));
[email protected]7aa0a962010-04-21 17:24:4286}
87#endif
88
[email protected]277404c22010-04-22 13:09:4589TEST(PrefServiceTest, NoObserverFire) {
[email protected]74379bc52010-07-21 13:54:0890 TestingPrefService prefs;
[email protected]7aa0a962010-04-21 17:24:4291
[email protected]57ecc4b2010-08-11 03:02:5192 const char pref_name[] = "homepage";
[email protected]20ce516d2010-06-18 02:20:0493 prefs.RegisterStringPref(pref_name, "");
[email protected]7aa0a962010-04-21 17:24:4294
[email protected]ddd231e2010-06-29 20:35:1995 const std::string new_pref_value("http://www.google.com/");
[email protected]7aa0a962010-04-21 17:24:4296 TestPrefObserver obs(&prefs, pref_name, new_pref_value);
97 prefs.AddPrefObserver(pref_name, &obs);
98 // This should fire the checks in TestPrefObserver::Observe.
99 prefs.SetString(pref_name, new_pref_value);
100
101 // Make sure the observer was actually fired.
102 EXPECT_TRUE(obs.observer_fired());
103
104 // Setting the pref to the same value should not set the pref value a second
105 // time.
106 obs.Reset(new_pref_value);
107 prefs.SetString(pref_name, new_pref_value);
[email protected]40a47c162010-09-09 11:14:01108 EXPECT_FALSE(obs.observer_fired());
[email protected]7aa0a962010-04-21 17:24:42109
110 // Clearing the pref should cause the pref to fire.
[email protected]ddd231e2010-06-29 20:35:19111 obs.Reset("");
[email protected]7aa0a962010-04-21 17:24:42112 prefs.ClearPref(pref_name);
113 EXPECT_TRUE(obs.observer_fired());
114
115 // Clearing the pref again should not cause the pref to fire.
[email protected]ddd231e2010-06-29 20:35:19116 obs.Reset("");
[email protected]7aa0a962010-04-21 17:24:42117 prefs.ClearPref(pref_name);
[email protected]40a47c162010-09-09 11:14:01118 EXPECT_FALSE(obs.observer_fired());
[email protected]7aa0a962010-04-21 17:24:42119
120 // Ok, clean up.
121 prefs.RemovePrefObserver(pref_name, &obs);
122}
123
[email protected]277404c22010-04-22 13:09:45124TEST(PrefServiceTest, HasPrefPath) {
[email protected]74379bc52010-07-21 13:54:08125 TestingPrefService prefs;
[email protected]7aa0a962010-04-21 17:24:42126
[email protected]57ecc4b2010-08-11 03:02:51127 const char path[] = "fake.path";
[email protected]7aa0a962010-04-21 17:24:42128
129 // Shouldn't initially have a path.
130 EXPECT_FALSE(prefs.HasPrefPath(path));
131
132 // Register the path. This doesn't set a value, so the path still shouldn't
133 // exist.
[email protected]20ce516d2010-06-18 02:20:04134 prefs.RegisterStringPref(path, std::string());
[email protected]7aa0a962010-04-21 17:24:42135 EXPECT_FALSE(prefs.HasPrefPath(path));
136
137 // Set a value and make sure we have a path.
[email protected]ddd231e2010-06-29 20:35:19138 prefs.SetString(path, "blah");
[email protected]7aa0a962010-04-21 17:24:42139 EXPECT_TRUE(prefs.HasPrefPath(path));
140}
141
[email protected]277404c22010-04-22 13:09:45142TEST(PrefServiceTest, Observers) {
[email protected]57ecc4b2010-08-11 03:02:51143 const char pref_name[] = "homepage";
[email protected]277404c22010-04-22 13:09:45144
[email protected]74379bc52010-07-21 13:54:08145 TestingPrefService prefs;
[email protected]57ecc4b2010-08-11 03:02:51146 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com"));
[email protected]20ce516d2010-06-18 02:20:04147 prefs.RegisterStringPref(pref_name, "");
[email protected]277404c22010-04-22 13:09:45148
[email protected]ddd231e2010-06-29 20:35:19149 const std::string new_pref_value("http://www.google.com/");
[email protected]277404c22010-04-22 13:09:45150 TestPrefObserver obs(&prefs, pref_name, new_pref_value);
151 prefs.AddPrefObserver(pref_name, &obs);
152 // This should fire the checks in TestPrefObserver::Observe.
153 prefs.SetString(pref_name, new_pref_value);
154
155 // Make sure the tests were actually run.
156 EXPECT_TRUE(obs.observer_fired());
157
158 // Now try adding a second pref observer.
[email protected]ddd231e2010-06-29 20:35:19159 const std::string new_pref_value2("http://www.youtube.com/");
[email protected]277404c22010-04-22 13:09:45160 obs.Reset(new_pref_value2);
161 TestPrefObserver obs2(&prefs, pref_name, new_pref_value2);
162 prefs.AddPrefObserver(pref_name, &obs2);
163 // This should fire the checks in obs and obs2.
164 prefs.SetString(pref_name, new_pref_value2);
165 EXPECT_TRUE(obs.observer_fired());
166 EXPECT_TRUE(obs2.observer_fired());
167
168 // Make sure obs2 still works after removing obs.
169 prefs.RemovePrefObserver(pref_name, &obs);
[email protected]ddd231e2010-06-29 20:35:19170 obs.Reset("");
[email protected]277404c22010-04-22 13:09:45171 obs2.Reset(new_pref_value);
172 // This should only fire the observer in obs2.
173 prefs.SetString(pref_name, new_pref_value);
174 EXPECT_FALSE(obs.observer_fired());
175 EXPECT_TRUE(obs2.observer_fired());
176
177 // Ok, clean up.
178 prefs.RemovePrefObserver(pref_name, &obs2);
179}
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);
214 prefs_.AddPrefObserver(name_, &observer_);
[email protected]c3b54f372010-09-14 08:25:07215 // Changing the controlling store from default to user triggers notification.
216 SetExpectPrefChanged();
217 prefs_.Set(name_, *default_value);
218 Mock::VerifyAndClearExpectations(&observer_);
219
[email protected]40a47c162010-09-09 11:14:01220 SetExpectNoNotification();
[email protected]ecde2742010-04-02 17:36:18221 prefs_.Set(name_, *default_value);
222 Mock::VerifyAndClearExpectations(&observer_);
223
224 scoped_ptr<Value> new_value(Value::CreateStringValue(value_));
225 SetExpectPrefChanged();
226 prefs_.Set(name_, *new_value);
227 EXPECT_EQ(value_, prefs_.GetString(name_));
228
229 prefs_.RemovePrefObserver(name_, &observer_);
230}
231
232TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
233 prefs_.RegisterDictionaryPref(name_);
234 prefs_.AddPrefObserver(name_, &observer_);
235
[email protected]c3b54f372010-09-14 08:25:07236 // Dictionary values are special: setting one to NULL is the same as clearing
237 // the user value, allowing the NULL default to take (or keep) control.
[email protected]ecde2742010-04-02 17:36:18238 SetExpectNoNotification();
239 prefs_.Set(name_, *null_value_);
240 Mock::VerifyAndClearExpectations(&observer_);
241
242 DictionaryValue new_value;
243 new_value.SetString(name_, value_);
244 SetExpectPrefChanged();
245 prefs_.Set(name_, new_value);
246 Mock::VerifyAndClearExpectations(&observer_);
247 DictionaryValue* dict = prefs_.GetMutableDictionary(name_);
248 EXPECT_EQ(1U, dict->size());
[email protected]ddd231e2010-06-29 20:35:19249 std::string out_value;
[email protected]ecde2742010-04-02 17:36:18250 dict->GetString(name_, &out_value);
251 EXPECT_EQ(value_, out_value);
252
[email protected]40a47c162010-09-09 11:14:01253 SetExpectNoNotification();
[email protected]ecde2742010-04-02 17:36:18254 prefs_.Set(name_, new_value);
255 Mock::VerifyAndClearExpectations(&observer_);
256
257 SetExpectPrefChanged();
258 prefs_.Set(name_, *null_value_);
259 Mock::VerifyAndClearExpectations(&observer_);
260 dict = prefs_.GetMutableDictionary(name_);
261 EXPECT_EQ(0U, dict->size());
262
263 prefs_.RemovePrefObserver(name_, &observer_);
264}
265
266TEST_F(PrefServiceSetValueTest, SetListValue) {
267 prefs_.RegisterListPref(name_);
268 prefs_.AddPrefObserver(name_, &observer_);
269
[email protected]c3b54f372010-09-14 08:25:07270 // List values are special: setting one to NULL is the same as clearing the
271 // user value, allowing the NULL default to take (or keep) control.
[email protected]ecde2742010-04-02 17:36:18272 SetExpectNoNotification();
273 prefs_.Set(name_, *null_value_);
274 Mock::VerifyAndClearExpectations(&observer_);
275
276 ListValue new_value;
277 new_value.Append(Value::CreateStringValue(value_));
278 SetExpectPrefChanged();
279 prefs_.Set(name_, new_value);
280 Mock::VerifyAndClearExpectations(&observer_);
281 ListValue* list = prefs_.GetMutableList(name_);
282 ASSERT_EQ(1U, list->GetSize());
[email protected]ddd231e2010-06-29 20:35:19283 std::string out_value;
[email protected]ecde2742010-04-02 17:36:18284 list->GetString(0, &out_value);
285 EXPECT_EQ(value_, out_value);
286
[email protected]40a47c162010-09-09 11:14:01287 SetExpectNoNotification();
[email protected]ecde2742010-04-02 17:36:18288 prefs_.Set(name_, new_value);
289 Mock::VerifyAndClearExpectations(&observer_);
290
291 SetExpectPrefChanged();
292 prefs_.Set(name_, *null_value_);
293 Mock::VerifyAndClearExpectations(&observer_);
294 list = prefs_.GetMutableList(name_);
295 EXPECT_EQ(0U, list->GetSize());
296
297 prefs_.RemovePrefObserver(name_, &observer_);
298}