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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/toolbar/toolbar_model.h"
9 #include "base/command_line.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
13 #include "chrome/browser/search/search.h"
14 #include "chrome/browser/search_engines/search_terms_data.h"
15 #include "chrome/browser/search_engines/template_url.h"
16 #include "chrome/browser/search_engines/template_url_service.h"
17 #include "chrome/browser/search_engines/template_url_service_factory.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/browser/ui/toolbar/toolbar_model.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/test/base/browser_with_test_window_test.h"
23 #include "components/variations/entropy_provider.h"
24 #include "content/public/browser/navigation_entry.h"
25 #include "content/public/browser/render_process_host.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/common/ssl_status.h"
28 #include "content/public/common/url_constants.h"
29 #include "net/base/escape.h"
31 using base::ASCIIToUTF16
;
33 // Test data ------------------------------------------------------------------
39 // The expected text to display when both forms of URL replacement are
41 base::string16 expected_text_url_replacement_inactive
;
42 // The expected text to display when query extraction is active.
43 base::string16 expected_text_query_extraction
;
44 // The expected text to display when the origin chip (URL removal) is active.
45 base::string16 expected_text_origin_chip
;
46 // The expected text to display when both query extraction and URL removal are
48 base::string16 expected_text_both
;
49 bool would_perform_search_term_replacement
;
50 bool should_display_url
;
53 GURL("view-source:http://www.google.com"),
54 ASCIIToUTF16("view-source:www.google.com"),
55 ASCIIToUTF16("view-source:www.google.com"),
62 GURL("view-source:chrome://newtab/"),
63 ASCIIToUTF16("view-source:chrome://newtab"),
64 ASCIIToUTF16("view-source:chrome://newtab"),
71 GURL("chrome-extension://monkey/balls.html"),
72 ASCIIToUTF16("chrome-extension://monkey/balls.html"),
73 ASCIIToUTF16("chrome-extension://monkey/balls.html"),
80 GURL(content::kAboutBlankURL
),
81 ASCIIToUTF16(content::kAboutBlankURL
),
82 ASCIIToUTF16(content::kAboutBlankURL
),
89 GURL("http://searchurl/?q=tractor+supply"),
90 ASCIIToUTF16("searchurl/?q=tractor+supply"),
91 ASCIIToUTF16("searchurl/?q=tractor+supply"),
98 GURL("http://google.com/search?q=tractor+supply&espv=1"),
99 ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1"),
100 ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1"),
107 GURL("https://google.ca/search?q=tractor+supply"),
108 ASCIIToUTF16("https://google.ca/search?q=tractor+supply"),
109 ASCIIToUTF16("https://google.ca/search?q=tractor+supply"),
116 GURL("https://google.com/search?q=tractor+supply"),
117 ASCIIToUTF16("https://google.com/search?q=tractor+supply"),
118 ASCIIToUTF16("https://google.com/search?q=tractor+supply"),
125 GURL("https://google.com/search?q=tractor+supply&espv=1"),
126 ASCIIToUTF16("https://google.com/search?q=tractor+supply&espv=1"),
127 ASCIIToUTF16("tractor supply"),
129 ASCIIToUTF16("tractor supply"),
134 GURL("https://google.com/search?q=tractorsupply.com&espv=1"),
135 ASCIIToUTF16("https://google.com/search?q=tractorsupply.com&espv=1"),
136 ASCIIToUTF16("tractorsupply.com"),
138 ASCIIToUTF16("tractorsupply.com"),
143 GURL("https://google.com/search?q=ftp://tractorsupply.ie&espv=1"),
144 ASCIIToUTF16("https://google.com/search?q=ftp://tractorsupply.ie&espv=1"),
145 ASCIIToUTF16("ftp://tractorsupply.ie"),
147 ASCIIToUTF16("ftp://tractorsupply.ie"),
156 // ToolbarModelTest -----------------------------------------------------------
158 class ToolbarModelTest
: public BrowserWithTestWindowTest
{
161 ToolbarModelTest(Browser::Type browser_type
,
162 chrome::HostDesktopType host_desktop_type
,
164 virtual ~ToolbarModelTest();
166 // BrowserWithTestWindowTest:
167 virtual void SetUp() OVERRIDE
;
170 void NavigateAndCheckText(const GURL
& url
,
171 const base::string16
& expected_text
,
172 bool would_perform_search_term_replacement
,
173 bool should_display_url
);
174 void EnableOriginChipFieldTrial();
177 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
179 DISALLOW_COPY_AND_ASSIGN(ToolbarModelTest
);
182 ToolbarModelTest::ToolbarModelTest() {
185 ToolbarModelTest::ToolbarModelTest(
186 Browser::Type browser_type
,
187 chrome::HostDesktopType host_desktop_type
,
189 : BrowserWithTestWindowTest(browser_type
,
194 ToolbarModelTest::~ToolbarModelTest() {
197 void ToolbarModelTest::SetUp() {
198 BrowserWithTestWindowTest::SetUp();
199 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
200 profile(), &TemplateURLServiceFactory::BuildInstanceFor
);
201 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
202 profile(), &AutocompleteClassifierFactory::BuildInstanceFor
);
203 UIThreadSearchTermsData::SetGoogleBaseURL("http://google.com/");
206 void ToolbarModelTest::EnableOriginChipFieldTrial() {
207 field_trial_list_
.reset(new base::FieldTrialList(
208 new metrics::SHA1EntropyProvider("platypus")));
209 base::FieldTrialList::CreateFieldTrial(
210 "EmbeddedSearch", "Group1 espv:2 origin_chip:1");
213 void ToolbarModelTest::NavigateAndCheckText(
215 const base::string16
& expected_text
,
216 bool would_perform_search_term_replacement
,
217 bool should_display_url
) {
218 // Check while loading.
219 content::NavigationController
* controller
=
220 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
221 controller
->LoadURL(url
, content::Referrer(), content::PAGE_TRANSITION_LINK
,
223 ToolbarModel
* toolbar_model
= browser()->toolbar_model();
224 EXPECT_EQ(expected_text
, toolbar_model
->GetText());
225 EXPECT_EQ(would_perform_search_term_replacement
,
226 toolbar_model
->WouldPerformSearchTermReplacement(false));
227 EXPECT_EQ(should_display_url
, toolbar_model
->ShouldDisplayURL());
229 // Check after commit.
230 CommitPendingLoad(controller
);
231 // Fake a secure connection for HTTPS URLs, or the toolbar will refuse to
232 // extract search terms.
233 if (url
.SchemeIsSecure()) {
234 controller
->GetVisibleEntry()->GetSSL().security_style
=
235 content::SECURITY_STYLE_AUTHENTICATED
;
237 EXPECT_EQ(expected_text
, toolbar_model
->GetText());
238 EXPECT_EQ(would_perform_search_term_replacement
,
239 toolbar_model
->WouldPerformSearchTermReplacement(false));
240 EXPECT_EQ(should_display_url
, toolbar_model
->ShouldDisplayURL());
242 // Now pretend the user started modifying the omnibox.
243 toolbar_model
->set_input_in_progress(true);
244 EXPECT_FALSE(toolbar_model
->WouldPerformSearchTermReplacement(false));
245 EXPECT_EQ(would_perform_search_term_replacement
,
246 toolbar_model
->WouldPerformSearchTermReplacement(true));
248 // Tell the ToolbarModel that the user has stopped editing. This prevents
249 // this function from having side effects.
250 toolbar_model
->set_input_in_progress(false);
253 class PopupToolbarModelTest
: public ToolbarModelTest
{
255 PopupToolbarModelTest();
256 virtual ~PopupToolbarModelTest();
258 DISALLOW_COPY_AND_ASSIGN(PopupToolbarModelTest
);
261 PopupToolbarModelTest::PopupToolbarModelTest()
262 : ToolbarModelTest(Browser::TYPE_POPUP
,
263 chrome::HOST_DESKTOP_TYPE_NATIVE
,
267 PopupToolbarModelTest::~PopupToolbarModelTest() {
270 // Actual tests ---------------------------------------------------------------
272 // Test that we only replace URLs when query extraction and URL replacement
274 TEST_F(ToolbarModelTest
, ShouldDisplayURL_QueryExtraction
) {
275 AddTab(browser(), GURL(content::kAboutBlankURL
));
277 // Before we enable instant extended, query extraction is disabled.
278 EXPECT_FALSE(chrome::IsQueryExtractionEnabled())
279 << "This test expects query extraction to be disabled.";
280 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
281 const TestItem
& test_item
= test_items
[i
];
282 NavigateAndCheckText(test_item
.url
,
283 test_item
.expected_text_url_replacement_inactive
,
284 false, test_item
.should_display_url
);
287 chrome::EnableQueryExtractionForTesting();
288 EXPECT_TRUE(chrome::IsQueryExtractionEnabled());
289 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
290 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
291 const TestItem
& test_item
= test_items
[i
];
292 NavigateAndCheckText(test_item
.url
,
293 test_item
.expected_text_query_extraction
,
294 test_item
.would_perform_search_term_replacement
,
295 test_item
.should_display_url
);
298 // Disabling URL replacement should reset to only showing URLs.
299 browser()->toolbar_model()->set_url_replacement_enabled(false);
300 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
301 const TestItem
& test_item
= test_items
[i
];
302 NavigateAndCheckText(test_item
.url
,
303 test_item
.expected_text_url_replacement_inactive
,
304 false, test_item
.should_display_url
);
308 // Test that we remove or replace URLs appropriately when the origin chip is
310 TEST_F(ToolbarModelTest
, ShouldDisplayURL_OriginChip
) {
311 EnableOriginChipFieldTrial();
312 AddTab(browser(), GURL(content::kAboutBlankURL
));
314 // Check each case with the origin chip enabled but query extraction disabled.
315 EXPECT_TRUE(chrome::ShouldDisplayOriginChip());
316 EXPECT_FALSE(chrome::IsQueryExtractionEnabled());
317 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
318 const TestItem
& test_item
= test_items
[i
];
319 NavigateAndCheckText(test_item
.url
, test_item
.expected_text_origin_chip
,
320 false, test_item
.should_display_url
);
323 // Check with both enabled.
324 chrome::EnableQueryExtractionForTesting();
325 EXPECT_TRUE(chrome::IsQueryExtractionEnabled());
326 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
327 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
328 const TestItem
& test_item
= test_items
[i
];
329 NavigateAndCheckText(test_item
.url
, test_item
.expected_text_both
,
330 test_item
.would_perform_search_term_replacement
,
331 test_item
.should_display_url
);
334 // Disabling URL replacement should reset to only showing URLs.
335 browser()->toolbar_model()->set_url_replacement_enabled(false);
336 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
337 const TestItem
& test_item
= test_items
[i
];
338 NavigateAndCheckText(test_item
.url
,
339 test_item
.expected_text_url_replacement_inactive
,
340 false, test_item
.should_display_url
);
344 // Verify that search terms are extracted while the page is loading.
345 TEST_F(ToolbarModelTest
, SearchTermsWhileLoading
) {
346 chrome::EnableQueryExtractionForTesting();
347 AddTab(browser(), GURL(content::kAboutBlankURL
));
349 // While loading, we should be willing to extract search terms.
350 content::NavigationController
* controller
=
351 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
352 controller
->LoadURL(GURL("https://google.com/search?q=tractor+supply&espv=1"),
353 content::Referrer(), content::PAGE_TRANSITION_LINK
,
355 ToolbarModel
* toolbar_model
= browser()->toolbar_model();
356 controller
->GetVisibleEntry()->GetSSL().security_style
=
357 content::SECURITY_STYLE_UNKNOWN
;
358 EXPECT_TRUE(toolbar_model
->WouldPerformSearchTermReplacement(false));
360 // When done loading, we shouldn't extract search terms if we didn't get an
361 // authenticated connection.
362 CommitPendingLoad(controller
);
363 controller
->GetVisibleEntry()->GetSSL().security_style
=
364 content::SECURITY_STYLE_UNKNOWN
;
365 EXPECT_FALSE(toolbar_model
->WouldPerformSearchTermReplacement(false));
368 // When the Google base URL is overridden on the command line, we should extract
369 // search terms from URLs that start with that base URL even when they're not
371 TEST_F(ToolbarModelTest
, GoogleBaseURL
) {
372 chrome::EnableQueryExtractionForTesting();
373 AddTab(browser(), GURL(content::kAboutBlankURL
));
375 // If the Google base URL wasn't specified on the command line, then if it's
376 // HTTP, we should not extract search terms.
377 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/");
378 NavigateAndCheckText(
379 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
380 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), false, true);
382 // The same URL, when specified on the command line, should allow search term
384 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
385 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL
,
386 "http://www.foo.com/");
387 NavigateAndCheckText(
388 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
389 ASCIIToUTF16("tractor supply"), true, true);
392 // Popup windows don't have an origin chip, so test that URL display in a popup
393 // ignores whether the origin chip is enabled and only respects the query
395 TEST_F(PopupToolbarModelTest
, ShouldDisplayURL
) {
396 AddTab(browser(), GURL(content::kAboutBlankURL
));
398 // Check with neither query extraction nor the origin chip enabled.
399 EXPECT_FALSE(chrome::ShouldDisplayOriginChip());
400 EXPECT_FALSE(chrome::IsQueryExtractionEnabled());
401 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
402 const TestItem
& test_item
= test_items
[i
];
403 NavigateAndCheckText(test_item
.url
,
404 test_item
.expected_text_url_replacement_inactive
,
405 false, test_item
.should_display_url
);
408 // Check with the origin chip enabled.
409 EnableOriginChipFieldTrial();
410 EXPECT_TRUE(chrome::ShouldDisplayOriginChip());
411 EXPECT_FALSE(chrome::IsQueryExtractionEnabled());
412 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
413 const TestItem
& test_item
= test_items
[i
];
414 NavigateAndCheckText(test_item
.url
,
415 test_item
.expected_text_url_replacement_inactive
,
416 false, test_item
.should_display_url
);
419 // With both origin chip and query extraction enabled, only search term
420 // replacement should be performed.
421 chrome::EnableQueryExtractionForTesting();
422 EXPECT_TRUE(chrome::IsQueryExtractionEnabled());
423 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
424 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
425 const TestItem
& test_item
= test_items
[i
];
426 NavigateAndCheckText(test_item
.url
,
427 test_item
.expected_text_query_extraction
,
428 test_item
.would_perform_search_term_replacement
,
429 test_item
.should_display_url
);
432 // Disabling URL replacement should reset to only showing URLs.
433 browser()->toolbar_model()->set_url_replacement_enabled(false);
434 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
435 const TestItem
& test_item
= test_items
[i
];
436 NavigateAndCheckText(test_item
.url
,
437 test_item
.expected_text_url_replacement_inactive
,
438 false, test_item
.should_display_url
);