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

Side by Side Diff: base/prefs/overlay_user_pref_store_unittest.cc

Issue 210063003: Refactor TestingPrefStore to make it useful to some tests of load errors and asynchrony. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adopt a non-gmock approach in the clients. Created 6 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 | « no previous file | base/prefs/pref_store_observer_mock.h » ('j') | base/prefs/testing_pref_store.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/prefs/overlay_user_pref_store.h" 5 #include "base/prefs/overlay_user_pref_store.h"
6 6
7 #include "base/prefs/pref_store_observer_mock.h" 7 #include "base/prefs/pref_store_observer_mock.h"
8 #include "base/prefs/testing_pref_store.h" 8 #include "base/prefs/testing_pref_store.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 using ::testing::Mock; 13 using ::testing::Mock;
14 using ::testing::StrEq; 14 using ::testing::StrEq;
15 15
16 namespace base { 16 namespace base {
17 namespace { 17 namespace {
18 18
19 const char kBrowserWindowPlacement[] = "browser.window_placement"; 19 const char kBrowserWindowPlacement[] = "browser.window_placement";
20 const char kShowBookmarkBar[] = "bookmark_bar.show_on_all_tabs"; 20 const char kShowBookmarkBar[] = "bookmark_bar.show_on_all_tabs";
21 21
22 const char* overlay_key = kBrowserWindowPlacement; 22 const char* overlay_key = kBrowserWindowPlacement;
23 const char* regular_key = kShowBookmarkBar; 23 const char* regular_key = kShowBookmarkBar;
24 // With the removal of the kWebKitGlobalXXX prefs, we'll no longer have real 24 // With the removal of the kWebKitGlobalXXX prefs, we'll no longer have real
25 // prefs using the overlay pref store, so make up keys here. 25 // prefs using the overlay pref store, so make up keys here.
26 const char* mapped_overlay_key = "test.per_tab.javascript_enabled"; 26 const char* mapped_overlay_key = "test.per_tab.javascript_enabled";
27 const char* mapped_underlay_key = "test.per_profile.javascript_enabled"; 27 const char* mapped_underlay_key = "test.per_profile.javascript_enabled";
28 28
29 void ExpectAndPopOneValue(const std::string& value,
Mattias Nissler (ping if slow) 2014/03/27 20:28:05 It'd probably make sense to turn this into: void
30 std::vector<std::string>* collection) {
31 EXPECT_EQ(1u, collection->size());
32 if (collection->size() >= 1) {
33 EXPECT_EQ(value, collection->front());
34 collection->erase(collection->begin());
35 }
36 }
37
29 } // namespace 38 } // namespace
30 39
31 class OverlayUserPrefStoreTest : public testing::Test { 40 class OverlayUserPrefStoreTest : public testing::Test {
32 protected: 41 protected:
33 OverlayUserPrefStoreTest() 42 OverlayUserPrefStoreTest()
34 : underlay_(new TestingPrefStore()), 43 : underlay_(new TestingPrefStore()),
35 overlay_(new OverlayUserPrefStore(underlay_.get())) { 44 overlay_(new OverlayUserPrefStore(underlay_.get())) {
36 overlay_->RegisterOverlayPref(overlay_key); 45 overlay_->RegisterOverlayPref(overlay_key);
37 overlay_->RegisterOverlayPref(mapped_overlay_key, mapped_underlay_key); 46 overlay_->RegisterOverlayPref(mapped_overlay_key, mapped_underlay_key);
38 } 47 }
39 48
40 virtual ~OverlayUserPrefStoreTest() {} 49 virtual ~OverlayUserPrefStoreTest() {}
41 50
42 scoped_refptr<TestingPrefStore> underlay_; 51 scoped_refptr<TestingPrefStore> underlay_;
43 scoped_refptr<OverlayUserPrefStore> overlay_; 52 scoped_refptr<OverlayUserPrefStore> overlay_;
44 }; 53 };
45 54
46 TEST_F(OverlayUserPrefStoreTest, Observer) { 55 TEST_F(OverlayUserPrefStoreTest, Observer) {
47 PrefStoreObserverMock obs; 56 PrefStoreObserverMock obs;
48 overlay_->AddObserver(&obs); 57 overlay_->AddObserver(&obs);
49 58
50 // Check that underlay first value is reported. 59 // Check that underlay first value is reported.
51 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(1);
52 underlay_->SetValue(overlay_key, new FundamentalValue(42)); 60 underlay_->SetValue(overlay_key, new FundamentalValue(42));
53 Mock::VerifyAndClearExpectations(&obs); 61 ExpectAndPopOneValue(overlay_key, &obs.changed_keys);
54 62
55 // Check that underlay overwriting is reported. 63 // Check that underlay overwriting is reported.
56 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(1);
57 underlay_->SetValue(overlay_key, new FundamentalValue(43)); 64 underlay_->SetValue(overlay_key, new FundamentalValue(43));
58 Mock::VerifyAndClearExpectations(&obs); 65 ExpectAndPopOneValue(overlay_key, &obs.changed_keys);
59 66
60 // Check that overwriting change in overlay is reported. 67 // Check that overwriting change in overlay is reported.
61 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(1);
62 overlay_->SetValue(overlay_key, new FundamentalValue(44)); 68 overlay_->SetValue(overlay_key, new FundamentalValue(44));
63 Mock::VerifyAndClearExpectations(&obs); 69 ExpectAndPopOneValue(overlay_key, &obs.changed_keys);
64 70
65 // Check that hidden underlay change is not reported. 71 // Check that hidden underlay change is not reported.
66 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(0);
67 underlay_->SetValue(overlay_key, new FundamentalValue(45)); 72 underlay_->SetValue(overlay_key, new FundamentalValue(45));
68 Mock::VerifyAndClearExpectations(&obs); 73 EXPECT_TRUE(obs.changed_keys.empty());
69 74
70 // Check that overlay remove is reported. 75 // Check that overlay remove is reported.
71 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(1);
72 overlay_->RemoveValue(overlay_key); 76 overlay_->RemoveValue(overlay_key);
73 Mock::VerifyAndClearExpectations(&obs); 77 ExpectAndPopOneValue(overlay_key, &obs.changed_keys);
74 78
75 // Check that underlay remove is reported. 79 // Check that underlay remove is reported.
76 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(1);
77 underlay_->RemoveValue(overlay_key); 80 underlay_->RemoveValue(overlay_key);
78 Mock::VerifyAndClearExpectations(&obs); 81 ExpectAndPopOneValue(overlay_key, &obs.changed_keys);
79 82
80 // Check respecting of silence. 83 // Check respecting of silence.
81 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(0);
82 overlay_->SetValueSilently(overlay_key, new FundamentalValue(46)); 84 overlay_->SetValueSilently(overlay_key, new FundamentalValue(46));
83 Mock::VerifyAndClearExpectations(&obs); 85 EXPECT_TRUE(obs.changed_keys.empty());
84 86
85 overlay_->RemoveObserver(&obs); 87 overlay_->RemoveObserver(&obs);
86 88
87 // Check successful unsubscription. 89 // Check successful unsubscription.
88 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(overlay_key))).Times(0);
89 underlay_->SetValue(overlay_key, new FundamentalValue(47)); 90 underlay_->SetValue(overlay_key, new FundamentalValue(47));
90 overlay_->SetValue(overlay_key, new FundamentalValue(48)); 91 overlay_->SetValue(overlay_key, new FundamentalValue(48));
91 Mock::VerifyAndClearExpectations(&obs); 92 EXPECT_TRUE(obs.changed_keys.empty());
92 } 93 }
93 94
94 TEST_F(OverlayUserPrefStoreTest, GetAndSet) { 95 TEST_F(OverlayUserPrefStoreTest, GetAndSet) {
95 const Value* value = NULL; 96 const Value* value = NULL;
96 EXPECT_FALSE(overlay_->GetValue(overlay_key, &value)); 97 EXPECT_FALSE(overlay_->GetValue(overlay_key, &value));
97 EXPECT_FALSE(underlay_->GetValue(overlay_key, &value)); 98 EXPECT_FALSE(underlay_->GetValue(overlay_key, &value));
98 99
99 underlay_->SetValue(overlay_key, new FundamentalValue(42)); 100 underlay_->SetValue(overlay_key, new FundamentalValue(42));
100 101
101 // Value shines through: 102 // Value shines through:
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 } 148 }
148 149
149 // Here we consider a global preference that is not overlayed. 150 // Here we consider a global preference that is not overlayed.
150 TEST_F(OverlayUserPrefStoreTest, GlobalPref) { 151 TEST_F(OverlayUserPrefStoreTest, GlobalPref) {
151 PrefStoreObserverMock obs; 152 PrefStoreObserverMock obs;
152 overlay_->AddObserver(&obs); 153 overlay_->AddObserver(&obs);
153 154
154 const Value* value = NULL; 155 const Value* value = NULL;
155 156
156 // Check that underlay first value is reported. 157 // Check that underlay first value is reported.
157 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(1);
158 underlay_->SetValue(regular_key, new FundamentalValue(42)); 158 underlay_->SetValue(regular_key, new FundamentalValue(42));
159 Mock::VerifyAndClearExpectations(&obs); 159 ExpectAndPopOneValue(regular_key, &obs.changed_keys);
160 160
161 // Check that underlay overwriting is reported. 161 // Check that underlay overwriting is reported.
162 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(1);
163 underlay_->SetValue(regular_key, new FundamentalValue(43)); 162 underlay_->SetValue(regular_key, new FundamentalValue(43));
164 Mock::VerifyAndClearExpectations(&obs); 163 ExpectAndPopOneValue(regular_key, &obs.changed_keys);
165 164
166 // Check that we get this value from the overlay 165 // Check that we get this value from the overlay
167 EXPECT_TRUE(overlay_->GetValue(regular_key, &value)); 166 EXPECT_TRUE(overlay_->GetValue(regular_key, &value));
168 EXPECT_TRUE(base::FundamentalValue(43).Equals(value)); 167 EXPECT_TRUE(base::FundamentalValue(43).Equals(value));
169 168
170 // Check that overwriting change in overlay is reported. 169 // Check that overwriting change in overlay is reported.
171 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(1);
172 overlay_->SetValue(regular_key, new FundamentalValue(44)); 170 overlay_->SetValue(regular_key, new FundamentalValue(44));
173 Mock::VerifyAndClearExpectations(&obs); 171 ExpectAndPopOneValue(regular_key, &obs.changed_keys);
174 172
175 // Check that we get this value from the overlay and the underlay. 173 // Check that we get this value from the overlay and the underlay.
176 EXPECT_TRUE(overlay_->GetValue(regular_key, &value)); 174 EXPECT_TRUE(overlay_->GetValue(regular_key, &value));
177 EXPECT_TRUE(base::FundamentalValue(44).Equals(value)); 175 EXPECT_TRUE(base::FundamentalValue(44).Equals(value));
178 EXPECT_TRUE(underlay_->GetValue(regular_key, &value)); 176 EXPECT_TRUE(underlay_->GetValue(regular_key, &value));
179 EXPECT_TRUE(base::FundamentalValue(44).Equals(value)); 177 EXPECT_TRUE(base::FundamentalValue(44).Equals(value));
180 178
181 // Check that overlay remove is reported. 179 // Check that overlay remove is reported.
182 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(1);
183 overlay_->RemoveValue(regular_key); 180 overlay_->RemoveValue(regular_key);
184 Mock::VerifyAndClearExpectations(&obs); 181 ExpectAndPopOneValue(regular_key, &obs.changed_keys);
185 182
186 // Check that value was removed from overlay and underlay 183 // Check that value was removed from overlay and underlay
187 EXPECT_FALSE(overlay_->GetValue(regular_key, &value)); 184 EXPECT_FALSE(overlay_->GetValue(regular_key, &value));
188 EXPECT_FALSE(underlay_->GetValue(regular_key, &value)); 185 EXPECT_FALSE(underlay_->GetValue(regular_key, &value));
189 186
190 // Check respecting of silence. 187 // Check respecting of silence.
191 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(0);
192 overlay_->SetValueSilently(regular_key, new FundamentalValue(46)); 188 overlay_->SetValueSilently(regular_key, new FundamentalValue(46));
193 Mock::VerifyAndClearExpectations(&obs); 189 EXPECT_TRUE(obs.changed_keys.empty());
194 190
195 overlay_->RemoveObserver(&obs); 191 overlay_->RemoveObserver(&obs);
196 192
197 // Check successful unsubscription. 193 // Check successful unsubscription.
198 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(regular_key))).Times(0);
199 underlay_->SetValue(regular_key, new FundamentalValue(47)); 194 underlay_->SetValue(regular_key, new FundamentalValue(47));
200 overlay_->SetValue(regular_key, new FundamentalValue(48)); 195 overlay_->SetValue(regular_key, new FundamentalValue(48));
201 Mock::VerifyAndClearExpectations(&obs); 196 EXPECT_TRUE(obs.changed_keys.empty());
202 } 197 }
203 198
204 // Check that names mapping works correctly. 199 // Check that names mapping works correctly.
205 TEST_F(OverlayUserPrefStoreTest, NamesMapping) { 200 TEST_F(OverlayUserPrefStoreTest, NamesMapping) {
206 PrefStoreObserverMock obs; 201 PrefStoreObserverMock obs;
207 overlay_->AddObserver(&obs); 202 overlay_->AddObserver(&obs);
208 203
209 const Value* value = NULL; 204 const Value* value = NULL;
210 205
211 // Check that if there is no override in the overlay, changing underlay value 206 // Check that if there is no override in the overlay, changing underlay value
212 // is reported as changing an overlay value. 207 // is reported as changing an overlay value.
213 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(1);
214 underlay_->SetValue(mapped_underlay_key, new FundamentalValue(42)); 208 underlay_->SetValue(mapped_underlay_key, new FundamentalValue(42));
215 Mock::VerifyAndClearExpectations(&obs); 209 ExpectAndPopOneValue(mapped_overlay_key, &obs.changed_keys);
216 210
217 // Check that underlay overwriting is reported. 211 // Check that underlay overwriting is reported.
218 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(1);
219 underlay_->SetValue(mapped_underlay_key, new FundamentalValue(43)); 212 underlay_->SetValue(mapped_underlay_key, new FundamentalValue(43));
220 Mock::VerifyAndClearExpectations(&obs); 213 ExpectAndPopOneValue(mapped_overlay_key, &obs.changed_keys);
221 214
222 // Check that we get this value from the overlay with both keys 215 // Check that we get this value from the overlay with both keys
223 EXPECT_TRUE(overlay_->GetValue(mapped_overlay_key, &value)); 216 EXPECT_TRUE(overlay_->GetValue(mapped_overlay_key, &value));
224 EXPECT_TRUE(base::FundamentalValue(43).Equals(value)); 217 EXPECT_TRUE(base::FundamentalValue(43).Equals(value));
225 // In this case, overlay reads directly from the underlay. 218 // In this case, overlay reads directly from the underlay.
226 EXPECT_TRUE(overlay_->GetValue(mapped_underlay_key, &value)); 219 EXPECT_TRUE(overlay_->GetValue(mapped_underlay_key, &value));
227 EXPECT_TRUE(base::FundamentalValue(43).Equals(value)); 220 EXPECT_TRUE(base::FundamentalValue(43).Equals(value));
228 221
229 // Check that overwriting change in overlay is reported. 222 // Check that overwriting change in overlay is reported.
230 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(1);
231 overlay_->SetValue(mapped_overlay_key, new FundamentalValue(44)); 223 overlay_->SetValue(mapped_overlay_key, new FundamentalValue(44));
232 Mock::VerifyAndClearExpectations(&obs); 224 ExpectAndPopOneValue(mapped_overlay_key, &obs.changed_keys);
233 225
234 // Check that we get an overriden value from overlay, while reading the 226 // Check that we get an overriden value from overlay, while reading the
235 // value from underlay still holds an old value. 227 // value from underlay still holds an old value.
236 EXPECT_TRUE(overlay_->GetValue(mapped_overlay_key, &value)); 228 EXPECT_TRUE(overlay_->GetValue(mapped_overlay_key, &value));
237 EXPECT_TRUE(base::FundamentalValue(44).Equals(value)); 229 EXPECT_TRUE(base::FundamentalValue(44).Equals(value));
238 EXPECT_TRUE(overlay_->GetValue(mapped_underlay_key, &value)); 230 EXPECT_TRUE(overlay_->GetValue(mapped_underlay_key, &value));
239 EXPECT_TRUE(base::FundamentalValue(43).Equals(value)); 231 EXPECT_TRUE(base::FundamentalValue(43).Equals(value));
240 EXPECT_TRUE(underlay_->GetValue(mapped_underlay_key, &value)); 232 EXPECT_TRUE(underlay_->GetValue(mapped_underlay_key, &value));
241 EXPECT_TRUE(base::FundamentalValue(43).Equals(value)); 233 EXPECT_TRUE(base::FundamentalValue(43).Equals(value));
242 234
243 // Check that hidden underlay change is not reported. 235 // Check that hidden underlay change is not reported.
244 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(0);
245 underlay_->SetValue(mapped_underlay_key, new FundamentalValue(45)); 236 underlay_->SetValue(mapped_underlay_key, new FundamentalValue(45));
246 Mock::VerifyAndClearExpectations(&obs); 237 EXPECT_TRUE(obs.changed_keys.empty());
247 238
248 // Check that overlay remove is reported. 239 // Check that overlay remove is reported.
249 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(1);
250 overlay_->RemoveValue(mapped_overlay_key); 240 overlay_->RemoveValue(mapped_overlay_key);
251 Mock::VerifyAndClearExpectations(&obs); 241 ExpectAndPopOneValue(mapped_overlay_key, &obs.changed_keys);
252 242
253 // Check that underlay remove is reported. 243 // Check that underlay remove is reported.
254 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(1);
255 underlay_->RemoveValue(mapped_underlay_key); 244 underlay_->RemoveValue(mapped_underlay_key);
256 Mock::VerifyAndClearExpectations(&obs); 245 ExpectAndPopOneValue(mapped_overlay_key, &obs.changed_keys);
257 246
258 // Check that value was removed. 247 // Check that value was removed.
259 EXPECT_FALSE(overlay_->GetValue(mapped_overlay_key, &value)); 248 EXPECT_FALSE(overlay_->GetValue(mapped_overlay_key, &value));
260 EXPECT_FALSE(overlay_->GetValue(mapped_underlay_key, &value)); 249 EXPECT_FALSE(overlay_->GetValue(mapped_underlay_key, &value));
261 250
262 // Check respecting of silence. 251 // Check respecting of silence.
263 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(0);
264 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_underlay_key))).Times(0);
265 overlay_->SetValueSilently(mapped_overlay_key, new FundamentalValue(46)); 252 overlay_->SetValueSilently(mapped_overlay_key, new FundamentalValue(46));
266 Mock::VerifyAndClearExpectations(&obs); 253 EXPECT_TRUE(obs.changed_keys.empty());
267 254
268 overlay_->RemoveObserver(&obs); 255 overlay_->RemoveObserver(&obs);
269 256
270 // Check successful unsubscription. 257 // Check successful unsubscription.
271 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_overlay_key))).Times(0);
272 EXPECT_CALL(obs, OnPrefValueChanged(StrEq(mapped_underlay_key))).Times(0);
273 underlay_->SetValue(mapped_underlay_key, new FundamentalValue(47)); 258 underlay_->SetValue(mapped_underlay_key, new FundamentalValue(47));
274 overlay_->SetValue(mapped_overlay_key, new FundamentalValue(48)); 259 overlay_->SetValue(mapped_overlay_key, new FundamentalValue(48));
275 Mock::VerifyAndClearExpectations(&obs); 260 EXPECT_TRUE(obs.changed_keys.empty());
276 } 261 }
277 262
278 } // namespace base 263 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/prefs/pref_store_observer_mock.h » ('j') | base/prefs/testing_pref_store.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698