OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "chrome/browser/prefs/tracked/segregated_pref_store.h" |
| 6 |
| 7 #include <set> |
| 8 #include <string> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/prefs/persistent_pref_store.h" |
| 15 #include "base/prefs/pref_store_observer_mock.h" |
| 16 #include "base/prefs/testing_pref_store.h" |
| 17 #include "base/values.h" |
| 18 #include "chrome/browser/prefs/tracked/segregated_pref_store.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 const char kSelectedPref[] = "selected_pref"; |
| 24 const char kUnselectedPref[] = "unselected_pref"; |
| 25 |
| 26 const char kValue1[] = "value1"; |
| 27 const char kValue2[] = "value2"; |
| 28 |
| 29 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
| 30 public: |
| 31 struct Data { |
| 32 Data(bool invoked_in, PersistentPrefStore::PrefReadError read_error_in) |
| 33 : invoked(invoked_in), read_error(read_error_in) {} |
| 34 |
| 35 bool invoked; |
| 36 PersistentPrefStore::PrefReadError read_error; |
| 37 }; |
| 38 |
| 39 explicit MockReadErrorDelegate(Data* data) : data_(data) { |
| 40 DCHECK(data_); |
| 41 EXPECT_FALSE(data_->invoked); |
| 42 } |
| 43 |
| 44 // PersistentPrefStore::ReadErrorDelegate implementation |
| 45 virtual void OnError(PersistentPrefStore::PrefReadError read_error) OVERRIDE { |
| 46 EXPECT_FALSE(data_->invoked); |
| 47 data_->invoked = true; |
| 48 data_->read_error = read_error; |
| 49 } |
| 50 |
| 51 private: |
| 52 Data* data_; |
| 53 }; |
| 54 |
| 55 } // namespace |
| 56 |
| 57 class SegregatedPrefStoreTest : public testing::Test { |
| 58 public: |
| 59 SegregatedPrefStoreTest() |
| 60 : initialization_callback_invoked_(false), |
| 61 read_error_delegate_data_(false, |
| 62 PersistentPrefStore::PREF_READ_ERROR_NONE), |
| 63 read_error_delegate_( |
| 64 new MockReadErrorDelegate(&read_error_delegate_data_)) {} |
| 65 |
| 66 virtual void SetUp() OVERRIDE { |
| 67 selected_store_ = new TestingPrefStore; |
| 68 default_store_ = new TestingPrefStore; |
| 69 |
| 70 std::set<std::string> selected_pref_names; |
| 71 selected_pref_names.insert(kSelectedPref); |
| 72 |
| 73 segregated_store_ = new SegregatedPrefStore( |
| 74 default_store_, |
| 75 selected_store_, |
| 76 selected_pref_names, |
| 77 base::Bind(&SegregatedPrefStoreTest::InitializationCallback, |
| 78 base::Unretained(this))); |
| 79 |
| 80 segregated_store_->AddObserver(&observer_); |
| 81 } |
| 82 |
| 83 virtual void TearDown() OVERRIDE { |
| 84 segregated_store_->RemoveObserver(&observer_); |
| 85 } |
| 86 |
| 87 protected: |
| 88 scoped_ptr<PersistentPrefStore::ReadErrorDelegate> GetReadErrorDelegate() { |
| 89 EXPECT_TRUE(read_error_delegate_); |
| 90 return read_error_delegate_ |
| 91 .PassAs<PersistentPrefStore::ReadErrorDelegate>(); |
| 92 } |
| 93 |
| 94 PrefStoreObserverMock observer_; |
| 95 bool initialization_callback_invoked_; |
| 96 |
| 97 scoped_refptr<TestingPrefStore> default_store_; |
| 98 scoped_refptr<TestingPrefStore> selected_store_; |
| 99 scoped_refptr<SegregatedPrefStore> segregated_store_; |
| 100 |
| 101 MockReadErrorDelegate::Data read_error_delegate_data_; |
| 102 |
| 103 private: |
| 104 void InitializationCallback() { |
| 105 EXPECT_FALSE(observer_.initialized); |
| 106 EXPECT_FALSE(initialization_callback_invoked_); |
| 107 initialization_callback_invoked_ = true; |
| 108 } |
| 109 |
| 110 scoped_ptr<MockReadErrorDelegate> read_error_delegate_; |
| 111 }; |
| 112 |
| 113 TEST_F(SegregatedPrefStoreTest, StoreValues) { |
| 114 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, |
| 115 segregated_store_->ReadPrefs()); |
| 116 |
| 117 // Properly stores new values. |
| 118 segregated_store_->SetValue(kSelectedPref, new base::StringValue(kValue1)); |
| 119 segregated_store_->SetValue(kUnselectedPref, new base::StringValue(kValue2)); |
| 120 |
| 121 ASSERT_TRUE(selected_store_->GetValue(kSelectedPref, NULL)); |
| 122 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL)); |
| 123 ASSERT_FALSE(default_store_->GetValue(kSelectedPref, NULL)); |
| 124 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL)); |
| 125 |
| 126 ASSERT_TRUE(segregated_store_->GetValue(kSelectedPref, NULL)); |
| 127 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL)); |
| 128 |
| 129 ASSERT_FALSE(selected_store_->committed()); |
| 130 ASSERT_FALSE(default_store_->committed()); |
| 131 |
| 132 segregated_store_->CommitPendingWrite(); |
| 133 |
| 134 ASSERT_TRUE(selected_store_->committed()); |
| 135 ASSERT_TRUE(default_store_->committed()); |
| 136 } |
| 137 |
| 138 TEST_F(SegregatedPrefStoreTest, ReadValues) { |
| 139 selected_store_->SetValue(kSelectedPref, new base::StringValue(kValue1)); |
| 140 default_store_->SetValue(kUnselectedPref, |
| 141 new base::StringValue(kValue2)); |
| 142 |
| 143 // Works properly with values that are already there. |
| 144 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, |
| 145 segregated_store_->ReadPrefs()); |
| 146 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, |
| 147 segregated_store_->GetReadError()); |
| 148 |
| 149 ASSERT_TRUE(selected_store_->GetValue(kSelectedPref, NULL)); |
| 150 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL)); |
| 151 ASSERT_FALSE(default_store_->GetValue(kSelectedPref, NULL)); |
| 152 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL)); |
| 153 |
| 154 ASSERT_TRUE(segregated_store_->GetValue(kSelectedPref, NULL)); |
| 155 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL)); |
| 156 } |
| 157 |
| 158 TEST_F(SegregatedPrefStoreTest, PreviouslySelected) { |
| 159 selected_store_->SetValue(kUnselectedPref, new base::StringValue(kValue1)); |
| 160 segregated_store_->ReadPrefs(); |
| 161 // It will read from the selected store. |
| 162 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL)); |
| 163 ASSERT_TRUE(selected_store_->GetValue(kUnselectedPref, NULL)); |
| 164 ASSERT_FALSE(default_store_->GetValue(kUnselectedPref, NULL)); |
| 165 |
| 166 // But when we update the value... |
| 167 segregated_store_->SetValue(kUnselectedPref, new base::StringValue(kValue2)); |
| 168 // ...it will be migrated. |
| 169 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL)); |
| 170 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL)); |
| 171 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL)); |
| 172 } |
| 173 |
| 174 TEST_F(SegregatedPrefStoreTest, Observer) { |
| 175 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, |
| 176 segregated_store_->ReadPrefs()); |
| 177 EXPECT_TRUE(initialization_callback_invoked_); |
| 178 EXPECT_TRUE(observer_.initialized); |
| 179 EXPECT_TRUE(observer_.initialization_success); |
| 180 EXPECT_TRUE(observer_.changed_keys.empty()); |
| 181 segregated_store_->SetValue(kSelectedPref, new base::StringValue(kValue1)); |
| 182 observer_.VerifyAndResetChangedKey(kSelectedPref); |
| 183 segregated_store_->SetValue(kUnselectedPref, new base::StringValue(kValue2)); |
| 184 observer_.VerifyAndResetChangedKey(kUnselectedPref); |
| 185 } |
| 186 |
| 187 TEST_F(SegregatedPrefStoreTest, SelectedPrefReadError) { |
| 188 selected_store_->set_read_error( |
| 189 PersistentPrefStore::PREF_READ_ERROR_NO_FILE); |
| 190 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 191 segregated_store_->ReadPrefs()); |
| 192 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 193 segregated_store_->GetReadError()); |
| 194 } |
| 195 |
| 196 TEST_F(SegregatedPrefStoreTest, UnselectedPrefReadError) { |
| 197 default_store_->set_read_error( |
| 198 PersistentPrefStore::PREF_READ_ERROR_NO_FILE); |
| 199 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 200 segregated_store_->ReadPrefs()); |
| 201 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 202 segregated_store_->GetReadError()); |
| 203 } |
| 204 |
| 205 TEST_F(SegregatedPrefStoreTest, BothPrefReadError) { |
| 206 default_store_->set_read_error( |
| 207 PersistentPrefStore::PREF_READ_ERROR_NO_FILE); |
| 208 selected_store_->set_read_error( |
| 209 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED); |
| 210 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 211 segregated_store_->ReadPrefs()); |
| 212 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 213 segregated_store_->GetReadError()); |
| 214 } |
| 215 |
| 216 TEST_F(SegregatedPrefStoreTest, BothPrefReadErrorAsync) { |
| 217 default_store_->set_read_error( |
| 218 PersistentPrefStore::PREF_READ_ERROR_NO_FILE); |
| 219 selected_store_->set_read_error( |
| 220 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED); |
| 221 |
| 222 selected_store_->SetBlockAsyncRead(true); |
| 223 |
| 224 EXPECT_FALSE(read_error_delegate_data_.invoked); |
| 225 |
| 226 segregated_store_->ReadPrefsAsync(GetReadErrorDelegate().release()); |
| 227 |
| 228 EXPECT_FALSE(read_error_delegate_data_.invoked); |
| 229 |
| 230 selected_store_->SetBlockAsyncRead(false); |
| 231 |
| 232 EXPECT_TRUE(read_error_delegate_data_.invoked); |
| 233 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 234 segregated_store_->GetReadError()); |
| 235 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 236 segregated_store_->GetReadError()); |
| 237 } |
| 238 |
| 239 TEST_F(SegregatedPrefStoreTest, IsInitializationComplete) { |
| 240 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); |
| 241 segregated_store_->ReadPrefs(); |
| 242 EXPECT_TRUE(segregated_store_->IsInitializationComplete()); |
| 243 } |
| 244 |
| 245 TEST_F(SegregatedPrefStoreTest, IsInitializationCompleteAsync) { |
| 246 selected_store_->SetBlockAsyncRead(true); |
| 247 default_store_->SetBlockAsyncRead(true); |
| 248 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); |
| 249 segregated_store_->ReadPrefsAsync(NULL); |
| 250 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); |
| 251 selected_store_->SetBlockAsyncRead(false); |
| 252 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); |
| 253 default_store_->SetBlockAsyncRead(false); |
| 254 EXPECT_TRUE(segregated_store_->IsInitializationComplete()); |
| 255 } |
OLD | NEW |