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