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