OLD | NEW |
(Empty) | |
| 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/incognito_user_pref_store.h" |
| 7 #include "chrome/browser/prefs/testing_pref_store.h" |
| 8 #include "chrome/common/pref_names.h" |
| 9 #include "chrome/common/pref_store_observer_mock.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 using ::testing::Mock; |
| 14 using ::testing::StrEq; |
| 15 |
| 16 namespace { |
| 17 |
| 18 const char* incognito_key = prefs::kBrowserWindowPlacement; |
| 19 const char* regular_key = prefs::kShowBookmarkBar; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 class IncognitoUserPrefStoreTest : public testing::Test { |
| 24 public: |
| 25 IncognitoUserPrefStoreTest() |
| 26 : underlay_(new TestingPrefStore()), |
| 27 overlay_(new IncognitoUserPrefStore(underlay_.get())) { |
| 28 } |
| 29 |
| 30 virtual ~IncognitoUserPrefStoreTest() {} |
| 31 |
| 32 scoped_refptr<TestingPrefStore> underlay_; |
| 33 scoped_refptr<IncognitoUserPrefStore> overlay_; |
| 34 }; |
| 35 |
| 36 TEST_F(IncognitoUserPrefStoreTest, Observer) { |
| 37 PrefStoreObserverMock obs; |
| 38 overlay_->AddObserver(&obs); |
| 39 |
| 40 // Check that underlay first value is reported. |
| 41 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(incognito_key))).Times(1); |
| 42 underlay_->SetValue(incognito_key, Value::CreateIntegerValue(42)); |
| 43 Mock::VerifyAndClearExpectations(&obs); |
| 44 |
| 45 // Check that underlay overwriting is reported. |
| 46 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(incognito_key))).Times(1); |
| 47 underlay_->SetValue(incognito_key, Value::CreateIntegerValue(43)); |
| 48 Mock::VerifyAndClearExpectations(&obs); |
| 49 |
| 50 // Check that overwriting change in overlay is reported. |
| 51 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(incognito_key))).Times(1); |
| 52 overlay_->SetValue(incognito_key, Value::CreateIntegerValue(44)); |
| 53 Mock::VerifyAndClearExpectations(&obs); |
| 54 |
| 55 // Check that hidden underlay change is not reported. |
| 56 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(incognito_key))).Times(0); |
| 57 underlay_->SetValue(incognito_key, Value::CreateIntegerValue(45)); |
| 58 Mock::VerifyAndClearExpectations(&obs); |
| 59 |
| 60 // Check that overlay remove is reported. |
| 61 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(incognito_key))).Times(1); |
| 62 overlay_->RemoveValue(incognito_key); |
| 63 Mock::VerifyAndClearExpectations(&obs); |
| 64 |
| 65 // Check that underlay remove is reported. |
| 66 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(incognito_key))).Times(1); |
| 67 underlay_->RemoveValue(incognito_key); |
| 68 Mock::VerifyAndClearExpectations(&obs); |
| 69 |
| 70 // Check respecting of silence. |
| 71 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(incognito_key))).Times(0); |
| 72 overlay_->SetValueSilently(incognito_key, Value::CreateIntegerValue(46)); |
| 73 Mock::VerifyAndClearExpectations(&obs); |
| 74 |
| 75 overlay_->RemoveObserver(&obs); |
| 76 |
| 77 // Check successful unsubscription. |
| 78 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(incognito_key))).Times(0); |
| 79 underlay_->SetValue(incognito_key, Value::CreateIntegerValue(47)); |
| 80 overlay_->SetValue(incognito_key, Value::CreateIntegerValue(48)); |
| 81 Mock::VerifyAndClearExpectations(&obs); |
| 82 } |
| 83 |
| 84 TEST_F(IncognitoUserPrefStoreTest, GetAndSet) { |
| 85 const Value* value = NULL; |
| 86 int i = -1; |
| 87 EXPECT_EQ(PrefStore::READ_NO_VALUE, |
| 88 overlay_->GetValue(incognito_key, &value)); |
| 89 EXPECT_EQ(PrefStore::READ_NO_VALUE, |
| 90 underlay_->GetValue(incognito_key, &value)); |
| 91 |
| 92 underlay_->SetValue(incognito_key, Value::CreateIntegerValue(42)); |
| 93 |
| 94 // Value shines through: |
| 95 EXPECT_EQ(PrefStore::READ_OK, overlay_->GetValue(incognito_key, &value)); |
| 96 ASSERT_TRUE(value); |
| 97 EXPECT_TRUE(value->GetAsInteger(&i)); |
| 98 EXPECT_EQ(42, i); |
| 99 |
| 100 EXPECT_EQ(PrefStore::READ_OK, underlay_->GetValue(incognito_key, &value)); |
| 101 ASSERT_TRUE(value); |
| 102 EXPECT_TRUE(value->GetAsInteger(&i)); |
| 103 EXPECT_EQ(42, i); |
| 104 |
| 105 overlay_->SetValue(incognito_key, Value::CreateIntegerValue(43)); |
| 106 |
| 107 EXPECT_EQ(PrefStore::READ_OK, overlay_->GetValue(incognito_key, &value)); |
| 108 ASSERT_TRUE(value); |
| 109 EXPECT_TRUE(value->GetAsInteger(&i)); |
| 110 EXPECT_EQ(43, i); |
| 111 |
| 112 EXPECT_EQ(PrefStore::READ_OK, underlay_->GetValue(incognito_key, &value)); |
| 113 ASSERT_TRUE(value); |
| 114 EXPECT_TRUE(value->GetAsInteger(&i)); |
| 115 EXPECT_EQ(42, i); |
| 116 |
| 117 overlay_->RemoveValue(incognito_key); |
| 118 |
| 119 // Value shines through: |
| 120 EXPECT_EQ(PrefStore::READ_OK, overlay_->GetValue(incognito_key, &value)); |
| 121 ASSERT_TRUE(value); |
| 122 EXPECT_TRUE(value->GetAsInteger(&i)); |
| 123 EXPECT_EQ(42, i); |
| 124 |
| 125 EXPECT_EQ(PrefStore::READ_OK, underlay_->GetValue(incognito_key, &value)); |
| 126 ASSERT_TRUE(value); |
| 127 EXPECT_TRUE(value->GetAsInteger(&i)); |
| 128 EXPECT_EQ(42, i); |
| 129 } |
| 130 |
| 131 // Check that GetMutableValue does not return the dictionary of the underlay. |
| 132 TEST_F(IncognitoUserPrefStoreTest, ModifyDictionaries) { |
| 133 underlay_->SetValue(incognito_key, new DictionaryValue); |
| 134 |
| 135 Value* modify = NULL; |
| 136 EXPECT_EQ(PrefStore::READ_OK, |
| 137 overlay_->GetMutableValue(incognito_key, &modify)); |
| 138 ASSERT_TRUE(modify); |
| 139 ASSERT_TRUE(modify->GetType() == Value::TYPE_DICTIONARY); |
| 140 static_cast<DictionaryValue*>(modify)->SetInteger(incognito_key, 42); |
| 141 |
| 142 Value* original_in_underlay = NULL; |
| 143 EXPECT_EQ(PrefStore::READ_OK, |
| 144 underlay_->GetMutableValue(incognito_key, &original_in_underlay)); |
| 145 ASSERT_TRUE(original_in_underlay); |
| 146 ASSERT_TRUE(original_in_underlay->GetType() == Value::TYPE_DICTIONARY); |
| 147 EXPECT_TRUE(static_cast<DictionaryValue*>(original_in_underlay)->empty()); |
| 148 |
| 149 Value* modified = NULL; |
| 150 EXPECT_EQ(PrefStore::READ_OK, |
| 151 overlay_->GetMutableValue(incognito_key, &modified)); |
| 152 ASSERT_TRUE(modified); |
| 153 ASSERT_TRUE(modified->GetType() == Value::TYPE_DICTIONARY); |
| 154 EXPECT_TRUE(Value::Equals(modify, static_cast<DictionaryValue*>(modified))); |
| 155 } |
| 156 |
| 157 // Here we consider a global preference that is not incognito specific. |
| 158 TEST_F(IncognitoUserPrefStoreTest, GlobalPref) { |
| 159 PrefStoreObserverMock obs; |
| 160 overlay_->AddObserver(&obs); |
| 161 |
| 162 const Value* value = NULL; |
| 163 int i = -1; |
| 164 |
| 165 // Check that underlay first value is reported. |
| 166 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(1); |
| 167 underlay_->SetValue(regular_key, Value::CreateIntegerValue(42)); |
| 168 Mock::VerifyAndClearExpectations(&obs); |
| 169 |
| 170 // Check that underlay overwriting is reported. |
| 171 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(1); |
| 172 underlay_->SetValue(regular_key, Value::CreateIntegerValue(43)); |
| 173 Mock::VerifyAndClearExpectations(&obs); |
| 174 |
| 175 // Check that we get this value from the overlay |
| 176 EXPECT_EQ(PrefStore::READ_OK, overlay_->GetValue(regular_key, &value)); |
| 177 ASSERT_TRUE(value); |
| 178 EXPECT_TRUE(value->GetAsInteger(&i)); |
| 179 EXPECT_EQ(43, i); |
| 180 |
| 181 // Check that overwriting change in overlay is reported. |
| 182 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(1); |
| 183 overlay_->SetValue(regular_key, Value::CreateIntegerValue(44)); |
| 184 Mock::VerifyAndClearExpectations(&obs); |
| 185 |
| 186 // Check that we get this value from the overlay and the underlay. |
| 187 EXPECT_EQ(PrefStore::READ_OK, overlay_->GetValue(regular_key, &value)); |
| 188 ASSERT_TRUE(value); |
| 189 EXPECT_TRUE(value->GetAsInteger(&i)); |
| 190 EXPECT_EQ(44, i); |
| 191 EXPECT_EQ(PrefStore::READ_OK, underlay_->GetValue(regular_key, &value)); |
| 192 ASSERT_TRUE(value); |
| 193 EXPECT_TRUE(value->GetAsInteger(&i)); |
| 194 EXPECT_EQ(44, i); |
| 195 |
| 196 // Check that overlay remove is reported. |
| 197 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(1); |
| 198 overlay_->RemoveValue(regular_key); |
| 199 Mock::VerifyAndClearExpectations(&obs); |
| 200 |
| 201 // Check that value was removed from overlay and underlay |
| 202 EXPECT_EQ(PrefStore::READ_NO_VALUE, overlay_->GetValue(regular_key, &value)); |
| 203 EXPECT_EQ(PrefStore::READ_NO_VALUE, underlay_->GetValue(regular_key, &value)); |
| 204 |
| 205 // Check respecting of silence. |
| 206 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(0); |
| 207 overlay_->SetValueSilently(regular_key, Value::CreateIntegerValue(46)); |
| 208 Mock::VerifyAndClearExpectations(&obs); |
| 209 |
| 210 overlay_->RemoveObserver(&obs); |
| 211 |
| 212 // Check successful unsubscription. |
| 213 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(0); |
| 214 underlay_->SetValue(regular_key, Value::CreateIntegerValue(47)); |
| 215 overlay_->SetValue(regular_key, Value::CreateIntegerValue(48)); |
| 216 Mock::VerifyAndClearExpectations(&obs); |
| 217 } |
OLD | NEW |