Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/public/cpp/pref_service_factory.h" | 5 #include "services/preferences/public/cpp/pref_service_factory.h" |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/test/sequenced_worker_pool_owner.h" | 10 #include "base/test/sequenced_worker_pool_owner.h" |
| 12 #include "components/prefs/pref_change_registrar.h" | 11 #include "components/prefs/pref_change_registrar.h" |
| 13 #include "components/prefs/pref_registry_simple.h" | 12 #include "components/prefs/pref_registry_simple.h" |
| 14 #include "components/prefs/pref_service.h" | 13 #include "components/prefs/pref_service.h" |
| 15 #include "components/prefs/value_map_pref_store.h" | 14 #include "components/prefs/value_map_pref_store.h" |
| 16 #include "components/prefs/writeable_pref_store.h" | 15 #include "components/prefs/writeable_pref_store.h" |
| 17 #include "mojo/public/cpp/bindings/binding_set.h" | 16 #include "mojo/public/cpp/bindings/binding_set.h" |
| 18 #include "services/preferences/public/cpp/dictionary_value_update.h" | 17 #include "services/preferences/public/cpp/dictionary_value_update.h" |
| 19 #include "services/preferences/public/cpp/pref_service_main.h" | 18 #include "services/preferences/public/cpp/pref_service_main.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 service_factory_bindings_; | 71 service_factory_bindings_; |
| 73 std::unique_ptr<service_manager::ServiceContext> pref_service_context_; | 72 std::unique_ptr<service_manager::ServiceContext> pref_service_context_; |
| 74 }; | 73 }; |
| 75 | 74 |
| 76 constexpr int kInitialValue = 1; | 75 constexpr int kInitialValue = 1; |
| 77 constexpr int kUpdatedValue = 2; | 76 constexpr int kUpdatedValue = 2; |
| 78 constexpr char kKey[] = "some_key"; | 77 constexpr char kKey[] = "some_key"; |
| 79 constexpr char kOtherKey[] = "some_other_key"; | 78 constexpr char kOtherKey[] = "some_other_key"; |
| 80 constexpr char kDictionaryKey[] = "a.dictionary.pref"; | 79 constexpr char kDictionaryKey[] = "a.dictionary.pref"; |
| 81 | 80 |
| 82 class PrefServiceFactoryTest : public base::MessageLoop::DestructionObserver, | 81 class PrefServiceFactoryTest : public service_manager::test::ServiceTest { |
| 83 public service_manager::test::ServiceTest { | |
| 84 public: | 82 public: |
| 85 PrefServiceFactoryTest() : ServiceTest("prefs_unittests", false) {} | 83 PrefServiceFactoryTest() |
| 84 : ServiceTest("prefs_unittests", false), | |
| 85 worker_pool_owner_(2, "PrefServiceFactoryTest") {} | |
| 86 | 86 |
| 87 protected: | 87 protected: |
| 88 void SetUp() override { | 88 void SetUp() override { |
| 89 ServiceTest::SetUp(); | 89 ServiceTest::SetUp(); |
| 90 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); | 90 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
| 91 | 91 |
| 92 // Init the pref service (in production Chrome startup would do this.) | 92 // Init the pref service (in production Chrome startup would do this.) |
| 93 mojom::PrefServiceControlPtr control; | 93 mojom::PrefServiceControlPtr control; |
| 94 connector()->BindInterface(mojom::kServiceName, &control); | 94 connector()->BindInterface(mojom::kServiceName, &control); |
| 95 auto config = mojom::PersistentPrefStoreConfiguration::New(); | 95 auto config = mojom::PersistentPrefStoreConfiguration::New(); |
| 96 config->set_simple_configuration( | 96 config->set_simple_configuration( |
| 97 mojom::SimplePersistentPrefStoreConfiguration::New( | 97 mojom::SimplePersistentPrefStoreConfiguration::New( |
| 98 profile_dir_.GetPath().AppendASCII("Preferences"))); | 98 profile_dir_.GetPath().AppendASCII("Preferences"))); |
| 99 control->Init(std::move(config)); | 99 control->Init(std::move(config)); |
| 100 above_user_prefs_pref_store_ = new ValueMapPrefStore(); | 100 above_user_prefs_pref_store_ = new ValueMapPrefStore(); |
| 101 below_user_prefs_pref_store_ = new ValueMapPrefStore(); | 101 below_user_prefs_pref_store_ = new ValueMapPrefStore(); |
| 102 mojom::PrefStoreRegistryPtr registry; | 102 mojom::PrefStoreRegistryPtr registry; |
| 103 connector()->BindInterface(mojom::kServiceName, ®istry); | 103 connector()->BindInterface(mojom::kServiceName, ®istry); |
| 104 above_user_prefs_impl_ = | 104 above_user_prefs_impl_ = |
| 105 PrefStoreImpl::Create(registry.get(), above_user_prefs_pref_store_, | 105 PrefStoreImpl::Create(registry.get(), above_user_prefs_pref_store_, |
| 106 PrefValueStore::COMMAND_LINE_STORE); | 106 PrefValueStore::COMMAND_LINE_STORE); |
| 107 below_user_prefs_impl_ = | 107 below_user_prefs_impl_ = |
| 108 PrefStoreImpl::Create(registry.get(), below_user_prefs_pref_store_, | 108 PrefStoreImpl::Create(registry.get(), below_user_prefs_pref_store_, |
| 109 PrefValueStore::RECOMMENDED_STORE); | 109 PrefValueStore::RECOMMENDED_STORE); |
| 110 } | 110 } |
| 111 | 111 |
| 112 // service_manager::test::ServiceTest: | 112 // service_manager::test::ServiceTest: |
| 113 std::unique_ptr<service_manager::Service> CreateService() override { | 113 std::unique_ptr<service_manager::Service> CreateService() override { |
| 114 return base::MakeUnique<ServiceTestClient>(this, | 114 return base::MakeUnique<ServiceTestClient>(this, worker_pool_owner_.pool()); |
| 115 worker_pool_owner_->pool()); | |
| 116 } | 115 } |
| 117 | 116 |
| 118 std::unique_ptr<base::MessageLoop> CreateMessageLoop() override { | |
|
fdoray
2017/05/10 16:53:50
I doubt that creating the SequencedWorkerPoolOwner
| |
| 119 auto loop = ServiceTest::CreateMessageLoop(); | |
| 120 worker_pool_owner_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( | |
| 121 2, "PrefServiceFactoryTest"); | |
| 122 loop->AddDestructionObserver(this); | |
| 123 return loop; | |
| 124 } | |
| 125 | |
| 126 // base::MessageLoop::DestructionObserver | |
| 127 void WillDestroyCurrentMessageLoop() override { worker_pool_owner_.reset(); } | |
| 128 | |
| 129 // Create a fully initialized PrefService synchronously. | 117 // Create a fully initialized PrefService synchronously. |
| 130 std::unique_ptr<PrefService> Create() { | 118 std::unique_ptr<PrefService> Create() { |
| 131 std::unique_ptr<PrefService> pref_service; | 119 std::unique_ptr<PrefService> pref_service; |
| 132 base::RunLoop run_loop; | 120 base::RunLoop run_loop; |
| 133 auto pref_registry = make_scoped_refptr(new PrefRegistrySimple()); | 121 auto pref_registry = make_scoped_refptr(new PrefRegistrySimple()); |
| 134 pref_registry->RegisterIntegerPref(kKey, kInitialValue); | 122 pref_registry->RegisterIntegerPref(kKey, kInitialValue); |
| 135 pref_registry->RegisterIntegerPref(kOtherKey, kInitialValue); | 123 pref_registry->RegisterIntegerPref(kOtherKey, kInitialValue); |
| 136 pref_registry->RegisterDictionaryPref(kDictionaryKey); | 124 pref_registry->RegisterDictionaryPref(kDictionaryKey); |
| 137 ConnectToPrefService(connector(), pref_registry, | 125 ConnectToPrefService(connector(), pref_registry, |
| 138 std::vector<PrefValueStore::PrefStoreType>(), | 126 std::vector<PrefValueStore::PrefStoreType>(), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 } | 168 } |
| 181 | 169 |
| 182 static void OnPrefChanged(const base::Closure& quit_closure, | 170 static void OnPrefChanged(const base::Closure& quit_closure, |
| 183 const std::string& expected_path, | 171 const std::string& expected_path, |
| 184 const std::string& path) { | 172 const std::string& path) { |
| 185 if (path == expected_path) | 173 if (path == expected_path) |
| 186 quit_closure.Run(); | 174 quit_closure.Run(); |
| 187 } | 175 } |
| 188 | 176 |
| 189 base::ScopedTempDir profile_dir_; | 177 base::ScopedTempDir profile_dir_; |
| 190 std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_owner_; | 178 base::SequencedWorkerPoolOwner worker_pool_owner_; |
| 191 scoped_refptr<WriteablePrefStore> above_user_prefs_pref_store_; | 179 scoped_refptr<WriteablePrefStore> above_user_prefs_pref_store_; |
| 192 std::unique_ptr<PrefStoreImpl> above_user_prefs_impl_; | 180 std::unique_ptr<PrefStoreImpl> above_user_prefs_impl_; |
| 193 scoped_refptr<WriteablePrefStore> below_user_prefs_pref_store_; | 181 scoped_refptr<WriteablePrefStore> below_user_prefs_pref_store_; |
| 194 std::unique_ptr<PrefStoreImpl> below_user_prefs_impl_; | 182 std::unique_ptr<PrefStoreImpl> below_user_prefs_impl_; |
| 195 | 183 |
| 196 DISALLOW_COPY_AND_ASSIGN(PrefServiceFactoryTest); | 184 DISALLOW_COPY_AND_ASSIGN(PrefServiceFactoryTest); |
| 197 }; | 185 }; |
| 198 | 186 |
| 199 // Check that a single client can set and read back values. | 187 // Check that a single client can set and read back values. |
| 200 TEST_F(PrefServiceFactoryTest, Basic) { | 188 TEST_F(PrefServiceFactoryTest, Basic) { |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 } | 474 } |
| 487 PrefChangeRegistrar registrar; | 475 PrefChangeRegistrar registrar; |
| 488 registrar.Init(pref_service2.get()); | 476 registrar.Init(pref_service2.get()); |
| 489 registrar.Add(kDictionaryKey, base::Bind(&Fail, pref_service2.get())); | 477 registrar.Add(kDictionaryKey, base::Bind(&Fail, pref_service2.get())); |
| 490 pref_service->SetInteger(kKey, kUpdatedValue); | 478 pref_service->SetInteger(kKey, kUpdatedValue); |
| 491 WaitForPrefChange(pref_service2.get(), kKey); | 479 WaitForPrefChange(pref_service2.get(), kKey); |
| 492 } | 480 } |
| 493 | 481 |
| 494 } // namespace | 482 } // namespace |
| 495 } // namespace prefs | 483 } // namespace prefs |
| OLD | NEW |