OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" | 5 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 #include "base/test/values_test_util.h" | 19 #include "base/test/values_test_util.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "chrome/app/chrome_command_ids.h" | 21 #include "chrome/app/chrome_command_ids.h" |
22 #include "chrome/browser/chrome_notification_types.h" | 22 #include "chrome/browser/chrome_notification_types.h" |
23 #include "chrome/browser/extensions/extension_service_unittest.h" | 23 #include "chrome/browser/extensions/extension_service_unittest.h" |
24 #include "chrome/browser/google/google_util.h" | 24 #include "chrome/browser/google/google_util.h" |
25 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h" | 25 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h" |
26 #include "chrome/browser/profile_resetter/profile_reset_global_error.h" | 26 #include "chrome/browser/profile_resetter/profile_reset_global_error.h" |
| 27 #include "chrome/browser/search_engines/default_search_manager.h" |
27 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 28 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
28 #include "chrome/browser/search_engines/template_url_service.h" | 29 #include "chrome/browser/search_engines/template_url_service.h" |
29 #include "chrome/browser/search_engines/template_url_service_factory.h" | 30 #include "chrome/browser/search_engines/template_url_service_factory.h" |
30 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 31 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
31 #include "chrome/browser/ui/global_error/global_error.h" | 32 #include "chrome/browser/ui/global_error/global_error.h" |
32 #include "chrome/browser/ui/global_error/global_error_service.h" | 33 #include "chrome/browser/ui/global_error/global_error_service.h" |
33 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | 34 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
34 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
35 #include "chrome/test/base/testing_pref_service_syncable.h" | 36 #include "chrome/test/base/testing_pref_service_syncable.h" |
36 #include "chrome/test/base/testing_profile.h" | 37 #include "chrome/test/base/testing_profile.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 base::Closure CreateClosure() { | 85 base::Closure CreateClosure() { |
85 return base::Bind(&MockCallbackTarget::Run, base::Unretained(this)); | 86 return base::Bind(&MockCallbackTarget::Run, base::Unretained(this)); |
86 } | 87 } |
87 | 88 |
88 private: | 89 private: |
89 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget); | 90 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget); |
90 }; | 91 }; |
91 | 92 |
92 // Returns the details of the default search provider from |prefs| in a format | 93 // Returns the details of the default search provider from |prefs| in a format |
93 // suitable for usage as |expected_details| in ExpectDetailsMatch(). | 94 // suitable for usage as |expected_details| in ExpectDetailsMatch(). |
94 scoped_ptr<base::DictionaryValue> GetDefaultSearchProviderDetailsFromPrefs( | 95 const base::DictionaryValue* GetDefaultSearchProviderDetailsFromPrefs( |
95 const PrefService* prefs) { | 96 const PrefService* prefs) { |
96 const char kDefaultSearchProviderPrefix[] = "default_search_provider"; | 97 return prefs->GetDictionary( |
97 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion( | 98 DefaultSearchManager::kDefaultSearchProviderDataPrefName); |
98 prefs->GetPreferenceValues()); | |
99 const base::DictionaryValue* dsp_details = NULL; | |
100 EXPECT_TRUE(pref_values_with_path_expansion->GetDictionary( | |
101 kDefaultSearchProviderPrefix, &dsp_details)); | |
102 return scoped_ptr<base::DictionaryValue>( | |
103 dsp_details ? dsp_details->DeepCopy() : new base::DictionaryValue); | |
104 } | 99 } |
105 | 100 |
106 // Verifies that the |details| of a search engine as provided by the delegate | 101 // Verifies that the |details| of a search engine as provided by the delegate |
107 // are correct in comparison to the |expected_details| coming from the Prefs. | 102 // are correct in comparison to the |expected_details| coming from the Prefs. |
108 void ExpectDetailsMatch(const base::DictionaryValue& expected_details, | 103 void ExpectDetailsMatch(const base::DictionaryValue& expected_details, |
109 const base::DictionaryValue& details) { | 104 const base::DictionaryValue& details) { |
110 for (base::DictionaryValue::Iterator it(expected_details); !it.IsAtEnd(); | 105 for (base::DictionaryValue::Iterator it(expected_details); !it.IsAtEnd(); |
111 it.Advance()) { | 106 it.Advance()) { |
112 SCOPED_TRACE(testing::Message("Key: ") << it.key()); | 107 SCOPED_TRACE(testing::Message("Key: ") << it.key()); |
113 if (it.key() == "enabled" || it.key() == "synced_guid") { | 108 if (it.key() == "enabled" || it.key() == "synced_guid") { |
114 // These attributes should not be present. | 109 // These attributes should not be present. |
115 EXPECT_FALSE(details.HasKey(it.key())); | 110 EXPECT_FALSE(details.HasKey(it.key())); |
116 continue; | 111 continue; |
117 } | 112 } |
118 const base::Value* expected_value = &it.value(); | 113 const base::Value* expected_value = &it.value(); |
119 const base::Value* actual_value = NULL; | 114 const base::Value* actual_value = NULL; |
120 ASSERT_TRUE(details.Get(it.key(), &actual_value)); | 115 ASSERT_TRUE(details.Get(it.key(), &actual_value)); |
121 | 116 |
122 if (it.key() == "id") { | 117 // Ignore ID as it is dynamically assigned by the TemplateURLService. |
123 // Ignore ID as it is dynamically assigned by the TemplateURLService. | 118 // last_modified may get updated during a run, so ignore value differences. |
124 } else if (it.key() == "encodings") { | 119 if (it.key() != "id" && it.key() != "last_modified") { |
125 // Encoding list is stored in Prefs as a single string with tokens | |
126 // delimited by semicolons. | |
127 std::string expected_encodings; | |
128 ASSERT_TRUE(expected_value->GetAsString(&expected_encodings)); | |
129 const base::ListValue* actual_encodings_list = NULL; | |
130 ASSERT_TRUE(actual_value->GetAsList(&actual_encodings_list)); | |
131 std::vector<std::string> actual_encodings_vector; | |
132 for (base::ListValue::const_iterator it = actual_encodings_list->begin(); | |
133 it != actual_encodings_list->end(); ++it) { | |
134 std::string encoding; | |
135 ASSERT_TRUE((*it)->GetAsString(&encoding)); | |
136 actual_encodings_vector.push_back(encoding); | |
137 } | |
138 EXPECT_EQ(expected_encodings, JoinString(actual_encodings_vector, ';')); | |
139 } else { | |
140 // Everything else is the same format. | 120 // Everything else is the same format. |
141 EXPECT_TRUE(actual_value->Equals(expected_value)) | 121 EXPECT_TRUE(actual_value->Equals(expected_value)) |
142 << "Expected: " << *expected_value << ". Actual: " << *actual_value; | 122 << "Expected: " << *expected_value << ". Actual: " << *actual_value; |
143 } | 123 } |
144 } | 124 } |
145 } | 125 } |
146 | 126 |
147 // If |simulate_failure| is false, then replies to the pending request on | 127 // If |simulate_failure| is false, then replies to the pending request on |
148 // |fetcher| with a brandcoded config that only specifies a home page URL. | 128 // |fetcher| with a brandcoded config that only specifies a home page URL. |
149 // If |simulate_failure| is true, replies with 404. | 129 // If |simulate_failure| is true, replies with 404. |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 // TemplateURLService into Prefs. | 352 // TemplateURLService into Prefs. |
373 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); | 353 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); |
374 TemplateURL* custom_dsp = owned_custom_dsp.get(); | 354 TemplateURL* custom_dsp = owned_custom_dsp.get(); |
375 template_url_service->Add(owned_custom_dsp.release()); | 355 template_url_service->Add(owned_custom_dsp.release()); |
376 template_url_service->SetUserSelectedDefaultSearchProvider(custom_dsp); | 356 template_url_service->SetUserSelectedDefaultSearchProvider(custom_dsp); |
377 | 357 |
378 PrefService* prefs = profile()->GetPrefs(); | 358 PrefService* prefs = profile()->GetPrefs(); |
379 ASSERT_TRUE(prefs); | 359 ASSERT_TRUE(prefs); |
380 scoped_ptr<base::DictionaryValue> dsp_details( | 360 scoped_ptr<base::DictionaryValue> dsp_details( |
381 resetter_delegate()->GetDefaultSearchProviderDetails()); | 361 resetter_delegate()->GetDefaultSearchProviderDetails()); |
382 scoped_ptr<base::DictionaryValue> expected_dsp_details( | 362 const base::DictionaryValue* expected_dsp_details = |
383 GetDefaultSearchProviderDetailsFromPrefs(prefs)); | 363 GetDefaultSearchProviderDetailsFromPrefs(prefs); |
384 | 364 |
385 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); | 365 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); |
386 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 366 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
387 } | 367 } |
388 | 368 |
389 TEST_F(AutomaticProfileResetterDelegateTest, | 369 TEST_F(AutomaticProfileResetterDelegateTest, |
390 DefaultSearchProviderDataWhenManaged) { | 370 DefaultSearchProviderDataWhenManaged) { |
391 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; | 371 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; |
392 const char kTestName[] = "name"; | 372 const char kTestName[] = "name"; |
393 const char kTestKeyword[] = "keyword"; | 373 const char kTestKeyword[] = "keyword"; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 ASSERT_TRUE(details->GetString("keyword", &keyword)); | 426 ASSERT_TRUE(details->GetString("keyword", &keyword)); |
447 TemplateURL* search_engine = | 427 TemplateURL* search_engine = |
448 template_url_service->GetTemplateURLForKeyword( | 428 template_url_service->GetTemplateURLForKeyword( |
449 base::ASCIIToUTF16(keyword)); | 429 base::ASCIIToUTF16(keyword)); |
450 ASSERT_TRUE(search_engine); | 430 ASSERT_TRUE(search_engine); |
451 template_url_service->SetUserSelectedDefaultSearchProvider( | 431 template_url_service->SetUserSelectedDefaultSearchProvider( |
452 prepopulated_engines[i]); | 432 prepopulated_engines[i]); |
453 | 433 |
454 PrefService* prefs = profile()->GetPrefs(); | 434 PrefService* prefs = profile()->GetPrefs(); |
455 ASSERT_TRUE(prefs); | 435 ASSERT_TRUE(prefs); |
456 scoped_ptr<base::DictionaryValue> expected_dsp_details( | 436 const base::DictionaryValue* expected_dsp_details = |
457 GetDefaultSearchProviderDetailsFromPrefs(prefs)); | 437 GetDefaultSearchProviderDetailsFromPrefs(prefs); |
458 ExpectDetailsMatch(*expected_dsp_details, *details); | 438 ExpectDetailsMatch(*expected_dsp_details, *details); |
459 } | 439 } |
460 } | 440 } |
461 | 441 |
462 TEST_F(AutomaticProfileResetterDelegateTest, | 442 TEST_F(AutomaticProfileResetterDelegateTest, |
463 FetchAndWaitOnDefaultSettingsVanilla) { | 443 FetchAndWaitOnDefaultSettingsVanilla) { |
464 google_util::BrandForTesting scoped_brand_for_testing((std::string())); | 444 google_util::BrandForTesting scoped_brand_for_testing((std::string())); |
465 | 445 |
466 // Expect ready_callback to be called just after empty brandcoded settings | 446 // Expect ready_callback to be called just after empty brandcoded settings |
467 // are loaded, given this is a vanilla build. Fail if it is not called, or | 447 // are loaded, given this is a vanilla build. Fail if it is not called, or |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 if (ProfileResetGlobalError::IsSupportedOnPlatform()) | 595 if (ProfileResetGlobalError::IsSupportedOnPlatform()) |
616 ExpectResetPromptState(true /*active*/); | 596 ExpectResetPromptState(true /*active*/); |
617 else | 597 else |
618 ExpectResetPromptState(false /*active*/); | 598 ExpectResetPromptState(false /*active*/); |
619 resetter_delegate()->DismissPrompt(); | 599 resetter_delegate()->DismissPrompt(); |
620 ExpectResetPromptState(false /*active*/); | 600 ExpectResetPromptState(false /*active*/); |
621 resetter_delegate()->DismissPrompt(); | 601 resetter_delegate()->DismissPrompt(); |
622 } | 602 } |
623 | 603 |
624 } // namespace | 604 } // namespace |
OLD | NEW |