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" | |
28 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 27 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
29 #include "chrome/browser/search_engines/template_url_service.h" | 28 #include "chrome/browser/search_engines/template_url_service.h" |
30 #include "chrome/browser/search_engines/template_url_service_factory.h" | 29 #include "chrome/browser/search_engines/template_url_service_factory.h" |
31 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 30 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
32 #include "chrome/browser/ui/global_error/global_error.h" | 31 #include "chrome/browser/ui/global_error/global_error.h" |
33 #include "chrome/browser/ui/global_error/global_error_service.h" | 32 #include "chrome/browser/ui/global_error/global_error_service.h" |
34 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | 33 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
35 #include "chrome/common/pref_names.h" | 34 #include "chrome/common/pref_names.h" |
36 #include "chrome/test/base/testing_pref_service_syncable.h" | 35 #include "chrome/test/base/testing_pref_service_syncable.h" |
37 #include "chrome/test/base/testing_profile.h" | 36 #include "chrome/test/base/testing_profile.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 base::Closure CreateClosure() { | 84 base::Closure CreateClosure() { |
86 return base::Bind(&MockCallbackTarget::Run, base::Unretained(this)); | 85 return base::Bind(&MockCallbackTarget::Run, base::Unretained(this)); |
87 } | 86 } |
88 | 87 |
89 private: | 88 private: |
90 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget); | 89 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget); |
91 }; | 90 }; |
92 | 91 |
93 // Returns the details of the default search provider from |prefs| in a format | 92 // Returns the details of the default search provider from |prefs| in a format |
94 // suitable for usage as |expected_details| in ExpectDetailsMatch(). | 93 // suitable for usage as |expected_details| in ExpectDetailsMatch(). |
95 const base::DictionaryValue* GetDefaultSearchProviderDetailsFromPrefs( | 94 scoped_ptr<base::DictionaryValue> GetDefaultSearchProviderDetailsFromPrefs( |
96 const PrefService* prefs) { | 95 const PrefService* prefs) { |
97 return prefs->GetDictionary( | 96 const char kDefaultSearchProviderPrefix[] = "default_search_provider"; |
98 DefaultSearchManager::kDefaultSearchProviderDataPrefName); | 97 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion( |
| 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); |
99 } | 104 } |
100 | 105 |
101 // Verifies that the |details| of a search engine as provided by the delegate | 106 // Verifies that the |details| of a search engine as provided by the delegate |
102 // are correct in comparison to the |expected_details| coming from the Prefs. | 107 // are correct in comparison to the |expected_details| coming from the Prefs. |
103 void ExpectDetailsMatch(const base::DictionaryValue& expected_details, | 108 void ExpectDetailsMatch(const base::DictionaryValue& expected_details, |
104 const base::DictionaryValue& details) { | 109 const base::DictionaryValue& details) { |
105 for (base::DictionaryValue::Iterator it(expected_details); !it.IsAtEnd(); | 110 for (base::DictionaryValue::Iterator it(expected_details); !it.IsAtEnd(); |
106 it.Advance()) { | 111 it.Advance()) { |
107 SCOPED_TRACE(testing::Message("Key: ") << it.key()); | 112 SCOPED_TRACE(testing::Message("Key: ") << it.key()); |
108 if (it.key() == "enabled" || it.key() == "synced_guid") { | 113 if (it.key() == "enabled" || it.key() == "synced_guid") { |
109 // These attributes should not be present. | 114 // These attributes should not be present. |
110 EXPECT_FALSE(details.HasKey(it.key())); | 115 EXPECT_FALSE(details.HasKey(it.key())); |
111 continue; | 116 continue; |
112 } | 117 } |
113 const base::Value* expected_value = &it.value(); | 118 const base::Value* expected_value = &it.value(); |
114 const base::Value* actual_value = NULL; | 119 const base::Value* actual_value = NULL; |
115 ASSERT_TRUE(details.Get(it.key(), &actual_value)); | 120 ASSERT_TRUE(details.Get(it.key(), &actual_value)); |
116 | 121 |
117 // Ignore ID as it is dynamically assigned by the TemplateURLService. | 122 if (it.key() == "id") { |
118 // last_modified may get updated during a run, so ignore value differences. | 123 // Ignore ID as it is dynamically assigned by the TemplateURLService. |
119 if (it.key() != "id" && it.key() != "last_modified") { | 124 } else if (it.key() == "encodings") { |
| 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 { |
120 // Everything else is the same format. | 140 // Everything else is the same format. |
121 EXPECT_TRUE(actual_value->Equals(expected_value)) | 141 EXPECT_TRUE(actual_value->Equals(expected_value)) |
122 << "Expected: " << *expected_value << ". Actual: " << *actual_value; | 142 << "Expected: " << *expected_value << ". Actual: " << *actual_value; |
123 } | 143 } |
124 } | 144 } |
125 } | 145 } |
126 | 146 |
127 // If |simulate_failure| is false, then replies to the pending request on | 147 // If |simulate_failure| is false, then replies to the pending request on |
128 // |fetcher| with a brandcoded config that only specifies a home page URL. | 148 // |fetcher| with a brandcoded config that only specifies a home page URL. |
129 // If |simulate_failure| is true, replies with 404. | 149 // If |simulate_failure| is true, replies with 404. |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 // TemplateURLService into Prefs. | 372 // TemplateURLService into Prefs. |
353 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); | 373 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); |
354 TemplateURL* custom_dsp = owned_custom_dsp.get(); | 374 TemplateURL* custom_dsp = owned_custom_dsp.get(); |
355 template_url_service->Add(owned_custom_dsp.release()); | 375 template_url_service->Add(owned_custom_dsp.release()); |
356 template_url_service->SetUserSelectedDefaultSearchProvider(custom_dsp); | 376 template_url_service->SetUserSelectedDefaultSearchProvider(custom_dsp); |
357 | 377 |
358 PrefService* prefs = profile()->GetPrefs(); | 378 PrefService* prefs = profile()->GetPrefs(); |
359 ASSERT_TRUE(prefs); | 379 ASSERT_TRUE(prefs); |
360 scoped_ptr<base::DictionaryValue> dsp_details( | 380 scoped_ptr<base::DictionaryValue> dsp_details( |
361 resetter_delegate()->GetDefaultSearchProviderDetails()); | 381 resetter_delegate()->GetDefaultSearchProviderDetails()); |
362 const base::DictionaryValue* expected_dsp_details = | 382 scoped_ptr<base::DictionaryValue> expected_dsp_details( |
363 GetDefaultSearchProviderDetailsFromPrefs(prefs); | 383 GetDefaultSearchProviderDetailsFromPrefs(prefs)); |
364 | 384 |
365 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); | 385 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); |
366 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 386 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
367 } | 387 } |
368 | 388 |
369 TEST_F(AutomaticProfileResetterDelegateTest, | 389 TEST_F(AutomaticProfileResetterDelegateTest, |
370 DefaultSearchProviderDataWhenManaged) { | 390 DefaultSearchProviderDataWhenManaged) { |
371 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; | 391 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; |
372 const char kTestName[] = "name"; | 392 const char kTestName[] = "name"; |
373 const char kTestKeyword[] = "keyword"; | 393 const char kTestKeyword[] = "keyword"; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 ASSERT_TRUE(details->GetString("keyword", &keyword)); | 446 ASSERT_TRUE(details->GetString("keyword", &keyword)); |
427 TemplateURL* search_engine = | 447 TemplateURL* search_engine = |
428 template_url_service->GetTemplateURLForKeyword( | 448 template_url_service->GetTemplateURLForKeyword( |
429 base::ASCIIToUTF16(keyword)); | 449 base::ASCIIToUTF16(keyword)); |
430 ASSERT_TRUE(search_engine); | 450 ASSERT_TRUE(search_engine); |
431 template_url_service->SetUserSelectedDefaultSearchProvider( | 451 template_url_service->SetUserSelectedDefaultSearchProvider( |
432 prepopulated_engines[i]); | 452 prepopulated_engines[i]); |
433 | 453 |
434 PrefService* prefs = profile()->GetPrefs(); | 454 PrefService* prefs = profile()->GetPrefs(); |
435 ASSERT_TRUE(prefs); | 455 ASSERT_TRUE(prefs); |
436 const base::DictionaryValue* expected_dsp_details = | 456 scoped_ptr<base::DictionaryValue> expected_dsp_details( |
437 GetDefaultSearchProviderDetailsFromPrefs(prefs); | 457 GetDefaultSearchProviderDetailsFromPrefs(prefs)); |
438 ExpectDetailsMatch(*expected_dsp_details, *details); | 458 ExpectDetailsMatch(*expected_dsp_details, *details); |
439 } | 459 } |
440 } | 460 } |
441 | 461 |
442 TEST_F(AutomaticProfileResetterDelegateTest, | 462 TEST_F(AutomaticProfileResetterDelegateTest, |
443 FetchAndWaitOnDefaultSettingsVanilla) { | 463 FetchAndWaitOnDefaultSettingsVanilla) { |
444 google_util::BrandForTesting scoped_brand_for_testing((std::string())); | 464 google_util::BrandForTesting scoped_brand_for_testing((std::string())); |
445 | 465 |
446 // Expect ready_callback to be called just after empty brandcoded settings | 466 // Expect ready_callback to be called just after empty brandcoded settings |
447 // are loaded, given this is a vanilla build. Fail if it is not called, or | 467 // 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... |
595 if (ProfileResetGlobalError::IsSupportedOnPlatform()) | 615 if (ProfileResetGlobalError::IsSupportedOnPlatform()) |
596 ExpectResetPromptState(true /*active*/); | 616 ExpectResetPromptState(true /*active*/); |
597 else | 617 else |
598 ExpectResetPromptState(false /*active*/); | 618 ExpectResetPromptState(false /*active*/); |
599 resetter_delegate()->DismissPrompt(); | 619 resetter_delegate()->DismissPrompt(); |
600 ExpectResetPromptState(false /*active*/); | 620 ExpectResetPromptState(false /*active*/); |
601 resetter_delegate()->DismissPrompt(); | 621 resetter_delegate()->DismissPrompt(); |
602 } | 622 } |
603 | 623 |
604 } // namespace | 624 } // namespace |
OLD | NEW |