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/basictypes.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_store_observer_mock.h"
9 #include "base/values.h"
10 #include "chrome/browser/extensions/extension_pref_value_map.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
15 const char kExt1
[] = "ext1";
16 const char kExt2
[] = "ext2";
17 const char kExt3
[] = "ext3";
19 const char kPref1
[] = "path1.subpath";
20 const char kPref2
[] = "path2";
21 const char kPref3
[] = "path3";
22 const char kPref4
[] = "path4";
25 static base::Value
* CreateVal(const char* str
) {
26 return new base::StringValue(str
);
29 static base::Time
CreateTime(int64 t
) {
30 return base::Time::FromInternalValue(t
);
33 template <typename BASECLASS
>
34 class ExtensionPrefValueMapTestBase
: public BASECLASS
{
36 static const extensions::ExtensionPrefsScope kRegular
=
37 extensions::kExtensionPrefsScopeRegular
;
38 static const extensions::ExtensionPrefsScope kRegularOnly
=
39 extensions::kExtensionPrefsScopeRegularOnly
;
40 static const extensions::ExtensionPrefsScope kIncognitoPersistent
=
41 extensions::kExtensionPrefsScopeIncognitoPersistent
;
42 static const extensions::ExtensionPrefsScope kIncognitoSessionOnly
=
43 extensions::kExtensionPrefsScopeIncognitoSessionOnly
;
45 // Returns an empty string if the key is not set.
46 std::string
GetValue(const char * key
, bool incognito
) const {
47 const base::Value
*value
=
48 epvm_
.GetEffectivePrefValue(key
, incognito
, NULL
);
49 std::string string_value
;
51 value
->GetAsString(&string_value
);
56 ExtensionPrefValueMap epvm_
;
59 class ExtensionPrefValueMapTest
60 : public ExtensionPrefValueMapTestBase
<testing::Test
> {
63 // A gmock-ified implementation of PrefStore::Observer.
64 class ExtensionPrefValueMapObserverMock
65 : public ExtensionPrefValueMap::Observer
{
67 ExtensionPrefValueMapObserverMock() {}
68 virtual ~ExtensionPrefValueMapObserverMock() {}
70 MOCK_METHOD1(OnPrefValueChanged
, void(const std::string
&));
71 MOCK_METHOD0(OnInitializationCompleted
, void());
72 MOCK_METHOD0(OnExtensionPrefValueMapDestruction
, void());
75 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefValueMapObserverMock
);
78 TEST_F(ExtensionPrefValueMapTest
, SetAndGetPrefValue
) {
79 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
80 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val1"));
81 EXPECT_EQ("val1", GetValue(kPref1
, false));
84 TEST_F(ExtensionPrefValueMapTest
, GetNotSetPrefValue
) {
85 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
86 EXPECT_EQ("", GetValue(kPref1
, false));
89 // Make sure the last-installed extension wins for each preference.
90 TEST_F(ExtensionPrefValueMapTest
, Override
) {
91 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
92 epvm_
.RegisterExtension(kExt2
, CreateTime(20), true);
93 epvm_
.RegisterExtension(kExt3
, CreateTime(30), true);
95 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val1"));
96 epvm_
.SetExtensionPref(kExt2
, kPref1
, kRegular
, CreateVal("val2"));
97 epvm_
.SetExtensionPref(kExt3
, kPref1
, kRegular
, CreateVal("val3"));
99 epvm_
.SetExtensionPref(kExt1
, kPref2
, kRegular
, CreateVal("val4"));
100 epvm_
.SetExtensionPref(kExt2
, kPref2
, kRegular
, CreateVal("val5"));
102 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val6"));
103 epvm_
.SetExtensionPref(kExt1
, kPref2
, kRegular
, CreateVal("val7"));
104 epvm_
.SetExtensionPref(kExt1
, kPref3
, kRegular
, CreateVal("val8"));
106 EXPECT_EQ("val3", GetValue(kPref1
, false));
107 EXPECT_EQ("val5", GetValue(kPref2
, false));
108 EXPECT_EQ("val8", GetValue(kPref3
, false));
111 TEST_F(ExtensionPrefValueMapTest
, OverrideChecks
) {
112 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
113 epvm_
.RegisterExtension(kExt2
, CreateTime(20), true);
114 epvm_
.RegisterExtension(kExt3
, CreateTime(30), true);
116 EXPECT_FALSE(epvm_
.DoesExtensionControlPref(kExt1
, kPref1
, NULL
));
117 EXPECT_FALSE(epvm_
.DoesExtensionControlPref(kExt2
, kPref1
, NULL
));
118 EXPECT_FALSE(epvm_
.DoesExtensionControlPref(kExt3
, kPref1
, NULL
));
119 EXPECT_TRUE(epvm_
.CanExtensionControlPref(kExt1
, kPref1
, false));
120 EXPECT_TRUE(epvm_
.CanExtensionControlPref(kExt2
, kPref1
, false));
121 EXPECT_TRUE(epvm_
.CanExtensionControlPref(kExt3
, kPref1
, false));
123 epvm_
.SetExtensionPref(kExt2
, kPref1
, kRegular
, CreateVal("val1"));
125 EXPECT_FALSE(epvm_
.DoesExtensionControlPref(kExt1
, kPref1
, NULL
));
126 EXPECT_TRUE(epvm_
.DoesExtensionControlPref(kExt2
, kPref1
, NULL
));
127 EXPECT_FALSE(epvm_
.DoesExtensionControlPref(kExt3
, kPref1
, NULL
));
128 EXPECT_FALSE(epvm_
.CanExtensionControlPref(kExt1
, kPref1
, false));
129 EXPECT_TRUE(epvm_
.CanExtensionControlPref(kExt2
, kPref1
, false));
130 EXPECT_TRUE(epvm_
.CanExtensionControlPref(kExt3
, kPref1
, false));
133 TEST_F(ExtensionPrefValueMapTest
, SetAndGetPrefValueIncognito
) {
134 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
135 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val1"));
136 EXPECT_EQ("val1", GetValue(kPref1
, true));
139 TEST_F(ExtensionPrefValueMapTest
, UninstallOnlyExtension
) {
140 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
141 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val1"));
142 epvm_
.UnregisterExtension(kExt1
);
144 EXPECT_EQ("", GetValue(kPref1
, false));
147 // Tests uninstalling an extension that wasn't winning for any preferences.
148 TEST_F(ExtensionPrefValueMapTest
, UninstallIrrelevantExtension
) {
149 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
150 epvm_
.RegisterExtension(kExt2
, CreateTime(10), true);
152 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val1"));
153 epvm_
.SetExtensionPref(kExt2
, kPref1
, kRegular
, CreateVal("val2"));
155 epvm_
.SetExtensionPref(kExt1
, kPref2
, kRegular
, CreateVal("val3"));
156 epvm_
.SetExtensionPref(kExt2
, kPref2
, kRegular
, CreateVal("val4"));
158 epvm_
.UnregisterExtension(kExt1
);
160 EXPECT_EQ("val2", GetValue(kPref1
, false));
161 EXPECT_EQ("val4", GetValue(kPref2
, false));
164 // Tests uninstalling an extension that was winning for all preferences.
165 TEST_F(ExtensionPrefValueMapTest
, UninstallExtensionFromTop
) {
166 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
167 epvm_
.RegisterExtension(kExt2
, CreateTime(20), true);
168 epvm_
.RegisterExtension(kExt3
, CreateTime(30), true);
170 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val1"));
171 epvm_
.SetExtensionPref(kExt2
, kPref1
, kRegular
, CreateVal("val2"));
172 epvm_
.SetExtensionPref(kExt3
, kPref1
, kRegular
, CreateVal("val3"));
174 epvm_
.SetExtensionPref(kExt1
, kPref2
, kRegular
, CreateVal("val4"));
175 epvm_
.SetExtensionPref(kExt3
, kPref2
, kRegular
, CreateVal("val5"));
177 epvm_
.UnregisterExtension(kExt3
);
179 EXPECT_EQ("val2", GetValue(kPref1
, false));
180 EXPECT_EQ("val4", GetValue(kPref2
, false));
183 // Tests uninstalling an extension that was winning for only some preferences.
184 TEST_F(ExtensionPrefValueMapTest
, UninstallExtensionFromMiddle
) {
185 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
186 epvm_
.RegisterExtension(kExt2
, CreateTime(20), true);
187 epvm_
.RegisterExtension(kExt3
, CreateTime(30), true);
189 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val1"));
190 epvm_
.SetExtensionPref(kExt2
, kPref1
, kRegular
, CreateVal("val2"));
191 epvm_
.SetExtensionPref(kExt3
, kPref1
, kRegular
, CreateVal("val3"));
193 epvm_
.SetExtensionPref(kExt1
, kPref2
, kRegular
, CreateVal("val4"));
194 epvm_
.SetExtensionPref(kExt2
, kPref2
, kRegular
, CreateVal("val5"));
196 epvm_
.SetExtensionPref(kExt1
, kPref3
, kRegular
, CreateVal("val6"));
198 epvm_
.SetExtensionPref(kExt2
, kPref4
, kRegular
, CreateVal("val7"));
200 epvm_
.UnregisterExtension(kExt2
);
202 EXPECT_EQ("val3", GetValue(kPref1
, false));
203 EXPECT_EQ("val4", GetValue(kPref2
, false));
204 EXPECT_EQ("val6", GetValue(kPref3
, false));
205 EXPECT_EQ("", GetValue(kPref4
, false));
208 // Tests triggering of notifications to registered observers.
209 TEST_F(ExtensionPrefValueMapTest
, NotifyWhenNeeded
) {
211 using testing::StrEq
;
213 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
215 ExtensionPrefValueMapObserverMock observer
;
216 epvm_
.AddObserver(&observer
);
218 EXPECT_CALL(observer
, OnPrefValueChanged(std::string(kPref1
)));
219 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val1"));
220 Mock::VerifyAndClearExpectations(&observer
);
222 // Write the same value again.
223 EXPECT_CALL(observer
, OnPrefValueChanged(std::string(kPref1
))).Times(0);
224 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val1"));
225 Mock::VerifyAndClearExpectations(&observer
);
227 // Override incognito value.
228 EXPECT_CALL(observer
, OnPrefValueChanged(std::string(kPref1
)));
229 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val2"));
230 Mock::VerifyAndClearExpectations(&observer
);
232 // Override non-incognito value.
233 EXPECT_CALL(observer
, OnPrefValueChanged(std::string(kPref1
)));
234 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val3"));
235 Mock::VerifyAndClearExpectations(&observer
);
238 EXPECT_CALL(observer
, OnPrefValueChanged(std::string(kPref1
)));
239 epvm_
.SetExtensionState(kExt1
, false);
240 Mock::VerifyAndClearExpectations(&observer
);
243 EXPECT_CALL(observer
, OnPrefValueChanged(std::string(kPref1
)));
244 epvm_
.SetExtensionState(kExt1
, true);
245 Mock::VerifyAndClearExpectations(&observer
);
248 EXPECT_CALL(observer
, OnPrefValueChanged(std::string(kPref1
)));
249 epvm_
.UnregisterExtension(kExt1
);
250 Mock::VerifyAndClearExpectations(&observer
);
252 epvm_
.RemoveObserver(&observer
);
254 // Write new value --> no notification after removing observer.
255 EXPECT_CALL(observer
, OnPrefValueChanged(std::string(kPref1
))).Times(0);
256 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
257 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val4"));
258 Mock::VerifyAndClearExpectations(&observer
);
261 // Tests disabling an extension.
262 TEST_F(ExtensionPrefValueMapTest
, DisableExt
) {
263 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
265 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val1"));
266 epvm_
.SetExtensionState(kExt1
, false);
267 EXPECT_EQ("", GetValue(kPref1
, false));
270 // Tests disabling and reenabling an extension.
271 TEST_F(ExtensionPrefValueMapTest
, ReenableExt
) {
272 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
274 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
, CreateVal("val1"));
275 epvm_
.SetExtensionState(kExt1
, false);
276 epvm_
.SetExtensionState(kExt1
, true);
277 EXPECT_EQ("val1", GetValue(kPref1
, false));
280 struct OverrideIncognitoTestCase
{
281 OverrideIncognitoTestCase(int val_ext1_regular
,
282 int val_ext1_regular_only
,
283 int val_ext1_incognito_pers
,
284 int val_ext1_incognito_sess
,
285 int val_ext2_regular
,
286 int val_ext2_regular_only
,
287 int val_ext2_incognito_pers
,
288 int val_ext2_incognito_sess
,
289 int effective_value_regular
,
290 int effective_value_incognito
)
291 : val_ext1_regular_(val_ext1_regular
),
292 val_ext1_regular_only_(val_ext1_regular_only
),
293 val_ext1_incognito_pers_(val_ext1_incognito_pers
),
294 val_ext1_incognito_sess_(val_ext1_incognito_sess
),
295 val_ext2_regular_(val_ext2_regular
),
296 val_ext2_regular_only_(val_ext2_regular_only
),
297 val_ext2_incognito_pers_(val_ext2_incognito_pers
),
298 val_ext2_incognito_sess_(val_ext2_incognito_sess
),
299 effective_value_regular_(effective_value_regular
),
300 effective_value_incognito_(effective_value_incognito
) {}
302 // pers. = persistent
303 // sess. = session only
304 int val_ext1_regular_
; // pref value of extension 1
305 int val_ext1_regular_only_
; // pref value of extension 1 regular-only.
306 int val_ext1_incognito_pers_
; // pref value of extension 1 incognito pers.
307 int val_ext1_incognito_sess_
; // pref value of extension 1 incognito sess.
308 int val_ext2_regular_
; // pref value of extension 2
309 int val_ext2_regular_only_
; // pref value of extension 2 regular-only.
310 int val_ext2_incognito_pers_
; // pref value of extension 2 incognito pers.
311 int val_ext2_incognito_sess_
; // pref value of extension 2 incognito sess.
312 int effective_value_regular_
; // desired winner regular
313 int effective_value_incognito_
; // desired winner incognito
316 class ExtensionPrefValueMapTestIncognitoTests
317 : public ExtensionPrefValueMapTestBase
<
318 testing::TestWithParam
<OverrideIncognitoTestCase
> > {
321 TEST_P(ExtensionPrefValueMapTestIncognitoTests
, OverrideIncognito
) {
322 OverrideIncognitoTestCase test
= GetParam();
323 const char* strings
[] = {
335 epvm_
.RegisterExtension(kExt1
, CreateTime(10), true);
336 epvm_
.RegisterExtension(kExt2
, CreateTime(20), true);
337 if (test
.val_ext1_regular_
) {
338 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegular
,
339 CreateVal(strings
[test
.val_ext1_regular_
]));
341 if (test
.val_ext1_regular_only_
) {
342 epvm_
.SetExtensionPref(kExt1
, kPref1
, kRegularOnly
,
343 CreateVal(strings
[test
.val_ext1_regular_only_
]));
345 if (test
.val_ext1_incognito_pers_
) {
346 epvm_
.SetExtensionPref(kExt1
, kPref1
, kIncognitoPersistent
,
347 CreateVal(strings
[test
.val_ext1_incognito_pers_
]));
349 if (test
.val_ext1_incognito_sess_
) {
350 epvm_
.SetExtensionPref(kExt1
, kPref1
, kIncognitoSessionOnly
,
351 CreateVal(strings
[test
.val_ext1_incognito_sess_
]));
353 if (test
.val_ext2_regular_
) {
354 epvm_
.SetExtensionPref(kExt2
, kPref1
, kRegular
,
355 CreateVal(strings
[test
.val_ext2_regular_
]));
357 if (test
.val_ext2_regular_only_
) {
358 epvm_
.SetExtensionPref(kExt2
, kPref1
, kRegularOnly
,
359 CreateVal(strings
[test
.val_ext2_regular_only_
]));
361 if (test
.val_ext2_incognito_pers_
) {
362 epvm_
.SetExtensionPref(kExt2
, kPref1
, kIncognitoPersistent
,
363 CreateVal(strings
[test
.val_ext2_incognito_pers_
]));
365 if (test
.val_ext2_incognito_sess_
) {
366 epvm_
.SetExtensionPref(kExt2
, kPref1
, kIncognitoSessionOnly
,
367 CreateVal(strings
[test
.val_ext2_incognito_sess_
]));
370 EXPECT_EQ(strings
[test
.effective_value_regular_
], GetValue(kPref1
, false));
371 EXPECT_EQ(strings
[test
.effective_value_incognito_
], GetValue(kPref1
, true));
372 epvm_
.UnregisterExtension(kExt1
);
373 epvm_
.UnregisterExtension(kExt2
);
376 INSTANTIATE_TEST_CASE_P(
377 ExtensionPrefValueMapTestIncognitoTestsInstance
,
378 ExtensionPrefValueMapTestIncognitoTests
,
380 // e.g. (1, 0, 0, 0, 0, 0, 7, 0, 1, 7), means:
381 // ext1 regular is set to "val1", ext2 incognito persistent is set to
383 // --> the winning regular value is "val1", the winning incognito
385 OverrideIncognitoTestCase(1, 0, 0, 0, 0, 0, 0, 0, 1, 1),
386 OverrideIncognitoTestCase(1, 2, 0, 0, 0, 0, 0, 0, 2, 1),
387 OverrideIncognitoTestCase(1, 0, 3, 0, 0, 0, 0, 0, 1, 3),
388 OverrideIncognitoTestCase(1, 0, 0, 4, 0, 0, 0, 0, 1, 4),
389 OverrideIncognitoTestCase(1, 0, 3, 4, 0, 0, 0, 0, 1, 4),
390 OverrideIncognitoTestCase(1, 2, 3, 0, 0, 0, 0, 0, 2, 3),
391 OverrideIncognitoTestCase(1, 0, 0, 0, 5, 0, 0, 0, 5, 5),
392 OverrideIncognitoTestCase(1, 2, 3, 0, 5, 0, 0, 0, 5, 5),
393 OverrideIncognitoTestCase(1, 0, 0, 0, 0, 6, 0, 0, 6, 1),
394 OverrideIncognitoTestCase(1, 0, 3, 0, 5, 6, 0, 0, 6, 5),
395 OverrideIncognitoTestCase(1, 0, 0, 4, 5, 6, 0, 0, 6, 5),
396 OverrideIncognitoTestCase(1, 0, 0, 0, 0, 0, 7, 0, 1, 7),
397 OverrideIncognitoTestCase(1, 2, 0, 0, 5, 0, 7, 0, 5, 7),
398 OverrideIncognitoTestCase(1, 2, 0, 0, 5, 0, 0, 8, 5, 8),
399 OverrideIncognitoTestCase(1, 2, 0, 0, 5, 0, 7, 8, 5, 8),
400 OverrideIncognitoTestCase(1, 2, 3, 0, 0, 6, 7, 0, 6, 7)));