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 3 // found in the LICENSE file. 4 5 #include "base/values.h" 6 #include "chrome/browser/prefs/overlay_persistent_pref_store.h" 7 #include "chrome/browser/prefs/testing_pref_store.h" 8 #include "chrome/common/pref_store_observer_mock.h" 9 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 using ::testing::Mock; 13 using ::testing::StrEq; 14 15 const char key[] = "test.key"; 16 17 class OverlayPersistentPrefStoreTest : public testing::Test { 18 public: 19 OverlayPersistentPrefStoreTest() 20 : underlay_(new TestingPrefStore()), 21 overlay_(new OverlayPersistentPrefStore(underlay_.get())) { 22 } 23 24 scoped_refptr<TestingPrefStore> underlay_; 25 scoped_refptr<OverlayPersistentPrefStore> overlay_; 26 }; 27 28 TEST_F(OverlayPersistentPrefStoreTest, Observer) { 29 PrefStoreObserverMock obs; 30 overlay_->AddObserver(&obs); 31 32 // Check that underlay first value is reported. 33 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(key))).Times(1); 34 underlay_->SetValue(key, Value::CreateIntegerValue(42)); 35 Mock::VerifyAndClearExpectations(&obs); 36 37 // Check that underlay overwriting is reported. 38 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(key))).Times(1); 39 underlay_->SetValue(key, Value::CreateIntegerValue(43)); 40 Mock::VerifyAndClearExpectations(&obs); 41 42 // Check that overwriting change in overlay is reported. 43 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(key))).Times(1); 44 overlay_->SetValue(key, Value::CreateIntegerValue(44)); 45 Mock::VerifyAndClearExpectations(&obs); 46 47 // Check that hidden underlay change is not reported. 48 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(key))).Times(0); 49 underlay_->SetValue(key, Value::CreateIntegerValue(45)); 50 Mock::VerifyAndClearExpectations(&obs); 51 52 // Check that overlay remove is reported. 53 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(key))).Times(1); 54 overlay_->RemoveValue(key); 55 Mock::VerifyAndClearExpectations(&obs); 56 57 // Check that underlay remove is reported. 58 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(key))).Times(1); 59 underlay_->RemoveValue(key); 60 Mock::VerifyAndClearExpectations(&obs); 61 62 // Check respecting of silence. 63 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(key))).Times(0); 64 overlay_->SetValueSilently(key, Value::CreateIntegerValue(46)); 65 Mock::VerifyAndClearExpectations(&obs); 66 67 overlay_->RemoveObserver(&obs); 68 69 // Check successful unsubscription. 70 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(key))).Times(0); 71 underlay_->SetValue(key, Value::CreateIntegerValue(47)); 72 overlay_->SetValue(key, Value::CreateIntegerValue(48)); 73 Mock::VerifyAndClearExpectations(&obs); 74 } 75 76 TEST_F(OverlayPersistentPrefStoreTest, GetAndSet) { 77 const Value* value = NULL; 78 int i = -1; 79 EXPECT_EQ(PrefStore::READ_NO_VALUE, overlay_->GetValue(key, &value)); 80 EXPECT_EQ(PrefStore::READ_NO_VALUE, underlay_->GetValue(key, &value)); 81 82 underlay_->SetValue(key, Value::CreateIntegerValue(42)); 83 84 // Value shines through: 85 EXPECT_EQ(PrefStore::READ_OK, overlay_->GetValue(key, &value)); 86 ASSERT_TRUE(value); 87 EXPECT_TRUE(value->GetAsInteger(&i)); 88 EXPECT_EQ(42, i); 89 90 EXPECT_EQ(PrefStore::READ_OK, underlay_->GetValue(key, &value)); 91 ASSERT_TRUE(value); 92 EXPECT_TRUE(value->GetAsInteger(&i)); 93 EXPECT_EQ(42, i); 94 95 overlay_->SetValue(key, Value::CreateIntegerValue(43)); 96 97 EXPECT_EQ(PrefStore::READ_OK, overlay_->GetValue(key, &value)); 98 ASSERT_TRUE(value); 99 EXPECT_TRUE(value->GetAsInteger(&i)); 100 EXPECT_EQ(43, i); 101 102 EXPECT_EQ(PrefStore::READ_OK, underlay_->GetValue(key, &value)); 103 ASSERT_TRUE(value); 104 EXPECT_TRUE(value->GetAsInteger(&i)); 105 EXPECT_EQ(42, i); 106 107 overlay_->RemoveValue(key); 108 109 // Value shines through: 110 EXPECT_EQ(PrefStore::READ_OK, overlay_->GetValue(key, &value)); 111 ASSERT_TRUE(value); 112 EXPECT_TRUE(value->GetAsInteger(&i)); 113 EXPECT_EQ(42, i); 114 115 EXPECT_EQ(PrefStore::READ_OK, underlay_->GetValue(key, &value)); 116 ASSERT_TRUE(value); 117 EXPECT_TRUE(value->GetAsInteger(&i)); 118 EXPECT_EQ(42, i); 119 } 120 121 // Check that GetMutableValue does not return the dictionary of the underlay. 122 TEST_F(OverlayPersistentPrefStoreTest, ModifyDictionaries) { 123 underlay_->SetValue(key, new DictionaryValue); 124 125 Value* modify = NULL; 126 EXPECT_EQ(PrefStore::READ_OK, overlay_->GetMutableValue(key, &modify)); 127 ASSERT_TRUE(modify); 128 ASSERT_TRUE(modify->GetType() == Value::TYPE_DICTIONARY); 129 static_cast<DictionaryValue*>(modify)->SetInteger(key, 42); 130 131 Value* original_in_underlay = NULL; 132 EXPECT_EQ(PrefStore::READ_OK, 133 underlay_->GetMutableValue(key, &original_in_underlay)); 134 ASSERT_TRUE(original_in_underlay); 135 ASSERT_TRUE(original_in_underlay->GetType() == Value::TYPE_DICTIONARY); 136 EXPECT_TRUE(static_cast<DictionaryValue*>(original_in_underlay)->empty()); 137 138 Value* modified = NULL; 139 EXPECT_EQ(PrefStore::READ_OK, overlay_->GetMutableValue(key, &modified)); 140 ASSERT_TRUE(modified); 141 ASSERT_TRUE(modified->GetType() == Value::TYPE_DICTIONARY); 142 EXPECT_TRUE(Value::Equals(modify, static_cast<DictionaryValue*>(modified))); 143 } 144