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/pref_member.h" | 5 #include "base/prefs/pref_member.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/prefs/pref_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
10 #include "base/prefs/testing_pref_service.h" | 10 #include "base/prefs/testing_pref_service.h" |
11 #include "base/single_thread_task_runner.h" | |
12 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
13 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 const char kBoolPref[] = "bool"; | 17 const char kBoolPref[] = "bool"; |
19 const char kIntPref[] = "int"; | 18 const char kIntPref[] = "int"; |
20 const char kDoublePref[] = "double"; | 19 const char kDoublePref[] = "double"; |
21 const char kStringPref[] = "string"; | 20 const char kStringPref[] = "string"; |
22 const char kStringListPref[] = "string_list"; | 21 const char kStringListPref[] = "string_list"; |
23 | 22 |
24 void RegisterTestPrefs(PrefRegistrySimple* registry) { | 23 void RegisterTestPrefs(PrefRegistrySimple* registry) { |
25 registry->RegisterBooleanPref(kBoolPref, false); | 24 registry->RegisterBooleanPref(kBoolPref, false); |
26 registry->RegisterIntegerPref(kIntPref, 0); | 25 registry->RegisterIntegerPref(kIntPref, 0); |
27 registry->RegisterDoublePref(kDoublePref, 0.0); | 26 registry->RegisterDoublePref(kDoublePref, 0.0); |
28 registry->RegisterStringPref(kStringPref, "default"); | 27 registry->RegisterStringPref(kStringPref, "default"); |
29 registry->RegisterListPref(kStringListPref, new base::ListValue()); | 28 registry->RegisterListPref(kStringListPref, new base::ListValue()); |
30 } | 29 } |
31 | 30 |
32 class GetPrefValueHelper | 31 class GetPrefValueHelper |
33 : public base::RefCountedThreadSafe<GetPrefValueHelper> { | 32 : public base::RefCountedThreadSafe<GetPrefValueHelper> { |
34 public: | 33 public: |
35 GetPrefValueHelper() : value_(false), pref_thread_("pref thread") { | 34 GetPrefValueHelper() : value_(false), pref_thread_("pref thread") { |
36 pref_thread_.Start(); | 35 pref_thread_.Start(); |
37 } | 36 } |
38 | 37 |
39 void Init(const std::string& pref_name, PrefService* prefs) { | 38 void Init(const std::string& pref_name, PrefService* prefs) { |
40 pref_.Init(pref_name, prefs); | 39 pref_.Init(pref_name, prefs); |
41 pref_.MoveToThread(pref_thread_.task_runner()); | 40 pref_.MoveToThread(pref_thread_.message_loop_proxy()); |
42 } | 41 } |
43 | 42 |
44 void Destroy() { | 43 void Destroy() { |
45 pref_.Destroy(); | 44 pref_.Destroy(); |
46 } | 45 } |
47 | 46 |
48 void FetchValue() { | 47 void FetchValue() { |
49 base::WaitableEvent event(true, false); | 48 base::WaitableEvent event(true, false); |
50 ASSERT_TRUE(pref_thread_.task_runner()->PostTask( | 49 ASSERT_TRUE( |
51 FROM_HERE, | 50 pref_thread_.message_loop_proxy()->PostTask( |
52 base::Bind(&GetPrefValueHelper::GetPrefValue, this, &event))); | 51 FROM_HERE, |
| 52 base::Bind(&GetPrefValueHelper::GetPrefValue, this, &event))); |
53 event.Wait(); | 53 event.Wait(); |
54 } | 54 } |
55 | 55 |
56 // The thread must be stopped on the main thread. GetPrefValueHelper being | 56 // The thread must be stopped on the main thread. GetPrefValueHelper being |
57 // ref-counted, the destructor can be called from any thread. | 57 // ref-counted, the destructor can be called from any thread. |
58 void StopThread() { | 58 void StopThread() { |
59 pref_thread_.Stop(); | 59 pref_thread_.Stop(); |
60 } | 60 } |
61 | 61 |
62 bool value() { return value_; } | 62 bool value() { return value_; } |
(...skipping 30 matching lines...) Expand all Loading... |
93 | 93 |
94 StringPrefMember str_; | 94 StringPrefMember str_; |
95 int observe_cnt_; | 95 int observe_cnt_; |
96 | 96 |
97 private: | 97 private: |
98 PrefService* prefs_; | 98 PrefService* prefs_; |
99 }; | 99 }; |
100 | 100 |
101 } // anonymous namespace | 101 } // anonymous namespace |
102 | 102 |
103 class PrefMemberTest : public testing::Test { | 103 TEST(PrefMemberTest, BasicGetAndSet) { |
104 base::MessageLoop message_loop_; | |
105 }; | |
106 | |
107 TEST_F(PrefMemberTest, BasicGetAndSet) { | |
108 TestingPrefServiceSimple prefs; | 104 TestingPrefServiceSimple prefs; |
109 RegisterTestPrefs(prefs.registry()); | 105 RegisterTestPrefs(prefs.registry()); |
110 | 106 |
111 // Test bool | 107 // Test bool |
112 BooleanPrefMember boolean; | 108 BooleanPrefMember boolean; |
113 boolean.Init(kBoolPref, &prefs); | 109 boolean.Init(kBoolPref, &prefs); |
114 | 110 |
115 // Check the defaults | 111 // Check the defaults |
116 EXPECT_FALSE(prefs.GetBoolean(kBoolPref)); | 112 EXPECT_FALSE(prefs.GetBoolean(kBoolPref)); |
117 EXPECT_FALSE(boolean.GetValue()); | 113 EXPECT_FALSE(boolean.GetValue()); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 // Try removing through the pref. | 220 // Try removing through the pref. |
225 expected_list.Remove(0, NULL); | 221 expected_list.Remove(0, NULL); |
226 expected_vector.erase(expected_vector.begin()); | 222 expected_vector.erase(expected_vector.begin()); |
227 prefs.Set(kStringListPref, expected_list); | 223 prefs.Set(kStringListPref, expected_list); |
228 | 224 |
229 EXPECT_TRUE(expected_list.Equals(prefs.GetList(kStringListPref))); | 225 EXPECT_TRUE(expected_list.Equals(prefs.GetList(kStringListPref))); |
230 EXPECT_EQ(expected_vector, string_list.GetValue()); | 226 EXPECT_EQ(expected_vector, string_list.GetValue()); |
231 EXPECT_EQ(expected_vector, *string_list); | 227 EXPECT_EQ(expected_vector, *string_list); |
232 } | 228 } |
233 | 229 |
234 TEST_F(PrefMemberTest, InvalidList) { | 230 TEST(PrefMemberTest, InvalidList) { |
235 // Set the vector to an initial good value. | 231 // Set the vector to an initial good value. |
236 std::vector<std::string> expected_vector; | 232 std::vector<std::string> expected_vector; |
237 expected_vector.push_back("foo"); | 233 expected_vector.push_back("foo"); |
238 | 234 |
239 // Try to add a valid list first. | 235 // Try to add a valid list first. |
240 base::ListValue list; | 236 base::ListValue list; |
241 list.AppendString("foo"); | 237 list.AppendString("foo"); |
242 std::vector<std::string> vector; | 238 std::vector<std::string> vector; |
243 EXPECT_TRUE(subtle::PrefMemberVectorStringUpdate(list, &vector)); | 239 EXPECT_TRUE(subtle::PrefMemberVectorStringUpdate(list, &vector)); |
244 EXPECT_EQ(expected_vector, vector); | 240 EXPECT_EQ(expected_vector, vector); |
245 | 241 |
246 // 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. |
247 list.AppendInteger(0); | 243 list.AppendInteger(0); |
248 EXPECT_FALSE(subtle::PrefMemberVectorStringUpdate(list, &vector)); | 244 EXPECT_FALSE(subtle::PrefMemberVectorStringUpdate(list, &vector)); |
249 EXPECT_EQ(expected_vector, vector); | 245 EXPECT_EQ(expected_vector, vector); |
250 } | 246 } |
251 | 247 |
252 TEST_F(PrefMemberTest, TwoPrefs) { | 248 TEST(PrefMemberTest, TwoPrefs) { |
253 // Make sure two DoublePrefMembers stay in sync. | 249 // Make sure two DoublePrefMembers stay in sync. |
254 TestingPrefServiceSimple prefs; | 250 TestingPrefServiceSimple prefs; |
255 RegisterTestPrefs(prefs.registry()); | 251 RegisterTestPrefs(prefs.registry()); |
256 | 252 |
257 DoublePrefMember pref1; | 253 DoublePrefMember pref1; |
258 pref1.Init(kDoublePref, &prefs); | 254 pref1.Init(kDoublePref, &prefs); |
259 DoublePrefMember pref2; | 255 DoublePrefMember pref2; |
260 pref2.Init(kDoublePref, &prefs); | 256 pref2.Init(kDoublePref, &prefs); |
261 | 257 |
262 pref1.SetValue(2.3); | 258 pref1.SetValue(2.3); |
263 EXPECT_EQ(2.3, *pref2); | 259 EXPECT_EQ(2.3, *pref2); |
264 | 260 |
265 pref2.SetValue(3.5); | 261 pref2.SetValue(3.5); |
266 EXPECT_EQ(3.5, *pref1); | 262 EXPECT_EQ(3.5, *pref1); |
267 | 263 |
268 prefs.SetDouble(kDoublePref, 4.2); | 264 prefs.SetDouble(kDoublePref, 4.2); |
269 EXPECT_EQ(4.2, *pref1); | 265 EXPECT_EQ(4.2, *pref1); |
270 EXPECT_EQ(4.2, *pref2); | 266 EXPECT_EQ(4.2, *pref2); |
271 } | 267 } |
272 | 268 |
273 TEST_F(PrefMemberTest, Observer) { | 269 TEST(PrefMemberTest, Observer) { |
274 TestingPrefServiceSimple prefs; | 270 TestingPrefServiceSimple prefs; |
275 RegisterTestPrefs(prefs.registry()); | 271 RegisterTestPrefs(prefs.registry()); |
276 | 272 |
277 PrefMemberTestClass test_obj(&prefs); | 273 PrefMemberTestClass test_obj(&prefs); |
278 EXPECT_EQ("default", *test_obj.str_); | 274 EXPECT_EQ("default", *test_obj.str_); |
279 | 275 |
280 // Calling SetValue should not fire the observer. | 276 // Calling SetValue should not fire the observer. |
281 test_obj.str_.SetValue("hello"); | 277 test_obj.str_.SetValue("hello"); |
282 EXPECT_EQ(0, test_obj.observe_cnt_); | 278 EXPECT_EQ(0, test_obj.observe_cnt_); |
283 EXPECT_EQ("hello", prefs.GetString(kStringPref)); | 279 EXPECT_EQ("hello", prefs.GetString(kStringPref)); |
284 | 280 |
285 // Changing the pref does fire the observer. | 281 // Changing the pref does fire the observer. |
286 prefs.SetString(kStringPref, "world"); | 282 prefs.SetString(kStringPref, "world"); |
287 EXPECT_EQ(1, test_obj.observe_cnt_); | 283 EXPECT_EQ(1, test_obj.observe_cnt_); |
288 EXPECT_EQ("world", *(test_obj.str_)); | 284 EXPECT_EQ("world", *(test_obj.str_)); |
289 | 285 |
290 // Not changing the value should not fire the observer. | 286 // Not changing the value should not fire the observer. |
291 prefs.SetString(kStringPref, "world"); | 287 prefs.SetString(kStringPref, "world"); |
292 EXPECT_EQ(1, test_obj.observe_cnt_); | 288 EXPECT_EQ(1, test_obj.observe_cnt_); |
293 EXPECT_EQ("world", *(test_obj.str_)); | 289 EXPECT_EQ("world", *(test_obj.str_)); |
294 | 290 |
295 prefs.SetString(kStringPref, "hello"); | 291 prefs.SetString(kStringPref, "hello"); |
296 EXPECT_EQ(2, test_obj.observe_cnt_); | 292 EXPECT_EQ(2, test_obj.observe_cnt_); |
297 EXPECT_EQ("hello", prefs.GetString(kStringPref)); | 293 EXPECT_EQ("hello", prefs.GetString(kStringPref)); |
298 } | 294 } |
299 | 295 |
300 TEST_F(PrefMemberTest, NoInit) { | 296 TEST(PrefMemberTest, NoInit) { |
301 // 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. |
302 IntegerPrefMember pref; | 298 IntegerPrefMember pref; |
303 } | 299 } |
304 | 300 |
305 TEST_F(PrefMemberTest, MoveToThread) { | 301 TEST(PrefMemberTest, MoveToThread) { |
306 TestingPrefServiceSimple prefs; | 302 TestingPrefServiceSimple prefs; |
307 scoped_refptr<GetPrefValueHelper> helper(new GetPrefValueHelper()); | 303 scoped_refptr<GetPrefValueHelper> helper(new GetPrefValueHelper()); |
308 RegisterTestPrefs(prefs.registry()); | 304 RegisterTestPrefs(prefs.registry()); |
309 helper->Init(kBoolPref, &prefs); | 305 helper->Init(kBoolPref, &prefs); |
310 | 306 |
311 helper->FetchValue(); | 307 helper->FetchValue(); |
312 EXPECT_FALSE(helper->value()); | 308 EXPECT_FALSE(helper->value()); |
313 | 309 |
314 prefs.SetBoolean(kBoolPref, true); | 310 prefs.SetBoolean(kBoolPref, true); |
315 | 311 |
316 helper->FetchValue(); | 312 helper->FetchValue(); |
317 EXPECT_TRUE(helper->value()); | 313 EXPECT_TRUE(helper->value()); |
318 | 314 |
319 helper->Destroy(); | 315 helper->Destroy(); |
320 | 316 |
321 helper->FetchValue(); | 317 helper->FetchValue(); |
322 EXPECT_TRUE(helper->value()); | 318 EXPECT_TRUE(helper->value()); |
323 | 319 |
324 helper->StopThread(); | 320 helper->StopThread(); |
325 } | 321 } |
OLD | NEW |