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 if (it.key() == "id" || it.key() == "last_modified") { |
123 // Ignore ID as it is dynamically assigned by the TemplateURLService. | 118 // Ignore ID as it is dynamically assigned by the TemplateURLService. |
124 } else if (it.key() == "encodings") { | 119 // last_modified may get updated during a run, so ignore value |
125 // Encoding list is stored in Prefs as a single string with tokens | 120 // differences. |
Peter Kasting
2014/05/07 23:38:29
Nit: Move the comments in both these arms above th
erikwright (departed)
2014/05/08 12:46:24
Done.
| |
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 { | 121 } else { |
140 // Everything else is the same format. | 122 // Everything else is the same format. |
141 EXPECT_TRUE(actual_value->Equals(expected_value)) | 123 EXPECT_TRUE(actual_value->Equals(expected_value)) |
142 << "Expected: " << *expected_value << ". Actual: " << *actual_value; | 124 << "Expected: " << *expected_value << ". Actual: " << *actual_value; |
143 } | 125 } |
144 } | 126 } |
145 } | 127 } |
146 | 128 |
147 // If |simulate_failure| is false, then replies to the pending request on | 129 // 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. | 130 // |fetcher| with a brandcoded config that only specifies a home page URL. |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
372 // TemplateURLService into Prefs. | 354 // TemplateURLService into Prefs. |
373 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); | 355 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); |
374 TemplateURL* custom_dsp = owned_custom_dsp.get(); | 356 TemplateURL* custom_dsp = owned_custom_dsp.get(); |
375 template_url_service->Add(owned_custom_dsp.release()); | 357 template_url_service->Add(owned_custom_dsp.release()); |
376 template_url_service->SetUserSelectedDefaultSearchProvider(custom_dsp); | 358 template_url_service->SetUserSelectedDefaultSearchProvider(custom_dsp); |
377 | 359 |
378 PrefService* prefs = profile()->GetPrefs(); | 360 PrefService* prefs = profile()->GetPrefs(); |
379 ASSERT_TRUE(prefs); | 361 ASSERT_TRUE(prefs); |
380 scoped_ptr<base::DictionaryValue> dsp_details( | 362 scoped_ptr<base::DictionaryValue> dsp_details( |
381 resetter_delegate()->GetDefaultSearchProviderDetails()); | 363 resetter_delegate()->GetDefaultSearchProviderDetails()); |
382 scoped_ptr<base::DictionaryValue> expected_dsp_details( | 364 const base::DictionaryValue* expected_dsp_details = |
383 GetDefaultSearchProviderDetailsFromPrefs(prefs)); | 365 GetDefaultSearchProviderDetailsFromPrefs(prefs); |
384 | 366 |
385 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); | 367 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); |
386 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 368 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
387 } | 369 } |
388 | 370 |
389 TEST_F(AutomaticProfileResetterDelegateTest, | 371 TEST_F(AutomaticProfileResetterDelegateTest, |
390 DefaultSearchProviderDataWhenManaged) { | 372 DefaultSearchProviderDataWhenManaged) { |
391 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; | 373 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; |
392 const char kTestName[] = "name"; | 374 const char kTestName[] = "name"; |
393 const char kTestKeyword[] = "keyword"; | 375 const char kTestKeyword[] = "keyword"; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
446 ASSERT_TRUE(details->GetString("keyword", &keyword)); | 428 ASSERT_TRUE(details->GetString("keyword", &keyword)); |
447 TemplateURL* search_engine = | 429 TemplateURL* search_engine = |
448 template_url_service->GetTemplateURLForKeyword( | 430 template_url_service->GetTemplateURLForKeyword( |
449 base::ASCIIToUTF16(keyword)); | 431 base::ASCIIToUTF16(keyword)); |
450 ASSERT_TRUE(search_engine); | 432 ASSERT_TRUE(search_engine); |
451 template_url_service->SetUserSelectedDefaultSearchProvider( | 433 template_url_service->SetUserSelectedDefaultSearchProvider( |
452 prepopulated_engines[i]); | 434 prepopulated_engines[i]); |
453 | 435 |
454 PrefService* prefs = profile()->GetPrefs(); | 436 PrefService* prefs = profile()->GetPrefs(); |
455 ASSERT_TRUE(prefs); | 437 ASSERT_TRUE(prefs); |
456 scoped_ptr<base::DictionaryValue> expected_dsp_details( | 438 const base::DictionaryValue* expected_dsp_details = |
457 GetDefaultSearchProviderDetailsFromPrefs(prefs)); | 439 GetDefaultSearchProviderDetailsFromPrefs(prefs); |
458 ExpectDetailsMatch(*expected_dsp_details, *details); | 440 ExpectDetailsMatch(*expected_dsp_details, *details); |
459 } | 441 } |
460 } | 442 } |
461 | 443 |
462 TEST_F(AutomaticProfileResetterDelegateTest, | 444 TEST_F(AutomaticProfileResetterDelegateTest, |
463 FetchAndWaitOnDefaultSettingsVanilla) { | 445 FetchAndWaitOnDefaultSettingsVanilla) { |
464 google_util::BrandForTesting scoped_brand_for_testing((std::string())); | 446 google_util::BrandForTesting scoped_brand_for_testing((std::string())); |
465 | 447 |
466 // Expect ready_callback to be called just after empty brandcoded settings | 448 // 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 | 449 // 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()) | 597 if (ProfileResetGlobalError::IsSupportedOnPlatform()) |
616 ExpectResetPromptState(true /*active*/); | 598 ExpectResetPromptState(true /*active*/); |
617 else | 599 else |
618 ExpectResetPromptState(false /*active*/); | 600 ExpectResetPromptState(false /*active*/); |
619 resetter_delegate()->DismissPrompt(); | 601 resetter_delegate()->DismissPrompt(); |
620 ExpectResetPromptState(false /*active*/); | 602 ExpectResetPromptState(false /*active*/); |
621 resetter_delegate()->DismissPrompt(); | 603 resetter_delegate()->DismissPrompt(); |
622 } | 604 } |
623 | 605 |
624 } // namespace | 606 } // namespace |
OLD | NEW |