NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / toolbar_model_unittest.cc
blob1807b019acf087bb63b543019989aa6836319dc0
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 <vector>
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 ------------------------------------------------------------------
35 namespace {
37 struct TestItem {
38 GURL url;
39 // The expected text to display when both forms of URL replacement are
40 // inactive.
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
47 // active.
48 base::string16 expected_text_both;
49 bool would_perform_search_term_replacement;
50 bool should_display_url;
51 } test_items[] = {
53 GURL("view-source:http://www.google.com"),
54 ASCIIToUTF16("view-source:www.google.com"),
55 ASCIIToUTF16("view-source:www.google.com"),
56 base::string16(),
57 base::string16(),
58 false,
59 true
62 GURL("view-source:chrome://newtab/"),
63 ASCIIToUTF16("view-source:chrome://newtab"),
64 ASCIIToUTF16("view-source:chrome://newtab"),
65 base::string16(),
66 base::string16(),
67 false,
68 true
71 GURL("chrome-extension://monkey/balls.html"),
72 ASCIIToUTF16("chrome-extension://monkey/balls.html"),
73 ASCIIToUTF16("chrome-extension://monkey/balls.html"),
74 base::string16(),
75 base::string16(),
76 false,
77 true
80 GURL(content::kAboutBlankURL),
81 ASCIIToUTF16(content::kAboutBlankURL),
82 ASCIIToUTF16(content::kAboutBlankURL),
83 base::string16(),
84 base::string16(),
85 false,
86 true
89 GURL("http://searchurl/?q=tractor+supply"),
90 ASCIIToUTF16("searchurl/?q=tractor+supply"),
91 ASCIIToUTF16("searchurl/?q=tractor+supply"),
92 base::string16(),
93 base::string16(),
94 false,
95 true
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"),
101 base::string16(),
102 base::string16(),
103 false,
104 true
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"),
110 base::string16(),
111 base::string16(),
112 false,
113 true
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"),
119 base::string16(),
120 base::string16(),
121 false,
122 true
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"),
128 base::string16(),
129 ASCIIToUTF16("tractor supply"),
130 true,
131 true
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"),
137 base::string16(),
138 ASCIIToUTF16("tractorsupply.com"),
139 true,
140 true
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"),
146 base::string16(),
147 ASCIIToUTF16("ftp://tractorsupply.ie"),
148 true,
149 true
153 } // namespace
156 // ToolbarModelTest -----------------------------------------------------------
158 class ToolbarModelTest : public BrowserWithTestWindowTest {
159 public:
160 ToolbarModelTest();
161 ToolbarModelTest(Browser::Type browser_type,
162 chrome::HostDesktopType host_desktop_type,
163 bool hosted_app);
164 virtual ~ToolbarModelTest();
166 // BrowserWithTestWindowTest:
167 virtual void SetUp() OVERRIDE;
169 protected:
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();
176 private:
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,
188 bool hosted_app)
189 : BrowserWithTestWindowTest(browser_type,
190 host_desktop_type,
191 hosted_app) {
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(
214 const GURL& url,
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,
222 std::string());
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 {
254 public:
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,
264 false) {
267 PopupToolbarModelTest::~PopupToolbarModelTest() {
270 // Actual tests ---------------------------------------------------------------
272 // Test that we only replace URLs when query extraction and URL replacement
273 // are enabled.
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
309 // enabled.
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,
354 std::string());
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
370 // secure.
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
383 // extraction.
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
394 // extraction flag.
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);