Chromium Code Reviews| Index: chrome/browser/profile_resetter/automatic_profile_resetter_delegate_unittest.cc |
| diff --git a/chrome/browser/profile_resetter/automatic_profile_resetter_delegate_unittest.cc b/chrome/browser/profile_resetter/automatic_profile_resetter_delegate_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..43cd46965ae839ec34cbaa4b68ee10ba24760874 |
| --- /dev/null |
| +++ b/chrome/browser/profile_resetter/automatic_profile_resetter_delegate_unittest.cc |
| @@ -0,0 +1,397 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/run_loop.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/test/values_test_util.h" |
| +#include "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/search_engines/template_url_service.h" |
| +#include "chrome/browser/search_engines/template_url_service_factory.h" |
| +#include "chrome/browser/search_engines/template_url_service_test_util.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/test/base/testing_pref_service_syncable.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +#if defined(OS_WIN) |
| +#include "chrome/browser/enumerate_modules_model_win.h" |
| +#endif |
| + |
| +using base::UTF8ToUTF16; |
| + |
| +namespace { |
| + |
| +// Keep this value in sync with prefs::kDefaultSearchProviderURL and friends. |
| +const char kDefaultSearchProviderPrefix[] = "default_search_provider"; |
| + |
| +const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; |
| +const char kTestSuggestURL[] = "http://example.com/suggest?q={searchTerms}"; |
| +const char kTestInstantURL[] = "http://example.com/instant?q={searchTerms}"; |
| +const char kTestImageURL[] = "http://example.com/image?q={searchTerms}"; |
| +const char kTestSearchURLPostParams[] = "search-post-params"; |
| +const char kTestSuggestURLPostParams[] = "suggest-post-params"; |
| +const char kTestInstantURLPostParams[] = "instant-post-params"; |
| +const char kTestImageURLPostParams[] = "image-post-params"; |
| + |
| +const char kTestIconURL[] = "http://example.com/favicon.ico"; |
| +const char kTestNewTabURL[] = "http://example.com/newtab.html"; |
| +const char kTestAlternateURL[] = "http://example.com/s?q={searchTerms}"; |
| + |
| +const char kTestName[] = "name"; |
| +const char kTestKeyword[] = "keyword"; |
| +const char kTestSearchTermReplacementKey[] = "key"; |
| +const char kTestPrepopulateId[] = "2"; |
| +const char kTestEncoding[] = "UTF-8"; |
| + |
| +// Test fixtures ------------------------------------------------------------- |
| + |
| +class GenericTestBase : public testing::Test { |
| + protected: |
| + GenericTestBase() {} |
| + |
| + virtual void SetUp() { profile_.reset(new TestingProfile()); } |
| + |
| + TestingProfile* profile() { return profile_.get(); } |
| + |
| + private: |
| + content::TestBrowserThreadBundle thread_bundle_; |
| + scoped_ptr<TestingProfile> profile_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(GenericTestBase); |
| +}; |
| + |
| +class TemplateURLSpecificTestBase : public testing::Test { |
| + protected: |
| + TemplateURLSpecificTestBase() {} |
| + |
| + virtual void SetUp() { test_util_.SetUp(); } |
| + |
| + virtual void TearDown() { test_util_.TearDown(); } |
| + |
| + TestingProfile* profile() { return test_util_.profile(); } |
| + |
| + TemplateURL* CreateTestTemplateURL() { |
| + TemplateURLData data; |
| + |
| + data.SetURL(kTestSearchURL); |
| + data.suggestions_url = kTestSuggestURL; |
| + data.instant_url = kTestInstantURL; |
| + data.image_url = kTestImageURL; |
| + data.search_url_post_params = kTestSearchURLPostParams; |
| + data.suggestions_url_post_params = kTestSuggestURLPostParams; |
| + data.instant_url_post_params = kTestInstantURLPostParams; |
| + data.image_url_post_params = kTestImageURLPostParams; |
| + |
| + data.favicon_url = GURL(kTestIconURL); |
| + data.new_tab_url = kTestNewTabURL; |
| + data.alternate_urls.push_back(kTestAlternateURL); |
| + |
| + data.short_name = base::UTF8ToUTF16(kTestName); |
| + data.SetKeyword(base::UTF8ToUTF16(kTestKeyword)); |
| + data.search_terms_replacement_key = kTestSearchTermReplacementKey; |
| + EXPECT_TRUE(base::StringToInt(kTestPrepopulateId, &data.prepopulate_id)); |
| + data.input_encodings.push_back(kTestEncoding); |
| + data.safe_for_autoreplace = true; |
| + |
| + return new TemplateURL(profile(), data); |
| + } |
| + |
| + TemplateURLServiceTestUtil test_util_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TemplateURLSpecificTestBase); |
| +}; |
| + |
| +template <class BaseTestFixture> |
| +class ResetterDelegateMixin : public BaseTestFixture { |
| + protected: |
| + ResetterDelegateMixin() {} |
| + |
| + virtual void SetUp() OVERRIDE { |
| + BaseTestFixture::SetUp(); |
| + resetter_delegate_.reset( |
| + new AutomaticProfileResetterDelegateImpl(BaseTestFixture::profile())); |
| + } |
| + |
| + virtual void TearDown() OVERRIDE { |
| + resetter_delegate_.reset(); |
| + BaseTestFixture::TearDown(); |
| + } |
| + |
| + AutomaticProfileResetterDelegate* resetter_delegate() { |
| + return resetter_delegate_.get(); |
| + } |
| + |
| + private: |
| + scoped_ptr<AutomaticProfileResetterDelegate> resetter_delegate_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ResetterDelegateMixin); |
| +}; |
| + |
| +typedef ResetterDelegateMixin<GenericTestBase> |
| + AutomaticProfileResetterDelegateTest; |
| + |
| +typedef ResetterDelegateMixin<TemplateURLSpecificTestBase> |
| + AutomaticProfileResetterDelegateTestTemplateURLs; |
| + |
| +// Helper classes and functions ---------------------------------------------- |
| + |
| +// Verifies that the |details| of a search engine as provided by the delegate |
| +// are correct in comparison to the |expected_details| coming from the Prefs. |
| +void VerifyDetails(const base::DictionaryValue& expected_details, |
| + const base::DictionaryValue& details) { |
| + const char* keys_to_verify[] = { |
| + "search_url", "search_terms_replacement_key", |
| + "suggest_url", "instant_url", |
| + "image_url", "new_tab_url", |
| + "icon_url", "search_url_post_params", |
| + "suggest_url_post_params", "instant_url_post_params", |
| + "image_url_post_params", "name", |
| + "keyword", "encodings", |
| + "prepopulate_id", "alternate_urls"}; |
| + |
| + for (size_t i = 0; i < arraysize(keys_to_verify); ++i) { |
| + SCOPED_TRACE(testing::Message() << "Key: " << keys_to_verify[i]); |
| + const base::Value* expected_value; |
| + const base::Value* actual_value; |
| + ASSERT_TRUE(expected_details.Get(keys_to_verify[i], &expected_value)); |
| + EXPECT_TRUE(details.Get(keys_to_verify[i], &actual_value) && |
|
vasilii
2013/10/14 16:43:33
Split this condition.
engedy
2013/10/14 19:35:50
Done.
|
| + actual_value->Equals(expected_value)); |
| + } |
| +} |
| + |
| +class MockCallbackTarget { |
| + public: |
| + MockCallbackTarget() {} |
| + |
| + MOCK_CONST_METHOD0(Run, void(void)); |
| + |
| + base::Closure CreateClosure() { |
| + return base::Closure( |
| + base::Bind(&MockCallbackTarget::Run, base::Unretained(this))); |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget); |
| +}; |
| + |
| +// Tests --------------------------------------------------------------------- |
| + |
| +TEST_F(AutomaticProfileResetterDelegateTest, |
| + TriggerAndWaitOnModuleEnumeration) { |
| + testing::StrictMock<MockCallbackTarget> mock_target; |
| + |
| + // Expect ready_callback to be called just after the modules have been |
| + // enumerated. Fail if it is not called, or called too early. |
| + resetter_delegate()->WaitOnEnumerationOfLoadedModules( |
| + mock_target.CreateClosure()); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + testing::Mock::VerifyAndClearExpectations(&mock_target); |
|
vasilii
2013/10/14 16:43:33
I think it would fail anyway in the line above.
engedy
2013/10/14 19:35:50
You are correct, done.
|
| + |
| + EXPECT_CALL(mock_target, Run()); |
| + resetter_delegate()->EnumerateLoadedModulesIfNeeded(); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + testing::Mock::VerifyAndClearExpectations(&mock_target); |
| + |
| + // Expect ready_callback to be posted immediately when the modules have |
| + // already been enumerated. |
| + EXPECT_CALL(mock_target, Run()); |
| + resetter_delegate()->WaitOnEnumerationOfLoadedModules( |
| + mock_target.CreateClosure()); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| +#if defined(OS_WIN) |
| + testing::Mock::VerifyAndClearExpectations(&mock_target); |
| + |
| + // Expect ready_callback to be posted immediately even when the modules had |
| + // already been enumerated when the delegate was constructed. |
| + scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( |
| + new AutomaticProfileResetterDelegateImpl(profile())); |
| + |
| + EXPECT_CALL(mock_target, Run()); |
| + late_resetter_delegate->WaitOnEnumerationOfLoadedModules( |
| + mock_target.CreateClosure()); |
| + base::RunLoop().RunUntilIdle(); |
| +#endif |
| +} |
| + |
| +TEST_F(AutomaticProfileResetterDelegateTest, GetLoadedModuleNameDigests) { |
| + resetter_delegate()->EnumerateLoadedModulesIfNeeded(); |
| + base::RunLoop().RunUntilIdle(); |
| + scoped_ptr<base::ListValue> module_name_digests( |
| + resetter_delegate()->GetLoadedModuleNameDigests()); |
| + |
| + ASSERT_TRUE(module_name_digests); |
| +#if defined(OS_WIN) |
| + scoped_ptr<base::ListValue> module_list; |
| + module_list.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); |
| + ASSERT_TRUE(module_list); |
| + EXPECT_EQ(module_list->GetSize(), module_name_digests->GetSize()); |
| +#endif |
| +} |
| + |
| +TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, |
| + LoadAndWaitOnTemplateURLService) { |
| + testing::StrictMock<MockCallbackTarget> mock_target; |
| + |
| + // Expect ready_callback to be called just after the template URL service gets |
| + // initialized. Fail if it is not called, or called too early. |
| + resetter_delegate()->WaitOnTemplateURLService(mock_target.CreateClosure()); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + testing::Mock::VerifyAndClearExpectations(&mock_target); |
| + |
| + EXPECT_CALL(mock_target, Run()); |
| + resetter_delegate()->LoadTemplateURLServiceIfNeeded(); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + testing::Mock::VerifyAndClearExpectations(&mock_target); |
| + |
| + // Expect ready_callback to be posted immediately when the template URL |
| + // service is already initialized. |
| + EXPECT_CALL(mock_target, Run()); |
| + resetter_delegate()->WaitOnTemplateURLService(mock_target.CreateClosure()); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + testing::Mock::VerifyAndClearExpectations(&mock_target); |
| + |
| + // Expect ready_callback to be posted immediately even when the template URL |
| + // service had already been initialized when the delegate was constructed. |
| + scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( |
| + new AutomaticProfileResetterDelegateImpl(profile())); |
| + |
| + EXPECT_CALL(mock_target, Run()); |
| + late_resetter_delegate->WaitOnTemplateURLService(mock_target.CreateClosure()); |
| + base::RunLoop().RunUntilIdle(); |
| +} |
| + |
| +TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, |
| + GetDefaultSearchProviderDetails) { |
| + TemplateURLService* template_url_service = test_util_.model(); |
| + test_util_.VerifyLoad(); |
| + |
| + // Create a custom search provider, and make it the default. Note that this |
| + // will update all data related to the default search provider in Prefs. |
| + TemplateURL* custom_dsp = CreateTestTemplateURL(); |
| + template_url_service->Add(custom_dsp); |
| + template_url_service->SetDefaultSearchProvider(custom_dsp); |
| + |
| + scoped_ptr<base::DictionaryValue> dsp_details( |
| + resetter_delegate()->GetDefaultSearchProviderDetails()); |
| + |
| + // Verify above details against the user preferences that have been stored by |
| + // TemplateURLService. We leverage on the fact that the names for all these |
| + // preferences start with the same prefix. |
| + PrefService* prefs = profile()->GetPrefs(); |
| + ASSERT_TRUE(prefs); |
| + scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion( |
| + prefs->GetPreferenceValues()); |
| + base::DictionaryValue* expected_dsp_details; |
| + ASSERT_TRUE(pref_values_with_path_expansion->GetDictionary( |
| + kDefaultSearchProviderPrefix, &expected_dsp_details)); |
| + VerifyDetails(*expected_dsp_details, *dsp_details); |
| +} |
| + |
| +TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, |
| + IsDefaultSearchProviderManaged) { |
| + test_util_.VerifyLoad(); |
| + |
| + EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| + |
| + // Enable having a default search provider, and also set one from policy. |
| + test_util_.SetManagedDefaultSearchPreferences( |
| + true, kTestName, kTestKeyword, kTestSearchURL, "", "", "", "", ""); |
| + |
| + EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| + scoped_ptr<base::DictionaryValue> dsp_details( |
| + resetter_delegate()->GetDefaultSearchProviderDetails()); |
| + base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url"); |
| + |
| + // Disable having a default search provider, but nevertheless, set one from |
| + // policy. |
| + test_util_.RemoveManagedDefaultSearchPreferences(); |
| + test_util_.SetManagedDefaultSearchPreferences( |
| + false, kTestName, kTestKeyword, kTestSearchURL, "", "", "", "", ""); |
| + |
| + EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| + EXPECT_EQ(NULL, resetter_delegate()->GetDefaultSearchProviderDetails()); |
| + |
| + // Disable having a default search provider, and set an invalid one from |
| + // policy. |
| + test_util_.RemoveManagedDefaultSearchPreferences(); |
| + test_util_.SetManagedDefaultSearchPreferences( |
| + true, "", "", "", "", "", "", "", ""); |
| + |
| + EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| + EXPECT_EQ(NULL, resetter_delegate()->GetDefaultSearchProviderDetails()); |
| +} |
| + |
| +TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, |
| + DISABLED_IsDefaultSearchProviderManagedSubtle) { |
|
vasilii
2013/10/14 16:43:33
We shouldn't commit the broken test.
engedy
2013/10/14 19:35:50
Agreed. I wanted to have this here for the review,
vasilii
2013/10/17 10:57:57
Either fix it or remove it.
|
| + test_util_.VerifyLoad(); |
| + |
| + // Only set a single policy to disable having a default search provider. |
| + TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService(); |
| + pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled, |
| + new base::FundamentalValue(false)); |
| + test_util_.model()->Observe( |
| + chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED, |
| + content::NotificationService::AllSources(), |
| + content::NotificationService::NoDetails()); |
| + |
| + EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| + EXPECT_EQ(NULL, resetter_delegate()->GetDefaultSearchProviderDetails()); |
| +} |
| + |
| +TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, |
| + GetPrepopulatedSearchProvidersDetails) { |
| + TemplateURLService* template_url_service = test_util_.model(); |
| + test_util_.VerifyLoad(); |
| + |
| + scoped_ptr<base::ListValue> prepopulated_search_engines_details( |
| + resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); |
| + |
| + // Do the same kind of verification as for GetDefaultSearchEngineDetails: |
| + // subsequently set each pre-populated engine as the default, so we can verify |
| + // that the details returned by the delegate about one particular engine are |
| + // correct in comparison to what has been stored to the Prefs. |
| + std::vector<TemplateURL*> prepopulated_engines = |
| + template_url_service->GetTemplateURLs(); |
| + |
| + ASSERT_EQ(prepopulated_engines.size(), |
| + prepopulated_search_engines_details->GetSize()); |
| + |
| + // This assumes that the order of engines are preserved. |
| + for (size_t i = 0; i < prepopulated_engines.size(); ++i) { |
| + template_url_service->SetDefaultSearchProvider(prepopulated_engines[i]); |
| + |
| + PrefService* prefs = profile()->GetPrefs(); |
| + ASSERT_TRUE(prefs); |
| + scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion( |
| + prefs->GetPreferenceValues()); |
|
vasilii
2013/10/14 16:43:33
Why don't you use GetPreferenceValue("default_sear
engedy
2013/10/14 19:35:50
That is not possible here, because preferences are
|
| + base::DictionaryValue* expected_dsp_details; |
| + ASSERT_TRUE(pref_values_with_path_expansion->GetDictionary( |
| + kDefaultSearchProviderPrefix, &expected_dsp_details)); |
| + |
| + base::DictionaryValue* details; |
| + ASSERT_TRUE( |
| + prepopulated_search_engines_details->GetDictionary(i, &details)); |
| + |
| + SCOPED_TRACE(testing::Message() << "Details: " << *details); |
| + VerifyDetails(*expected_dsp_details, *details); |
| + } |
| +} |
| + |
| +} // namespace |