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

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

Issue 2743463002: WIP: Pref service user prefs. (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
« no previous file with comments | « services/preferences/user_prefs_factory.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "services/preferences/public/cpp/pref_store_client.h"
6
7 #include "base/macros.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/values.h"
12 #include "components/prefs/in_memory_pref_store.h"
13 #include "mojo/public/cpp/bindings/binding_set.h"
14 #include "services/preferences/public/cpp/persistent_pref_store_client.h"
15 #include "services/preferences/public/interfaces/preferences.mojom.h"
16 #include "services/preferences/user_prefs.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 using testing::Invoke;
21 using testing::WithArg;
22 using testing::WithoutArgs;
23 using testing::_;
24
25 namespace prefs {
26
27 namespace {
28
29 class PrefStoreObserverMock : public PrefStore::Observer {
30 public:
31 MOCK_METHOD1(OnPrefValueChanged, void(const std::string&));
32 MOCK_METHOD1(OnInitializationCompleted, void(bool succeeded));
33 };
34
35 class PrefStoreConnectorMock : public mojom::PrefStoreConnector {
36 public:
37 MOCK_METHOD1(Connect, void(const ConnectCallback&));
38 };
39
40 class DelayedInitializationPrefStore : public InMemoryPrefStore {
41 public:
42 DelayedInitializationPrefStore(bool should_succeed)
43 : should_succeed_(should_succeed) {}
44 bool IsInitializationComplete() const override { return initialized_; }
45
46 void AddObserver(PrefStore::Observer* observer) override {
47 observers_.AddObserver(observer);
48 }
49
50 void RemoveObserver(PrefStore::Observer* observer) override {
51 observers_.RemoveObserver(observer);
52 }
53
54 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override {
55 DCHECK(!error_delegate);
56 base::ThreadTaskRunnerHandle::Get()->PostTask(
57 FROM_HERE,
58 base::Bind(&DelayedInitializationPrefStore::Initialize, this));
59 }
60
61 private:
62 ~DelayedInitializationPrefStore() override = default;
63
64 void Initialize() {
65 initialized_ = should_succeed_;
66 for (auto& observer : observers_) {
67 observer.OnInitializationCompleted(initialized_);
68 }
69 }
70
71 bool should_succeed_;
72 bool initialized_ = false;
73 base::ObserverList<PrefStore::Observer, true> observers_;
74 };
75
76 } // namespace
77
78 class UserPrefsTest : public testing::Test {
79 public:
80 UserPrefsTest() = default;
81
82 // testing::Test:
83 void SetUp() override {}
84
85 void TearDown() override {}
86
87 void CreateUserPrefs(scoped_refptr<PersistentPrefStore> backing_pref_store) {
88 user_prefs_ =
89 base::MakeUnique<UserPrefs>(std::move(backing_pref_store), nullptr);
90 mojo::Binding<mojom::PersistentPrefStoreConnector> binding(
91 user_prefs_.get());
92 pref_store_ = CreateConnection();
93 }
94
95 scoped_refptr<PersistentPrefStore> CreateConnection() {
96 return make_scoped_refptr(new PersistentPrefStoreClient(
97 bindings_.CreateInterfacePtrAndBind(user_prefs_.get())));
98 }
99
100 PersistentPrefStore& pref_store() { return *pref_store_; }
101
102 private:
103 base::MessageLoop message_loop_;
104
105 std::unique_ptr<UserPrefs> user_prefs_;
106 mojo::BindingSet<mojom::PersistentPrefStoreConnector> bindings_;
107
108 scoped_refptr<PersistentPrefStore> pref_store_;
109
110 DISALLOW_COPY_AND_ASSIGN(UserPrefsTest);
111 };
112
113 TEST_F(UserPrefsTest, InitializationSuccess) {
114 auto backing_pref_store =
115 make_scoped_refptr(new DelayedInitializationPrefStore(true));
116 CreateUserPrefs(backing_pref_store);
117 pref_store().ReadPrefsAsync(nullptr);
118 PrefStoreObserverMock observer;
119 pref_store().AddObserver(&observer);
120 base::RunLoop run_loop;
121 EXPECT_CALL(observer, OnInitializationCompleted(true))
122 .Times(1)
123 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); })));
124 run_loop.Run();
125 EXPECT_TRUE(pref_store().IsInitializationComplete());
126 pref_store().RemoveObserver(&observer);
127 }
128
129 TEST_F(UserPrefsTest, InitializationFailure) {
130 auto backing_pref_store =
131 make_scoped_refptr(new DelayedInitializationPrefStore(false));
132 CreateUserPrefs(backing_pref_store);
133 pref_store().ReadPrefsAsync(nullptr);
134 PrefStoreObserverMock observer;
135 pref_store().AddObserver(&observer);
136 base::RunLoop run_loop;
137 EXPECT_CALL(observer, OnInitializationCompleted(false))
138 .Times(1)
139 .WillOnce(WithoutArgs(Invoke([&run_loop]() { run_loop.Quit(); })));
140 run_loop.Run();
141 EXPECT_FALSE(pref_store().IsInitializationComplete());
142 pref_store().RemoveObserver(&observer);
143 }
144
145 } // namespace prefs
OLDNEW
« no previous file with comments | « services/preferences/user_prefs_factory.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698