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

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

Issue 5441002: Clean up pref change notification handling. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 10 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 <set>
6 #include <string>
7
5 #include "base/scoped_ptr.h" 8 #include "base/scoped_ptr.h"
6 #include "base/values.h" 9 #include "base/values.h"
7 #include "chrome/browser/browser_thread.h" 10 #include "chrome/browser/browser_thread.h"
8 #include "chrome/browser/policy/configuration_policy_pref_store.h" 11 #include "chrome/browser/policy/configuration_policy_pref_store.h"
9 #include "chrome/browser/prefs/dummy_pref_store.h" 12 #include "chrome/browser/prefs/pref_notifier.h"
10 #include "chrome/browser/prefs/pref_value_store.h" 13 #include "chrome/browser/prefs/pref_value_store.h"
14 #include "chrome/browser/prefs/testing_pref_store.h"
11 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
12 #include "chrome/test/testing_pref_value_store.h"
13 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
15 18
16 using testing::_; 19 using testing::_;
20 using testing::AnyNumber;
17 using testing::Mock; 21 using testing::Mock;
22 using testing::Invoke;
18 23
19 namespace { 24 namespace {
20 25
21 class MockPolicyRefreshCallback { 26 // Records preference changes.
27 class PrefChangeRecorder {
22 public: 28 public:
23 MockPolicyRefreshCallback() {} 29 void Record(const std::string& pref_name) {
24 MOCK_METHOD1(DoCallback, void(const std::vector<std::string>)); 30 changed_prefs_.insert(pref_name);
31 }
32
33 void Clear() {
34 changed_prefs_.clear();
35 }
36
37 const std::set<std::string>& changed_prefs() { return changed_prefs_; }
38
39 private:
40 std::set<std::string> changed_prefs_;
41 };
42
43 // Allows to capture pref notifications through gmock.
44 class MockPrefNotifier : public PrefNotifier {
45 public:
46 // It's OK to pass a NULL PrefService, since it's only used as notification
47 // source.
48 MockPrefNotifier()
49 : PrefNotifier(NULL) {}
50
51 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
52 MOCK_METHOD0(OnIntializationCompleted, void());
25 }; 53 };
26 54
27 } // namespace 55 } // namespace
28 56
29 // Names of the preferences used in this test program. 57 // Names of the preferences used in this test program.
30 namespace prefs { 58 namespace prefs {
31 const char kMissingPref[] = "this.pref.does_not_exist"; 59 const char kMissingPref[] = "this.pref.does_not_exist";
32 const char kRecommendedPref[] = "this.pref.recommended_value_only"; 60 const char kRecommendedPref[] = "this.pref.recommended_value_only";
33 const char kSampleDict[] = "sample.dict"; 61 const char kSampleDict[] = "sample.dict";
34 const char kSampleList[] = "sample.list"; 62 const char kSampleList[] = "sample.list";
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 virtual void SetUp() { 113 virtual void SetUp() {
86 // Create dummy user preferences. 114 // Create dummy user preferences.
87 managed_platform_prefs_= CreateManagedPlatformPrefs(); 115 managed_platform_prefs_= CreateManagedPlatformPrefs();
88 device_management_prefs_ = CreateDeviceManagementPrefs(); 116 device_management_prefs_ = CreateDeviceManagementPrefs();
89 extension_prefs_ = CreateExtensionPrefs(); 117 extension_prefs_ = CreateExtensionPrefs();
90 command_line_prefs_ = CreateCommandLinePrefs(); 118 command_line_prefs_ = CreateCommandLinePrefs();
91 user_prefs_ = CreateUserPrefs(); 119 user_prefs_ = CreateUserPrefs();
92 recommended_prefs_ = CreateRecommendedPrefs(); 120 recommended_prefs_ = CreateRecommendedPrefs();
93 default_prefs_ = CreateDefaultPrefs(); 121 default_prefs_ = CreateDefaultPrefs();
94 122
95 std::sort(expected_differing_paths_.begin(), 123 // Create TestingPrefStores.
96 expected_differing_paths_.end()); 124 managed_platform_pref_store_ = new TestingPrefStore();
97
98 // Create |DummyPrefStore|s.
99 managed_platform_pref_store_ = new DummyPrefStore();
100 managed_platform_pref_store_->set_prefs(managed_platform_prefs_); 125 managed_platform_pref_store_->set_prefs(managed_platform_prefs_);
101 device_management_pref_store_ = new DummyPrefStore(); 126 device_management_pref_store_ = new TestingPrefStore();
102 device_management_pref_store_->set_prefs(device_management_prefs_); 127 device_management_pref_store_->set_prefs(device_management_prefs_);
103 extension_pref_store_ = new DummyPrefStore(); 128 extension_pref_store_ = new TestingPrefStore();
104 extension_pref_store_->set_prefs(extension_prefs_); 129 extension_pref_store_->set_prefs(extension_prefs_);
105 command_line_pref_store_ = new DummyPrefStore(); 130 command_line_pref_store_ = new TestingPrefStore();
106 command_line_pref_store_->set_prefs(command_line_prefs_); 131 command_line_pref_store_->set_prefs(command_line_prefs_);
107 user_pref_store_ = new DummyPrefStore(); 132 user_pref_store_ = new TestingPrefStore();
108 user_pref_store_->set_read_only(false); 133 user_pref_store_->set_read_only(false);
109 user_pref_store_->set_prefs(user_prefs_); 134 user_pref_store_->set_prefs(user_prefs_);
110 recommended_pref_store_ = new DummyPrefStore(); 135 recommended_pref_store_ = new TestingPrefStore();
111 recommended_pref_store_->set_prefs(recommended_prefs_); 136 recommended_pref_store_->set_prefs(recommended_prefs_);
112 default_pref_store_ = new DummyPrefStore(); 137 default_pref_store_ = new TestingPrefStore();
113 default_pref_store_->set_prefs(default_prefs_); 138 default_pref_store_->set_prefs(default_prefs_);
114 139
115 // Create a new pref-value-store. 140 // Create a new pref-value-store.
116 pref_value_store_ = new TestingPrefValueStore( 141 pref_value_store_ = new PrefValueStore(
117 managed_platform_pref_store_, 142 managed_platform_pref_store_,
118 device_management_pref_store_, 143 device_management_pref_store_,
119 extension_pref_store_, 144 extension_pref_store_,
120 command_line_pref_store_, 145 command_line_pref_store_,
121 user_pref_store_, 146 user_pref_store_,
122 recommended_pref_store_, 147 recommended_pref_store_,
123 default_pref_store_); 148 default_pref_store_,
149 &pref_notifier_,
150 NULL);
124 151
125 // Register prefs with the PrefValueStore. 152 // Register prefs with the PrefValueStore.
126 pref_value_store_->RegisterPreferenceType(prefs::kApplicationLocale, 153 pref_value_store_->RegisterPreferenceType(prefs::kApplicationLocale,
127 Value::TYPE_STRING); 154 Value::TYPE_STRING);
128 pref_value_store_->RegisterPreferenceType(prefs::kCurrentThemeID, 155 pref_value_store_->RegisterPreferenceType(prefs::kCurrentThemeID,
129 Value::TYPE_STRING); 156 Value::TYPE_STRING);
130 pref_value_store_->RegisterPreferenceType( 157 pref_value_store_->RegisterPreferenceType(
131 prefs::kDefaultSearchProviderName, 158 prefs::kDefaultSearchProviderName,
132 Value::TYPE_STRING); 159 Value::TYPE_STRING);
133 pref_value_store_->RegisterPreferenceType(prefs::kDeleteCache, 160 pref_value_store_->RegisterPreferenceType(prefs::kDeleteCache,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 user_pref::kSearchProviderNameValue); 193 user_pref::kSearchProviderNameValue);
167 user_prefs->SetString(prefs::kHomePage, user_pref::kHomepageValue); 194 user_prefs->SetString(prefs::kHomePage, user_pref::kHomepageValue);
168 return user_prefs; 195 return user_prefs;
169 } 196 }
170 197
171 DictionaryValue* CreateManagedPlatformPrefs() { 198 DictionaryValue* CreateManagedPlatformPrefs() {
172 DictionaryValue* managed_platform_prefs = new DictionaryValue(); 199 DictionaryValue* managed_platform_prefs = new DictionaryValue();
173 managed_platform_prefs->SetString( 200 managed_platform_prefs->SetString(
174 prefs::kHomePage, 201 prefs::kHomePage,
175 managed_platform_pref::kHomepageValue); 202 managed_platform_pref::kHomepageValue);
176 expected_differing_paths_.push_back(prefs::kHomePage); 203 expected_differing_paths_.insert(prefs::kHomePage);
177 return managed_platform_prefs; 204 return managed_platform_prefs;
178 } 205 }
179 206
180 DictionaryValue* CreateDeviceManagementPrefs() { 207 DictionaryValue* CreateDeviceManagementPrefs() {
181 DictionaryValue* device_management_prefs = new DictionaryValue(); 208 DictionaryValue* device_management_prefs = new DictionaryValue();
182 device_management_prefs->SetString( 209 device_management_prefs->SetString(
183 prefs::kDefaultSearchProviderName, 210 prefs::kDefaultSearchProviderName,
184 device_management_pref::kSearchProviderNameValue); 211 device_management_pref::kSearchProviderNameValue);
185 expected_differing_paths_.push_back("default_search_provider"); 212 expected_differing_paths_.insert("default_search_provider");
186 expected_differing_paths_.push_back(prefs::kDefaultSearchProviderName); 213 expected_differing_paths_.insert(prefs::kDefaultSearchProviderName);
187 device_management_prefs->SetString(prefs::kHomePage, 214 device_management_prefs->SetString(prefs::kHomePage,
188 device_management_pref::kHomepageValue); 215 device_management_pref::kHomepageValue);
189 return device_management_prefs; 216 return device_management_prefs;
190 } 217 }
191 218
192 DictionaryValue* CreateExtensionPrefs() { 219 DictionaryValue* CreateExtensionPrefs() {
193 DictionaryValue* extension_prefs = new DictionaryValue(); 220 DictionaryValue* extension_prefs = new DictionaryValue();
194 extension_prefs->SetString(prefs::kCurrentThemeID, 221 extension_prefs->SetString(prefs::kCurrentThemeID,
195 extension_pref::kCurrentThemeIDValue); 222 extension_pref::kCurrentThemeIDValue);
196 extension_prefs->SetString(prefs::kHomePage, 223 extension_prefs->SetString(prefs::kHomePage,
(...skipping 18 matching lines...) Expand all
215 } 242 }
216 243
217 DictionaryValue* CreateRecommendedPrefs() { 244 DictionaryValue* CreateRecommendedPrefs() {
218 DictionaryValue* recommended_prefs = new DictionaryValue(); 245 DictionaryValue* recommended_prefs = new DictionaryValue();
219 recommended_prefs->SetInteger(prefs::kStabilityLaunchCount, 246 recommended_prefs->SetInteger(prefs::kStabilityLaunchCount,
220 recommended_pref::kStabilityLaunchCountValue); 247 recommended_pref::kStabilityLaunchCountValue);
221 recommended_prefs->SetBoolean( 248 recommended_prefs->SetBoolean(
222 prefs::kRecommendedPref, 249 prefs::kRecommendedPref,
223 recommended_pref::kRecommendedPrefValue); 250 recommended_pref::kRecommendedPrefValue);
224 251
225 // Expected differing paths must be added in lexicographic order 252 expected_differing_paths_.insert("this");
226 // to work properly 253 expected_differing_paths_.insert("this.pref");
227 expected_differing_paths_.push_back("this"); 254 expected_differing_paths_.insert(prefs::kRecommendedPref);
228 expected_differing_paths_.push_back("this.pref"); 255 expected_differing_paths_.insert("user_experience_metrics");
229 expected_differing_paths_.push_back(prefs::kRecommendedPref); 256 expected_differing_paths_.insert("user_experience_metrics.stability");
230 expected_differing_paths_.push_back("user_experience_metrics"); 257 expected_differing_paths_.insert(prefs::kStabilityLaunchCount);
231 expected_differing_paths_.push_back("user_experience_metrics.stability");
232 expected_differing_paths_.push_back(prefs::kStabilityLaunchCount);
233 return recommended_prefs; 258 return recommended_prefs;
234 } 259 }
235 260
236 DictionaryValue* CreateDefaultPrefs() { 261 DictionaryValue* CreateDefaultPrefs() {
237 DictionaryValue* default_prefs = new DictionaryValue(); 262 DictionaryValue* default_prefs = new DictionaryValue();
238 default_prefs->SetInteger(prefs::kDefaultPref, default_pref::kDefaultValue); 263 default_prefs->SetInteger(prefs::kDefaultPref, default_pref::kDefaultValue);
239 return default_prefs; 264 return default_prefs;
240 } 265 }
241 266
242 DictionaryValue* CreateSampleDictValue() { 267 DictionaryValue* CreateSampleDictValue() {
(...skipping 11 matching lines...) Expand all
254 sample_list->Set(2, Value::CreateIntegerValue(2)); 279 sample_list->Set(2, Value::CreateIntegerValue(2));
255 sample_list->Set(3, Value::CreateIntegerValue(3)); 280 sample_list->Set(3, Value::CreateIntegerValue(3));
256 return sample_list; 281 return sample_list;
257 } 282 }
258 283
259 virtual void TearDown() { 284 virtual void TearDown() {
260 loop_.RunAllPending(); 285 loop_.RunAllPending();
261 } 286 }
262 287
263 MessageLoop loop_; 288 MessageLoop loop_;
264 289 MockPrefNotifier pref_notifier_;
265 scoped_refptr<TestingPrefValueStore> pref_value_store_; 290 scoped_refptr<PrefValueStore> pref_value_store_;
266 291
267 // |PrefStore|s are owned by the |PrefValueStore|. 292 // |PrefStore|s are owned by the |PrefValueStore|.
268 DummyPrefStore* managed_platform_pref_store_; 293 TestingPrefStore* managed_platform_pref_store_;
269 DummyPrefStore* device_management_pref_store_; 294 TestingPrefStore* device_management_pref_store_;
270 DummyPrefStore* extension_pref_store_; 295 TestingPrefStore* extension_pref_store_;
271 DummyPrefStore* command_line_pref_store_; 296 TestingPrefStore* command_line_pref_store_;
272 DummyPrefStore* user_pref_store_; 297 TestingPrefStore* user_pref_store_;
273 DummyPrefStore* recommended_pref_store_; 298 TestingPrefStore* recommended_pref_store_;
274 DummyPrefStore* default_pref_store_; 299 TestingPrefStore* default_pref_store_;
275 300
276 // A vector of the preferences paths in the managed and recommended 301 // A vector of the preferences paths in the managed and recommended
277 // PrefStores that are set at the beginning of a test. Can be modified 302 // PrefStores that are set at the beginning of a test. Can be modified
278 // by the test to track changes that it makes to the preferences 303 // by the test to track changes that it makes to the preferences
279 // stored in the managed and recommended PrefStores. 304 // stored in the managed and recommended PrefStores.
280 std::vector<std::string> expected_differing_paths_; 305 std::set<std::string> expected_differing_paths_;
281 306
282 // Preferences are owned by the individual |DummyPrefStores|. 307 // Preferences are owned by the individual |TestingPrefStores|.
283 DictionaryValue* managed_platform_prefs_; 308 DictionaryValue* managed_platform_prefs_;
284 DictionaryValue* device_management_prefs_; 309 DictionaryValue* device_management_prefs_;
285 DictionaryValue* extension_prefs_; 310 DictionaryValue* extension_prefs_;
286 DictionaryValue* command_line_prefs_; 311 DictionaryValue* command_line_prefs_;
287 DictionaryValue* user_prefs_; 312 DictionaryValue* user_prefs_;
288 DictionaryValue* recommended_prefs_; 313 DictionaryValue* recommended_prefs_;
289 DictionaryValue* default_prefs_; 314 DictionaryValue* default_prefs_;
290 315
291 private: 316 private:
292 scoped_ptr<BrowserThread> ui_thread_; 317 scoped_ptr<BrowserThread> ui_thread_;
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 // User preference 445 // User preference
421 EXPECT_TRUE(pref_value_store_->HasPrefPath(prefs::kDeleteCache)); 446 EXPECT_TRUE(pref_value_store_->HasPrefPath(prefs::kDeleteCache));
422 // Recommended preference 447 // Recommended preference
423 EXPECT_TRUE(pref_value_store_->HasPrefPath(prefs::kRecommendedPref)); 448 EXPECT_TRUE(pref_value_store_->HasPrefPath(prefs::kRecommendedPref));
424 // Default preference 449 // Default preference
425 EXPECT_FALSE(pref_value_store_->HasPrefPath(prefs::kDefaultPref)); 450 EXPECT_FALSE(pref_value_store_->HasPrefPath(prefs::kDefaultPref));
426 // Unknown preference 451 // Unknown preference
427 EXPECT_FALSE(pref_value_store_->HasPrefPath(prefs::kMissingPref)); 452 EXPECT_FALSE(pref_value_store_->HasPrefPath(prefs::kMissingPref));
428 } 453 }
429 454
430 TEST_F(PrefValueStoreTest, PrefHasChanged) { 455 TEST_F(PrefValueStoreTest, PrefChanges) {
431 // Setup. 456 // Setup.
432 const char managed_platform_pref_path[] = "managed_platform_pref"; 457 const char managed_platform_pref_path[] = "managed_platform_pref";
433 pref_value_store_->RegisterPreferenceType(managed_platform_pref_path, 458 pref_value_store_->RegisterPreferenceType(managed_platform_pref_path,
434 Value::TYPE_STRING); 459 Value::TYPE_STRING);
435 managed_platform_pref_store_->prefs()->SetString(managed_platform_pref_path, 460 managed_platform_pref_store_->prefs()->SetString(managed_platform_pref_path,
436 "managed value"); 461 "managed value");
437 const char user_pref_path[] = "user_pref"; 462 const char user_pref_path[] = "user_pref";
438 pref_value_store_->RegisterPreferenceType(user_pref_path, Value::TYPE_STRING); 463 pref_value_store_->RegisterPreferenceType(user_pref_path, Value::TYPE_STRING);
439 user_pref_store_->prefs()->SetString(user_pref_path, "user value"); 464 user_pref_store_->prefs()->SetString(user_pref_path, "user value");
440 const char default_pref_path[] = "default_pref"; 465 const char default_pref_path[] = "default_pref";
441 pref_value_store_->RegisterPreferenceType(default_pref_path, 466 pref_value_store_->RegisterPreferenceType(default_pref_path,
442 Value::TYPE_STRING); 467 Value::TYPE_STRING);
443 default_pref_store_->prefs()->SetString(default_pref_path, "default value"); 468 default_pref_store_->prefs()->SetString(default_pref_path, "default value");
444 469
445 // Check pref controlled by highest-priority store. 470 // Check pref controlled by highest-priority store.
446 EXPECT_TRUE(pref_value_store_->PrefHasChanged(managed_platform_pref_path, 471 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(managed_platform_pref_path));
447 static_cast<PrefNotifier::PrefStoreType>(0))); 472 managed_platform_pref_store_->NotifyPrefValueChanged(
448 EXPECT_FALSE(pref_value_store_->PrefHasChanged(managed_platform_pref_path, 473 managed_platform_pref_path);
449 PrefNotifier::USER_STORE)); 474 Mock::VerifyAndClearExpectations(&pref_notifier_);
475
476 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(_)).Times(0);
477 user_pref_store_->NotifyPrefValueChanged(managed_platform_pref_path);
478 Mock::VerifyAndClearExpectations(&pref_notifier_);
450 479
451 // Check pref controlled by user store. 480 // Check pref controlled by user store.
452 EXPECT_TRUE(pref_value_store_->PrefHasChanged(user_pref_path, 481 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(user_pref_path));
453 static_cast<PrefNotifier::PrefStoreType>(0))); 482 managed_platform_pref_store_->NotifyPrefValueChanged(user_pref_path);
454 EXPECT_TRUE(pref_value_store_->PrefHasChanged(user_pref_path, 483 Mock::VerifyAndClearExpectations(&pref_notifier_);
455 PrefNotifier::USER_STORE)); 484
456 EXPECT_FALSE(pref_value_store_->PrefHasChanged(user_pref_path, 485 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(user_pref_path));
457 PrefNotifier::PREF_STORE_TYPE_MAX)); 486 user_pref_store_->NotifyPrefValueChanged(user_pref_path);
487 Mock::VerifyAndClearExpectations(&pref_notifier_);
488
489 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(_)).Times(0);
490 default_pref_store_->NotifyPrefValueChanged(user_pref_path);
491 Mock::VerifyAndClearExpectations(&pref_notifier_);
458 492
459 // Check pref controlled by default-pref store. 493 // Check pref controlled by default-pref store.
460 EXPECT_TRUE(pref_value_store_->PrefHasChanged(default_pref_path, 494 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(default_pref_path));
461 PrefNotifier::USER_STORE)); 495 user_pref_store_->NotifyPrefValueChanged(default_pref_path);
462 EXPECT_TRUE(pref_value_store_->PrefHasChanged(default_pref_path, 496 Mock::VerifyAndClearExpectations(&pref_notifier_);
463 PrefNotifier::DEFAULT_STORE)); 497
498 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(default_pref_path));
499 default_pref_store_->NotifyPrefValueChanged(default_pref_path);
500 Mock::VerifyAndClearExpectations(&pref_notifier_);
464 } 501 }
465 502
466 TEST_F(PrefValueStoreTest, ReadPrefs) { 503 TEST_F(PrefValueStoreTest, ReadPrefs) {
467 pref_value_store_->ReadPrefs(); 504 pref_value_store_->ReadPrefs();
468 // The ReadPrefs method of the |DummyPrefStore| deletes the |pref_store|s 505 // The ReadPrefs method of the |TestingPrefStore| deletes the |pref_store|s
469 // internal dictionary and creates a new empty dictionary. Hence this 506 // internal dictionary and creates a new empty dictionary. Hence this
470 // dictionary does not contain any of the preloaded preferences. 507 // dictionary does not contain any of the preloaded preferences.
471 // This shows that the ReadPrefs method of the |DummyPrefStore| was called. 508 // This shows that the ReadPrefs method of the |TestingPrefStore| was called.
472 EXPECT_FALSE(pref_value_store_->HasPrefPath(prefs::kDeleteCache)); 509 EXPECT_FALSE(pref_value_store_->HasPrefPath(prefs::kDeleteCache));
473 } 510 }
474 511
475 TEST_F(PrefValueStoreTest, WritePrefs) { 512 TEST_F(PrefValueStoreTest, WritePrefs) {
476 user_pref_store_->set_prefs_written(false); 513 user_pref_store_->set_prefs_written(false);
477 pref_value_store_->WritePrefs(); 514 pref_value_store_->WritePrefs();
478 ASSERT_TRUE(user_pref_store_->get_prefs_written()); 515 ASSERT_TRUE(user_pref_store_->get_prefs_written());
479 } 516 }
480 517
481 TEST_F(PrefValueStoreTest, SetUserPrefValue) { 518 TEST_F(PrefValueStoreTest, SetUserPrefValue) {
482 Value* new_value = NULL; 519 Value* new_value = NULL;
483 Value* actual_value = NULL; 520 Value* actual_value = NULL;
484 521
485 // Test that managed platform values can not be set. 522 // Test that managed platform values can not be set.
523 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(_)).Times(0);
486 ASSERT_TRUE(pref_value_store_->PrefValueInManagedPlatformStore( 524 ASSERT_TRUE(pref_value_store_->PrefValueInManagedPlatformStore(
487 prefs::kHomePage)); 525 prefs::kHomePage));
488 // The Ownership is tranfered to |PrefValueStore|. 526 // The ownership is transfered to PrefValueStore.
489 new_value = Value::CreateStringValue("http://www.youtube.com"); 527 new_value = Value::CreateStringValue("http://www.youtube.com");
490 pref_value_store_->SetUserPrefValue(prefs::kHomePage, new_value); 528 pref_value_store_->SetUserPrefValue(prefs::kHomePage, new_value);
529 Mock::VerifyAndClearExpectations(&pref_notifier_);
491 530
492 ASSERT_TRUE(pref_value_store_->GetValue(prefs::kHomePage, &actual_value)); 531 ASSERT_TRUE(pref_value_store_->GetValue(prefs::kHomePage, &actual_value));
493 std::string value_str; 532 std::string value_str;
494 actual_value->GetAsString(&value_str); 533 actual_value->GetAsString(&value_str);
495 ASSERT_EQ(managed_platform_pref::kHomepageValue, value_str); 534 ASSERT_EQ(managed_platform_pref::kHomepageValue, value_str);
496 535
497 // User preferences values can be set 536 // User preferences values can be set.
498 ASSERT_FALSE(pref_value_store_->PrefValueInManagedPlatformStore( 537 ASSERT_FALSE(pref_value_store_->PrefValueInManagedPlatformStore(
499 prefs::kStabilityLaunchCount)); 538 prefs::kStabilityLaunchCount));
500 actual_value = NULL; 539 actual_value = NULL;
501 pref_value_store_->GetValue(prefs::kStabilityLaunchCount, &actual_value); 540 pref_value_store_->GetValue(prefs::kStabilityLaunchCount, &actual_value);
502 int int_value; 541 int int_value;
503 EXPECT_TRUE(actual_value->GetAsInteger(&int_value)); 542 EXPECT_TRUE(actual_value->GetAsInteger(&int_value));
504 EXPECT_EQ(user_pref::kStabilityLaunchCountValue, int_value); 543 EXPECT_EQ(user_pref::kStabilityLaunchCountValue, int_value);
505 544
545 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(_)).Times(1);
506 new_value = Value::CreateIntegerValue(1); 546 new_value = Value::CreateIntegerValue(1);
507 pref_value_store_->SetUserPrefValue(prefs::kStabilityLaunchCount, new_value); 547 pref_value_store_->SetUserPrefValue(prefs::kStabilityLaunchCount, new_value);
508 actual_value = NULL; 548 actual_value = NULL;
509 pref_value_store_->GetValue(prefs::kStabilityLaunchCount, &actual_value); 549 pref_value_store_->GetValue(prefs::kStabilityLaunchCount, &actual_value);
510 EXPECT_TRUE(new_value->Equals(actual_value)); 550 EXPECT_TRUE(new_value->Equals(actual_value));
551 Mock::VerifyAndClearExpectations(&pref_notifier_);
511 552
512 // Set and Get |DictionaryValue| 553 // Set and Get DictionaryValue.
554 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(_)).Times(1);
513 DictionaryValue* expected_dict_value = CreateSampleDictValue(); 555 DictionaryValue* expected_dict_value = CreateSampleDictValue();
514 pref_value_store_->SetUserPrefValue(prefs::kSampleDict, expected_dict_value); 556 pref_value_store_->SetUserPrefValue(prefs::kSampleDict, expected_dict_value);
557 Mock::VerifyAndClearExpectations(&pref_notifier_);
515 558
516 actual_value = NULL; 559 actual_value = NULL;
517 std::string key(prefs::kSampleDict); 560 std::string key(prefs::kSampleDict);
518 pref_value_store_->GetValue(key, &actual_value); 561 pref_value_store_->GetValue(key, &actual_value);
519 562
520 ASSERT_EQ(expected_dict_value, actual_value); 563 ASSERT_EQ(expected_dict_value, actual_value);
521 ASSERT_TRUE(expected_dict_value->Equals(actual_value)); 564 ASSERT_TRUE(expected_dict_value->Equals(actual_value));
522 565
523 // Set and Get a |ListValue| 566 // Set and Get a ListValue.
567 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(_)).Times(1);
524 ListValue* expected_list_value = CreateSampleListValue(); 568 ListValue* expected_list_value = CreateSampleListValue();
525 pref_value_store_->SetUserPrefValue(prefs::kSampleList, expected_list_value); 569 pref_value_store_->SetUserPrefValue(prefs::kSampleList, expected_list_value);
570 Mock::VerifyAndClearExpectations(&pref_notifier_);
526 571
527 actual_value = NULL; 572 actual_value = NULL;
528 key = prefs::kSampleList; 573 key = prefs::kSampleList;
529 pref_value_store_->GetValue(key, &actual_value); 574 pref_value_store_->GetValue(key, &actual_value);
530 575
531 ASSERT_EQ(expected_list_value, actual_value); 576 ASSERT_EQ(expected_list_value, actual_value);
532 ASSERT_TRUE(expected_list_value->Equals(actual_value)); 577 ASSERT_TRUE(expected_list_value->Equals(actual_value));
533 } 578 }
534 579
535 TEST_F(PrefValueStoreTest, PrefValueInManagedPlatformStore) { 580 TEST_F(PrefValueStoreTest, PrefValueInManagedPlatformStore) {
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
739 EXPECT_FALSE( 784 EXPECT_FALSE(
740 pref_value_store_->PrefValueFromDefaultStore(prefs::kMissingPref)); 785 pref_value_store_->PrefValueFromDefaultStore(prefs::kMissingPref));
741 } 786 }
742 787
743 TEST_F(PrefValueStoreTest, TestPolicyRefresh) { 788 TEST_F(PrefValueStoreTest, TestPolicyRefresh) {
744 // pref_value_store_ is initialized by PrefValueStoreTest to have values in 789 // pref_value_store_ is initialized by PrefValueStoreTest to have values in
745 // the managed platform, device management and recommended stores. By 790 // the managed platform, device management and recommended stores. By
746 // replacing them with dummy stores, all of the paths of the prefs originally 791 // replacing them with dummy stores, all of the paths of the prefs originally
747 // in the managed platform, device management and recommended stores should 792 // in the managed platform, device management and recommended stores should
748 // change. 793 // change.
749 MockPolicyRefreshCallback callback; 794 pref_value_store_->RefreshPolicyPrefs();
750 EXPECT_CALL(callback, DoCallback(_)).Times(0); 795
796 PrefChangeRecorder recorder;
797 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(_))
798 .WillRepeatedly(Invoke(&recorder, &PrefChangeRecorder::Record));
799 loop_.RunAllPending();
800 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs());
801 }
802
803 TEST_F(PrefValueStoreTest, TestRefreshPolicyPrefsCompletion) {
804 PrefChangeRecorder recorder;
805 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(_))
806 .WillRepeatedly(Invoke(&recorder, &PrefChangeRecorder::Record));
807
808 // Test changed preferences in the managed platform store and removed
809 // preferences in the recommended store. In addition to "homepage", the other
810 // prefs that are set by default in the test class are removed by the
811 // DummyStore.
812 scoped_ptr<TestingPrefStore> new_managed_platform_store(
813 new TestingPrefStore());
814 DictionaryValue* dict = new DictionaryValue();
815 dict->SetString("homepage", "some other changed homepage");
816 new_managed_platform_store->set_prefs(dict);
817
818 recorder.Clear();
819 pref_value_store_->RefreshPolicyPrefsCompletion(
820 new_managed_platform_store.release(),
821 new TestingPrefStore(),
822 new TestingPrefStore());
823 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs());
824
825 // Test properties that have been removed from the managed platform store.
826 // Homepage is still set in managed prefs.
827 expected_differing_paths_.clear();
828 expected_differing_paths_.insert(prefs::kHomePage);
829
830 recorder.Clear();
831 pref_value_store_->RefreshPolicyPrefsCompletion(
832 new TestingPrefStore(),
833 new TestingPrefStore(),
834 new TestingPrefStore());
835 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs());
836
837 // Test properties that are added to the device management store.
838 expected_differing_paths_.clear();
839 expected_differing_paths_.insert(prefs::kHomePage);
840 scoped_ptr<TestingPrefStore> new_device_management_store(
841 new TestingPrefStore());
842 dict = new DictionaryValue();
843 dict->SetString("homepage", "some other changed homepage");
844 new_device_management_store->set_prefs(dict);
845
846 recorder.Clear();
847 pref_value_store_->RefreshPolicyPrefsCompletion(
848 new TestingPrefStore(),
849 new_device_management_store.release(),
850 new TestingPrefStore());
851 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs());
852
853 // Test properties that are added to the recommended store.
854 scoped_ptr<TestingPrefStore> new_recommended_store(new TestingPrefStore());
855 dict = new DictionaryValue();
856 dict->SetString("homepage", "some other changed homepage 2");
857 new_recommended_store->set_prefs(dict);
858 expected_differing_paths_.clear();
859 expected_differing_paths_.insert(prefs::kHomePage);
860
861 recorder.Clear();
862 pref_value_store_->RefreshPolicyPrefsCompletion(
863 new TestingPrefStore(),
864 new TestingPrefStore(),
865 new_recommended_store.release());
866 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs());
867
868 // Test adding a multi-key path.
869 new_managed_platform_store.reset(new TestingPrefStore());
870 dict = new DictionaryValue();
871 dict->SetString("segment1.segment2", "value");
872 new_managed_platform_store->set_prefs(dict);
873 expected_differing_paths_.clear();
874 expected_differing_paths_.insert(prefs::kHomePage);
875 expected_differing_paths_.insert("segment1");
876 expected_differing_paths_.insert("segment1.segment2");
877
878 recorder.Clear();
879 pref_value_store_->RefreshPolicyPrefsCompletion(
880 new_managed_platform_store.release(),
881 new TestingPrefStore(),
882 new TestingPrefStore());
883 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs());
884 }
885
886 TEST_F(PrefValueStoreTest, TestConcurrentPolicyRefresh) {
887 PrefChangeRecorder recorder;
888 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(_))
889 .WillRepeatedly(Invoke(&recorder, &PrefChangeRecorder::Record));
890
751 BrowserThread::PostTask( 891 BrowserThread::PostTask(
752 BrowserThread::UI, FROM_HERE, 892 BrowserThread::UI, FROM_HERE,
753 NewRunnableMethod( 893 NewRunnableMethod(
754 pref_value_store_.get(), 894 pref_value_store_.get(),
755 &PrefValueStore::RefreshPolicyPrefs, 895 &PrefValueStore::RefreshPolicyPrefs));
756 NewCallback(&callback,
757 &MockPolicyRefreshCallback::DoCallback)));
758 Mock::VerifyAndClearExpectations(&callback);
759 EXPECT_CALL(callback, DoCallback(expected_differing_paths_)).Times(1);
760 loop_.RunAllPending();
761 }
762 896
763 TEST_F(PrefValueStoreTest, TestRefreshPolicyPrefsCompletion) {
764 // Test changed preferences in the managed platform store and removed
765 // preferences in the recommended store. In addition to "homepage", the other
766 // prefs that are set by default in the test class are removed by the
767 // DummyStore.
768 scoped_ptr<DummyPrefStore> new_managed_platform_store(new DummyPrefStore());
769 DictionaryValue* dict = new DictionaryValue();
770 dict->SetString("homepage", "some other changed homepage");
771 new_managed_platform_store->set_prefs(dict);
772 MockPolicyRefreshCallback callback;
773 EXPECT_CALL(callback, DoCallback(expected_differing_paths_)).Times(1);
774 pref_value_store_->RefreshPolicyPrefsCompletion(
775 new_managed_platform_store.release(),
776 new DummyPrefStore(),
777 new DummyPrefStore(),
778 NewCallback(&callback,
779 &MockPolicyRefreshCallback::DoCallback));
780
781 // Test properties that have been removed from the managed platform store.
782 // Homepage is still set in managed prefs.
783 expected_differing_paths_.clear();
784 expected_differing_paths_.push_back(std::string("homepage"));
785 MockPolicyRefreshCallback callback2;
786 EXPECT_CALL(callback2, DoCallback(expected_differing_paths_)).Times(1);
787 pref_value_store_->RefreshPolicyPrefsCompletion(
788 new DummyPrefStore(),
789 new DummyPrefStore(),
790 new DummyPrefStore(),
791 NewCallback(&callback2,
792 &MockPolicyRefreshCallback::DoCallback));
793
794 // Test properties that are added to the device management store.
795 expected_differing_paths_.clear();
796 expected_differing_paths_.push_back(std::string("homepage"));
797 scoped_ptr<DummyPrefStore> new_device_management_store(
798 new DummyPrefStore());
799 dict = new DictionaryValue();
800 dict->SetString("homepage", "some other changed homepage");
801 new_device_management_store->set_prefs(dict);
802 MockPolicyRefreshCallback callback3;
803 EXPECT_CALL(callback3, DoCallback(expected_differing_paths_)).Times(1);
804 pref_value_store_->RefreshPolicyPrefsCompletion(
805 new DummyPrefStore(),
806 new_device_management_store.release(),
807 new DummyPrefStore(),
808 NewCallback(&callback3,
809 &MockPolicyRefreshCallback::DoCallback));
810
811 // Test properties that are added to the recommended store.
812 scoped_ptr<DummyPrefStore> new_recommended_store(new DummyPrefStore());
813 dict = new DictionaryValue();
814 dict->SetString("homepage", "some other changed homepage 2");
815 new_recommended_store->set_prefs(dict);
816 expected_differing_paths_.clear();
817 expected_differing_paths_.push_back(std::string("homepage"));
818 MockPolicyRefreshCallback callback4;
819 EXPECT_CALL(callback4, DoCallback(expected_differing_paths_)).Times(1);
820 pref_value_store_->RefreshPolicyPrefsCompletion(
821 new DummyPrefStore(),
822 new DummyPrefStore(),
823 new_recommended_store.release(),
824 NewCallback(&callback4,
825 &MockPolicyRefreshCallback::DoCallback));
826
827 // Test adding a multi-key path.
828 new_managed_platform_store.reset(new DummyPrefStore());
829 dict = new DictionaryValue();
830 dict->SetString("segment1.segment2", "value");
831 new_managed_platform_store->set_prefs(dict);
832 expected_differing_paths_.clear();
833 expected_differing_paths_.push_back(std::string("homepage"));
834 expected_differing_paths_.push_back(std::string("segment1"));
835 expected_differing_paths_.push_back(std::string("segment1.segment2"));
836 MockPolicyRefreshCallback callback5;
837 EXPECT_CALL(callback5, DoCallback(expected_differing_paths_)).Times(1);
838 pref_value_store_->RefreshPolicyPrefsCompletion(
839 new_managed_platform_store.release(),
840 new DummyPrefStore(),
841 new DummyPrefStore(),
842 NewCallback(&callback5,
843 &MockPolicyRefreshCallback::DoCallback));
844 }
845
846 TEST_F(PrefValueStoreTest, TestConcurrentPolicyRefresh) {
847 MockPolicyRefreshCallback callback1;
848 BrowserThread::PostTask( 897 BrowserThread::PostTask(
849 BrowserThread::UI, FROM_HERE, 898 BrowserThread::UI, FROM_HERE,
850 NewRunnableMethod( 899 NewRunnableMethod(
851 pref_value_store_.get(), 900 pref_value_store_.get(),
852 &PrefValueStore::RefreshPolicyPrefs, 901 &PrefValueStore::RefreshPolicyPrefs));
853 NewCallback(&callback1,
854 &MockPolicyRefreshCallback::DoCallback)));
855 EXPECT_CALL(callback1, DoCallback(_)).Times(0);
856 902
857 MockPolicyRefreshCallback callback2;
858 BrowserThread::PostTask( 903 BrowserThread::PostTask(
859 BrowserThread::UI, FROM_HERE, 904 BrowserThread::UI, FROM_HERE,
860 NewRunnableMethod( 905 NewRunnableMethod(
861 pref_value_store_.get(), 906 pref_value_store_.get(),
862 &PrefValueStore::RefreshPolicyPrefs, 907 &PrefValueStore::RefreshPolicyPrefs));
863 NewCallback(&callback2,
864 &MockPolicyRefreshCallback::DoCallback)));
865 EXPECT_CALL(callback2, DoCallback(_)).Times(0);
866 908
867 MockPolicyRefreshCallback callback3;
868 BrowserThread::PostTask(
869 BrowserThread::UI, FROM_HERE,
870 NewRunnableMethod(
871 pref_value_store_.get(),
872 &PrefValueStore::RefreshPolicyPrefs,
873 NewCallback(&callback3,
874 &MockPolicyRefreshCallback::DoCallback)));
875 EXPECT_CALL(callback3, DoCallback(_)).Times(0);
876 Mock::VerifyAndClearExpectations(&callback1);
877 Mock::VerifyAndClearExpectations(&callback2);
878 Mock::VerifyAndClearExpectations(&callback3);
879
880 EXPECT_CALL(callback1, DoCallback(expected_differing_paths_)).Times(1);
881 std::vector<std::string> no_differing_paths;
882 EXPECT_CALL(callback2, DoCallback(no_differing_paths)).Times(1);
883 EXPECT_CALL(callback3, DoCallback(no_differing_paths)).Times(1);
884 loop_.RunAllPending(); 909 loop_.RunAllPending();
910 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs());
885 } 911 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698