| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/prefs/mock_pref_change_callback.h" | |
| 6 #include "base/prefs/pref_change_registrar.h" | |
| 7 #include "base/prefs/pref_registry_simple.h" | |
| 8 #include "base/prefs/scoped_user_pref_update.h" | |
| 9 #include "base/prefs/testing_pref_service.h" | |
| 10 #include "testing/gmock/include/gmock/gmock.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 using testing::_; | |
| 14 using testing::Mock; | |
| 15 | |
| 16 class ScopedUserPrefUpdateTest : public testing::Test { | |
| 17 public: | |
| 18 ScopedUserPrefUpdateTest() : observer_(&prefs_) {} | |
| 19 ~ScopedUserPrefUpdateTest() override {} | |
| 20 | |
| 21 protected: | |
| 22 void SetUp() override { | |
| 23 prefs_.registry()->RegisterDictionaryPref(kPref); | |
| 24 registrar_.Init(&prefs_); | |
| 25 registrar_.Add(kPref, observer_.GetCallback()); | |
| 26 } | |
| 27 | |
| 28 static const char kPref[]; | |
| 29 static const char kKey[]; | |
| 30 static const char kValue[]; | |
| 31 | |
| 32 TestingPrefServiceSimple prefs_; | |
| 33 MockPrefChangeCallback observer_; | |
| 34 PrefChangeRegistrar registrar_; | |
| 35 }; | |
| 36 | |
| 37 const char ScopedUserPrefUpdateTest::kPref[] = "name"; | |
| 38 const char ScopedUserPrefUpdateTest::kKey[] = "key"; | |
| 39 const char ScopedUserPrefUpdateTest::kValue[] = "value"; | |
| 40 | |
| 41 TEST_F(ScopedUserPrefUpdateTest, RegularUse) { | |
| 42 // Dictionary that will be expected to be set at the end. | |
| 43 base::DictionaryValue expected_dictionary; | |
| 44 expected_dictionary.SetString(kKey, kValue); | |
| 45 | |
| 46 { | |
| 47 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); | |
| 48 DictionaryPrefUpdate update(&prefs_, kPref); | |
| 49 base::DictionaryValue* value = update.Get(); | |
| 50 ASSERT_TRUE(value); | |
| 51 value->SetString(kKey, kValue); | |
| 52 | |
| 53 // The dictionary was created for us but the creation should have happened | |
| 54 // silently without notifications. | |
| 55 Mock::VerifyAndClearExpectations(&observer_); | |
| 56 | |
| 57 // Modifications happen online and are instantly visible, though. | |
| 58 const base::DictionaryValue* current_value = prefs_.GetDictionary(kPref); | |
| 59 ASSERT_TRUE(current_value); | |
| 60 EXPECT_TRUE(expected_dictionary.Equals(current_value)); | |
| 61 | |
| 62 // Now we are leaving the scope of the update so we should be notified. | |
| 63 observer_.Expect(kPref, &expected_dictionary); | |
| 64 } | |
| 65 Mock::VerifyAndClearExpectations(&observer_); | |
| 66 | |
| 67 const base::DictionaryValue* current_value = prefs_.GetDictionary(kPref); | |
| 68 ASSERT_TRUE(current_value); | |
| 69 EXPECT_TRUE(expected_dictionary.Equals(current_value)); | |
| 70 } | |
| 71 | |
| 72 TEST_F(ScopedUserPrefUpdateTest, NeverTouchAnything) { | |
| 73 const base::DictionaryValue* old_value = prefs_.GetDictionary(kPref); | |
| 74 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); | |
| 75 { | |
| 76 DictionaryPrefUpdate update(&prefs_, kPref); | |
| 77 } | |
| 78 const base::DictionaryValue* new_value = prefs_.GetDictionary(kPref); | |
| 79 EXPECT_EQ(old_value, new_value); | |
| 80 Mock::VerifyAndClearExpectations(&observer_); | |
| 81 } | |
| OLD | NEW |