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..129a4aac78b0c7b1112b8b35d4ce1550297dd985 |
| --- /dev/null |
| +++ b/chrome/browser/profile_resetter/automatic_profile_resetter_delegate_unittest.cc |
| @@ -0,0 +1,431 @@ |
| +// 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/string_split.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/test/values_test_util.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/search_engines/template_url_prepopulate_data.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 testing::WhenSorted; |
| +using testing::ElementsAreArray; |
|
Peter Kasting
2013/10/16 00:40:21
Nit: Avoid using directives when they don't save l
engedy
2013/10/16 11:13:54
Done.
|
| + |
| +namespace { |
| + |
| +// 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: |
| + static const char kDefaultSearchProviderPrefix[]; |
| + |
| + TemplateURLSpecificTestBase() {} |
| + |
| + virtual void SetUp() { test_util_.SetUp(); } |
| + |
| + virtual void TearDown() { test_util_.TearDown(); } |
| + |
| + TestingProfile* profile() { return test_util_.profile(); } |
| + |
| + scoped_ptr<TemplateURL> CreateTestTemplateURL() { |
| + TemplateURLData data; |
| + |
| + data.SetURL("http://example.com/search?q={searchTerms}"); |
| + data.suggestions_url = "http://example.com/suggest?q={searchTerms}"; |
| + data.instant_url = "http://example.com/instant?q={searchTerms}"; |
| + data.image_url = "http://example.com/image?q={searchTerms}"; |
| + data.search_url_post_params = "search-post-params"; |
| + data.suggestions_url_post_params = "suggest-post-params"; |
| + data.instant_url_post_params = "instant-post-params"; |
| + data.image_url_post_params = "image-post-params"; |
| + |
| + data.favicon_url = GURL("http://example.com/favicon.ico"); |
| + data.new_tab_url = "http://example.com/newtab.html"; |
| + data.alternate_urls.push_back("http://example.com/s?q={searchTerms}"); |
| + |
| + data.short_name = base::UTF8ToUTF16("name"); |
| + data.SetKeyword(base::UTF8ToUTF16("keyword")); |
| + data.search_terms_replacement_key = "search-terms-replacment-key"; |
| + data.prepopulate_id = 42; |
| + data.input_encodings.push_back("UTF-8"); |
| + data.safe_for_autoreplace = true; |
| + |
| + return scoped_ptr<TemplateURL>(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 ---------------------------------------------- |
| + |
| +// Returns the details of the default search provider from |prefs| in a format |
| +// suitable for usage as |expected_details| in VerifyDetails(). |
| +scoped_ptr<base::DictionaryValue> GetDefaultSearchProviderDetails( |
| + const PrefService* prefs) { |
| + const char kDefaultSearchProviderPrefix[] = "default_search_provider"; |
| + scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion( |
| + prefs->GetPreferenceValues()); |
| + const base::DictionaryValue* dsp_details = NULL; |
| + EXPECT_TRUE(pref_values_with_path_expansion->GetDictionary( |
| + kDefaultSearchProviderPrefix, &dsp_details)); |
| + return dsp_details |
| + ? scoped_ptr<base::DictionaryValue>(dsp_details->DeepCopy()) |
|
Peter Kasting
2013/10/16 00:40:21
Nit: See comments in delegate .cc regarding wrappi
engedy
2013/10/16 11:13:54
Done.
|
| + : scoped_ptr<base::DictionaryValue>(new base::DictionaryValue); |
| +} |
| + |
| +// 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) { |
| + for (base::DictionaryValue::Iterator it(expected_details); !it.IsAtEnd(); |
| + it.Advance()) { |
| + SCOPED_TRACE(testing::Message() << "Key: " << it.key()); |
| + if (it.key() == "enabled" || it.key() == "synced_guid") { |
| + // These attributes should not be present. |
| + EXPECT_FALSE(details.HasKey(it.key())); |
| + continue; |
| + } |
| + const base::Value* expected_value = &it.value(); |
| + const base::Value* actual_value = NULL; |
| + ASSERT_TRUE(details.Get(it.key(), &actual_value)); |
| + if (it.key() == "id") { |
| + // Ignore ID as it is dynamically assigned by the TemplateURLService. |
| + } else if (it.key() == "encodings") { |
| + // Encoding list is stored in Prefs as a semicolon-separated string. |
|
Peter Kasting
2013/10/16 00:40:21
This is sure ugly... I don't have much good advice
engedy
2013/10/16 11:13:54
Agreed that this is very ugly. Vasilii, as Owner,
|
| + std::string expected_encodings; |
| + ASSERT_TRUE(expected_value->GetAsString(&expected_encodings)); |
| + std::vector<std::string> expected_encodings_vector; |
| + base::SplitString(expected_encodings, ';', &expected_encodings_vector); |
| + const base::ListValue* actual_encodings_list = NULL; |
| + ASSERT_TRUE(actual_value->GetAsList(&actual_encodings_list)); |
| + std::vector<std::string> actual_encodings_vector; |
| + for (base::ListValue::const_iterator it = actual_encodings_list->begin(); |
| + it != actual_encodings_list->end(); ++it) { |
| + std::string encoding; |
| + ASSERT_TRUE((*it)->GetAsString(&encoding)); |
| + actual_encodings_vector.push_back(encoding); |
| + } |
| + std::sort(expected_encodings_vector.begin(), |
| + expected_encodings_vector.end()); |
| + EXPECT_THAT(actual_encodings_vector, |
| + WhenSorted(ElementsAreArray(expected_encodings_vector))); |
| + } else { |
| + // Everything else is the same format. |
| + EXPECT_TRUE(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()->RequestCallbackWhenLoadedModulesAreEnumerated( |
| + mock_target.CreateClosure()); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + 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()->RequestCallbackWhenLoadedModulesAreEnumerated( |
| + 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->RequestCallbackWhenLoadedModulesAreEnumerated( |
| + 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()); |
| + |
| + // Just verify that each element looks like an MD5 hash in hexadecimal, and |
| + // also that we have at least one element on Win. |
| + ASSERT_TRUE(module_name_digests); |
| + for (base::ListValue::const_iterator it = module_name_digests->begin(); |
| + it != module_name_digests->end(); ++it) { |
| + std::string digest_hex; |
| + std::vector<uint8> digest_raw; |
| + |
| + ASSERT_TRUE((*it)->GetAsString(&digest_hex)); |
| + ASSERT_TRUE(base::HexStringToBytes(digest_hex, &digest_raw)); |
| + EXPECT_EQ(16u, digest_raw.size()); |
| + } |
| +#if defined(OS_WIN) |
| + EXPECT_LE(1u, 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()->RequestCallbackWhenTemplateURLServiceIsLoaded( |
| + mock_target.CreateClosure()); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + 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()->RequestCallbackWhenTemplateURLServiceIsLoaded( |
| + 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->RequestCallbackWhenTemplateURLServiceIsLoaded( |
| + 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. |
| + scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); |
| + TemplateURL* custom_dsp = owned_custom_dsp.get(); |
| + template_url_service->Add(owned_custom_dsp.release()); |
| + 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 paths for all these |
| + // preferences share the same first segment (before the '.'). Note, however, |
| + // that preferences are originally stored without path expansion. |
|
Peter Kasting
2013/10/16 00:40:21
Unfortunately, I still don't really know what this
engedy
2013/10/16 11:13:54
Done. On second read, this really does not make mu
|
| + PrefService* prefs = profile()->GetPrefs(); |
| + ASSERT_TRUE(prefs); |
| + scoped_ptr<base::DictionaryValue> expected_dsp_details( |
| + GetDefaultSearchProviderDetails(prefs)); |
| + VerifyDetails(*expected_dsp_details, *dsp_details); |
| +} |
| + |
| +TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, |
| + IsDefaultSearchProviderManaged) { |
| + const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; |
| + const char kTestName[] = "name"; |
| + const char kTestKeyword[] = "keyword"; |
| + |
| + 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, std::string(), |
| + std::string(), std::string(), std::string(), std::string()); |
| + |
| + 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, std::string(), |
| + std::string(), std::string(), std::string(), std::string()); |
| + |
| + dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails(); |
| + EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| + EXPECT_TRUE(dsp_details->empty()); |
| + |
| + // Disable having a default search provider, and set an invalid one from |
| + // policy. |
| + test_util_.RemoveManagedDefaultSearchPreferences(); |
| + test_util_.SetManagedDefaultSearchPreferences( |
| + true, std::string(), std::string(), std::string(), std::string(), |
| + std::string(), std::string(), std::string(), std::string()); |
| + |
| + dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails(); |
| + EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| + EXPECT_TRUE(dsp_details->empty()); |
| +} |
| + |
| +TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, |
| + DISABLED_IsDefaultSearchProviderManagedSubtle) { |
| + 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()); |
| + |
| + scoped_ptr<base::DictionaryValue> dsp_details( |
| + resetter_delegate()->GetDefaultSearchProviderDetails()); |
| + EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| + EXPECT_TRUE(dsp_details->empty()); |
| +} |
| + |
| +TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, |
| + GetPrepopulatedSearchProvidersDetails) { |
| + TemplateURLService* template_url_service = test_util_.model(); |
| + test_util_.VerifyLoad(); |
| + |
| + scoped_ptr<base::ListValue> 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(), search_engines_details->GetSize()); |
| + |
| + // This assumes that the engines pre-populated for the current locale all have |
| + // a unique keyword specified. |
|
Peter Kasting
2013/10/16 00:40:21
All engines in the database must always have uniqu
engedy
2013/10/16 11:13:54
Done.
|
| + for (size_t i = 0; i < search_engines_details->GetSize(); ++i) { |
| + const base::DictionaryValue* details = NULL; |
| + ASSERT_TRUE(search_engines_details->GetDictionary(i, &details)); |
| + |
| + std::string keyword; |
| + ASSERT_TRUE(details->GetString("keyword", &keyword)); |
| + TemplateURL* search_engine = |
| + template_url_service->GetTemplateURLForKeyword(UTF8ToUTF16(keyword)); |
| + ASSERT_TRUE(search_engine); |
| + template_url_service->SetDefaultSearchProvider(prepopulated_engines[i]); |
| + |
| + PrefService* prefs = profile()->GetPrefs(); |
| + ASSERT_TRUE(prefs); |
| + scoped_ptr<base::DictionaryValue> expected_dsp_details( |
| + GetDefaultSearchProviderDetails(prefs)); |
| + VerifyDetails(*expected_dsp_details, *details); |
| + } |
| +} |
| + |
| +} // namespace |