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 "components/toolbar/toolbar_model.h"
7 #include "base/command_line.h"
8 #include "base/metrics/field_trial.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
11 #include "chrome/browser/search/search.h"
12 #include "chrome/browser/search_engines/template_url_service_factory.h"
13 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
14 #include "chrome/browser/ssl/security_state_model.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/browser_with_test_window_test.h"
19 #include "components/google/core/browser/google_switches.h"
20 #include "components/search/search.h"
21 #include "components/toolbar/toolbar_model.h"
22 #include "components/variations/entropy_provider.h"
23 #include "content/public/browser/navigation_entry.h"
24 #include "content/public/common/ssl_status.h"
25 #include "content/public/common/url_constants.h"
28 // Test data ------------------------------------------------------------------
34 // The expected text to display when both forms of URL replacement are
36 base::string16 expected_text_url_replacement_inactive
;
37 // The expected text to display when query extraction is active.
38 base::string16 expected_text_query_extraction
;
39 // The expected text to display when both query extraction and URL removal are
41 base::string16 expected_text_both
;
42 bool would_perform_search_term_replacement
;
43 bool should_display_url
;
46 GURL("view-source:http://www.google.com"),
47 base::ASCIIToUTF16("view-source:www.google.com"),
48 base::ASCIIToUTF16("view-source:www.google.com"),
54 GURL("view-source:chrome://newtab/"),
55 base::ASCIIToUTF16("view-source:chrome://newtab"),
56 base::ASCIIToUTF16("view-source:chrome://newtab"),
62 GURL("chrome-extension://monkey/balls.html"),
63 base::ASCIIToUTF16("chrome-extension://monkey/balls.html"),
64 base::ASCIIToUTF16("chrome-extension://monkey/balls.html"),
70 GURL(url::kAboutBlankURL
),
71 base::ASCIIToUTF16(url::kAboutBlankURL
),
72 base::ASCIIToUTF16(url::kAboutBlankURL
),
78 GURL("http://searchurl/?q=tractor+supply"),
79 base::ASCIIToUTF16("searchurl/?q=tractor+supply"),
80 base::ASCIIToUTF16("searchurl/?q=tractor+supply"),
86 GURL("http://google.com/search?q=tractor+supply&espv=1"),
87 base::ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1"),
88 base::ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1"),
94 GURL("https://google.ca/search?q=tractor+supply"),
95 base::ASCIIToUTF16("https://google.ca/search?q=tractor+supply"),
96 base::ASCIIToUTF16("https://google.ca/search?q=tractor+supply"),
102 GURL("https://google.com/search?q=tractor+supply"),
103 base::ASCIIToUTF16("https://google.com/search?q=tractor+supply"),
104 base::ASCIIToUTF16("https://google.com/search?q=tractor+supply"),
110 GURL("https://google.com/search?q=tractor+supply&espv=1"),
111 base::ASCIIToUTF16("https://google.com/search?q=tractor+supply&espv=1"),
112 base::ASCIIToUTF16("tractor supply"),
113 base::ASCIIToUTF16("tractor supply"),
118 GURL("https://google.com/search?q=tractorsupply.com&espv=1"),
119 base::ASCIIToUTF16("https://google.com/search?q=tractorsupply.com&espv=1"),
120 base::ASCIIToUTF16("tractorsupply.com"),
121 base::ASCIIToUTF16("tractorsupply.com"),
126 GURL("https://google.com/search?q=ftp://tractorsupply.ie&espv=1"),
128 "https://google.com/search?q=ftp://tractorsupply.ie&espv=1"),
129 base::ASCIIToUTF16("ftp://tractorsupply.ie"),
130 base::ASCIIToUTF16("ftp://tractorsupply.ie"),
136 void SetSecurityStyle(content::NavigationController
* controller
,
137 content::SecurityStyle new_style
) {
138 controller
->GetVisibleEntry()->GetSSL().security_style
= new_style
;
139 SecurityStateModel
* model
=
140 SecurityStateModel::FromWebContents(controller
->GetWebContents());
142 model
->SecurityStateChanged();
148 // ToolbarModelTest -----------------------------------------------------------
150 class ToolbarModelTest
: public BrowserWithTestWindowTest
{
153 ToolbarModelTest(Browser::Type browser_type
,
154 chrome::HostDesktopType host_desktop_type
,
156 ~ToolbarModelTest() override
;
158 // BrowserWithTestWindowTest:
159 void SetUp() override
;
162 void NavigateAndCheckText(const GURL
& url
,
163 const base::string16
& expected_text
,
164 bool would_perform_search_term_replacement
,
165 bool should_display_url
);
168 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
170 DISALLOW_COPY_AND_ASSIGN(ToolbarModelTest
);
173 ToolbarModelTest::ToolbarModelTest() {
176 ToolbarModelTest::ToolbarModelTest(
177 Browser::Type browser_type
,
178 chrome::HostDesktopType host_desktop_type
,
180 : BrowserWithTestWindowTest(browser_type
,
185 ToolbarModelTest::~ToolbarModelTest() {
188 void ToolbarModelTest::SetUp() {
189 BrowserWithTestWindowTest::SetUp();
190 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
191 profile(), &TemplateURLServiceFactory::BuildInstanceFor
);
192 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
193 profile(), &AutocompleteClassifierFactory::BuildInstanceFor
);
194 UIThreadSearchTermsData::SetGoogleBaseURL("http://google.com/");
197 void ToolbarModelTest::NavigateAndCheckText(
199 const base::string16
& expected_text
,
200 bool would_perform_search_term_replacement
,
201 bool should_display_url
) {
202 // Check while loading.
203 content::NavigationController
* controller
=
204 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
205 controller
->LoadURL(url
, content::Referrer(), ui::PAGE_TRANSITION_LINK
,
207 ToolbarModel
* toolbar_model
= browser()->toolbar_model();
208 EXPECT_EQ(expected_text
, toolbar_model
->GetText());
209 EXPECT_EQ(would_perform_search_term_replacement
,
210 toolbar_model
->WouldPerformSearchTermReplacement(false));
211 EXPECT_EQ(should_display_url
, toolbar_model
->ShouldDisplayURL());
213 // Check after commit.
214 CommitPendingLoad(controller
);
215 // Fake a secure connection for HTTPS URLs, or the toolbar will refuse to
216 // extract search terms.
217 if (url
.SchemeIsCryptographic()) {
218 ASSERT_NO_FATAL_FAILURE(
219 SetSecurityStyle(controller
, content::SECURITY_STYLE_AUTHENTICATED
));
221 EXPECT_EQ(expected_text
, toolbar_model
->GetText());
222 EXPECT_EQ(would_perform_search_term_replacement
,
223 toolbar_model
->WouldPerformSearchTermReplacement(false));
224 EXPECT_EQ(should_display_url
, toolbar_model
->ShouldDisplayURL());
226 // Now pretend the user started modifying the omnibox.
227 toolbar_model
->set_input_in_progress(true);
228 EXPECT_FALSE(toolbar_model
->WouldPerformSearchTermReplacement(false));
229 EXPECT_EQ(would_perform_search_term_replacement
,
230 toolbar_model
->WouldPerformSearchTermReplacement(true));
232 // Tell the ToolbarModel that the user has stopped editing. This prevents
233 // this function from having side effects.
234 toolbar_model
->set_input_in_progress(false);
237 class PopupToolbarModelTest
: public ToolbarModelTest
{
239 PopupToolbarModelTest();
240 ~PopupToolbarModelTest() override
;
242 DISALLOW_COPY_AND_ASSIGN(PopupToolbarModelTest
);
245 PopupToolbarModelTest::PopupToolbarModelTest()
246 : ToolbarModelTest(Browser::TYPE_POPUP
,
247 chrome::HOST_DESKTOP_TYPE_NATIVE
,
251 PopupToolbarModelTest::~PopupToolbarModelTest() {
254 // Actual tests ---------------------------------------------------------------
256 // Test that we only replace URLs when query extraction and URL replacement
258 TEST_F(ToolbarModelTest
, ShouldDisplayURL_QueryExtraction
) {
259 AddTab(browser(), GURL(url::kAboutBlankURL
));
261 // Before we enable instant extended, query extraction is disabled.
262 EXPECT_FALSE(search::IsQueryExtractionEnabled())
263 << "This test expects query extraction to be disabled.";
264 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
265 const TestItem
& test_item
= test_items
[i
];
266 NavigateAndCheckText(test_item
.url
,
267 test_item
.expected_text_url_replacement_inactive
,
268 false, test_item
.should_display_url
);
271 search::EnableQueryExtractionForTesting();
272 EXPECT_TRUE(search::IsQueryExtractionEnabled());
273 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
274 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
275 const TestItem
& test_item
= test_items
[i
];
276 NavigateAndCheckText(test_item
.url
,
277 test_item
.expected_text_query_extraction
,
278 test_item
.would_perform_search_term_replacement
,
279 test_item
.should_display_url
);
282 // Disabling URL replacement should reset to only showing URLs.
283 browser()->toolbar_model()->set_url_replacement_enabled(false);
284 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
285 const TestItem
& test_item
= test_items
[i
];
286 NavigateAndCheckText(test_item
.url
,
287 test_item
.expected_text_url_replacement_inactive
,
288 false, test_item
.should_display_url
);
292 // Verify that search terms are extracted while the page is loading.
293 TEST_F(ToolbarModelTest
, SearchTermsWhileLoading
) {
294 search::EnableQueryExtractionForTesting();
295 AddTab(browser(), GURL(url::kAboutBlankURL
));
297 // While loading, we should be willing to extract search terms.
298 content::NavigationController
* controller
=
299 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
300 controller
->LoadURL(GURL("https://google.com/search?q=tractor+supply&espv=1"),
301 content::Referrer(), ui::PAGE_TRANSITION_LINK
,
303 ToolbarModel
* toolbar_model
= browser()->toolbar_model();
304 ASSERT_NO_FATAL_FAILURE(
305 SetSecurityStyle(controller
, content::SECURITY_STYLE_UNKNOWN
));
306 EXPECT_TRUE(toolbar_model
->WouldPerformSearchTermReplacement(false));
308 // When done loading, we shouldn't extract search terms if we didn't get an
309 // authenticated connection.
310 CommitPendingLoad(controller
);
311 ASSERT_NO_FATAL_FAILURE(
312 SetSecurityStyle(controller
, content::SECURITY_STYLE_UNKNOWN
));
313 EXPECT_FALSE(toolbar_model
->WouldPerformSearchTermReplacement(false));
316 // When the Google base URL is overridden on the command line, we should extract
317 // search terms from URLs that start with that base URL even when they're not
319 TEST_F(ToolbarModelTest
, GoogleBaseURL
) {
320 search::EnableQueryExtractionForTesting();
321 AddTab(browser(), GURL(url::kAboutBlankURL
));
323 // If the Google base URL wasn't specified on the command line, then if it's
324 // HTTP, we should not extract search terms.
325 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/");
326 NavigateAndCheckText(
327 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
328 base::ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), false,
331 // The same URL, when specified on the command line, should allow search term
333 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
334 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
335 switches::kGoogleBaseURL
, "http://www.foo.com/");
336 NavigateAndCheckText(
337 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
338 base::ASCIIToUTF16("tractor supply"), true, true);
341 // Test that URL display in a popup respects the query extraction flag.
342 TEST_F(PopupToolbarModelTest
, ShouldDisplayURL
) {
343 AddTab(browser(), GURL(url::kAboutBlankURL
));
345 // Check with query extraction disabled.
346 EXPECT_FALSE(search::IsQueryExtractionEnabled());
347 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
348 const TestItem
& test_item
= test_items
[i
];
349 NavigateAndCheckText(test_item
.url
,
350 test_item
.expected_text_url_replacement_inactive
,
351 false, test_item
.should_display_url
);
354 // With query extraction enabled, search term replacement should be performed.
355 search::EnableQueryExtractionForTesting();
356 EXPECT_TRUE(search::IsQueryExtractionEnabled());
357 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
358 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
359 const TestItem
& test_item
= test_items
[i
];
360 NavigateAndCheckText(test_item
.url
,
361 test_item
.expected_text_query_extraction
,
362 test_item
.would_perform_search_term_replacement
,
363 test_item
.should_display_url
);
366 // Disabling URL replacement should reset to only showing URLs.
367 browser()->toolbar_model()->set_url_replacement_enabled(false);
368 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
369 const TestItem
& test_item
= test_items
[i
];
370 NavigateAndCheckText(test_item
.url
,
371 test_item
.expected_text_url_replacement_inactive
,
372 false, test_item
.should_display_url
);