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 underlay_
->SetValue(overlay_key
, new FundamentalValue(42));
52 obs
.VerifyAndResetChangedKey(overlay_key
);
54 // Check that underlay overwriting is reported.
55 underlay_
->SetValue(overlay_key
, new FundamentalValue(43));
56 obs
.VerifyAndResetChangedKey(overlay_key
);
58 // Check that overwriting change in overlay is reported.
59 overlay_
->SetValue(overlay_key
, new FundamentalValue(44));
60 obs
.VerifyAndResetChangedKey(overlay_key
);
62 // Check that hidden underlay change is not reported.
63 underlay_
->SetValue(overlay_key
, new FundamentalValue(45));
64 EXPECT_TRUE(obs
.changed_keys
.empty());
66 // Check that overlay remove is reported.
67 overlay_
->RemoveValue(overlay_key
);
68 obs
.VerifyAndResetChangedKey(overlay_key
);
70 // Check that underlay remove is reported.
71 underlay_
->RemoveValue(overlay_key
);
72 obs
.VerifyAndResetChangedKey(overlay_key
);
74 // Check respecting of silence.
75 overlay_
->SetValueSilently(overlay_key
, new FundamentalValue(46));
76 EXPECT_TRUE(obs
.changed_keys
.empty());
78 overlay_
->RemoveObserver(&obs
);
80 // Check successful unsubscription.
81 underlay_
->SetValue(overlay_key
, new FundamentalValue(47));
82 overlay_
->SetValue(overlay_key
, new FundamentalValue(48));
83 EXPECT_TRUE(obs
.changed_keys
.empty());
86 TEST_F(OverlayUserPrefStoreTest
, GetAndSet
) {
87 const Value
* value
= NULL
;
88 EXPECT_FALSE(overlay_
->GetValue(overlay_key
, &value
));
89 EXPECT_FALSE(underlay_
->GetValue(overlay_key
, &value
));
91 underlay_
->SetValue(overlay_key
, new FundamentalValue(42));
93 // Value shines through:
94 EXPECT_TRUE(overlay_
->GetValue(overlay_key
, &value
));
95 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
97 EXPECT_TRUE(underlay_
->GetValue(overlay_key
, &value
));
98 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
100 overlay_
->SetValue(overlay_key
, new FundamentalValue(43));
102 EXPECT_TRUE(overlay_
->GetValue(overlay_key
, &value
));
103 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
105 EXPECT_TRUE(underlay_
->GetValue(overlay_key
, &value
));
106 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
108 overlay_
->RemoveValue(overlay_key
);
110 // Value shines through:
111 EXPECT_TRUE(overlay_
->GetValue(overlay_key
, &value
));
112 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
114 EXPECT_TRUE(underlay_
->GetValue(overlay_key
, &value
));
115 EXPECT_TRUE(base::FundamentalValue(42).Equals(value
));
118 // Check that GetMutableValue does not return the dictionary of the underlay.
119 TEST_F(OverlayUserPrefStoreTest
, ModifyDictionaries
) {
120 underlay_
->SetValue(overlay_key
, new DictionaryValue
);
122 Value
* modify
= NULL
;
123 EXPECT_TRUE(overlay_
->GetMutableValue(overlay_key
, &modify
));
125 ASSERT_TRUE(modify
->IsType(Value::TYPE_DICTIONARY
));
126 static_cast<DictionaryValue
*>(modify
)->SetInteger(overlay_key
, 42);
128 Value
* original_in_underlay
= NULL
;
129 EXPECT_TRUE(underlay_
->GetMutableValue(overlay_key
, &original_in_underlay
));
130 ASSERT_TRUE(original_in_underlay
);
131 ASSERT_TRUE(original_in_underlay
->IsType(Value::TYPE_DICTIONARY
));
132 EXPECT_TRUE(static_cast<DictionaryValue
*>(original_in_underlay
)->empty());
134 Value
* modified
= NULL
;
135 EXPECT_TRUE(overlay_
->GetMutableValue(overlay_key
, &modified
));
136 ASSERT_TRUE(modified
);
137 ASSERT_TRUE(modified
->IsType(Value::TYPE_DICTIONARY
));
138 EXPECT_TRUE(Value::Equals(modify
, static_cast<DictionaryValue
*>(modified
)));
141 // Here we consider a global preference that is not overlayed.
142 TEST_F(OverlayUserPrefStoreTest
, GlobalPref
) {
143 PrefStoreObserverMock obs
;
144 overlay_
->AddObserver(&obs
);
146 const Value
* value
= NULL
;
148 // Check that underlay first value is reported.
149 underlay_
->SetValue(regular_key
, new FundamentalValue(42));
150 obs
.VerifyAndResetChangedKey(regular_key
);
152 // Check that underlay overwriting is reported.
153 underlay_
->SetValue(regular_key
, new FundamentalValue(43));
154 obs
.VerifyAndResetChangedKey(regular_key
);
156 // Check that we get this value from the overlay
157 EXPECT_TRUE(overlay_
->GetValue(regular_key
, &value
));
158 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
160 // Check that overwriting change in overlay is reported.
161 overlay_
->SetValue(regular_key
, new FundamentalValue(44));
162 obs
.VerifyAndResetChangedKey(regular_key
);
164 // Check that we get this value from the overlay and the underlay.
165 EXPECT_TRUE(overlay_
->GetValue(regular_key
, &value
));
166 EXPECT_TRUE(base::FundamentalValue(44).Equals(value
));
167 EXPECT_TRUE(underlay_
->GetValue(regular_key
, &value
));
168 EXPECT_TRUE(base::FundamentalValue(44).Equals(value
));
170 // Check that overlay remove is reported.
171 overlay_
->RemoveValue(regular_key
);
172 obs
.VerifyAndResetChangedKey(regular_key
);
174 // Check that value was removed from overlay and underlay
175 EXPECT_FALSE(overlay_
->GetValue(regular_key
, &value
));
176 EXPECT_FALSE(underlay_
->GetValue(regular_key
, &value
));
178 // Check respecting of silence.
179 overlay_
->SetValueSilently(regular_key
, new FundamentalValue(46));
180 EXPECT_TRUE(obs
.changed_keys
.empty());
182 overlay_
->RemoveObserver(&obs
);
184 // Check successful unsubscription.
185 underlay_
->SetValue(regular_key
, new FundamentalValue(47));
186 overlay_
->SetValue(regular_key
, new FundamentalValue(48));
187 EXPECT_TRUE(obs
.changed_keys
.empty());
190 // Check that names mapping works correctly.
191 TEST_F(OverlayUserPrefStoreTest
, NamesMapping
) {
192 PrefStoreObserverMock obs
;
193 overlay_
->AddObserver(&obs
);
195 const Value
* value
= NULL
;
197 // Check that if there is no override in the overlay, changing underlay value
198 // is reported as changing an overlay value.
199 underlay_
->SetValue(mapped_underlay_key
, new FundamentalValue(42));
200 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
202 // Check that underlay overwriting is reported.
203 underlay_
->SetValue(mapped_underlay_key
, new FundamentalValue(43));
204 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
206 // Check that we get this value from the overlay with both keys
207 EXPECT_TRUE(overlay_
->GetValue(mapped_overlay_key
, &value
));
208 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
209 // In this case, overlay reads directly from the underlay.
210 EXPECT_TRUE(overlay_
->GetValue(mapped_underlay_key
, &value
));
211 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
213 // Check that overwriting change in overlay is reported.
214 overlay_
->SetValue(mapped_overlay_key
, new FundamentalValue(44));
215 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
217 // Check that we get an overriden value from overlay, while reading the
218 // value from underlay still holds an old value.
219 EXPECT_TRUE(overlay_
->GetValue(mapped_overlay_key
, &value
));
220 EXPECT_TRUE(base::FundamentalValue(44).Equals(value
));
221 EXPECT_TRUE(overlay_
->GetValue(mapped_underlay_key
, &value
));
222 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
223 EXPECT_TRUE(underlay_
->GetValue(mapped_underlay_key
, &value
));
224 EXPECT_TRUE(base::FundamentalValue(43).Equals(value
));
226 // Check that hidden underlay change is not reported.
227 underlay_
->SetValue(mapped_underlay_key
, new FundamentalValue(45));
228 EXPECT_TRUE(obs
.changed_keys
.empty());
230 // Check that overlay remove is reported.
231 overlay_
->RemoveValue(mapped_overlay_key
);
232 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
234 // Check that underlay remove is reported.
235 underlay_
->RemoveValue(mapped_underlay_key
);
236 obs
.VerifyAndResetChangedKey(mapped_overlay_key
);
238 // Check that value was removed.
239 EXPECT_FALSE(overlay_
->GetValue(mapped_overlay_key
, &value
));
240 EXPECT_FALSE(overlay_
->GetValue(mapped_underlay_key
, &value
));
242 // Check respecting of silence.
243 overlay_
->SetValueSilently(mapped_overlay_key
, new FundamentalValue(46));
244 EXPECT_TRUE(obs
.changed_keys
.empty());
246 overlay_
->RemoveObserver(&obs
);
248 // Check successful unsubscription.
249 underlay_
->SetValue(mapped_underlay_key
, new FundamentalValue(47));
250 overlay_
->SetValue(mapped_overlay_key
, new FundamentalValue(48));
251 EXPECT_TRUE(obs
.changed_keys
.empty());