| 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..c257b6e669d99f9323d981c94c9fd07ab06fb462
|
| --- /dev/null
|
| +++ b/chrome/browser/profile_resetter/automatic_profile_resetter_delegate_unittest.cc
|
| @@ -0,0 +1,293 @@
|
| +// 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/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/test/base/testing_profile.h"
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +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 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<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) &&
|
| + 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(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());
|
| +
|
| + 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");
|
| +
|
| + test_util_.RemoveManagedDefaultSearchPreferences();
|
| + test_util_.SetManagedDefaultSearchPreferences(
|
| + false, kTestName, kTestKeyword, kTestSearchURL, "", "", "", "", "");
|
| +
|
| + EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
|
| + EXPECT_EQ(NULL, resetter_delegate()->GetDefaultSearchProviderDetails());
|
| +
|
| + test_util_.RemoveManagedDefaultSearchPreferences();
|
| + test_util_.SetManagedDefaultSearchPreferences(
|
| + true, "", "", "", "", "", "", "", "");
|
| +
|
| + 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 (we do not assume the same ordering, so we just try all).
|
| + std::vector<TemplateURL*> prepopulated_engines =
|
| + template_url_service->GetTemplateURLs();
|
| +
|
| + ASSERT_EQ(prepopulated_engines.size(),
|
| + prepopulated_search_engines_details->GetSize());
|
| +
|
| + 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());
|
| + 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
|
|
|