OLD | NEW |
---|---|
(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 "services/preferences/public/interfaces/preferences.mojom.h" | |
13 #include "testing/gmock/include/gmock/gmock.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 using testing::Invoke; | |
17 using testing::WithArg; | |
18 using testing::WithoutArgs; | |
19 using testing::_; | |
20 | |
21 namespace prefs { | |
22 | |
23 namespace { | |
24 | |
25 class PrefStoreObserverMock : public PrefStore::Observer { | |
26 public: | |
27 MOCK_METHOD1(OnPrefValueChanged, void(const std::string&)); | |
28 MOCK_METHOD1(OnInitializationCompleted, void(bool succeeded)); | |
29 }; | |
30 | |
31 class PrefStoreConnectorMock : public mojom::PrefStoreConnector { | |
32 public: | |
33 MOCK_METHOD1(Connect, void(const ConnectCallback&)); | |
34 }; | |
35 | |
36 } // namespace | |
37 | |
38 class PrefStoreClientTest : public testing::Test { | |
39 public: | |
40 PrefStoreClientTest() = default; | |
41 ~PrefStoreClientTest() override {} | |
42 | |
43 PrefStoreObserverMock& observer() { return observer_; } | |
44 PrefStoreClient* store() { return store_.get(); } | |
45 | |
46 bool initialized() { return store_->initialized_; } | |
47 void OnPrefChanged(const std::string& key, const base::Value& value) { | |
48 observer_ptr_->OnPrefChanged(key, value.CreateDeepCopy()); | |
49 } | |
50 void OnInitializationCompleted() { | |
51 observer_ptr_->OnInitializationCompleted(true); | |
52 } | |
53 | |
54 // testing::Test: | |
55 void SetUp() override { | |
56 store_ = new PrefStoreClient(mojom::PrefStoreConnection::New( | |
57 mojo::MakeRequest(&observer_ptr_), | |
58 base::MakeUnique<base::DictionaryValue>(), false)); | |
59 store_->AddObserver(&observer_); | |
60 } | |
61 void TearDown() override { | |
62 store_->RemoveObserver(&observer_); | |
63 store_ = nullptr; | |
64 } | |
65 | |
66 private: | |
67 mojom::PrefStoreObserverPtr observer_ptr_; | |
68 PrefStoreObserverMock observer_; | |
69 scoped_refptr<PrefStoreClient> store_; | |
70 | |
71 // Required by mojo binding code within PrefStoreClient. | |
72 base::MessageLoop message_loop_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(PrefStoreClientTest); | |
75 }; | |
76 | |
77 // Tests that observers are notified upon the completion of initialization, and | |
78 // that values become available. | |
79 TEST_F(PrefStoreClientTest, Initialization) { | |
80 // The store should start out uninitialized if the backing store does. | |
81 EXPECT_FALSE(initialized()); | |
82 EXPECT_CALL(observer(), OnInitializationCompleted(_)).Times(0); | |
83 | |
84 testing::Mock::VerifyAndClearExpectations(&observer()); | |
85 | |
86 const char key[] = "hey"; | |
87 const int kValue = 42; | |
88 base::Value pref(kValue); | |
89 | |
90 // PrefStore notifies of PreferencesChanged, completing | |
91 // initialization. | |
92 base::RunLoop loop; | |
93 EXPECT_CALL(observer(), OnInitializationCompleted(true)); | |
94 EXPECT_CALL(observer(), OnPrefValueChanged(key)) | |
95 .WillOnce(WithoutArgs(Invoke([&loop]() { loop.Quit(); }))); | |
96 OnInitializationCompleted(); | |
97 OnPrefChanged(key, pref); | |
98 loop.Run(); | |
99 EXPECT_TRUE(initialized()); | |
100 | |
101 const base::Value* value = nullptr; | |
102 int actual_value; | |
103 EXPECT_TRUE(store()->GetValue(key, &value)); | |
104 ASSERT_TRUE(value); | |
105 EXPECT_TRUE(value->GetAsInteger(&actual_value)); | |
106 EXPECT_EQ(kValue, actual_value); | |
107 } | |
108 | |
109 // Test that when initialized with multiple keys, that observers receive a | |
110 // notification for each key. | |
111 TEST_F(PrefStoreClientTest, MultipleKeyInitialization) { | |
112 const char key1[] = "hey"; | |
113 const char key2[] = "listen"; | |
114 | |
115 EXPECT_FALSE(initialized()); | |
116 EXPECT_CALL(observer(), OnInitializationCompleted(_)).Times(0); | |
117 | |
118 testing::Mock::VerifyAndClearExpectations(&observer()); | |
119 | |
120 const int kValue = 42; | |
121 base::Value pref1(kValue); | |
122 base::Value pref2("look"); | |
123 | |
124 base::DictionaryValue prefs; | |
125 prefs.Set(key1, pref1.CreateDeepCopy()); | |
126 prefs.Set(key2, pref2.CreateDeepCopy()); | |
127 | |
128 // The observer should be notified of all keys set. | |
129 base::RunLoop loop; | |
130 EXPECT_CALL(observer(), OnInitializationCompleted(true)); | |
131 EXPECT_CALL(observer(), OnPrefValueChanged(key1)); | |
132 EXPECT_CALL(observer(), OnPrefValueChanged(key2)) | |
133 .WillOnce(WithoutArgs(Invoke([&loop]() { loop.Quit(); }))); | |
134 OnInitializationCompleted(); | |
135 OnPrefChanged(key1, pref1); | |
136 OnPrefChanged(key2, pref2); | |
137 loop.Run(); | |
138 EXPECT_TRUE(initialized()); | |
139 } | |
140 | |
141 // Tests that multiple PrefStore::Observers can be added to a PrefStoreClient | |
142 // and that they are each notified of changes. | |
143 TEST_F(PrefStoreClientTest, MultipleObservers) { | |
144 PrefStoreObserverMock observer2; | |
145 store()->AddObserver(&observer2); | |
146 | |
147 const char key[] = "hey"; | |
148 const int kValue = 42; | |
149 base::Value pref(kValue); | |
150 | |
151 // PrefStore notifies of PreferencesChanged, completing | |
152 // initialization. | |
153 base::RunLoop loop; | |
154 EXPECT_CALL(observer(), OnInitializationCompleted(true)); | |
155 EXPECT_CALL(observer2, OnInitializationCompleted(true)); | |
156 EXPECT_CALL(observer(), OnPrefValueChanged(key)); | |
157 EXPECT_CALL(observer2, OnPrefValueChanged(key)) | |
158 .WillOnce(WithoutArgs(Invoke([&loop]() { loop.Quit(); }))); | |
159 OnInitializationCompleted(); | |
160 OnPrefChanged(key, pref); | |
161 loop.Run(); | |
162 | |
163 store()->RemoveObserver(&observer2); | |
164 } | |
165 | |
166 TEST_F(PrefStoreClientTest, Initialized) { | |
167 mojom::PrefStoreObserverPtr observer_ptr; | |
168 PrefStoreObserverMock observer; | |
169 const char key[] = "hey"; | |
170 const int kValue = 42; | |
171 base::Value pref(kValue); | |
172 auto prefs = base::MakeUnique<base::DictionaryValue>(); | |
173 prefs->Set(key, pref.CreateDeepCopy()); | |
174 auto store = | |
175 make_scoped_refptr(new PrefStoreClient(mojom::PrefStoreConnection::New( | |
176 mojo::MakeRequest(&observer_ptr), std::move(prefs), true))); | |
177 store->AddObserver(&observer); | |
178 | |
179 const base::Value* value = nullptr; | |
180 int actual_value; | |
181 EXPECT_TRUE(store->GetValue(key, &value)); | |
182 ASSERT_TRUE(value); | |
183 EXPECT_TRUE(value->GetAsInteger(&actual_value)); | |
Sam McNally
2017/03/07 03:38:08
Either ASSERT_TRUE here or initialize |actual_valu
tibell
2017/03/07 04:20:28
Done.
| |
184 EXPECT_EQ(kValue, actual_value); | |
185 EXPECT_CALL(observer, OnInitializationCompleted(_)).Times(0); | |
186 EXPECT_CALL(observer, OnPrefValueChanged(_)).Times(0); | |
187 observer_ptr.FlushForTesting(); | |
188 | |
189 store->RemoveObserver(&observer); | |
190 } | |
191 | |
192 } // namespace prefs | |
OLD | NEW |