Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: base/prefs/pref_member_unittest.cc

Issue 1100773004: base: Remove most uses of MessageLoopProxy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added some missing includes. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/prefs/json_pref_store_unittest.cc ('k') | base/prefs/pref_service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/message_loop.h" 8 #include "base/location.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"
11 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/thread.h" 13 #include "base/threading/thread.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 15
15 namespace { 16 namespace {
16 17
17 const char kBoolPref[] = "bool"; 18 const char kBoolPref[] = "bool";
18 const char kIntPref[] = "int"; 19 const char kIntPref[] = "int";
19 const char kDoublePref[] = "double"; 20 const char kDoublePref[] = "double";
20 const char kStringPref[] = "string"; 21 const char kStringPref[] = "string";
21 const char kStringListPref[] = "string_list"; 22 const char kStringListPref[] = "string_list";
22 23
23 void RegisterTestPrefs(PrefRegistrySimple* registry) { 24 void RegisterTestPrefs(PrefRegistrySimple* registry) {
24 registry->RegisterBooleanPref(kBoolPref, false); 25 registry->RegisterBooleanPref(kBoolPref, false);
25 registry->RegisterIntegerPref(kIntPref, 0); 26 registry->RegisterIntegerPref(kIntPref, 0);
26 registry->RegisterDoublePref(kDoublePref, 0.0); 27 registry->RegisterDoublePref(kDoublePref, 0.0);
27 registry->RegisterStringPref(kStringPref, "default"); 28 registry->RegisterStringPref(kStringPref, "default");
28 registry->RegisterListPref(kStringListPref, new base::ListValue()); 29 registry->RegisterListPref(kStringListPref, new base::ListValue());
29 } 30 }
30 31
31 class GetPrefValueHelper 32 class GetPrefValueHelper
32 : public base::RefCountedThreadSafe<GetPrefValueHelper> { 33 : public base::RefCountedThreadSafe<GetPrefValueHelper> {
33 public: 34 public:
34 GetPrefValueHelper() : value_(false), pref_thread_("pref thread") { 35 GetPrefValueHelper() : value_(false), pref_thread_("pref thread") {
35 pref_thread_.Start(); 36 pref_thread_.Start();
36 } 37 }
37 38
38 void Init(const std::string& pref_name, PrefService* prefs) { 39 void Init(const std::string& pref_name, PrefService* prefs) {
39 pref_.Init(pref_name, prefs); 40 pref_.Init(pref_name, prefs);
40 pref_.MoveToThread(pref_thread_.message_loop_proxy()); 41 pref_.MoveToThread(pref_thread_.task_runner());
41 } 42 }
42 43
43 void Destroy() { 44 void Destroy() {
44 pref_.Destroy(); 45 pref_.Destroy();
45 } 46 }
46 47
47 void FetchValue() { 48 void FetchValue() {
48 base::WaitableEvent event(true, false); 49 base::WaitableEvent event(true, false);
49 ASSERT_TRUE( 50 ASSERT_TRUE(pref_thread_.task_runner()->PostTask(
50 pref_thread_.message_loop_proxy()->PostTask( 51 FROM_HERE,
51 FROM_HERE, 52 base::Bind(&GetPrefValueHelper::GetPrefValue, this, &event)));
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
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 TEST(PrefMemberTest, BasicGetAndSet) { 103 class PrefMemberTest : public testing::Test {
104 base::MessageLoop message_loop_;
105 };
106
107 TEST_F(PrefMemberTest, BasicGetAndSet) {
104 TestingPrefServiceSimple prefs; 108 TestingPrefServiceSimple prefs;
105 RegisterTestPrefs(prefs.registry()); 109 RegisterTestPrefs(prefs.registry());
106 110
107 // Test bool 111 // Test bool
108 BooleanPrefMember boolean; 112 BooleanPrefMember boolean;
109 boolean.Init(kBoolPref, &prefs); 113 boolean.Init(kBoolPref, &prefs);
110 114
111 // Check the defaults 115 // Check the defaults
112 EXPECT_FALSE(prefs.GetBoolean(kBoolPref)); 116 EXPECT_FALSE(prefs.GetBoolean(kBoolPref));
113 EXPECT_FALSE(boolean.GetValue()); 117 EXPECT_FALSE(boolean.GetValue());
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 // Try removing through the pref. 224 // Try removing through the pref.
221 expected_list.Remove(0, NULL); 225 expected_list.Remove(0, NULL);
222 expected_vector.erase(expected_vector.begin()); 226 expected_vector.erase(expected_vector.begin());
223 prefs.Set(kStringListPref, expected_list); 227 prefs.Set(kStringListPref, expected_list);
224 228
225 EXPECT_TRUE(expected_list.Equals(prefs.GetList(kStringListPref))); 229 EXPECT_TRUE(expected_list.Equals(prefs.GetList(kStringListPref)));
226 EXPECT_EQ(expected_vector, string_list.GetValue()); 230 EXPECT_EQ(expected_vector, string_list.GetValue());
227 EXPECT_EQ(expected_vector, *string_list); 231 EXPECT_EQ(expected_vector, *string_list);
228 } 232 }
229 233
230 TEST(PrefMemberTest, InvalidList) { 234 TEST_F(PrefMemberTest, InvalidList) {
231 // Set the vector to an initial good value. 235 // Set the vector to an initial good value.
232 std::vector<std::string> expected_vector; 236 std::vector<std::string> expected_vector;
233 expected_vector.push_back("foo"); 237 expected_vector.push_back("foo");
234 238
235 // Try to add a valid list first. 239 // Try to add a valid list first.
236 base::ListValue list; 240 base::ListValue list;
237 list.AppendString("foo"); 241 list.AppendString("foo");
238 std::vector<std::string> vector; 242 std::vector<std::string> vector;
239 EXPECT_TRUE(subtle::PrefMemberVectorStringUpdate(list, &vector)); 243 EXPECT_TRUE(subtle::PrefMemberVectorStringUpdate(list, &vector));
240 EXPECT_EQ(expected_vector, vector); 244 EXPECT_EQ(expected_vector, vector);
241 245
242 // Now try to add an invalid list. |vector| should not be changed. 246 // Now try to add an invalid list. |vector| should not be changed.
243 list.AppendInteger(0); 247 list.AppendInteger(0);
244 EXPECT_FALSE(subtle::PrefMemberVectorStringUpdate(list, &vector)); 248 EXPECT_FALSE(subtle::PrefMemberVectorStringUpdate(list, &vector));
245 EXPECT_EQ(expected_vector, vector); 249 EXPECT_EQ(expected_vector, vector);
246 } 250 }
247 251
248 TEST(PrefMemberTest, TwoPrefs) { 252 TEST_F(PrefMemberTest, TwoPrefs) {
249 // Make sure two DoublePrefMembers stay in sync. 253 // Make sure two DoublePrefMembers stay in sync.
250 TestingPrefServiceSimple prefs; 254 TestingPrefServiceSimple prefs;
251 RegisterTestPrefs(prefs.registry()); 255 RegisterTestPrefs(prefs.registry());
252 256
253 DoublePrefMember pref1; 257 DoublePrefMember pref1;
254 pref1.Init(kDoublePref, &prefs); 258 pref1.Init(kDoublePref, &prefs);
255 DoublePrefMember pref2; 259 DoublePrefMember pref2;
256 pref2.Init(kDoublePref, &prefs); 260 pref2.Init(kDoublePref, &prefs);
257 261
258 pref1.SetValue(2.3); 262 pref1.SetValue(2.3);
259 EXPECT_EQ(2.3, *pref2); 263 EXPECT_EQ(2.3, *pref2);
260 264
261 pref2.SetValue(3.5); 265 pref2.SetValue(3.5);
262 EXPECT_EQ(3.5, *pref1); 266 EXPECT_EQ(3.5, *pref1);
263 267
264 prefs.SetDouble(kDoublePref, 4.2); 268 prefs.SetDouble(kDoublePref, 4.2);
265 EXPECT_EQ(4.2, *pref1); 269 EXPECT_EQ(4.2, *pref1);
266 EXPECT_EQ(4.2, *pref2); 270 EXPECT_EQ(4.2, *pref2);
267 } 271 }
268 272
269 TEST(PrefMemberTest, Observer) { 273 TEST_F(PrefMemberTest, Observer) {
270 TestingPrefServiceSimple prefs; 274 TestingPrefServiceSimple prefs;
271 RegisterTestPrefs(prefs.registry()); 275 RegisterTestPrefs(prefs.registry());
272 276
273 PrefMemberTestClass test_obj(&prefs); 277 PrefMemberTestClass test_obj(&prefs);
274 EXPECT_EQ("default", *test_obj.str_); 278 EXPECT_EQ("default", *test_obj.str_);
275 279
276 // Calling SetValue should not fire the observer. 280 // Calling SetValue should not fire the observer.
277 test_obj.str_.SetValue("hello"); 281 test_obj.str_.SetValue("hello");
278 EXPECT_EQ(0, test_obj.observe_cnt_); 282 EXPECT_EQ(0, test_obj.observe_cnt_);
279 EXPECT_EQ("hello", prefs.GetString(kStringPref)); 283 EXPECT_EQ("hello", prefs.GetString(kStringPref));
280 284
281 // Changing the pref does fire the observer. 285 // Changing the pref does fire the observer.
282 prefs.SetString(kStringPref, "world"); 286 prefs.SetString(kStringPref, "world");
283 EXPECT_EQ(1, test_obj.observe_cnt_); 287 EXPECT_EQ(1, test_obj.observe_cnt_);
284 EXPECT_EQ("world", *(test_obj.str_)); 288 EXPECT_EQ("world", *(test_obj.str_));
285 289
286 // Not changing the value should not fire the observer. 290 // Not changing the value should not fire the observer.
287 prefs.SetString(kStringPref, "world"); 291 prefs.SetString(kStringPref, "world");
288 EXPECT_EQ(1, test_obj.observe_cnt_); 292 EXPECT_EQ(1, test_obj.observe_cnt_);
289 EXPECT_EQ("world", *(test_obj.str_)); 293 EXPECT_EQ("world", *(test_obj.str_));
290 294
291 prefs.SetString(kStringPref, "hello"); 295 prefs.SetString(kStringPref, "hello");
292 EXPECT_EQ(2, test_obj.observe_cnt_); 296 EXPECT_EQ(2, test_obj.observe_cnt_);
293 EXPECT_EQ("hello", prefs.GetString(kStringPref)); 297 EXPECT_EQ("hello", prefs.GetString(kStringPref));
294 } 298 }
295 299
296 TEST(PrefMemberTest, NoInit) { 300 TEST_F(PrefMemberTest, NoInit) {
297 // Make sure not calling Init on a PrefMember doesn't cause problems. 301 // Make sure not calling Init on a PrefMember doesn't cause problems.
298 IntegerPrefMember pref; 302 IntegerPrefMember pref;
299 } 303 }
300 304
301 TEST(PrefMemberTest, MoveToThread) { 305 TEST_F(PrefMemberTest, MoveToThread) {
302 TestingPrefServiceSimple prefs; 306 TestingPrefServiceSimple prefs;
303 scoped_refptr<GetPrefValueHelper> helper(new GetPrefValueHelper()); 307 scoped_refptr<GetPrefValueHelper> helper(new GetPrefValueHelper());
304 RegisterTestPrefs(prefs.registry()); 308 RegisterTestPrefs(prefs.registry());
305 helper->Init(kBoolPref, &prefs); 309 helper->Init(kBoolPref, &prefs);
306 310
307 helper->FetchValue(); 311 helper->FetchValue();
308 EXPECT_FALSE(helper->value()); 312 EXPECT_FALSE(helper->value());
309 313
310 prefs.SetBoolean(kBoolPref, true); 314 prefs.SetBoolean(kBoolPref, true);
311 315
312 helper->FetchValue(); 316 helper->FetchValue();
313 EXPECT_TRUE(helper->value()); 317 EXPECT_TRUE(helper->value());
314 318
315 helper->Destroy(); 319 helper->Destroy();
316 320
317 helper->FetchValue(); 321 helper->FetchValue();
318 EXPECT_TRUE(helper->value()); 322 EXPECT_TRUE(helper->value());
319 323
320 helper->StopThread(); 324 helper->StopThread();
321 } 325 }
OLDNEW
« no previous file with comments | « base/prefs/json_pref_store_unittest.cc ('k') | base/prefs/pref_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698