blob: fc801d6892010f6d7e5652aa24b292587cee4c12 [file] [log] [blame]
[email protected]4dad9ad82009-11-25 20:47:521// Copyright (c) 2009 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]277404c22010-04-22 13:09:4510#include "chrome/browser/dummy_pref_store.h"
[email protected]052313b2010-02-19 09:43:0811#include "chrome/browser/pref_service.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]ecde2742010-04-02 17:36:1817#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2918#include "testing/gtest/include/gtest/gtest.h"
19
[email protected]ecde2742010-04-02 17:36:1820using testing::_;
21using testing::Mock;
22using testing::Pointee;
23using testing::Property;
24
initial.commit09911bf2008-07-26 23:55:2925class TestPrefObserver : public NotificationObserver {
26 public:
27 TestPrefObserver(const PrefService* prefs, const std::wstring& pref_name,
28 const std::wstring& new_pref_value)
29 : observer_fired_(false),
30 prefs_(prefs),
31 pref_name_(pref_name),
32 new_pref_value_(new_pref_value) {
33 }
34 virtual ~TestPrefObserver() {}
35
36 virtual void Observe(NotificationType type,
37 const NotificationSource& source,
38 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:5639 EXPECT_EQ(type.value, NotificationType::PREF_CHANGED);
initial.commit09911bf2008-07-26 23:55:2940 PrefService* prefs_in = Source<PrefService>(source).ptr();
41 EXPECT_EQ(prefs_in, prefs_);
42 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
43 EXPECT_EQ(*pref_name_in, pref_name_);
44 EXPECT_EQ(new_pref_value_, prefs_in->GetString(L"homepage"));
45 observer_fired_ = true;
46 }
47
48 bool observer_fired() { return observer_fired_; }
49
50 void Reset(const std::wstring& new_pref_value) {
51 observer_fired_ = false;
52 new_pref_value_ = new_pref_value;
53 }
54
55 private:
56 bool observer_fired_;
57 const PrefService* prefs_;
58 const std::wstring pref_name_;
59 std::wstring new_pref_value_;
60};
61
[email protected]7aa0a962010-04-21 17:24:4262// TODO(port): port this test to POSIX.
63#if defined(OS_WIN)
[email protected]277404c22010-04-22 13:09:4564TEST(PrefServiceTest, LocalizedPrefs) {
65 PrefService prefs(new DummyPrefStore());
[email protected]7aa0a962010-04-21 17:24:4266 const wchar_t kBoolean[] = L"boolean";
67 const wchar_t kInteger[] = L"integer";
68 const wchar_t kString[] = L"string";
69 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL);
70 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT);
71 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING);
72
73 // The locale default should take preference over the user default.
74 EXPECT_FALSE(prefs.GetBoolean(kBoolean));
75 EXPECT_EQ(1, prefs.GetInteger(kInteger));
76 EXPECT_EQ(L"hello", prefs.GetString(kString));
77
78 prefs.SetBoolean(kBoolean, true);
79 EXPECT_TRUE(prefs.GetBoolean(kBoolean));
80 prefs.SetInteger(kInteger, 5);
81 EXPECT_EQ(5, prefs.GetInteger(kInteger));
82 prefs.SetString(kString, L"foo");
83 EXPECT_EQ(L"foo", prefs.GetString(kString));
84}
85#endif
86
[email protected]277404c22010-04-22 13:09:4587TEST(PrefServiceTest, NoObserverFire) {
88 PrefService prefs(new DummyPrefStore());
[email protected]7aa0a962010-04-21 17:24:4289
90 const wchar_t pref_name[] = L"homepage";
91 prefs.RegisterStringPref(pref_name, L"");
92
93 const std::wstring new_pref_value(L"http://www.google.com/");
94 TestPrefObserver obs(&prefs, pref_name, new_pref_value);
95 prefs.AddPrefObserver(pref_name, &obs);
96 // This should fire the checks in TestPrefObserver::Observe.
97 prefs.SetString(pref_name, new_pref_value);
98
99 // Make sure the observer was actually fired.
100 EXPECT_TRUE(obs.observer_fired());
101
102 // Setting the pref to the same value should not set the pref value a second
103 // time.
104 obs.Reset(new_pref_value);
105 prefs.SetString(pref_name, new_pref_value);
106 EXPECT_FALSE(obs.observer_fired());
107
108 // Clearing the pref should cause the pref to fire.
109 obs.Reset(L"");
110 prefs.ClearPref(pref_name);
111 EXPECT_TRUE(obs.observer_fired());
112
113 // Clearing the pref again should not cause the pref to fire.
114 obs.Reset(L"");
115 prefs.ClearPref(pref_name);
116 EXPECT_FALSE(obs.observer_fired());
117
118 // Ok, clean up.
119 prefs.RemovePrefObserver(pref_name, &obs);
120}
121
[email protected]277404c22010-04-22 13:09:45122TEST(PrefServiceTest, HasPrefPath) {
123 PrefService prefs(new DummyPrefStore());
[email protected]7aa0a962010-04-21 17:24:42124
125 const wchar_t path[] = L"fake.path";
126
127 // Shouldn't initially have a path.
128 EXPECT_FALSE(prefs.HasPrefPath(path));
129
130 // Register the path. This doesn't set a value, so the path still shouldn't
131 // exist.
132 prefs.RegisterStringPref(path, std::wstring());
133 EXPECT_FALSE(prefs.HasPrefPath(path));
134
135 // Set a value and make sure we have a path.
136 prefs.SetString(path, L"blah");
137 EXPECT_TRUE(prefs.HasPrefPath(path));
138}
139
[email protected]277404c22010-04-22 13:09:45140TEST(PrefServiceTest, Observers) {
141 const wchar_t pref_name[] = L"homepage";
142
143 DictionaryValue* dict = new DictionaryValue();
144 dict->SetString(pref_name, std::wstring(L"http://www.cnn.com"));
145 DummyPrefStore* pref_store = new DummyPrefStore();
146 pref_store->SetPrefs(dict);
147 PrefService prefs(pref_store);
148 prefs.RegisterStringPref(pref_name, L"");
149
150 const std::wstring new_pref_value(L"http://www.google.com/");
151 TestPrefObserver obs(&prefs, pref_name, new_pref_value);
152 prefs.AddPrefObserver(pref_name, &obs);
153 // This should fire the checks in TestPrefObserver::Observe.
154 prefs.SetString(pref_name, new_pref_value);
155
156 // Make sure the tests were actually run.
157 EXPECT_TRUE(obs.observer_fired());
158
159 // Now try adding a second pref observer.
160 const std::wstring new_pref_value2(L"http://www.youtube.com/");
161 obs.Reset(new_pref_value2);
162 TestPrefObserver obs2(&prefs, pref_name, new_pref_value2);
163 prefs.AddPrefObserver(pref_name, &obs2);
164 // This should fire the checks in obs and obs2.
165 prefs.SetString(pref_name, new_pref_value2);
166 EXPECT_TRUE(obs.observer_fired());
167 EXPECT_TRUE(obs2.observer_fired());
168
169 // Make sure obs2 still works after removing obs.
170 prefs.RemovePrefObserver(pref_name, &obs);
171 obs.Reset(L"");
172 obs2.Reset(new_pref_value);
173 // This should only fire the observer in obs2.
174 prefs.SetString(pref_name, new_pref_value);
175 EXPECT_FALSE(obs.observer_fired());
176 EXPECT_TRUE(obs2.observer_fired());
177
178 // Ok, clean up.
179 prefs.RemovePrefObserver(pref_name, &obs2);
180}
181
[email protected]ecde2742010-04-02 17:36:18182class PrefServiceSetValueTest : public testing::Test {
183 protected:
184 static const wchar_t name_[];
185 static const wchar_t value_[];
186
187 PrefServiceSetValueTest()
[email protected]277404c22010-04-22 13:09:45188 : prefs_(new DummyPrefStore()),
[email protected]ecde2742010-04-02 17:36:18189 name_string_(name_),
190 null_value_(Value::CreateNullValue()) {}
191
192 void SetExpectNoNotification() {
193 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0);
194 }
195
196 void SetExpectPrefChanged() {
197 EXPECT_CALL(observer_,
198 Observe(NotificationType(NotificationType::PREF_CHANGED), _,
199 Property(&Details<std::wstring>::ptr,
200 Pointee(name_string_))));
201 }
202
203 PrefService prefs_;
204 std::wstring name_string_;
205 scoped_ptr<Value> null_value_;
206 NotificationObserverMock observer_;
207};
208const wchar_t PrefServiceSetValueTest::name_[] = L"name";
209const wchar_t PrefServiceSetValueTest::value_[] = L"value";
210
211TEST_F(PrefServiceSetValueTest, SetStringValue) {
212 const wchar_t default_string[] = L"default";
213 scoped_ptr<Value> default_value(Value::CreateStringValue(default_string));
214 prefs_.RegisterStringPref(name_, default_string);
215 prefs_.AddPrefObserver(name_, &observer_);
216 SetExpectNoNotification();
217 prefs_.Set(name_, *default_value);
218 Mock::VerifyAndClearExpectations(&observer_);
219
220 scoped_ptr<Value> new_value(Value::CreateStringValue(value_));
221 SetExpectPrefChanged();
222 prefs_.Set(name_, *new_value);
223 EXPECT_EQ(value_, prefs_.GetString(name_));
224
225 prefs_.RemovePrefObserver(name_, &observer_);
226}
227
228TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
229 prefs_.RegisterDictionaryPref(name_);
230 prefs_.AddPrefObserver(name_, &observer_);
231
232 SetExpectNoNotification();
233 prefs_.Set(name_, *null_value_);
234 Mock::VerifyAndClearExpectations(&observer_);
235
236 DictionaryValue new_value;
237 new_value.SetString(name_, value_);
238 SetExpectPrefChanged();
239 prefs_.Set(name_, new_value);
240 Mock::VerifyAndClearExpectations(&observer_);
241 DictionaryValue* dict = prefs_.GetMutableDictionary(name_);
242 EXPECT_EQ(1U, dict->size());
243 std::wstring out_value;
244 dict->GetString(name_, &out_value);
245 EXPECT_EQ(value_, out_value);
246
247 SetExpectNoNotification();
248 prefs_.Set(name_, new_value);
249 Mock::VerifyAndClearExpectations(&observer_);
250
251 SetExpectPrefChanged();
252 prefs_.Set(name_, *null_value_);
253 Mock::VerifyAndClearExpectations(&observer_);
254 dict = prefs_.GetMutableDictionary(name_);
255 EXPECT_EQ(0U, dict->size());
256
257 prefs_.RemovePrefObserver(name_, &observer_);
258}
259
260TEST_F(PrefServiceSetValueTest, SetListValue) {
261 prefs_.RegisterListPref(name_);
262 prefs_.AddPrefObserver(name_, &observer_);
263
264 SetExpectNoNotification();
265 prefs_.Set(name_, *null_value_);
266 Mock::VerifyAndClearExpectations(&observer_);
267
268 ListValue new_value;
269 new_value.Append(Value::CreateStringValue(value_));
270 SetExpectPrefChanged();
271 prefs_.Set(name_, new_value);
272 Mock::VerifyAndClearExpectations(&observer_);
273 ListValue* list = prefs_.GetMutableList(name_);
274 ASSERT_EQ(1U, list->GetSize());
275 std::wstring out_value;
276 list->GetString(0, &out_value);
277 EXPECT_EQ(value_, out_value);
278
279 SetExpectNoNotification();
280 prefs_.Set(name_, new_value);
281 Mock::VerifyAndClearExpectations(&observer_);
282
283 SetExpectPrefChanged();
284 prefs_.Set(name_, *null_value_);
285 Mock::VerifyAndClearExpectations(&observer_);
286 list = prefs_.GetMutableList(name_);
287 EXPECT_EQ(0U, list->GetSize());
288
289 prefs_.RemovePrefObserver(name_, &observer_);
290}