Chromium Code Reviews| 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..5fb624db03b62eb554f713b19dcdd28aef764f06 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,21 @@ |
| #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/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 +31,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_; } |
| + 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); |
| }; |
| + |
|
gab
2014/05/07 20:25:09
nit: rm empty lines.
erikwright (departed)
2014/05/07 22:26:47
Done.
|
| + |
| DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() { |
| } |
| void DefaultSearchPrefMigrationTest::SetUp() { |
| - test_util_.SetUp(); |
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + profile_.reset(new TestingProfile(temp_dir_.path())); |
| + default_search_manager_.reset(new DefaultSearchManager( |
| + profile_->GetPrefs(), DefaultSearchManager::ObserverCallback())); |
| } |
| void DefaultSearchPrefMigrationTest::TearDown() { |
| - test_util_.TearDown(); |
| + default_search_manager_.reset(); |
| + profile_.reset(); |
| +} |
| + |
| +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,7 +147,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,21 +155,49 @@ 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()); |
| + SaveDefaultSearchProviderToLegacyPrefs(t_url.get()); |
| + |
| + // 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()); |
| +} |
| + |
| +TEST_F(DefaultSearchPrefMigrationTest, MigrateOnlyOnce) { |
| + scoped_ptr<TemplateURL> t_url( |
| + CreateKeyword("name1", "key1", "http://foo1/{searchTerms}")); |
| + // Store a value in the legacy location. |
| + SaveDefaultSearchProviderToLegacyPrefs(t_url.get()); |
| // 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()); |
| + 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) { |
| @@ -87,23 +206,20 @@ TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) { |
| 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()); |
| + SaveDefaultSearchProviderToLegacyPrefs(t_url.get()); |
| // 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 +227,20 @@ 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()); |
| - |
| - // 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()); |
| -} |
| + TemplateURL prepopulated_turl(profile(), prepopulated_default); |
| -TEST_F(DefaultSearchPrefMigrationTest, ManagedValueIsNotMigrated) { |
|
gab
2014/05/07 20:25:09
Why is this test removed? Shouldn't this still be
|
| - // 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()); |
| - |
| - // 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); |
| } |