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 "chrome/browser/ui/toolbar/toolbar_model.h" | 5 #include "chrome/browser/ui/toolbar/toolbar_model.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 11 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
12 #include "chrome/browser/search/search.h" | 12 #include "chrome/browser/search/search.h" |
| 13 #include "chrome/browser/search_engines/search_terms_data.h" |
13 #include "chrome/browser/search_engines/template_url.h" | 14 #include "chrome/browser/search_engines/template_url.h" |
14 #include "chrome/browser/search_engines/template_url_service.h" | 15 #include "chrome/browser/search_engines/template_url_service.h" |
15 #include "chrome/browser/search_engines/template_url_service_factory.h" | 16 #include "chrome/browser/search_engines/template_url_service_factory.h" |
16 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
18 #include "chrome/browser/ui/toolbar/toolbar_model.h" | 19 #include "chrome/browser/ui/toolbar/toolbar_model.h" |
19 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
20 #include "chrome/test/base/browser_with_test_window_test.h" | 21 #include "chrome/test/base/browser_with_test_window_test.h" |
21 #include "content/public/browser/navigation_entry.h" | 22 #include "content/public/browser/navigation_entry.h" |
22 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 | 176 |
176 ToolbarModelTest::~ToolbarModelTest() { | 177 ToolbarModelTest::~ToolbarModelTest() { |
177 } | 178 } |
178 | 179 |
179 void ToolbarModelTest::SetUp() { | 180 void ToolbarModelTest::SetUp() { |
180 BrowserWithTestWindowTest::SetUp(); | 181 BrowserWithTestWindowTest::SetUp(); |
181 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 182 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
182 profile(), &TemplateURLServiceFactory::BuildInstanceFor); | 183 profile(), &TemplateURLServiceFactory::BuildInstanceFor); |
183 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( | 184 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( |
184 profile(), &AutocompleteClassifierFactory::BuildInstanceFor); | 185 profile(), &AutocompleteClassifierFactory::BuildInstanceFor); |
| 186 UIThreadSearchTermsData::SetGoogleBaseURL("http://google.com/"); |
185 } | 187 } |
186 | 188 |
187 void ToolbarModelTest::ResetDefaultTemplateURL() { | 189 void ToolbarModelTest::ResetDefaultTemplateURL() { |
188 ResetTemplateURLForInstant(GURL("http://does/not/exist")); | 190 ResetTemplateURLForInstant(GURL("http://does/not/exist")); |
189 } | 191 } |
190 | 192 |
191 void ToolbarModelTest::NavigateAndCheckText( | 193 void ToolbarModelTest::NavigateAndCheckText( |
192 const GURL& url, | 194 const GURL& url, |
193 const string16& expected_text, | 195 const string16& expected_text, |
194 const string16& expected_replace_text, | 196 const string16& expected_replace_text, |
195 bool would_replace, | 197 bool would_replace, |
196 bool should_display) { | 198 bool should_display) { |
197 NavigateAndCheckTextImpl(url, false, expected_text, would_replace, | 199 NavigateAndCheckTextImpl(url, false, expected_text, would_replace, |
198 should_display); | 200 should_display); |
199 NavigateAndCheckTextImpl(url, true, expected_replace_text, would_replace, | 201 NavigateAndCheckTextImpl(url, true, expected_replace_text, would_replace, |
200 should_display); | 202 should_display); |
201 } | 203 } |
202 | 204 |
203 void ToolbarModelTest::ResetTemplateURLForInstant(const GURL& instant_url) { | 205 void ToolbarModelTest::ResetTemplateURLForInstant(const GURL& instant_url) { |
204 TemplateURLData data; | 206 TemplateURLData data; |
205 data.short_name = ASCIIToUTF16("Google"); | 207 data.short_name = ASCIIToUTF16("Google"); |
206 data.SetURL("http://google.com/search?q={searchTerms}"); | 208 data.SetURL("{google:baseURL}search?q={searchTerms}"); |
207 data.instant_url = instant_url.spec(); | 209 data.instant_url = instant_url.spec(); |
208 data.search_terms_replacement_key = "{google:instantExtendedEnabledKey}"; | 210 data.search_terms_replacement_key = "{google:instantExtendedEnabledKey}"; |
209 TemplateURL* search_template_url = new TemplateURL(profile(), data); | 211 TemplateURL* search_template_url = new TemplateURL(profile(), data); |
210 TemplateURLService* template_url_service = | 212 TemplateURLService* template_url_service = |
211 TemplateURLServiceFactory::GetForProfile(profile()); | 213 TemplateURLServiceFactory::GetForProfile(profile()); |
212 template_url_service->Add(search_template_url); | 214 template_url_service->Add(search_template_url); |
213 template_url_service->SetDefaultSearchProvider(search_template_url); | 215 template_url_service->SetDefaultSearchProvider(search_template_url); |
214 ASSERT_NE(0, search_template_url->id()); | 216 ASSERT_NE(0, search_template_url->id()); |
215 template_url_service->Load(); | 217 template_url_service->Load(); |
216 } | 218 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 toolbar_model->WouldReplaceSearchURLWithSearchTerms()); | 251 toolbar_model->WouldReplaceSearchURLWithSearchTerms()); |
250 } | 252 } |
251 | 253 |
252 | 254 |
253 // Actual tests --------------------------------------------------------------- | 255 // Actual tests --------------------------------------------------------------- |
254 | 256 |
255 // Test that we don't replace any URLs when the query extraction is disabled. | 257 // Test that we don't replace any URLs when the query extraction is disabled. |
256 TEST_F(ToolbarModelTest, ShouldDisplayURLQueryExtractionDisabled) { | 258 TEST_F(ToolbarModelTest, ShouldDisplayURLQueryExtractionDisabled) { |
257 ASSERT_FALSE(chrome::IsQueryExtractionEnabled()) | 259 ASSERT_FALSE(chrome::IsQueryExtractionEnabled()) |
258 << "This test expects query extraction to be disabled."; | 260 << "This test expects query extraction to be disabled."; |
259 ResetDefaultTemplateURL(); | |
260 AddTab(browser(), GURL(content::kAboutBlankURL)); | 261 AddTab(browser(), GURL(content::kAboutBlankURL)); |
261 for (size_t i = 0; i < arraysize(test_items); ++i) { | 262 for (size_t i = 0; i < arraysize(test_items); ++i) { |
262 const TestItem& test_item = test_items[i]; | 263 const TestItem& test_item = test_items[i]; |
263 NavigateAndCheckText(test_item.url, | 264 NavigateAndCheckText(test_item.url, test_item.expected_text, |
264 test_item.expected_text, | 265 test_item.expected_replace_text_inactive, false, |
265 test_item.expected_replace_text_inactive, | |
266 false, | |
267 test_item.should_display); | 266 test_item.should_display); |
268 } | 267 } |
269 } | 268 } |
270 | 269 |
271 // Test that we replace URLs when the query extraction API is enabled. | 270 // Test that we replace URLs when the query extraction API is enabled. |
272 TEST_F(ToolbarModelTest, ShouldDisplayURLQueryExtractionEnabled) { | 271 TEST_F(ToolbarModelTest, ShouldDisplayURLQueryExtractionEnabled) { |
273 chrome::EnableInstantExtendedAPIForTesting(); | 272 chrome::EnableInstantExtendedAPIForTesting(); |
274 ResetDefaultTemplateURL(); | |
275 AddTab(browser(), GURL(content::kAboutBlankURL)); | 273 AddTab(browser(), GURL(content::kAboutBlankURL)); |
276 for (size_t i = 0; i < arraysize(test_items); ++i) { | 274 for (size_t i = 0; i < arraysize(test_items); ++i) { |
277 const TestItem& test_item = test_items[i]; | 275 const TestItem& test_item = test_items[i]; |
278 NavigateAndCheckText(test_item.url, | 276 NavigateAndCheckText(test_item.url, test_item.expected_text, |
279 test_item.expected_text, | |
280 test_item.expected_replace_text_active, | 277 test_item.expected_replace_text_active, |
281 test_item.would_replace, | 278 test_item.would_replace, test_item.should_display); |
282 test_item.should_display); | |
283 } | 279 } |
284 } | 280 } |
285 | 281 |
286 // Verify that search terms are extracted while the page is loading. | 282 // Verify that search terms are extracted while the page is loading. |
287 TEST_F(ToolbarModelTest, SearchTermsWhileLoading) { | 283 TEST_F(ToolbarModelTest, SearchTermsWhileLoading) { |
288 chrome::EnableInstantExtendedAPIForTesting(); | 284 chrome::EnableInstantExtendedAPIForTesting(); |
289 ResetDefaultTemplateURL(); | 285 ResetDefaultTemplateURL(); |
290 AddTab(browser(), GURL(content::kAboutBlankURL)); | 286 AddTab(browser(), GURL(content::kAboutBlankURL)); |
291 | 287 |
292 // While loading, we should be willing to extract search terms. | 288 // While loading, we should be willing to extract search terms. |
293 content::NavigationController* controller = | 289 content::NavigationController* controller = |
294 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); | 290 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); |
295 controller->LoadURL(GURL("https://google.com/search?q=tractor+supply&espv=1"), | 291 controller->LoadURL(GURL("https://google.com/search?q=tractor+supply&espv=1"), |
296 content::Referrer(), content::PAGE_TRANSITION_LINK, | 292 content::Referrer(), content::PAGE_TRANSITION_LINK, |
297 std::string()); | 293 std::string()); |
298 ToolbarModel* toolbar_model = browser()->toolbar_model(); | 294 ToolbarModel* toolbar_model = browser()->toolbar_model(); |
299 controller->GetVisibleEntry()->GetSSL().security_style = | 295 controller->GetVisibleEntry()->GetSSL().security_style = |
300 content::SECURITY_STYLE_UNKNOWN; | 296 content::SECURITY_STYLE_UNKNOWN; |
301 EXPECT_TRUE(toolbar_model->WouldReplaceSearchURLWithSearchTerms()); | 297 EXPECT_TRUE(toolbar_model->WouldReplaceSearchURLWithSearchTerms()); |
302 | 298 |
303 // When done loading, we shouldn't extract search terms if we didn't get an | 299 // When done loading, we shouldn't extract search terms if we didn't get an |
304 // authenticated connection. | 300 // authenticated connection. |
305 CommitPendingLoad(controller); | 301 CommitPendingLoad(controller); |
306 controller->GetVisibleEntry()->GetSSL().security_style = | 302 controller->GetVisibleEntry()->GetSSL().security_style = |
307 content::SECURITY_STYLE_UNKNOWN; | 303 content::SECURITY_STYLE_UNKNOWN; |
308 EXPECT_FALSE(toolbar_model->WouldReplaceSearchURLWithSearchTerms()); | 304 EXPECT_FALSE(toolbar_model->WouldReplaceSearchURLWithSearchTerms()); |
309 } | 305 } |
| 306 |
| 307 // When the Google base URL is overridden on the command line, we should extract |
| 308 // search terms from URLs that start with that base URL even when they're not |
| 309 // secure. |
| 310 TEST_F(ToolbarModelTest, GoogleBaseURL) { |
| 311 chrome::EnableInstantExtendedAPIForTesting(); |
| 312 AddTab(browser(), GURL(content::kAboutBlankURL)); |
| 313 |
| 314 // If the Google base URL wasn't specified on the command line, then if it's |
| 315 // HTTP, we should not extract search terms. |
| 316 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/"); |
| 317 NavigateAndCheckText( |
| 318 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"), |
| 319 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), |
| 320 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), false, |
| 321 true); |
| 322 |
| 323 // The same URL, when specified on the command line, should allow search term |
| 324 // extraction. |
| 325 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); |
| 326 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL, |
| 327 "http://www.foo.com/"); |
| 328 NavigateAndCheckText( |
| 329 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"), |
| 330 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), |
| 331 ASCIIToUTF16("tractor supply"), true, true); |
| 332 } |
OLD | NEW |