history_url_provider_unittest.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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 string16 text, 176 const 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 string16 text, 184 const 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 string16 text, 265 const 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, 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"), 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"), string16(), true, expected_synth, 315 arraysize(expected_synth)); 316 317 // Test that unpopular pages are ignored completely. 318 RunTest(ASCIIToUTF16("fresh"), 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"), string16(), false, expected_synthesisa, 329 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"), string16(), false, expected_synthesisb, 336 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"), 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"), string16(), true, short_1, arraysize(short_1)); 360 361 // When the user types the whole host, make sure we don't get two results for 362 // it. 363 const UrlAndLegalDefault short_2[] = { 364 { "http://foo.com/", true }, 365 { "http://foo.com/dir/another/again/myfile.html", false }, 366 { "http://foo.com/dir/", false }, 367 { "http://foo.com/dir/another/", false } 368 }; 369 RunTest(ASCIIToUTF16("foo.com"), string16(), true, short_2, 370 arraysize(short_2)); 371 RunTest(ASCIIToUTF16("foo.com/"), string16(), true, short_2, 372 arraysize(short_2)); 373 374 // The filename is the second best of the foo.com* entries, but there is a 375 // shorter URL that's "good enough". The host doesn't match the user input 376 // and so should not appear. 377 const UrlAndLegalDefault short_3[] = { 378 { "http://foo.com/d", true }, 379 { "http://foo.com/dir/another/", false }, 380 { "http://foo.com/dir/another/again/myfile.html", false }, 381 { "http://foo.com/dir/", false } 382 }; 383 RunTest(ASCIIToUTF16("foo.com/d"), string16(), true, short_3, 384 arraysize(short_3)); 385 386 // We shouldn't promote shorter URLs than the best if they're not good 387 // enough. 388 const UrlAndLegalDefault short_4[] = { 389 { "http://foo.com/dir/another/a", true }, 390 { "http://foo.com/dir/another/again/myfile.html", false }, 391 { "http://foo.com/dir/another/again/", false } 392 }; 393 RunTest(ASCIIToUTF16("foo.com/dir/another/a"), string16(), true, short_4, 394 arraysize(short_4)); 395 396 // Exact matches should always be best no matter how much more another match 397 // has been typed. 398 const UrlAndLegalDefault short_5a[] = { 399 { "http://gooey/", true }, 400 { "http://www.google.com/", true }, 401 { "http://go/", true } 402 }; 403 const UrlAndLegalDefault short_5b[] = { 404 { "http://go/", true }, 405 { "http://gooey/", true }, 406 { "http://www.google.com/", true } 407 }; 408 RunTest(ASCIIToUTF16("g"), string16(), false, short_5a, arraysize(short_5a)); 409 RunTest(ASCIIToUTF16("go"), string16(), false, short_5b, arraysize(short_5b)); 410} 411 412TEST_F(HistoryURLProviderTest, CullRedirects) { 413 // URLs we will be using, plus the visit counts they will initially get 414 // (the redirect set below will also increment the visit counts). We want 415 // the results to be in A,B,C order. Note also that our visit counts are 416 // all high enough so that domain synthesizing won't get triggered. 417 struct TestCase { 418 const char* url; 419 int count; 420 } test_cases[] = { 421 {"http://redirects/A", 30}, 422 {"http://redirects/B", 20}, 423 {"http://redirects/C", 10} 424 }; 425 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 426 history_service_->AddPageWithDetails(GURL(test_cases[i].url), 427 UTF8ToUTF16("Title"), test_cases[i].count, test_cases[i].count, 428 Time::Now(), false, history::SOURCE_BROWSED); 429 } 430 431 // Create a B->C->A redirect chain, but set the visit counts such that they 432 // will appear in A,B,C order in the results. The autocomplete query will 433 // search for the most recent visit when looking for redirects, so this will 434 // be found even though the previous visits had no redirects. 435 history::RedirectList redirects_to_a; 436 redirects_to_a.push_back(GURL(test_cases[1].url)); 437 redirects_to_a.push_back(GURL(test_cases[2].url)); 438 redirects_to_a.push_back(GURL(test_cases[0].url)); 439 history_service_->AddPage(GURL(test_cases[0].url), base::Time::Now(), 440 NULL, 0, GURL(), redirects_to_a, content::PAGE_TRANSITION_TYPED, 441 history::SOURCE_BROWSED, true); 442 443 // Because all the results are part of a redirect chain with other results, 444 // all but the first one (A) should be culled. We should get the default 445 // "what you typed" result, plus this one. 446 const string16 typing(ASCIIToUTF16("http://redirects/")); 447 const UrlAndLegalDefault expected_results[] = { 448 { UTF16ToUTF8(typing), true }, 449 { test_cases[0].url, false } 450 }; 451 RunTest(typing, string16(), true, expected_results, 452 arraysize(expected_results)); 453} 454 455TEST_F(HistoryURLProviderTest, WhatYouTyped) { 456 // Make sure we suggest a What You Typed match at the right times. 457 RunTest(ASCIIToUTF16("wytmatch"), string16(), false, NULL, 0); 458 RunTest(ASCIIToUTF16("wytmatch foo bar"), string16(), false, NULL, 0); 459 RunTest(ASCIIToUTF16("wytmatch+foo+bar"), string16(), false, NULL, 0); 460 RunTest(ASCIIToUTF16("wytmatch+foo+bar.com"), string16(), false, NULL, 0); 461 462 const UrlAndLegalDefault results_1[] = { 463 { "http://www.wytmatch.com/", true } 464 }; 465 RunTest(ASCIIToUTF16("wytmatch"), ASCIIToUTF16("com"), false, results_1, 466 arraysize(results_1)); 467 468 const UrlAndLegalDefault results_2[] = { 469 { "http://wytmatch%20foo%20bar/", true } 470 }; 471 RunTest(ASCIIToUTF16("http://wytmatch foo bar"), string16(), false, results_2, 472 arraysize(results_2)); 473 474 const UrlAndLegalDefault results_3[] = { 475 { "https://wytmatch%20foo%20bar/", true } 476 }; 477 RunTest(ASCIIToUTF16("https://wytmatch foo bar"), string16(), false, 478 results_3, arraysize(results_3)); 479} 480 481TEST_F(HistoryURLProviderTest, Fixup) { 482 // Test for various past crashes we've had. 483 RunTest(ASCIIToUTF16("\\"), string16(), false, NULL, 0); 484 RunTest(ASCIIToUTF16("#"), string16(), false, NULL, 0); 485 RunTest(ASCIIToUTF16("%20"), string16(), false, NULL, 0); 486 const UrlAndLegalDefault fixup_crash[] = { 487 { "http://%EF%BD%A5@s/", true } 488 }; 489 RunTest(WideToUTF16(L"\uff65@s"), string16(), false, fixup_crash, 490 arraysize(fixup_crash)); 491 RunTest(WideToUTF16(L"\u2015\u2015@ \uff7c"), string16(), false, NULL, 0); 492 493 // Fixing up "file:" should result in an inline autocomplete offset of just 494 // after "file:", not just after "file://". 495 const string16 input_1(ASCIIToUTF16("file:")); 496 const UrlAndLegalDefault fixup_1[] = { 497 { "file:///C:/foo.txt", true } 498 }; 499 ASSERT_NO_FATAL_FAILURE(RunTest(input_1, string16(), false, fixup_1, 500 arraysize(fixup_1))); 501 EXPECT_EQ(ASCIIToUTF16("///C:/foo.txt"), 502 matches_.front().inline_autocompletion); 503 504 // Fixing up "http:/" should result in an inline autocomplete offset of just 505 // after "http:/", not just after "http:". 506 const string16 input_2(ASCIIToUTF16("http:/")); 507 const UrlAndLegalDefault fixup_2[] = { 508 { "http://bogussite.com/a", true }, 509 { "http://bogussite.com/b", true }, 510 { "http://bogussite.com/c", true } 511 }; 512 ASSERT_NO_FATAL_FAILURE(RunTest(input_2, string16(), false, fixup_2, 513 arraysize(fixup_2))); 514 EXPECT_EQ(ASCIIToUTF16("/bogussite.com/a"), 515 matches_.front().inline_autocompletion); 516 517 // Adding a TLD to a small number like "56" should result in "www.56.com" 518 // rather than "0.0.0.56.com". 519 const UrlAndLegalDefault fixup_3[] = { 520 { "http://www.56.com/", true } 521 }; 522 RunTest(ASCIIToUTF16("56"), ASCIIToUTF16("com"), true, fixup_3, 523 arraysize(fixup_3)); 524 525 // An input looks like a IP address like "127.0.0.1" should result in 526 // "http://127.0.0.1/". 527 const UrlAndLegalDefault fixup_4[] = { 528 { "http://127.0.0.1/", true } 529 }; 530 RunTest(ASCIIToUTF16("127.0.0.1"), string16(), false, fixup_4, 531 arraysize(fixup_4)); 532 533 // An number "17173" should result in "http://www.17173.com/" in db. 534 const UrlAndLegalDefault fixup_5[] = { 535 { "http://www.17173.com/", true } 536 }; 537 RunTest(ASCIIToUTF16("17173"), string16(), false, fixup_5, 538 arraysize(fixup_5)); 539} 540 541// Make sure the results for the input 'p' don't change between the first and 542// second passes. 543TEST_F(HistoryURLProviderTest, EmptyVisits) { 544 // Wait for history to create the in memory DB. 545 profile_->BlockUntilHistoryProcessesPendingRequests(); 546 547 AutocompleteInput input(ASCIIToUTF16("p"), string16::npos, string16(), GURL(), 548 AutocompleteInput::INVALID_SPEC, false, false, true, 549 AutocompleteInput::ALL_MATCHES); 550 autocomplete_->Start(input, false); 551 // HistoryURLProvider shouldn't be done (waiting on async results). 552 EXPECT_FALSE(autocomplete_->done()); 553 554 // We should get back an entry for pandora. 555 matches_ = autocomplete_->matches(); 556 ASSERT_GT(matches_.size(), 0u); 557 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url); 558 int pandora_relevance = matches_[0].relevance; 559 560 // Run the message loop. When |autocomplete_| finishes the loop is quit. 561 base::MessageLoop::current()->Run(); 562 EXPECT_TRUE(autocomplete_->done()); 563 matches_ = autocomplete_->matches(); 564 ASSERT_GT(matches_.size(), 0u); 565 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url); 566 EXPECT_EQ(pandora_relevance, matches_[0].relevance); 567} 568 569TEST_F(HistoryURLProviderTestNoDB, NavigateWithoutDB) { 570 // Ensure that we will still produce matches for navigation when there is no 571 // database. 572 UrlAndLegalDefault navigation_1[] = { 573 { "http://test.com/", true } 574 }; 575 RunTest(ASCIIToUTF16("test.com"), string16(), false, navigation_1, 576 arraysize(navigation_1)); 577 578 UrlAndLegalDefault navigation_2[] = { 579 { "http://slash/", true } 580 }; 581 RunTest(ASCIIToUTF16("slash"), string16(), false, navigation_2, 582 arraysize(navigation_2)); 583 584 RunTest(ASCIIToUTF16("this is a query"), string16(), false, NULL, 0); 585} 586 587TEST_F(HistoryURLProviderTest, DontAutocompleteOnTrailingWhitespace) { 588 AutocompleteInput input(ASCIIToUTF16("slash "), string16::npos, string16(), 589 GURL(), AutocompleteInput::INVALID_SPEC, false, false, 590 true, AutocompleteInput::ALL_MATCHES); 591 autocomplete_->Start(input, false); 592 if (!autocomplete_->done()) 593 base::MessageLoop::current()->Run(); 594 595 // None of the matches should attempt to autocomplete. 596 matches_ = autocomplete_->matches(); 597 for (size_t i = 0; i < matches_.size(); ++i) { 598 EXPECT_TRUE(matches_[i].inline_autocompletion.empty()); 599 EXPECT_FALSE(matches_[i].allowed_to_be_default_match); 600 } 601} 602 603TEST_F(HistoryURLProviderTest, TreatEmailsAsSearches) { 604 // Visiting foo.com should not make this string be treated as a navigation. 605 // That means the result should be scored around 1200 ("what you typed") 606 // and not 1400+. 607 const UrlAndLegalDefault expected[] = { 608 { "http://user@foo.com/", true } 609 }; 610 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("user@foo.com"), string16(), 611 false, expected, arraysize(expected))); 612 EXPECT_LE(1200, matches_[0].relevance); 613 EXPECT_LT(matches_[0].relevance, 1210); 614} 615 616TEST_F(HistoryURLProviderTest, IntranetURLsWithPaths) { 617 struct TestCase { 618 const char* input; 619 int relevance; 620 } test_cases[] = { 621 { "fooey", 0 }, 622 { "fooey/", 1200 }, // 1200 for URL would still navigate by default. 623 { "fooey/a", 1200 }, // 1200 for UNKNOWN would not. 624 { "fooey/a b", 1200 }, // Also UNKNOWN. 625 { "gooey", 1410 }, 626 { "gooey/", 1410 }, 627 { "gooey/a", 1400 }, 628 { "gooey/a b", 1400 }, 629 }; 630 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 631 SCOPED_TRACE(test_cases[i].input); 632 if (test_cases[i].relevance == 0) { 633 RunTest(ASCIIToUTF16(test_cases[i].input), string16(), false, NULL, 0); 634 } else { 635 const UrlAndLegalDefault output[] = { 636 { URLFixerUpper::FixupURL(test_cases[i].input, std::string()).spec(), 637 true } 638 }; 639 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases[i].input), 640 string16(), false, output, arraysize(output))); 641 // Actual relevance should be at least what test_cases expects and 642 // and no more than 10 more. 643 EXPECT_LE(test_cases[i].relevance, matches_[0].relevance); 644 EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10); 645 } 646 } 647} 648 649TEST_F(HistoryURLProviderTest, IntranetURLsWithRefs) { 650 struct TestCase { 651 const char* input; 652 int relevance; 653 AutocompleteInput::Type type; 654 } test_cases[] = { 655 { "gooey", 1410, AutocompleteInput::UNKNOWN }, 656 { "gooey/", 1410, AutocompleteInput::URL }, 657 { "gooey#", 1200, AutocompleteInput::UNKNOWN }, 658 { "gooey/#", 1200, AutocompleteInput::URL }, 659 { "gooey#foo", 1200, AutocompleteInput::UNKNOWN }, 660 { "gooey/#foo", 1200, AutocompleteInput::URL }, 661 { "gooey# foo", 1200, AutocompleteInput::UNKNOWN }, 662 { "gooey/# foo", 1200, AutocompleteInput::URL }, 663 }; 664 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 665 SCOPED_TRACE(test_cases[i].input); 666 const UrlAndLegalDefault output[] = { 667 { URLFixerUpper::FixupURL(test_cases[i].input, std::string()).spec(), 668 true } 669 }; 670 AutocompleteInput::Type type; 671 ASSERT_NO_FATAL_FAILURE( 672 RunTest(ASCIIToUTF16(test_cases[i].input), 673 string16(), false, output, arraysize(output), &type)); 674 // Actual relevance should be at least what test_cases expects and 675 // and no more than 10 more. 676 EXPECT_LE(test_cases[i].relevance, matches_[0].relevance); 677 EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10); 678 // Input type should be what we expect. This is important because 679 // this provider counts on SearchProvider to give queries a relevance 680 // score >1200 for UNKNOWN inputs and <1200 for URL inputs. (That's 681 // already tested in search_provider_unittest.cc.) For this test 682 // here to test that the user sees the correct behavior, it needs 683 // to check that the input type was identified correctly. 684 EXPECT_EQ(test_cases[i].type, type); 685 } 686} 687 688// Makes sure autocompletion happens for intranet sites that have been 689// previoulsy visited. 690TEST_F(HistoryURLProviderTest, IntranetURLCompletion) { 691 sort_matches_ = true; 692 693 const UrlAndLegalDefault expected1[] = { 694 { "http://intra/three", true }, 695 { "http://intra/two", true } 696 }; 697 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/t"), string16(), false, 698 expected1, arraysize(expected1))); 699 EXPECT_LE(1410, matches_[0].relevance); 700 EXPECT_LT(matches_[0].relevance, 1420); 701 EXPECT_EQ(matches_[0].relevance - 1, matches_[1].relevance); 702 703 const UrlAndLegalDefault expected2[] = { 704 { "http://moo/b", true }, 705 { "http://moo/bar", true } 706 }; 707 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("moo/b"), string16(), false, 708 expected2, arraysize(expected2))); 709 // The url what you typed match should be around 1400, otherwise the 710 // search what you typed match is going to be first. 711 EXPECT_LE(1400, matches_[0].relevance); 712 EXPECT_LT(matches_[0].relevance, 1410); 713 714 const UrlAndLegalDefault expected3[] = { 715 { "http://intra/one", true }, 716 { "http://intra/three", true }, 717 { "http://intra/two", true } 718 }; 719 RunTest(ASCIIToUTF16("intra"), string16(), false, expected3, 720 arraysize(expected3)); 721 722 const UrlAndLegalDefault expected4[] = { 723 { "http://intra/one", true }, 724 { "http://intra/three", true }, 725 { "http://intra/two", true } 726 }; 727 RunTest(ASCIIToUTF16("intra/"), string16(), false, expected4, 728 arraysize(expected4)); 729 730 const UrlAndLegalDefault expected5[] = { 731 { "http://intra/one", true } 732 }; 733 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/o"), string16(), false, 734 expected5, arraysize(expected5))); 735 EXPECT_LE(1410, matches_[0].relevance); 736 EXPECT_LT(matches_[0].relevance, 1420); 737 738 const UrlAndLegalDefault expected6[] = { 739 { "http://intra/x", true } 740 }; 741 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/x"), string16(), false, 742 expected6, arraysize(expected6))); 743 EXPECT_LE(1400, matches_[0].relevance); 744 EXPECT_LT(matches_[0].relevance, 1410); 745 746 const UrlAndLegalDefault expected7[] = { 747 { "http://typedhost/untypedpath", true } 748 }; 749 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("typedhost/untypedpath"), 750 string16(), false, expected7, arraysize(expected7))); 751 EXPECT_LE(1400, matches_[0].relevance); 752 EXPECT_LT(matches_[0].relevance, 1410); 753} 754 755TEST_F(HistoryURLProviderTest, CrashDueToFixup) { 756 // This test passes if we don't crash. The results don't matter. 757 const char* const test_cases[] = { 758 "//c", 759 "\\@st", 760 "view-source:x", 761 }; 762 for (size_t i = 0; i < arraysize(test_cases); ++i) { 763 AutocompleteInput input(ASCIIToUTF16(test_cases[i]), string16::npos, 764 string16(), GURL(), AutocompleteInput::INVALID_SPEC, 765 false, false, true, AutocompleteInput::ALL_MATCHES); 766 autocomplete_->Start(input, false); 767 if (!autocomplete_->done()) 768 base::MessageLoop::current()->Run(); 769 } 770} 771 772TEST_F(HistoryURLProviderTest, CullSearchResults) { 773 // Set up a default search engine. 774 TemplateURLData data; 775 data.SetKeyword(ASCIIToUTF16("TestEngine")); 776 data.SetURL("http://testsearch.com/?q={searchTerms}"); 777 TemplateURLService* template_url_service = 778 TemplateURLServiceFactory::GetForProfile(profile_.get()); 779 TemplateURL* template_url = new TemplateURL(profile_.get(), data); 780 template_url_service->Add(template_url); 781 template_url_service->SetDefaultSearchProvider(template_url); 782 template_url_service->Load(); 783 784 // URLs we will be using, plus the visit counts they will initially get 785 // (the redirect set below will also increment the visit counts). We want 786 // the results to be in A,B,C order. Note also that our visit counts are 787 // all high enough so that domain synthesizing won't get triggered. 788 struct TestCase { 789 const char* url; 790 int count; 791 } test_cases[] = { 792 {"https://testsearch.com/", 30}, 793 {"https://testsearch.com/?q=foobar", 20}, 794 {"http://foobar.com/", 10} 795 }; 796 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 797 history_service_->AddPageWithDetails(GURL(test_cases[i].url), 798 UTF8ToUTF16("Title"), test_cases[i].count, test_cases[i].count, 799 Time::Now(), false, history::SOURCE_BROWSED); 800 } 801 802 // We should not see search URLs when typing a previously used query. 803 const UrlAndLegalDefault expected_when_searching_query[] = { 804 { test_cases[2].url, false } 805 }; 806 RunTest(ASCIIToUTF16("foobar"), string16(), true, 807 expected_when_searching_query, arraysize(expected_when_searching_query)); 808 809 // We should not see search URLs when typing the search engine name. 810 const UrlAndLegalDefault expected_when_searching_site[] = { 811 { test_cases[0].url, false } 812 }; 813 RunTest(ASCIIToUTF16("testsearch"), string16(), true, 814 expected_when_searching_site, arraysize(expected_when_searching_site)); 815} 816 817TEST_F(HistoryURLProviderTest, SuggestExactInput) { 818 const size_t npos = std::string::npos; 819 struct TestCase { 820 // Inputs: 821 const char* input; 822 bool trim_http; 823 // Expected Outputs: 824 const char* contents; 825 // Offsets of the ACMatchClassifications, terminated by npos. 826 size_t offsets[3]; 827 // The index of the ACMatchClassification that should have the MATCH bit 828 // set, npos if no ACMatchClassification should have the MATCH bit set. 829 size_t match_classification_index; 830 } test_cases[] = { 831 { "http://www.somesite.com", false, 832 "http://www.somesite.com", {0, npos, npos}, 0 }, 833 { "www.somesite.com", true, 834 "www.somesite.com", {0, npos, npos}, 0 }, 835 { "www.somesite.com", false, 836 "http://www.somesite.com", {0, 7, npos}, 1 }, 837 { "somesite.com", true, 838 "somesite.com", {0, npos, npos}, 0 }, 839 { "somesite.com", false, 840 "http://somesite.com", {0, 7, npos}, 1 }, 841 { "w", true, 842 "w", {0, npos, npos}, 0 }, 843 { "w", false, 844 "http://w", {0, 7, npos}, 1 }, 845 { "w.com", true, 846 "w.com", {0, npos, npos}, 0 }, 847 { "w.com", false, 848 "http://w.com", {0, 7, npos}, 1 }, 849 { "www.w.com", true, 850 "www.w.com", {0, npos, npos}, 0 }, 851 { "www.w.com", false, 852 "http://www.w.com", {0, 7, npos}, 1 }, 853 { "view-source:w", true, 854 "view-source:w", {0, npos, npos}, 0 }, 855 { "view-source:www.w.com/", true, 856 "view-source:www.w.com", {0, npos, npos}, npos }, 857 { "view-source:www.w.com/", false, 858 "view-source:http://www.w.com", {0, npos, npos}, npos }, 859 { "view-source:http://www.w.com/", false, 860 "view-source:http://www.w.com", {0, npos, npos}, 0 }, 861 { " view-source:", true, 862 "view-source:", {0, npos, npos}, 0 }, 863 { "http:////////w.com", false, 864 "http://w.com", {0, npos, npos}, npos }, 865 { " http:////////www.w.com", false, 866 "http://www.w.com", {0, npos, npos}, npos }, 867 { "http:a///www.w.com", false, 868 "http://a///www.w.com", {0, npos, npos}, npos }, 869 { "mailto://a@b.com", true, 870 "mailto://a@b.com", {0, npos, npos}, 0 }, 871 { "mailto://a@b.com", false, 872 "mailto://a@b.com", {0, npos, npos}, 0 }, 873 }; 874 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 875 SCOPED_TRACE(testing::Message() << "Index " << i << " input: " 876 << test_cases[i].input << ", trim_http: " 877 << test_cases[i].trim_http); 878 879 AutocompleteInput input(ASCIIToUTF16(test_cases[i].input), string16::npos, 880 string16(), GURL("about:blank"), 881 AutocompleteInput::INVALID_SPEC, false, false, true, 882 AutocompleteInput::ALL_MATCHES); 883 AutocompleteMatch match = HistoryURLProvider::SuggestExactInput( 884 autocomplete_.get(), input, test_cases[i].trim_http); 885 EXPECT_EQ(ASCIIToUTF16(test_cases[i].contents), match.contents); 886 for (size_t match_index = 0; match_index < match.contents_class.size(); 887 ++match_index) { 888 EXPECT_EQ(test_cases[i].offsets[match_index], 889 match.contents_class[match_index].offset); 890 EXPECT_EQ(ACMatchClassification::URL | 891 (match_index == test_cases[i].match_classification_index ? 892 ACMatchClassification::MATCH : 0), 893 match.contents_class[match_index].style); 894 } 895 EXPECT_EQ(npos, test_cases[i].offsets[match.contents_class.size()]); 896 } 897} 898