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* overlay_key
= kBrowserWindowPlacement
;
23 const char* 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 virtual ~OverlayUserPrefStoreTest() {}
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 EXPECT_CALL(obs
, OnPrefValueChanged(StrEq(overlay_key
))).Times(1);
52 underlay_
->SetValue(overlay_key
, new FundamentalValue(42));
53 Mock::VerifyAndClearExpectations(&obs
);
55 // Check that underlay overwriting is reported.
56 EXPECT_CALL(obs
, OnPrefValueChanged(StrEq(overlay_key
))).Times(1);
57 underlay_
->SetValue(overlay_key
, new FundamentalValue(43));
58 Mock::VerifyAndClearExpectations(&obs
);
60 // 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));
63 Mock::VerifyAndClearExpectations(&obs
);
65 // 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));
68 Mock::VerifyAndClearExpectations(&obs
);
70 // Check that overlay remove is reported.
71 EXPECT_CALL(obs
, OnPrefValueChanged(StrEq(overlay_key
))).Times(1);
72 overlay_
->RemoveValue(overlay_key
);
73 Mock::VerifyAndClearExpectations(&obs
);
75 // Check that underlay remove is reported.
76 EXPECT_CALL(obs
, OnPrefValueChanged(StrEq(overlay_key
))).Times(1);
77 underlay_
->RemoveValue(overlay_key
);
78 Mock::VerifyAndClearExpectations(&obs
);
80 // Check respecting of silence.
81 EXPECT_CALL(obs
, OnPrefValueChanged(StrEq(overlay_key
))).Times(0);
82 overlay_
->SetValueSilently(overlay_key
, new FundamentalValue(46));
83 Mock::VerifyAndClearExpectations(&obs
);
85 overlay_
->RemoveObserver(&obs
);
87 // Check successful unsubscription.
88 EXPECT_CALL(obs
, OnPrefValueChanged(StrEq(overlay_key
))).Times(0);
89 underlay_
->SetValue(overlay_key
, new FundamentalValue(47));
90 overlay_
->SetValue(overlay_key
, new FundamentalValue(48));
91 Mock::VerifyAndClearExpectations(&obs
);
94 TEST_F(OverlayUserPrefStoreTest
, GetAndSet
) {
95 const Value
* value
= NULL
;
96 EXPECT_FALSE(overlay_
->GetValue(overlay_key
, &value
));
97 EXPECT_FALSE(underlay_
->GetValue(overlay_key
, &value
));
99 underlay_
->SetValue(overlay_key
, new FundamentalValue(42));
101 // Value shines through:
102 EXPECT_TRUE(overlay_
->GetValue(overlay_key
, &value
));
103 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
105 EXPECT_TRUE(underlay_
->GetValue(overlay_key
, &value
));
106 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
108 overlay_
->SetValue(overlay_key
, new FundamentalValue(43));
110 EXPECT_TRUE(overlay_
->GetValue(overlay_key
, &value
));
111 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
113 EXPECT_TRUE(underlay_
->GetValue(overlay_key
, &value
));
114 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
116 overlay_
->RemoveValue(overlay_key
);
118 // Value shines through:
119 EXPECT_TRUE(overlay_
->GetValue(overlay_key
, &value
));
120 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
122 EXPECT_TRUE(underlay_
->GetValue(overlay_key
, &value
));
123 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
126 // Check that GetMutableValue does not return the dictionary of the underlay.
127 TEST_F(OverlayUserPrefStoreTest
, ModifyDictionaries
) {
128 underlay_
->SetValue(overlay_key
, new DictionaryValue
);
130 Value
* modify
= NULL
;
131 EXPECT_TRUE(overlay_
->GetMutableValue(overlay_key
, &modify
));
133 ASSERT_TRUE(modify
->IsType(Value::TYPE_DICTIONARY
));
134 static_cast<DictionaryValue
*>(modify
)->SetInteger(overlay_key
, 42);
136 Value
* original_in_underlay
= NULL
;
137 EXPECT_TRUE(underlay_
->GetMutableValue(overlay_key
, &original_in_underlay
));
138 ASSERT_TRUE(original_in_underlay
);
139 ASSERT_TRUE(original_in_underlay
->IsType(Value::TYPE_DICTIONARY
));
140 EXPECT_TRUE(static_cast<DictionaryValue
*>(original_in_underlay
)->empty());
142 Value
* modified
= NULL
;
143 EXPECT_TRUE(overlay_
->GetMutableValue(overlay_key
, &modified
));
144 ASSERT_TRUE(modified
);
145 ASSERT_TRUE(modified
->IsType(Value::TYPE_DICTIONARY
));
146 EXPECT_TRUE(Value::Equals(modify
, static_cast<DictionaryValue
*>(modified
)));
149 // Here we consider a global preference that is not overlayed.
150 TEST_F(OverlayUserPrefStoreTest
, GlobalPref
) {
151 PrefStoreObserverMock obs
;
152 overlay_
->AddObserver(&obs
);
154 const Value
* value
= NULL
;
156 // 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));
159 Mock::VerifyAndClearExpectations(&obs
);
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));
164 Mock::VerifyAndClearExpectations(&obs
);
166 // Check that we get this value from the overlay
167 EXPECT_TRUE(overlay_
->GetValue(regular_key
, &value
));
168 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
170 // 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));
173 Mock::VerifyAndClearExpectations(&obs
);
175 // Check that we get this value from the overlay and the underlay.
176 EXPECT_TRUE(overlay_
->GetValue(regular_key
, &value
));
177 EXPECT_TRUE(base::FundamentalValue(44).Equals(value
));
178 EXPECT_TRUE(underlay_
->GetValue(regular_key
, &value
));
179 EXPECT_TRUE(base::FundamentalValue(44).Equals(value
));
181 // Check that overlay remove is reported.
182 EXPECT_CALL(obs
, OnPrefValueChanged(StrEq(regular_key
))).Times(1);
183 overlay_
->RemoveValue(regular_key
);
184 Mock::VerifyAndClearExpectations(&obs
);
186 // Check that value was removed from overlay and underlay
187 EXPECT_FALSE(overlay_
->GetValue(regular_key
, &value
));
188 EXPECT_FALSE(underlay_
->GetValue(regular_key
, &value
));
190 // Check respecting of silence.
191 EXPECT_CALL(obs
, OnPrefValueChanged(StrEq(regular_key
))).Times(0);
192 overlay_
->SetValueSilently(regular_key
, new FundamentalValue(46));
193 Mock::VerifyAndClearExpectations(&obs
);
195 overlay_
->RemoveObserver(&obs
);
197 // Check successful unsubscription.
198 EXPECT_CALL(obs
, OnPrefValueChanged(StrEq(regular_key
))).Times(0);
199 underlay_
->SetValue(regular_key
, new FundamentalValue(47));
200 overlay_
->SetValue(regular_key
, new FundamentalValue(48));
201 Mock::VerifyAndClearExpectations(&obs
);
204 // Check that names mapping works correctly.
205 TEST_F(OverlayUserPrefStoreTest
, NamesMapping
) {
206 PrefStoreObserverMock obs
;
207 overlay_
->AddObserver(&obs
);
209 const Value
* value
= NULL
;
211 // Check that if there is no override in the overlay, changing underlay value
212 // 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));
215 Mock::VerifyAndClearExpectations(&obs
);
217 // 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));
220 Mock::VerifyAndClearExpectations(&obs
);
222 // Check that we get this value from the overlay with both keys
223 EXPECT_TRUE(overlay_
->GetValue(mapped_overlay_key
, &value
));
224 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
225 // In this case, overlay reads directly from the underlay.
226 EXPECT_TRUE(overlay_
->GetValue(mapped_underlay_key
, &value
));
227 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
229 // 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));
232 Mock::VerifyAndClearExpectations(&obs
);
234 // Check that we get an overriden value from overlay, while reading the
235 // value from underlay still holds an old value.
236 EXPECT_TRUE(overlay_
->GetValue(mapped_overlay_key
, &value
));
237 EXPECT_TRUE(base::FundamentalValue(44).Equals(value
));
238 EXPECT_TRUE(overlay_
->GetValue(mapped_underlay_key
, &value
));
239 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
240 EXPECT_TRUE(underlay_
->GetValue(mapped_underlay_key
, &value
));
241 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
243 // 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));
246 Mock::VerifyAndClearExpectations(&obs
);
248 // Check that overlay remove is reported.
249 EXPECT_CALL(obs
, OnPrefValueChanged(StrEq(mapped_overlay_key
))).Times(1);
250 overlay_
->RemoveValue(mapped_overlay_key
);
251 Mock::VerifyAndClearExpectations(&obs
);
253 // Check that underlay remove is reported.
254 EXPECT_CALL(obs
, OnPrefValueChanged(StrEq(mapped_overlay_key
))).Times(1);
255 underlay_
->RemoveValue(mapped_underlay_key
);
256 Mock::VerifyAndClearExpectations(&obs
);
258 // Check that value was removed.
259 EXPECT_FALSE(overlay_
->GetValue(mapped_overlay_key
, &value
));
260 EXPECT_FALSE(overlay_
->GetValue(mapped_underlay_key
, &value
));
262 // 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));
266 Mock::VerifyAndClearExpectations(&obs
);
268 overlay_
->RemoveObserver(&obs
);
270 // 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));
274 overlay_
->SetValue(mapped_overlay_key
, new FundamentalValue(48));
275 Mock::VerifyAndClearExpectations(&obs
);