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 void SetUp() override
{ pref_service_
= profile_
.GetPrefs(); }
17 void SetContentPattern(base::DictionaryValue
* patterns_dict
,
18 const std::string
& expression
,
19 const std::string
& content_type
,
21 base::DictionaryValue
* expression_dict
;
23 patterns_dict
->GetDictionaryWithoutPathExpansion(expression
,
26 expression_dict
= new base::DictionaryValue
;
27 patterns_dict
->SetWithoutPathExpansion(expression
, expression_dict
);
29 expression_dict
->SetWithoutPathExpansion(
30 content_type
, new base::FundamentalValue(setting
));
33 void SetPrefToEmpty(const std::string
& pref_name
) {
34 scoped_ptr
<base::Value
> empty_value
;
35 const PrefService::Preference
* pref
=
36 pref_service_
->FindPreference(pref_name
.c_str());
38 base::Value::Type type
= pref
->GetType();
39 if (type
== base::Value::TYPE_DICTIONARY
)
40 empty_value
.reset(new base::DictionaryValue
);
41 else if (type
== base::Value::TYPE_LIST
)
42 empty_value
.reset(new base::ListValue
);
45 pref_service_
->Set(pref_name
.c_str(), *empty_value
);
48 TestingProfile profile_
;
49 PrefService
* pref_service_
;
52 class ListPreferenceMergeTest
: public AbstractPreferenceMergeTest
{
54 ListPreferenceMergeTest() :
55 server_url0_("http://example.com/server0"),
56 server_url1_("http://example.com/server1"),
57 local_url0_("http://example.com/local0"),
58 local_url1_("http://example.com/local1") {}
60 void SetUp() override
{
61 AbstractPreferenceMergeTest::SetUp();
62 server_url_list_
.Append(new base::StringValue(server_url0_
));
63 server_url_list_
.Append(new base::StringValue(server_url1_
));
66 std::string server_url0_
;
67 std::string server_url1_
;
68 std::string local_url0_
;
69 std::string local_url1_
;
70 base::ListValue server_url_list_
;
73 TEST_F(ListPreferenceMergeTest
, NotListOrDictionary
) {
74 pref_service_
->SetString(prefs::kHomePage
, local_url0_
);
75 const PrefService::Preference
* pref
=
76 pref_service_
->FindPreference(prefs::kHomePage
);
77 scoped_ptr
<base::Value
> server_value(new base::StringValue(server_url0_
));
78 scoped_ptr
<base::Value
> merged_value(
79 PrefModelAssociator::MergePreference(pref
->name(),
82 EXPECT_TRUE(merged_value
->Equals(server_value
.get()));
85 TEST_F(ListPreferenceMergeTest
, LocalEmpty
) {
86 SetPrefToEmpty(prefs::kURLsToRestoreOnStartup
);
87 const PrefService::Preference
* pref
=
88 pref_service_
->FindPreference(prefs::kURLsToRestoreOnStartup
);
89 scoped_ptr
<base::Value
> merged_value(
90 PrefModelAssociator::MergePreference(pref
->name(),
93 EXPECT_TRUE(merged_value
->Equals(&server_url_list_
));
96 TEST_F(ListPreferenceMergeTest
, ServerNull
) {
97 scoped_ptr
<base::Value
> null_value(base::Value::CreateNullValue());
99 ListPrefUpdate
update(pref_service_
, prefs::kURLsToRestoreOnStartup
);
100 base::ListValue
* local_list_value
= update
.Get();
101 local_list_value
->Append(new base::StringValue(local_url0_
));
104 const PrefService::Preference
* pref
=
105 pref_service_
->FindPreference(prefs::kURLsToRestoreOnStartup
);
106 scoped_ptr
<base::Value
> merged_value(
107 PrefModelAssociator::MergePreference(pref
->name(),
110 const base::ListValue
* local_list_value
=
111 pref_service_
->GetList(prefs::kURLsToRestoreOnStartup
);
112 EXPECT_TRUE(merged_value
->Equals(local_list_value
));
115 TEST_F(ListPreferenceMergeTest
, ServerEmpty
) {
116 scoped_ptr
<base::Value
> empty_value(new base::ListValue
);
118 ListPrefUpdate
update(pref_service_
, prefs::kURLsToRestoreOnStartup
);
119 base::ListValue
* local_list_value
= update
.Get();
120 local_list_value
->Append(new base::StringValue(local_url0_
));
123 const PrefService::Preference
* pref
=
124 pref_service_
->FindPreference(prefs::kURLsToRestoreOnStartup
);
125 scoped_ptr
<base::Value
> merged_value(
126 PrefModelAssociator::MergePreference(pref
->name(),
129 const base::ListValue
* local_list_value
=
130 pref_service_
->GetList(prefs::kURLsToRestoreOnStartup
);
131 EXPECT_TRUE(merged_value
->Equals(local_list_value
));
134 TEST_F(ListPreferenceMergeTest
, Merge
) {
136 ListPrefUpdate
update(pref_service_
, prefs::kURLsToRestoreOnStartup
);
137 base::ListValue
* local_list_value
= update
.Get();
138 local_list_value
->Append(new base::StringValue(local_url0_
));
139 local_list_value
->Append(new base::StringValue(local_url1_
));
142 const PrefService::Preference
* pref
=
143 pref_service_
->FindPreference(prefs::kURLsToRestoreOnStartup
);
144 scoped_ptr
<base::Value
> merged_value(
145 PrefModelAssociator::MergePreference(pref
->name(),
149 base::ListValue expected
;
150 expected
.Append(new base::StringValue(server_url0_
));
151 expected
.Append(new base::StringValue(server_url1_
));
152 expected
.Append(new base::StringValue(local_url0_
));
153 expected
.Append(new base::StringValue(local_url1_
));
154 EXPECT_TRUE(merged_value
->Equals(&expected
));
157 TEST_F(ListPreferenceMergeTest
, Duplicates
) {
159 ListPrefUpdate
update(pref_service_
, prefs::kURLsToRestoreOnStartup
);
160 base::ListValue
* local_list_value
= update
.Get();
161 local_list_value
->Append(new base::StringValue(local_url0_
));
162 local_list_value
->Append(new base::StringValue(server_url0_
));
163 local_list_value
->Append(new base::StringValue(server_url1_
));
166 const PrefService::Preference
* pref
=
167 pref_service_
->FindPreference(prefs::kURLsToRestoreOnStartup
);
168 scoped_ptr
<base::Value
> merged_value(
169 PrefModelAssociator::MergePreference(pref
->name(),
173 base::ListValue expected
;
174 expected
.Append(new base::StringValue(server_url0_
));
175 expected
.Append(new base::StringValue(server_url1_
));
176 expected
.Append(new base::StringValue(local_url0_
));
177 EXPECT_TRUE(merged_value
->Equals(&expected
));
180 TEST_F(ListPreferenceMergeTest
, Equals
) {
182 ListPrefUpdate
update(pref_service_
, prefs::kURLsToRestoreOnStartup
);
183 base::ListValue
* local_list_value
= update
.Get();
184 local_list_value
->Append(new base::StringValue(server_url0_
));
185 local_list_value
->Append(new base::StringValue(server_url1_
));
188 scoped_ptr
<base::Value
> original(server_url_list_
.DeepCopy());
189 const PrefService::Preference
* pref
=
190 pref_service_
->FindPreference(prefs::kURLsToRestoreOnStartup
);
191 scoped_ptr
<base::Value
> merged_value(
192 PrefModelAssociator::MergePreference(pref
->name(),
195 EXPECT_TRUE(merged_value
->Equals(original
.get()));
198 class DictionaryPreferenceMergeTest
: public AbstractPreferenceMergeTest
{
200 DictionaryPreferenceMergeTest() :
201 expression0_("expression0"),
202 expression1_("expression1"),
203 expression2_("expression2"),
204 content_type0_("content_type0"),
205 content_type1_("content_type1") {}
207 void SetUp() override
{
208 AbstractPreferenceMergeTest::SetUp();
209 SetContentPattern(&server_patterns_
, expression0_
, content_type0_
, 1);
210 SetContentPattern(&server_patterns_
, expression0_
, content_type1_
, 2);
211 SetContentPattern(&server_patterns_
, expression1_
, content_type0_
, 1);
214 std::string expression0_
;
215 std::string expression1_
;
216 std::string expression2_
;
217 std::string content_type0_
;
218 std::string content_type1_
;
219 base::DictionaryValue server_patterns_
;
222 TEST_F(DictionaryPreferenceMergeTest
, LocalEmpty
) {
223 SetPrefToEmpty(prefs::kContentSettingsPatternPairs
);
224 const PrefService::Preference
* pref
=
225 pref_service_
->FindPreference(prefs::kContentSettingsPatternPairs
);
226 scoped_ptr
<base::Value
> merged_value(
227 PrefModelAssociator::MergePreference(pref
->name(),
230 EXPECT_TRUE(merged_value
->Equals(&server_patterns_
));
233 TEST_F(DictionaryPreferenceMergeTest
, ServerNull
) {
234 scoped_ptr
<base::Value
> null_value(base::Value::CreateNullValue());
236 DictionaryPrefUpdate
update(pref_service_
,
237 prefs::kContentSettingsPatternPairs
);
238 base::DictionaryValue
* local_dict_value
= update
.Get();
239 SetContentPattern(local_dict_value
, expression2_
, content_type0_
, 1);
242 const PrefService::Preference
* pref
=
243 pref_service_
->FindPreference(prefs::kContentSettingsPatternPairs
);
244 scoped_ptr
<base::Value
> merged_value(
245 PrefModelAssociator::MergePreference(pref
->name(),
248 const base::DictionaryValue
* local_dict_value
=
249 pref_service_
->GetDictionary(prefs::kContentSettingsPatternPairs
);
250 EXPECT_TRUE(merged_value
->Equals(local_dict_value
));
253 TEST_F(DictionaryPreferenceMergeTest
, ServerEmpty
) {
254 scoped_ptr
<base::Value
> empty_value(new base::DictionaryValue
);
256 DictionaryPrefUpdate
update(pref_service_
,
257 prefs::kContentSettingsPatternPairs
);
258 base::DictionaryValue
* local_dict_value
= update
.Get();
259 SetContentPattern(local_dict_value
, expression2_
, content_type0_
, 1);
262 const PrefService::Preference
* pref
=
263 pref_service_
->FindPreference(prefs::kContentSettingsPatternPairs
);
264 scoped_ptr
<base::Value
> merged_value(
265 PrefModelAssociator::MergePreference(pref
->name(),
268 const base::DictionaryValue
* local_dict_value
=
269 pref_service_
->GetDictionary(prefs::kContentSettingsPatternPairs
);
270 EXPECT_TRUE(merged_value
->Equals(local_dict_value
));
273 TEST_F(DictionaryPreferenceMergeTest
, MergeNoConflicts
) {
275 DictionaryPrefUpdate
update(pref_service_
,
276 prefs::kContentSettingsPatternPairs
);
277 base::DictionaryValue
* local_dict_value
= update
.Get();
278 SetContentPattern(local_dict_value
, expression2_
, content_type0_
, 1);
281 scoped_ptr
<base::Value
> merged_value(PrefModelAssociator::MergePreference(
282 prefs::kContentSettingsPatternPairs
,
283 *pref_service_
->FindPreference(prefs::kContentSettingsPatternPairs
)->
287 base::DictionaryValue expected
;
288 SetContentPattern(&expected
, expression0_
, content_type0_
, 1);
289 SetContentPattern(&expected
, expression0_
, content_type1_
, 2);
290 SetContentPattern(&expected
, expression1_
, content_type0_
, 1);
291 SetContentPattern(&expected
, expression2_
, content_type0_
, 1);
292 EXPECT_TRUE(merged_value
->Equals(&expected
));
295 TEST_F(DictionaryPreferenceMergeTest
, MergeConflicts
) {
297 DictionaryPrefUpdate
update(pref_service_
,
298 prefs::kContentSettingsPatternPairs
);
299 base::DictionaryValue
* local_dict_value
= update
.Get();
300 SetContentPattern(local_dict_value
, expression0_
, content_type0_
, 2);
301 SetContentPattern(local_dict_value
, expression1_
, content_type0_
, 1);
302 SetContentPattern(local_dict_value
, expression1_
, content_type1_
, 1);
303 SetContentPattern(local_dict_value
, expression2_
, content_type0_
, 2);
306 scoped_ptr
<base::Value
> merged_value(PrefModelAssociator::MergePreference(
307 prefs::kContentSettingsPatternPairs
,
308 *pref_service_
->FindPreference(prefs::kContentSettingsPatternPairs
)->
312 base::DictionaryValue expected
;
313 SetContentPattern(&expected
, expression0_
, content_type0_
, 1);
314 SetContentPattern(&expected
, expression0_
, content_type1_
, 2);
315 SetContentPattern(&expected
, expression1_
, content_type0_
, 1);
316 SetContentPattern(&expected
, expression1_
, content_type1_
, 1);
317 SetContentPattern(&expected
, expression2_
, content_type0_
, 2);
318 EXPECT_TRUE(merged_value
->Equals(&expected
));
321 TEST_F(DictionaryPreferenceMergeTest
, Equal
) {
323 DictionaryPrefUpdate
update(pref_service_
,
324 prefs::kContentSettingsPatternPairs
);
325 base::DictionaryValue
* local_dict_value
= update
.Get();
326 SetContentPattern(local_dict_value
, expression0_
, content_type0_
, 1);
327 SetContentPattern(local_dict_value
, expression0_
, content_type1_
, 2);
328 SetContentPattern(local_dict_value
, expression1_
, content_type0_
, 1);
331 scoped_ptr
<base::Value
> merged_value(PrefModelAssociator::MergePreference(
332 prefs::kContentSettingsPatternPairs
,
334 FindPreference(prefs::kContentSettingsPatternPairs
)->GetValue(),
336 EXPECT_TRUE(merged_value
->Equals(&server_patterns_
));
339 TEST_F(DictionaryPreferenceMergeTest
, ConflictButServerWins
) {
341 DictionaryPrefUpdate
update(pref_service_
,
342 prefs::kContentSettingsPatternPairs
);
343 base::DictionaryValue
* local_dict_value
= update
.Get();
344 SetContentPattern(local_dict_value
, expression0_
, content_type0_
, 2);
345 SetContentPattern(local_dict_value
, expression0_
, content_type1_
, 2);
346 SetContentPattern(local_dict_value
, expression1_
, content_type0_
, 1);
349 scoped_ptr
<base::Value
> merged_value(PrefModelAssociator::MergePreference(
350 prefs::kContentSettingsPatternPairs
,
352 FindPreference(prefs::kContentSettingsPatternPairs
)->GetValue(),
354 EXPECT_TRUE(merged_value
->Equals(&server_patterns_
));
357 class IndividualPreferenceMergeTest
: public AbstractPreferenceMergeTest
{
359 IndividualPreferenceMergeTest() :
360 url0_("http://example.com/server0"),
361 url1_("http://example.com/server1"),
362 expression0_("expression0"),
363 expression1_("expression1"),
364 content_type0_("content_type0") {}
366 void SetUp() override
{
367 AbstractPreferenceMergeTest::SetUp();
368 server_url_list_
.Append(new base::StringValue(url0_
));
369 SetContentPattern(&server_patterns_
, expression0_
, content_type0_
, 1);
372 bool MergeListPreference(const char* pref
) {
374 ListPrefUpdate
update(pref_service_
, pref
);
375 base::ListValue
* local_list_value
= update
.Get();
376 local_list_value
->Append(new base::StringValue(url1_
));
379 scoped_ptr
<base::Value
> merged_value(PrefModelAssociator::MergePreference(
381 *pref_service_
->GetUserPrefValue(pref
),
384 base::ListValue expected
;
385 expected
.Append(new base::StringValue(url0_
));
386 expected
.Append(new base::StringValue(url1_
));
387 return merged_value
->Equals(&expected
);
390 bool MergeDictionaryPreference(const char* pref
) {
392 DictionaryPrefUpdate
update(pref_service_
, pref
);
393 base::DictionaryValue
* local_dict_value
= update
.Get();
394 SetContentPattern(local_dict_value
, expression1_
, content_type0_
, 1);
397 scoped_ptr
<base::Value
> merged_value(PrefModelAssociator::MergePreference(
399 *pref_service_
->GetUserPrefValue(pref
),
402 base::DictionaryValue expected
;
403 SetContentPattern(&expected
, expression0_
, content_type0_
, 1);
404 SetContentPattern(&expected
, expression1_
, content_type0_
, 1);
405 return merged_value
->Equals(&expected
);
410 std::string expression0_
;
411 std::string expression1_
;
412 std::string content_type0_
;
413 base::ListValue server_url_list_
;
414 base::DictionaryValue server_patterns_
;
417 TEST_F(IndividualPreferenceMergeTest
, URLsToRestoreOnStartup
) {
418 EXPECT_TRUE(MergeListPreference(prefs::kURLsToRestoreOnStartup
));