| 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 181367013f1504b66ae43d2402081e65e3ddfa6c..bc245f9d9c6b62639da987d815245890dc1ac95c 100644
|
| --- a/chrome/browser/search_engines/default_search_pref_migration_unittest.cc
|
| +++ b/chrome/browser/search_engines/default_search_pref_migration_unittest.cc
|
| @@ -7,12 +7,14 @@
|
| #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/strings/string16.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/test/base/testing_pref_service_syncable.h"
|
| #include "chrome/test/base/testing_profile.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| @@ -23,16 +25,21 @@ class DefaultSearchPrefMigrationTest : public testing::Test {
|
|
|
| // testing::Test:
|
| virtual void SetUp() OVERRIDE;
|
| - virtual void TearDown() OVERRIDE;
|
|
|
| scoped_ptr<TemplateURL> CreateKeyword(const std::string& short_name,
|
| const std::string& keyword,
|
| const std::string& url);
|
|
|
| - TemplateURLServiceTestUtil* test_util() { return &test_util_; }
|
| + TestingProfile* profile() { return profile_.get(); }
|
| +
|
| + DefaultSearchManager* default_search_manager() {
|
| + return default_search_manager_.get();
|
| + }
|
|
|
| private:
|
| - TemplateURLServiceTestUtil test_util_;
|
| + base::ScopedTempDir temp_dir_;
|
| + scoped_ptr<TestingProfile> profile_;
|
| + scoped_ptr<DefaultSearchManager> default_search_manager_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(DefaultSearchPrefMigrationTest);
|
| };
|
| @@ -41,11 +48,10 @@ DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() {
|
| }
|
|
|
| void DefaultSearchPrefMigrationTest::SetUp() {
|
| - test_util_.SetUp();
|
| -}
|
| -
|
| -void DefaultSearchPrefMigrationTest::TearDown() {
|
| - test_util_.TearDown();
|
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
|
| + profile_.reset(new TestingProfile(temp_dir_.path()));
|
| + default_search_manager_.reset(new DefaultSearchManager(
|
| + profile_->GetPrefs(), DefaultSearchManager::ObserverCallback()));
|
| }
|
|
|
| scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword(
|
| @@ -56,7 +62,7 @@ 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();
|
| }
|
|
|
| @@ -64,46 +70,74 @@ TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) {
|
| scoped_ptr<TemplateURL> t_url(
|
| CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
|
| // Store a value in the legacy location.
|
| - test_util()->model()->SaveDefaultSearchProviderToPrefs(
|
| - t_url.get(), test_util()->profile()->GetPrefs());
|
| + TemplateURLService::SaveDefaultSearchProviderToPrefs(t_url.get(),
|
| + profile()->GetPrefs());
|
|
|
| // Run the migration.
|
| - ConfigureDefaultSearchPrefMigrationToDictionaryValue(
|
| - test_util()->profile()->GetPrefs());
|
| + ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
|
|
|
| // Test that it was migrated.
|
| - DefaultSearchManager manager(test_util()->profile()->GetPrefs(),
|
| - DefaultSearchManager::ObserverCallback());
|
| - TemplateURLData* modern_default = manager.GetDefaultSearchEngine(NULL);
|
| + DefaultSearchManager::Source source;
|
| + const TemplateURLData* modern_default =
|
| + default_search_manager()->GetDefaultSearchEngine(&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, MigrateOnlyOnce) {
|
| + scoped_ptr<TemplateURL> t_url(
|
| + CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
|
| + // Store a value in the legacy location.
|
| + TemplateURLService::SaveDefaultSearchProviderToPrefs(t_url.get(),
|
| + profile()->GetPrefs());
|
| +
|
| + // Run the migration.
|
| + ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
|
| +
|
| + // Test that it was migrated.
|
| + DefaultSearchManager::Source source;
|
| + const TemplateURLData* modern_default =
|
| + default_search_manager()->GetDefaultSearchEngine(&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());
|
| + default_search_manager()->ClearUserSelectedDefaultSearchEngine();
|
| +
|
| + // Run the migration.
|
| + ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
|
| +
|
| + // Test that it was NOT migrated.
|
| + modern_default = default_search_manager()->GetDefaultSearchEngine(&source);
|
| + ASSERT_TRUE(modern_default);
|
| + EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
|
| +}
|
| +
|
| TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) {
|
| scoped_ptr<TemplateURL> t_url(
|
| CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
|
| scoped_ptr<TemplateURL> t_url2(
|
| CreateKeyword("name2", "key2", "http://foo2/{searchTerms}"));
|
| // Store a value in the legacy location.
|
| - test_util()->model()->SaveDefaultSearchProviderToPrefs(
|
| - t_url.get(), test_util()->profile()->GetPrefs());
|
| + TemplateURLService::SaveDefaultSearchProviderToPrefs(t_url.get(),
|
| + profile()->GetPrefs());
|
|
|
| // Store another value in the modern location.
|
| - DefaultSearchManager(test_util()->profile()->GetPrefs(),
|
| - DefaultSearchManager::ObserverCallback())
|
| - .SetUserSelectedDefaultSearchEngine(t_url2->data());
|
| + default_search_manager()->SetUserSelectedDefaultSearchEngine(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.
|
| - DefaultSearchManager manager(test_util()->profile()->GetPrefs(),
|
| - DefaultSearchManager::ObserverCallback());
|
| - TemplateURLData* modern_default = manager.GetDefaultSearchEngine(NULL);
|
| + DefaultSearchManager::Source source;
|
| + const TemplateURLData* modern_default =
|
| + default_search_manager()->GetDefaultSearchEngine(&source);
|
| ASSERT_TRUE(modern_default);
|
| + 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());
|
| @@ -111,70 +145,21 @@ TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) {
|
|
|
| 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(
|
| + *default_search_manager()->GetDefaultSearchEngine(&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.
|
| - ASSERT_EQ(DefaultSearchManager::FROM_FALLBACK,
|
| - DefaultSearchManager(test_util()->profile()->GetPrefs(),
|
| - DefaultSearchManager::ObserverCallback())
|
| - .GetDefaultSearchEngineSource());
|
| -}
|
| -
|
| -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.
|
| + TemplateURLService::SaveDefaultSearchProviderToPrefs(&prepopulated_turl,
|
| + profile()->GetPrefs());
|
|
|
| // Run the migration.
|
| - ConfigureDefaultSearchPrefMigrationToDictionaryValue(
|
| - test_util()->profile()->GetPrefs());
|
| -
|
| - // TODO(caitkp/erikwright): Look into loading policy values in tests. In
|
| - // practice, the DefaultSearchEngineSource() would be FROM_POLICY in this
|
| - // case, but since we are not loading the policy here, it will be
|
| - // FROM_FALLBACK instead.
|
| - // Test that the policy-defined value is not migrated.
|
| - ASSERT_EQ(DefaultSearchManager::FROM_FALLBACK,
|
| - DefaultSearchManager(test_util()->profile()->GetPrefs(),
|
| - DefaultSearchManager::ObserverCallback())
|
| - .GetDefaultSearchEngineSource());
|
| + ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
|
| +
|
| + // Test that the legacy value is not migrated, as it is not user-selected.
|
| + default_search_manager()->GetDefaultSearchEngine(&source);
|
| + EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
|
| }
|
|
|