Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / prefs / pref_model_associator_unittest.cc
blobc6aa3d53fe1cf3bc9e12653581b80f7d0425e72f
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 "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 // Any content settings pref name should do for these tests.
15 const char* kContentSettingsPrefName =
16 "profile.content_settings.exceptions.cookies";
18 class AbstractPreferenceMergeTest : public testing::Test {
19 protected:
20 void SetUp() override { pref_service_ = profile_.GetPrefs(); }
22 void SetContentPattern(base::DictionaryValue* patterns_dict,
23 const std::string& expression,
24 int setting) {
25 base::DictionaryValue* expression_dict;
26 bool found =
27 patterns_dict->GetDictionaryWithoutPathExpansion(expression,
28 &expression_dict);
29 if (!found) {
30 expression_dict = new base::DictionaryValue;
31 patterns_dict->SetWithoutPathExpansion(expression, expression_dict);
33 expression_dict->SetWithoutPathExpansion(
34 "setting", new base::FundamentalValue(setting));
37 void SetPrefToEmpty(const std::string& pref_name) {
38 scoped_ptr<base::Value> empty_value;
39 const PrefService::Preference* pref =
40 pref_service_->FindPreference(pref_name.c_str());
41 ASSERT_TRUE(pref);
42 base::Value::Type type = pref->GetType();
43 if (type == base::Value::TYPE_DICTIONARY)
44 empty_value.reset(new base::DictionaryValue);
45 else if (type == base::Value::TYPE_LIST)
46 empty_value.reset(new base::ListValue);
47 else
48 FAIL();
49 pref_service_->Set(pref_name.c_str(), *empty_value);
52 content::TestBrowserThreadBundle thread_bundle_;
53 TestingProfile profile_;
54 PrefService* pref_service_;
57 class ListPreferenceMergeTest : public AbstractPreferenceMergeTest {
58 protected:
59 ListPreferenceMergeTest() :
60 server_url0_("http://example.com/server0"),
61 server_url1_("http://example.com/server1"),
62 local_url0_("http://example.com/local0"),
63 local_url1_("http://example.com/local1") {}
65 void SetUp() override {
66 AbstractPreferenceMergeTest::SetUp();
67 server_url_list_.Append(new base::StringValue(server_url0_));
68 server_url_list_.Append(new base::StringValue(server_url1_));
71 std::string server_url0_;
72 std::string server_url1_;
73 std::string local_url0_;
74 std::string local_url1_;
75 base::ListValue server_url_list_;
78 TEST_F(ListPreferenceMergeTest, NotListOrDictionary) {
79 pref_service_->SetString(prefs::kHomePage, local_url0_);
80 const PrefService::Preference* pref =
81 pref_service_->FindPreference(prefs::kHomePage);
82 scoped_ptr<base::Value> server_value(new base::StringValue(server_url0_));
83 scoped_ptr<base::Value> merged_value(
84 PrefModelAssociator::MergePreference(pref->name(),
85 *pref->GetValue(),
86 *server_value));
87 EXPECT_TRUE(merged_value->Equals(server_value.get()));
90 TEST_F(ListPreferenceMergeTest, LocalEmpty) {
91 SetPrefToEmpty(prefs::kURLsToRestoreOnStartup);
92 const PrefService::Preference* pref =
93 pref_service_->FindPreference(prefs::kURLsToRestoreOnStartup);
94 scoped_ptr<base::Value> merged_value(
95 PrefModelAssociator::MergePreference(pref->name(),
96 *pref->GetValue(),
97 server_url_list_));
98 EXPECT_TRUE(merged_value->Equals(&server_url_list_));
101 TEST_F(ListPreferenceMergeTest, ServerNull) {
102 scoped_ptr<base::Value> null_value = base::Value::CreateNullValue();
104 ListPrefUpdate update(pref_service_, prefs::kURLsToRestoreOnStartup);
105 base::ListValue* local_list_value = update.Get();
106 local_list_value->Append(new base::StringValue(local_url0_));
109 const PrefService::Preference* pref =
110 pref_service_->FindPreference(prefs::kURLsToRestoreOnStartup);
111 scoped_ptr<base::Value> merged_value(
112 PrefModelAssociator::MergePreference(pref->name(),
113 *pref->GetValue(),
114 *null_value));
115 const base::ListValue* local_list_value =
116 pref_service_->GetList(prefs::kURLsToRestoreOnStartup);
117 EXPECT_TRUE(merged_value->Equals(local_list_value));
120 TEST_F(ListPreferenceMergeTest, ServerEmpty) {
121 scoped_ptr<base::Value> empty_value(new base::ListValue);
123 ListPrefUpdate update(pref_service_, prefs::kURLsToRestoreOnStartup);
124 base::ListValue* local_list_value = update.Get();
125 local_list_value->Append(new base::StringValue(local_url0_));
128 const PrefService::Preference* pref =
129 pref_service_->FindPreference(prefs::kURLsToRestoreOnStartup);
130 scoped_ptr<base::Value> merged_value(
131 PrefModelAssociator::MergePreference(pref->name(),
132 *pref->GetValue(),
133 *empty_value));
134 const base::ListValue* local_list_value =
135 pref_service_->GetList(prefs::kURLsToRestoreOnStartup);
136 EXPECT_TRUE(merged_value->Equals(local_list_value));
139 TEST_F(ListPreferenceMergeTest, Merge) {
141 ListPrefUpdate update(pref_service_, prefs::kURLsToRestoreOnStartup);
142 base::ListValue* local_list_value = update.Get();
143 local_list_value->Append(new base::StringValue(local_url0_));
144 local_list_value->Append(new base::StringValue(local_url1_));
147 const PrefService::Preference* pref =
148 pref_service_->FindPreference(prefs::kURLsToRestoreOnStartup);
149 scoped_ptr<base::Value> merged_value(
150 PrefModelAssociator::MergePreference(pref->name(),
151 *pref->GetValue(),
152 server_url_list_));
154 base::ListValue expected;
155 expected.Append(new base::StringValue(server_url0_));
156 expected.Append(new base::StringValue(server_url1_));
157 expected.Append(new base::StringValue(local_url0_));
158 expected.Append(new base::StringValue(local_url1_));
159 EXPECT_TRUE(merged_value->Equals(&expected));
162 TEST_F(ListPreferenceMergeTest, Duplicates) {
164 ListPrefUpdate update(pref_service_, prefs::kURLsToRestoreOnStartup);
165 base::ListValue* local_list_value = update.Get();
166 local_list_value->Append(new base::StringValue(local_url0_));
167 local_list_value->Append(new base::StringValue(server_url0_));
168 local_list_value->Append(new base::StringValue(server_url1_));
171 const PrefService::Preference* pref =
172 pref_service_->FindPreference(prefs::kURLsToRestoreOnStartup);
173 scoped_ptr<base::Value> merged_value(
174 PrefModelAssociator::MergePreference(pref->name(),
175 *pref->GetValue(),
176 server_url_list_));
178 base::ListValue expected;
179 expected.Append(new base::StringValue(server_url0_));
180 expected.Append(new base::StringValue(server_url1_));
181 expected.Append(new base::StringValue(local_url0_));
182 EXPECT_TRUE(merged_value->Equals(&expected));
185 TEST_F(ListPreferenceMergeTest, Equals) {
187 ListPrefUpdate update(pref_service_, prefs::kURLsToRestoreOnStartup);
188 base::ListValue* local_list_value = update.Get();
189 local_list_value->Append(new base::StringValue(server_url0_));
190 local_list_value->Append(new base::StringValue(server_url1_));
193 scoped_ptr<base::Value> original(server_url_list_.DeepCopy());
194 const PrefService::Preference* pref =
195 pref_service_->FindPreference(prefs::kURLsToRestoreOnStartup);
196 scoped_ptr<base::Value> merged_value(
197 PrefModelAssociator::MergePreference(pref->name(),
198 *pref->GetValue(),
199 server_url_list_));
200 EXPECT_TRUE(merged_value->Equals(original.get()));
203 class DictionaryPreferenceMergeTest : public AbstractPreferenceMergeTest {
204 protected:
205 DictionaryPreferenceMergeTest() :
206 expression0_("expression0"),
207 expression1_("expression1"),
208 expression2_("expression2"),
209 expression3_("expression3"),
210 expression4_("expression4") {}
212 void SetUp() override {
213 AbstractPreferenceMergeTest::SetUp();
214 SetContentPattern(&server_patterns_, expression0_, 1);
215 SetContentPattern(&server_patterns_, expression1_, 2);
216 SetContentPattern(&server_patterns_, expression2_, 1);
219 std::string expression0_;
220 std::string expression1_;
221 std::string expression2_;
222 std::string expression3_;
223 std::string expression4_;
224 base::DictionaryValue server_patterns_;
227 TEST_F(DictionaryPreferenceMergeTest, LocalEmpty) {
228 SetPrefToEmpty(kContentSettingsPrefName);
229 const PrefService::Preference* pref =
230 pref_service_->FindPreference(kContentSettingsPrefName);
231 scoped_ptr<base::Value> merged_value(
232 PrefModelAssociator::MergePreference(pref->name(),
233 *pref->GetValue(),
234 server_patterns_));
235 EXPECT_TRUE(merged_value->Equals(&server_patterns_));
238 TEST_F(DictionaryPreferenceMergeTest, ServerNull) {
239 scoped_ptr<base::Value> null_value = base::Value::CreateNullValue();
241 DictionaryPrefUpdate update(pref_service_, kContentSettingsPrefName);
242 base::DictionaryValue* local_dict_value = update.Get();
243 SetContentPattern(local_dict_value, expression3_, 1);
246 const PrefService::Preference* pref =
247 pref_service_->FindPreference(kContentSettingsPrefName);
248 scoped_ptr<base::Value> merged_value(
249 PrefModelAssociator::MergePreference(pref->name(),
250 *pref->GetValue(),
251 *null_value));
252 const base::DictionaryValue* local_dict_value =
253 pref_service_->GetDictionary(kContentSettingsPrefName);
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_, kContentSettingsPrefName);
261 base::DictionaryValue* local_dict_value = update.Get();
262 SetContentPattern(local_dict_value, expression3_, 1);
265 const PrefService::Preference* pref =
266 pref_service_->FindPreference(kContentSettingsPrefName);
267 scoped_ptr<base::Value> merged_value(
268 PrefModelAssociator::MergePreference(pref->name(),
269 *pref->GetValue(),
270 *empty_value));
271 const base::DictionaryValue* local_dict_value =
272 pref_service_->GetDictionary(kContentSettingsPrefName);
273 EXPECT_TRUE(merged_value->Equals(local_dict_value));
276 TEST_F(DictionaryPreferenceMergeTest, MergeNoConflicts) {
278 DictionaryPrefUpdate update(pref_service_, kContentSettingsPrefName);
279 base::DictionaryValue* local_dict_value = update.Get();
280 SetContentPattern(local_dict_value, expression3_, 1);
283 scoped_ptr<base::Value> merged_value(PrefModelAssociator::MergePreference(
284 kContentSettingsPrefName,
285 *pref_service_->FindPreference(kContentSettingsPrefName)->GetValue(),
286 server_patterns_));
288 base::DictionaryValue expected;
289 SetContentPattern(&expected, expression0_, 1);
290 SetContentPattern(&expected, expression1_, 2);
291 SetContentPattern(&expected, expression2_, 1);
292 SetContentPattern(&expected, expression3_, 1);
293 EXPECT_TRUE(merged_value->Equals(&expected));
296 TEST_F(DictionaryPreferenceMergeTest, MergeConflicts) {
298 DictionaryPrefUpdate update(pref_service_, kContentSettingsPrefName);
299 base::DictionaryValue* local_dict_value = update.Get();
300 SetContentPattern(local_dict_value, expression0_, 2);
301 SetContentPattern(local_dict_value, expression2_, 1);
302 SetContentPattern(local_dict_value, expression3_, 1);
303 SetContentPattern(local_dict_value, expression4_, 2);
306 scoped_ptr<base::Value> merged_value(PrefModelAssociator::MergePreference(
307 kContentSettingsPrefName,
308 *pref_service_->FindPreference(kContentSettingsPrefName)->GetValue(),
309 server_patterns_));
311 base::DictionaryValue expected;
312 SetContentPattern(&expected, expression0_, 1);
313 SetContentPattern(&expected, expression1_, 2);
314 SetContentPattern(&expected, expression2_, 1);
315 SetContentPattern(&expected, expression3_, 1);
316 SetContentPattern(&expected, expression4_, 2);
317 EXPECT_TRUE(merged_value->Equals(&expected));
320 TEST_F(DictionaryPreferenceMergeTest, Equal) {
322 DictionaryPrefUpdate update(pref_service_, kContentSettingsPrefName);
323 base::DictionaryValue* local_dict_value = update.Get();
324 SetContentPattern(local_dict_value, expression0_, 1);
325 SetContentPattern(local_dict_value, expression1_, 2);
326 SetContentPattern(local_dict_value, expression2_, 1);
329 scoped_ptr<base::Value> merged_value(PrefModelAssociator::MergePreference(
330 kContentSettingsPrefName,
331 *pref_service_->FindPreference(kContentSettingsPrefName)->GetValue(),
332 server_patterns_));
333 EXPECT_TRUE(merged_value->Equals(&server_patterns_));
336 TEST_F(DictionaryPreferenceMergeTest, ConflictButServerWins) {
338 DictionaryPrefUpdate update(pref_service_, kContentSettingsPrefName);
339 base::DictionaryValue* local_dict_value = update.Get();
340 SetContentPattern(local_dict_value, expression0_, 2);
341 SetContentPattern(local_dict_value, expression1_, 2);
342 SetContentPattern(local_dict_value, expression2_, 1);
345 scoped_ptr<base::Value> merged_value(PrefModelAssociator::MergePreference(
346 kContentSettingsPrefName,
347 *pref_service_->FindPreference(kContentSettingsPrefName)->GetValue(),
348 server_patterns_));
349 EXPECT_TRUE(merged_value->Equals(&server_patterns_));
352 class IndividualPreferenceMergeTest : public AbstractPreferenceMergeTest {
353 protected:
354 IndividualPreferenceMergeTest() :
355 url0_("http://example.com/server0"),
356 url1_("http://example.com/server1"),
357 expression0_("expression0"),
358 expression1_("expression1") {}
360 void SetUp() override {
361 AbstractPreferenceMergeTest::SetUp();
362 server_url_list_.Append(new base::StringValue(url0_));
363 SetContentPattern(&server_patterns_, expression0_, 1);
366 bool MergeListPreference(const char* pref) {
368 ListPrefUpdate update(pref_service_, pref);
369 base::ListValue* local_list_value = update.Get();
370 local_list_value->Append(new base::StringValue(url1_));
373 scoped_ptr<base::Value> merged_value(PrefModelAssociator::MergePreference(
374 pref,
375 *pref_service_->GetUserPrefValue(pref),
376 server_url_list_));
378 base::ListValue expected;
379 expected.Append(new base::StringValue(url0_));
380 expected.Append(new base::StringValue(url1_));
381 return merged_value->Equals(&expected);
384 bool MergeDictionaryPreference(const char* pref) {
386 DictionaryPrefUpdate update(pref_service_, pref);
387 base::DictionaryValue* local_dict_value = update.Get();
388 SetContentPattern(local_dict_value, expression1_, 1);
391 scoped_ptr<base::Value> merged_value(PrefModelAssociator::MergePreference(
392 pref,
393 *pref_service_->GetUserPrefValue(pref),
394 server_patterns_));
396 base::DictionaryValue expected;
397 SetContentPattern(&expected, expression0_, 1);
398 SetContentPattern(&expected, expression1_, 1);
399 return merged_value->Equals(&expected);
402 std::string url0_;
403 std::string url1_;
404 std::string expression0_;
405 std::string expression1_;
406 std::string content_type0_;
407 base::ListValue server_url_list_;
408 base::DictionaryValue server_patterns_;
411 TEST_F(IndividualPreferenceMergeTest, URLsToRestoreOnStartup) {
412 EXPECT_TRUE(MergeListPreference(prefs::kURLsToRestoreOnStartup));