Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / prefs / pref_model_associator_unittest.cc
blob520eea8c92cbd3c814173d3637d4de834c80d58d
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, MergeValueToDictionary) {
321 base::DictionaryValue local_dict_value;
322 local_dict_value.SetInteger("key", 0);
324 base::DictionaryValue server_dict_value;
325 server_dict_value.SetInteger("key.subkey", 0);
327 scoped_ptr<base::Value> merged_value(PrefModelAssociator::MergePreference(
328 kContentSettingsPrefName,
329 local_dict_value,
330 server_dict_value));
332 EXPECT_TRUE(merged_value->Equals(&server_dict_value));
335 TEST_F(DictionaryPreferenceMergeTest, Equal) {
337 DictionaryPrefUpdate update(pref_service_, kContentSettingsPrefName);
338 base::DictionaryValue* local_dict_value = update.Get();
339 SetContentPattern(local_dict_value, expression0_, 1);
340 SetContentPattern(local_dict_value, expression1_, 2);
341 SetContentPattern(local_dict_value, expression2_, 1);
344 scoped_ptr<base::Value> merged_value(PrefModelAssociator::MergePreference(
345 kContentSettingsPrefName,
346 *pref_service_->FindPreference(kContentSettingsPrefName)->GetValue(),
347 server_patterns_));
348 EXPECT_TRUE(merged_value->Equals(&server_patterns_));
351 TEST_F(DictionaryPreferenceMergeTest, ConflictButServerWins) {
353 DictionaryPrefUpdate update(pref_service_, kContentSettingsPrefName);
354 base::DictionaryValue* local_dict_value = update.Get();
355 SetContentPattern(local_dict_value, expression0_, 2);
356 SetContentPattern(local_dict_value, expression1_, 2);
357 SetContentPattern(local_dict_value, expression2_, 1);
360 scoped_ptr<base::Value> merged_value(PrefModelAssociator::MergePreference(
361 kContentSettingsPrefName,
362 *pref_service_->FindPreference(kContentSettingsPrefName)->GetValue(),
363 server_patterns_));
364 EXPECT_TRUE(merged_value->Equals(&server_patterns_));
367 class IndividualPreferenceMergeTest : public AbstractPreferenceMergeTest {
368 protected:
369 IndividualPreferenceMergeTest() :
370 url0_("http://example.com/server0"),
371 url1_("http://example.com/server1"),
372 expression0_("expression0"),
373 expression1_("expression1") {}
375 void SetUp() override {
376 AbstractPreferenceMergeTest::SetUp();
377 server_url_list_.Append(new base::StringValue(url0_));
378 SetContentPattern(&server_patterns_, expression0_, 1);
381 bool MergeListPreference(const char* pref) {
383 ListPrefUpdate update(pref_service_, pref);
384 base::ListValue* local_list_value = update.Get();
385 local_list_value->Append(new base::StringValue(url1_));
388 scoped_ptr<base::Value> merged_value(PrefModelAssociator::MergePreference(
389 pref,
390 *pref_service_->GetUserPrefValue(pref),
391 server_url_list_));
393 base::ListValue expected;
394 expected.Append(new base::StringValue(url0_));
395 expected.Append(new base::StringValue(url1_));
396 return merged_value->Equals(&expected);
399 bool MergeDictionaryPreference(const char* pref) {
401 DictionaryPrefUpdate update(pref_service_, pref);
402 base::DictionaryValue* local_dict_value = update.Get();
403 SetContentPattern(local_dict_value, expression1_, 1);
406 scoped_ptr<base::Value> merged_value(PrefModelAssociator::MergePreference(
407 pref,
408 *pref_service_->GetUserPrefValue(pref),
409 server_patterns_));
411 base::DictionaryValue expected;
412 SetContentPattern(&expected, expression0_, 1);
413 SetContentPattern(&expected, expression1_, 1);
414 return merged_value->Equals(&expected);
417 std::string url0_;
418 std::string url1_;
419 std::string expression0_;
420 std::string expression1_;
421 std::string content_type0_;
422 base::ListValue server_url_list_;
423 base::DictionaryValue server_patterns_;
426 TEST_F(IndividualPreferenceMergeTest, URLsToRestoreOnStartup) {
427 EXPECT_TRUE(MergeListPreference(prefs::kURLsToRestoreOnStartup));