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_quick_provider.h" 6 7 #include <algorithm> 8 #include <functional> 9 #include <set> 10 #include <string> 11 #include <vector> 12 13 #include "base/format_macros.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/message_loop/message_loop.h" 16 #include "base/prefs/pref_service.h" 17 #include "base/strings/stringprintf.h" 18 #include "base/strings/utf_string_conversions.h" 19 #include "chrome/browser/autocomplete/autocomplete_match.h" 20 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" 21 #include "chrome/browser/autocomplete/autocomplete_result.h" 22 #include "chrome/browser/autocomplete/history_url_provider.h" 23 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 24 #include "chrome/browser/chrome_notification_types.h" 25 #include "chrome/browser/history/history_backend.h" 26 #include "chrome/browser/history/history_database.h" 27 #include "chrome/browser/history/history_service.h" 28 #include "chrome/browser/history/history_service_factory.h" 29 #include "chrome/browser/history/in_memory_url_index.h" 30 #include "chrome/browser/history/url_database.h" 31 #include "chrome/browser/history/url_index_private_data.h" 32 #include "chrome/browser/search_engines/template_url.h" 33 #include "chrome/browser/search_engines/template_url_service.h" 34 #include "chrome/browser/search_engines/template_url_service_factory.h" 35 #include "chrome/common/pref_names.h" 36 #include "chrome/test/base/testing_browser_process.h" 37 #include "chrome/test/base/testing_profile.h" 38 #include "components/bookmarks/test/bookmark_test_helpers.h" 39 #include "components/metrics/proto/omnibox_event.pb.h" 40 #include "content/public/browser/notification_service.h" 41 #include "content/public/test/test_browser_thread.h" 42 #include "content/public/test/test_utils.h" 43 #include "sql/transaction.h" 44 #include "testing/gtest/include/gtest/gtest.h" 45 46 using base::ASCIIToUTF16; 47 using base::Time; 48 using base::TimeDelta; 49 50 using content::BrowserThread; 51 52 struct TestURLInfo { 53 std::string url; 54 std::string title; 55 int visit_count; 56 int typed_count; 57 int days_from_now; 58 } quick_test_db[] = { 59 {"http://www.google.com/", "Google", 3, 3, 0}, 60 {"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100, 0}, 61 {"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0, 0}, 62 {"http://freshmeat.net/unpopular.html", "Unpopular", 1, 1, 0}, 63 {"http://news.google.com/?ned=us&topic=n", "Google News - U.S.", 2, 2, 0}, 64 {"http://news.google.com/", "Google News", 1, 1, 0}, 65 {"http://foo.com/", "Dir", 200, 100, 0}, 66 {"http://foo.com/dir/", "Dir", 2, 1, 10}, 67 {"http://foo.com/dir/another/", "Dir", 10, 5, 0}, 68 {"http://foo.com/dir/another/again/", "Dir", 5, 1, 0}, 69 {"http://foo.com/dir/another/again/myfile.html", "File", 3, 1, 0}, 70 {"http://visitedest.com/y/a", "VA", 10, 1, 20}, 71 {"http://visitedest.com/y/b", "VB", 9, 1, 20}, 72 {"http://visitedest.com/x/c", "VC", 8, 1, 20}, 73 {"http://visitedest.com/x/d", "VD", 7, 1, 20}, 74 {"http://visitedest.com/y/e", "VE", 6, 1, 20}, 75 {"http://typeredest.com/y/a", "TA", 5, 5, 0}, 76 {"http://typeredest.com/y/b", "TB", 5, 4, 0}, 77 {"http://typeredest.com/x/c", "TC", 5, 3, 0}, 78 {"http://typeredest.com/x/d", "TD", 5, 2, 0}, 79 {"http://typeredest.com/y/e", "TE", 5, 1, 0}, 80 {"http://daysagoest.com/y/a", "DA", 1, 1, 0}, 81 {"http://daysagoest.com/y/b", "DB", 1, 1, 1}, 82 {"http://daysagoest.com/x/c", "DC", 1, 1, 2}, 83 {"http://daysagoest.com/x/d", "DD", 1, 1, 3}, 84 {"http://daysagoest.com/y/e", "DE", 1, 1, 4}, 85 {"http://abcdefghixyzjklmnopqrstuvw.com/a", "", 3, 1, 0}, 86 {"http://spaces.com/path%20with%20spaces/foo.html", "Spaces", 2, 2, 0}, 87 {"http://abcdefghijklxyzmnopqrstuvw.com/a", "", 3, 1, 0}, 88 {"http://abcdefxyzghijklmnopqrstuvw.com/a", "", 3, 1, 0}, 89 {"http://abcxyzdefghijklmnopqrstuvw.com/a", "", 3, 1, 0}, 90 {"http://xyzabcdefghijklmnopqrstuvw.com/a", "", 3, 1, 0}, 91 {"http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice", 92 "Dogs & Cats & Mice & Other Animals", 1, 1, 0}, 93 {"https://monkeytrap.org/", "", 3, 1, 0}, 94 {"http://popularsitewithpathonly.com/moo", 95 "popularsitewithpathonly.com/moo", 50, 50, 0}, 96 {"http://popularsitewithroot.com/", "popularsitewithroot.com", 50, 50, 0}, 97 {"http://testsearch.com/?q=thequery", "Test Search Engine", 10, 10, 0}, 98 {"http://testsearch.com/", "Test Search Engine", 9, 9, 0}, 99 {"http://anotherengine.com/?q=thequery", "Another Search Engine", 8, 8, 0}, 100 // The encoded stuff between /wiki/ and the # is 101 {"http://ja.wikipedia.org/wiki/%E7%AC%AC%E4%BA%8C%E6%AC%A1%E4%B8%96%E7%95" 102 "%8C%E5%A4%A7%E6%88%A6#.E3.83.B4.E3.82.A7.E3.83.AB.E3.82.B5.E3.82.A4.E3." 103 "83.A6.E4.BD.93.E5.88.B6", "Title Unimportant", 2, 2, 0} 104 }; 105 106 class HistoryQuickProviderTest : public testing::Test, 107 public AutocompleteProviderListener { 108 public: 109 HistoryQuickProviderTest() 110 : ui_thread_(BrowserThread::UI, &message_loop_), 111 file_thread_(BrowserThread::FILE, &message_loop_) {} 112 113 // AutocompleteProviderListener: 114 virtual void OnProviderUpdate(bool updated_matches) OVERRIDE {} 115 116 protected: 117 class SetShouldContain : public std::unary_function<const std::string&, 118 std::set<std::string> > { 119 public: 120 explicit SetShouldContain(const ACMatches& matched_urls); 121 122 void operator()(const std::string& expected); 123 124 std::set<std::string> LeftOvers() const { return matches_; } 125 126 private: 127 std::set<std::string> matches_; 128 }; 129 130 static KeyedService* CreateTemplateURLService( 131 content::BrowserContext* profile) { 132 return new TemplateURLService(static_cast<Profile*>(profile)); 133 } 134 135 virtual void SetUp(); 136 virtual void TearDown(); 137 138 virtual void GetTestData(size_t* data_count, TestURLInfo** test_data); 139 140 // Fills test data into the history system. 141 void FillData(); 142 143 // Runs an autocomplete query on |text| and checks to see that the returned 144 // results' destination URLs match those provided. |expected_urls| does not 145 // need to be in sorted order. 146 void RunTest(const base::string16 text, 147 bool prevent_inline_autocomplete, 148 std::vector<std::string> expected_urls, 149 bool can_inline_top_result, 150 base::string16 expected_fill_into_edit, 151 base::string16 autocompletion); 152 153 history::HistoryBackend* history_backend() { 154 return history_service_->history_backend_; 155 } 156 157 base::MessageLoopForUI message_loop_; 158 content::TestBrowserThread ui_thread_; 159 content::TestBrowserThread file_thread_; 160 161 scoped_ptr<TestingProfile> profile_; 162 HistoryService* history_service_; 163 164 ACMatches ac_matches_; // The resulting matches after running RunTest. 165 166 scoped_refptr<HistoryQuickProvider> provider_; 167 }; 168 169 void HistoryQuickProviderTest::SetUp() { 170 profile_.reset(new TestingProfile()); 171 ASSERT_TRUE(profile_->CreateHistoryService(true, false)); 172 profile_->CreateBookmarkModel(true); 173 test::WaitForBookmarkModelToLoad( 174 BookmarkModelFactory::GetForProfile(profile_.get())); 175 profile_->BlockUntilHistoryIndexIsRefreshed(); 176 history_service_ = 177 HistoryServiceFactory::GetForProfile(profile_.get(), 178 Profile::EXPLICIT_ACCESS); 179 EXPECT_TRUE(history_service_); 180 provider_ = new HistoryQuickProvider(this, profile_.get()); 181 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 182 profile_.get(), &HistoryQuickProviderTest::CreateTemplateURLService); 183 FillData(); 184 provider_->GetIndex()->RebuildFromHistory(history_backend()->db()); 185 } 186 187 void HistoryQuickProviderTest::TearDown() { 188 provider_ = NULL; 189 } 190 191 void HistoryQuickProviderTest::GetTestData(size_t* data_count, 192 TestURLInfo** test_data) { 193 DCHECK(data_count); 194 DCHECK(test_data); 195 *data_count = arraysize(quick_test_db); 196 *test_data = &quick_test_db[0]; 197 } 198 199 void HistoryQuickProviderTest::FillData() { 200 sql::Connection& db(history_backend()->db()->GetDB()); 201 ASSERT_TRUE(db.is_open()); 202 203 size_t data_count = 0; 204 TestURLInfo* test_data = NULL; 205 GetTestData(&data_count, &test_data); 206 size_t visit_id = 1; 207 for (size_t i = 0; i < data_count; ++i) { 208 const TestURLInfo& cur(test_data[i]); 209 Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now); 210 sql::Transaction transaction(&db); 211 212 // Add URL. 213 transaction.Begin(); 214 std::string sql_cmd_line = base::StringPrintf( 215 "INSERT INTO \"urls\" VALUES(%" PRIuS ", \'%s\', \'%s\', %d, %d, %" 216 PRId64 ", 0, 0)", 217 i + 1, cur.url.c_str(), cur.title.c_str(), cur.visit_count, 218 cur.typed_count, visit_time.ToInternalValue()); 219 sql::Statement sql_stmt(db.GetUniqueStatement(sql_cmd_line.c_str())); 220 EXPECT_TRUE(sql_stmt.Run()); 221 transaction.Commit(); 222 223 // Add visits. 224 for (int j = 0; j < cur.visit_count; ++j) { 225 // Assume earlier visits are at one-day intervals. 226 visit_time -= TimeDelta::FromDays(1); 227 transaction.Begin(); 228 // Mark the most recent |cur.typed_count| visits as typed. 229 std::string sql_cmd_line = base::StringPrintf( 230 "INSERT INTO \"visits\" VALUES(%" PRIuS ", %" PRIuS ", %" PRId64 231 ", 0, %d, 0, 1)", 232 visit_id++, i + 1, visit_time.ToInternalValue(), 233 (j < cur.typed_count) ? content::PAGE_TRANSITION_TYPED : 234 content::PAGE_TRANSITION_LINK); 235 236 sql::Statement sql_stmt(db.GetUniqueStatement(sql_cmd_line.c_str())); 237 EXPECT_TRUE(sql_stmt.Run()); 238 transaction.Commit(); 239 } 240 } 241 } 242 243 HistoryQuickProviderTest::SetShouldContain::SetShouldContain( 244 const ACMatches& matched_urls) { 245 for (ACMatches::const_iterator iter = matched_urls.begin(); 246 iter != matched_urls.end(); ++iter) 247 matches_.insert(iter->destination_url.spec()); 248 } 249 250 void HistoryQuickProviderTest::SetShouldContain::operator()( 251 const std::string& expected) { 252 EXPECT_EQ(1U, matches_.erase(expected)) 253 << "Results did not contain '" << expected << "' but should have."; 254 } 255 256 257 void HistoryQuickProviderTest::RunTest(const base::string16 text, 258 bool prevent_inline_autocomplete, 259 std::vector<std::string> expected_urls, 260 bool can_inline_top_result, 261 base::string16 expected_fill_into_edit, 262 base::string16 expected_autocompletion) { 263 SCOPED_TRACE(text); // Minimal hint to query being run. 264 base::MessageLoop::current()->RunUntilIdle(); 265 AutocompleteInput input(text, base::string16::npos, base::string16(), 266 GURL(), metrics::OmniboxEventProto::INVALID_SPEC, 267 prevent_inline_autocomplete, false, true, true); 268 provider_->Start(input, false); 269 EXPECT_TRUE(provider_->done()); 270 271 ac_matches_ = provider_->matches(); 272 273 // We should have gotten back at most AutocompleteProvider::kMaxMatches. 274 EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches); 275 276 // If the number of expected and actual matches aren't equal then we need 277 // test no further, but let's do anyway so that we know which URLs failed. 278 EXPECT_EQ(expected_urls.size(), ac_matches_.size()); 279 280 // Verify that all expected URLs were found and that all found URLs 281 // were expected. 282 std::set<std::string> leftovers = 283 for_each(expected_urls.begin(), expected_urls.end(), 284 SetShouldContain(ac_matches_)).LeftOvers(); 285 EXPECT_EQ(0U, leftovers.size()) << "There were " << leftovers.size() 286 << " unexpected results, one of which was: '" 287 << *(leftovers.begin()) << "'."; 288 289 if (expected_urls.empty()) 290 return; 291 292 // Verify that we got the results in the order expected. 293 int best_score = ac_matches_.begin()->relevance + 1; 294 int i = 0; 295 std::vector<std::string>::const_iterator expected = expected_urls.begin(); 296 for (ACMatches::const_iterator actual = ac_matches_.begin(); 297 actual != ac_matches_.end() && expected != expected_urls.end(); 298 ++actual, ++expected, ++i) { 299 EXPECT_EQ(*expected, actual->destination_url.spec()) 300 << "For result #" << i << " we got '" << actual->destination_url.spec() 301 << "' but expected '" << *expected << "'."; 302 EXPECT_LT(actual->relevance, best_score) 303 << "At result #" << i << " (url=" << actual->destination_url.spec() 304 << "), we noticed scores are not monotonically decreasing."; 305 best_score = actual->relevance; 306 } 307 308 EXPECT_EQ(can_inline_top_result, ac_matches_[0].allowed_to_be_default_match); 309 if (can_inline_top_result) 310 EXPECT_EQ(expected_autocompletion, ac_matches_[0].inline_autocompletion); 311 EXPECT_EQ(expected_fill_into_edit, ac_matches_[0].fill_into_edit); 312 } 313 314 TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) { 315 std::vector<std::string> expected_urls; 316 expected_urls.push_back("http://slashdot.org/favorite_page.html"); 317 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, 318 ASCIIToUTF16("slashdot.org/favorite_page.html"), 319 ASCIIToUTF16(".org/favorite_page.html")); 320 } 321 322 TEST_F(HistoryQuickProviderTest, WordBoundariesWithPunctuationMatch) { 323 std::vector<std::string> expected_urls; 324 expected_urls.push_back("http://popularsitewithpathonly.com/moo"); 325 RunTest(ASCIIToUTF16("/moo"), false, expected_urls, false, 326 ASCIIToUTF16("popularsitewithpathonly.com/moo"), base::string16()); 327 } 328 329 TEST_F(HistoryQuickProviderTest, MultiTermTitleMatch) { 330 std::vector<std::string> expected_urls; 331 expected_urls.push_back( 332 "http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice"); 333 RunTest(ASCIIToUTF16("mice other animals"), false, expected_urls, false, 334 ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"), 335 base::string16()); 336 } 337 338 TEST_F(HistoryQuickProviderTest, NonWordLastCharacterMatch) { 339 std::string expected_url("http://slashdot.org/favorite_page.html"); 340 std::vector<std::string> expected_urls; 341 expected_urls.push_back(expected_url); 342 RunTest(ASCIIToUTF16("slashdot.org/"), false, expected_urls, true, 343 ASCIIToUTF16("slashdot.org/favorite_page.html"), 344 ASCIIToUTF16("favorite_page.html")); 345 } 346 347 TEST_F(HistoryQuickProviderTest, MultiMatch) { 348 std::vector<std::string> expected_urls; 349 // Scores high because of typed_count. 350 expected_urls.push_back("http://foo.com/"); 351 // Scores high because of visit count. 352 expected_urls.push_back("http://foo.com/dir/another/"); 353 // Scores high because of high visit count. 354 expected_urls.push_back("http://foo.com/dir/another/again/"); 355 RunTest(ASCIIToUTF16("foo"), false, expected_urls, true, 356 ASCIIToUTF16("foo.com"), ASCIIToUTF16(".com")); 357 } 358 359 TEST_F(HistoryQuickProviderTest, StartRelativeMatch) { 360 std::vector<std::string> expected_urls; 361 expected_urls.push_back("http://xyzabcdefghijklmnopqrstuvw.com/a"); 362 RunTest(ASCIIToUTF16("xyza"), false, expected_urls, true, 363 ASCIIToUTF16("xyzabcdefghijklmnopqrstuvw.com/a"), 364 ASCIIToUTF16("bcdefghijklmnopqrstuvw.com/a")); 365 } 366 367 TEST_F(HistoryQuickProviderTest, EncodingMatch) { 368 std::vector<std::string> expected_urls; 369 expected_urls.push_back("http://spaces.com/path%20with%20spaces/foo.html"); 370 RunTest(ASCIIToUTF16("path with spaces"), false, expected_urls, false, 371 ASCIIToUTF16("spaces.com/path with spaces/foo.html"), 372 base::string16()); 373 } 374 375 TEST_F(HistoryQuickProviderTest, ContentsClass) { 376 std::vector<std::string> expected_urls; 377 expected_urls.push_back( 378 "http://ja.wikipedia.org/wiki/%E7%AC%AC%E4%BA%8C%E6%AC%A1%E4%B8%96%E7" 379 "%95%8C%E5%A4%A7%E6%88%A6#.E3.83.B4.E3.82.A7.E3.83.AB.E3.82.B5.E3.82." 380 "A4.E3.83.A6.E4.BD.93.E5.88.B6"); 381 RunTest(base::UTF8ToUTF16(" e3"), false, expected_urls, false, 382 base::UTF8ToUTF16("ja.wikipedia.org/wiki/#.E3.83.B4.E3." 383 "82.A7.E3.83.AB.E3.82.B5.E3.82.A4.E3.83.A6.E4.BD." 384 "93.E5.88.B6"), 385 base::string16()); 386 #ifndef NDEBUG 387 ac_matches_[0].Validate(); 388 #endif 389 // Verify that contents_class divides the string in the right places. 390 // [22, 24) is the "". All the other pairs are the "e3". 391 ACMatchClassifications contents_class(ac_matches_[0].contents_class); 392 size_t expected_offsets[] = { 0, 22, 24, 31, 33, 40, 42, 49, 51, 58, 60, 67, 393 69, 76, 78 }; 394 // ScoredHistoryMatch may not highlight all the occurrences of these terms 395 // because it only highlights terms at word breaks, and it only stores word 396 // breaks up to some specified number of characters (50 at the time of this 397 // comment). This test is written flexibly so it still will pass if we 398 // increase that number in the future. Regardless, we require the first 399 // five offsets to be correct--in this example these cover at least one 400 // occurrence of each term. 401 EXPECT_LE(contents_class.size(), arraysize(expected_offsets)); 402 EXPECT_GE(contents_class.size(), 5u); 403 for (size_t i = 0; i < contents_class.size(); ++i) 404 EXPECT_EQ(expected_offsets[i], contents_class[i].offset); 405 } 406 407 TEST_F(HistoryQuickProviderTest, VisitCountMatches) { 408 std::vector<std::string> expected_urls; 409 expected_urls.push_back("http://visitedest.com/y/a"); 410 expected_urls.push_back("http://visitedest.com/y/b"); 411 expected_urls.push_back("http://visitedest.com/x/c"); 412 RunTest(ASCIIToUTF16("visitedest"), false, expected_urls, true, 413 ASCIIToUTF16("visitedest.com/y/a"), 414 ASCIIToUTF16(".com/y/a")); 415 } 416 417 TEST_F(HistoryQuickProviderTest, TypedCountMatches) { 418 std::vector<std::string> expected_urls; 419 expected_urls.push_back("http://typeredest.com/y/a"); 420 expected_urls.push_back("http://typeredest.com/y/b"); 421 expected_urls.push_back("http://typeredest.com/x/c"); 422 RunTest(ASCIIToUTF16("typeredest"), false, expected_urls, true, 423 ASCIIToUTF16("typeredest.com/y/a"), 424 ASCIIToUTF16(".com/y/a")); 425 } 426 427 TEST_F(HistoryQuickProviderTest, DaysAgoMatches) { 428 std::vector<std::string> expected_urls; 429 expected_urls.push_back("http://daysagoest.com/y/a"); 430 expected_urls.push_back("http://daysagoest.com/y/b"); 431 expected_urls.push_back("http://daysagoest.com/x/c"); 432 RunTest(ASCIIToUTF16("daysagoest"), false, expected_urls, true, 433 ASCIIToUTF16("daysagoest.com/y/a"), 434 ASCIIToUTF16(".com/y/a")); 435 } 436 437 TEST_F(HistoryQuickProviderTest, EncodingLimitMatch) { 438 std::vector<std::string> expected_urls; 439 std::string url( 440 "http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice"); 441 // First check that a mid-word match yield no results. 442 RunTest(ASCIIToUTF16("ice"), false, expected_urls, false, 443 ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"), 444 base::string16()); 445 // Then check that we get results when the match is at a word start 446 // that is present because of an encoded separate (%20 = space). 447 expected_urls.push_back(url); 448 RunTest(ASCIIToUTF16("Mice"), false, expected_urls, false, 449 ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"), 450 base::string16()); 451 // Verify that the matches' ACMatchClassifications offsets are in range. 452 ACMatchClassifications content(ac_matches_[0].contents_class); 453 // The max offset accounts for 6 occurrences of '%20' plus the 'http://'. 454 const size_t max_offset = url.length() - ((6 * 2) + 7); 455 for (ACMatchClassifications::const_iterator citer = content.begin(); 456 citer != content.end(); ++citer) 457 EXPECT_LT(citer->offset, max_offset); 458 ACMatchClassifications description(ac_matches_[0].description_class); 459 std::string page_title("Dogs & Cats & Mice & Other Animals"); 460 for (ACMatchClassifications::const_iterator diter = description.begin(); 461 diter != description.end(); ++diter) 462 EXPECT_LT(diter->offset, page_title.length()); 463 } 464 465 TEST_F(HistoryQuickProviderTest, Spans) { 466 // Test SpansFromTermMatch 467 history::TermMatches matches_a; 468 // Simulates matches: '.xx.xxx..xx...xxxxx..' which will test no match at 469 // either beginning or end as well as adjacent matches. 470 matches_a.push_back(history::TermMatch(1, 1, 2)); 471 matches_a.push_back(history::TermMatch(2, 4, 3)); 472 matches_a.push_back(history::TermMatch(3, 9, 1)); 473 matches_a.push_back(history::TermMatch(3, 10, 1)); 474 matches_a.push_back(history::TermMatch(4, 14, 5)); 475 ACMatchClassifications spans_a = 476 HistoryQuickProvider::SpansFromTermMatch(matches_a, 20, false); 477 // ACMatch spans should be: 'NM-NM---N-M-N--M----N-' 478 ASSERT_EQ(9U, spans_a.size()); 479 EXPECT_EQ(0U, spans_a[0].offset); 480 EXPECT_EQ(ACMatchClassification::NONE, spans_a[0].style); 481 EXPECT_EQ(1U, spans_a[1].offset); 482 EXPECT_EQ(ACMatchClassification::MATCH, spans_a[1].style); 483 EXPECT_EQ(3U, spans_a[2].offset); 484 EXPECT_EQ(ACMatchClassification::NONE, spans_a[2].style); 485 EXPECT_EQ(4U, spans_a[3].offset); 486 EXPECT_EQ(ACMatchClassification::MATCH, spans_a[3].style); 487 EXPECT_EQ(7U, spans_a[4].offset); 488 EXPECT_EQ(ACMatchClassification::NONE, spans_a[4].style); 489 EXPECT_EQ(9U, spans_a[5].offset); 490 EXPECT_EQ(ACMatchClassification::MATCH, spans_a[5].style); 491 EXPECT_EQ(11U, spans_a[6].offset); 492 EXPECT_EQ(ACMatchClassification::NONE, spans_a[6].style); 493 EXPECT_EQ(14U, spans_a[7].offset); 494 EXPECT_EQ(ACMatchClassification::MATCH, spans_a[7].style); 495 EXPECT_EQ(19U, spans_a[8].offset); 496 EXPECT_EQ(ACMatchClassification::NONE, spans_a[8].style); 497 // Simulates matches: 'xx.xx' which will test matches at both beginning and 498 // end. 499 history::TermMatches matches_b; 500 matches_b.push_back(history::TermMatch(1, 0, 2)); 501 matches_b.push_back(history::TermMatch(2, 3, 2)); 502 ACMatchClassifications spans_b = 503 HistoryQuickProvider::SpansFromTermMatch(matches_b, 5, true); 504 // ACMatch spans should be: 'M-NM-' 505 ASSERT_EQ(3U, spans_b.size()); 506 EXPECT_EQ(0U, spans_b[0].offset); 507 EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL, 508 spans_b[0].style); 509 EXPECT_EQ(2U, spans_b[1].offset); 510 EXPECT_EQ(ACMatchClassification::URL, spans_b[1].style); 511 EXPECT_EQ(3U, spans_b[2].offset); 512 EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL, 513 spans_b[2].style); 514 } 515 516 TEST_F(HistoryQuickProviderTest, DeleteMatch) { 517 GURL test_url("http://slashdot.org/favorite_page.html"); 518 std::vector<std::string> expected_urls; 519 expected_urls.push_back(test_url.spec()); 520 // Fill up ac_matches_; we don't really care about the test yet. 521 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, 522 ASCIIToUTF16("slashdot.org/favorite_page.html"), 523 ASCIIToUTF16(".org/favorite_page.html")); 524 EXPECT_EQ(1U, ac_matches_.size()); 525 EXPECT_TRUE(history_backend()->GetURL(test_url, NULL)); 526 provider_->DeleteMatch(ac_matches_[0]); 527 528 // Check that the underlying URL is deleted from the history DB (this implies 529 // that all visits are gone as well). Also verify that a deletion notification 530 // is sent, in response to which the secondary data stores (InMemoryDatabase, 531 // InMemoryURLIndex) will drop any data they might have pertaining to the URL. 532 // To ensure that the deletion has been propagated everywhere before we start 533 // verifying post-deletion states, first wait until we see the notification. 534 content::WindowedNotificationObserver observer( 535 chrome::NOTIFICATION_HISTORY_URLS_DELETED, 536 content::NotificationService::AllSources()); 537 observer.Wait(); 538 EXPECT_FALSE(history_backend()->GetURL(test_url, NULL)); 539 540 // Just to be on the safe side, explicitly verify that we have deleted enough 541 // data so that we will not be serving the same result again. 542 expected_urls.clear(); 543 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, 544 ASCIIToUTF16("NONE EXPECTED"), base::string16()); 545 } 546 547 TEST_F(HistoryQuickProviderTest, PreventBeatingURLWhatYouTypedMatch) { 548 std::vector<std::string> expected_urls; 549 550 expected_urls.clear(); 551 expected_urls.push_back("http://popularsitewithroot.com/"); 552 // If the user enters a hostname (no path) that he/she has visited 553 // before, we should make sure that all HistoryQuickProvider results 554 // have scores less than what HistoryURLProvider will assign the 555 // URL-what-you-typed match. 556 RunTest(ASCIIToUTF16("popularsitewithroot.com"), false, expected_urls, true, 557 ASCIIToUTF16("popularsitewithroot.com"), base::string16()); 558 EXPECT_LT(ac_matches_[0].relevance, 559 HistoryURLProvider::kScoreForBestInlineableResult); 560 561 // Check that if the user didn't quite enter the full hostname, this 562 // hostname would've normally scored above the URL-what-you-typed match. 563 RunTest(ASCIIToUTF16("popularsitewithroot.c"), false, expected_urls, true, 564 ASCIIToUTF16("popularsitewithroot.com"), 565 ASCIIToUTF16("om")); 566 EXPECT_GE(ac_matches_[0].relevance, 567 HistoryURLProvider::kScoreForWhatYouTypedResult); 568 569 expected_urls.clear(); 570 expected_urls.push_back("http://popularsitewithpathonly.com/moo"); 571 // If the user enters a hostname of a host that he/she has visited 572 // but never visited the root page of, we should make sure that all 573 // HistoryQuickProvider results have scores less than what the 574 // HistoryURLProvider will assign the URL-what-you-typed match. 575 RunTest(ASCIIToUTF16("popularsitewithpathonly.com"), false, expected_urls, 576 true, 577 ASCIIToUTF16("popularsitewithpathonly.com/moo"), 578 ASCIIToUTF16("/moo")); 579 EXPECT_LT(ac_matches_[0].relevance, 580 HistoryURLProvider::kScoreForUnvisitedIntranetResult); 581 582 // Verify the same thing happens if the user adds a / to end of the 583 // hostname. 584 RunTest(ASCIIToUTF16("popularsitewithpathonly.com/"), false, expected_urls, 585 true, ASCIIToUTF16("popularsitewithpathonly.com/moo"), 586 ASCIIToUTF16("moo")); 587 EXPECT_LT(ac_matches_[0].relevance, 588 HistoryURLProvider::kScoreForUnvisitedIntranetResult); 589 590 // Check that if the user didn't quite enter the full hostname, this 591 // page would've normally scored above the URL-what-you-typed match. 592 RunTest(ASCIIToUTF16("popularsitewithpathonly.co"), false, expected_urls, 593 true, ASCIIToUTF16("popularsitewithpathonly.com/moo"), 594 ASCIIToUTF16("m/moo")); 595 EXPECT_GE(ac_matches_[0].relevance, 596 HistoryURLProvider::kScoreForWhatYouTypedResult); 597 598 // If the user enters a hostname + path that he/she has not visited 599 // before (but visited other things on the host), we can allow 600 // inline autocompletions. 601 RunTest(ASCIIToUTF16("popularsitewithpathonly.com/mo"), false, expected_urls, 602 true, 603 ASCIIToUTF16("popularsitewithpathonly.com/moo"), 604 ASCIIToUTF16("o")); 605 EXPECT_GE(ac_matches_[0].relevance, 606 HistoryURLProvider::kScoreForWhatYouTypedResult); 607 608 // If the user enters a hostname + path that he/she has visited 609 // before, we should make sure that all HistoryQuickProvider results 610 // have scores less than what the HistoryURLProvider will assign 611 // the URL-what-you-typed match. 612 RunTest(ASCIIToUTF16("popularsitewithpathonly.com/moo"), false, 613 expected_urls, true, 614 ASCIIToUTF16("popularsitewithpathonly.com/moo"), base::string16()); 615 EXPECT_LT(ac_matches_[0].relevance, 616 HistoryURLProvider::kScoreForBestInlineableResult); 617 } 618 619 TEST_F(HistoryQuickProviderTest, PreventInlineAutocomplete) { 620 std::vector<std::string> expected_urls; 621 expected_urls.push_back("http://popularsitewithroot.com/"); 622 623 // Check that the desired URL is normally allowed to be the default match 624 // against input that is a prefex of the URL. 625 RunTest(ASCIIToUTF16("popularsitewithr"), false, expected_urls, true, 626 ASCIIToUTF16("popularsitewithroot.com"), 627 ASCIIToUTF16("oot.com")); 628 629 // Check that it's not allowed to be the default match if 630 // prevent_inline_autocomplete is true. 631 RunTest(ASCIIToUTF16("popularsitewithr"), true, expected_urls, false, 632 ASCIIToUTF16("popularsitewithroot.com"), 633 ASCIIToUTF16("oot.com")); 634 635 // But the exact hostname can still match even if prevent inline autocomplete 636 // is true. i.e., there's no autocompletion necessary; this is effectively 637 // URL-what-you-typed. 638 RunTest(ASCIIToUTF16("popularsitewithroot.com"), true, expected_urls, true, 639 ASCIIToUTF16("popularsitewithroot.com"), base::string16()); 640 641 // The above still holds even with an extra trailing slash. 642 RunTest(ASCIIToUTF16("popularsitewithroot.com/"), true, expected_urls, true, 643 ASCIIToUTF16("popularsitewithroot.com"), base::string16()); 644 } 645 646 TEST_F(HistoryQuickProviderTest, CullSearchResults) { 647 // Set up a default search engine. 648 TemplateURLData data; 649 data.SetKeyword(ASCIIToUTF16("TestEngine")); 650 data.SetURL("http://testsearch.com/?q={searchTerms}"); 651 TemplateURLService* template_url_service = 652 TemplateURLServiceFactory::GetForProfile(profile_.get()); 653 TemplateURL* template_url = new TemplateURL(data); 654 template_url_service->Add(template_url); 655 template_url_service->SetUserSelectedDefaultSearchProvider(template_url); 656 template_url_service->Load(); 657 658 // A search results page should not be returned when typing a query. 659 std::vector<std::string> expected_urls; 660 expected_urls.push_back("http://anotherengine.com/?q=thequery"); 661 RunTest(ASCIIToUTF16("thequery"), false, expected_urls, false, 662 ASCIIToUTF16("anotherengine.com/?q=thequery"), base::string16()); 663 664 // A search results page should not be returned when typing the engine URL. 665 expected_urls.clear(); 666 expected_urls.push_back("http://testsearch.com/"); 667 RunTest(ASCIIToUTF16("testsearch"), false, expected_urls, true, 668 ASCIIToUTF16("testsearch.com"), 669 ASCIIToUTF16(".com")); 670 } 671 672 // HQPOrderingTest ------------------------------------------------------------- 673 674 TestURLInfo ordering_test_db[] = { 675 {"http://www.teamliquid.net/tlpd/korean/games/21648_bisu_vs_iris", "", 6, 3, 676 256}, 677 {"http://www.amazon.com/", "amazon.com: online shopping for electronics, " 678 "apparel, computers, books, dvds & more", 20, 20, 10}, 679 {"http://www.teamliquid.net/forum/viewmessage.php?topic_id=52045&" 680 "currentpage=83", "google images", 6, 6, 0}, 681 {"http://www.tempurpedic.com/", "tempur-pedic", 7, 7, 0}, 682 {"http://www.teamfortress.com/", "", 5, 5, 6}, 683 {"http://www.rottentomatoes.com/", "", 3, 3, 7}, 684 {"http://music.google.com/music/listen?u=0#start_pl", "", 3, 3, 9}, 685 {"https://www.emigrantdirect.com/", "high interest savings account, high " 686 "yield savings - emigrantdirect", 5, 5, 3}, 687 {"http://store.steampowered.com/", "", 6, 6, 1}, 688 {"http://techmeme.com/", "techmeme", 111, 110, 4}, 689 {"http://www.teamliquid.net/tlpd", "team liquid progaming database", 15, 15, 690 2}, 691 {"http://store.steampowered.com/", "the steam summer camp sale", 6, 6, 1}, 692 {"http://www.teamliquid.net/tlpd/korean/players", "tlpd - bw korean - player " 693 "index", 25, 7, 219}, 694 {"http://slashdot.org/", "slashdot: news for nerds, stuff that matters", 3, 3, 695 6}, 696 {"http://translate.google.com/", "google translate", 3, 3, 0}, 697 {"http://arstechnica.com/", "ars technica", 3, 3, 3}, 698 {"http://www.rottentomatoes.com/", "movies | movie trailers | reviews - " 699 "rotten tomatoes", 3, 3, 7}, 700 {"http://www.teamliquid.net/", "team liquid - starcraft 2 and brood war pro " 701 "gaming news", 26, 25, 3}, 702 {"http://metaleater.com/", "metaleater", 4, 3, 8}, 703 {"http://half.com/", "half.com: textbooks , books , music , movies , games , " 704 "video games", 4, 4, 6}, 705 {"http://teamliquid.net/", "team liquid - starcraft 2 and brood war pro " 706 "gaming news", 8, 5, 9}, 707 }; 708 709 class HQPOrderingTest : public HistoryQuickProviderTest { 710 protected: 711 virtual void GetTestData(size_t* data_count, 712 TestURLInfo** test_data) OVERRIDE; 713 }; 714 715 void HQPOrderingTest::GetTestData(size_t* data_count, TestURLInfo** test_data) { 716 DCHECK(data_count); 717 DCHECK(test_data); 718 *data_count = arraysize(ordering_test_db); 719 *test_data = &ordering_test_db[0]; 720 } 721 722 TEST_F(HQPOrderingTest, TEMatch) { 723 std::vector<std::string> expected_urls; 724 expected_urls.push_back("http://techmeme.com/"); 725 expected_urls.push_back("http://www.teamliquid.net/"); 726 expected_urls.push_back("http://www.teamliquid.net/tlpd"); 727 RunTest(ASCIIToUTF16("te"), false, expected_urls, true, 728 ASCIIToUTF16("techmeme.com"), 729 ASCIIToUTF16("chmeme.com")); 730 } 731 732 TEST_F(HQPOrderingTest, TEAMatch) { 733 std::vector<std::string> expected_urls; 734 expected_urls.push_back("http://www.teamliquid.net/"); 735 expected_urls.push_back("http://www.teamliquid.net/tlpd"); 736 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); 737 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true, 738 ASCIIToUTF16("www.teamliquid.net"), 739 ASCIIToUTF16("mliquid.net")); 740 } 741