OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/prefs/public/pref_member.h" | 5 #include "base/prefs/public/pref_member.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
11 #include "chrome/test/base/testing_pref_service.h" | 11 #include "chrome/test/base/testing_pref_service.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 const char kBoolPref[] = "bool"; | 16 const char kBoolPref[] = "bool"; |
17 const char kIntPref[] = "int"; | 17 const char kIntPref[] = "int"; |
18 const char kDoublePref[] = "double"; | 18 const char kDoublePref[] = "double"; |
19 const char kStringPref[] = "string"; | 19 const char kStringPref[] = "string"; |
20 const char kStringListPref[] = "string_list"; | 20 const char kStringListPref[] = "string_list"; |
21 | 21 |
22 void RegisterTestPrefs(PrefService* prefs) { | 22 void RegisterTestPrefs(PrefServiceSimple* prefs) { |
23 prefs->RegisterBooleanPref(kBoolPref, false, PrefService::UNSYNCABLE_PREF); | 23 prefs->RegisterBooleanPref(kBoolPref, false); |
24 prefs->RegisterIntegerPref(kIntPref, 0, PrefService::UNSYNCABLE_PREF); | 24 prefs->RegisterIntegerPref(kIntPref, 0); |
25 prefs->RegisterDoublePref(kDoublePref, 0.0, PrefService::UNSYNCABLE_PREF); | 25 prefs->RegisterDoublePref(kDoublePref, 0.0); |
26 prefs->RegisterStringPref(kStringPref, | 26 prefs->RegisterStringPref(kStringPref, "default"); |
27 "default", | 27 prefs->RegisterListPref(kStringListPref, new ListValue()); |
28 PrefService::UNSYNCABLE_PREF); | |
29 prefs->RegisterListPref(kStringListPref, | |
30 new ListValue(), | |
31 PrefService::UNSYNCABLE_PREF); | |
32 } | 28 } |
33 | 29 |
34 class GetPrefValueHelper | 30 class GetPrefValueHelper |
35 : public base::RefCountedThreadSafe<GetPrefValueHelper> { | 31 : public base::RefCountedThreadSafe<GetPrefValueHelper> { |
36 public: | 32 public: |
37 GetPrefValueHelper() : value_(false), pref_thread_("pref thread") { | 33 GetPrefValueHelper() : value_(false), pref_thread_("pref thread") { |
38 pref_thread_.Start(); | 34 pref_thread_.Start(); |
39 } | 35 } |
40 | 36 |
41 void Init(const char* pref_name, PrefService* prefs) { | 37 void Init(const char* pref_name, PrefService* prefs) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 StringPrefMember str_; | 93 StringPrefMember str_; |
98 int observe_cnt_; | 94 int observe_cnt_; |
99 | 95 |
100 private: | 96 private: |
101 PrefService* prefs_; | 97 PrefService* prefs_; |
102 }; | 98 }; |
103 | 99 |
104 } // anonymous namespace | 100 } // anonymous namespace |
105 | 101 |
106 TEST(PrefMemberTest, BasicGetAndSet) { | 102 TEST(PrefMemberTest, BasicGetAndSet) { |
107 TestingPrefService prefs; | 103 TestingPrefServiceSimple prefs; |
108 RegisterTestPrefs(&prefs); | 104 RegisterTestPrefs(&prefs); |
109 | 105 |
110 // Test bool | 106 // Test bool |
111 BooleanPrefMember boolean; | 107 BooleanPrefMember boolean; |
112 boolean.Init(kBoolPref, &prefs); | 108 boolean.Init(kBoolPref, &prefs); |
113 | 109 |
114 // Check the defaults | 110 // Check the defaults |
115 EXPECT_FALSE(prefs.GetBoolean(kBoolPref)); | 111 EXPECT_FALSE(prefs.GetBoolean(kBoolPref)); |
116 EXPECT_FALSE(boolean.GetValue()); | 112 EXPECT_FALSE(boolean.GetValue()); |
117 EXPECT_FALSE(*boolean); | 113 EXPECT_FALSE(*boolean); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 EXPECT_EQ(expected_vector, vector); | 239 EXPECT_EQ(expected_vector, vector); |
244 | 240 |
245 // Now try to add an invalid list. |vector| should not be changed. | 241 // Now try to add an invalid list. |vector| should not be changed. |
246 list.AppendInteger(0); | 242 list.AppendInteger(0); |
247 EXPECT_FALSE(subtle::PrefMemberVectorStringUpdate(list, &vector)); | 243 EXPECT_FALSE(subtle::PrefMemberVectorStringUpdate(list, &vector)); |
248 EXPECT_EQ(expected_vector, vector); | 244 EXPECT_EQ(expected_vector, vector); |
249 } | 245 } |
250 | 246 |
251 TEST(PrefMemberTest, TwoPrefs) { | 247 TEST(PrefMemberTest, TwoPrefs) { |
252 // Make sure two DoublePrefMembers stay in sync. | 248 // Make sure two DoublePrefMembers stay in sync. |
253 TestingPrefService prefs; | 249 TestingPrefServiceSimple prefs; |
254 RegisterTestPrefs(&prefs); | 250 RegisterTestPrefs(&prefs); |
255 | 251 |
256 DoublePrefMember pref1; | 252 DoublePrefMember pref1; |
257 pref1.Init(kDoublePref, &prefs); | 253 pref1.Init(kDoublePref, &prefs); |
258 DoublePrefMember pref2; | 254 DoublePrefMember pref2; |
259 pref2.Init(kDoublePref, &prefs); | 255 pref2.Init(kDoublePref, &prefs); |
260 | 256 |
261 pref1.SetValue(2.3); | 257 pref1.SetValue(2.3); |
262 EXPECT_EQ(2.3, *pref2); | 258 EXPECT_EQ(2.3, *pref2); |
263 | 259 |
264 pref2.SetValue(3.5); | 260 pref2.SetValue(3.5); |
265 EXPECT_EQ(3.5, *pref1); | 261 EXPECT_EQ(3.5, *pref1); |
266 | 262 |
267 prefs.SetDouble(kDoublePref, 4.2); | 263 prefs.SetDouble(kDoublePref, 4.2); |
268 EXPECT_EQ(4.2, *pref1); | 264 EXPECT_EQ(4.2, *pref1); |
269 EXPECT_EQ(4.2, *pref2); | 265 EXPECT_EQ(4.2, *pref2); |
270 } | 266 } |
271 | 267 |
272 TEST(PrefMemberTest, Observer) { | 268 TEST(PrefMemberTest, Observer) { |
273 TestingPrefService prefs; | 269 TestingPrefServiceSimple prefs; |
274 RegisterTestPrefs(&prefs); | 270 RegisterTestPrefs(&prefs); |
275 | 271 |
276 PrefMemberTestClass test_obj(&prefs); | 272 PrefMemberTestClass test_obj(&prefs); |
277 EXPECT_EQ("default", *test_obj.str_); | 273 EXPECT_EQ("default", *test_obj.str_); |
278 | 274 |
279 // Calling SetValue should not fire the observer. | 275 // Calling SetValue should not fire the observer. |
280 test_obj.str_.SetValue("hello"); | 276 test_obj.str_.SetValue("hello"); |
281 EXPECT_EQ(0, test_obj.observe_cnt_); | 277 EXPECT_EQ(0, test_obj.observe_cnt_); |
282 EXPECT_EQ("hello", prefs.GetString(kStringPref)); | 278 EXPECT_EQ("hello", prefs.GetString(kStringPref)); |
283 | 279 |
(...skipping 11 matching lines...) Expand all Loading... |
295 EXPECT_EQ(2, test_obj.observe_cnt_); | 291 EXPECT_EQ(2, test_obj.observe_cnt_); |
296 EXPECT_EQ("hello", prefs.GetString(kStringPref)); | 292 EXPECT_EQ("hello", prefs.GetString(kStringPref)); |
297 } | 293 } |
298 | 294 |
299 TEST(PrefMemberTest, NoInit) { | 295 TEST(PrefMemberTest, NoInit) { |
300 // Make sure not calling Init on a PrefMember doesn't cause problems. | 296 // Make sure not calling Init on a PrefMember doesn't cause problems. |
301 IntegerPrefMember pref; | 297 IntegerPrefMember pref; |
302 } | 298 } |
303 | 299 |
304 TEST(PrefMemberTest, MoveToThread) { | 300 TEST(PrefMemberTest, MoveToThread) { |
305 TestingPrefService prefs; | 301 TestingPrefServiceSimple prefs; |
306 scoped_refptr<GetPrefValueHelper> helper(new GetPrefValueHelper()); | 302 scoped_refptr<GetPrefValueHelper> helper(new GetPrefValueHelper()); |
307 RegisterTestPrefs(&prefs); | 303 RegisterTestPrefs(&prefs); |
308 helper->Init(kBoolPref, &prefs); | 304 helper->Init(kBoolPref, &prefs); |
309 | 305 |
310 helper->FetchValue(); | 306 helper->FetchValue(); |
311 EXPECT_FALSE(helper->value()); | 307 EXPECT_FALSE(helper->value()); |
312 | 308 |
313 prefs.SetBoolean(kBoolPref, true); | 309 prefs.SetBoolean(kBoolPref, true); |
314 | 310 |
315 helper->FetchValue(); | 311 helper->FetchValue(); |
316 EXPECT_TRUE(helper->value()); | 312 EXPECT_TRUE(helper->value()); |
317 | 313 |
318 helper->Destroy(); | 314 helper->Destroy(); |
319 | 315 |
320 helper->FetchValue(); | 316 helper->FetchValue(); |
321 EXPECT_TRUE(helper->value()); | 317 EXPECT_TRUE(helper->value()); |
322 | 318 |
323 helper->StopThread(); | 319 helper->StopThread(); |
324 } | 320 } |
OLD | NEW |