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
, 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
, 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
, 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
, 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
, new FundamentalValue(46),
82 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
83 EXPECT_TRUE(obs
.changed_keys
.empty());
85 overlay_
->RemoveObserver(&obs
);
87 // Check successful unsubscription.
88 underlay_
->SetValue(overlay_key
, new FundamentalValue(47),
89 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
90 overlay_
->SetValue(overlay_key
, new FundamentalValue(48),
91 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
92 EXPECT_TRUE(obs
.changed_keys
.empty());
95 TEST_F(OverlayUserPrefStoreTest
, GetAndSet
) {
96 const Value
* value
= NULL
;
97 EXPECT_FALSE(overlay_
->GetValue(overlay_key
, &value
));
98 EXPECT_FALSE(underlay_
->GetValue(overlay_key
, &value
));
100 underlay_
->SetValue(overlay_key
, new FundamentalValue(42),
101 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
103 // Value shines through:
104 EXPECT_TRUE(overlay_
->GetValue(overlay_key
, &value
));
105 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
107 EXPECT_TRUE(underlay_
->GetValue(overlay_key
, &value
));
108 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
110 overlay_
->SetValue(overlay_key
, new FundamentalValue(43),
111 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
113 EXPECT_TRUE(overlay_
->GetValue(overlay_key
, &value
));
114 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
116 EXPECT_TRUE(underlay_
->GetValue(overlay_key
, &value
));
117 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
119 overlay_
->RemoveValue(overlay_key
,
120 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
122 // Value shines through:
123 EXPECT_TRUE(overlay_
->GetValue(overlay_key
, &value
));
124 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
126 EXPECT_TRUE(underlay_
->GetValue(overlay_key
, &value
));
127 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
130 // Check that GetMutableValue does not return the dictionary of the underlay.
131 TEST_F(OverlayUserPrefStoreTest
, ModifyDictionaries
) {
132 underlay_
->SetValue(overlay_key
, new DictionaryValue
,
133 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
135 Value
* modify
= NULL
;
136 EXPECT_TRUE(overlay_
->GetMutableValue(overlay_key
, &modify
));
138 ASSERT_TRUE(modify
->IsType(Value::TYPE_DICTIONARY
));
139 static_cast<DictionaryValue
*>(modify
)->SetInteger(overlay_key
, 42);
141 Value
* original_in_underlay
= NULL
;
142 EXPECT_TRUE(underlay_
->GetMutableValue(overlay_key
, &original_in_underlay
));
143 ASSERT_TRUE(original_in_underlay
);
144 ASSERT_TRUE(original_in_underlay
->IsType(Value::TYPE_DICTIONARY
));
145 EXPECT_TRUE(static_cast<DictionaryValue
*>(original_in_underlay
)->empty());
147 Value
* modified
= NULL
;
148 EXPECT_TRUE(overlay_
->GetMutableValue(overlay_key
, &modified
));
149 ASSERT_TRUE(modified
);
150 ASSERT_TRUE(modified
->IsType(Value::TYPE_DICTIONARY
));
151 EXPECT_TRUE(Value::Equals(modify
, static_cast<DictionaryValue
*>(modified
)));
154 // Here we consider a global preference that is not overlayed.
155 TEST_F(OverlayUserPrefStoreTest
, GlobalPref
) {
156 PrefStoreObserverMock obs
;
157 overlay_
->AddObserver(&obs
);
159 const Value
* value
= NULL
;
161 // Check that underlay first value is reported.
162 underlay_
->SetValue(regular_key
, new FundamentalValue(42),
163 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
164 obs
.VerifyAndResetChangedKey(regular_key
);
166 // Check that underlay overwriting is reported.
167 underlay_
->SetValue(regular_key
, new FundamentalValue(43),
168 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
169 obs
.VerifyAndResetChangedKey(regular_key
);
171 // Check that we get this value from the overlay
172 EXPECT_TRUE(overlay_
->GetValue(regular_key
, &value
));
173 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
175 // Check that overwriting change in overlay is reported.
176 overlay_
->SetValue(regular_key
, new FundamentalValue(44),
177 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
178 obs
.VerifyAndResetChangedKey(regular_key
);
180 // Check that we get this value from the overlay and the underlay.
181 EXPECT_TRUE(overlay_
->GetValue(regular_key
, &value
));
182 EXPECT_TRUE(base::FundamentalValue(44).Equals(value
));
183 EXPECT_TRUE(underlay_
->GetValue(regular_key
, &value
));
184 EXPECT_TRUE(base::FundamentalValue(44).Equals(value
));
186 // Check that overlay remove is reported.
187 overlay_
->RemoveValue(regular_key
,
188 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
189 obs
.VerifyAndResetChangedKey(regular_key
);
191 // Check that value was removed from overlay and underlay
192 EXPECT_FALSE(overlay_
->GetValue(regular_key
, &value
));
193 EXPECT_FALSE(underlay_
->GetValue(regular_key
, &value
));
195 // Check respecting of silence.
196 overlay_
->SetValueSilently(regular_key
, new FundamentalValue(46),
197 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
198 EXPECT_TRUE(obs
.changed_keys
.empty());
200 overlay_
->RemoveObserver(&obs
);
202 // Check successful unsubscription.
203 underlay_
->SetValue(regular_key
, new FundamentalValue(47),
204 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
205 overlay_
->SetValue(regular_key
, new FundamentalValue(48),
206 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
207 EXPECT_TRUE(obs
.changed_keys
.empty());
210 // Check that names mapping works correctly.
211 TEST_F(OverlayUserPrefStoreTest
, NamesMapping
) {
212 PrefStoreObserverMock obs
;
213 overlay_
->AddObserver(&obs
);
215 const Value
* value
= NULL
;
217 // Check that if there is no override in the overlay, changing underlay value
218 // is reported as changing an overlay value.
219 underlay_
->SetValue(mapped_underlay_key
, new FundamentalValue(42),
220 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
221 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
223 // Check that underlay overwriting is reported.
224 underlay_
->SetValue(mapped_underlay_key
, new FundamentalValue(43),
225 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
226 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
228 // Check that we get this value from the overlay with both keys
229 EXPECT_TRUE(overlay_
->GetValue(mapped_overlay_key
, &value
));
230 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
231 // In this case, overlay reads directly from the underlay.
232 EXPECT_TRUE(overlay_
->GetValue(mapped_underlay_key
, &value
));
233 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
235 // Check that overwriting change in overlay is reported.
236 overlay_
->SetValue(mapped_overlay_key
, new FundamentalValue(44),
237 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
238 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
240 // Check that we get an overriden value from overlay, while reading the
241 // value from underlay still holds an old value.
242 EXPECT_TRUE(overlay_
->GetValue(mapped_overlay_key
, &value
));
243 EXPECT_TRUE(base::FundamentalValue(44).Equals(value
));
244 EXPECT_TRUE(overlay_
->GetValue(mapped_underlay_key
, &value
));
245 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
246 EXPECT_TRUE(underlay_
->GetValue(mapped_underlay_key
, &value
));
247 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
249 // Check that hidden underlay change is not reported.
250 underlay_
->SetValue(mapped_underlay_key
, new FundamentalValue(45),
251 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
252 EXPECT_TRUE(obs
.changed_keys
.empty());
254 // Check that overlay remove is reported.
255 overlay_
->RemoveValue(mapped_overlay_key
,
256 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
257 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
259 // Check that underlay remove is reported.
260 underlay_
->RemoveValue(mapped_underlay_key
,
261 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
262 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
264 // Check that value was removed.
265 EXPECT_FALSE(overlay_
->GetValue(mapped_overlay_key
, &value
));
266 EXPECT_FALSE(overlay_
->GetValue(mapped_underlay_key
, &value
));
268 // Check respecting of silence.
269 overlay_
->SetValueSilently(mapped_overlay_key
, new FundamentalValue(46),
270 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
271 EXPECT_TRUE(obs
.changed_keys
.empty());
273 overlay_
->RemoveObserver(&obs
);
275 // Check successful unsubscription.
276 underlay_
->SetValue(mapped_underlay_key
, new FundamentalValue(47),
277 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
278 overlay_
->SetValue(mapped_overlay_key
, new FundamentalValue(48),
279 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS
);
280 EXPECT_TRUE(obs
.changed_keys
.empty());