1 // Copyright (c) 2011 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/memory/scoped_ptr.h"
6 #include "base/prefs/scoped_user_pref_update.h"
7 #include "base/values.h"
8 #include "chrome/browser/prefs/pref_model_associator.h"
9 #include "chrome/common/pref_names.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 class AbstractPreferenceMergeTest
: public testing::Test
{
15 virtual void SetUp() {
16 pref_service_
= profile_
.GetPrefs();
19 void SetContentPattern(base::DictionaryValue
* patterns_dict
,
20 const std::string
& expression
,
21 const std::string
& content_type
,
23 base::DictionaryValue
* expression_dict
;
25 patterns_dict
->GetDictionaryWithoutPathExpansion(expression
,
28 expression_dict
= new base::DictionaryValue
;
29 patterns_dict
->SetWithoutPathExpansion(expression
, expression_dict
);
31 expression_dict
->SetWithoutPathExpansion(
33 base::Value::CreateIntegerValue(setting
));
36 void SetPrefToEmpty(const std::string
& pref_name
) {
37 scoped_ptr
<base::Value
> empty_value
;
38 const PrefService::Preference
* pref
=
39 pref_service_
->FindPreference(pref_name
.c_str());
41 base::Value::Type type
= pref
->GetType();
42 if (type
== base::Value::TYPE_DICTIONARY
)
43 empty_value
.reset(new base::DictionaryValue
);
44 else if (type
== base::Value::TYPE_LIST
)
45 empty_value
.reset(new base::ListValue
);
48 pref_service_
->Set(pref_name
.c_str(), *empty_value
);
51 TestingProfile profile_
;
52 PrefService
* pref_service_
;
55 class ListPreferenceMergeTest
: public AbstractPreferenceMergeTest
{
57 ListPreferenceMergeTest() :
58 server_url0_("http://example.com/server0"),
59 server_url1_("http://example.com/server1"),
60 local_url0_("http://example.com/local0"),
61 local_url1_("http://example.com/local1") {}
63 virtual void SetUp() {
64 AbstractPreferenceMergeTest::SetUp();
65 server_url_list_
.Append(base::Value::CreateStringValue(server_url0_
));
66 server_url_list_
.Append(base::Value::CreateStringValue(server_url1_
));
69 std::string server_url0_
;
70 std::string server_url1_
;
71 std::string local_url0_
;
72 std::string local_url1_
;
73 base::ListValue server_url_list_
;
76 TEST_F(ListPreferenceMergeTest
, NotListOrDictionary
) {
77 pref_service_
->SetString(prefs::kHomePage
, local_url0_
);
78 const PrefService::Preference
* pref
=
79 pref_service_
->FindPreference(prefs::kHomePage
);
80 scoped_ptr
<base::Value
> server_value(
81 base::Value::CreateStringValue(server_url0_
));
82 scoped_ptr
<base::Value
> merged_value(
83 PrefModelAssociator::MergePreference(pref
->name(),
86 EXPECT_TRUE(merged_value
->Equals(server_value
.get()));
89 TEST_F(ListPreferenceMergeTest
, LocalEmpty
) {
90 SetPrefToEmpty(prefs::kURLsToRestoreOnStartup
);
91 const PrefService::Preference
* pref
=
92 pref_service_
->FindPreference(prefs::kURLsToRestoreOnStartup
);
93 scoped_ptr
<base::Value
> merged_value(
94 PrefModelAssociator::MergePreference(pref
->name(),
97 EXPECT_TRUE(merged_value
->Equals(&server_url_list_
));
100 TEST_F(ListPreferenceMergeTest
, ServerNull
) {
101 scoped_ptr
<base::Value
> null_value(base::Value::CreateNullValue());
103 ListPrefUpdate
update(pref_service_
, prefs::kURLsToRestoreOnStartup
);
104 base::ListValue
* local_list_value
= update
.Get();
105 local_list_value
->Append(base::Value::CreateStringValue(local_url0_
));
108 const PrefService::Preference
* pref
=
109 pref_service_
->FindPreference(prefs::kURLsToRestoreOnStartup
);
110 scoped_ptr
<base::Value
> merged_value(
111 PrefModelAssociator::MergePreference(pref
->name(),
114 const base::ListValue
* local_list_value
=
115 pref_service_
->GetList(prefs::kURLsToRestoreOnStartup
);
116 EXPECT_TRUE(merged_value
->Equals(local_list_value
));
119 TEST_F(ListPreferenceMergeTest
, ServerEmpty
) {
120 scoped_ptr
<base::Value
> empty_value(new base::ListValue
);
122 ListPrefUpdate
update(pref_service_
, prefs::kURLsToRestoreOnStartup
);
123 base::ListValue
* local_list_value
= update
.Get();
124 local_list_value
->Append(base::Value::CreateStringValue(local_url0_
));
127 const PrefService::Preference
* pref
=
128 pref_service_
->FindPreference(prefs::kURLsToRestoreOnStartup
);
129 scoped_ptr
<base::Value
> merged_value(
130 PrefModelAssociator::MergePreference(pref
->name(),
133 const base::ListValue
* local_list_value
=
134 pref_service_
->GetList(prefs::kURLsToRestoreOnStartup
);
135 EXPECT_TRUE(merged_value
->Equals(local_list_value
));
138 TEST_F(ListPreferenceMergeTest
, Merge
) {
140 ListPrefUpdate
update(pref_service_
, prefs::kURLsToRestoreOnStartup
);
141 base::ListValue
* local_list_value
= update
.Get();
142 local_list_value
->Append(base::Value::CreateStringValue(local_url0_
));
143 local_list_value
->Append(base::Value::CreateStringValue(local_url1_
));
146 const PrefService::Preference
* pref
=
147 pref_service_
->FindPreference(prefs::kURLsToRestoreOnStartup
);
148 scoped_ptr
<base::Value
> merged_value(
149 PrefModelAssociator::MergePreference(pref
->name(),
153 base::ListValue expected
;
154 expected
.Append(base::Value::CreateStringValue(server_url0_
));
155 expected
.Append(base::Value::CreateStringValue(server_url1_
));
156 expected
.Append(base::Value::CreateStringValue(local_url0_
));
157 expected
.Append(base::Value::CreateStringValue(local_url1_
));
158 EXPECT_TRUE(merged_value
->Equals(&expected
));
161 TEST_F(ListPreferenceMergeTest
, Duplicates
) {
163 ListPrefUpdate
update(pref_service_
, prefs::kURLsToRestoreOnStartup
);
164 base::ListValue
* local_list_value
= update
.Get();
165 local_list_value
->Append(base::Value::CreateStringValue(local_url0_
));
166 local_list_value
->Append(base::Value::CreateStringValue(server_url0_
));
167 local_list_value
->Append(base::Value::CreateStringValue(server_url1_
));
170 const PrefService::Preference
* pref
=
171 pref_service_
->FindPreference(prefs::kURLsToRestoreOnStartup
);
172 scoped_ptr
<base::Value
> merged_value(
173 PrefModelAssociator::MergePreference(pref
->name(),
177 base::ListValue expected
;
178 expected
.Append(base::Value::CreateStringValue(server_url0_
));
179 expected
.Append(base::Value::CreateStringValue(server_url1_
));
180 expected
.Append(base::Value::CreateStringValue(local_url0_
));
181 EXPECT_TRUE(merged_value
->Equals(&expected
));
184 TEST_F(ListPreferenceMergeTest
, Equals
) {
186 ListPrefUpdate
update(pref_service_
, prefs::kURLsToRestoreOnStartup
);
187 base::ListValue
* local_list_value
= update
.Get();
188 local_list_value
->Append(base::Value::CreateStringValue(server_url0_
));
189 local_list_value
->Append(base::Value::CreateStringValue(server_url1_
));
192 scoped_ptr
<base::Value
> original(server_url_list_
.DeepCopy());
193 const PrefService::Preference
* pref
=
194 pref_service_
->FindPreference(prefs::kURLsToRestoreOnStartup
);
195 scoped_ptr
<base::Value
> merged_value(
196 PrefModelAssociator::MergePreference(pref
->name(),
199 EXPECT_TRUE(merged_value
->Equals(original
.get()));
202 class DictionaryPreferenceMergeTest
: public AbstractPreferenceMergeTest
{
204 DictionaryPreferenceMergeTest() :
205 expression0_("expression0"),
206 expression1_("expression1"),
207 expression2_("expression2"),
208 content_type0_("content_type0"),
209 content_type1_("content_type1") {}
211 virtual void SetUp() {
212 AbstractPreferenceMergeTest::SetUp();
213 SetContentPattern(&server_patterns_
, expression0_
, content_type0_
, 1);
214 SetContentPattern(&server_patterns_
, expression0_
, content_type1_
, 2);
215 SetContentPattern(&server_patterns_
, expression1_
, content_type0_
, 1);
218 std::string expression0_
;
219 std::string expression1_
;
220 std::string expression2_
;
221 std::string content_type0_
;
222 std::string content_type1_
;
223 base::DictionaryValue server_patterns_
;
226 TEST_F(DictionaryPreferenceMergeTest
, LocalEmpty
) {
227 SetPrefToEmpty(prefs::kContentSettingsPatternPairs
);
228 const PrefService::Preference
* pref
=
229 pref_service_
->FindPreference(prefs::kContentSettingsPatternPairs
);
230 scoped_ptr
<base::Value
> merged_value(
231 PrefModelAssociator::MergePreference(pref
->name(),
234 EXPECT_TRUE(merged_value
->Equals(&server_patterns_
));
237 TEST_F(DictionaryPreferenceMergeTest
, ServerNull
) {
238 scoped_ptr
<base::Value
> null_value(base::Value::CreateNullValue());
240 DictionaryPrefUpdate
update(pref_service_
,
241 prefs::kContentSettingsPatternPairs
);
242 base::DictionaryValue
* local_dict_value
= update
.Get();
243 SetContentPattern(local_dict_value
, expression2_
, content_type0_
, 1);
246 const PrefService::Preference
* pref
=
247 pref_service_
->FindPreference(prefs::kContentSettingsPatternPairs
);
248 scoped_ptr
<base::Value
> merged_value(
249 PrefModelAssociator::MergePreference(pref
->name(),
252 const base::DictionaryValue
* local_dict_value
=
253 pref_service_
->GetDictionary(prefs::kContentSettingsPatternPairs
);
254 EXPECT_TRUE(merged_value
->Equals(local_dict_value
));
257 TEST_F(DictionaryPreferenceMergeTest
, ServerEmpty
) {
258 scoped_ptr
<base::Value
> empty_value(new base::DictionaryValue
);
260 DictionaryPrefUpdate
update(pref_service_
,
261 prefs::kContentSettingsPatternPairs
);
262 base::DictionaryValue
* local_dict_value
= update
.Get();
263 SetContentPattern(local_dict_value
, expression2_
, content_type0_
, 1);
266 const PrefService::Preference
* pref
=
267 pref_service_
->FindPreference(prefs::kContentSettingsPatternPairs
);
268 scoped_ptr
<base::Value
> merged_value(
269 PrefModelAssociator::MergePreference(pref
->name(),
272 const base::DictionaryValue
* local_dict_value
=
273 pref_service_
->GetDictionary(prefs::kContentSettingsPatternPairs
);
274 EXPECT_TRUE(merged_value
->Equals(local_dict_value
));
277 TEST_F(DictionaryPreferenceMergeTest
, MergeNoConflicts
) {
279 DictionaryPrefUpdate
update(pref_service_
,
280 prefs::kContentSettingsPatternPairs
);
281 base::DictionaryValue
* local_dict_value
= update
.Get();
282 SetContentPattern(local_dict_value
, expression2_
, content_type0_
, 1);
285 scoped_ptr
<base::Value
> merged_value(PrefModelAssociator::MergePreference(
286 prefs::kContentSettingsPatternPairs
,
287 *pref_service_
->FindPreference(prefs::kContentSettingsPatternPairs
)->
291 base::DictionaryValue expected
;
292 SetContentPattern(&expected
, expression0_
, content_type0_
, 1);
293 SetContentPattern(&expected
, expression0_
, content_type1_
, 2);
294 SetContentPattern(&expected
, expression1_
, content_type0_
, 1);
295 SetContentPattern(&expected
, expression2_
, content_type0_
, 1);
296 EXPECT_TRUE(merged_value
->Equals(&expected
));
299 TEST_F(DictionaryPreferenceMergeTest
, MergeConflicts
) {
301 DictionaryPrefUpdate
update(pref_service_
,
302 prefs::kContentSettingsPatternPairs
);
303 base::DictionaryValue
* local_dict_value
= update
.Get();
304 SetContentPattern(local_dict_value
, expression0_
, content_type0_
, 2);
305 SetContentPattern(local_dict_value
, expression1_
, content_type0_
, 1);
306 SetContentPattern(local_dict_value
, expression1_
, content_type1_
, 1);
307 SetContentPattern(local_dict_value
, expression2_
, content_type0_
, 2);
310 scoped_ptr
<base::Value
> merged_value(PrefModelAssociator::MergePreference(
311 prefs::kContentSettingsPatternPairs
,
312 *pref_service_
->FindPreference(prefs::kContentSettingsPatternPairs
)->
316 base::DictionaryValue expected
;
317 SetContentPattern(&expected
, expression0_
, content_type0_
, 1);
318 SetContentPattern(&expected
, expression0_
, content_type1_
, 2);
319 SetContentPattern(&expected
, expression1_
, content_type0_
, 1);
320 SetContentPattern(&expected
, expression1_
, content_type1_
, 1);
321 SetContentPattern(&expected
, expression2_
, content_type0_
, 2);
322 EXPECT_TRUE(merged_value
->Equals(&expected
));
325 TEST_F(DictionaryPreferenceMergeTest
, Equal
) {
327 DictionaryPrefUpdate
update(pref_service_
,
328 prefs::kContentSettingsPatternPairs
);
329 base::DictionaryValue
* local_dict_value
= update
.Get();
330 SetContentPattern(local_dict_value
, expression0_
, content_type0_
, 1);
331 SetContentPattern(local_dict_value
, expression0_
, content_type1_
, 2);
332 SetContentPattern(local_dict_value
, expression1_
, content_type0_
, 1);
335 scoped_ptr
<base::Value
> merged_value(PrefModelAssociator::MergePreference(
336 prefs::kContentSettingsPatternPairs
,
338 FindPreference(prefs::kContentSettingsPatternPairs
)->GetValue(),
340 EXPECT_TRUE(merged_value
->Equals(&server_patterns_
));
343 TEST_F(DictionaryPreferenceMergeTest
, ConflictButServerWins
) {
345 DictionaryPrefUpdate
update(pref_service_
,
346 prefs::kContentSettingsPatternPairs
);
347 base::DictionaryValue
* local_dict_value
= update
.Get();
348 SetContentPattern(local_dict_value
, expression0_
, content_type0_
, 2);
349 SetContentPattern(local_dict_value
, expression0_
, content_type1_
, 2);
350 SetContentPattern(local_dict_value
, expression1_
, content_type0_
, 1);
353 scoped_ptr
<base::Value
> merged_value(PrefModelAssociator::MergePreference(
354 prefs::kContentSettingsPatternPairs
,
356 FindPreference(prefs::kContentSettingsPatternPairs
)->GetValue(),
358 EXPECT_TRUE(merged_value
->Equals(&server_patterns_
));
361 class IndividualPreferenceMergeTest
: public AbstractPreferenceMergeTest
{
363 IndividualPreferenceMergeTest() :
364 url0_("http://example.com/server0"),
365 url1_("http://example.com/server1"),
366 expression0_("expression0"),
367 expression1_("expression1"),
368 content_type0_("content_type0") {}
370 virtual void SetUp() {
371 AbstractPreferenceMergeTest::SetUp();
372 server_url_list_
.Append(base::Value::CreateStringValue(url0_
));
373 SetContentPattern(&server_patterns_
, expression0_
, content_type0_
, 1);
376 bool MergeListPreference(const char* pref
) {
378 ListPrefUpdate
update(pref_service_
, pref
);
379 base::ListValue
* local_list_value
= update
.Get();
380 local_list_value
->Append(base::Value::CreateStringValue(url1_
));
383 scoped_ptr
<base::Value
> merged_value(PrefModelAssociator::MergePreference(
385 *pref_service_
->GetUserPrefValue(pref
),
388 base::ListValue expected
;
389 expected
.Append(base::Value::CreateStringValue(url0_
));
390 expected
.Append(base::Value::CreateStringValue(url1_
));
391 return merged_value
->Equals(&expected
);
394 bool MergeDictionaryPreference(const char* pref
) {
396 DictionaryPrefUpdate
update(pref_service_
, pref
);
397 base::DictionaryValue
* local_dict_value
= update
.Get();
398 SetContentPattern(local_dict_value
, expression1_
, content_type0_
, 1);
401 scoped_ptr
<base::Value
> merged_value(PrefModelAssociator::MergePreference(
403 *pref_service_
->GetUserPrefValue(pref
),
406 base::DictionaryValue expected
;
407 SetContentPattern(&expected
, expression0_
, content_type0_
, 1);
408 SetContentPattern(&expected
, expression1_
, content_type0_
, 1);
409 return merged_value
->Equals(&expected
);
414 std::string expression0_
;
415 std::string expression1_
;
416 std::string content_type0_
;
417 base::ListValue server_url_list_
;
418 base::DictionaryValue server_patterns_
;
421 TEST_F(IndividualPreferenceMergeTest
, URLsToRestoreOnStartup
) {
422 EXPECT_TRUE(MergeListPreference(prefs::kURLsToRestoreOnStartup
));