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
3 // found in the LICENSE file.
5 #include "base/prefs/overlay_user_pref_store.h"
7 #include "base/prefs/pref_store_observer_mock.h"
8 #include "base/prefs/testing_pref_store.h"
9 #include "base/values.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using ::testing::Mock
;
14 using ::testing::StrEq
;
19 const char kBrowserWindowPlacement
[] = "browser.window_placement";
20 const char kShowBookmarkBar
[] = "bookmark_bar.show_on_all_tabs";
22 const char* const overlay_key
= kBrowserWindowPlacement
;
23 const char* const regular_key
= kShowBookmarkBar
;
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.
26 const char mapped_overlay_key
[] = "test.per_tab.javascript_enabled";
27 const char mapped_underlay_key
[] = "test.per_profile.javascript_enabled";
31 class OverlayUserPrefStoreTest
: public testing::Test
{
33 OverlayUserPrefStoreTest()
34 : underlay_(new TestingPrefStore()),
35 overlay_(new OverlayUserPrefStore(underlay_
.get())) {
36 overlay_
->RegisterOverlayPref(overlay_key
);
37 overlay_
->RegisterOverlayPref(mapped_overlay_key
, mapped_underlay_key
);
40 ~OverlayUserPrefStoreTest() override
{}
42 scoped_refptr
<TestingPrefStore
> underlay_
;
43 scoped_refptr
<OverlayUserPrefStore
> overlay_
;
46 TEST_F(OverlayUserPrefStoreTest
, Observer
) {
47 PrefStoreObserverMock obs
;
48 overlay_
->AddObserver(&obs
);
50 // Check that underlay first value is reported.
51 underlay_
->SetValue(overlay_key
, make_scoped_ptr(new FundamentalValue(42)),
52 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
53 obs
.VerifyAndResetChangedKey(overlay_key
);
55 // Check that underlay overwriting is reported.
56 underlay_
->SetValue(overlay_key
, make_scoped_ptr(new FundamentalValue(43)),
57 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
58 obs
.VerifyAndResetChangedKey(overlay_key
);
60 // Check that overwriting change in overlay is reported.
61 overlay_
->SetValue(overlay_key
, make_scoped_ptr(new FundamentalValue(44)),
62 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
63 obs
.VerifyAndResetChangedKey(overlay_key
);
65 // Check that hidden underlay change is not reported.
66 underlay_
->SetValue(overlay_key
, make_scoped_ptr(new FundamentalValue(45)),
67 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
68 EXPECT_TRUE(obs
.changed_keys
.empty());
70 // Check that overlay remove is reported.
71 overlay_
->RemoveValue(overlay_key
,
72 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
73 obs
.VerifyAndResetChangedKey(overlay_key
);
75 // Check that underlay remove is reported.
76 underlay_
->RemoveValue(overlay_key
,
77 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
78 obs
.VerifyAndResetChangedKey(overlay_key
);
80 // Check respecting of silence.
81 overlay_
->SetValueSilently(overlay_key
,
82 make_scoped_ptr(new FundamentalValue(46)),
83 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
84 EXPECT_TRUE(obs
.changed_keys
.empty());
86 overlay_
->RemoveObserver(&obs
);
88 // Check successful unsubscription.
89 underlay_
->SetValue(overlay_key
, make_scoped_ptr(new FundamentalValue(47)),
90 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
91 overlay_
->SetValue(overlay_key
, make_scoped_ptr(new FundamentalValue(48)),
92 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
93 EXPECT_TRUE(obs
.changed_keys
.empty());
96 TEST_F(OverlayUserPrefStoreTest
, GetAndSet
) {
97 const Value
* value
= NULL
;
98 EXPECT_FALSE(overlay_
->GetValue(overlay_key
, &value
));
99 EXPECT_FALSE(underlay_
->GetValue(overlay_key
, &value
));
101 underlay_
->SetValue(overlay_key
, make_scoped_ptr(new FundamentalValue(42)),
102 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
104 // Value shines through:
105 EXPECT_TRUE(overlay_
->GetValue(overlay_key
, &value
));
106 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
108 EXPECT_TRUE(underlay_
->GetValue(overlay_key
, &value
));
109 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
111 overlay_
->SetValue(overlay_key
, make_scoped_ptr(new FundamentalValue(43)),
112 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
114 EXPECT_TRUE(overlay_
->GetValue(overlay_key
, &value
));
115 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
117 EXPECT_TRUE(underlay_
->GetValue(overlay_key
, &value
));
118 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
120 overlay_
->RemoveValue(overlay_key
,
121 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
123 // Value shines through:
124 EXPECT_TRUE(overlay_
->GetValue(overlay_key
, &value
));
125 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
127 EXPECT_TRUE(underlay_
->GetValue(overlay_key
, &value
));
128 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
131 // Check that GetMutableValue does not return the dictionary of the underlay.
132 TEST_F(OverlayUserPrefStoreTest
, ModifyDictionaries
) {
133 underlay_
->SetValue(overlay_key
, make_scoped_ptr(new DictionaryValue
),
134 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
136 Value
* modify
= NULL
;
137 EXPECT_TRUE(overlay_
->GetMutableValue(overlay_key
, &modify
));
139 ASSERT_TRUE(modify
->IsType(Value::TYPE_DICTIONARY
));
140 static_cast<DictionaryValue
*>(modify
)->SetInteger(overlay_key
, 42);
142 Value
* original_in_underlay
= NULL
;
143 EXPECT_TRUE(underlay_
->GetMutableValue(overlay_key
, &original_in_underlay
));
144 ASSERT_TRUE(original_in_underlay
);
145 ASSERT_TRUE(original_in_underlay
->IsType(Value::TYPE_DICTIONARY
));
146 EXPECT_TRUE(static_cast<DictionaryValue
*>(original_in_underlay
)->empty());
148 Value
* modified
= NULL
;
149 EXPECT_TRUE(overlay_
->GetMutableValue(overlay_key
, &modified
));
150 ASSERT_TRUE(modified
);
151 ASSERT_TRUE(modified
->IsType(Value::TYPE_DICTIONARY
));
152 EXPECT_TRUE(Value::Equals(modify
, static_cast<DictionaryValue
*>(modified
)));
155 // Here we consider a global preference that is not overlayed.
156 TEST_F(OverlayUserPrefStoreTest
, GlobalPref
) {
157 PrefStoreObserverMock obs
;
158 overlay_
->AddObserver(&obs
);
160 const Value
* value
= NULL
;
162 // Check that underlay first value is reported.
163 underlay_
->SetValue(regular_key
, make_scoped_ptr(new FundamentalValue(42)),
164 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
165 obs
.VerifyAndResetChangedKey(regular_key
);
167 // Check that underlay overwriting is reported.
168 underlay_
->SetValue(regular_key
, make_scoped_ptr(new FundamentalValue(43)),
169 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
170 obs
.VerifyAndResetChangedKey(regular_key
);
172 // Check that we get this value from the overlay
173 EXPECT_TRUE(overlay_
->GetValue(regular_key
, &value
));
174 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
176 // Check that overwriting change in overlay is reported.
177 overlay_
->SetValue(regular_key
, make_scoped_ptr(new FundamentalValue(44)),
178 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
179 obs
.VerifyAndResetChangedKey(regular_key
);
181 // Check that we get this value from the overlay and the underlay.
182 EXPECT_TRUE(overlay_
->GetValue(regular_key
, &value
));
183 EXPECT_TRUE(base::FundamentalValue(44).Equals(value
));
184 EXPECT_TRUE(underlay_
->GetValue(regular_key
, &value
));
185 EXPECT_TRUE(base::FundamentalValue(44).Equals(value
));
187 // Check that overlay remove is reported.
188 overlay_
->RemoveValue(regular_key
,
189 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
190 obs
.VerifyAndResetChangedKey(regular_key
);
192 // Check that value was removed from overlay and underlay
193 EXPECT_FALSE(overlay_
->GetValue(regular_key
, &value
));
194 EXPECT_FALSE(underlay_
->GetValue(regular_key
, &value
));
196 // Check respecting of silence.
197 overlay_
->SetValueSilently(regular_key
,
198 make_scoped_ptr(new FundamentalValue(46)),
199 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
200 EXPECT_TRUE(obs
.changed_keys
.empty());
202 overlay_
->RemoveObserver(&obs
);
204 // Check successful unsubscription.
205 underlay_
->SetValue(regular_key
, make_scoped_ptr(new FundamentalValue(47)),
206 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
207 overlay_
->SetValue(regular_key
, make_scoped_ptr(new FundamentalValue(48)),
208 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
209 EXPECT_TRUE(obs
.changed_keys
.empty());
212 // Check that names mapping works correctly.
213 TEST_F(OverlayUserPrefStoreTest
, NamesMapping
) {
214 PrefStoreObserverMock obs
;
215 overlay_
->AddObserver(&obs
);
217 const Value
* value
= NULL
;
219 // Check that if there is no override in the overlay, changing underlay value
220 // is reported as changing an overlay value.
221 underlay_
->SetValue(mapped_underlay_key
,
222 make_scoped_ptr(new FundamentalValue(42)),
223 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
224 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
226 // Check that underlay overwriting is reported.
227 underlay_
->SetValue(mapped_underlay_key
,
228 make_scoped_ptr(new FundamentalValue(43)),
229 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
230 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
232 // Check that we get this value from the overlay with both keys
233 EXPECT_TRUE(overlay_
->GetValue(mapped_overlay_key
, &value
));
234 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
235 // In this case, overlay reads directly from the underlay.
236 EXPECT_TRUE(overlay_
->GetValue(mapped_underlay_key
, &value
));
237 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
239 // Check that overwriting change in overlay is reported.
240 overlay_
->SetValue(mapped_overlay_key
,
241 make_scoped_ptr(new FundamentalValue(44)),
242 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
243 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
245 // Check that we get an overriden value from overlay, while reading the
246 // value from underlay still holds an old value.
247 EXPECT_TRUE(overlay_
->GetValue(mapped_overlay_key
, &value
));
248 EXPECT_TRUE(base::FundamentalValue(44).Equals(value
));
249 EXPECT_TRUE(overlay_
->GetValue(mapped_underlay_key
, &value
));
250 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
251 EXPECT_TRUE(underlay_
->GetValue(mapped_underlay_key
, &value
));
252 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
254 // Check that hidden underlay change is not reported.
255 underlay_
->SetValue(mapped_underlay_key
,
256 make_scoped_ptr(new FundamentalValue(45)),
257 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
258 EXPECT_TRUE(obs
.changed_keys
.empty());
260 // Check that overlay remove is reported.
261 overlay_
->RemoveValue(mapped_overlay_key
,
262 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
263 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
265 // Check that underlay remove is reported.
266 underlay_
->RemoveValue(mapped_underlay_key
,
267 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
268 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
270 // Check that value was removed.
271 EXPECT_FALSE(overlay_
->GetValue(mapped_overlay_key
, &value
));
272 EXPECT_FALSE(overlay_
->GetValue(mapped_underlay_key
, &value
));
274 // Check respecting of silence.
275 overlay_
->SetValueSilently(mapped_overlay_key
,
276 make_scoped_ptr(new FundamentalValue(46)),
277 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
278 EXPECT_TRUE(obs
.changed_keys
.empty());
280 overlay_
->RemoveObserver(&obs
);
282 // Check successful unsubscription.
283 underlay_
->SetValue(mapped_underlay_key
,
284 make_scoped_ptr(new FundamentalValue(47)),
285 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
286 overlay_
->SetValue(mapped_overlay_key
,
287 make_scoped_ptr(new FundamentalValue(48)),
288 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
289 EXPECT_TRUE(obs
.changed_keys
.empty());