Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: services/preferences/persistent_pref_store_impl_unittest.cc

Issue 2778643002: Pref service: Filter updates from read-only pref stores. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "components/prefs/in_memory_pref_store.h" 14 #include "components/prefs/in_memory_pref_store.h"
15 #include "mojo/public/cpp/bindings/binding_set.h" 15 #include "mojo/public/cpp/bindings/binding_set.h"
16 #include "services/preferences/public/cpp/persistent_pref_store_client.h" 16 #include "services/preferences/public/cpp/persistent_pref_store_client.h"
17 #include "services/preferences/public/interfaces/preferences.mojom.h" 17 #include "services/preferences/public/interfaces/preferences.mojom.h"
18 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 using testing::Invoke; 21 using testing::Invoke;
22 using testing::WithoutArgs; 22 using testing::WithoutArgs;
23 23
24 namespace prefs { 24 namespace prefs {
25 namespace { 25 namespace {
26 26
27 class PrefStoreObserverMock : public PrefStore::Observer { 27 class PrefStoreObserverMock : public PrefStore::Observer {
28 public: 28 public:
29 MOCK_METHOD1(OnPrefValueChanged, void(const std::string&)); 29 MOCK_METHOD1(OnPrefValueChanged, void(const std::string&));
30 MOCK_METHOD1(OnInitializationCompleted, void(bool succeeded)); 30 MOCK_METHOD1(OnInitializationCompleted, void(bool));
31 }; 31 };
32 32
33 class PersistentPrefStoreMock : public InMemoryPrefStore { 33 class PersistentPrefStoreMock : public InMemoryPrefStore {
34 public: 34 public:
35 MOCK_METHOD0(CommitPendingWrite, void()); 35 MOCK_METHOD0(CommitPendingWrite, void());
36 MOCK_METHOD0(SchedulePendingLossyWrites, void()); 36 MOCK_METHOD0(SchedulePendingLossyWrites, void());
37 MOCK_METHOD0(ClearMutableValues, void()); 37 MOCK_METHOD0(ClearMutableValues, void());
38 38
39 private: 39 private:
40 ~PersistentPrefStoreMock() override = default; 40 ~PersistentPrefStoreMock() override = default;
41 }; 41 };
42 42
43 void ExpectPrefChange(PrefStore* pref_store, base::StringPiece key) {
44 PrefStoreObserverMock observer;
45 pref_store->AddObserver(&observer);
46 base::RunLoop run_loop;
47 EXPECT_CALL(observer, OnPrefValueChanged(key.as_string()))
48 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); })));
49 run_loop.Run();
50 pref_store->RemoveObserver(&observer);
51 }
52
43 class InitializationMockPersistentPrefStore : public InMemoryPrefStore { 53 class InitializationMockPersistentPrefStore : public InMemoryPrefStore {
44 public: 54 public:
45 InitializationMockPersistentPrefStore( 55 InitializationMockPersistentPrefStore(
46 bool success, 56 bool success,
47 PersistentPrefStore::PrefReadError error, 57 PersistentPrefStore::PrefReadError error,
48 bool read_only) 58 bool read_only)
49 : success_(success), read_error_(error), read_only_(read_only) {} 59 : success_(success), read_error_(error), read_only_(read_only) {}
50 60
51 bool IsInitializationComplete() const override { 61 bool IsInitializationComplete() const override {
52 return initialized_ && success_; 62 return initialized_ && success_;
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 auto backing_pref_store = 161 auto backing_pref_store =
152 make_scoped_refptr(new InitializationMockPersistentPrefStore( 162 make_scoped_refptr(new InitializationMockPersistentPrefStore(
153 false, PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, true)); 163 false, PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, true));
154 CreateImpl(backing_pref_store); 164 CreateImpl(backing_pref_store);
155 EXPECT_FALSE(pref_store()->IsInitializationComplete()); 165 EXPECT_FALSE(pref_store()->IsInitializationComplete());
156 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, 166 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE,
157 pref_store()->GetReadError()); 167 pref_store()->GetReadError());
158 EXPECT_TRUE(pref_store()->ReadOnly()); 168 EXPECT_TRUE(pref_store()->ReadOnly());
159 } 169 }
160 170
161 class TestReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
162 public:
163 TestReadErrorDelegate(PersistentPrefStore::PrefReadError* storage,
164 const base::Closure& quit)
165 : storage_(storage), quit_(quit) {
166 DCHECK(storage_);
167 DCHECK(quit_);
168 }
169
170 void OnError(PersistentPrefStore::PrefReadError error) override {
171 *storage_ = error;
172 quit_.Run();
173 }
174
175 private:
176 PersistentPrefStore::PrefReadError* const storage_;
177 const base::Closure quit_;
178 };
179
180 TEST_F(PersistentPrefStoreImplTest, InitialValue) { 171 TEST_F(PersistentPrefStoreImplTest, InitialValue) {
181 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore()); 172 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore());
182 const base::Value value("value"); 173 const base::Value value("value");
183 backing_pref_store->SetValue(kKey, value.CreateDeepCopy(), 0); 174 backing_pref_store->SetValue(kKey, value.CreateDeepCopy(), 0);
184 CreateImpl(backing_pref_store); 175 CreateImpl(backing_pref_store);
185 EXPECT_TRUE(pref_store()->IsInitializationComplete()); 176 EXPECT_TRUE(pref_store()->IsInitializationComplete());
186 const base::Value* output = nullptr; 177 const base::Value* output = nullptr;
187 ASSERT_TRUE(pref_store()->GetValue(kKey, &output)); 178 ASSERT_TRUE(pref_store()->GetValue(kKey, &output));
188 EXPECT_TRUE(value.Equals(output)); 179 EXPECT_TRUE(value.Equals(output));
189 } 180 }
(...skipping 14 matching lines...) Expand all
204 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore()); 195 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore());
205 CreateImpl(backing_pref_store); 196 CreateImpl(backing_pref_store);
206 EXPECT_TRUE(pref_store()->IsInitializationComplete()); 197 EXPECT_TRUE(pref_store()->IsInitializationComplete());
207 198
208 auto other_pref_store = CreateConnection(); 199 auto other_pref_store = CreateConnection();
209 EXPECT_TRUE(other_pref_store->IsInitializationComplete()); 200 EXPECT_TRUE(other_pref_store->IsInitializationComplete());
210 201
211 const base::Value value("value"); 202 const base::Value value("value");
212 pref_store()->SetValueSilently(kKey, value.CreateDeepCopy(), 0); 203 pref_store()->SetValueSilently(kKey, value.CreateDeepCopy(), 0);
213 204
214 PrefStoreObserverMock observer; 205 ExpectPrefChange(other_pref_store.get(), kKey);
215 other_pref_store->AddObserver(&observer);
216 base::RunLoop run_loop;
217 EXPECT_CALL(observer, OnPrefValueChanged(kKey))
218 .Times(1)
219 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); })));
220 run_loop.Run();
221 other_pref_store->RemoveObserver(&observer);
222
223 const base::Value* output = nullptr; 206 const base::Value* output = nullptr;
224 ASSERT_TRUE(other_pref_store->GetValue(kKey, &output)); 207 ASSERT_TRUE(other_pref_store->GetValue(kKey, &output));
225 EXPECT_TRUE(value.Equals(output)); 208 EXPECT_TRUE(value.Equals(output));
226 } 209 }
227 210
228 TEST_F(PersistentPrefStoreImplTest, UnregisteredPrefNotObservedByOtherClient) { 211 TEST_F(PersistentPrefStoreImplTest, UnregisteredPrefNotObservedByOtherClient) {
229 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore()); 212 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore());
230 CreateImpl(backing_pref_store); 213 CreateImpl(backing_pref_store);
231 EXPECT_TRUE(pref_store()->IsInitializationComplete()); 214 EXPECT_TRUE(pref_store()->IsInitializationComplete());
232 215
233 PersistentPrefStoreImpl::ObservedPrefs observed_prefs; 216 PersistentPrefStoreImpl::ObservedPrefs observed_prefs;
234 observed_prefs.insert(kKey); 217 observed_prefs.insert(kKey);
235 218
236 auto other_pref_store = CreateConnection(std::move(observed_prefs)); 219 auto other_pref_store = CreateConnection(std::move(observed_prefs));
237 EXPECT_TRUE(other_pref_store->IsInitializationComplete()); 220 EXPECT_TRUE(other_pref_store->IsInitializationComplete());
238 221
239 pref_store()->SetValue(kOtherKey, base::MakeUnique<base::Value>(123), 0); 222 pref_store()->SetValue(kOtherKey, base::MakeUnique<base::Value>(123), 0);
240 pref_store()->SetValue(kKey, base::MakeUnique<base::Value>("value"), 0); 223 pref_store()->SetValue(kKey, base::MakeUnique<base::Value>("value"), 0);
241 224
242 PrefStoreObserverMock observer; 225 ExpectPrefChange(other_pref_store.get(), kKey);
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)); 226 EXPECT_FALSE(other_pref_store->GetValue(kOtherKey, nullptr));
253 } 227 }
254 228
255 TEST_F(PersistentPrefStoreImplTest, 229 TEST_F(PersistentPrefStoreImplTest,
256 WriteWithoutPathExpansionObservedByOtherClient) { 230 WriteWithoutPathExpansionObservedByOtherClient) {
257 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore()); 231 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore());
258 CreateImpl(backing_pref_store); 232 CreateImpl(backing_pref_store);
259 EXPECT_TRUE(pref_store()->IsInitializationComplete()); 233 EXPECT_TRUE(pref_store()->IsInitializationComplete());
260 234
261 auto other_pref_store = CreateConnection(); 235 auto other_pref_store = CreateConnection();
262 EXPECT_TRUE(other_pref_store->IsInitializationComplete()); 236 EXPECT_TRUE(other_pref_store->IsInitializationComplete());
263 237
264 base::DictionaryValue dict; 238 base::DictionaryValue dict;
265 dict.SetStringWithoutPathExpansion(kKey, "value"); 239 dict.SetStringWithoutPathExpansion(kKey, "value");
266 pref_store()->SetValue(kKey, dict.CreateDeepCopy(), 0); 240 pref_store()->SetValue(kKey, dict.CreateDeepCopy(), 0);
267 241
268 PrefStoreObserverMock observer; 242 ExpectPrefChange(other_pref_store.get(), kKey);
269 other_pref_store->AddObserver(&observer);
270 base::RunLoop run_loop;
271 EXPECT_CALL(observer, OnPrefValueChanged(kKey))
272 .Times(1)
273 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); })));
274 run_loop.Run();
275 other_pref_store->RemoveObserver(&observer);
276
277 const base::Value* output = nullptr; 243 const base::Value* output = nullptr;
278 ASSERT_TRUE(other_pref_store->GetValue(kKey, &output)); 244 ASSERT_TRUE(other_pref_store->GetValue(kKey, &output));
279 EXPECT_TRUE(dict.Equals(output)); 245 EXPECT_TRUE(dict.Equals(output));
280 } 246 }
281 247
282 TEST_F(PersistentPrefStoreImplTest, RemoveObservedByOtherClient) { 248 TEST_F(PersistentPrefStoreImplTest, RemoveObservedByOtherClient) {
283 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore()); 249 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore());
284 const base::Value value("value"); 250 const base::Value value("value");
285 backing_pref_store->SetValue(kKey, value.CreateDeepCopy(), 0); 251 backing_pref_store->SetValue(kKey, value.CreateDeepCopy(), 0);
286 CreateImpl(backing_pref_store); 252 CreateImpl(backing_pref_store);
287 EXPECT_TRUE(pref_store()->IsInitializationComplete()); 253 EXPECT_TRUE(pref_store()->IsInitializationComplete());
288 254
289 auto other_pref_store = CreateConnection(); 255 auto other_pref_store = CreateConnection();
290 EXPECT_TRUE(other_pref_store->IsInitializationComplete()); 256 EXPECT_TRUE(other_pref_store->IsInitializationComplete());
291 257
292 const base::Value* output = nullptr; 258 const base::Value* output = nullptr;
293 ASSERT_TRUE(other_pref_store->GetValue(kKey, &output)); 259 ASSERT_TRUE(other_pref_store->GetValue(kKey, &output));
294 EXPECT_TRUE(value.Equals(output)); 260 EXPECT_TRUE(value.Equals(output));
295 pref_store()->RemoveValue(kKey, 0); 261 pref_store()->RemoveValue(kKey, 0);
296 262
297 // This should be a no-op and shouldn't trigger a notification for the other 263 // This should be a no-op and shouldn't trigger a notification for the other
298 // client. 264 // client.
299 pref_store()->RemoveValue(kKey, 0); 265 pref_store()->RemoveValue(kKey, 0);
300 266
301 PrefStoreObserverMock observer; 267 ExpectPrefChange(other_pref_store.get(), kKey);
302 other_pref_store->AddObserver(&observer);
303 base::RunLoop run_loop;
304 EXPECT_CALL(observer, OnPrefValueChanged(kKey))
305 .Times(1)
306 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); })));
307 run_loop.Run();
308 base::RunLoop().RunUntilIdle();
309 other_pref_store->RemoveObserver(&observer);
310
311 EXPECT_FALSE(other_pref_store->GetValue(kKey, &output)); 268 EXPECT_FALSE(other_pref_store->GetValue(kKey, &output));
312 } 269 }
313 270
314 TEST_F(PersistentPrefStoreImplTest, 271 TEST_F(PersistentPrefStoreImplTest,
315 RemoveWithoutPathExpansionObservedByOtherClient) { 272 RemoveWithoutPathExpansionObservedByOtherClient) {
316 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore()); 273 auto backing_pref_store = make_scoped_refptr(new InMemoryPrefStore());
317 base::DictionaryValue dict; 274 base::DictionaryValue dict;
318 dict.SetStringWithoutPathExpansion(kKey, "value"); 275 dict.SetStringWithoutPathExpansion(kKey, "value");
319 backing_pref_store->SetValue(kKey, dict.CreateDeepCopy(), 0); 276 backing_pref_store->SetValue(kKey, dict.CreateDeepCopy(), 0);
320 CreateImpl(backing_pref_store); 277 CreateImpl(backing_pref_store);
321 EXPECT_TRUE(pref_store()->IsInitializationComplete()); 278 EXPECT_TRUE(pref_store()->IsInitializationComplete());
322 279
323 auto other_pref_store = CreateConnection(); 280 auto other_pref_store = CreateConnection();
324 EXPECT_TRUE(other_pref_store->IsInitializationComplete()); 281 EXPECT_TRUE(other_pref_store->IsInitializationComplete());
325 282
326 const base::Value* output = nullptr; 283 const base::Value* output = nullptr;
327 ASSERT_TRUE(other_pref_store->GetValue(kKey, &output)); 284 ASSERT_TRUE(other_pref_store->GetValue(kKey, &output));
328 EXPECT_TRUE(dict.Equals(output)); 285 EXPECT_TRUE(dict.Equals(output));
329 286
330 base::Value* mutable_value = nullptr; 287 base::Value* mutable_value = nullptr;
331 dict.SetStringWithoutPathExpansion(kKey, "value"); 288 dict.SetStringWithoutPathExpansion(kKey, "value");
332 ASSERT_TRUE(pref_store()->GetMutableValue(kKey, &mutable_value)); 289 ASSERT_TRUE(pref_store()->GetMutableValue(kKey, &mutable_value));
333 base::DictionaryValue* mutable_dict = nullptr; 290 base::DictionaryValue* mutable_dict = nullptr;
334 ASSERT_TRUE(mutable_value->GetAsDictionary(&mutable_dict)); 291 ASSERT_TRUE(mutable_value->GetAsDictionary(&mutable_dict));
335 mutable_dict->RemoveWithoutPathExpansion(kKey, nullptr); 292 mutable_dict->RemoveWithoutPathExpansion(kKey, nullptr);
336 pref_store()->ReportValueChanged(kKey, 0); 293 pref_store()->ReportValueChanged(kKey, 0);
337 294
338 PrefStoreObserverMock observer; 295 ExpectPrefChange(other_pref_store.get(), kKey);
339 other_pref_store->AddObserver(&observer);
340 base::RunLoop run_loop;
341 EXPECT_CALL(observer, OnPrefValueChanged(kKey))
342 .Times(1)
343 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); })));
344 run_loop.Run();
345 other_pref_store->RemoveObserver(&observer);
346
347 ASSERT_TRUE(other_pref_store->GetValue(kKey, &output)); 296 ASSERT_TRUE(other_pref_store->GetValue(kKey, &output));
348 const base::DictionaryValue* dict_value = nullptr; 297 const base::DictionaryValue* dict_value = nullptr;
349 ASSERT_TRUE(output->GetAsDictionary(&dict_value)); 298 ASSERT_TRUE(output->GetAsDictionary(&dict_value));
350 EXPECT_TRUE(dict_value->empty()); 299 EXPECT_TRUE(dict_value->empty());
351 } 300 }
352 301
353 TEST_F(PersistentPrefStoreImplTest, CommitPendingWrite) { 302 TEST_F(PersistentPrefStoreImplTest, CommitPendingWrite) {
354 auto backing_store = make_scoped_refptr(new PersistentPrefStoreMock); 303 auto backing_store = make_scoped_refptr(new PersistentPrefStoreMock);
355 CreateImpl(backing_store); 304 CreateImpl(backing_store);
356 base::RunLoop run_loop; 305 base::RunLoop run_loop;
357 EXPECT_CALL(*backing_store, CommitPendingWrite()) 306 EXPECT_CALL(*backing_store, CommitPendingWrite())
358 .Times(2) 307 .Times(2)
359 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); }))); 308 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); })));
360 pref_store()->CommitPendingWrite(); 309 pref_store()->CommitPendingWrite();
361 run_loop.Run(); 310 run_loop.Run();
362 } 311 }
363 312
364 TEST_F(PersistentPrefStoreImplTest, SchedulePendingLossyWrites) { 313 TEST_F(PersistentPrefStoreImplTest, SchedulePendingLossyWrites) {
365 auto backing_store = make_scoped_refptr(new PersistentPrefStoreMock); 314 auto backing_store = make_scoped_refptr(new PersistentPrefStoreMock);
366 CreateImpl(backing_store); 315 CreateImpl(backing_store);
367 base::RunLoop run_loop; 316 base::RunLoop run_loop;
368 EXPECT_CALL(*backing_store, SchedulePendingLossyWrites()) 317 EXPECT_CALL(*backing_store, SchedulePendingLossyWrites())
369 .Times(1)
370 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); }))); 318 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); })));
371 EXPECT_CALL(*backing_store, CommitPendingWrite()).Times(1); 319 EXPECT_CALL(*backing_store, CommitPendingWrite()).Times(1);
372 pref_store()->SchedulePendingLossyWrites(); 320 pref_store()->SchedulePendingLossyWrites();
373 run_loop.Run(); 321 run_loop.Run();
374 } 322 }
375 323
376 TEST_F(PersistentPrefStoreImplTest, ClearMutableValues) { 324 TEST_F(PersistentPrefStoreImplTest, ClearMutableValues) {
377 auto backing_store = make_scoped_refptr(new PersistentPrefStoreMock); 325 auto backing_store = make_scoped_refptr(new PersistentPrefStoreMock);
378 CreateImpl(backing_store); 326 CreateImpl(backing_store);
379 base::RunLoop run_loop; 327 base::RunLoop run_loop;
380 EXPECT_CALL(*backing_store, ClearMutableValues()) 328 EXPECT_CALL(*backing_store, ClearMutableValues())
381 .Times(1)
382 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); }))); 329 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); })));
383 EXPECT_CALL(*backing_store, CommitPendingWrite()).Times(1); 330 EXPECT_CALL(*backing_store, CommitPendingWrite()).Times(1);
384 pref_store()->ClearMutableValues(); 331 pref_store()->ClearMutableValues();
385 run_loop.Run(); 332 run_loop.Run();
386 } 333 }
387 334
388 } // namespace 335 } // namespace
389 } // namespace prefs 336 } // namespace prefs
OLDNEW
« no previous file with comments | « components/sync_preferences/pref_service_syncable_factory.cc ('k') | services/preferences/pref_service_factory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698