Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: chrome/browser/ui/toolbar/toolbar_model_unittest.cc

Issue 2211983002: Remove search::IsQueryExtractionEnabled (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@remove_prefetch_non_default
Patch Set: remove more tests Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/toolbar/toolbar_model.h" 5 #include "components/toolbar/toolbar_model.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" 13 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
15 #include "chrome/browser/search/search.h"
16 #include "chrome/browser/search_engines/template_url_service_factory.h"
17 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
18 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h" 15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/test/base/browser_with_test_window_test.h" 16 #include "chrome/test/base/browser_with_test_window_test.h"
22 #include "components/google/core/browser/google_switches.h"
23 #include "components/search/search.h"
24 #include "components/search_engines/template_url_service.h"
25 #include "components/toolbar/toolbar_model.h" 17 #include "components/toolbar/toolbar_model.h"
26 #include "components/variations/entropy_provider.h"
27 #include "content/public/browser/navigation_entry.h" 18 #include "content/public/browser/navigation_entry.h"
28 #include "content/public/common/content_constants.h" 19 #include "content/public/common/content_constants.h"
29 #include "content/public/common/ssl_status.h" 20 #include "content/public/common/ssl_status.h"
30 #include "content/public/common/url_constants.h" 21 #include "content/public/common/url_constants.h"
31 #include "ui/gfx/text_elider.h" 22 #include "ui/gfx/text_elider.h"
32 23
33 // Test data ------------------------------------------------------------------ 24 // Test data ------------------------------------------------------------------
34 25
35 namespace { 26 namespace {
36 27
37 struct TestItem { 28 struct TestItem {
38 GURL url; 29 GURL url;
39 // The expected text to display when both forms of URL replacement are 30 base::string16 expected_text;
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 both query extraction and URL removal are
45 // active.
46 base::string16 expected_text_both;
47 bool would_perform_search_term_replacement;
48 bool should_display_url;
49 } test_items[] = { 31 } test_items[] = {
50 { 32 {
51 GURL("view-source:http://www.google.com"), 33 GURL("view-source:http://www.google.com"),
52 base::ASCIIToUTF16("view-source:www.google.com"), 34 base::ASCIIToUTF16("view-source:www.google.com")
53 base::ASCIIToUTF16("view-source:www.google.com"),
54 base::string16(),
55 false,
56 true
57 }, 35 },
58 { 36 {
59 GURL("view-source:chrome://newtab/"), 37 GURL("view-source:chrome://newtab/"),
60 base::ASCIIToUTF16("view-source:chrome://newtab"), 38 base::ASCIIToUTF16("view-source:chrome://newtab")
61 base::ASCIIToUTF16("view-source:chrome://newtab"),
62 base::string16(),
63 false,
64 true
65 }, 39 },
66 { 40 {
67 GURL("chrome-extension://monkey/balls.html"), 41 GURL("chrome-extension://monkey/balls.html"),
68 base::ASCIIToUTF16("chrome-extension://monkey/balls.html"), 42 base::ASCIIToUTF16("chrome-extension://monkey/balls.html")
69 base::ASCIIToUTF16("chrome-extension://monkey/balls.html"),
70 base::string16(),
71 false,
72 true
73 }, 43 },
74 { 44 {
75 GURL(url::kAboutBlankURL), 45 GURL(url::kAboutBlankURL),
76 base::ASCIIToUTF16(url::kAboutBlankURL), 46 base::ASCIIToUTF16(url::kAboutBlankURL)
77 base::ASCIIToUTF16(url::kAboutBlankURL),
78 base::string16(),
79 false,
80 true
81 }, 47 },
82 { 48 {
83 GURL("http://searchurl/?q=tractor+supply"), 49 GURL("http://searchurl/?q=tractor+supply"),
84 base::ASCIIToUTF16("searchurl/?q=tractor+supply"), 50 base::ASCIIToUTF16("searchurl/?q=tractor+supply")
85 base::ASCIIToUTF16("searchurl/?q=tractor+supply"),
86 base::string16(),
87 false,
88 true
89 }, 51 },
90 { 52 {
91 GURL("http://google.com/search?q=tractor+supply&espv=1"), 53 GURL("http://google.com/search?q=tractor+supply&espv=1"),
92 base::ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1"), 54 base::ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1")
93 base::ASCIIToUTF16("google.com/search?q=tractor+supply&espv=1"),
94 base::string16(),
95 false,
96 true
97 }, 55 },
98 { 56 {
99 GURL("https://google.ca/search?q=tractor+supply"), 57 GURL("https://google.ca/search?q=tractor+supply"),
100 base::ASCIIToUTF16("https://google.ca/search?q=tractor+supply"), 58 base::ASCIIToUTF16("https://google.ca/search?q=tractor+supply")
101 base::ASCIIToUTF16("https://google.ca/search?q=tractor+supply"),
102 base::string16(),
103 false,
104 true
105 },
106 {
107 GURL("https://google.com/search?q=tractor+supply"),
108 base::ASCIIToUTF16("https://google.com/search?q=tractor+supply"),
109 base::ASCIIToUTF16("https://google.com/search?q=tractor+supply"),
110 base::string16(),
111 false,
112 true
113 },
114 {
115 GURL("https://google.com/search?q=tractor+supply&espv=1"),
116 base::ASCIIToUTF16("https://google.com/search?q=tractor+supply&espv=1"),
117 base::ASCIIToUTF16("tractor supply"),
118 base::ASCIIToUTF16("tractor supply"),
119 true,
120 true
121 },
122 {
123 GURL("https://google.com/search?q=tractorsupply.com&espv=1"),
124 base::ASCIIToUTF16("https://google.com/search?q=tractorsupply.com&espv=1"),
125 base::ASCIIToUTF16("tractorsupply.com"),
126 base::ASCIIToUTF16("tractorsupply.com"),
127 true,
128 true
129 },
130 {
131 GURL("https://google.com/search?q=ftp://tractorsupply.ie&espv=1"),
132 base::ASCIIToUTF16(
133 "https://google.com/search?q=ftp://tractorsupply.ie&espv=1"),
134 base::ASCIIToUTF16("ftp://tractorsupply.ie"),
135 base::ASCIIToUTF16("ftp://tractorsupply.ie"),
136 true,
137 true
138 }, 59 },
139 }; 60 };
140 61
141 } // namespace 62 } // namespace
142 63
143 64
144 // ToolbarModelTest ----------------------------------------------------------- 65 // ToolbarModelTest -----------------------------------------------------------
145 66
146 class ToolbarModelTest : public BrowserWithTestWindowTest { 67 class ToolbarModelTest : public BrowserWithTestWindowTest {
147 public: 68 public:
148 ToolbarModelTest(); 69 ToolbarModelTest();
149 ToolbarModelTest(Browser::Type browser_type, bool hosted_app);
150 ~ToolbarModelTest() override; 70 ~ToolbarModelTest() override;
151 71
152 // BrowserWithTestWindowTest: 72 // BrowserWithTestWindowTest:
153 void SetUp() override; 73 void SetUp() override;
154 74
155 protected: 75 protected:
156 void NavigateAndCheckText(const GURL& url, 76 void NavigateAndCheckText(const GURL& url,
157 const base::string16& expected_text, 77 const base::string16& expected_text);
158 bool would_perform_search_term_replacement,
159 bool should_display_url);
160 void NavigateAndCheckElided(const GURL& https_url); 78 void NavigateAndCheckElided(const GURL& https_url);
161 79
162 private: 80 private:
163 std::unique_ptr<base::FieldTrialList> field_trial_list_;
164
165 DISALLOW_COPY_AND_ASSIGN(ToolbarModelTest); 81 DISALLOW_COPY_AND_ASSIGN(ToolbarModelTest);
166 }; 82 };
167 83
168 ToolbarModelTest::ToolbarModelTest() { 84 ToolbarModelTest::ToolbarModelTest() {
169 } 85 }
170 86
171 ToolbarModelTest::ToolbarModelTest(Browser::Type browser_type, bool hosted_app)
172 : BrowserWithTestWindowTest(browser_type, hosted_app) {}
173
174 ToolbarModelTest::~ToolbarModelTest() { 87 ToolbarModelTest::~ToolbarModelTest() {
175 } 88 }
176 89
177 void ToolbarModelTest::SetUp() { 90 void ToolbarModelTest::SetUp() {
178 BrowserWithTestWindowTest::SetUp(); 91 BrowserWithTestWindowTest::SetUp();
179 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
180 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
181 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( 92 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
182 profile(), &AutocompleteClassifierFactory::BuildInstanceFor); 93 profile(), &AutocompleteClassifierFactory::BuildInstanceFor);
183 UIThreadSearchTermsData::SetGoogleBaseURL("http://google.com/");
184 } 94 }
185 95
186 void ToolbarModelTest::NavigateAndCheckText( 96 void ToolbarModelTest::NavigateAndCheckText(
187 const GURL& url, 97 const GURL& url,
188 const base::string16& expected_text, 98 const base::string16& expected_text) {
189 bool would_perform_search_term_replacement,
190 bool should_display_url) {
191 // Check while loading. 99 // Check while loading.
192 content::NavigationController* controller = 100 content::NavigationController* controller =
193 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); 101 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
194 controller->LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_LINK, 102 controller->LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_LINK,
195 std::string()); 103 std::string());
196 ToolbarModel* toolbar_model = browser()->toolbar_model(); 104 ToolbarModel* toolbar_model = browser()->toolbar_model();
197 EXPECT_EQ(expected_text, toolbar_model->GetText()); 105 EXPECT_EQ(expected_text, toolbar_model->GetText());
198 EXPECT_EQ(would_perform_search_term_replacement, 106 EXPECT_FALSE(toolbar_model->WouldPerformSearchTermReplacement(false));
199 toolbar_model->WouldPerformSearchTermReplacement(false)); 107 EXPECT_TRUE(toolbar_model->ShouldDisplayURL());
200 EXPECT_EQ(should_display_url, toolbar_model->ShouldDisplayURL());
201 108
202 // Check after commit. 109 // Check after commit.
203 CommitPendingLoad(controller); 110 CommitPendingLoad(controller);
204 // Fake a secure connection for HTTPS URLs, or the toolbar will refuse to
205 // extract search terms.
206 if (url.SchemeIsCryptographic()) {
207 controller->GetVisibleEntry()->GetSSL().security_style =
208 content::SECURITY_STYLE_AUTHENTICATED;
209 }
210 EXPECT_EQ(expected_text, toolbar_model->GetText()); 111 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());
214
215 // Now pretend the user started modifying the omnibox.
216 toolbar_model->set_input_in_progress(true);
217 EXPECT_FALSE(toolbar_model->WouldPerformSearchTermReplacement(false)); 112 EXPECT_FALSE(toolbar_model->WouldPerformSearchTermReplacement(false));
218 EXPECT_EQ(would_perform_search_term_replacement, 113 EXPECT_TRUE(toolbar_model->ShouldDisplayURL());
219 toolbar_model->WouldPerformSearchTermReplacement(true));
220
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);
224 } 114 }
225 115
226 void ToolbarModelTest::NavigateAndCheckElided(const GURL& url) { 116 void ToolbarModelTest::NavigateAndCheckElided(const GURL& url) {
227 // Check while loading. 117 // Check while loading.
228 content::NavigationController* controller = 118 content::NavigationController* controller =
229 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); 119 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
230 controller->LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_LINK, 120 controller->LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_LINK,
231 std::string()); 121 std::string());
232 ToolbarModel* toolbar_model = browser()->toolbar_model(); 122 ToolbarModel* toolbar_model = browser()->toolbar_model();
233 const base::string16 toolbar_text_before(toolbar_model->GetText()); 123 const base::string16 toolbar_text_before(toolbar_model->GetText());
234 EXPECT_LT(toolbar_text_before.size(), url.spec().size()); 124 EXPECT_LT(toolbar_text_before.size(), url.spec().size());
235 EXPECT_TRUE(base::EndsWith(toolbar_text_before, 125 EXPECT_TRUE(base::EndsWith(toolbar_text_before,
236 base::string16(gfx::kEllipsisUTF16), 126 base::string16(gfx::kEllipsisUTF16),
237 base::CompareCase::SENSITIVE)); 127 base::CompareCase::SENSITIVE));
238 // Check after commit. 128 // Check after commit.
239 CommitPendingLoad(controller); 129 CommitPendingLoad(controller);
240 const base::string16 toolbar_text_after(toolbar_model->GetText()); 130 const base::string16 toolbar_text_after(toolbar_model->GetText());
241 EXPECT_LT(toolbar_text_after.size(), url.spec().size()); 131 EXPECT_LT(toolbar_text_after.size(), url.spec().size());
242 EXPECT_TRUE(base::EndsWith(toolbar_text_after, 132 EXPECT_TRUE(base::EndsWith(toolbar_text_after,
243 base::string16(gfx::kEllipsisUTF16), 133 base::string16(gfx::kEllipsisUTF16),
244 base::CompareCase::SENSITIVE)); 134 base::CompareCase::SENSITIVE));
245 } 135 }
246 136
247 class PopupToolbarModelTest : public ToolbarModelTest {
248 public:
249 PopupToolbarModelTest();
250 ~PopupToolbarModelTest() override;
251
252 DISALLOW_COPY_AND_ASSIGN(PopupToolbarModelTest);
253 };
254
255 PopupToolbarModelTest::PopupToolbarModelTest()
256 : ToolbarModelTest(Browser::TYPE_POPUP, false) {}
257
258 PopupToolbarModelTest::~PopupToolbarModelTest() {
259 }
260
261 // Actual tests --------------------------------------------------------------- 137 // Actual tests ---------------------------------------------------------------
262 138
263 // Test that we only replace URLs when query extraction and URL replacement 139 // Test URL display.
264 // are enabled. 140 TEST_F(ToolbarModelTest, ShouldDisplayURL) {
265 TEST_F(ToolbarModelTest, ShouldDisplayURL_QueryExtraction) {
266 AddTab(browser(), GURL(url::kAboutBlankURL)); 141 AddTab(browser(), GURL(url::kAboutBlankURL));
267 142
268 // Before we enable instant extended, query extraction is disabled. 143 for (const TestItem& test_item : test_items) {
269 EXPECT_FALSE(search::IsQueryExtractionEnabled()) 144 NavigateAndCheckText(test_item.url, test_item.expected_text);
270 << "This test expects query extraction to be disabled.";
271 for (size_t i = 0; i < arraysize(test_items); ++i) {
272 const TestItem& test_item = test_items[i];
273 NavigateAndCheckText(test_item.url,
274 test_item.expected_text_url_replacement_inactive,
275 false, test_item.should_display_url);
276 }
277
278 search::EnableQueryExtractionForTesting();
279 EXPECT_TRUE(search::IsQueryExtractionEnabled());
280 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
281 for (size_t i = 0; i < arraysize(test_items); ++i) {
282 const TestItem& test_item = test_items[i];
283 NavigateAndCheckText(test_item.url,
284 test_item.expected_text_query_extraction,
285 test_item.would_perform_search_term_replacement,
286 test_item.should_display_url);
287 }
288
289 // Disabling URL replacement should reset to only showing URLs.
290 browser()->toolbar_model()->set_url_replacement_enabled(false);
291 for (size_t i = 0; i < arraysize(test_items); ++i) {
292 const TestItem& test_item = test_items[i];
293 NavigateAndCheckText(test_item.url,
294 test_item.expected_text_url_replacement_inactive,
295 false, test_item.should_display_url);
296 } 145 }
297 } 146 }
298 147
299 // Verify that search terms are extracted while the page is loading.
300 TEST_F(ToolbarModelTest, SearchTermsWhileLoading) {
301 search::EnableQueryExtractionForTesting();
302 AddTab(browser(), GURL(url::kAboutBlankURL));
303
304 // While loading, we should be willing to extract search terms.
305 content::NavigationController* controller =
306 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
307 controller->LoadURL(GURL("https://google.com/search?q=tractor+supply&espv=1"),
308 content::Referrer(), ui::PAGE_TRANSITION_LINK,
309 std::string());
310 ToolbarModel* toolbar_model = browser()->toolbar_model();
311 controller->GetVisibleEntry()->GetSSL().security_style =
312 content::SECURITY_STYLE_UNKNOWN;
313 EXPECT_TRUE(toolbar_model->WouldPerformSearchTermReplacement(false));
314
315 // When done loading, we shouldn't extract search terms if we didn't get an
316 // authenticated connection.
317 CommitPendingLoad(controller);
318 controller->GetVisibleEntry()->GetSSL().security_style =
319 content::SECURITY_STYLE_UNKNOWN;
320 EXPECT_FALSE(toolbar_model->WouldPerformSearchTermReplacement(false));
321 }
322
323 // When the Google base URL is overridden on the command line, we should extract
324 // search terms from URLs that start with that base URL even when they're not
325 // secure.
326 TEST_F(ToolbarModelTest, GoogleBaseURL) {
327 search::EnableQueryExtractionForTesting();
328 AddTab(browser(), GURL(url::kAboutBlankURL));
329
330 // If the Google base URL wasn't specified on the command line, then if it's
331 // HTTP, we should not extract search terms.
332 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/");
333 NavigateAndCheckText(
334 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
335 base::ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), false,
336 true);
337
338 // The same URL, when specified on the command line, should allow search term
339 // extraction.
340 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
341 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
342 switches::kGoogleBaseURL, "http://www.foo.com/");
343
344 // For the default search engine to reflect the new Google base URL we just
345 // set, the TemplateURLService has to know the base URL changed. Normally the
346 // GoogleURLTracker is the source of such change notifications, but since here
347 // we're modifying the base URL value directly by changing the command-line
348 // flags, we need to manually tell TemplateURLService to check for changes.
349 TemplateURLServiceFactory::GetInstance()->GetForProfile(profile())->
350 GoogleBaseURLChanged();
351
352 NavigateAndCheckText(
353 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
354 base::ASCIIToUTF16("tractor supply"), true, true);
355 }
356
357 TEST_F(ToolbarModelTest, ShouldElideLongURLs) { 148 TEST_F(ToolbarModelTest, ShouldElideLongURLs) {
358 AddTab(browser(), GURL(url::kAboutBlankURL)); 149 AddTab(browser(), GURL(url::kAboutBlankURL));
359 const std::string long_text(content::kMaxURLDisplayChars + 1024, '0'); 150 const std::string long_text(content::kMaxURLDisplayChars + 1024, '0');
360 NavigateAndCheckElided( 151 NavigateAndCheckElided(
361 GURL(std::string("https://www.foo.com/?") + long_text)); 152 GURL(std::string("https://www.foo.com/?") + long_text));
362 NavigateAndCheckElided(GURL(std::string("data:abc") + long_text)); 153 NavigateAndCheckElided(GURL(std::string("data:abc") + long_text));
363 } 154 }
364
365 // Test that URL display in a popup respects the query extraction flag.
366 TEST_F(PopupToolbarModelTest, ShouldDisplayURL) {
367 AddTab(browser(), GURL(url::kAboutBlankURL));
368
369 // Check with query extraction disabled.
370 EXPECT_FALSE(search::IsQueryExtractionEnabled());
371 for (size_t i = 0; i < arraysize(test_items); ++i) {
372 const TestItem& test_item = test_items[i];
373 NavigateAndCheckText(test_item.url,
374 test_item.expected_text_url_replacement_inactive,
375 false, test_item.should_display_url);
376 }
377
378 // With query extraction enabled, search term replacement should be performed.
379 search::EnableQueryExtractionForTesting();
380 EXPECT_TRUE(search::IsQueryExtractionEnabled());
381 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
382 for (size_t i = 0; i < arraysize(test_items); ++i) {
383 const TestItem& test_item = test_items[i];
384 NavigateAndCheckText(test_item.url,
385 test_item.expected_text_query_extraction,
386 test_item.would_perform_search_term_replacement,
387 test_item.should_display_url);
388 }
389
390 // Disabling URL replacement should reset to only showing URLs.
391 browser()->toolbar_model()->set_url_replacement_enabled(false);
392 for (size_t i = 0; i < arraysize(test_items); ++i) {
393 const TestItem& test_item = test_items[i];
394 NavigateAndCheckText(test_item.url,
395 test_item.expected_text_url_replacement_inactive,
396 false, test_item.should_display_url);
397 }
398 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698