Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / search_engines / default_search_pref_migration_unittest.cc
blob0ff5c79815b6bb12404b619145fc3a7c3d29d49b
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/search_engines/default_search_pref_migration.h"
7 #include <string>
9 #include "base/compiler_specific.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/logging.h"
12 #include "base/macros.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/test/base/testing_pref_service_syncable.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "components/search_engines/template_url.h"
19 #include "components/search_engines/template_url_service.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 class DefaultSearchPrefMigrationTest : public testing::Test {
23 public:
24 DefaultSearchPrefMigrationTest();
26 // testing::Test:
27 void SetUp() override;
29 scoped_ptr<TemplateURL> CreateKeyword(const std::string& short_name,
30 const std::string& keyword,
31 const std::string& url);
33 TestingProfile* profile() { return profile_.get(); }
35 DefaultSearchManager* default_search_manager() {
36 return default_search_manager_.get();
39 private:
40 base::ScopedTempDir temp_dir_;
41 scoped_ptr<TestingProfile> profile_;
42 scoped_ptr<DefaultSearchManager> default_search_manager_;
44 DISALLOW_COPY_AND_ASSIGN(DefaultSearchPrefMigrationTest);
47 DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() {
50 void DefaultSearchPrefMigrationTest::SetUp() {
51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
52 profile_.reset(new TestingProfile(temp_dir_.path()));
53 default_search_manager_.reset(new DefaultSearchManager(
54 profile_->GetPrefs(), DefaultSearchManager::ObserverCallback()));
57 scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword(
58 const std::string& short_name,
59 const std::string& keyword,
60 const std::string& url) {
61 TemplateURLData data;
62 data.short_name = base::ASCIIToUTF16(short_name);
63 data.SetKeyword(base::ASCIIToUTF16(keyword));
64 data.SetURL(url);
65 scoped_ptr<TemplateURL> t_url(new TemplateURL(data));
66 return t_url.Pass();
69 TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) {
70 scoped_ptr<TemplateURL> t_url(
71 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
72 // Store a value in the legacy location.
73 TemplateURLService::SaveDefaultSearchProviderToPrefs(t_url.get(),
74 profile()->GetPrefs());
76 // Run the migration.
77 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
79 // Test that it was migrated.
80 DefaultSearchManager::Source source;
81 const TemplateURLData* modern_default =
82 default_search_manager()->GetDefaultSearchEngine(&source);
83 ASSERT_TRUE(modern_default);
84 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
85 EXPECT_EQ(t_url->short_name(), modern_default->short_name);
86 EXPECT_EQ(t_url->keyword(), modern_default->keyword());
87 EXPECT_EQ(t_url->url(), modern_default->url());
90 TEST_F(DefaultSearchPrefMigrationTest, MigrateOnlyOnce) {
91 scoped_ptr<TemplateURL> t_url(
92 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
93 // Store a value in the legacy location.
94 TemplateURLService::SaveDefaultSearchProviderToPrefs(t_url.get(),
95 profile()->GetPrefs());
97 // Run the migration.
98 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
100 // Test that it was migrated.
101 DefaultSearchManager::Source source;
102 const TemplateURLData* modern_default =
103 default_search_manager()->GetDefaultSearchEngine(&source);
104 ASSERT_TRUE(modern_default);
105 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
106 EXPECT_EQ(t_url->short_name(), modern_default->short_name);
107 EXPECT_EQ(t_url->keyword(), modern_default->keyword());
108 EXPECT_EQ(t_url->url(), modern_default->url());
109 default_search_manager()->ClearUserSelectedDefaultSearchEngine();
111 // Run the migration.
112 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
114 // Test that it was NOT migrated.
115 modern_default = default_search_manager()->GetDefaultSearchEngine(&source);
116 ASSERT_TRUE(modern_default);
117 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
120 TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) {
121 scoped_ptr<TemplateURL> t_url(
122 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
123 scoped_ptr<TemplateURL> t_url2(
124 CreateKeyword("name2", "key2", "http://foo2/{searchTerms}"));
125 // Store a value in the legacy location.
126 TemplateURLService::SaveDefaultSearchProviderToPrefs(t_url.get(),
127 profile()->GetPrefs());
129 // Store another value in the modern location.
130 default_search_manager()->SetUserSelectedDefaultSearchEngine(t_url2->data());
132 // Run the migration.
133 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
135 // Test that no migration occurred. The modern value is left intact.
136 DefaultSearchManager::Source source;
137 const TemplateURLData* modern_default =
138 default_search_manager()->GetDefaultSearchEngine(&source);
139 ASSERT_TRUE(modern_default);
140 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
141 EXPECT_EQ(t_url2->short_name(), modern_default->short_name);
142 EXPECT_EQ(t_url2->keyword(), modern_default->keyword());
143 EXPECT_EQ(t_url2->url(), modern_default->url());
146 TEST_F(DefaultSearchPrefMigrationTest,
147 AutomaticallySelectedValueIsNotMigrated) {
148 DefaultSearchManager::Source source;
149 TemplateURLData prepopulated_default(
150 *default_search_manager()->GetDefaultSearchEngine(&source));
151 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
153 TemplateURL prepopulated_turl(prepopulated_default);
155 // Store a value in the legacy location.
156 TemplateURLService::SaveDefaultSearchProviderToPrefs(&prepopulated_turl,
157 profile()->GetPrefs());
159 // Run the migration.
160 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
162 // Test that the legacy value is not migrated, as it is not user-selected.
163 default_search_manager()->GetDefaultSearchEngine(&source);
164 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);