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