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

Side by Side Diff: chrome/browser/prefs/pref_service_unittest.cc

Issue 11316163: Remove the last usages of PrefObserver outside of Prefs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Win and CrOS build. Created 8 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <string> 5 #include <string>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/path_service.h" 11 #include "base/path_service.h"
12 #include "base/prefs/json_pref_store.h" 12 #include "base/prefs/json_pref_store.h"
13 #include "base/prefs/public/pref_change_registrar.h" 13 #include "base/prefs/public/pref_change_registrar.h"
14 #include "base/prefs/testing_pref_store.h" 14 #include "base/prefs/testing_pref_store.h"
15 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/policy/configuration_policy_pref_store.h" 17 #include "chrome/browser/policy/configuration_policy_pref_store.h"
18 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 18 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
19 #include "chrome/browser/prefs/browser_prefs.h" 19 #include "chrome/browser/prefs/browser_prefs.h"
20 #include "chrome/browser/prefs/command_line_pref_store.h" 20 #include "chrome/browser/prefs/command_line_pref_store.h"
21 #include "chrome/browser/prefs/pref_observer_mock.h" 21 #include "chrome/browser/prefs/mock_pref_change_callback.h"
22 #include "chrome/browser/prefs/pref_service_mock_builder.h" 22 #include "chrome/browser/prefs/pref_service_mock_builder.h"
23 #include "chrome/browser/prefs/pref_value_store.h" 23 #include "chrome/browser/prefs/pref_value_store.h"
24 #include "chrome/browser/prefs/scoped_user_pref_update.h" 24 #include "chrome/browser/prefs/scoped_user_pref_update.h"
25 #include "chrome/common/chrome_paths.h" 25 #include "chrome/common/chrome_paths.h"
26 #include "chrome/common/chrome_switches.h" 26 #include "chrome/common/chrome_switches.h"
27 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
28 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 28 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
29 #include "chrome/test/base/testing_pref_service.h" 29 #include "chrome/test/base/testing_pref_service.h"
30 #include "chrome/test/base/testing_profile.h" 30 #include "chrome/test/base/testing_profile.h"
31 #include "content/public/test/test_browser_thread.h" 31 #include "content/public/test/test_browser_thread.h"
32 #include "content/public/test/web_contents_tester.h" 32 #include "content/public/test/web_contents_tester.h"
33 #include "testing/gmock/include/gmock/gmock.h" 33 #include "testing/gmock/include/gmock/gmock.h"
34 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
35 #include "ui/base/test/data/resource.h" 35 #include "ui/base/test/data/resource.h"
36 #include "webkit/glue/webpreferences.h" 36 #include "webkit/glue/webpreferences.h"
37 37
38 using content::BrowserThread; 38 using content::BrowserThread;
39 using content::WebContentsTester; 39 using content::WebContentsTester;
40 using testing::_; 40 using testing::_;
41 using testing::Mock; 41 using testing::Mock;
42 42
43 TEST(PrefServiceTest, NoObserverFire) { 43 TEST(PrefServiceTest, NoObserverFire) {
44 TestingPrefService prefs; 44 TestingPrefService prefs;
45 45
46 const char pref_name[] = "homepage"; 46 const char pref_name[] = "homepage";
47 prefs.RegisterStringPref(pref_name, std::string()); 47 prefs.RegisterStringPref(pref_name, std::string());
48 48
49 const char new_pref_value[] = "http://www.google.com/"; 49 const char new_pref_value[] = "http://www.google.com/";
50 PrefObserverMock obs; 50 MockPrefChangeCallback obs(&prefs);
Mattias Nissler (ping if slow) 2012/12/04 10:10:34 Ah, so here you actually don't have a fixture. OK
Jói 2012/12/04 10:55:57 Right, and I don't want to define the PrefValueMat
51 PrefChangeRegistrar registrar; 51 PrefChangeRegistrar registrar;
52 registrar.Init(&prefs); 52 registrar.Init(&prefs);
53 registrar.Add(pref_name, &obs); 53 registrar.Add(pref_name, obs.GetCallback());
54 54
55 // This should fire the checks in PrefObserverMock::Observe. 55 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
56 const StringValue expected_value(new_pref_value); 56 const StringValue expected_value(new_pref_value);
57 obs.Expect(&prefs, pref_name, &expected_value); 57 obs.Expect(pref_name, &expected_value);
58 prefs.SetString(pref_name, new_pref_value); 58 prefs.SetString(pref_name, new_pref_value);
59 Mock::VerifyAndClearExpectations(&obs); 59 Mock::VerifyAndClearExpectations(&obs);
60 60
61 // Setting the pref to the same value should not set the pref value a second 61 // Setting the pref to the same value should not set the pref value a second
62 // time. 62 // time.
63 EXPECT_CALL(obs, OnPreferenceChanged(_, _)).Times(0); 63 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
64 prefs.SetString(pref_name, new_pref_value); 64 prefs.SetString(pref_name, new_pref_value);
65 Mock::VerifyAndClearExpectations(&obs); 65 Mock::VerifyAndClearExpectations(&obs);
66 66
67 // Clearing the pref should cause the pref to fire. 67 // Clearing the pref should cause the pref to fire.
68 const StringValue expected_default_value(""); 68 const StringValue expected_default_value("");
69 obs.Expect(&prefs, pref_name, &expected_default_value); 69 obs.Expect(pref_name, &expected_default_value);
70 prefs.ClearPref(pref_name); 70 prefs.ClearPref(pref_name);
71 Mock::VerifyAndClearExpectations(&obs); 71 Mock::VerifyAndClearExpectations(&obs);
72 72
73 // Clearing the pref again should not cause the pref to fire. 73 // Clearing the pref again should not cause the pref to fire.
74 EXPECT_CALL(obs, OnPreferenceChanged(_, _)).Times(0); 74 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
75 prefs.ClearPref(pref_name); 75 prefs.ClearPref(pref_name);
76 Mock::VerifyAndClearExpectations(&obs); 76 Mock::VerifyAndClearExpectations(&obs);
77 } 77 }
78 78
79 TEST(PrefServiceTest, HasPrefPath) { 79 TEST(PrefServiceTest, HasPrefPath) {
80 TestingPrefService prefs; 80 TestingPrefService prefs;
81 81
82 const char path[] = "fake.path"; 82 const char path[] = "fake.path";
83 83
84 // Shouldn't initially have a path. 84 // Shouldn't initially have a path.
(...skipping 11 matching lines...) Expand all
96 96
97 TEST(PrefServiceTest, Observers) { 97 TEST(PrefServiceTest, Observers) {
98 const char pref_name[] = "homepage"; 98 const char pref_name[] = "homepage";
99 99
100 TestingPrefService prefs; 100 TestingPrefService prefs;
101 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); 101 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com"));
102 prefs.RegisterStringPref(pref_name, std::string()); 102 prefs.RegisterStringPref(pref_name, std::string());
103 103
104 const char new_pref_value[] = "http://www.google.com/"; 104 const char new_pref_value[] = "http://www.google.com/";
105 const StringValue expected_new_pref_value(new_pref_value); 105 const StringValue expected_new_pref_value(new_pref_value);
106 PrefObserverMock obs; 106 MockPrefChangeCallback obs(&prefs);
107 PrefChangeRegistrar registrar; 107 PrefChangeRegistrar registrar;
108 registrar.Init(&prefs); 108 registrar.Init(&prefs);
109 registrar.Add(pref_name, &obs); 109 registrar.Add(pref_name, obs.GetCallback());
110 110
111 PrefChangeRegistrar registrar_two; 111 PrefChangeRegistrar registrar_two;
112 registrar_two.Init(&prefs); 112 registrar_two.Init(&prefs);
113 113
114 // This should fire the checks in PrefObserverMock::Observe. 114 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
115 obs.Expect(&prefs, pref_name, &expected_new_pref_value); 115 obs.Expect(pref_name, &expected_new_pref_value);
116 prefs.SetString(pref_name, new_pref_value); 116 prefs.SetString(pref_name, new_pref_value);
117 Mock::VerifyAndClearExpectations(&obs); 117 Mock::VerifyAndClearExpectations(&obs);
118 118
119 // Now try adding a second pref observer. 119 // Now try adding a second pref observer.
120 const char new_pref_value2[] = "http://www.youtube.com/"; 120 const char new_pref_value2[] = "http://www.youtube.com/";
121 const StringValue expected_new_pref_value2(new_pref_value2); 121 const StringValue expected_new_pref_value2(new_pref_value2);
122 PrefObserverMock obs2; 122 MockPrefChangeCallback obs2(&prefs);
123 obs.Expect(&prefs, pref_name, &expected_new_pref_value2); 123 obs.Expect(pref_name, &expected_new_pref_value2);
124 obs2.Expect(&prefs, pref_name, &expected_new_pref_value2); 124 obs2.Expect(pref_name, &expected_new_pref_value2);
125 registrar_two.Add(pref_name, &obs2); 125 registrar_two.Add(pref_name, obs2.GetCallback());
126 // This should fire the checks in obs and obs2. 126 // This should fire the checks in obs and obs2.
127 prefs.SetString(pref_name, new_pref_value2); 127 prefs.SetString(pref_name, new_pref_value2);
128 Mock::VerifyAndClearExpectations(&obs); 128 Mock::VerifyAndClearExpectations(&obs);
129 Mock::VerifyAndClearExpectations(&obs2); 129 Mock::VerifyAndClearExpectations(&obs2);
130 130
131 // Set a recommended value. 131 // Set a recommended value.
132 const StringValue recommended_pref_value("http://www.gmail.com/"); 132 const StringValue recommended_pref_value("http://www.gmail.com/");
133 obs.Expect(&prefs, pref_name, &expected_new_pref_value2); 133 obs.Expect(pref_name, &expected_new_pref_value2);
134 obs2.Expect(&prefs, pref_name, &expected_new_pref_value2); 134 obs2.Expect(pref_name, &expected_new_pref_value2);
135 // This should fire the checks in obs and obs2 but with an unchanged value 135 // This should fire the checks in obs and obs2 but with an unchanged value
136 // as the recommended value is being overridden by the user-set value. 136 // as the recommended value is being overridden by the user-set value.
137 prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy()); 137 prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy());
138 Mock::VerifyAndClearExpectations(&obs); 138 Mock::VerifyAndClearExpectations(&obs);
139 Mock::VerifyAndClearExpectations(&obs2); 139 Mock::VerifyAndClearExpectations(&obs2);
140 140
141 // Make sure obs2 still works after removing obs. 141 // Make sure obs2 still works after removing obs.
142 registrar.Remove(pref_name); 142 registrar.Remove(pref_name);
143 EXPECT_CALL(obs, OnPreferenceChanged(_, _)).Times(0); 143 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
144 obs2.Expect(&prefs, pref_name, &expected_new_pref_value); 144 obs2.Expect(pref_name, &expected_new_pref_value);
145 // This should only fire the observer in obs2. 145 // This should only fire the observer in obs2.
146 prefs.SetString(pref_name, new_pref_value); 146 prefs.SetString(pref_name, new_pref_value);
147 Mock::VerifyAndClearExpectations(&obs); 147 Mock::VerifyAndClearExpectations(&obs);
148 Mock::VerifyAndClearExpectations(&obs2); 148 Mock::VerifyAndClearExpectations(&obs2);
149 } 149 }
150 150
151 // Make sure that if a preference changes type, so the wrong type is stored in 151 // Make sure that if a preference changes type, so the wrong type is stored in
152 // the user pref file, it uses the correct fallback value instead. 152 // the user pref file, it uses the correct fallback value instead.
153 TEST(PrefServiceTest, GetValueChangedType) { 153 TEST(PrefServiceTest, GetValueChangedType) {
154 const int kTestValue = 10; 154 const int kTestValue = 10;
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 data_dir_.AppendASCII("write.golden.need_empty_value.json"); 354 data_dir_.AppendASCII("write.golden.need_empty_value.json");
355 ASSERT_TRUE(file_util::PathExists(golden_output_file)); 355 ASSERT_TRUE(file_util::PathExists(golden_output_file));
356 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); 356 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file));
357 } 357 }
358 358
359 class PrefServiceSetValueTest : public testing::Test { 359 class PrefServiceSetValueTest : public testing::Test {
360 protected: 360 protected:
361 static const char kName[]; 361 static const char kName[];
362 static const char kValue[]; 362 static const char kValue[];
363 363
364 PrefServiceSetValueTest() : observer_(&prefs_) {}
365
364 TestingPrefService prefs_; 366 TestingPrefService prefs_;
365 PrefObserverMock observer_; 367 MockPrefChangeCallback observer_;
366 }; 368 };
367 369
368 const char PrefServiceSetValueTest::kName[] = "name"; 370 const char PrefServiceSetValueTest::kName[] = "name";
369 const char PrefServiceSetValueTest::kValue[] = "value"; 371 const char PrefServiceSetValueTest::kValue[] = "value";
370 372
371 TEST_F(PrefServiceSetValueTest, SetStringValue) { 373 TEST_F(PrefServiceSetValueTest, SetStringValue) {
372 const char default_string[] = "default"; 374 const char default_string[] = "default";
373 const StringValue default_value(default_string); 375 const StringValue default_value(default_string);
374 prefs_.RegisterStringPref(kName, default_string); 376 prefs_.RegisterStringPref(kName, default_string);
375 377
376 PrefChangeRegistrar registrar; 378 PrefChangeRegistrar registrar;
377 registrar.Init(&prefs_); 379 registrar.Init(&prefs_);
378 registrar.Add(kName, &observer_); 380 registrar.Add(kName, observer_.GetCallback());
379 381
380 // Changing the controlling store from default to user triggers notification. 382 // Changing the controlling store from default to user triggers notification.
381 observer_.Expect(&prefs_, kName, &default_value); 383 observer_.Expect(kName, &default_value);
382 prefs_.Set(kName, default_value); 384 prefs_.Set(kName, default_value);
383 Mock::VerifyAndClearExpectations(&observer_); 385 Mock::VerifyAndClearExpectations(&observer_);
384 386
385 EXPECT_CALL(observer_, OnPreferenceChanged(_, _)).Times(0); 387 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
386 prefs_.Set(kName, default_value); 388 prefs_.Set(kName, default_value);
387 Mock::VerifyAndClearExpectations(&observer_); 389 Mock::VerifyAndClearExpectations(&observer_);
388 390
389 StringValue new_value(kValue); 391 StringValue new_value(kValue);
390 observer_.Expect(&prefs_, kName, &new_value); 392 observer_.Expect(kName, &new_value);
391 prefs_.Set(kName, new_value); 393 prefs_.Set(kName, new_value);
392 Mock::VerifyAndClearExpectations(&observer_); 394 Mock::VerifyAndClearExpectations(&observer_);
393 } 395 }
394 396
395 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) { 397 TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
396 prefs_.RegisterDictionaryPref(kName); 398 prefs_.RegisterDictionaryPref(kName);
397 PrefChangeRegistrar registrar; 399 PrefChangeRegistrar registrar;
398 registrar.Init(&prefs_); 400 registrar.Init(&prefs_);
399 registrar.Add(kName, &observer_); 401 registrar.Add(kName, observer_.GetCallback());
400 402 ;
Mattias Nissler (ping if slow) 2012/12/04 10:10:34 nit: remove
Jói 2012/12/04 10:55:57 Done.
401 EXPECT_CALL(observer_, OnPreferenceChanged(_, _)).Times(0); 403 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
402 prefs_.RemoveUserPref(kName); 404 prefs_.RemoveUserPref(kName);
403 Mock::VerifyAndClearExpectations(&observer_); 405 Mock::VerifyAndClearExpectations(&observer_);
404 406
405 DictionaryValue new_value; 407 DictionaryValue new_value;
406 new_value.SetString(kName, kValue); 408 new_value.SetString(kName, kValue);
407 observer_.Expect(&prefs_, kName, &new_value); 409 observer_.Expect(kName, &new_value);
408 prefs_.Set(kName, new_value); 410 prefs_.Set(kName, new_value);
409 Mock::VerifyAndClearExpectations(&observer_); 411 Mock::VerifyAndClearExpectations(&observer_);
410 412
411 EXPECT_CALL(observer_, OnPreferenceChanged(_, _)).Times(0); 413 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
412 prefs_.Set(kName, new_value); 414 prefs_.Set(kName, new_value);
413 Mock::VerifyAndClearExpectations(&observer_); 415 Mock::VerifyAndClearExpectations(&observer_);
414 416
415 DictionaryValue empty; 417 DictionaryValue empty;
416 observer_.Expect(&prefs_, kName, &empty); 418 observer_.Expect(kName, &empty);
417 prefs_.Set(kName, empty); 419 prefs_.Set(kName, empty);
418 Mock::VerifyAndClearExpectations(&observer_); 420 Mock::VerifyAndClearExpectations(&observer_);
419 } 421 }
420 422
421 TEST_F(PrefServiceSetValueTest, SetListValue) { 423 TEST_F(PrefServiceSetValueTest, SetListValue) {
422 prefs_.RegisterListPref(kName); 424 prefs_.RegisterListPref(kName);
423 PrefChangeRegistrar registrar; 425 PrefChangeRegistrar registrar;
424 registrar.Init(&prefs_); 426 registrar.Init(&prefs_);
425 registrar.Add(kName, &observer_); 427 registrar.Add(kName, observer_.GetCallback());
426 428
427 EXPECT_CALL(observer_, OnPreferenceChanged(_, _)).Times(0); 429 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
428 prefs_.RemoveUserPref(kName); 430 prefs_.RemoveUserPref(kName);
429 Mock::VerifyAndClearExpectations(&observer_); 431 Mock::VerifyAndClearExpectations(&observer_);
430 432
431 ListValue new_value; 433 ListValue new_value;
432 new_value.Append(Value::CreateStringValue(kValue)); 434 new_value.Append(Value::CreateStringValue(kValue));
433 observer_.Expect(&prefs_, kName, &new_value); 435 observer_.Expect(kName, &new_value);
434 prefs_.Set(kName, new_value); 436 prefs_.Set(kName, new_value);
435 Mock::VerifyAndClearExpectations(&observer_); 437 Mock::VerifyAndClearExpectations(&observer_);
436 438
437 EXPECT_CALL(observer_, OnPreferenceChanged(_, _)).Times(0); 439 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
438 prefs_.Set(kName, new_value); 440 prefs_.Set(kName, new_value);
439 Mock::VerifyAndClearExpectations(&observer_); 441 Mock::VerifyAndClearExpectations(&observer_);
440 442
441 ListValue empty; 443 ListValue empty;
442 observer_.Expect(&prefs_, kName, &empty); 444 observer_.Expect(kName, &empty);
443 prefs_.Set(kName, empty); 445 prefs_.Set(kName, empty);
444 Mock::VerifyAndClearExpectations(&observer_); 446 Mock::VerifyAndClearExpectations(&observer_);
445 } 447 }
446 448
447 class PrefServiceWebKitPrefs : public ChromeRenderViewHostTestHarness { 449 class PrefServiceWebKitPrefs : public ChromeRenderViewHostTestHarness {
448 protected: 450 protected:
449 PrefServiceWebKitPrefs() : ui_thread_(BrowserThread::UI, &message_loop_) { 451 PrefServiceWebKitPrefs() : ui_thread_(BrowserThread::UI, &message_loop_) {
450 } 452 }
451 453
452 virtual void SetUp() { 454 virtual void SetUp() {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 const char kDefaultFont[] = "Times"; 496 const char kDefaultFont[] = "Times";
495 #elif defined(OS_CHROMEOS) 497 #elif defined(OS_CHROMEOS)
496 const char kDefaultFont[] = "Tinos"; 498 const char kDefaultFont[] = "Tinos";
497 #else 499 #else
498 const char kDefaultFont[] = "Times New Roman"; 500 const char kDefaultFont[] = "Times New Roman";
499 #endif 501 #endif
500 EXPECT_EQ(ASCIIToUTF16(kDefaultFont), 502 EXPECT_EQ(ASCIIToUTF16(kDefaultFont),
501 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]); 503 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]);
502 EXPECT_TRUE(webkit_prefs.javascript_enabled); 504 EXPECT_TRUE(webkit_prefs.javascript_enabled);
503 } 505 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698