Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / toolbar_model_unittest.cc
bloba303ca374069e786c04b233c38fec6db59afcf43
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"
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/browser/ui/toolbar/toolbar_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/variations/entropy_provider.h"
21 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/common/ssl_status.h"
23 #include "content/public/common/url_constants.h"
26 // Test data ------------------------------------------------------------------
28 namespace {
30 struct TestItem {
31 GURL url;
32 // The expected text to display when both forms of URL replacement are
33 // inactive.
34 base::string16 expected_text_url_replacement_inactive;
35 // The expected text to display when query extraction is active.
36 base::string16 expected_text_query_extraction;
37 // The expected text to display when both query extraction and URL removal are
38 // active.
39 base::string16 expected_text_both;
40 bool would_perform_search_term_replacement;
41 bool should_display_url;
42 } test_items[] = {
44 GURL("view-source:http://www.google.com"),
45 base::ASCIIToUTF16("view-source:www.google.com"),
46 base::ASCIIToUTF16("view-source:www.google.com"),
47 base::string16(),
48 false,
49 true
52 GURL("view-source:chrome://newtab/"),
53 base::ASCIIToUTF16("view-source:chrome://newtab"),
54 base::ASCIIToUTF16("view-source:chrome://newtab"),
55 base::string16(),
56 false,
57 true
60 GURL("chrome-extension://monkey/balls.html"),
61 base::ASCIIToUTF16("chrome-extension://monkey/balls.html"),
62 base::ASCIIToUTF16("chrome-extension://monkey/balls.html"),
63 base::string16(),
64 false,
65 true
68 GURL(url::kAboutBlankURL),
69 base::ASCIIToUTF16(url::kAboutBlankURL),
70 base::ASCIIToUTF16(url::kAboutBlankURL),
71 base::string16(),
72 false,
73 true
76 GURL("http://searchurl/?q=tractor+supply"),
77 base::ASCIIToUTF16("searchurl/?q=tractor+supply"),
78 base::ASCIIToUTF16("searchurl/?q=tractor+supply"),
79 base::string16(),
80 false,
81 true
84 GURL("http://google.com/search?q=tractor+supply&espv=1"),
85 base::ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1"),
86 base::ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1"),
87 base::string16(),
88 false,
89 true
92 GURL("https://google.ca/search?q=tractor+supply"),
93 base::ASCIIToUTF16("https://google.ca/search?q=tractor+supply"),
94 base::ASCIIToUTF16("https://google.ca/search?q=tractor+supply"),
95 base::string16(),
96 false,
97 true
100 GURL("https://google.com/search?q=tractor+supply"),
101 base::ASCIIToUTF16("https://google.com/search?q=tractor+supply"),
102 base::ASCIIToUTF16("https://google.com/search?q=tractor+supply"),
103 base::string16(),
104 false,
105 true
108 GURL("https://google.com/search?q=tractor+supply&espv=1"),
109 base::ASCIIToUTF16("https://google.com/search?q=tractor+supply&espv=1"),
110 base::ASCIIToUTF16("tractor supply"),
111 base::ASCIIToUTF16("tractor supply"),
112 true,
113 true
116 GURL("https://google.com/search?q=tractorsupply.com&espv=1"),
117 base::ASCIIToUTF16("https://google.com/search?q=tractorsupply.com&espv=1"),
118 base::ASCIIToUTF16("tractorsupply.com"),
119 base::ASCIIToUTF16("tractorsupply.com"),
120 true,
121 true
124 GURL("https://google.com/search?q=ftp://tractorsupply.ie&espv=1"),
125 base::ASCIIToUTF16(
126 "https://google.com/search?q=ftp://tractorsupply.ie&espv=1"),
127 base::ASCIIToUTF16("ftp://tractorsupply.ie"),
128 base::ASCIIToUTF16("ftp://tractorsupply.ie"),
129 true,
130 true
134 } // namespace
137 // ToolbarModelTest -----------------------------------------------------------
139 class ToolbarModelTest : public BrowserWithTestWindowTest {
140 public:
141 ToolbarModelTest();
142 ToolbarModelTest(Browser::Type browser_type,
143 chrome::HostDesktopType host_desktop_type,
144 bool hosted_app);
145 ~ToolbarModelTest() override;
147 // BrowserWithTestWindowTest:
148 void SetUp() override;
150 protected:
151 void NavigateAndCheckText(const GURL& url,
152 const base::string16& expected_text,
153 bool would_perform_search_term_replacement,
154 bool should_display_url);
156 private:
157 scoped_ptr<base::FieldTrialList> field_trial_list_;
159 DISALLOW_COPY_AND_ASSIGN(ToolbarModelTest);
162 ToolbarModelTest::ToolbarModelTest() {
165 ToolbarModelTest::ToolbarModelTest(
166 Browser::Type browser_type,
167 chrome::HostDesktopType host_desktop_type,
168 bool hosted_app)
169 : BrowserWithTestWindowTest(browser_type,
170 host_desktop_type,
171 hosted_app) {
174 ToolbarModelTest::~ToolbarModelTest() {
177 void ToolbarModelTest::SetUp() {
178 BrowserWithTestWindowTest::SetUp();
179 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
180 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
181 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
182 profile(), &AutocompleteClassifierFactory::BuildInstanceFor);
183 UIThreadSearchTermsData::SetGoogleBaseURL("http://google.com/");
186 void ToolbarModelTest::NavigateAndCheckText(
187 const GURL& url,
188 const base::string16& expected_text,
189 bool would_perform_search_term_replacement,
190 bool should_display_url) {
191 // Check while loading.
192 content::NavigationController* controller =
193 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
194 controller->LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_LINK,
195 std::string());
196 ToolbarModel* toolbar_model = browser()->toolbar_model();
197 EXPECT_EQ(expected_text, toolbar_model->GetText());
198 EXPECT_EQ(would_perform_search_term_replacement,
199 toolbar_model->WouldPerformSearchTermReplacement(false));
200 EXPECT_EQ(should_display_url, toolbar_model->ShouldDisplayURL());
202 // Check after commit.
203 CommitPendingLoad(controller);
204 // Fake a secure connection for HTTPS URLs, or the toolbar will refuse to
205 // extract search terms.
206 if (url.SchemeIsSecure()) {
207 controller->GetVisibleEntry()->GetSSL().security_style =
208 content::SECURITY_STYLE_AUTHENTICATED;
210 EXPECT_EQ(expected_text, toolbar_model->GetText());
211 EXPECT_EQ(would_perform_search_term_replacement,
212 toolbar_model->WouldPerformSearchTermReplacement(false));
213 EXPECT_EQ(should_display_url, toolbar_model->ShouldDisplayURL());
215 // Now pretend the user started modifying the omnibox.
216 toolbar_model->set_input_in_progress(true);
217 EXPECT_FALSE(toolbar_model->WouldPerformSearchTermReplacement(false));
218 EXPECT_EQ(would_perform_search_term_replacement,
219 toolbar_model->WouldPerformSearchTermReplacement(true));
221 // Tell the ToolbarModel that the user has stopped editing. This prevents
222 // this function from having side effects.
223 toolbar_model->set_input_in_progress(false);
226 class PopupToolbarModelTest : public ToolbarModelTest {
227 public:
228 PopupToolbarModelTest();
229 ~PopupToolbarModelTest() override;
231 DISALLOW_COPY_AND_ASSIGN(PopupToolbarModelTest);
234 PopupToolbarModelTest::PopupToolbarModelTest()
235 : ToolbarModelTest(Browser::TYPE_POPUP,
236 chrome::HOST_DESKTOP_TYPE_NATIVE,
237 false) {
240 PopupToolbarModelTest::~PopupToolbarModelTest() {
243 // Actual tests ---------------------------------------------------------------
245 // Test that we only replace URLs when query extraction and URL replacement
246 // are enabled.
247 TEST_F(ToolbarModelTest, ShouldDisplayURL_QueryExtraction) {
248 AddTab(browser(), GURL(url::kAboutBlankURL));
250 // Before we enable instant extended, query extraction is disabled.
251 EXPECT_FALSE(chrome::IsQueryExtractionEnabled())
252 << "This test expects query extraction to be disabled.";
253 for (size_t i = 0; i < arraysize(test_items); ++i) {
254 const TestItem& test_item = test_items[i];
255 NavigateAndCheckText(test_item.url,
256 test_item.expected_text_url_replacement_inactive,
257 false, test_item.should_display_url);
260 chrome::EnableQueryExtractionForTesting();
261 EXPECT_TRUE(chrome::IsQueryExtractionEnabled());
262 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
263 for (size_t i = 0; i < arraysize(test_items); ++i) {
264 const TestItem& test_item = test_items[i];
265 NavigateAndCheckText(test_item.url,
266 test_item.expected_text_query_extraction,
267 test_item.would_perform_search_term_replacement,
268 test_item.should_display_url);
271 // Disabling URL replacement should reset to only showing URLs.
272 browser()->toolbar_model()->set_url_replacement_enabled(false);
273 for (size_t i = 0; i < arraysize(test_items); ++i) {
274 const TestItem& test_item = test_items[i];
275 NavigateAndCheckText(test_item.url,
276 test_item.expected_text_url_replacement_inactive,
277 false, test_item.should_display_url);
281 // Verify that search terms are extracted while the page is loading.
282 TEST_F(ToolbarModelTest, SearchTermsWhileLoading) {
283 chrome::EnableQueryExtractionForTesting();
284 AddTab(browser(), GURL(url::kAboutBlankURL));
286 // While loading, we should be willing to extract search terms.
287 content::NavigationController* controller =
288 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
289 controller->LoadURL(GURL("https://google.com/search?q=tractor+supply&espv=1"),
290 content::Referrer(), ui::PAGE_TRANSITION_LINK,
291 std::string());
292 ToolbarModel* toolbar_model = browser()->toolbar_model();
293 controller->GetVisibleEntry()->GetSSL().security_style =
294 content::SECURITY_STYLE_UNKNOWN;
295 EXPECT_TRUE(toolbar_model->WouldPerformSearchTermReplacement(false));
297 // When done loading, we shouldn't extract search terms if we didn't get an
298 // authenticated connection.
299 CommitPendingLoad(controller);
300 controller->GetVisibleEntry()->GetSSL().security_style =
301 content::SECURITY_STYLE_UNKNOWN;
302 EXPECT_FALSE(toolbar_model->WouldPerformSearchTermReplacement(false));
305 // When the Google base URL is overridden on the command line, we should extract
306 // search terms from URLs that start with that base URL even when they're not
307 // secure.
308 TEST_F(ToolbarModelTest, GoogleBaseURL) {
309 chrome::EnableQueryExtractionForTesting();
310 AddTab(browser(), GURL(url::kAboutBlankURL));
312 // If the Google base URL wasn't specified on the command line, then if it's
313 // HTTP, we should not extract search terms.
314 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/");
315 NavigateAndCheckText(
316 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
317 base::ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), false,
318 true);
320 // The same URL, when specified on the command line, should allow search term
321 // extraction.
322 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
323 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
324 switches::kGoogleBaseURL, "http://www.foo.com/");
325 NavigateAndCheckText(
326 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
327 base::ASCIIToUTF16("tractor supply"), true, true);
330 // Test that URL display in a popup respects the query extraction flag.
331 TEST_F(PopupToolbarModelTest, ShouldDisplayURL) {
332 AddTab(browser(), GURL(url::kAboutBlankURL));
334 // Check with query extraction disabled.
335 EXPECT_FALSE(chrome::IsQueryExtractionEnabled());
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);
343 // With query extraction enabled, search term replacement should be performed.
344 chrome::EnableQueryExtractionForTesting();
345 EXPECT_TRUE(chrome::IsQueryExtractionEnabled());
346 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
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_query_extraction,
351 test_item.would_perform_search_term_replacement,
352 test_item.should_display_url);
355 // Disabling URL replacement should reset to only showing URLs.
356 browser()->toolbar_model()->set_url_replacement_enabled(false);
357 for (size_t i = 0; i < arraysize(test_items); ++i) {
358 const TestItem& test_item = test_items[i];
359 NavigateAndCheckText(test_item.url,
360 test_item.expected_text_url_replacement_inactive,
361 false, test_item.should_display_url);