blob: d4d7e02c9782396701137fdc0a8d8a78db7d4c99 [file] [log] [blame]
[email protected]b3841c502011-03-09 01:21:311// Copyright (c) 2011 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]1ab137b2013-03-21 03:33:185#include "base/prefs/pref_member.h"
[email protected]68d9d352011-02-21 16:35:046
[email protected]bebe69432011-09-28 18:36:457#include "base/bind.h"
[email protected]495cad92013-07-18 08:12:408#include "base/message_loop/message_loop.h"
[email protected]3853a4c2013-02-11 17:15:579#include "base/prefs/pref_registry_simple.h"
[email protected]e5ba874f2013-02-14 17:20:1910#include "base/prefs/testing_pref_service.h"
[email protected]909f1f942012-12-13 12:04:1011#include "base/synchronization/waitable_event.h"
12#include "base/threading/thread.h"
initial.commit09911bf2008-07-26 23:55:2913#include "testing/gtest/include/gtest/gtest.h"
[email protected]631bb742011-11-02 11:29:3914
initial.commit09911bf2008-07-26 23:55:2915namespace {
16
[email protected]68d9d352011-02-21 16:35:0417const char kBoolPref[] = "bool";
18const char kIntPref[] = "int";
19const char kDoublePref[] = "double";
20const char kStringPref[] = "string";
[email protected]7bbf4882012-10-23 17:44:0521const char kStringListPref[] = "string_list";
initial.commit09911bf2008-07-26 23:55:2922
[email protected]b1de2c72013-02-06 02:45:4723void RegisterTestPrefs(PrefRegistrySimple* registry) {
24 registry->RegisterBooleanPref(kBoolPref, false);
25 registry->RegisterIntegerPref(kIntPref, 0);
26 registry->RegisterDoublePref(kDoublePref, 0.0);
27 registry->RegisterStringPref(kStringPref, "default");
[email protected]a43a667b2013-06-14 17:56:0828 registry->RegisterListPref(kStringListPref, new base::ListValue());
initial.commit09911bf2008-07-26 23:55:2929}
30
[email protected]909f1f942012-12-13 12:04:1031class GetPrefValueHelper
32 : public base::RefCountedThreadSafe<GetPrefValueHelper> {
[email protected]68d9d352011-02-21 16:35:0433 public:
[email protected]909f1f942012-12-13 12:04:1034 GetPrefValueHelper() : value_(false), pref_thread_("pref thread") {
35 pref_thread_.Start();
36 }
[email protected]68d9d352011-02-21 16:35:0437
38 void Init(const char* pref_name, PrefService* prefs) {
[email protected]96a5c342012-12-04 18:14:0239 pref_.Init(pref_name, prefs);
[email protected]909f1f942012-12-13 12:04:1040 pref_.MoveToThread(pref_thread_.message_loop_proxy());
[email protected]68d9d352011-02-21 16:35:0441 }
42
[email protected]c710b792012-10-31 00:03:0743 void Destroy() {
44 pref_.Destroy();
45 }
46
[email protected]909f1f942012-12-13 12:04:1047 void FetchValue() {
48 base::WaitableEvent event(true, false);
49 ASSERT_TRUE(
50 pref_thread_.message_loop_proxy()->PostTask(
51 FROM_HERE,
52 base::Bind(&GetPrefValueHelper::GetPrefValue, this, &event)));
53 event.Wait();
[email protected]68d9d352011-02-21 16:35:0454 }
55
[email protected]8dda17e4a2012-12-21 14:08:0556 // The thread must be stopped on the main thread. GetPrefValueHelper being
57 // ref-counted, the destructor can be called from any thread.
58 void StopThread() {
59 pref_thread_.Stop();
60 }
61
[email protected]68d9d352011-02-21 16:35:0462 bool value() { return value_; }
63
64 private:
[email protected]909f1f942012-12-13 12:04:1065 friend class base::RefCountedThreadSafe<GetPrefValueHelper>;
66 ~GetPrefValueHelper() {}
[email protected]68d9d352011-02-21 16:35:0467
[email protected]909f1f942012-12-13 12:04:1068 void GetPrefValue(base::WaitableEvent* event) {
[email protected]68d9d352011-02-21 16:35:0469 value_ = pref_.GetValue();
[email protected]909f1f942012-12-13 12:04:1070 event->Signal();
[email protected]68d9d352011-02-21 16:35:0471 }
72
73 BooleanPrefMember pref_;
74 bool value_;
[email protected]909f1f942012-12-13 12:04:1075
76 base::Thread pref_thread_; // The thread |pref_| runs on.
[email protected]68d9d352011-02-21 16:35:0477};
78
[email protected]96a5c342012-12-04 18:14:0279class PrefMemberTestClass {
initial.commit09911bf2008-07-26 23:55:2980 public:
[email protected]4a3dab22009-11-11 17:36:5081 explicit PrefMemberTestClass(PrefService* prefs)
82 : observe_cnt_(0), prefs_(prefs) {
[email protected]96a5c342012-12-04 18:14:0283 str_.Init(kStringPref, prefs,
84 base::Bind(&PrefMemberTestClass::OnPreferenceChanged,
85 base::Unretained(this)));
initial.commit09911bf2008-07-26 23:55:2986 }
87
[email protected]96a5c342012-12-04 18:14:0288 void OnPreferenceChanged(const std::string& pref_name) {
[email protected]a6a7ced2012-11-01 17:24:1889 EXPECT_EQ(pref_name, kStringPref);
initial.commit09911bf2008-07-26 23:55:2990 EXPECT_EQ(str_.GetValue(), prefs_->GetString(kStringPref));
91 ++observe_cnt_;
92 }
93
94 StringPrefMember str_;
95 int observe_cnt_;
96
97 private:
98 PrefService* prefs_;
99};
100
101} // anonymous namespace
102
103TEST(PrefMemberTest, BasicGetAndSet) {
[email protected]5b199522012-12-22 17:24:44104 TestingPrefServiceSimple prefs;
[email protected]b1de2c72013-02-06 02:45:47105 RegisterTestPrefs(prefs.registry());
initial.commit09911bf2008-07-26 23:55:29106
107 // Test bool
108 BooleanPrefMember boolean;
[email protected]96a5c342012-12-04 18:14:02109 boolean.Init(kBoolPref, &prefs);
initial.commit09911bf2008-07-26 23:55:29110
111 // Check the defaults
112 EXPECT_FALSE(prefs.GetBoolean(kBoolPref));
113 EXPECT_FALSE(boolean.GetValue());
114 EXPECT_FALSE(*boolean);
115
116 // Try changing through the member variable.
117 boolean.SetValue(true);
118 EXPECT_TRUE(boolean.GetValue());
119 EXPECT_TRUE(prefs.GetBoolean(kBoolPref));
120 EXPECT_TRUE(*boolean);
121
122 // Try changing back through the pref.
123 prefs.SetBoolean(kBoolPref, false);
124 EXPECT_FALSE(prefs.GetBoolean(kBoolPref));
125 EXPECT_FALSE(boolean.GetValue());
126 EXPECT_FALSE(*boolean);
127
128 // Test int
129 IntegerPrefMember integer;
[email protected]96a5c342012-12-04 18:14:02130 integer.Init(kIntPref, &prefs);
initial.commit09911bf2008-07-26 23:55:29131
132 // Check the defaults
133 EXPECT_EQ(0, prefs.GetInteger(kIntPref));
134 EXPECT_EQ(0, integer.GetValue());
135 EXPECT_EQ(0, *integer);
136
137 // Try changing through the member variable.
138 integer.SetValue(5);
139 EXPECT_EQ(5, integer.GetValue());
140 EXPECT_EQ(5, prefs.GetInteger(kIntPref));
141 EXPECT_EQ(5, *integer);
142
143 // Try changing back through the pref.
144 prefs.SetInteger(kIntPref, 2);
145 EXPECT_EQ(2, prefs.GetInteger(kIntPref));
146 EXPECT_EQ(2, integer.GetValue());
147 EXPECT_EQ(2, *integer);
148
[email protected]fb534c92011-02-01 01:02:07149 // Test double
150 DoublePrefMember double_member;
[email protected]96a5c342012-12-04 18:14:02151 double_member.Init(kDoublePref, &prefs);
initial.commit09911bf2008-07-26 23:55:29152
153 // Check the defaults
[email protected]fb534c92011-02-01 01:02:07154 EXPECT_EQ(0.0, prefs.GetDouble(kDoublePref));
155 EXPECT_EQ(0.0, double_member.GetValue());
156 EXPECT_EQ(0.0, *double_member);
initial.commit09911bf2008-07-26 23:55:29157
158 // Try changing through the member variable.
[email protected]fb534c92011-02-01 01:02:07159 double_member.SetValue(1.0);
160 EXPECT_EQ(1.0, double_member.GetValue());
161 EXPECT_EQ(1.0, prefs.GetDouble(kDoublePref));
162 EXPECT_EQ(1.0, *double_member);
initial.commit09911bf2008-07-26 23:55:29163
164 // Try changing back through the pref.
[email protected]fb534c92011-02-01 01:02:07165 prefs.SetDouble(kDoublePref, 3.0);
166 EXPECT_EQ(3.0, prefs.GetDouble(kDoublePref));
167 EXPECT_EQ(3.0, double_member.GetValue());
168 EXPECT_EQ(3.0, *double_member);
initial.commit09911bf2008-07-26 23:55:29169
170 // Test string
171 StringPrefMember string;
[email protected]96a5c342012-12-04 18:14:02172 string.Init(kStringPref, &prefs);
initial.commit09911bf2008-07-26 23:55:29173
174 // Check the defaults
[email protected]ddd231e2010-06-29 20:35:19175 EXPECT_EQ("default", prefs.GetString(kStringPref));
176 EXPECT_EQ("default", string.GetValue());
177 EXPECT_EQ("default", *string);
initial.commit09911bf2008-07-26 23:55:29178
179 // Try changing through the member variable.
[email protected]ddd231e2010-06-29 20:35:19180 string.SetValue("foo");
181 EXPECT_EQ("foo", string.GetValue());
182 EXPECT_EQ("foo", prefs.GetString(kStringPref));
183 EXPECT_EQ("foo", *string);
initial.commit09911bf2008-07-26 23:55:29184
185 // Try changing back through the pref.
[email protected]ddd231e2010-06-29 20:35:19186 prefs.SetString(kStringPref, "bar");
187 EXPECT_EQ("bar", prefs.GetString(kStringPref));
188 EXPECT_EQ("bar", string.GetValue());
189 EXPECT_EQ("bar", *string);
[email protected]7bbf4882012-10-23 17:44:05190
191 // Test string list
[email protected]a43a667b2013-06-14 17:56:08192 base::ListValue expected_list;
[email protected]7bbf4882012-10-23 17:44:05193 std::vector<std::string> expected_vector;
194 StringListPrefMember string_list;
[email protected]96a5c342012-12-04 18:14:02195 string_list.Init(kStringListPref, &prefs);
[email protected]7bbf4882012-10-23 17:44:05196
197 // Check the defaults
198 EXPECT_TRUE(expected_list.Equals(prefs.GetList(kStringListPref)));
199 EXPECT_EQ(expected_vector, string_list.GetValue());
200 EXPECT_EQ(expected_vector, *string_list);
201
202 // Try changing through the pref member.
203 expected_list.AppendString("foo");
204 expected_vector.push_back("foo");
205 string_list.SetValue(expected_vector);
206
207 EXPECT_TRUE(expected_list.Equals(prefs.GetList(kStringListPref)));
208 EXPECT_EQ(expected_vector, string_list.GetValue());
209 EXPECT_EQ(expected_vector, *string_list);
210
211 // Try adding through the pref.
212 expected_list.AppendString("bar");
213 expected_vector.push_back("bar");
214 prefs.Set(kStringListPref, expected_list);
215
216 EXPECT_TRUE(expected_list.Equals(prefs.GetList(kStringListPref)));
217 EXPECT_EQ(expected_vector, string_list.GetValue());
218 EXPECT_EQ(expected_vector, *string_list);
219
220 // Try removing through the pref.
221 expected_list.Remove(0, NULL);
222 expected_vector.erase(expected_vector.begin());
223 prefs.Set(kStringListPref, expected_list);
224
225 EXPECT_TRUE(expected_list.Equals(prefs.GetList(kStringListPref)));
226 EXPECT_EQ(expected_vector, string_list.GetValue());
227 EXPECT_EQ(expected_vector, *string_list);
228}
229
230TEST(PrefMemberTest, InvalidList) {
231 // Set the vector to an initial good value.
232 std::vector<std::string> expected_vector;
233 expected_vector.push_back("foo");
234
235 // Try to add a valid list first.
[email protected]a43a667b2013-06-14 17:56:08236 base::ListValue list;
[email protected]7bbf4882012-10-23 17:44:05237 list.AppendString("foo");
238 std::vector<std::string> vector;
239 EXPECT_TRUE(subtle::PrefMemberVectorStringUpdate(list, &vector));
240 EXPECT_EQ(expected_vector, vector);
241
242 // Now try to add an invalid list. |vector| should not be changed.
243 list.AppendInteger(0);
244 EXPECT_FALSE(subtle::PrefMemberVectorStringUpdate(list, &vector));
245 EXPECT_EQ(expected_vector, vector);
initial.commit09911bf2008-07-26 23:55:29246}
247
248TEST(PrefMemberTest, TwoPrefs) {
[email protected]fb534c92011-02-01 01:02:07249 // Make sure two DoublePrefMembers stay in sync.
[email protected]5b199522012-12-22 17:24:44250 TestingPrefServiceSimple prefs;
[email protected]b1de2c72013-02-06 02:45:47251 RegisterTestPrefs(prefs.registry());
initial.commit09911bf2008-07-26 23:55:29252
[email protected]fb534c92011-02-01 01:02:07253 DoublePrefMember pref1;
[email protected]96a5c342012-12-04 18:14:02254 pref1.Init(kDoublePref, &prefs);
[email protected]fb534c92011-02-01 01:02:07255 DoublePrefMember pref2;
[email protected]96a5c342012-12-04 18:14:02256 pref2.Init(kDoublePref, &prefs);
initial.commit09911bf2008-07-26 23:55:29257
258 pref1.SetValue(2.3);
259 EXPECT_EQ(2.3, *pref2);
260
261 pref2.SetValue(3.5);
262 EXPECT_EQ(3.5, *pref1);
263
[email protected]fb534c92011-02-01 01:02:07264 prefs.SetDouble(kDoublePref, 4.2);
initial.commit09911bf2008-07-26 23:55:29265 EXPECT_EQ(4.2, *pref1);
266 EXPECT_EQ(4.2, *pref2);
267}
268
269TEST(PrefMemberTest, Observer) {
[email protected]5b199522012-12-22 17:24:44270 TestingPrefServiceSimple prefs;
[email protected]b1de2c72013-02-06 02:45:47271 RegisterTestPrefs(prefs.registry());
initial.commit09911bf2008-07-26 23:55:29272
273 PrefMemberTestClass test_obj(&prefs);
[email protected]ddd231e2010-06-29 20:35:19274 EXPECT_EQ("default", *test_obj.str_);
initial.commit09911bf2008-07-26 23:55:29275
276 // Calling SetValue should not fire the observer.
[email protected]ddd231e2010-06-29 20:35:19277 test_obj.str_.SetValue("hello");
initial.commit09911bf2008-07-26 23:55:29278 EXPECT_EQ(0, test_obj.observe_cnt_);
[email protected]ddd231e2010-06-29 20:35:19279 EXPECT_EQ("hello", prefs.GetString(kStringPref));
initial.commit09911bf2008-07-26 23:55:29280
281 // Changing the pref does fire the observer.
[email protected]ddd231e2010-06-29 20:35:19282 prefs.SetString(kStringPref, "world");
initial.commit09911bf2008-07-26 23:55:29283 EXPECT_EQ(1, test_obj.observe_cnt_);
[email protected]ddd231e2010-06-29 20:35:19284 EXPECT_EQ("world", *(test_obj.str_));
initial.commit09911bf2008-07-26 23:55:29285
286 // Not changing the value should not fire the observer.
[email protected]ddd231e2010-06-29 20:35:19287 prefs.SetString(kStringPref, "world");
[email protected]40a47c162010-09-09 11:14:01288 EXPECT_EQ(1, test_obj.observe_cnt_);
[email protected]ddd231e2010-06-29 20:35:19289 EXPECT_EQ("world", *(test_obj.str_));
initial.commit09911bf2008-07-26 23:55:29290
[email protected]ddd231e2010-06-29 20:35:19291 prefs.SetString(kStringPref, "hello");
[email protected]40a47c162010-09-09 11:14:01292 EXPECT_EQ(2, test_obj.observe_cnt_);
[email protected]ddd231e2010-06-29 20:35:19293 EXPECT_EQ("hello", prefs.GetString(kStringPref));
initial.commit09911bf2008-07-26 23:55:29294}
295
296TEST(PrefMemberTest, NoInit) {
297 // Make sure not calling Init on a PrefMember doesn't cause problems.
298 IntegerPrefMember pref;
299}
[email protected]68d9d352011-02-21 16:35:04300
[email protected]27252a752011-03-28 08:35:17301TEST(PrefMemberTest, MoveToThread) {
[email protected]5b199522012-12-22 17:24:44302 TestingPrefServiceSimple prefs;
[email protected]909f1f942012-12-13 12:04:10303 scoped_refptr<GetPrefValueHelper> helper(new GetPrefValueHelper());
[email protected]b1de2c72013-02-06 02:45:47304 RegisterTestPrefs(prefs.registry());
[email protected]909f1f942012-12-13 12:04:10305 helper->Init(kBoolPref, &prefs);
[email protected]68d9d352011-02-21 16:35:04306
[email protected]909f1f942012-12-13 12:04:10307 helper->FetchValue();
308 EXPECT_FALSE(helper->value());
[email protected]68d9d352011-02-21 16:35:04309
310 prefs.SetBoolean(kBoolPref, true);
311
[email protected]909f1f942012-12-13 12:04:10312 helper->FetchValue();
313 EXPECT_TRUE(helper->value());
[email protected]c710b792012-10-31 00:03:07314
[email protected]909f1f942012-12-13 12:04:10315 helper->Destroy();
[email protected]c710b792012-10-31 00:03:07316
[email protected]909f1f942012-12-13 12:04:10317 helper->FetchValue();
318 EXPECT_TRUE(helper->value());
[email protected]8dda17e4a2012-12-21 14:08:05319
320 helper->StopThread();
[email protected]68d9d352011-02-21 16:35:04321}