| Index: chrome/browser/search_engines/default_search_pref_migration_unittest.cc
|
| diff --git a/chrome/browser/search_engines/default_search_pref_migration_unittest.cc b/chrome/browser/search_engines/default_search_pref_migration_unittest.cc
|
| index 9d85c0e2d492d8b937f22ec144c9fda2be3384ff..e99d8b1e2a2d152f30c263c12cf0763365291d4f 100644
|
| --- a/chrome/browser/search_engines/default_search_pref_migration_unittest.cc
|
| +++ b/chrome/browser/search_engines/default_search_pref_migration_unittest.cc
|
| @@ -7,15 +7,22 @@
|
| #include <string>
|
|
|
| #include "base/compiler_specific.h"
|
| +#include "base/files/scoped_temp_dir.h"
|
| +#include "base/logging.h"
|
| #include "base/macros.h"
|
| +#include "base/prefs/pref_service.h"
|
| +#include "base/run_loop.h"
|
| #include "base/strings/string16.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| +#include "base/strings/string_util.h"
|
| #include "base/strings/utf_string_conversions.h"
|
| #include "chrome/browser/search_engines/template_url.h"
|
| #include "chrome/browser/search_engines/template_url_service.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 "testing/gtest/include/gtest/gtest.h"
|
| +#include "url/gurl.h"
|
|
|
| class DefaultSearchPrefMigrationTest : public testing::Test {
|
| public:
|
| @@ -25,27 +32,112 @@ class DefaultSearchPrefMigrationTest : public testing::Test {
|
| virtual void SetUp() OVERRIDE;
|
| virtual void TearDown() OVERRIDE;
|
|
|
| + void SaveDefaultSearchProviderToLegacyPrefs(const TemplateURL* t_url);
|
| +
|
| scoped_ptr<TemplateURL> CreateKeyword(const std::string& short_name,
|
| const std::string& keyword,
|
| const std::string& url);
|
|
|
| - TemplateURLServiceTestUtil* test_util() { return &test_util_; }
|
| + const TemplateURLData* GetModernDefaultSearchEngine(
|
| + DefaultSearchManager::Source* source);
|
| +
|
| + void SetModernUserSelectedDefaultSearchEngine(const TemplateURLData& data);
|
| +
|
| + TestingProfile* profile() { return profile_.get(); }
|
|
|
| private:
|
| - TemplateURLServiceTestUtil test_util_;
|
| + base::ScopedTempDir temp_dir_;
|
| + scoped_ptr<TestingProfile> profile_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(DefaultSearchPrefMigrationTest);
|
| };
|
|
|
| +
|
| +
|
| DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() {
|
| }
|
|
|
| void DefaultSearchPrefMigrationTest::SetUp() {
|
| - test_util_.SetUp();
|
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
|
| + profile_.reset(new TestingProfile(temp_dir_.path()));
|
| }
|
|
|
| void DefaultSearchPrefMigrationTest::TearDown() {
|
| - test_util_.TearDown();
|
| + profile_.reset();
|
| +
|
| + // Flush the message loop to make application verifiers happy.
|
| + base::RunLoop().RunUntilIdle();
|
| +}
|
| +
|
| +void DefaultSearchPrefMigrationTest::SaveDefaultSearchProviderToLegacyPrefs(
|
| + const TemplateURL* t_url) {
|
| + PrefService* prefs = profile()->GetPrefs();
|
| +
|
| + bool enabled = false;
|
| + std::string search_url;
|
| + std::string suggest_url;
|
| + std::string instant_url;
|
| + std::string image_url;
|
| + std::string new_tab_url;
|
| + std::string search_url_post_params;
|
| + std::string suggest_url_post_params;
|
| + std::string instant_url_post_params;
|
| + std::string image_url_post_params;
|
| + std::string icon_url;
|
| + std::string encodings;
|
| + std::string short_name;
|
| + std::string keyword;
|
| + std::string id_string;
|
| + std::string prepopulate_id;
|
| + base::ListValue alternate_urls;
|
| + std::string search_terms_replacement_key;
|
| + if (t_url) {
|
| + DCHECK_EQ(TemplateURL::NORMAL, t_url->GetType());
|
| + enabled = true;
|
| + search_url = t_url->url();
|
| + suggest_url = t_url->suggestions_url();
|
| + instant_url = t_url->instant_url();
|
| + image_url = t_url->image_url();
|
| + new_tab_url = t_url->new_tab_url();
|
| + search_url_post_params = t_url->search_url_post_params();
|
| + suggest_url_post_params = t_url->suggestions_url_post_params();
|
| + instant_url_post_params = t_url->instant_url_post_params();
|
| + image_url_post_params = t_url->image_url_post_params();
|
| + GURL icon_gurl = t_url->favicon_url();
|
| + if (!icon_gurl.is_empty())
|
| + icon_url = icon_gurl.spec();
|
| + encodings = JoinString(t_url->input_encodings(), ';');
|
| + short_name = base::UTF16ToUTF8(t_url->short_name());
|
| + keyword = base::UTF16ToUTF8(t_url->keyword());
|
| + id_string = base::Int64ToString(t_url->id());
|
| + prepopulate_id = base::Int64ToString(t_url->prepopulate_id());
|
| + for (size_t i = 0; i < t_url->alternate_urls().size(); ++i)
|
| + alternate_urls.AppendString(t_url->alternate_urls()[i]);
|
| + search_terms_replacement_key = t_url->search_terms_replacement_key();
|
| + }
|
| + prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled);
|
| + prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url);
|
| + prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url);
|
| + prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url);
|
| + prefs->SetString(prefs::kDefaultSearchProviderImageURL, image_url);
|
| + prefs->SetString(prefs::kDefaultSearchProviderNewTabURL, new_tab_url);
|
| + prefs->SetString(prefs::kDefaultSearchProviderSearchURLPostParams,
|
| + search_url_post_params);
|
| + prefs->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams,
|
| + suggest_url_post_params);
|
| + prefs->SetString(prefs::kDefaultSearchProviderInstantURLPostParams,
|
| + instant_url_post_params);
|
| + prefs->SetString(prefs::kDefaultSearchProviderImageURLPostParams,
|
| + image_url_post_params);
|
| + prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url);
|
| + prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings);
|
| + prefs->SetString(prefs::kDefaultSearchProviderName, short_name);
|
| + prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword);
|
| + prefs->SetString(prefs::kDefaultSearchProviderID, id_string);
|
| + prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id);
|
| + prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls);
|
| + prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey,
|
| + search_terms_replacement_key);
|
| }
|
|
|
| scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword(
|
| @@ -56,28 +148,42 @@ scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword(
|
| data.short_name = base::ASCIIToUTF16(short_name);
|
| data.SetKeyword(base::ASCIIToUTF16(keyword));
|
| data.SetURL(url);
|
| - scoped_ptr<TemplateURL> t_url(new TemplateURL(test_util_.profile(), data));
|
| + scoped_ptr<TemplateURL> t_url(new TemplateURL(profile(), data));
|
| return t_url.Pass();
|
| }
|
|
|
| +void DefaultSearchPrefMigrationTest::SetModernUserSelectedDefaultSearchEngine(
|
| + const TemplateURLData& data) {
|
| + DefaultSearchManager(profile()->GetPrefs(),
|
| + DefaultSearchManager::ObserverCallback())
|
| + .SetUserSelectedDefaultSearchEngine(data);
|
| +}
|
| +
|
| +const TemplateURLData*
|
| +DefaultSearchPrefMigrationTest::GetModernDefaultSearchEngine(
|
| + DefaultSearchManager::Source* source) {
|
| + return DefaultSearchManager(profile()->GetPrefs(),
|
| + DefaultSearchManager::ObserverCallback())
|
| + .GetDefaultSearchEngine(source);
|
| +}
|
| +
|
| TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) {
|
| scoped_ptr<TemplateURL> t_url(
|
| CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
|
| // Store a value in the legacy location.
|
| - TemplateURLService::SaveDefaultSearchProviderToPrefs(
|
| - t_url.get(), test_util()->profile()->GetPrefs());
|
| + SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
|
|
|
| // Run the migration.
|
| - ConfigureDefaultSearchPrefMigrationToDictionaryValue(
|
| - test_util()->profile()->GetPrefs());
|
| + ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
|
|
|
| // Test that it was migrated.
|
| - TemplateURLData modern_default;
|
| - ASSERT_TRUE(DefaultSearchManager(test_util()->profile()->GetPrefs())
|
| - .GetDefaultSearchEngine(&modern_default));
|
| - EXPECT_EQ(t_url->short_name(), modern_default.short_name);
|
| - EXPECT_EQ(t_url->keyword(), modern_default.keyword());
|
| - EXPECT_EQ(t_url->url(), modern_default.url());
|
| + DefaultSearchManager::Source source;
|
| + const TemplateURLData* modern_default = GetModernDefaultSearchEngine(&source);
|
| + ASSERT_TRUE(modern_default);
|
| + EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
|
| + EXPECT_EQ(t_url->short_name(), modern_default->short_name);
|
| + EXPECT_EQ(t_url->keyword(), modern_default->keyword());
|
| + EXPECT_EQ(t_url->url(), modern_default->url());
|
| }
|
|
|
| TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) {
|
| @@ -86,86 +192,38 @@ TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) {
|
| scoped_ptr<TemplateURL> t_url2(
|
| CreateKeyword("name2", "key2", "http://foo2/{searchTerms}"));
|
| // Store a value in the legacy location.
|
| - TemplateURLService::SaveDefaultSearchProviderToPrefs(
|
| - t_url.get(), test_util()->profile()->GetPrefs());
|
| + SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
|
|
|
| // Store another value in the modern location.
|
| - DefaultSearchManager(test_util()->profile()->GetPrefs())
|
| - .SetUserSelectedDefaultSearchEngine(t_url2->data());
|
| + SetModernUserSelectedDefaultSearchEngine(t_url2->data());
|
|
|
| // Run the migration.
|
| - ConfigureDefaultSearchPrefMigrationToDictionaryValue(
|
| - test_util()->profile()->GetPrefs());
|
| + ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
|
|
|
| // Test that no migration occurred. The modern value is left intact.
|
| - TemplateURLData modern_default;
|
| - ASSERT_TRUE(DefaultSearchManager(test_util()->profile()->GetPrefs())
|
| - .GetDefaultSearchEngine(&modern_default));
|
| - EXPECT_EQ(t_url2->short_name(), modern_default.short_name);
|
| - EXPECT_EQ(t_url2->keyword(), modern_default.keyword());
|
| - EXPECT_EQ(t_url2->url(), modern_default.url());
|
| + DefaultSearchManager::Source source;
|
| + const TemplateURLData* modern_default = GetModernDefaultSearchEngine(&source);
|
| + EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
|
| + EXPECT_EQ(t_url2->short_name(), modern_default->short_name);
|
| + EXPECT_EQ(t_url2->keyword(), modern_default->keyword());
|
| + EXPECT_EQ(t_url2->url(), modern_default->url());
|
| }
|
|
|
| TEST_F(DefaultSearchPrefMigrationTest,
|
| AutomaticallySelectedValueIsNotMigrated) {
|
| - test_util()->VerifyLoad();
|
| - scoped_ptr<TemplateURLData> legacy_default;
|
| - bool legacy_is_managed = false;
|
| - // The initialization of the TemplateURLService will have stored the
|
| - // pre-populated DSE in the legacy location in prefs.
|
| - ASSERT_TRUE(TemplateURLService::LoadDefaultSearchProviderFromPrefs(
|
| - test_util()->profile()->GetPrefs(), &legacy_default, &legacy_is_managed));
|
| - EXPECT_FALSE(legacy_is_managed);
|
| - EXPECT_TRUE(legacy_default);
|
| - EXPECT_GT(legacy_default->prepopulate_id, 0);
|
| + DefaultSearchManager::Source source;
|
| + TemplateURLData prepopulated_default(*GetModernDefaultSearchEngine(&source));
|
| + EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
|
|
|
| - // Run the migration.
|
| - ConfigureDefaultSearchPrefMigrationToDictionaryValue(
|
| - test_util()->profile()->GetPrefs());
|
| + TemplateURL prepopulated_turl(profile(), prepopulated_default);
|
|
|
| - // Test that the legacy value is not migrated, as it is not user-selected.
|
| - TemplateURLData modern_default;
|
| - ASSERT_FALSE(DefaultSearchManager(test_util()->profile()->GetPrefs())
|
| - .GetDefaultSearchEngine(&modern_default));
|
| -}
|
| -
|
| -TEST_F(DefaultSearchPrefMigrationTest, ManagedValueIsNotMigrated) {
|
| - // Set a managed preference that establishes a default search provider.
|
| - const char kName[] = "test1";
|
| - const char kKeyword[] = "test.com";
|
| - const char kSearchURL[] = "http://test.com/search?t={searchTerms}";
|
| - const char kIconURL[] = "http://test.com/icon.jpg";
|
| - const char kEncodings[] = "UTF-16;UTF-32";
|
| - const char kAlternateURL[] = "http://test.com/search#t={searchTerms}";
|
| - const char kSearchTermsReplacementKey[] = "espv";
|
| -
|
| - // This method only updates the legacy location for managed DSEs. So it will
|
| - // not cause DefaultSearchManager to report a value.
|
| - test_util()->SetManagedDefaultSearchPreferences(true,
|
| - kName,
|
| - kKeyword,
|
| - kSearchURL,
|
| - std::string(),
|
| - kIconURL,
|
| - kEncodings,
|
| - kAlternateURL,
|
| - kSearchTermsReplacementKey);
|
| - test_util()->VerifyLoad();
|
| -
|
| - // Verify that the policy value is correctly installed.
|
| - scoped_ptr<TemplateURLData> legacy_default;
|
| - bool legacy_is_managed = false;
|
| - ASSERT_TRUE(TemplateURLService::LoadDefaultSearchProviderFromPrefs(
|
| - test_util()->profile()->GetPrefs(), &legacy_default, &legacy_is_managed));
|
| - EXPECT_TRUE(legacy_is_managed);
|
| - EXPECT_TRUE(legacy_default);
|
| + // Store a value in the legacy location.
|
| + SaveDefaultSearchProviderToLegacyPrefs(&prepopulated_turl);
|
|
|
| // Run the migration.
|
| - ConfigureDefaultSearchPrefMigrationToDictionaryValue(
|
| - test_util()->profile()->GetPrefs());
|
| + ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
|
|
|
| - // Test that the policy-defined value is not migrated.
|
| - TemplateURLData modern_default;
|
| - ASSERT_FALSE(DefaultSearchManager(test_util()->profile()->GetPrefs())
|
| - .GetDefaultSearchEngine(&modern_default));
|
| + // Test that the legacy value is not migrated, as it is not user-selected.
|
| + GetModernDefaultSearchEngine(&source);
|
| + EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
|
| }
|
|
|