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 virtual ~ToolbarModelTest();
163 // BrowserWithTestWindowTest:
164 virtual void SetUp() OVERRIDE
;
167 void NavigateAndCheckText(const GURL
& url
,
168 const base::string16
& expected_text
,
169 bool would_perform_search_term_replacement
,
170 bool should_display_url
);
171 void EnableOriginChipFieldTrial();
174 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
176 DISALLOW_COPY_AND_ASSIGN(ToolbarModelTest
);
179 ToolbarModelTest::ToolbarModelTest() {
182 ToolbarModelTest::~ToolbarModelTest() {
185 void ToolbarModelTest::SetUp() {
186 BrowserWithTestWindowTest::SetUp();
187 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
188 profile(), &TemplateURLServiceFactory::BuildInstanceFor
);
189 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
190 profile(), &AutocompleteClassifierFactory::BuildInstanceFor
);
191 UIThreadSearchTermsData::SetGoogleBaseURL("http://google.com/");
194 void ToolbarModelTest::EnableOriginChipFieldTrial() {
195 field_trial_list_
.reset(new base::FieldTrialList(
196 new metrics::SHA1EntropyProvider("platypus")));
197 base::FieldTrialList::CreateFieldTrial(
198 "EmbeddedSearch", "Group1 espv:2 origin_chip:1");
201 void ToolbarModelTest::NavigateAndCheckText(
203 const base::string16
& expected_text
,
204 bool would_perform_search_term_replacement
,
205 bool should_display_url
) {
206 // Check while loading.
207 content::NavigationController
* controller
=
208 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
209 controller
->LoadURL(url
, content::Referrer(), content::PAGE_TRANSITION_LINK
,
211 ToolbarModel
* toolbar_model
= browser()->toolbar_model();
212 EXPECT_EQ(expected_text
, toolbar_model
->GetText());
213 EXPECT_EQ(would_perform_search_term_replacement
,
214 toolbar_model
->WouldPerformSearchTermReplacement(false));
215 EXPECT_EQ(should_display_url
, toolbar_model
->ShouldDisplayURL());
217 // Check after commit.
218 CommitPendingLoad(controller
);
219 // Fake a secure connection for HTTPS URLs, or the toolbar will refuse to
220 // extract search terms.
221 if (url
.SchemeIsSecure()) {
222 controller
->GetVisibleEntry()->GetSSL().security_style
=
223 content::SECURITY_STYLE_AUTHENTICATED
;
225 EXPECT_EQ(expected_text
, toolbar_model
->GetText());
226 EXPECT_EQ(would_perform_search_term_replacement
,
227 toolbar_model
->WouldPerformSearchTermReplacement(false));
228 EXPECT_EQ(should_display_url
, toolbar_model
->ShouldDisplayURL());
230 // Now pretend the user started modifying the omnibox.
231 toolbar_model
->set_input_in_progress(true);
232 EXPECT_FALSE(toolbar_model
->WouldPerformSearchTermReplacement(false));
233 EXPECT_EQ(would_perform_search_term_replacement
,
234 toolbar_model
->WouldPerformSearchTermReplacement(true));
236 // Tell the ToolbarModel that the user has stopped editing. This prevents
237 // this function from having side effects.
238 toolbar_model
->set_input_in_progress(false);
242 // Actual tests ---------------------------------------------------------------
244 // Test that we only replace URLs when query extraction and URL replacement
246 TEST_F(ToolbarModelTest
, ShouldDisplayURL_QueryExtraction
) {
247 AddTab(browser(), GURL(content::kAboutBlankURL
));
249 // Before we enable instant extended, query extraction is disabled.
250 EXPECT_FALSE(chrome::IsQueryExtractionEnabled())
251 << "This test expects query extraction to be disabled.";
252 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
253 const TestItem
& test_item
= test_items
[i
];
254 NavigateAndCheckText(test_item
.url
,
255 test_item
.expected_text_url_replacement_inactive
,
256 false, test_item
.should_display_url
);
259 chrome::EnableQueryExtractionForTesting();
260 EXPECT_TRUE(chrome::IsQueryExtractionEnabled());
261 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
262 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
263 const TestItem
& test_item
= test_items
[i
];
264 NavigateAndCheckText(test_item
.url
,
265 test_item
.expected_text_query_extraction
,
266 test_item
.would_perform_search_term_replacement
,
267 test_item
.should_display_url
);
270 // Disabling URL replacement should reset to only showing URLs.
271 browser()->toolbar_model()->set_url_replacement_enabled(false);
272 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
273 const TestItem
& test_item
= test_items
[i
];
274 NavigateAndCheckText(test_item
.url
,
275 test_item
.expected_text_url_replacement_inactive
,
276 false, test_item
.should_display_url
);
280 // Test that we remove or replace URLs appropriately when the origin chip is
282 TEST_F(ToolbarModelTest
, ShouldDisplayURL_OriginChip
) {
283 EnableOriginChipFieldTrial();
284 AddTab(browser(), GURL(content::kAboutBlankURL
));
286 // Check each case with the origin chip enabled but query extraction disabled.
287 EXPECT_TRUE(chrome::ShouldDisplayOriginChip());
288 EXPECT_FALSE(chrome::IsQueryExtractionEnabled());
289 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
290 const TestItem
& test_item
= test_items
[i
];
291 NavigateAndCheckText(test_item
.url
, test_item
.expected_text_origin_chip
,
292 false, test_item
.should_display_url
);
295 // Check with both enabled.
296 chrome::EnableQueryExtractionForTesting();
297 EXPECT_TRUE(chrome::IsQueryExtractionEnabled());
298 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
299 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
300 const TestItem
& test_item
= test_items
[i
];
301 NavigateAndCheckText(test_item
.url
, test_item
.expected_text_both
,
302 test_item
.would_perform_search_term_replacement
,
303 test_item
.should_display_url
);
306 // Disabling URL replacement should reset to only showing URLs.
307 browser()->toolbar_model()->set_url_replacement_enabled(false);
308 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
309 const TestItem
& test_item
= test_items
[i
];
310 NavigateAndCheckText(test_item
.url
,
311 test_item
.expected_text_url_replacement_inactive
,
312 false, test_item
.should_display_url
);
316 // Verify that search terms are extracted while the page is loading.
317 TEST_F(ToolbarModelTest
, SearchTermsWhileLoading
) {
318 chrome::EnableQueryExtractionForTesting();
319 AddTab(browser(), GURL(content::kAboutBlankURL
));
321 // While loading, we should be willing to extract search terms.
322 content::NavigationController
* controller
=
323 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
324 controller
->LoadURL(GURL("https://google.com/search?q=tractor+supply&espv=1"),
325 content::Referrer(), content::PAGE_TRANSITION_LINK
,
327 ToolbarModel
* toolbar_model
= browser()->toolbar_model();
328 controller
->GetVisibleEntry()->GetSSL().security_style
=
329 content::SECURITY_STYLE_UNKNOWN
;
330 EXPECT_TRUE(toolbar_model
->WouldPerformSearchTermReplacement(false));
332 // When done loading, we shouldn't extract search terms if we didn't get an
333 // authenticated connection.
334 CommitPendingLoad(controller
);
335 controller
->GetVisibleEntry()->GetSSL().security_style
=
336 content::SECURITY_STYLE_UNKNOWN
;
337 EXPECT_FALSE(toolbar_model
->WouldPerformSearchTermReplacement(false));
340 // When the Google base URL is overridden on the command line, we should extract
341 // search terms from URLs that start with that base URL even when they're not
343 TEST_F(ToolbarModelTest
, GoogleBaseURL
) {
344 chrome::EnableQueryExtractionForTesting();
345 AddTab(browser(), GURL(content::kAboutBlankURL
));
347 // If the Google base URL wasn't specified on the command line, then if it's
348 // HTTP, we should not extract search terms.
349 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/");
350 NavigateAndCheckText(
351 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
352 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), false, true);
354 // The same URL, when specified on the command line, should allow search term
356 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
357 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL
,
358 "http://www.foo.com/");
359 NavigateAndCheckText(
360 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
361 ASCIIToUTF16("tractor supply"), true, true);