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. 4 5#include "chrome/browser/autocomplete/history_url_provider.h" 6 7#include <algorithm> 8 9#include "base/message_loop/message_loop.h" 10#include "base/path_service.h" 11#include "base/prefs/pref_service.h" 12#include "base/strings/string_util.h" 13#include "base/strings/utf_string_conversions.h" 14#include "base/time/time.h" 15#include "chrome/browser/autocomplete/autocomplete_match.h" 16#include "chrome/browser/autocomplete/autocomplete_provider.h" 17#include "chrome/browser/autocomplete/autocomplete_provider_listener.h" 18#include "chrome/browser/autocomplete/history_quick_provider.h" 19#include "chrome/browser/history/history_service.h" 20#include "chrome/browser/history/history_service_factory.h" 21#include "chrome/browser/search_engines/template_url.h" 22#include "chrome/browser/search_engines/template_url_service.h" 23#include "chrome/browser/search_engines/template_url_service_factory.h" 24#include "chrome/common/net/url_fixer_upper.h" 25#include "chrome/common/pref_names.h" 26#include "chrome/test/base/testing_browser_process.h" 27#include "chrome/test/base/testing_profile.h" 28#include "content/public/test/test_browser_thread_bundle.h" 29#include "testing/gtest/include/gtest/gtest.h" 30 31using base::Time; 32using base::TimeDelta; 33 34using content::TestBrowserThreadBundle; 35 36struct TestURLInfo { 37 const char* url; 38 const char* title; 39 int visit_count; 40 int typed_count; 41} test_db[] = { 42 {"http://www.google.com/", "Google", 3, 3}, 43 44 // High-quality pages should get a host synthesized as a lower-quality match. 45 {"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100}, 46 47 // Less popular pages should have hosts synthesized as higher-quality 48 // matches. 49 {"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0}, 50 51 // Unpopular pages should not appear in the results at all. 52 {"http://freshmeat.net/unpopular.html", "Unpopular", 1, 0}, 53 54 // If a host has a match, we should pick it up during host synthesis. 55 {"http://news.google.com/?ned=us&topic=n", "Google News - U.S.", 2, 2}, 56 {"http://news.google.com/", "Google News", 1, 1}, 57 58 // Matches that are normally not inline-autocompletable should be 59 // autocompleted if they are shorter substitutes for longer matches that would 60 // have been inline autocompleted. 61 {"http://synthesisatest.com/foo/", "Test A", 1, 1}, 62 {"http://synthesisbtest.com/foo/", "Test B", 1, 1}, 63 {"http://synthesisbtest.com/foo/bar.html", "Test B Bar", 2, 2}, 64 65 // Suggested short URLs must be "good enough" and must match user input. 66 {"http://foo.com/", "Dir", 5, 5}, 67 {"http://foo.com/dir/", "Dir", 2, 2}, 68 {"http://foo.com/dir/another/", "Dir", 5, 1}, 69 {"http://foo.com/dir/another/again/", "Dir", 10, 0}, 70 {"http://foo.com/dir/another/again/myfile.html", "File", 10, 2}, 71 72 // We throw in a lot of extra URLs here to make sure we're testing the 73 // history database's query, not just the autocomplete provider. 74 {"http://startest.com/y/a", "A", 2, 2}, 75 {"http://startest.com/y/b", "B", 5, 2}, 76 {"http://startest.com/x/c", "C", 5, 2}, 77 {"http://startest.com/x/d", "D", 5, 5}, 78 {"http://startest.com/y/e", "E", 4, 2}, 79 {"http://startest.com/y/f", "F", 3, 2}, 80 {"http://startest.com/y/g", "G", 3, 2}, 81 {"http://startest.com/y/h", "H", 3, 2}, 82 {"http://startest.com/y/i", "I", 3, 2}, 83 {"http://startest.com/y/j", "J", 3, 2}, 84 {"http://startest.com/y/k", "K", 3, 2}, 85 {"http://startest.com/y/l", "L", 3, 2}, 86 {"http://startest.com/y/m", "M", 3, 2}, 87 88 // A file: URL is useful for testing that fixup does the right thing w.r.t. 89 // the number of trailing slashes on the user's input. 90 {"file:///C:/foo.txt", "", 2, 2}, 91 92 // Results with absurdly high typed_counts so that very generic queries like 93 // "http" will give consistent results even if more data is added above. 94 {"http://bogussite.com/a", "Bogus A", 10002, 10000}, 95 {"http://bogussite.com/b", "Bogus B", 10001, 10000}, 96 {"http://bogussite.com/c", "Bogus C", 10000, 10000}, 97 98 // Domain name with number. 99 {"http://www.17173.com/", "Domain with number", 3, 3}, 100 101 // URLs to test exact-matching behavior. 102 {"http://go/", "Intranet URL", 1, 1}, 103 {"http://gooey/", "Intranet URL 2", 5, 5}, 104 105 // URLs for testing offset adjustment. 106 {"http://www.\xEA\xB5\x90\xEC\x9C\xA1.kr/", "Korean", 2, 2}, 107 {"http://spaces.com/path%20with%20spaces/foo.html", "Spaces", 2, 2}, 108 {"http://ms/c++%20style%20guide", "Style guide", 2, 2}, 109 110 // URLs for testing ctrl-enter behavior. 111 {"http://binky/", "Intranet binky", 2, 2}, 112 {"http://winky/", "Intranet winky", 2, 2}, 113 {"http://www.winky.com/", "Internet winky", 5, 0}, 114 115 // URLs used by EmptyVisits. 116 {"http://pandora.com/", "Pandora", 2, 2}, 117 // This entry is explicitly added more recently than 118 // history::kLowQualityMatchAgeLimitInDays. 119 // {"http://p/", "p", 0, 0}, 120 121 // For intranet based tests. 122 {"http://intra/one", "Intranet", 2, 2}, 123 {"http://intra/two", "Intranet two", 1, 1}, 124 {"http://intra/three", "Intranet three", 2, 2}, 125 {"http://moo/bar", "Intranet moo", 1, 1}, 126 {"http://typedhost/typedpath", "Intranet typed", 1, 1}, 127 {"http://typedhost/untypedpath", "Intranet untyped", 1, 0}, 128 129 {"http://x.com/one", "Internet", 2, 2}, 130 {"http://x.com/two", "Internet two", 1, 1}, 131 {"http://x.com/three", "Internet three", 2, 2}, 132}; 133 134class HistoryURLProviderTest : public testing::Test, 135 public AutocompleteProviderListener { 136 public: 137 struct UrlAndLegalDefault { 138 std::string url; 139 bool allowed_to_be_default_match; 140 }; 141 142 HistoryURLProviderTest() 143 : sort_matches_(false) { 144 HistoryQuickProvider::set_disabled(true); 145 } 146 147 virtual ~HistoryURLProviderTest() { 148 HistoryQuickProvider::set_disabled(false); 149 } 150 151 // AutocompleteProviderListener: 152 virtual void OnProviderUpdate(bool updated_matches) OVERRIDE; 153 154 protected: 155 static BrowserContextKeyedService* CreateTemplateURLService( 156 content::BrowserContext* profile) { 157 return new TemplateURLService(static_cast<Profile*>(profile)); 158 } 159 160 // testing::Test 161 virtual void SetUp() { 162 ASSERT_TRUE(SetUpImpl(false)); 163 } 164 virtual void TearDown(); 165 166 // Does the real setup. 167 bool SetUpImpl(bool no_db) WARN_UNUSED_RESULT; 168 169 // Fills test data into the history system. 170 void FillData(); 171 172 // Runs an autocomplete query on |text| and checks to see that the returned 173 // results' destination URLs match those provided. Also allows checking 174 // that the input type was identified correctly. 175 void RunTest(const base::string16 text, 176 const base::string16& desired_tld, 177 bool prevent_inline_autocomplete, 178 const UrlAndLegalDefault* expected_urls, 179 size_t num_results, 180 AutocompleteInput::Type* identified_input_type); 181 182 // A version of the above without the final |type| output parameter. 183 void RunTest(const base::string16 text, 184 const base::string16& desired_tld, 185 bool prevent_inline_autocomplete, 186 const UrlAndLegalDefault* expected_urls, 187 size_t num_results) { 188 AutocompleteInput::Type type; 189 return RunTest(text, desired_tld, prevent_inline_autocomplete, 190 expected_urls, num_results, &type); 191 } 192 193 content::TestBrowserThreadBundle thread_bundle_; 194 ACMatches matches_; 195 scoped_ptr<TestingProfile> profile_; 196 HistoryService* history_service_; 197 scoped_refptr<HistoryURLProvider> autocomplete_; 198 // Should the matches be sorted and duplicates removed? 199 bool sort_matches_; 200}; 201 202class HistoryURLProviderTestNoDB : public HistoryURLProviderTest { 203 protected: 204 virtual void SetUp() { 205 ASSERT_TRUE(SetUpImpl(true)); 206 } 207}; 208 209void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches) { 210 if (autocomplete_->done()) 211 base::MessageLoop::current()->Quit(); 212} 213 214bool HistoryURLProviderTest::SetUpImpl(bool no_db) { 215 profile_.reset(new TestingProfile()); 216 if (!(profile_->CreateHistoryService(true, no_db))) 217 return false; 218 if (!no_db) { 219 profile_->BlockUntilHistoryProcessesPendingRequests(); 220 profile_->BlockUntilHistoryIndexIsRefreshed(); 221 } 222 profile_->GetPrefs()->SetString(prefs::kAcceptLanguages, "en-US,en,ko"); 223 history_service_ = HistoryServiceFactory::GetForProfile( 224 profile_.get(), Profile::EXPLICIT_ACCESS); 225 226 autocomplete_ = new HistoryURLProvider(this, profile_.get()); 227 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 228 profile_.get(), &HistoryURLProviderTest::CreateTemplateURLService); 229 FillData(); 230 return true; 231} 232 233void HistoryURLProviderTest::TearDown() { 234 autocomplete_ = NULL; 235} 236 237void HistoryURLProviderTest::FillData() { 238 // All visits are a long time ago (some tests require this since we do some 239 // special logic for things visited very recently). Note that this time must 240 // be more recent than the "archived history" threshold for the data to go 241 // into the main database. 242 // 243 // TODO(brettw) It would be nice if we could test this behavior, in which 244 // case the time would be specifed in the test_db structure. 245 Time visit_time = Time::Now() - TimeDelta::FromDays(80); 246 247 for (size_t i = 0; i < arraysize(test_db); ++i) { 248 const TestURLInfo& cur = test_db[i]; 249 const GURL current_url(cur.url); 250 history_service_->AddPageWithDetails(current_url, UTF8ToUTF16(cur.title), 251 cur.visit_count, cur.typed_count, 252 visit_time, false, 253 history::SOURCE_BROWSED); 254 } 255 256 history_service_->AddPageWithDetails( 257 GURL("http://p/"), UTF8ToUTF16("p"), 0, 0, 258 Time::Now() - 259 TimeDelta::FromDays(history::kLowQualityMatchAgeLimitInDays - 1), 260 false, history::SOURCE_BROWSED); 261} 262 263void HistoryURLProviderTest::RunTest( 264 const base::string16 text, 265 const base::string16& desired_tld, 266 bool prevent_inline_autocomplete, 267 const UrlAndLegalDefault* expected_urls, 268 size_t num_results, 269 AutocompleteInput::Type* identified_input_type) { 270 AutocompleteInput input(text, base::string16::npos, desired_tld, GURL(), 271 AutocompleteInput::INVALID_SPEC, 272 prevent_inline_autocomplete, false, true, 273 AutocompleteInput::ALL_MATCHES); 274 *identified_input_type = input.type(); 275 autocomplete_->Start(input, false); 276 if (!autocomplete_->done()) 277 base::MessageLoop::current()->Run(); 278 279 matches_ = autocomplete_->matches(); 280 if (sort_matches_) { 281 for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i) 282 i->ComputeStrippedDestinationURL(profile_.get()); 283 std::sort(matches_.begin(), matches_.end(), 284 &AutocompleteMatch::DestinationSortFunc); 285 matches_.erase(std::unique(matches_.begin(), matches_.end(), 286 &AutocompleteMatch::DestinationsEqual), 287 matches_.end()); 288 std::sort(matches_.begin(), matches_.end(), 289 &AutocompleteMatch::MoreRelevant); 290 } 291 ASSERT_EQ(num_results, matches_.size()) << "Input text: " << text 292 << "\nTLD: \"" << desired_tld << "\""; 293 for (size_t i = 0; i < num_results; ++i) { 294 EXPECT_EQ(expected_urls[i].url, matches_[i].destination_url.spec()); 295 EXPECT_EQ(expected_urls[i].allowed_to_be_default_match, 296 matches_[i].allowed_to_be_default_match); 297 } 298} 299 300TEST_F(HistoryURLProviderTest, PromoteShorterURLs) { 301 // Test that hosts get synthesized below popular pages. 302 const UrlAndLegalDefault expected_nonsynth[] = { 303 { "http://slashdot.org/favorite_page.html", false }, 304 { "http://slashdot.org/", false } 305 }; 306 RunTest(ASCIIToUTF16("slash"), base::string16(), true, expected_nonsynth, 307 arraysize(expected_nonsynth)); 308 309 // Test that hosts get synthesized above less popular pages. 310 const UrlAndLegalDefault expected_synth[] = { 311 { "http://kerneltrap.org/", false }, 312 { "http://kerneltrap.org/not_very_popular.html", false } 313 }; 314 RunTest(ASCIIToUTF16("kernel"), base::string16(), true, expected_synth, 315 arraysize(expected_synth)); 316 317 // Test that unpopular pages are ignored completely. 318 RunTest(ASCIIToUTF16("fresh"), base::string16(), true, NULL, 0); 319 320 // Test that if we create or promote shorter suggestions that would not 321 // normally be inline autocompletable, we make them inline autocompletable if 322 // the original suggestion (that we replaced as "top") was inline 323 // autocompletable. 324 const UrlAndLegalDefault expected_synthesisa[] = { 325 { "http://synthesisatest.com/", true }, 326 { "http://synthesisatest.com/foo/", true } 327 }; 328 RunTest(ASCIIToUTF16("synthesisa"), base::string16(), false, 329 expected_synthesisa, arraysize(expected_synthesisa)); 330 EXPECT_LT(matches_.front().relevance, 1200); 331 const UrlAndLegalDefault expected_synthesisb[] = { 332 { "http://synthesisbtest.com/foo/", true }, 333 { "http://synthesisbtest.com/foo/bar.html", true } 334 }; 335 RunTest(ASCIIToUTF16("synthesisb"), base::string16(), false, 336 expected_synthesisb, arraysize(expected_synthesisb)); 337 EXPECT_GE(matches_.front().relevance, 1410); 338 339 // Test that if we have a synthesized host that matches a suggestion, they 340 // get combined into one. 341 const UrlAndLegalDefault expected_combine[] = { 342 { "http://news.google.com/", false }, 343 { "http://news.google.com/?ned=us&topic=n", false }, 344 }; 345 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("news"), base::string16(), true, 346 expected_combine, arraysize(expected_combine))); 347 // The title should also have gotten set properly on the host for the 348 // synthesized one, since it was also in the results. 349 EXPECT_EQ(ASCIIToUTF16("Google News"), matches_.front().description); 350 351 // Test that short URL matching works correctly as the user types more 352 // (several tests): 353 // The entry for foo.com is the best of all five foo.com* entries. 354 const UrlAndLegalDefault short_1[] = { 355 { "http://foo.com/", false }, 356 { "http://foo.com/dir/another/again/myfile.html", false }, 357 { "http://foo.com/dir/", false } 358 }; 359 RunTest(ASCIIToUTF16("foo"), base::string16(), true, 360 short_1, arraysize(short_1)); 361 362 // When the user types the whole host, make sure we don't get two results for 363 // it. 364 const UrlAndLegalDefault short_2[] = { 365 { "http://foo.com/", true }, 366 { "http://foo.com/dir/another/again/myfile.html", false }, 367 { "http://foo.com/dir/", false }, 368 { "http://foo.com/dir/another/", false } 369 }; 370 RunTest(ASCIIToUTF16("foo.com"), base::string16(), true, short_2, 371 arraysize(short_2)); 372 RunTest(ASCIIToUTF16("foo.com/"), base::string16(), true, short_2, 373 arraysize(short_2)); 374 375 // The filename is the second best of the foo.com* entries, but there is a 376 // shorter URL that's "good enough". The host doesn't match the user input 377 // and so should not appear. 378 const UrlAndLegalDefault short_3[] = { 379 { "http://foo.com/d", true }, 380 { "http://foo.com/dir/another/", false }, 381 { "http://foo.com/dir/another/again/myfile.html", false }, 382 { "http://foo.com/dir/", false } 383 }; 384 RunTest(ASCIIToUTF16("foo.com/d"), base::string16(), true, short_3, 385 arraysize(short_3)); 386 387 // We shouldn't promote shorter URLs than the best if they're not good 388 // enough. 389 const UrlAndLegalDefault short_4[] = { 390 { "http://foo.com/dir/another/a", true }, 391 { "http://foo.com/dir/another/again/myfile.html", false }, 392 { "http://foo.com/dir/another/again/", false } 393 }; 394 RunTest(ASCIIToUTF16("foo.com/dir/another/a"), base::string16(), true, 395 short_4, arraysize(short_4)); 396 397 // Exact matches should always be best no matter how much more another match 398 // has been typed. 399 const UrlAndLegalDefault short_5a[] = { 400 { "http://gooey/", true }, 401 { "http://www.google.com/", true }, 402 { "http://go/", true } 403 }; 404 const UrlAndLegalDefault short_5b[] = { 405 { "http://go/", true }, 406 { "http://gooey/", true }, 407 { "http://www.google.com/", true } 408 }; 409 RunTest(ASCIIToUTF16("g"), base::string16(), false, 410 short_5a, arraysize(short_5a)); 411 RunTest(ASCIIToUTF16("go"), base::string16(), false, 412 short_5b, arraysize(short_5b)); 413} 414 415TEST_F(HistoryURLProviderTest, CullRedirects) { 416 // URLs we will be using, plus the visit counts they will initially get 417 // (the redirect set below will also increment the visit counts). We want 418 // the results to be in A,B,C order. Note also that our visit counts are 419 // all high enough so that domain synthesizing won't get triggered. 420 struct TestCase { 421 const char* url; 422 int count; 423 } test_cases[] = { 424 {"http://redirects/A", 30}, 425 {"http://redirects/B", 20}, 426 {"http://redirects/C", 10} 427 }; 428 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 429 history_service_->AddPageWithDetails(GURL(test_cases[i].url), 430 UTF8ToUTF16("Title"), test_cases[i].count, test_cases[i].count, 431 Time::Now(), false, history::SOURCE_BROWSED); 432 } 433 434 // Create a B->C->A redirect chain, but set the visit counts such that they 435 // will appear in A,B,C order in the results. The autocomplete query will 436 // search for the most recent visit when looking for redirects, so this will 437 // be found even though the previous visits had no redirects. 438 history::RedirectList redirects_to_a; 439 redirects_to_a.push_back(GURL(test_cases[1].url)); 440 redirects_to_a.push_back(GURL(test_cases[2].url)); 441 redirects_to_a.push_back(GURL(test_cases[0].url)); 442 history_service_->AddPage(GURL(test_cases[0].url), base::Time::Now(), 443 NULL, 0, GURL(), redirects_to_a, content::PAGE_TRANSITION_TYPED, 444 history::SOURCE_BROWSED, true); 445 446 // Because all the results are part of a redirect chain with other results, 447 // all but the first one (A) should be culled. We should get the default 448 // "what you typed" result, plus this one. 449 const base::string16 typing(ASCIIToUTF16("http://redirects/")); 450 const UrlAndLegalDefault expected_results[] = { 451 { UTF16ToUTF8(typing), true }, 452 { test_cases[0].url, false } 453 }; 454 RunTest(typing, base::string16(), true, expected_results, 455 arraysize(expected_results)); 456} 457 458TEST_F(HistoryURLProviderTest, WhatYouTyped) { 459 // Make sure we suggest a What You Typed match at the right times. 460 RunTest(ASCIIToUTF16("wytmatch"), base::string16(), false, NULL, 0); 461 RunTest(ASCIIToUTF16("wytmatch foo bar"), base::string16(), false, NULL, 0); 462 RunTest(ASCIIToUTF16("wytmatch+foo+bar"), base::string16(), false, NULL, 0); 463 RunTest(ASCIIToUTF16("wytmatch+foo+bar.com"), base::string16(), false, 464 NULL, 0); 465 466 const UrlAndLegalDefault results_1[] = { 467 { "http://www.wytmatch.com/", true } 468 }; 469 RunTest(ASCIIToUTF16("wytmatch"), ASCIIToUTF16("com"), false, results_1, 470 arraysize(results_1)); 471 472 const UrlAndLegalDefault results_2[] = { 473 { "http://wytmatch%20foo%20bar/", true } 474 }; 475 RunTest(ASCIIToUTF16("http://wytmatch foo bar"), base::string16(), false, 476 results_2, arraysize(results_2)); 477 478 const UrlAndLegalDefault results_3[] = { 479 { "https://wytmatch%20foo%20bar/", true } 480 }; 481 RunTest(ASCIIToUTF16("https://wytmatch foo bar"), base::string16(), false, 482 results_3, arraysize(results_3)); 483} 484 485TEST_F(HistoryURLProviderTest, Fixup) { 486 // Test for various past crashes we've had. 487 RunTest(ASCIIToUTF16("\\"), base::string16(), false, NULL, 0); 488 RunTest(ASCIIToUTF16("#"), base::string16(), false, NULL, 0); 489 RunTest(ASCIIToUTF16("%20"), base::string16(), false, NULL, 0); 490 const UrlAndLegalDefault fixup_crash[] = { 491 { "http://%EF%BD%A5@s/", true } 492 }; 493 RunTest(WideToUTF16(L"\uff65@s"), base::string16(), false, fixup_crash, 494 arraysize(fixup_crash)); 495 RunTest(WideToUTF16(L"\u2015\u2015@ \uff7c"), base::string16(), false, 496 NULL, 0); 497 498 // Fixing up "file:" should result in an inline autocomplete offset of just 499 // after "file:", not just after "file://". 500 const base::string16 input_1(ASCIIToUTF16("file:")); 501 const UrlAndLegalDefault fixup_1[] = { 502 { "file:///C:/foo.txt", true } 503 }; 504 ASSERT_NO_FATAL_FAILURE(RunTest(input_1, base::string16(), false, fixup_1, 505 arraysize(fixup_1))); 506 EXPECT_EQ(ASCIIToUTF16("///C:/foo.txt"), 507 matches_.front().inline_autocompletion); 508 509 // Fixing up "http:/" should result in an inline autocomplete offset of just 510 // after "http:/", not just after "http:". 511 const base::string16 input_2(ASCIIToUTF16("http:/")); 512 const UrlAndLegalDefault fixup_2[] = { 513 { "http://bogussite.com/a", true }, 514 { "http://bogussite.com/b", true }, 515 { "http://bogussite.com/c", true } 516 }; 517 ASSERT_NO_FATAL_FAILURE(RunTest(input_2, base::string16(), false, fixup_2, 518 arraysize(fixup_2))); 519 EXPECT_EQ(ASCIIToUTF16("/bogussite.com/a"), 520 matches_.front().inline_autocompletion); 521 522 // Adding a TLD to a small number like "56" should result in "www.56.com" 523 // rather than "0.0.0.56.com". 524 const UrlAndLegalDefault fixup_3[] = { 525 { "http://www.56.com/", true } 526 }; 527 RunTest(ASCIIToUTF16("56"), ASCIIToUTF16("com"), true, fixup_3, 528 arraysize(fixup_3)); 529 530 // An input looks like a IP address like "127.0.0.1" should result in 531 // "http://127.0.0.1/". 532 const UrlAndLegalDefault fixup_4[] = { 533 { "http://127.0.0.1/", true } 534 }; 535 RunTest(ASCIIToUTF16("127.0.0.1"), base::string16(), false, fixup_4, 536 arraysize(fixup_4)); 537 538 // An number "17173" should result in "http://www.17173.com/" in db. 539 const UrlAndLegalDefault fixup_5[] = { 540 { "http://www.17173.com/", true } 541 }; 542 RunTest(ASCIIToUTF16("17173"), base::string16(), false, fixup_5, 543 arraysize(fixup_5)); 544} 545 546// Make sure the results for the input 'p' don't change between the first and 547// second passes. 548TEST_F(HistoryURLProviderTest, EmptyVisits) { 549 // Wait for history to create the in memory DB. 550 profile_->BlockUntilHistoryProcessesPendingRequests(); 551 552 AutocompleteInput input(ASCIIToUTF16("p"), base::string16::npos, 553 base::string16(), GURL(), 554 AutocompleteInput::INVALID_SPEC, false, false, true, 555 AutocompleteInput::ALL_MATCHES); 556 autocomplete_->Start(input, false); 557 // HistoryURLProvider shouldn't be done (waiting on async results). 558 EXPECT_FALSE(autocomplete_->done()); 559 560 // We should get back an entry for pandora. 561 matches_ = autocomplete_->matches(); 562 ASSERT_GT(matches_.size(), 0u); 563 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url); 564 int pandora_relevance = matches_[0].relevance; 565 566 // Run the message loop. When |autocomplete_| finishes the loop is quit. 567 base::MessageLoop::current()->Run(); 568 EXPECT_TRUE(autocomplete_->done()); 569 matches_ = autocomplete_->matches(); 570 ASSERT_GT(matches_.size(), 0u); 571 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url); 572 EXPECT_EQ(pandora_relevance, matches_[0].relevance); 573} 574 575TEST_F(HistoryURLProviderTestNoDB, NavigateWithoutDB) { 576 // Ensure that we will still produce matches for navigation when there is no 577 // database. 578 UrlAndLegalDefault navigation_1[] = { 579 { "http://test.com/", true } 580 }; 581 RunTest(ASCIIToUTF16("test.com"), base::string16(), false, navigation_1, 582 arraysize(navigation_1)); 583 584 UrlAndLegalDefault navigation_2[] = { 585 { "http://slash/", true } 586 }; 587 RunTest(ASCIIToUTF16("slash"), base::string16(), false, navigation_2, 588 arraysize(navigation_2)); 589 590 RunTest(ASCIIToUTF16("this is a query"), base::string16(), false, NULL, 0); 591} 592 593TEST_F(HistoryURLProviderTest, DontAutocompleteOnTrailingWhitespace) { 594 AutocompleteInput input(ASCIIToUTF16("slash "), base::string16::npos, 595 base::string16(), GURL(), 596 AutocompleteInput::INVALID_SPEC, false, false, 597 true, AutocompleteInput::ALL_MATCHES); 598 autocomplete_->Start(input, false); 599 if (!autocomplete_->done()) 600 base::MessageLoop::current()->Run(); 601 602 // None of the matches should attempt to autocomplete. 603 matches_ = autocomplete_->matches(); 604 for (size_t i = 0; i < matches_.size(); ++i) { 605 EXPECT_TRUE(matches_[i].inline_autocompletion.empty()); 606 EXPECT_FALSE(matches_[i].allowed_to_be_default_match); 607 } 608} 609 610TEST_F(HistoryURLProviderTest, TreatEmailsAsSearches) { 611 // Visiting foo.com should not make this string be treated as a navigation. 612 // That means the result should be scored around 1200 ("what you typed") 613 // and not 1400+. 614 const UrlAndLegalDefault expected[] = { 615 { "http://user@foo.com/", true } 616 }; 617 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("user@foo.com"), 618 base::string16(), false, expected, 619 arraysize(expected))); 620 EXPECT_LE(1200, matches_[0].relevance); 621 EXPECT_LT(matches_[0].relevance, 1210); 622} 623 624TEST_F(HistoryURLProviderTest, IntranetURLsWithPaths) { 625 struct TestCase { 626 const char* input; 627 int relevance; 628 } test_cases[] = { 629 { "fooey", 0 }, 630 { "fooey/", 1200 }, // 1200 for URL would still navigate by default. 631 { "fooey/a", 1200 }, // 1200 for UNKNOWN would not. 632 { "fooey/a b", 1200 }, // Also UNKNOWN. 633 { "gooey", 1410 }, 634 { "gooey/", 1410 }, 635 { "gooey/a", 1400 }, 636 { "gooey/a b", 1400 }, 637 }; 638 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 639 SCOPED_TRACE(test_cases[i].input); 640 if (test_cases[i].relevance == 0) { 641 RunTest(ASCIIToUTF16(test_cases[i].input), base::string16(), false, 642 NULL, 0); 643 } else { 644 const UrlAndLegalDefault output[] = { 645 { URLFixerUpper::FixupURL(test_cases[i].input, std::string()).spec(), 646 true } 647 }; 648 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases[i].input), 649 base::string16(), false, 650 output, arraysize(output))); 651 // Actual relevance should be at least what test_cases expects and 652 // and no more than 10 more. 653 EXPECT_LE(test_cases[i].relevance, matches_[0].relevance); 654 EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10); 655 } 656 } 657} 658 659TEST_F(HistoryURLProviderTest, IntranetURLsWithRefs) { 660 struct TestCase { 661 const char* input; 662 int relevance; 663 AutocompleteInput::Type type; 664 } test_cases[] = { 665 { "gooey", 1410, AutocompleteInput::UNKNOWN }, 666 { "gooey/", 1410, AutocompleteInput::URL }, 667 { "gooey#", 1200, AutocompleteInput::UNKNOWN }, 668 { "gooey/#", 1200, AutocompleteInput::URL }, 669 { "gooey#foo", 1200, AutocompleteInput::UNKNOWN }, 670 { "gooey/#foo", 1200, AutocompleteInput::URL }, 671 { "gooey# foo", 1200, AutocompleteInput::UNKNOWN }, 672 { "gooey/# foo", 1200, AutocompleteInput::URL }, 673 }; 674 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 675 SCOPED_TRACE(test_cases[i].input); 676 const UrlAndLegalDefault output[] = { 677 { URLFixerUpper::FixupURL(test_cases[i].input, std::string()).spec(), 678 true } 679 }; 680 AutocompleteInput::Type type; 681 ASSERT_NO_FATAL_FAILURE( 682 RunTest(ASCIIToUTF16(test_cases[i].input), 683 base::string16(), false, output, arraysize(output), &type)); 684 // Actual relevance should be at least what test_cases expects and 685 // and no more than 10 more. 686 EXPECT_LE(test_cases[i].relevance, matches_[0].relevance); 687 EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10); 688 // Input type should be what we expect. This is important because 689 // this provider counts on SearchProvider to give queries a relevance 690 // score >1200 for UNKNOWN inputs and <1200 for URL inputs. (That's 691 // already tested in search_provider_unittest.cc.) For this test 692 // here to test that the user sees the correct behavior, it needs 693 // to check that the input type was identified correctly. 694 EXPECT_EQ(test_cases[i].type, type); 695 } 696} 697 698// Makes sure autocompletion happens for intranet sites that have been 699// previoulsy visited. 700TEST_F(HistoryURLProviderTest, IntranetURLCompletion) { 701 sort_matches_ = true; 702 703 const UrlAndLegalDefault expected1[] = { 704 { "http://intra/three", true }, 705 { "http://intra/two", true } 706 }; 707 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/t"), base::string16(), 708 false, expected1, arraysize(expected1))); 709 EXPECT_LE(1410, matches_[0].relevance); 710 EXPECT_LT(matches_[0].relevance, 1420); 711 EXPECT_EQ(matches_[0].relevance - 1, matches_[1].relevance); 712 713 const UrlAndLegalDefault expected2[] = { 714 { "http://moo/b", true }, 715 { "http://moo/bar", true } 716 }; 717 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("moo/b"), base::string16(), 718 false, expected2, arraysize(expected2))); 719 // The url what you typed match should be around 1400, otherwise the 720 // search what you typed match is going to be first. 721 EXPECT_LE(1400, matches_[0].relevance); 722 EXPECT_LT(matches_[0].relevance, 1410); 723 724 const UrlAndLegalDefault expected3[] = { 725 { "http://intra/one", true }, 726 { "http://intra/three", true }, 727 { "http://intra/two", true } 728 }; 729 RunTest(ASCIIToUTF16("intra"), base::string16(), false, expected3, 730 arraysize(expected3)); 731 732 const UrlAndLegalDefault expected4[] = { 733 { "http://intra/one", true }, 734 { "http://intra/three", true }, 735 { "http://intra/two", true } 736 }; 737 RunTest(ASCIIToUTF16("intra/"), base::string16(), false, expected4, 738 arraysize(expected4)); 739 740 const UrlAndLegalDefault expected5[] = { 741 { "http://intra/one", true } 742 }; 743 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/o"), base::string16(), 744 false, expected5, arraysize(expected5))); 745 EXPECT_LE(1410, matches_[0].relevance); 746 EXPECT_LT(matches_[0].relevance, 1420); 747 748 const UrlAndLegalDefault expected6[] = { 749 { "http://intra/x", true } 750 }; 751 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/x"), base::string16(), 752 false, expected6, arraysize(expected6))); 753 EXPECT_LE(1400, matches_[0].relevance); 754 EXPECT_LT(matches_[0].relevance, 1410); 755 756 const UrlAndLegalDefault expected7[] = { 757 { "http://typedhost/untypedpath", true } 758 }; 759 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("typedhost/untypedpath"), 760 base::string16(), false, expected7, arraysize(expected7))); 761 EXPECT_LE(1400, matches_[0].relevance); 762 EXPECT_LT(matches_[0].relevance, 1410); 763} 764 765TEST_F(HistoryURLProviderTest, CrashDueToFixup) { 766 // This test passes if we don't crash. The results don't matter. 767 const char* const test_cases[] = { 768 "//c", 769 "\\@st", 770 "view-source:x", 771 }; 772 for (size_t i = 0; i < arraysize(test_cases); ++i) { 773 AutocompleteInput input(ASCIIToUTF16(test_cases[i]), base::string16::npos, 774 base::string16(), GURL(), 775 AutocompleteInput::INVALID_SPEC, 776 false, false, true, AutocompleteInput::ALL_MATCHES); 777 autocomplete_->Start(input, false); 778 if (!autocomplete_->done()) 779 base::MessageLoop::current()->Run(); 780 } 781} 782 783TEST_F(HistoryURLProviderTest, CullSearchResults) { 784 // Set up a default search engine. 785 TemplateURLData data; 786 data.SetKeyword(ASCIIToUTF16("TestEngine")); 787 data.SetURL("http://testsearch.com/?q={searchTerms}"); 788 TemplateURLService* template_url_service = 789 TemplateURLServiceFactory::GetForProfile(profile_.get()); 790 TemplateURL* template_url = new TemplateURL(profile_.get(), data); 791 template_url_service->Add(template_url); 792 template_url_service->SetDefaultSearchProvider(template_url); 793 template_url_service->Load(); 794 795 // URLs we will be using, plus the visit counts they will initially get 796 // (the redirect set below will also increment the visit counts). We want 797 // the results to be in A,B,C order. Note also that our visit counts are 798 // all high enough so that domain synthesizing won't get triggered. 799 struct TestCase { 800 const char* url; 801 int count; 802 } test_cases[] = { 803 {"https://testsearch.com/", 30}, 804 {"https://testsearch.com/?q=foobar", 20}, 805 {"http://foobar.com/", 10} 806 }; 807 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 808 history_service_->AddPageWithDetails(GURL(test_cases[i].url), 809 UTF8ToUTF16("Title"), test_cases[i].count, test_cases[i].count, 810 Time::Now(), false, history::SOURCE_BROWSED); 811 } 812 813 // We should not see search URLs when typing a previously used query. 814 const UrlAndLegalDefault expected_when_searching_query[] = { 815 { test_cases[2].url, false } 816 }; 817 RunTest(ASCIIToUTF16("foobar"), base::string16(), true, 818 expected_when_searching_query, arraysize(expected_when_searching_query)); 819 820 // We should not see search URLs when typing the search engine name. 821 const UrlAndLegalDefault expected_when_searching_site[] = { 822 { test_cases[0].url, false } 823 }; 824 RunTest(ASCIIToUTF16("testsearch"), base::string16(), true, 825 expected_when_searching_site, arraysize(expected_when_searching_site)); 826} 827 828TEST_F(HistoryURLProviderTest, SuggestExactInput) { 829 const size_t npos = std::string::npos; 830 struct TestCase { 831 // Inputs: 832 const char* input; 833 bool trim_http; 834 // Expected Outputs: 835 const char* contents; 836 // Offsets of the ACMatchClassifications, terminated by npos. 837 size_t offsets[3]; 838 // The index of the ACMatchClassification that should have the MATCH bit 839 // set, npos if no ACMatchClassification should have the MATCH bit set. 840 size_t match_classification_index; 841 } test_cases[] = { 842 { "http://www.somesite.com", false, 843 "http://www.somesite.com", {0, npos, npos}, 0 }, 844 { "www.somesite.com", true, 845 "www.somesite.com", {0, npos, npos}, 0 }, 846 { "www.somesite.com", false, 847 "http://www.somesite.com", {0, 7, npos}, 1 }, 848 { "somesite.com", true, 849 "somesite.com", {0, npos, npos}, 0 }, 850 { "somesite.com", false, 851 "http://somesite.com", {0, 7, npos}, 1 }, 852 { "w", true, 853 "w", {0, npos, npos}, 0 }, 854 { "w", false, 855 "http://w", {0, 7, npos}, 1 }, 856 { "w.com", true, 857 "w.com", {0, npos, npos}, 0 }, 858 { "w.com", false, 859 "http://w.com", {0, 7, npos}, 1 }, 860 { "www.w.com", true, 861 "www.w.com", {0, npos, npos}, 0 }, 862 { "www.w.com", false, 863 "http://www.w.com", {0, 7, npos}, 1 }, 864 { "view-source:w", true, 865 "view-source:w", {0, npos, npos}, 0 }, 866 { "view-source:www.w.com/", true, 867 "view-source:www.w.com", {0, npos, npos}, npos }, 868 { "view-source:www.w.com/", false, 869 "view-source:http://www.w.com", {0, npos, npos}, npos }, 870 { "view-source:http://www.w.com/", false, 871 "view-source:http://www.w.com", {0, npos, npos}, 0 }, 872 { " view-source:", true, 873 "view-source:", {0, npos, npos}, 0 }, 874 { "http:////////w.com", false, 875 "http://w.com", {0, npos, npos}, npos }, 876 { " http:////////www.w.com", false, 877 "http://www.w.com", {0, npos, npos}, npos }, 878 { "http:a///www.w.com", false, 879 "http://a///www.w.com", {0, npos, npos}, npos }, 880 { "mailto://a@b.com", true, 881 "mailto://a@b.com", {0, npos, npos}, 0 }, 882 { "mailto://a@b.com", false, 883 "mailto://a@b.com", {0, npos, npos}, 0 }, 884 }; 885 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 886 SCOPED_TRACE(testing::Message() << "Index " << i << " input: " 887 << test_cases[i].input << ", trim_http: " 888 << test_cases[i].trim_http); 889 890 AutocompleteInput input(ASCIIToUTF16(test_cases[i].input), 891 base::string16::npos, base::string16(), 892 GURL("about:blank"), 893 AutocompleteInput::INVALID_SPEC, false, false, true, 894 AutocompleteInput::ALL_MATCHES); 895 AutocompleteMatch match(autocomplete_->SuggestExactInput( 896 input.text(), input.canonicalized_url(), test_cases[i].trim_http)); 897 EXPECT_EQ(ASCIIToUTF16(test_cases[i].contents), match.contents); 898 for (size_t match_index = 0; match_index < match.contents_class.size(); 899 ++match_index) { 900 EXPECT_EQ(test_cases[i].offsets[match_index], 901 match.contents_class[match_index].offset); 902 EXPECT_EQ(ACMatchClassification::URL | 903 (match_index == test_cases[i].match_classification_index ? 904 ACMatchClassification::MATCH : 0), 905 match.contents_class[match_index].style); 906 } 907 EXPECT_EQ(npos, test_cases[i].offsets[match.contents_class.size()]); 908 } 909} 910