OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/memory/scoped_vector.h" | 6 #include "base/memory/scoped_vector.h" |
7 #include "base/scoped_temp_dir.h" | 7 #include "base/scoped_temp_dir.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/search_engines/search_engine_type.h" | |
10 #include "chrome/browser/search_engines/search_terms_data.h" | 9 #include "chrome/browser/search_engines/search_terms_data.h" |
11 #include "chrome/browser/search_engines/template_url.h" | 10 #include "chrome/browser/search_engines/template_url.h" |
12 #include "chrome/browser/search_engines/template_url_service.h" | 11 #include "chrome/browser/search_engines/template_url_service.h" |
13 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 12 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
14 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
15 #include "chrome/test/base/testing_pref_service.h" | 14 #include "chrome/test/base/testing_pref_service.h" |
16 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "grit/generated_resources.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "ui/base/l10n/l10n_util.h" |
18 | 19 |
19 typedef testing::Test TemplateURLPrepopulateDataTest; | 20 typedef testing::Test TemplateURLPrepopulateDataTest; |
20 | 21 |
21 const int kCountryIds[] = { | 22 const int kCountryIds[] = { |
22 'A'<<8|'D', 'A'<<8|'E', 'A'<<8|'F', 'A'<<8|'G', 'A'<<8|'I', | 23 'A'<<8|'D', 'A'<<8|'E', 'A'<<8|'F', 'A'<<8|'G', 'A'<<8|'I', |
23 'A'<<8|'L', 'A'<<8|'M', 'A'<<8|'N', 'A'<<8|'O', 'A'<<8|'Q', | 24 'A'<<8|'L', 'A'<<8|'M', 'A'<<8|'N', 'A'<<8|'O', 'A'<<8|'Q', |
24 'A'<<8|'R', 'A'<<8|'S', 'A'<<8|'T', 'A'<<8|'U', 'A'<<8|'W', | 25 'A'<<8|'R', 'A'<<8|'S', 'A'<<8|'T', 'A'<<8|'U', 'A'<<8|'W', |
25 'A'<<8|'X', 'A'<<8|'Z', 'B'<<8|'A', 'B'<<8|'B', 'B'<<8|'D', | 26 'A'<<8|'X', 'A'<<8|'Z', 'B'<<8|'A', 'B'<<8|'B', 'B'<<8|'D', |
26 'B'<<8|'E', 'B'<<8|'F', 'B'<<8|'G', 'B'<<8|'H', 'B'<<8|'I', | 27 'B'<<8|'E', 'B'<<8|'F', 'B'<<8|'G', 'B'<<8|'H', 'B'<<8|'I', |
27 'B'<<8|'J', 'B'<<8|'M', 'B'<<8|'N', 'B'<<8|'O', 'B'<<8|'R', | 28 'B'<<8|'J', 'B'<<8|'M', 'B'<<8|'N', 'B'<<8|'O', 'B'<<8|'R', |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 'T'<<8|'F', 'T'<<8|'G', 'T'<<8|'H', 'T'<<8|'J', 'T'<<8|'K', | 65 'T'<<8|'F', 'T'<<8|'G', 'T'<<8|'H', 'T'<<8|'J', 'T'<<8|'K', |
65 'T'<<8|'L', 'T'<<8|'M', 'T'<<8|'N', 'T'<<8|'O', 'T'<<8|'R', | 66 'T'<<8|'L', 'T'<<8|'M', 'T'<<8|'N', 'T'<<8|'O', 'T'<<8|'R', |
66 'T'<<8|'T', 'T'<<8|'V', 'T'<<8|'W', 'T'<<8|'Z', 'U'<<8|'A', | 67 'T'<<8|'T', 'T'<<8|'V', 'T'<<8|'W', 'T'<<8|'Z', 'U'<<8|'A', |
67 'U'<<8|'G', 'U'<<8|'M', 'U'<<8|'S', 'U'<<8|'Y', 'U'<<8|'Z', | 68 'U'<<8|'G', 'U'<<8|'M', 'U'<<8|'S', 'U'<<8|'Y', 'U'<<8|'Z', |
68 'V'<<8|'A', 'V'<<8|'C', 'V'<<8|'E', 'V'<<8|'G', 'V'<<8|'I', | 69 'V'<<8|'A', 'V'<<8|'C', 'V'<<8|'E', 'V'<<8|'G', 'V'<<8|'I', |
69 'V'<<8|'N', 'V'<<8|'U', 'W'<<8|'F', 'W'<<8|'S', 'Y'<<8|'E', | 70 'V'<<8|'N', 'V'<<8|'U', 'W'<<8|'F', 'W'<<8|'S', 'Y'<<8|'E', |
70 'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 }; | 71 'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 }; |
71 | 72 |
72 // Verifies the set of prepopulate data doesn't contain entries with duplicate | 73 // Verifies the set of prepopulate data doesn't contain entries with duplicate |
73 // ids. | 74 // ids. |
74 TEST_F(TemplateURLPrepopulateDataTest, UniqueIDs) { | 75 TEST(TemplateURLPrepopulateDataTest, UniqueIDs) { |
75 TestingProfile profile; | 76 TestingProfile profile; |
76 for (size_t i = 0; i < arraysize(kCountryIds); ++i) { | 77 for (size_t i = 0; i < arraysize(kCountryIds); ++i) { |
77 profile.GetPrefs()->SetInteger(prefs::kCountryIDAtInstall, kCountryIds[i]); | 78 profile.GetPrefs()->SetInteger(prefs::kCountryIDAtInstall, kCountryIds[i]); |
78 ScopedVector<TemplateURL> urls; | 79 ScopedVector<TemplateURL> urls; |
79 size_t default_index; | 80 size_t default_index; |
80 TemplateURLPrepopulateData::GetPrepopulatedEngines( | 81 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile.GetPrefs(), |
81 profile.GetPrefs(), &(urls.get()), &default_index); | 82 &urls.get(), &default_index); |
82 std::set<int> unique_ids; | 83 std::set<int> unique_ids; |
83 for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) { | 84 for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) { |
84 ASSERT_TRUE(unique_ids.find(urls[turl_i]->prepopulate_id()) == | 85 ASSERT_TRUE(unique_ids.find(urls[turl_i]->prepopulate_id()) == |
85 unique_ids.end()); | 86 unique_ids.end()); |
86 unique_ids.insert(urls[turl_i]->prepopulate_id()); | 87 unique_ids.insert(urls[turl_i]->prepopulate_id()); |
87 } | 88 } |
88 } | 89 } |
89 } | 90 } |
90 | 91 |
91 // Verifies that default search providers from the preferences file | 92 // Verifies that default search providers from the preferences file |
92 // override the built-in ones. | 93 // override the built-in ones. |
93 TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) { | 94 TEST(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) { |
94 TestingPrefService prefs; | 95 TestingProfile profile; |
95 TemplateURLPrepopulateData::RegisterUserPrefs(&prefs); | 96 TestingPrefService* prefs = profile.GetTestingPrefService(); |
96 prefs.SetUserPref(prefs::kSearchProviderOverridesVersion, | 97 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, |
97 Value::CreateIntegerValue(1)); | 98 Value::CreateIntegerValue(1)); |
98 ListValue* overrides = new ListValue; | 99 ListValue* overrides = new ListValue; |
99 DictionaryValue* entry = new DictionaryValue; | 100 DictionaryValue* entry = new DictionaryValue; |
100 entry->SetString("name", "foo"); | 101 entry->SetString("name", "foo"); |
101 entry->SetString("keyword", "fook"); | 102 entry->SetString("keyword", "fook"); |
102 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); | 103 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); |
103 entry->SetString("favicon_url", "http://foi.com/favicon.ico"); | 104 entry->SetString("favicon_url", "http://foi.com/favicon.ico"); |
104 entry->SetString("suggest_url", ""); | 105 entry->SetString("suggest_url", ""); |
105 entry->SetString("instant_url", ""); | 106 entry->SetString("instant_url", ""); |
106 entry->SetString("encoding", "UTF-8"); | 107 entry->SetString("encoding", "UTF-8"); |
107 entry->SetInteger("search_engine_type", 1); | 108 entry->SetInteger("search_engine_type", 1); |
108 entry->SetInteger("id", 1001); | 109 entry->SetInteger("id", 1001); |
109 overrides->Append(entry); | 110 overrides->Append(entry); |
110 prefs.SetUserPref(prefs::kSearchProviderOverrides, overrides); | 111 prefs->SetUserPref(prefs::kSearchProviderOverrides, overrides); |
111 | 112 |
112 int version = TemplateURLPrepopulateData::GetDataVersion(&prefs); | 113 int version = TemplateURLPrepopulateData::GetDataVersion(prefs); |
113 EXPECT_EQ(1, version); | 114 EXPECT_EQ(1, version); |
114 | 115 |
115 ScopedVector<TemplateURL> t_urls; | 116 ScopedVector<TemplateURL> t_urls; |
116 size_t default_index; | 117 size_t default_index; |
117 TemplateURLPrepopulateData::GetPrepopulatedEngines( | 118 TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs, &t_urls.get(), |
118 &prefs, &(t_urls.get()), &default_index); | 119 &default_index); |
119 | 120 |
120 ASSERT_EQ(1u, t_urls.size()); | 121 ASSERT_EQ(1u, t_urls.size()); |
121 EXPECT_EQ(ASCIIToUTF16("foo"), t_urls[0]->short_name()); | 122 EXPECT_EQ(ASCIIToUTF16("foo"), t_urls[0]->short_name()); |
122 EXPECT_EQ(ASCIIToUTF16("fook"), t_urls[0]->keyword()); | 123 EXPECT_EQ(ASCIIToUTF16("fook"), t_urls[0]->keyword()); |
123 EXPECT_EQ("foo.com", t_urls[0]->url()->GetHost()); | 124 EXPECT_EQ("foo.com", t_urls[0]->url()->GetHost()); |
124 EXPECT_EQ("foi.com", t_urls[0]->GetFaviconURL().host()); | 125 EXPECT_EQ("foi.com", t_urls[0]->GetFaviconURL().host()); |
125 EXPECT_EQ(1u, t_urls[0]->input_encodings().size()); | 126 EXPECT_EQ(1u, t_urls[0]->input_encodings().size()); |
126 EXPECT_EQ(1001, t_urls[0]->prepopulate_id()); | 127 EXPECT_EQ(1001, t_urls[0]->prepopulate_id()); |
127 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, t_urls[0]->search_engine_type()); | |
128 } | 128 } |
129 | 129 |
130 TEST_F(TemplateURLPrepopulateDataTest, SearchEngineFromOrigin) { | 130 TEST(TemplateURLPrepopulateDataTest, GetEngineName) { |
131 UIThreadSearchTermsData search_terms_data; | 131 EXPECT_EQ(ASCIIToUTF16("Atlas"), |
132 std::set<GURL> unique_engines; | 132 TemplateURLPrepopulateData::GetEngineName("http://search.atlas.cz/")); |
133 | 133 EXPECT_EQ(ASCIIToUTF16("Google"), |
134 { // Scoping for the profile. | 134 TemplateURLPrepopulateData::GetEngineName("http://www.google.com/")); |
135 TestingProfile profile; | 135 EXPECT_EQ(ASCIIToUTF16("example.com"), |
136 for (size_t i = 0; i < arraysize(kCountryIds); ++i) { | 136 TemplateURLPrepopulateData::GetEngineName("http://example.com/")); |
137 profile.GetPrefs()->SetInteger(prefs::kCountryIDAtInstall, | 137 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_UNKNOWN_SEARCH_ENGINE_NAME), |
138 kCountryIds[i]); | 138 TemplateURLPrepopulateData::GetEngineName("!@#")); |
139 ScopedVector<TemplateURL> urls; | |
140 size_t default_index; | |
141 TemplateURLPrepopulateData::GetPrepopulatedEngines( | |
142 profile.GetPrefs(), &(urls.get()), &default_index); | |
143 for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) { | |
144 GURL engine_url(urls[turl_i]->url()->url()); | |
145 if (!engine_url.is_valid()) { | |
146 engine_url = TemplateURLService::GenerateSearchURLUsingTermsData( | |
147 urls[turl_i], search_terms_data); | |
148 } | |
149 GURL origin = engine_url.GetOrigin(); | |
150 unique_engines.insert(origin); | |
151 } | |
152 } | |
153 } | |
154 | |
155 TestingProfile profile; | |
156 for (std::set<GURL>::iterator it = unique_engines.begin(); | |
157 it != unique_engines.end(); ++it) { | |
158 scoped_ptr<TemplateURL> found_url( | |
159 TemplateURLPrepopulateData::GetEngineForOrigin(profile.GetPrefs(), | |
160 *it)); | |
161 EXPECT_EQ( | |
162 TemplateURLService::GenerateSearchURLUsingTermsData( | |
163 found_url.get(), search_terms_data).GetOrigin(), | |
164 it->GetOrigin()); | |
165 } | |
166 | |
167 GURL not_a_search_engine("http://example.com/"); | |
168 EXPECT_EQ(NULL, TemplateURLPrepopulateData::GetEngineForOrigin( | |
169 profile.GetPrefs(), | |
170 not_a_search_engine)); | |
171 } | 139 } |
172 | 140 |
173 TEST_F(TemplateURLPrepopulateDataTest, FindPrepopulatedEngine) { | 141 TEST(TemplateURLPrepopulateDataTest, GetEngineTypeBasic) { |
| 142 EXPECT_EQ(SEARCH_ENGINE_OTHER, |
| 143 TemplateURLPrepopulateData::GetEngineType("http://example.com/")); |
| 144 EXPECT_EQ(SEARCH_ENGINE_ASK, |
| 145 TemplateURLPrepopulateData::GetEngineType("http://www.ask.com/")); |
| 146 EXPECT_EQ(SEARCH_ENGINE_OTHER, |
| 147 TemplateURLPrepopulateData::GetEngineType("http://search.atlas.cz/")); |
| 148 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, |
| 149 TemplateURLPrepopulateData::GetEngineType("http://www.google.com/")); |
| 150 } |
| 151 |
| 152 TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeAdvanced) { |
174 // Google URLs in different forms. | 153 // Google URLs in different forms. |
175 const char* kGoogleURLs[] = { | 154 const char* kGoogleURLs[] = { |
176 // Original with google:baseURL: | 155 // Original with google:baseURL: |
177 "{google:baseURL}search?{google:RLZ}{google:acceptedSuggestion}" | 156 "{google:baseURL}search?{google:RLZ}{google:acceptedSuggestion}" |
178 "{google:originalQueryForSuggestion}{google:searchFieldtrialParameter}" | 157 "{google:originalQueryForSuggestion}{google:searchFieldtrialParameter}" |
179 "{google:instantFieldTrialGroupParameter}" | 158 "{google:instantFieldTrialGroupParameter}" |
180 "sourceid=chrome&ie={inputEncoding}&q={searchTerms}", | 159 "sourceid=chrome&ie={inputEncoding}&q={searchTerms}", |
181 // Custom with google.com: | 160 // Custom with google.com: |
182 "http://google.com/search?{google:RLZ}{google:acceptedSuggestion}" | 161 "http://google.com/search?{google:RLZ}{google:acceptedSuggestion}" |
183 "{google:originalQueryForSuggestion}{google:searchFieldtrialParameter}" | 162 "{google:originalQueryForSuggestion}{google:searchFieldtrialParameter}" |
184 "{google:instantFieldTrialGroupParameter}" | 163 "{google:instantFieldTrialGroupParameter}" |
185 "sourceid=chrome&ie={inputEncoding}&q={searchTerms}", | 164 "sourceid=chrome&ie={inputEncoding}&q={searchTerms}", |
186 // Custom with a country TLD: | 165 // Custom with a country TLD: |
187 "http://www.google.ru/search?{google:RLZ}{google:acceptedSuggestion}" | 166 "http://www.google.ru/search?{google:RLZ}{google:acceptedSuggestion}" |
188 "{google:originalQueryForSuggestion}{google:searchFieldtrialParameter}" | 167 "{google:originalQueryForSuggestion}{google:searchFieldtrialParameter}" |
189 "{google:instantFieldTrialGroupParameter}" | 168 "{google:instantFieldTrialGroupParameter}" |
190 "sourceid=chrome&ie={inputEncoding}&q={searchTerms}" | 169 "sourceid=chrome&ie={inputEncoding}&q={searchTerms}" |
191 }; | 170 }; |
192 scoped_ptr<TemplateURL> t_url; | |
193 for (size_t i = 0; i < arraysize(kGoogleURLs); ++i) { | 171 for (size_t i = 0; i < arraysize(kGoogleURLs); ++i) { |
194 t_url.reset( | 172 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, |
195 TemplateURLPrepopulateData::FindPrepopulatedEngine(kGoogleURLs[i])); | 173 TemplateURLPrepopulateData::GetEngineType(kGoogleURLs[i])); |
196 ASSERT_TRUE(t_url.get()); | |
197 // Google's prepopulated ID is 1. | |
198 EXPECT_EQ(1, t_url->prepopulate_id()); | |
199 } | 174 } |
200 // Non-Google URLs. | 175 // Non-Google URLs. |
201 const char* kYahooURLs[] = { | 176 const char* kYahooURLs[] = { |
202 "http://search.yahoo.com/search?" | 177 "http://search.yahoo.com/search?" |
203 "ei={inputEncoding}&fr=crmas&p={searchTerms}", | 178 "ei={inputEncoding}&fr=crmas&p={searchTerms}", |
204 "http://search.yahoo.com/search?p={searchTerms}" | 179 "http://search.yahoo.com/search?p={searchTerms}" |
205 }; | 180 }; |
206 for (size_t i = 0; i < arraysize(kYahooURLs); ++i) { | 181 for (size_t i = 0; i < arraysize(kYahooURLs); ++i) { |
207 t_url.reset( | 182 EXPECT_EQ(SEARCH_ENGINE_YAHOO, |
208 TemplateURLPrepopulateData::FindPrepopulatedEngine(kYahooURLs[i])); | 183 TemplateURLPrepopulateData::GetEngineType(kYahooURLs[i])); |
209 ASSERT_TRUE(t_url.get()); | |
210 // Yahoo!'s prepopulated ID is 2. | |
211 EXPECT_EQ(2, t_url->prepopulate_id()); | |
212 } | 184 } |
213 // Search URL for which no prepopulated search provider exists. | 185 // Search URL for which no prepopulated search provider exists. |
214 std::string kExampleSearchURL = "http://example.net/search?q={searchTerms}"; | 186 std::string kExampleSearchURL = "http://example.net/search?q={searchTerms}"; |
215 EXPECT_FALSE(TemplateURLPrepopulateData::FindPrepopulatedEngine( | 187 EXPECT_EQ(SEARCH_ENGINE_OTHER, |
216 kExampleSearchURL)); | 188 TemplateURLPrepopulateData::GetEngineType(kExampleSearchURL)); |
217 // Invalid search URL. | 189 EXPECT_EQ(SEARCH_ENGINE_OTHER, |
218 EXPECT_FALSE(TemplateURLPrepopulateData::FindPrepopulatedEngine( | 190 TemplateURLPrepopulateData::GetEngineType("invalid:search:url")); |
219 "invalid:search:url")); | |
220 } | 191 } |
OLD | NEW |