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" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/test/sequenced_worker_pool_owner.h" | 10 #include "base/test/sequenced_worker_pool_owner.h" |
11 #include "components/prefs/pref_change_registrar.h" | 11 #include "components/prefs/pref_change_registrar.h" |
12 #include "components/prefs/pref_registry_simple.h" | 12 #include "components/prefs/pref_registry_simple.h" |
13 #include "components/prefs/pref_service.h" | 13 #include "components/prefs/pref_service.h" |
14 #include "components/prefs/value_map_pref_store.h" | |
15 #include "components/prefs/writeable_pref_store.h" | |
14 #include "mojo/public/cpp/bindings/binding_set.h" | 16 #include "mojo/public/cpp/bindings/binding_set.h" |
15 #include "services/preferences/public/cpp/pref_service_main.h" | 17 #include "services/preferences/public/cpp/pref_service_main.h" |
18 #include "services/preferences/public/cpp/pref_store_impl.h" | |
16 #include "services/preferences/public/interfaces/preferences.mojom.h" | 19 #include "services/preferences/public/interfaces/preferences.mojom.h" |
17 #include "services/service_manager/public/cpp/interface_factory.h" | 20 #include "services/service_manager/public/cpp/interface_factory.h" |
18 #include "services/service_manager/public/cpp/interface_registry.h" | 21 #include "services/service_manager/public/cpp/interface_registry.h" |
19 #include "services/service_manager/public/cpp/service_context.h" | 22 #include "services/service_manager/public/cpp/service_context.h" |
20 #include "services/service_manager/public/cpp/service_test.h" | 23 #include "services/service_manager/public/cpp/service_test.h" |
21 #include "services/service_manager/public/interfaces/service_factory.mojom.h" | 24 #include "services/service_manager/public/interfaces/service_factory.mojom.h" |
22 | 25 |
23 namespace prefs { | 26 namespace prefs { |
24 namespace { | 27 namespace { |
25 | 28 |
(...skipping 11 matching lines...) Expand all Loading... | |
37 bool OnConnect(const service_manager::ServiceInfo& remote_info, | 40 bool OnConnect(const service_manager::ServiceInfo& remote_info, |
38 service_manager::InterfaceRegistry* registry) override { | 41 service_manager::InterfaceRegistry* registry) override { |
39 registry->AddInterface<service_manager::mojom::ServiceFactory>(this); | 42 registry->AddInterface<service_manager::mojom::ServiceFactory>(this); |
40 return true; | 43 return true; |
41 } | 44 } |
42 | 45 |
43 void CreateService(service_manager::mojom::ServiceRequest request, | 46 void CreateService(service_manager::mojom::ServiceRequest request, |
44 const std::string& name) override { | 47 const std::string& name) override { |
45 if (name == prefs::mojom::kPrefStoreServiceName) { | 48 if (name == prefs::mojom::kPrefStoreServiceName) { |
46 pref_service_context_.reset(new service_manager::ServiceContext( | 49 pref_service_context_.reset(new service_manager::ServiceContext( |
47 CreatePrefService(std::set<PrefValueStore::PrefStoreType>(), | 50 CreatePrefService({PrefValueStore::COMMAND_LINE_STORE, |
51 PrefValueStore::RECOMMENDED_STORE}, | |
48 worker_pool_), | 52 worker_pool_), |
49 std::move(request))); | 53 std::move(request))); |
50 } | 54 } |
51 } | 55 } |
52 | 56 |
53 void Create(const service_manager::Identity& remote_identity, | 57 void Create(const service_manager::Identity& remote_identity, |
54 service_manager::mojom::ServiceFactoryRequest request) override { | 58 service_manager::mojom::ServiceFactoryRequest request) override { |
55 service_factory_bindings_.AddBinding(this, std::move(request)); | 59 service_factory_bindings_.AddBinding(this, std::move(request)); |
56 } | 60 } |
57 | 61 |
58 private: | 62 private: |
59 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | 63 scoped_refptr<base::SequencedWorkerPool> worker_pool_; |
60 mojo::BindingSet<service_manager::mojom::ServiceFactory> | 64 mojo::BindingSet<service_manager::mojom::ServiceFactory> |
61 service_factory_bindings_; | 65 service_factory_bindings_; |
62 std::unique_ptr<service_manager::ServiceContext> pref_service_context_; | 66 std::unique_ptr<service_manager::ServiceContext> pref_service_context_; |
63 }; | 67 }; |
64 | 68 |
65 constexpr int kInitialValue = 1; | 69 constexpr int kInitialValue = 1; |
66 constexpr int kUpdatedValue = 2; | 70 constexpr int kUpdatedValue = 2; |
67 constexpr char kKey[] = "some_key"; | 71 constexpr char kKey[] = "some_key"; |
72 constexpr char kOtherKey[] = "some_other_key"; | |
68 | 73 |
69 class PrefServiceFactoryTest : public base::MessageLoop::DestructionObserver, | 74 class PrefServiceFactoryTest : public base::MessageLoop::DestructionObserver, |
70 public service_manager::test::ServiceTest { | 75 public service_manager::test::ServiceTest { |
71 public: | 76 public: |
72 PrefServiceFactoryTest() : ServiceTest("prefs_unittests", false) {} | 77 PrefServiceFactoryTest() : ServiceTest("prefs_unittests", false) {} |
73 | 78 |
74 protected: | 79 protected: |
75 void SetUp() override { | 80 void SetUp() override { |
76 ServiceTest::SetUp(); | 81 ServiceTest::SetUp(); |
77 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); | 82 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
78 | 83 |
79 // Init the pref service (in production Chrome startup would do this.) | 84 // Init the pref service (in production Chrome startup would do this.) |
80 mojom::PrefServiceControlPtr control; | 85 mojom::PrefServiceControlPtr control; |
81 connector()->BindInterface(mojom::kPrefStoreServiceName, &control); | 86 connector()->BindInterface(mojom::kPrefStoreServiceName, &control); |
82 auto config = mojom::PersistentPrefStoreConfiguration::New(); | 87 auto config = mojom::PersistentPrefStoreConfiguration::New(); |
83 config->set_simple_configuration( | 88 config->set_simple_configuration( |
84 mojom::SimplePersistentPrefStoreConfiguration::New( | 89 mojom::SimplePersistentPrefStoreConfiguration::New( |
85 profile_dir_.GetPath().AppendASCII("Preferences"))); | 90 profile_dir_.GetPath().AppendASCII("Preferences"))); |
86 control->Init(std::move(config)); | 91 control->Init(std::move(config)); |
92 above_user_prefs_pref_store_ = new ValueMapPrefStore(); | |
93 below_user_prefs_pref_store_ = new ValueMapPrefStore(); | |
94 mojom::PrefStoreRegistryPtr registry; | |
95 connector()->BindInterface(mojom::kPrefStoreServiceName, ®istry); | |
96 above_user_prefs_impl_ = | |
97 PrefStoreImpl::Create(registry.get(), above_user_prefs_pref_store_, | |
98 PrefValueStore::COMMAND_LINE_STORE); | |
99 below_user_prefs_impl_ = | |
100 PrefStoreImpl::Create(registry.get(), below_user_prefs_pref_store_, | |
101 PrefValueStore::RECOMMENDED_STORE); | |
87 } | 102 } |
88 | 103 |
89 // service_manager::test::ServiceTest: | 104 // service_manager::test::ServiceTest: |
90 std::unique_ptr<service_manager::Service> CreateService() override { | 105 std::unique_ptr<service_manager::Service> CreateService() override { |
91 return base::MakeUnique<ServiceTestClient>(this, | 106 return base::MakeUnique<ServiceTestClient>(this, |
92 worker_pool_owner_->pool()); | 107 worker_pool_owner_->pool()); |
93 } | 108 } |
94 | 109 |
95 std::unique_ptr<base::MessageLoop> CreateMessageLoop() override { | 110 std::unique_ptr<base::MessageLoop> CreateMessageLoop() override { |
96 auto loop = ServiceTest::CreateMessageLoop(); | 111 auto loop = ServiceTest::CreateMessageLoop(); |
97 worker_pool_owner_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( | 112 worker_pool_owner_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( |
98 2, "PrefServiceFactoryTest"); | 113 2, "PrefServiceFactoryTest"); |
99 loop->AddDestructionObserver(this); | 114 loop->AddDestructionObserver(this); |
100 return loop; | 115 return loop; |
101 } | 116 } |
102 | 117 |
103 // base::MessageLoop::DestructionObserver | 118 // base::MessageLoop::DestructionObserver |
104 void WillDestroyCurrentMessageLoop() override { worker_pool_owner_.reset(); } | 119 void WillDestroyCurrentMessageLoop() override { worker_pool_owner_.reset(); } |
105 | 120 |
106 // Create a fully initialized PrefService synchronously. | 121 // Create a fully initialized PrefService synchronously. |
107 std::unique_ptr<PrefService> Create() { | 122 std::unique_ptr<PrefService> Create() { |
108 std::unique_ptr<PrefService> pref_service; | 123 std::unique_ptr<PrefService> pref_service; |
109 base::RunLoop run_loop; | 124 base::RunLoop run_loop; |
110 auto pref_registry = make_scoped_refptr(new PrefRegistrySimple()); | 125 auto pref_registry = make_scoped_refptr(new PrefRegistrySimple()); |
111 pref_registry->RegisterIntegerPref(kKey, kInitialValue); | 126 pref_registry->RegisterIntegerPref(kKey, kInitialValue); |
127 pref_registry->RegisterIntegerPref(kOtherKey, kInitialValue); | |
112 ConnectToPrefService(connector(), pref_registry, | 128 ConnectToPrefService(connector(), pref_registry, |
113 base::Bind(&PrefServiceFactoryTest::OnCreate, | 129 base::Bind(&PrefServiceFactoryTest::OnCreate, |
114 run_loop.QuitClosure(), &pref_service)); | 130 run_loop.QuitClosure(), &pref_service)); |
115 run_loop.Run(); | 131 run_loop.Run(); |
116 return pref_service; | 132 return pref_service; |
117 } | 133 } |
118 | 134 |
119 // Wait until first update of the pref |key| in |pref_service| synchronously. | 135 // Wait until first update of the pref |key| in |pref_service| synchronously. |
120 void WaitForPrefChange(PrefService* pref_service, const std::string& key) { | 136 void WaitForPrefChange(PrefService* pref_service, const std::string& key) { |
121 PrefChangeRegistrar registrar; | 137 PrefChangeRegistrar registrar; |
122 registrar.Init(pref_service); | 138 registrar.Init(pref_service); |
123 base::RunLoop run_loop; | 139 base::RunLoop run_loop; |
124 registrar.Add(key, base::Bind(&OnPrefChanged, run_loop.QuitClosure(), key)); | 140 registrar.Add(key, base::Bind(&OnPrefChanged, run_loop.QuitClosure(), key)); |
125 run_loop.Run(); | 141 run_loop.Run(); |
126 } | 142 } |
127 | 143 |
144 WriteablePrefStore* above_user_prefs_pref_store() { | |
145 return above_user_prefs_pref_store_.get(); | |
146 } | |
147 WriteablePrefStore* below_user_prefs_pref_store() { | |
148 return below_user_prefs_pref_store_.get(); | |
149 } | |
150 | |
128 private: | 151 private: |
129 // Called when the PrefService has been initialized. | 152 // Called when the PrefService has been initialized. |
130 static void OnInit(const base::Closure& quit_closure, bool success) { | 153 static void OnInit(const base::Closure& quit_closure, bool success) { |
131 quit_closure.Run(); | 154 quit_closure.Run(); |
132 } | 155 } |
133 | 156 |
134 // Called when the PrefService has been created. | 157 // Called when the PrefService has been created. |
135 static void OnCreate(const base::Closure& quit_closure, | 158 static void OnCreate(const base::Closure& quit_closure, |
136 std::unique_ptr<PrefService>* out, | 159 std::unique_ptr<PrefService>* out, |
137 std::unique_ptr<PrefService> pref_service) { | 160 std::unique_ptr<PrefService> pref_service) { |
(...skipping 10 matching lines...) Expand all Loading... | |
148 | 171 |
149 static void OnPrefChanged(const base::Closure& quit_closure, | 172 static void OnPrefChanged(const base::Closure& quit_closure, |
150 const std::string& expected_path, | 173 const std::string& expected_path, |
151 const std::string& path) { | 174 const std::string& path) { |
152 if (path == expected_path) | 175 if (path == expected_path) |
153 quit_closure.Run(); | 176 quit_closure.Run(); |
154 } | 177 } |
155 | 178 |
156 base::ScopedTempDir profile_dir_; | 179 base::ScopedTempDir profile_dir_; |
157 std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_owner_; | 180 std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_owner_; |
181 scoped_refptr<WriteablePrefStore> above_user_prefs_pref_store_; | |
182 std::unique_ptr<PrefStoreImpl> above_user_prefs_impl_; | |
183 scoped_refptr<WriteablePrefStore> below_user_prefs_pref_store_; | |
184 std::unique_ptr<PrefStoreImpl> below_user_prefs_impl_; | |
158 | 185 |
159 DISALLOW_COPY_AND_ASSIGN(PrefServiceFactoryTest); | 186 DISALLOW_COPY_AND_ASSIGN(PrefServiceFactoryTest); |
160 }; | 187 }; |
161 | 188 |
162 // Check that a single client can set and read back values. | 189 // Check that a single client can set and read back values. |
163 TEST_F(PrefServiceFactoryTest, Basic) { | 190 TEST_F(PrefServiceFactoryTest, Basic) { |
164 auto pref_service = Create(); | 191 auto pref_service = Create(); |
165 | 192 |
166 EXPECT_EQ(kInitialValue, pref_service->GetInteger(kKey)); | 193 EXPECT_EQ(kInitialValue, pref_service->GetInteger(kKey)); |
167 pref_service->SetInteger(kKey, kUpdatedValue); | 194 pref_service->SetInteger(kKey, kUpdatedValue); |
168 EXPECT_EQ(kUpdatedValue, pref_service->GetInteger(kKey)); | 195 EXPECT_EQ(kUpdatedValue, pref_service->GetInteger(kKey)); |
169 } | 196 } |
170 | 197 |
171 // Check that updates in one client eventually propagates to the other. | 198 // Check that updates in one client eventually propagates to the other. |
172 TEST_F(PrefServiceFactoryTest, MultipleClients) { | 199 TEST_F(PrefServiceFactoryTest, MultipleClients) { |
173 auto pref_service = Create(); | 200 auto pref_service = Create(); |
174 auto pref_service2 = Create(); | 201 auto pref_service2 = Create(); |
175 | 202 |
176 EXPECT_EQ(kInitialValue, pref_service->GetInteger(kKey)); | 203 EXPECT_EQ(kInitialValue, pref_service->GetInteger(kKey)); |
177 EXPECT_EQ(kInitialValue, pref_service2->GetInteger(kKey)); | 204 EXPECT_EQ(kInitialValue, pref_service2->GetInteger(kKey)); |
178 pref_service->SetInteger(kKey, kUpdatedValue); | 205 pref_service->SetInteger(kKey, kUpdatedValue); |
179 WaitForPrefChange(pref_service2.get(), kKey); | 206 WaitForPrefChange(pref_service2.get(), kKey); |
180 EXPECT_EQ(kUpdatedValue, pref_service2->GetInteger(kKey)); | 207 EXPECT_EQ(kUpdatedValue, pref_service2->GetInteger(kKey)); |
181 } | 208 } |
182 | 209 |
210 // Check that read-only pref store changes are observed. | |
211 TEST_F(PrefServiceFactoryTest, ReadOnlyPrefStore) { | |
212 auto pref_service = Create(); | |
213 | |
214 EXPECT_EQ(kInitialValue, pref_service->GetInteger(kKey)); | |
215 | |
216 below_user_prefs_pref_store()->SetValue( | |
217 kKey, base::MakeUnique<base::Value>(kUpdatedValue), 0); | |
218 WaitForPrefChange(pref_service.get(), kKey); | |
219 EXPECT_EQ(kUpdatedValue, pref_service->GetInteger(kKey)); | |
220 pref_service->SetInteger(kKey, 3); | |
221 EXPECT_EQ(3, pref_service->GetInteger(kKey)); | |
222 above_user_prefs_pref_store()->SetValue(kKey, | |
223 base::MakeUnique<base::Value>(4), 0); | |
224 WaitForPrefChange(pref_service.get(), kKey); | |
225 EXPECT_EQ(4, pref_service->GetInteger(kKey)); | |
226 } | |
227 | |
228 // Check that updates to read-only pref stores are correctly layered. | |
229 TEST_F(PrefServiceFactoryTest, ReadOnlyPrefStore_Layering) { | |
230 auto pref_service = Create(); | |
231 | |
232 above_user_prefs_pref_store()->SetValue( | |
233 kKey, base::MakeUnique<base::Value>(kInitialValue), 0); | |
234 WaitForPrefChange(pref_service.get(), kKey); | |
235 EXPECT_EQ(kInitialValue, pref_service->GetInteger(kKey)); | |
236 | |
237 below_user_prefs_pref_store()->SetValue( | |
238 kKey, base::MakeUnique<base::Value>(kUpdatedValue), 0); | |
239 below_user_prefs_pref_store()->SetValue( | |
tibell
2017/03/27 03:37:55
Add:
// This update is needed to check that the c
Sam McNally
2017/03/27 06:44:40
Done.
| |
240 kOtherKey, base::MakeUnique<base::Value>(kUpdatedValue), 0); | |
241 WaitForPrefChange(pref_service.get(), kOtherKey); | |
242 EXPECT_EQ(kInitialValue, pref_service->GetInteger(kKey)); | |
243 } | |
244 | |
245 // Check that writes to user prefs are correctly layered with read-only | |
246 // pref stores. | |
247 TEST_F(PrefServiceFactoryTest, ReadOnlyPrefStore_UserPrefStoreLayering) { | |
248 auto pref_service = Create(); | |
249 | |
250 above_user_prefs_pref_store()->SetValue(kKey, | |
251 base::MakeUnique<base::Value>(2), 0); | |
252 WaitForPrefChange(pref_service.get(), kKey); | |
253 EXPECT_EQ(2, pref_service->GetInteger(kKey)); | |
254 | |
255 pref_service->SetInteger(kKey, 3); | |
256 EXPECT_EQ(2, pref_service->GetInteger(kKey)); | |
257 } | |
258 | |
183 } // namespace | 259 } // namespace |
184 } // namespace prefs | 260 } // namespace prefs |
OLD | NEW |