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/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/test/base/browser_with_test_window_test.h"
18 #include "components/google/core/browser/google_switches.h"
19 #include "components/search/search.h"
20 #include "components/toolbar/toolbar_model.h"
21 #include "components/variations/entropy_provider.h"
22 #include "content/public/browser/navigation_entry.h"
23 #include "content/public/common/ssl_status.h"
24 #include "content/public/common/url_constants.h"
27 // Test data ------------------------------------------------------------------
33 // The expected text to display when both forms of URL replacement are
35 base::string16 expected_text_url_replacement_inactive
;
36 // The expected text to display when query extraction is active.
37 base::string16 expected_text_query_extraction
;
38 // The expected text to display when both query extraction and URL removal are
40 base::string16 expected_text_both
;
41 bool would_perform_search_term_replacement
;
42 bool should_display_url
;
45 GURL("view-source:http://www.google.com"),
46 base::ASCIIToUTF16("view-source:www.google.com"),
47 base::ASCIIToUTF16("view-source:www.google.com"),
53 GURL("view-source:chrome://newtab/"),
54 base::ASCIIToUTF16("view-source:chrome://newtab"),
55 base::ASCIIToUTF16("view-source:chrome://newtab"),
61 GURL("chrome-extension://monkey/balls.html"),
62 base::ASCIIToUTF16("chrome-extension://monkey/balls.html"),
63 base::ASCIIToUTF16("chrome-extension://monkey/balls.html"),
69 GURL(url::kAboutBlankURL
),
70 base::ASCIIToUTF16(url::kAboutBlankURL
),
71 base::ASCIIToUTF16(url::kAboutBlankURL
),
77 GURL("http://searchurl/?q=tractor+supply"),
78 base::ASCIIToUTF16("searchurl/?q=tractor+supply"),
79 base::ASCIIToUTF16("searchurl/?q=tractor+supply"),
85 GURL("http://google.com/search?q=tractor+supply&espv=1"),
86 base::ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1"),
87 base::ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1"),
93 GURL("https://google.ca/search?q=tractor+supply"),
94 base::ASCIIToUTF16("https://google.ca/search?q=tractor+supply"),
95 base::ASCIIToUTF16("https://google.ca/search?q=tractor+supply"),
101 GURL("https://google.com/search?q=tractor+supply"),
102 base::ASCIIToUTF16("https://google.com/search?q=tractor+supply"),
103 base::ASCIIToUTF16("https://google.com/search?q=tractor+supply"),
109 GURL("https://google.com/search?q=tractor+supply&espv=1"),
110 base::ASCIIToUTF16("https://google.com/search?q=tractor+supply&espv=1"),
111 base::ASCIIToUTF16("tractor supply"),
112 base::ASCIIToUTF16("tractor supply"),
117 GURL("https://google.com/search?q=tractorsupply.com&espv=1"),
118 base::ASCIIToUTF16("https://google.com/search?q=tractorsupply.com&espv=1"),
119 base::ASCIIToUTF16("tractorsupply.com"),
120 base::ASCIIToUTF16("tractorsupply.com"),
125 GURL("https://google.com/search?q=ftp://tractorsupply.ie&espv=1"),
127 "https://google.com/search?q=ftp://tractorsupply.ie&espv=1"),
128 base::ASCIIToUTF16("ftp://tractorsupply.ie"),
129 base::ASCIIToUTF16("ftp://tractorsupply.ie"),
138 // ToolbarModelTest -----------------------------------------------------------
140 class ToolbarModelTest
: public BrowserWithTestWindowTest
{
143 ToolbarModelTest(Browser::Type browser_type
,
144 chrome::HostDesktopType host_desktop_type
,
146 ~ToolbarModelTest() override
;
148 // BrowserWithTestWindowTest:
149 void SetUp() override
;
152 void NavigateAndCheckText(const GURL
& url
,
153 const base::string16
& expected_text
,
154 bool would_perform_search_term_replacement
,
155 bool should_display_url
);
158 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
160 DISALLOW_COPY_AND_ASSIGN(ToolbarModelTest
);
163 ToolbarModelTest::ToolbarModelTest() {
166 ToolbarModelTest::ToolbarModelTest(
167 Browser::Type browser_type
,
168 chrome::HostDesktopType host_desktop_type
,
170 : BrowserWithTestWindowTest(browser_type
,
175 ToolbarModelTest::~ToolbarModelTest() {
178 void ToolbarModelTest::SetUp() {
179 BrowserWithTestWindowTest::SetUp();
180 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
181 profile(), &TemplateURLServiceFactory::BuildInstanceFor
);
182 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
183 profile(), &AutocompleteClassifierFactory::BuildInstanceFor
);
184 UIThreadSearchTermsData::SetGoogleBaseURL("http://google.com/");
187 void ToolbarModelTest::NavigateAndCheckText(
189 const base::string16
& expected_text
,
190 bool would_perform_search_term_replacement
,
191 bool should_display_url
) {
192 // Check while loading.
193 content::NavigationController
* controller
=
194 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
195 controller
->LoadURL(url
, content::Referrer(), ui::PAGE_TRANSITION_LINK
,
197 ToolbarModel
* toolbar_model
= browser()->toolbar_model();
198 EXPECT_EQ(expected_text
, toolbar_model
->GetText());
199 EXPECT_EQ(would_perform_search_term_replacement
,
200 toolbar_model
->WouldPerformSearchTermReplacement(false));
201 EXPECT_EQ(should_display_url
, toolbar_model
->ShouldDisplayURL());
203 // Check after commit.
204 CommitPendingLoad(controller
);
205 // Fake a secure connection for HTTPS URLs, or the toolbar will refuse to
206 // extract search terms.
207 if (url
.SchemeIsCryptographic()) {
208 controller
->GetVisibleEntry()->GetSSL().security_style
=
209 content::SECURITY_STYLE_AUTHENTICATED
;
211 EXPECT_EQ(expected_text
, toolbar_model
->GetText());
212 EXPECT_EQ(would_perform_search_term_replacement
,
213 toolbar_model
->WouldPerformSearchTermReplacement(false));
214 EXPECT_EQ(should_display_url
, toolbar_model
->ShouldDisplayURL());
216 // Now pretend the user started modifying the omnibox.
217 toolbar_model
->set_input_in_progress(true);
218 EXPECT_FALSE(toolbar_model
->WouldPerformSearchTermReplacement(false));
219 EXPECT_EQ(would_perform_search_term_replacement
,
220 toolbar_model
->WouldPerformSearchTermReplacement(true));
222 // Tell the ToolbarModel that the user has stopped editing. This prevents
223 // this function from having side effects.
224 toolbar_model
->set_input_in_progress(false);
227 class PopupToolbarModelTest
: public ToolbarModelTest
{
229 PopupToolbarModelTest();
230 ~PopupToolbarModelTest() override
;
232 DISALLOW_COPY_AND_ASSIGN(PopupToolbarModelTest
);
235 PopupToolbarModelTest::PopupToolbarModelTest()
236 : ToolbarModelTest(Browser::TYPE_POPUP
,
237 chrome::HOST_DESKTOP_TYPE_NATIVE
,
241 PopupToolbarModelTest::~PopupToolbarModelTest() {
244 // Actual tests ---------------------------------------------------------------
246 // Test that we only replace URLs when query extraction and URL replacement
248 TEST_F(ToolbarModelTest
, ShouldDisplayURL_QueryExtraction
) {
249 AddTab(browser(), GURL(url::kAboutBlankURL
));
251 // Before we enable instant extended, query extraction is disabled.
252 EXPECT_FALSE(search::IsQueryExtractionEnabled())
253 << "This test expects query extraction to be disabled.";
254 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
255 const TestItem
& test_item
= test_items
[i
];
256 NavigateAndCheckText(test_item
.url
,
257 test_item
.expected_text_url_replacement_inactive
,
258 false, test_item
.should_display_url
);
261 search::EnableQueryExtractionForTesting();
262 EXPECT_TRUE(search::IsQueryExtractionEnabled());
263 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
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_query_extraction
,
268 test_item
.would_perform_search_term_replacement
,
269 test_item
.should_display_url
);
272 // Disabling URL replacement should reset to only showing URLs.
273 browser()->toolbar_model()->set_url_replacement_enabled(false);
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_url_replacement_inactive
,
278 false, test_item
.should_display_url
);
282 // Verify that search terms are extracted while the page is loading.
283 TEST_F(ToolbarModelTest
, SearchTermsWhileLoading
) {
284 search::EnableQueryExtractionForTesting();
285 AddTab(browser(), GURL(url::kAboutBlankURL
));
287 // While loading, we should be willing to extract search terms.
288 content::NavigationController
* controller
=
289 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
290 controller
->LoadURL(GURL("https://google.com/search?q=tractor+supply&espv=1"),
291 content::Referrer(), ui::PAGE_TRANSITION_LINK
,
293 ToolbarModel
* toolbar_model
= browser()->toolbar_model();
294 controller
->GetVisibleEntry()->GetSSL().security_style
=
295 content::SECURITY_STYLE_UNKNOWN
;
296 EXPECT_TRUE(toolbar_model
->WouldPerformSearchTermReplacement(false));
298 // When done loading, we shouldn't extract search terms if we didn't get an
299 // authenticated connection.
300 CommitPendingLoad(controller
);
301 controller
->GetVisibleEntry()->GetSSL().security_style
=
302 content::SECURITY_STYLE_UNKNOWN
;
303 EXPECT_FALSE(toolbar_model
->WouldPerformSearchTermReplacement(false));
306 // When the Google base URL is overridden on the command line, we should extract
307 // search terms from URLs that start with that base URL even when they're not
309 TEST_F(ToolbarModelTest
, GoogleBaseURL
) {
310 search::EnableQueryExtractionForTesting();
311 AddTab(browser(), GURL(url::kAboutBlankURL
));
313 // If the Google base URL wasn't specified on the command line, then if it's
314 // HTTP, we should not extract search terms.
315 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/");
316 NavigateAndCheckText(
317 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
318 base::ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), false,
321 // The same URL, when specified on the command line, should allow search term
323 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
324 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
325 switches::kGoogleBaseURL
, "http://www.foo.com/");
326 NavigateAndCheckText(
327 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
328 base::ASCIIToUTF16("tractor supply"), true, true);
331 // Test that URL display in a popup respects the query extraction flag.
332 TEST_F(PopupToolbarModelTest
, ShouldDisplayURL
) {
333 AddTab(browser(), GURL(url::kAboutBlankURL
));
335 // Check with query extraction disabled.
336 EXPECT_FALSE(search::IsQueryExtractionEnabled());
337 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
338 const TestItem
& test_item
= test_items
[i
];
339 NavigateAndCheckText(test_item
.url
,
340 test_item
.expected_text_url_replacement_inactive
,
341 false, test_item
.should_display_url
);
344 // With query extraction enabled, search term replacement should be performed.
345 search::EnableQueryExtractionForTesting();
346 EXPECT_TRUE(search::IsQueryExtractionEnabled());
347 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
348 for (size_t i
= 0; i
< arraysize(test_items
); ++i
) {
349 const TestItem
& test_item
= test_items
[i
];
350 NavigateAndCheckText(test_item
.url
,
351 test_item
.expected_text_query_extraction
,
352 test_item
.would_perform_search_term_replacement
,
353 test_item
.should_display_url
);
356 // Disabling URL replacement should reset to only showing URLs.
357 browser()->toolbar_model()->set_url_replacement_enabled(false);
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_url_replacement_inactive
,
362 false, test_item
.should_display_url
);