| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/utf_string_conversions.h" |
| 6 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 5 #include "chrome/browser/autocomplete/history_quick_provider.h" | 7 #include "chrome/browser/autocomplete/history_quick_provider.h" |
| 8 #include "chrome/browser/history/in_memory_url_cache_database.h" |
| 9 #include "chrome/browser/history/in_memory_url_index_base_unittest.h" |
| 10 #include "chrome/test/base/testing_profile.h" |
| 6 | 11 |
| 7 #include <algorithm> | 12 class HistoryQuickProviderTest : public history::InMemoryURLIndexBaseTest { |
| 8 #include <functional> | 13 protected: |
| 9 #include <set> | 14 virtual FilePath::StringType TestDBName() const OVERRIDE; |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | 15 |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/message_loop.h" | |
| 15 #include "base/utf_string_conversions.h" | |
| 16 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | |
| 17 #include "chrome/browser/autocomplete/autocomplete_result.h" | |
| 18 #include "chrome/browser/history/history.h" | |
| 19 #include "chrome/browser/history/history_service_factory.h" | |
| 20 #include "chrome/browser/history/in_memory_url_index.h" | |
| 21 #include "chrome/browser/history/url_database.h" | |
| 22 #include "chrome/browser/history/url_index_private_data.h" | |
| 23 #include "chrome/browser/prefs/pref_service.h" | |
| 24 #include "chrome/common/pref_names.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 | |
| 30 using base::Time; | |
| 31 using base::TimeDelta; | |
| 32 | |
| 33 using content::BrowserThread; | |
| 34 | |
| 35 struct TestURLInfo { | |
| 36 std::string url; | |
| 37 std::string title; | |
| 38 int visit_count; | |
| 39 int typed_count; | |
| 40 int days_from_now; | |
| 41 } quick_test_db[] = { | |
| 42 {"http://www.google.com/", "Google", 3, 3, 0}, | |
| 43 {"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100, 0}, | |
| 44 {"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0, 0}, | |
| 45 {"http://freshmeat.net/unpopular.html", "Unpopular", 1, 1, 0}, | |
| 46 {"http://news.google.com/?ned=us&topic=n", "Google News - U.S.", 2, 2, 0}, | |
| 47 {"http://news.google.com/", "Google News", 1, 1, 0}, | |
| 48 {"http://foo.com/", "Dir", 200, 100, 0}, | |
| 49 {"http://foo.com/dir/", "Dir", 2, 1, 10}, | |
| 50 {"http://foo.com/dir/another/", "Dir", 5, 10, 0}, | |
| 51 {"http://foo.com/dir/another/again/", "Dir", 5, 1, 0}, | |
| 52 {"http://foo.com/dir/another/again/myfile.html", "File", 3, 2, 0}, | |
| 53 {"http://visitedest.com/y/a", "VA", 10, 1, 20}, | |
| 54 {"http://visitedest.com/y/b", "VB", 9, 1, 20}, | |
| 55 {"http://visitedest.com/x/c", "VC", 8, 1, 20}, | |
| 56 {"http://visitedest.com/x/d", "VD", 7, 1, 20}, | |
| 57 {"http://visitedest.com/y/e", "VE", 6, 1, 20}, | |
| 58 {"http://typeredest.com/y/a", "TA", 3, 5, 0}, | |
| 59 {"http://typeredest.com/y/b", "TB", 3, 4, 0}, | |
| 60 {"http://typeredest.com/x/c", "TC", 3, 3, 0}, | |
| 61 {"http://typeredest.com/x/d", "TD", 3, 2, 0}, | |
| 62 {"http://typeredest.com/y/e", "TE", 3, 1, 0}, | |
| 63 {"http://daysagoest.com/y/a", "DA", 1, 1, 0}, | |
| 64 {"http://daysagoest.com/y/b", "DB", 1, 1, 1}, | |
| 65 {"http://daysagoest.com/x/c", "DC", 1, 1, 2}, | |
| 66 {"http://daysagoest.com/x/d", "DD", 1, 1, 3}, | |
| 67 {"http://daysagoest.com/y/e", "DE", 1, 1, 4}, | |
| 68 {"http://abcdefghixyzjklmnopqrstuvw.com/a", "", 3, 1, 0}, | |
| 69 {"http://spaces.com/path%20with%20spaces/foo.html", "Spaces", 2, 2, 0}, | |
| 70 {"http://abcdefghijklxyzmnopqrstuvw.com/a", "", 3, 1, 0}, | |
| 71 {"http://abcdefxyzghijklmnopqrstuvw.com/a", "", 3, 1, 0}, | |
| 72 {"http://abcxyzdefghijklmnopqrstuvw.com/a", "", 3, 1, 0}, | |
| 73 {"http://xyzabcdefghijklmnopqrstuvw.com/a", "", 3, 1, 0}, | |
| 74 {"http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice", | |
| 75 "Dogs & Cats & Mice & Other Animals", 1, 1, 0}, | |
| 76 {"https://monkeytrap.org/", "", 3, 1, 0}, | |
| 77 }; | |
| 78 | |
| 79 class HistoryQuickProviderTest : public testing::Test, | |
| 80 public AutocompleteProviderListener { | |
| 81 public: | |
| 82 HistoryQuickProviderTest() | |
| 83 : ui_thread_(BrowserThread::UI, &message_loop_), | |
| 84 file_thread_(BrowserThread::FILE, &message_loop_) {} | |
| 85 | |
| 86 // AutocompleteProviderListener: | |
| 87 virtual void OnProviderUpdate(bool updated_matches) OVERRIDE; | |
| 88 | |
| 89 protected: | |
| 90 class SetShouldContain : public std::unary_function<const std::string&, | 16 class SetShouldContain : public std::unary_function<const std::string&, |
| 91 std::set<std::string> > { | 17 std::set<std::string> > { |
| 92 public: | 18 public: |
| 93 explicit SetShouldContain(const ACMatches& matched_urls); | 19 explicit SetShouldContain(const ACMatches& matched_urls); |
| 94 | |
| 95 void operator()(const std::string& expected); | 20 void operator()(const std::string& expected); |
| 96 | |
| 97 std::set<std::string> LeftOvers() const { return matches_; } | 21 std::set<std::string> LeftOvers() const { return matches_; } |
| 98 | 22 |
| 99 private: | 23 private: |
| 100 std::set<std::string> matches_; | 24 std::set<std::string> matches_; |
| 101 }; | 25 }; |
| 102 | 26 |
| 103 void SetUp(); | 27 virtual void SetUp() OVERRIDE; |
| 104 void TearDown(); | |
| 105 | |
| 106 virtual void GetTestData(size_t* data_count, TestURLInfo** test_data); | |
| 107 | |
| 108 // Fills test data into the history system. | |
| 109 void FillData(); | |
| 110 | 28 |
| 111 // Runs an autocomplete query on |text| and checks to see that the returned | 29 // Runs an autocomplete query on |text| and checks to see that the returned |
| 112 // results' destination URLs match those provided. |expected_urls| does not | 30 // results' destination URLs match those provided. |expected_urls| does not |
| 113 // need to be in sorted order. | 31 // need to be in sorted order. |
| 114 void RunTest(const string16 text, | 32 void RunTest(const string16 text, |
| 115 std::vector<std::string> expected_urls, | 33 std::vector<std::string> expected_urls, |
| 116 bool can_inline_top_result, | 34 bool can_inline_top_result, |
| 117 string16 expected_fill_into_edit); | 35 string16 expected_fill_into_edit); |
| 118 | 36 |
| 119 // Pass-through functions to simplify our friendship with URLIndexPrivateData. | |
| 120 bool UpdateURL(const history::URLRow& row); | |
| 121 | |
| 122 MessageLoopForUI message_loop_; | |
| 123 content::TestBrowserThread ui_thread_; | |
| 124 content::TestBrowserThread file_thread_; | |
| 125 | |
| 126 scoped_ptr<TestingProfile> profile_; | |
| 127 HistoryService* history_service_; | |
| 128 | |
| 129 ACMatches ac_matches_; // The resulting matches after running RunTest. | 37 ACMatches ac_matches_; // The resulting matches after running RunTest. |
| 130 | 38 |
| 131 private: | 39 private: |
| 132 scoped_refptr<HistoryQuickProvider> provider_; | 40 scoped_refptr<HistoryQuickProvider> provider_; |
| 133 }; | 41 }; |
| 134 | 42 |
| 135 void HistoryQuickProviderTest::SetUp() { | 43 void HistoryQuickProviderTest::SetUp() { |
| 136 profile_.reset(new TestingProfile()); | 44 InMemoryURLIndexBaseTest::SetUp(); |
| 137 profile_->CreateHistoryService(true, false); | 45 LoadIndex(); |
| 138 profile_->CreateBookmarkModel(true); | 46 DCHECK(url_index_->index_available()); |
| 139 profile_->BlockUntilBookmarkModelLoaded(); | 47 provider_ = new HistoryQuickProvider(NULL, profile_.get()); |
| 140 history_service_ = | |
| 141 HistoryServiceFactory::GetForProfile(profile_.get(), | |
| 142 Profile::EXPLICIT_ACCESS); | |
| 143 EXPECT_TRUE(history_service_); | |
| 144 provider_ = new HistoryQuickProvider(this, profile_.get()); | |
| 145 FillData(); | |
| 146 } | 48 } |
| 147 | 49 |
| 148 void HistoryQuickProviderTest::TearDown() { | 50 FilePath::StringType HistoryQuickProviderTest::TestDBName() const { |
| 149 provider_ = NULL; | 51 return FILE_PATH_LITERAL("history_quick_provider_test.db.txt"); |
| 150 } | |
| 151 | |
| 152 bool HistoryQuickProviderTest::UpdateURL(const history::URLRow& row) { | |
| 153 history::InMemoryURLIndex* index = provider_->GetIndex(); | |
| 154 DCHECK(index); | |
| 155 history::URLIndexPrivateData* private_data = index->private_data(); | |
| 156 DCHECK(private_data); | |
| 157 return private_data->UpdateURL(row, index->languages_, | |
| 158 index->scheme_whitelist_); | |
| 159 } | |
| 160 | |
| 161 void HistoryQuickProviderTest::OnProviderUpdate(bool updated_matches) { | |
| 162 MessageLoop::current()->Quit(); | |
| 163 } | |
| 164 | |
| 165 void HistoryQuickProviderTest::GetTestData(size_t* data_count, | |
| 166 TestURLInfo** test_data) { | |
| 167 DCHECK(data_count); | |
| 168 DCHECK(test_data); | |
| 169 *data_count = arraysize(quick_test_db); | |
| 170 *test_data = &quick_test_db[0]; | |
| 171 } | |
| 172 | |
| 173 void HistoryQuickProviderTest::FillData() { | |
| 174 history::URLDatabase* db = history_service_->InMemoryDatabase(); | |
| 175 ASSERT_TRUE(db != NULL); | |
| 176 size_t data_count = 0; | |
| 177 TestURLInfo* test_data = NULL; | |
| 178 GetTestData(&data_count, &test_data); | |
| 179 for (size_t i = 0; i < data_count; ++i) { | |
| 180 const TestURLInfo& cur(test_data[i]); | |
| 181 const GURL current_url(cur.url); | |
| 182 Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now); | |
| 183 | |
| 184 history::URLRow url_info(current_url); | |
| 185 url_info.set_id(i + 5000); | |
| 186 url_info.set_title(UTF8ToUTF16(cur.title)); | |
| 187 url_info.set_visit_count(cur.visit_count); | |
| 188 url_info.set_typed_count(cur.typed_count); | |
| 189 url_info.set_last_visit(visit_time); | |
| 190 url_info.set_hidden(false); | |
| 191 UpdateURL(url_info); | |
| 192 } | |
| 193 } | 52 } |
| 194 | 53 |
| 195 HistoryQuickProviderTest::SetShouldContain::SetShouldContain( | 54 HistoryQuickProviderTest::SetShouldContain::SetShouldContain( |
| 196 const ACMatches& matched_urls) { | 55 const ACMatches& matched_urls) { |
| 197 for (ACMatches::const_iterator iter = matched_urls.begin(); | 56 for (ACMatches::const_iterator iter = matched_urls.begin(); |
| 198 iter != matched_urls.end(); ++iter) | 57 iter != matched_urls.end(); ++iter) |
| 199 matches_.insert(iter->destination_url.spec()); | 58 matches_.insert(iter->destination_url.spec()); |
| 200 } | 59 } |
| 201 | 60 |
| 202 void HistoryQuickProviderTest::SetShouldContain::operator()( | 61 void HistoryQuickProviderTest::SetShouldContain::operator()( |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 spans_b[0].style); | 284 spans_b[0].style); |
| 426 EXPECT_EQ(2U, spans_b[1].offset); | 285 EXPECT_EQ(2U, spans_b[1].offset); |
| 427 EXPECT_EQ(ACMatchClassification::URL, spans_b[1].style); | 286 EXPECT_EQ(ACMatchClassification::URL, spans_b[1].style); |
| 428 EXPECT_EQ(3U, spans_b[2].offset); | 287 EXPECT_EQ(3U, spans_b[2].offset); |
| 429 EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL, | 288 EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL, |
| 430 spans_b[2].style); | 289 spans_b[2].style); |
| 431 } | 290 } |
| 432 | 291 |
| 433 // HQPOrderingTest ------------------------------------------------------------- | 292 // HQPOrderingTest ------------------------------------------------------------- |
| 434 | 293 |
| 435 TestURLInfo ordering_test_db[] = { | 294 class HQPOrderingTest : public HistoryQuickProviderTest { |
| 436 {"http://www.teamliquid.net/tlpd/korean/games/21648_bisu_vs_iris", "", 6, 3, | 295 protected: |
| 437 256}, | 296 virtual FilePath::StringType TestDBName() const OVERRIDE; |
| 438 {"http://www.amazon.com/", "amazon.com: online shopping for electronics, " | |
| 439 "apparel, computers, books, dvds & more", 20, 20, 10}, | |
| 440 {"http://www.teamliquid.net/forum/viewmessage.php?topic_id=52045&" | |
| 441 "currentpage=83", "google images", 6, 6, 0}, | |
| 442 {"http://www.tempurpedic.com/", "tempur-pedic", 7, 7, 0}, | |
| 443 {"http://www.teamfortress.com/", "", 5, 5, 6}, | |
| 444 {"http://www.rottentomatoes.com/", "", 3, 3, 7}, | |
| 445 {"http://music.google.com/music/listen?u=0#start_pl", "", 3, 3, 9}, | |
| 446 {"https://www.emigrantdirect.com/", "high interest savings account, high " | |
| 447 "yield savings - emigrantdirect", 5, 5, 3}, | |
| 448 {"http://store.steampowered.com/", "", 6, 6, 1}, | |
| 449 {"http://techmeme.com/", "techmeme", 111, 110, 4}, | |
| 450 {"http://www.teamliquid.net/tlpd", "team liquid progaming database", 15, 15, | |
| 451 2}, | |
| 452 {"http://store.steampowered.com/", "the steam summer camp sale", 6, 6, 1}, | |
| 453 {"http://www.teamliquid.net/tlpd/korean/players", "tlpd - bw korean - player " | |
| 454 "index", 100, 45, 219}, | |
| 455 {"http://slashdot.org/", "slashdot: news for nerds, stuff that matters", 3, 3, | |
| 456 6}, | |
| 457 {"http://translate.google.com/", "google translate", 3, 3, 0}, | |
| 458 {"http://arstechnica.com/", "ars technica", 3, 3, 3}, | |
| 459 {"http://www.rottentomatoes.com/", "movies | movie trailers | reviews - " | |
| 460 "rotten tomatoes", 3, 3, 7}, | |
| 461 {"http://www.teamliquid.net/", "team liquid - starcraft 2 and brood war pro " | |
| 462 "gaming news", 26, 25, 3}, | |
| 463 {"http://metaleater.com/", "metaleater", 4, 3, 8}, | |
| 464 {"http://half.com/", "half.com: textbooks , books , music , movies , games , " | |
| 465 "video games", 4, 4, 6}, | |
| 466 {"http://teamliquid.net/", "team liquid - starcraft 2 and brood war pro " | |
| 467 "gaming news", 8, 5, 9}, | |
| 468 }; | 297 }; |
| 469 | 298 |
| 470 class HQPOrderingTest : public HistoryQuickProviderTest { | 299 FilePath::StringType HQPOrderingTest::TestDBName() const { |
| 471 protected: | 300 return FILE_PATH_LITERAL("history_quick_provider_ordering_test.db.txt"); |
| 472 virtual void GetTestData(size_t* data_count, | |
| 473 TestURLInfo** test_data) OVERRIDE; | |
| 474 }; | |
| 475 | |
| 476 void HQPOrderingTest::GetTestData(size_t* data_count, TestURLInfo** test_data) { | |
| 477 DCHECK(data_count); | |
| 478 DCHECK(test_data); | |
| 479 *data_count = arraysize(ordering_test_db); | |
| 480 *test_data = &ordering_test_db[0]; | |
| 481 } | 301 } |
| 482 | 302 |
| 483 TEST_F(HQPOrderingTest, TEMatch) { | 303 TEST_F(HQPOrderingTest, TEMatch) { |
| 484 std::vector<std::string> expected_urls; | 304 std::vector<std::string> expected_urls; |
| 485 expected_urls.push_back("http://techmeme.com/"); | 305 expected_urls.push_back("http://techmeme.com/"); |
| 486 expected_urls.push_back("http://www.teamliquid.net/"); | 306 expected_urls.push_back("http://www.teamliquid.net/"); |
| 487 expected_urls.push_back("http://www.teamliquid.net/tlpd"); | 307 expected_urls.push_back("http://www.teamliquid.net/tlpd"); |
| 488 RunTest(ASCIIToUTF16("te"), expected_urls, true, | 308 RunTest(ASCIIToUTF16("te"), expected_urls, true, |
| 489 ASCIIToUTF16("techmeme.com")); | 309 ASCIIToUTF16("techmeme.com")); |
| 490 } | 310 } |
| 491 | 311 |
| 492 TEST_F(HQPOrderingTest, TEAMatch) { | 312 TEST_F(HQPOrderingTest, TEAMatch) { |
| 493 std::vector<std::string> expected_urls; | 313 std::vector<std::string> expected_urls; |
| 494 expected_urls.push_back("http://www.teamliquid.net/"); | 314 expected_urls.push_back("http://www.teamliquid.net/"); |
| 495 expected_urls.push_back("http://www.teamliquid.net/tlpd"); | 315 expected_urls.push_back("http://www.teamliquid.net/tlpd"); |
| 496 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); | 316 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); |
| 497 RunTest(ASCIIToUTF16("tea"), expected_urls, true, | 317 RunTest(ASCIIToUTF16("tea"), expected_urls, true, |
| 498 ASCIIToUTF16("www.teamliquid.net")); | 318 ASCIIToUTF16("www.teamliquid.net")); |
| 499 } | 319 } |
| 500 | 320 |
| OLD | NEW |