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