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