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 bc245f9d9c6b62639da987d815245890dc1ac95c..181367013f1504b66ae43d2402081e65e3ddfa6c 100644 |
--- a/chrome/browser/search_engines/default_search_pref_migration_unittest.cc |
+++ b/chrome/browser/search_engines/default_search_pref_migration_unittest.cc |
@@ -7,14 +7,12 @@ |
#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" |
@@ -25,21 +23,16 @@ |
// 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); |
- TestingProfile* profile() { return profile_.get(); } |
- |
- DefaultSearchManager* default_search_manager() { |
- return default_search_manager_.get(); |
- } |
+ TemplateURLServiceTestUtil* test_util() { return &test_util_; } |
private: |
- base::ScopedTempDir temp_dir_; |
- scoped_ptr<TestingProfile> profile_; |
- scoped_ptr<DefaultSearchManager> default_search_manager_; |
+ TemplateURLServiceTestUtil test_util_; |
DISALLOW_COPY_AND_ASSIGN(DefaultSearchPrefMigrationTest); |
}; |
@@ -48,10 +41,11 @@ |
} |
void DefaultSearchPrefMigrationTest::SetUp() { |
- ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
- profile_.reset(new TestingProfile(temp_dir_.path())); |
- default_search_manager_.reset(new DefaultSearchManager( |
- profile_->GetPrefs(), DefaultSearchManager::ObserverCallback())); |
+ test_util_.SetUp(); |
+} |
+ |
+void DefaultSearchPrefMigrationTest::TearDown() { |
+ test_util_.TearDown(); |
} |
scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword( |
@@ -62,7 +56,7 @@ |
data.short_name = base::ASCIIToUTF16(short_name); |
data.SetKeyword(base::ASCIIToUTF16(keyword)); |
data.SetURL(url); |
- scoped_ptr<TemplateURL> t_url(new TemplateURL(profile(), data)); |
+ scoped_ptr<TemplateURL> t_url(new TemplateURL(test_util_.profile(), data)); |
return t_url.Pass(); |
} |
@@ -70,51 +64,21 @@ |
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()); |
+ test_util()->model()->SaveDefaultSearchProviderToPrefs( |
+ t_url.get(), test_util()->profile()->GetPrefs()); |
// Run the migration. |
- ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs()); |
+ ConfigureDefaultSearchPrefMigrationToDictionaryValue( |
+ test_util()->profile()->GetPrefs()); |
// Test that it was migrated. |
- DefaultSearchManager::Source source; |
- const TemplateURLData* modern_default = |
- default_search_manager()->GetDefaultSearchEngine(&source); |
+ DefaultSearchManager manager(test_util()->profile()->GetPrefs(), |
+ DefaultSearchManager::ObserverCallback()); |
+ TemplateURLData* modern_default = manager.GetDefaultSearchEngine(NULL); |
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) { |
@@ -123,21 +87,23 @@ |
scoped_ptr<TemplateURL> t_url2( |
CreateKeyword("name2", "key2", "http://foo2/{searchTerms}")); |
// Store a value in the legacy location. |
- TemplateURLService::SaveDefaultSearchProviderToPrefs(t_url.get(), |
- profile()->GetPrefs()); |
+ test_util()->model()->SaveDefaultSearchProviderToPrefs( |
+ t_url.get(), test_util()->profile()->GetPrefs()); |
// Store another value in the modern location. |
- default_search_manager()->SetUserSelectedDefaultSearchEngine(t_url2->data()); |
+ DefaultSearchManager(test_util()->profile()->GetPrefs(), |
+ DefaultSearchManager::ObserverCallback()) |
+ .SetUserSelectedDefaultSearchEngine(t_url2->data()); |
// Run the migration. |
- ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs()); |
+ ConfigureDefaultSearchPrefMigrationToDictionaryValue( |
+ test_util()->profile()->GetPrefs()); |
// Test that no migration occurred. The modern value is left intact. |
- DefaultSearchManager::Source source; |
- const TemplateURLData* modern_default = |
- default_search_manager()->GetDefaultSearchEngine(&source); |
+ DefaultSearchManager manager(test_util()->profile()->GetPrefs(), |
+ DefaultSearchManager::ObserverCallback()); |
+ TemplateURLData* modern_default = manager.GetDefaultSearchEngine(NULL); |
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()); |
@@ -145,21 +111,70 @@ |
TEST_F(DefaultSearchPrefMigrationTest, |
AutomaticallySelectedValueIsNotMigrated) { |
- DefaultSearchManager::Source source; |
- TemplateURLData prepopulated_default( |
- *default_search_manager()->GetDefaultSearchEngine(&source)); |
- EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source); |
- |
- TemplateURL prepopulated_turl(profile(), prepopulated_default); |
- |
- // Store a value in the legacy location. |
- TemplateURLService::SaveDefaultSearchProviderToPrefs(&prepopulated_turl, |
- profile()->GetPrefs()); |
+ 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); |
// Run the migration. |
- ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs()); |
+ ConfigureDefaultSearchPrefMigrationToDictionaryValue( |
+ test_util()->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); |
+ 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); |
+ |
+ // 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()); |
+} |