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