OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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. | |
4 | |
5 #include "base/command_line.h" | |
6 #include "base/prefs/pref_service.h" | |
7 #include "chrome/browser/search_engines/template_url_service.h" | |
8 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
9 #include "chrome/browser/ui/search/search.h" | |
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
11 #include "chrome/common/chrome_switches.h" | |
12 #include "chrome/common/pref_names.h" | |
13 #include "chrome/test/base/browser_with_test_window_test.h" | |
14 #include "chrome/test/base/ui_test_utils.h" | |
15 #include "content/public/browser/web_contents.h" | |
16 | |
17 namespace chrome { | |
18 namespace search { | |
19 | |
20 TEST(EmbeddedSearchFieldTrialTest, GetFieldTrialInfo) { | |
21 FieldTrialFlags flags; | |
22 uint64 group_number = 0; | |
23 const uint64 ZERO = 0; | |
24 | |
25 EXPECT_FALSE(GetFieldTrialInfo("", &flags, &group_number)); | |
26 EXPECT_EQ(ZERO, group_number); | |
27 EXPECT_EQ(ZERO, flags.size()); | |
28 | |
29 EXPECT_TRUE(GetFieldTrialInfo("Group77", &flags, &group_number)); | |
30 EXPECT_EQ(uint64(77), group_number); | |
31 EXPECT_EQ(ZERO, flags.size()); | |
32 | |
33 group_number = 0; | |
34 EXPECT_FALSE(GetFieldTrialInfo("Group77.2", &flags, &group_number)); | |
35 EXPECT_EQ(ZERO, group_number); | |
36 EXPECT_EQ(ZERO, flags.size()); | |
37 | |
38 EXPECT_FALSE(GetFieldTrialInfo("Invalid77", &flags, &group_number)); | |
39 EXPECT_EQ(ZERO, group_number); | |
40 EXPECT_EQ(ZERO, flags.size()); | |
41 | |
42 EXPECT_FALSE(GetFieldTrialInfo("Invalid77", &flags, NULL)); | |
43 EXPECT_EQ(ZERO, flags.size()); | |
44 | |
45 EXPECT_TRUE(GetFieldTrialInfo("Group77 ", &flags, &group_number)); | |
46 EXPECT_EQ(uint64(77), group_number); | |
47 EXPECT_EQ(ZERO, flags.size()); | |
48 | |
49 group_number = 0; | |
50 flags.clear(); | |
51 EXPECT_EQ(uint64(9999), GetUInt64ValueForFlagWithDefault("foo", 9999, flags)); | |
52 EXPECT_TRUE(GetFieldTrialInfo("Group77 foo:6", &flags, &group_number)); | |
53 EXPECT_EQ(uint64(77), group_number); | |
54 EXPECT_EQ(uint64(1), flags.size()); | |
55 EXPECT_EQ(uint64(6), GetUInt64ValueForFlagWithDefault("foo", 9999, flags)); | |
56 | |
57 group_number = 0; | |
58 flags.clear(); | |
59 EXPECT_TRUE(GetFieldTrialInfo( | |
60 "Group77 bar:1 baz:7 cat:dogs", &flags, &group_number)); | |
61 EXPECT_EQ(uint64(77), group_number); | |
62 EXPECT_EQ(uint64(3), flags.size()); | |
63 EXPECT_EQ(true, GetBoolValueForFlagWithDefault("bar", false, flags)); | |
64 EXPECT_EQ(uint64(7), GetUInt64ValueForFlagWithDefault("baz", 0, flags)); | |
65 EXPECT_EQ("dogs", GetStringValueForFlagWithDefault("cat", "", flags)); | |
66 EXPECT_EQ("default", GetStringValueForFlagWithDefault( | |
67 "moose", "default", flags)); | |
68 | |
69 group_number = 0; | |
70 flags.clear(); | |
71 EXPECT_FALSE(GetFieldTrialInfo( | |
72 "Group77 bar:1 baz:7 cat:dogs DISABLED", &flags, &group_number)); | |
73 EXPECT_EQ(ZERO, group_number); | |
74 EXPECT_EQ(ZERO, flags.size()); | |
75 } | |
76 | |
77 class SearchTest : public BrowserWithTestWindowTest { | |
78 protected: | |
79 virtual void SetUp() OVERRIDE { | |
80 BrowserWithTestWindowTest::SetUp(); | |
81 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
82 profile(), &TemplateURLServiceFactory::BuildInstanceFor); | |
83 TemplateURLService* template_url_service = | |
84 TemplateURLServiceFactory::GetForProfile(profile()); | |
85 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); | |
86 | |
87 TemplateURLData data; | |
88 data.SetURL("http://foo.com/url?bar={searchTerms}"); | |
89 data.instant_url = "http://foo.com/instant?foo=foo#foo=foo"; | |
90 data.alternate_urls.push_back("http://foo.com/alt#quux={searchTerms}"); | |
91 data.search_terms_replacement_key = "strk"; | |
92 | |
93 TemplateURL* template_url = new TemplateURL(profile(), data); | |
94 // Takes ownership of |template_url|. | |
95 template_url_service->Add(template_url); | |
96 template_url_service->SetDefaultSearchProvider(template_url); | |
97 } | |
98 }; | |
99 | |
100 struct SearchTestCase { | |
101 const char* url; | |
102 bool expected_result; | |
103 const char* comment; | |
104 }; | |
105 | |
106 TEST_F(SearchTest, ShouldAssignURLToInstantRendererExtendedDisabled) { | |
107 const SearchTestCase kTestCases[] = { | |
108 {"chrome-search://foo/bar", true, ""}, | |
109 {"http://foo.com/instant", true, ""}, | |
110 {"http://foo.com/instant?foo=bar", true, ""}, | |
111 {"https://foo.com/instant", true, ""}, | |
112 {"https://foo.com/instant#foo=bar", true, ""}, | |
113 {"HtTpS://fOo.CoM/instant", true, ""}, | |
114 {"http://foo.com:80/instant", true, ""}, | |
115 {"invalid URL", false, "Invalid URL"}, | |
116 {"unknown://scheme/path", false, "Unknown scheme"}, | |
117 {"ftp://foo.com/instant", false, "Non-HTTP scheme"}, | |
118 {"http://sub.foo.com/instant", false, "Non-exact host"}, | |
119 {"http://foo.com:26/instant", false, "Non-default port"}, | |
120 {"http://foo.com/instant/bar", false, "Non-exact path"}, | |
121 {"http://foo.com/Instant", false, "Case sensitive path"}, | |
122 {"http://foo.com/", false, "Non-exact path"}, | |
123 {"https://foo.com/", false, "Non-exact path"}, | |
124 {kLocalOmniboxPopupURL, false, "Non-extended mode"}, | |
125 {"https://foo.com/url?strk", false, "Non-extended mode"}, | |
126 {"https://foo.com/alt?strk", false, "Non-extended mode"}, | |
127 }; | |
128 | |
129 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | |
130 const SearchTestCase& test = kTestCases[i]; | |
131 EXPECT_EQ(test.expected_result, | |
132 ShouldAssignURLToInstantRenderer(GURL(test.url), profile())) | |
133 << test.url << " " << test.comment; | |
134 } | |
135 } | |
136 | |
137 TEST_F(SearchTest, ShouldAssignURLToInstantRendererExtendedEnabled) { | |
138 EnableInstantExtendedAPIForTesting(); | |
139 | |
140 const SearchTestCase kTestCases[] = { | |
141 {kLocalOmniboxPopupURL, true, ""}, | |
142 {"https://foo.com/instant?strk", true, ""}, | |
143 {"https://foo.com/instant#strk", true, ""}, | |
144 {"https://foo.com/instant?strk=0", true, ""}, | |
145 {"https://foo.com/url?strk", true, ""}, | |
146 {"https://foo.com/alt?strk", true, ""}, | |
147 {"http://foo.com/instant", false, "Non-HTTPS"}, | |
148 {"http://foo.com/instant?strk", false, "Non-HTTPS"}, | |
149 {"http://foo.com/instant?strk=1", false, "Non-HTTPS"}, | |
150 {"https://foo.com/instant", false, "No search terms replacement"}, | |
151 {"https://foo.com/?strk", false, "Non-exact path"}, | |
152 }; | |
153 | |
154 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | |
155 const SearchTestCase& test = kTestCases[i]; | |
156 EXPECT_EQ(test.expected_result, | |
157 ShouldAssignURLToInstantRenderer(GURL(test.url), profile())) | |
158 << test.url << " " << test.comment; | |
159 } | |
160 } | |
161 | |
162 TEST_F(SearchTest, CoerceCommandLineURLToTemplateURL) { | |
163 TemplateURL* template_url = | |
164 TemplateURLServiceFactory::GetForProfile(profile())-> | |
165 GetDefaultSearchProvider(); | |
166 EXPECT_EQ( | |
167 GURL("https://foo.com/instant?bar=bar#bar=bar"), | |
168 CoerceCommandLineURLToTemplateURL( | |
169 GURL("http://myserver.com:9000/dev?bar=bar#bar=bar"), | |
170 template_url->instant_url_ref())); | |
171 } | |
172 | |
173 const SearchTestCase kInstantNTPTestCases[] = { | |
174 {"https://foo.com/instant?strk", true, "Valid Instant URL"}, | |
175 {"https://foo.com/instant#strk", true, "Valid Instant URL"}, | |
176 {"https://foo.com/url?strk", true, "Valid search URL"}, | |
177 {"https://foo.com/url#strk", true, "Valid search URL"}, | |
178 {"https://foo.com/alt?strk", true, "Valid alternative URL"}, | |
179 {"https://foo.com/alt#strk", true, "Valid alternative URL"}, | |
180 {"https://foo.com/url?strk&bar=", true, "No query terms"}, | |
181 {"https://foo.com/url?strk&q=abc", true, "No query terms key"}, | |
182 {"https://foo.com/url?strk#bar=abc", true, "Query terms key in ref"}, | |
183 {"https://foo.com/url?strk&bar=abc", false, "Has query terms"}, | |
184 {"http://foo.com/instant?strk=1", false, "Insecure URL"}, | |
185 {"https://foo.com/instant", false, "No search terms replacement"}, | |
186 {"chrome://blank/", false, "Chrome scheme"}, | |
187 {"chrome-search//foo", false, "Chrome-search scheme"}, | |
188 {kLocalOmniboxPopupURL, false, "Local omnibox popup"}, | |
189 {"https://bar.com/instant?strk=1", false, "Random non-search page"}, | |
190 }; | |
191 | |
192 TEST_F(SearchTest, InstantNTPExtendedEnabled) { | |
193 EnableInstantExtendedAPIForTesting(); | |
194 AddTab(browser(), GURL("chrome://blank")); | |
195 for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) { | |
196 const SearchTestCase& test = kInstantNTPTestCases[i]; | |
197 NavigateAndCommitActiveTab(GURL(test.url)); | |
198 const content::WebContents* contents = | |
199 browser()->tab_strip_model()->GetWebContentsAt(0); | |
200 EXPECT_EQ(test.expected_result, IsInstantNTP(contents)) | |
201 << test.url << " " << test.comment; | |
202 } | |
203 } | |
204 | |
205 TEST_F(SearchTest, InstantNTPExtendedDisabled) { | |
206 AddTab(browser(), GURL("chrome://blank")); | |
207 for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) { | |
208 const SearchTestCase& test = kInstantNTPTestCases[i]; | |
209 NavigateAndCommitActiveTab(GURL(test.url)); | |
210 const content::WebContents* contents = | |
211 browser()->tab_strip_model()->GetWebContentsAt(0); | |
212 EXPECT_FALSE(IsInstantNTP(contents)) << test.url << " " << test.comment; | |
213 } | |
214 } | |
215 | |
216 TEST_F(SearchTest, InstantNTPCustomNavigationEntry) { | |
217 EnableInstantExtendedAPIForTesting(); | |
218 AddTab(browser(), GURL("chrome://blank")); | |
219 for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) { | |
220 const SearchTestCase& test = kInstantNTPTestCases[i]; | |
221 NavigateAndCommitActiveTab(GURL(test.url)); | |
222 content::WebContents* contents = | |
223 browser()->tab_strip_model()->GetWebContentsAt(0); | |
224 content::NavigationController& controller = contents->GetController(); | |
225 controller.SetTransientEntry( | |
226 controller.CreateNavigationEntry(GURL("chrome://blank"), | |
227 content::Referrer(), | |
228 content::PAGE_TRANSITION_LINK, | |
229 false, | |
230 std::string(), | |
231 contents->GetBrowserContext())); | |
232 // The active entry is chrome://blank and not an NTP. | |
233 EXPECT_FALSE(IsInstantNTP(contents)); | |
234 EXPECT_EQ(test.expected_result, | |
235 NavEntryIsInstantNTP(contents, | |
236 controller.GetLastCommittedEntry())) | |
237 << test.url << " " << test.comment; | |
238 } | |
239 } | |
240 | |
241 TEST_F(SearchTest, GetInstantURLExtendedDisabled) { | |
242 // Instant is disabled, so no Instant URL. | |
243 EXPECT_EQ(GURL(), GetInstantURL(profile())); | |
244 | |
245 // Enable Instant. | |
246 profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true); | |
247 EXPECT_EQ(GURL("http://foo.com/instant?foo=foo#foo=foo"), | |
248 GetInstantURL(profile())); | |
249 | |
250 // Override the Instant URL on the commandline. | |
251 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
252 switches::kInstantURL, | |
253 "http://myserver.com:9000/dev?bar=bar#bar=bar"); | |
254 EXPECT_EQ(GURL("http://myserver.com:9000/dev?bar=bar#bar=bar"), | |
255 GetInstantURL(profile())); | |
256 } | |
257 | |
258 TEST_F(SearchTest, GetInstantURLExtendedEnabled) { | |
259 EnableInstantExtendedAPIForTesting(); | |
260 | |
261 // Instant is disabled, so no Instant URL. | |
262 EXPECT_EQ(GURL(), GetInstantURL(profile())); | |
263 | |
264 // Enable Instant. Still no Instant URL because "strk" is missing. | |
265 profile()->GetPrefs()->SetBoolean(prefs::kInstantExtendedEnabled, true); | |
266 EXPECT_EQ(GURL(), GetInstantURL(profile())); | |
267 | |
268 { | |
269 // Set an Instant URL with a valid search terms replacement key. | |
270 TemplateURLService* template_url_service = | |
271 TemplateURLServiceFactory::GetForProfile(profile()); | |
272 | |
273 TemplateURLData data; | |
274 data.SetURL("http://foo.com/url?bar={searchTerms}"); | |
275 data.instant_url = "http://foo.com/instant?foo=foo#foo=foo&strk"; | |
276 data.search_terms_replacement_key = "strk"; | |
277 | |
278 TemplateURL* template_url = new TemplateURL(profile(), data); | |
279 // Takes ownership of |template_url|. | |
280 template_url_service->Add(template_url); | |
281 template_url_service->SetDefaultSearchProvider(template_url); | |
282 } | |
283 | |
284 // Now there should be a valid Instant URL. Note the HTTPS "upgrade". | |
285 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), | |
286 GetInstantURL(profile())); | |
287 | |
288 // Enable suggest. No difference. | |
289 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); | |
290 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), | |
291 GetInstantURL(profile())); | |
292 | |
293 // Disable Instant. No difference, because suggest is still enabled. | |
294 profile()->GetPrefs()->SetBoolean(prefs::kInstantExtendedEnabled, false); | |
295 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), | |
296 GetInstantURL(profile())); | |
297 | |
298 // Override the Instant URL on the commandline. Oops, forgot "strk". | |
299 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
300 switches::kInstantURL, | |
301 "http://myserver.com:9000/dev?bar=bar#bar=bar"); | |
302 EXPECT_EQ(GURL(), GetInstantURL(profile())); | |
303 | |
304 // Override with "strk". For fun, put it in the query, instead of the ref. | |
305 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
306 switches::kInstantURL, | |
307 "http://myserver.com:9000/dev?bar=bar&strk#bar=bar"); | |
308 EXPECT_EQ(GURL("http://myserver.com:9000/dev?bar=bar&strk#bar=bar"), | |
309 GetInstantURL(profile())); | |
310 | |
311 // Disable suggest. No Instant URL. | |
312 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); | |
313 EXPECT_EQ(GURL(), GetInstantURL(profile())); | |
314 } | |
315 | |
316 } // namespace search | |
317 } // namespace chrome | |
OLD | NEW |