| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "services/preferences/persistent_pref_store_impl.h" | 5 #include "services/preferences/persistent_pref_store_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 ~InitializationMockPersistentPrefStore() override = default; | 84 ~InitializationMockPersistentPrefStore() override = default; |
| 85 | 85 |
| 86 bool initialized_ = false; | 86 bool initialized_ = false; |
| 87 bool success_; | 87 bool success_; |
| 88 PersistentPrefStore::PrefReadError read_error_; | 88 PersistentPrefStore::PrefReadError read_error_; |
| 89 bool read_only_; | 89 bool read_only_; |
| 90 base::ObserverList<PrefStore::Observer, true> observers_; | 90 base::ObserverList<PrefStore::Observer, true> observers_; |
| 91 }; | 91 }; |
| 92 | 92 |
| 93 constexpr char kKey[] = "path.to.key"; | 93 constexpr char kKey[] = "path.to.key"; |
| 94 constexpr char kOtherKey[] = "path.to.other_key"; |
| 94 | 95 |
| 95 class PersistentPrefStoreImplTest : public testing::Test { | 96 class PersistentPrefStoreImplTest : public testing::Test { |
| 96 public: | 97 public: |
| 97 PersistentPrefStoreImplTest() = default; | 98 PersistentPrefStoreImplTest() = default; |
| 98 | 99 |
| 99 // testing::Test: | 100 // testing::Test: |
| 100 void TearDown() override { | 101 void TearDown() override { |
| 101 pref_store_ = nullptr; | 102 pref_store_ = nullptr; |
| 102 base::RunLoop().RunUntilIdle(); | 103 base::RunLoop().RunUntilIdle(); |
| 103 impl_.reset(); | 104 impl_.reset(); |
| 104 base::RunLoop().RunUntilIdle(); | 105 base::RunLoop().RunUntilIdle(); |
| 105 } | 106 } |
| 106 | 107 |
| 107 void CreateImpl(scoped_refptr<PersistentPrefStore> backing_pref_store) { | 108 void CreateImpl(scoped_refptr<PersistentPrefStore> backing_pref_store) { |
| 108 base::RunLoop run_loop; | 109 base::RunLoop run_loop; |
| 109 bool initialized = backing_pref_store->IsInitializationComplete(); | 110 bool initialized = backing_pref_store->IsInitializationComplete(); |
| 110 impl_ = base::MakeUnique<PersistentPrefStoreImpl>( | 111 impl_ = base::MakeUnique<PersistentPrefStoreImpl>( |
| 111 std::move(backing_pref_store), nullptr, run_loop.QuitClosure()); | 112 std::move(backing_pref_store), nullptr, run_loop.QuitClosure()); |
| 112 if (!initialized) | 113 if (!initialized) |
| 113 run_loop.Run(); | 114 run_loop.Run(); |
| 114 pref_store_ = CreateConnection(); | 115 pref_store_ = CreateConnection(); |
| 115 } | 116 } |
| 116 | 117 |
| 117 scoped_refptr<PersistentPrefStore> CreateConnection() { | 118 scoped_refptr<PersistentPrefStore> CreateConnection( |
| 118 return make_scoped_refptr( | 119 PersistentPrefStoreImpl::ObservedPrefs observed_prefs = |
| 119 new PersistentPrefStoreClient(impl_->CreateConnection())); | 120 PersistentPrefStoreImpl::ObservedPrefs()) { |
| 121 if (observed_prefs.empty()) |
| 122 observed_prefs.insert({kKey, kOtherKey}); |
| 123 return make_scoped_refptr(new PersistentPrefStoreClient( |
| 124 impl_->CreateConnection(std::move(observed_prefs)))); |
| 120 } | 125 } |
| 121 | 126 |
| 122 PersistentPrefStore* pref_store() { return pref_store_.get(); } | 127 PersistentPrefStore* pref_store() { return pref_store_.get(); } |
| 123 | 128 |
| 124 private: | 129 private: |
| 125 base::MessageLoop message_loop_; | 130 base::MessageLoop message_loop_; |
| 126 | 131 |
| 127 std::unique_ptr<PersistentPrefStoreImpl> impl_; | 132 std::unique_ptr<PersistentPrefStoreImpl> impl_; |
| 128 | 133 |
| 129 scoped_refptr<PersistentPrefStore> pref_store_; | 134 scoped_refptr<PersistentPrefStore> pref_store_; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 .Times(1) | 218 .Times(1) |
| 214 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); }))); | 219 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); }))); |
| 215 run_loop.Run(); | 220 run_loop.Run(); |
| 216 other_pref_store->RemoveObserver(&observer); | 221 other_pref_store->RemoveObserver(&observer); |
| 217 | 222 |
| 218 const base::Value* output = nullptr; | 223 const base::Value* output = nullptr; |
| 219 ASSERT_TRUE(other_pref_store->GetValue(kKey, &output)); | 224 ASSERT_TRUE(other_pref_store->GetValue(kKey, &output)); |
| 220 EXPECT_TRUE(value.Equals(output)); | 225 EXPECT_TRUE(value.Equals(output)); |
| 221 } | 226 } |
| 222 | 227 |
| 228 TEST_F(PersistentPrefStoreImplTest, UnregisteredPrefNotObservedByOtherClient) { |
| 229 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore()); |
| 230 CreateImpl(backing_pref_store); |
| 231 EXPECT_TRUE(pref_store()->IsInitializationComplete()); |
| 232 |
| 233 PersistentPrefStoreImpl::ObservedPrefs observed_prefs; |
| 234 observed_prefs.insert(kKey); |
| 235 |
| 236 auto other_pref_store = CreateConnection(std::move(observed_prefs)); |
| 237 EXPECT_TRUE(other_pref_store->IsInitializationComplete()); |
| 238 |
| 239 pref_store()->SetValue(kOtherKey, base::MakeUnique<base::Value>(123), 0); |
| 240 pref_store()->SetValue(kKey, base::MakeUnique<base::Value>("value"), 0); |
| 241 |
| 242 PrefStoreObserverMock observer; |
| 243 other_pref_store->AddObserver(&observer); |
| 244 base::RunLoop run_loop; |
| 245 EXPECT_CALL(observer, OnPrefValueChanged(kOtherKey)).Times(0); |
| 246 EXPECT_CALL(observer, OnPrefValueChanged(kKey)) |
| 247 .Times(1) |
| 248 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); }))); |
| 249 run_loop.Run(); |
| 250 other_pref_store->RemoveObserver(&observer); |
| 251 |
| 252 EXPECT_FALSE(other_pref_store->GetValue(kOtherKey, nullptr)); |
| 253 } |
| 254 |
| 223 TEST_F(PersistentPrefStoreImplTest, | 255 TEST_F(PersistentPrefStoreImplTest, |
| 224 WriteWithoutPathExpansionObservedByOtherClient) { | 256 WriteWithoutPathExpansionObservedByOtherClient) { |
| 225 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore()); | 257 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore()); |
| 226 CreateImpl(backing_pref_store); | 258 CreateImpl(backing_pref_store); |
| 227 EXPECT_TRUE(pref_store()->IsInitializationComplete()); | 259 EXPECT_TRUE(pref_store()->IsInitializationComplete()); |
| 228 | 260 |
| 229 auto other_pref_store = CreateConnection(); | 261 auto other_pref_store = CreateConnection(); |
| 230 EXPECT_TRUE(other_pref_store->IsInitializationComplete()); | 262 EXPECT_TRUE(other_pref_store->IsInitializationComplete()); |
| 231 | 263 |
| 232 base::DictionaryValue dict; | 264 base::DictionaryValue dict; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 EXPECT_CALL(*backing_store, ClearMutableValues()) | 380 EXPECT_CALL(*backing_store, ClearMutableValues()) |
| 349 .Times(1) | 381 .Times(1) |
| 350 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); }))); | 382 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); }))); |
| 351 EXPECT_CALL(*backing_store, CommitPendingWrite()).Times(1); | 383 EXPECT_CALL(*backing_store, CommitPendingWrite()).Times(1); |
| 352 pref_store()->ClearMutableValues(); | 384 pref_store()->ClearMutableValues(); |
| 353 run_loop.Run(); | 385 run_loop.Run(); |
| 354 } | 386 } |
| 355 | 387 |
| 356 } // namespace | 388 } // namespace |
| 357 } // namespace prefs | 389 } // namespace prefs |
| OLD | NEW |