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/message_loop.h" | 8 #include "base/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/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 const char kBoolPref[] = "bool"; | 17 const char kBoolPref[] = "bool"; |
18 const char kIntPref[] = "int"; | 18 const char kIntPref[] = "int"; |
19 const char kDoublePref[] = "double"; | 19 const char kDoublePref[] = "double"; |
20 const char kStringPref[] = "string"; | 20 const char kStringPref[] = "string"; |
21 const char kStringListPref[] = "string_list"; | 21 const char kStringListPref[] = "string_list"; |
22 | 22 |
23 void RegisterTestPrefs(PrefRegistrySimple* registry) { | 23 void RegisterTestPrefs(PrefRegistrySimple* registry) { |
24 registry->RegisterBooleanPref(kBoolPref, false); | 24 registry->RegisterBooleanPref(kBoolPref, false); |
25 registry->RegisterIntegerPref(kIntPref, 0); | 25 registry->RegisterIntegerPref(kIntPref, 0); |
26 registry->RegisterDoublePref(kDoublePref, 0.0); | 26 registry->RegisterDoublePref(kDoublePref, 0.0); |
27 registry->RegisterStringPref(kStringPref, "default"); | 27 registry->RegisterStringPref(kStringPref, "default"); |
28 registry->RegisterListPref(kStringListPref, new ListValue()); | 28 registry->RegisterListPref(kStringListPref, new base::ListValue()); |
29 } | 29 } |
30 | 30 |
31 class GetPrefValueHelper | 31 class GetPrefValueHelper |
32 : public base::RefCountedThreadSafe<GetPrefValueHelper> { | 32 : public base::RefCountedThreadSafe<GetPrefValueHelper> { |
33 public: | 33 public: |
34 GetPrefValueHelper() : value_(false), pref_thread_("pref thread") { | 34 GetPrefValueHelper() : value_(false), pref_thread_("pref thread") { |
35 pref_thread_.Start(); | 35 pref_thread_.Start(); |
36 } | 36 } |
37 | 37 |
38 void Init(const char* pref_name, PrefService* prefs) { | 38 void Init(const char* pref_name, PrefService* prefs) { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 EXPECT_EQ("foo", prefs.GetString(kStringPref)); | 182 EXPECT_EQ("foo", prefs.GetString(kStringPref)); |
183 EXPECT_EQ("foo", *string); | 183 EXPECT_EQ("foo", *string); |
184 | 184 |
185 // Try changing back through the pref. | 185 // Try changing back through the pref. |
186 prefs.SetString(kStringPref, "bar"); | 186 prefs.SetString(kStringPref, "bar"); |
187 EXPECT_EQ("bar", prefs.GetString(kStringPref)); | 187 EXPECT_EQ("bar", prefs.GetString(kStringPref)); |
188 EXPECT_EQ("bar", string.GetValue()); | 188 EXPECT_EQ("bar", string.GetValue()); |
189 EXPECT_EQ("bar", *string); | 189 EXPECT_EQ("bar", *string); |
190 | 190 |
191 // Test string list | 191 // Test string list |
192 ListValue expected_list; | 192 base::ListValue expected_list; |
193 std::vector<std::string> expected_vector; | 193 std::vector<std::string> expected_vector; |
194 StringListPrefMember string_list; | 194 StringListPrefMember string_list; |
195 string_list.Init(kStringListPref, &prefs); | 195 string_list.Init(kStringListPref, &prefs); |
196 | 196 |
197 // Check the defaults | 197 // Check the defaults |
198 EXPECT_TRUE(expected_list.Equals(prefs.GetList(kStringListPref))); | 198 EXPECT_TRUE(expected_list.Equals(prefs.GetList(kStringListPref))); |
199 EXPECT_EQ(expected_vector, string_list.GetValue()); | 199 EXPECT_EQ(expected_vector, string_list.GetValue()); |
200 EXPECT_EQ(expected_vector, *string_list); | 200 EXPECT_EQ(expected_vector, *string_list); |
201 | 201 |
202 // Try changing through the pref member. | 202 // Try changing through the pref member. |
(...skipping 23 matching lines...) Expand all Loading... |
226 EXPECT_EQ(expected_vector, string_list.GetValue()); | 226 EXPECT_EQ(expected_vector, string_list.GetValue()); |
227 EXPECT_EQ(expected_vector, *string_list); | 227 EXPECT_EQ(expected_vector, *string_list); |
228 } | 228 } |
229 | 229 |
230 TEST(PrefMemberTest, InvalidList) { | 230 TEST(PrefMemberTest, InvalidList) { |
231 // Set the vector to an initial good value. | 231 // Set the vector to an initial good value. |
232 std::vector<std::string> expected_vector; | 232 std::vector<std::string> expected_vector; |
233 expected_vector.push_back("foo"); | 233 expected_vector.push_back("foo"); |
234 | 234 |
235 // Try to add a valid list first. | 235 // Try to add a valid list first. |
236 ListValue list; | 236 base::ListValue list; |
237 list.AppendString("foo"); | 237 list.AppendString("foo"); |
238 std::vector<std::string> vector; | 238 std::vector<std::string> vector; |
239 EXPECT_TRUE(subtle::PrefMemberVectorStringUpdate(list, &vector)); | 239 EXPECT_TRUE(subtle::PrefMemberVectorStringUpdate(list, &vector)); |
240 EXPECT_EQ(expected_vector, vector); | 240 EXPECT_EQ(expected_vector, vector); |
241 | 241 |
242 // 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. |
243 list.AppendInteger(0); | 243 list.AppendInteger(0); |
244 EXPECT_FALSE(subtle::PrefMemberVectorStringUpdate(list, &vector)); | 244 EXPECT_FALSE(subtle::PrefMemberVectorStringUpdate(list, &vector)); |
245 EXPECT_EQ(expected_vector, vector); | 245 EXPECT_EQ(expected_vector, vector); |
246 } | 246 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 helper->FetchValue(); | 312 helper->FetchValue(); |
313 EXPECT_TRUE(helper->value()); | 313 EXPECT_TRUE(helper->value()); |
314 | 314 |
315 helper->Destroy(); | 315 helper->Destroy(); |
316 | 316 |
317 helper->FetchValue(); | 317 helper->FetchValue(); |
318 EXPECT_TRUE(helper->value()); | 318 EXPECT_TRUE(helper->value()); |
319 | 319 |
320 helper->StopThread(); | 320 helper->StopThread(); |
321 } | 321 } |
OLD | NEW |