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