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 "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/memory/scoped_ptr.h" |
| 14 #include "base/message_loop.h" |
5 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
6 #include "chrome/browser/autocomplete/autocomplete_result.h" | 17 #include "chrome/browser/autocomplete/autocomplete_result.h" |
7 #include "chrome/browser/autocomplete/history_quick_provider.h" | 18 #include "chrome/browser/history/history.h" |
8 #include "chrome/browser/history/in_memory_url_cache_database.h" | 19 #include "chrome/browser/history/history_service_factory.h" |
9 #include "chrome/browser/history/in_memory_url_index_base_unittest.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" |
10 #include "chrome/test/base/testing_profile.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" |
11 | 29 |
12 class HistoryQuickProviderTest : public history::InMemoryURLIndexBaseTest { | 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 |
13 protected: | 89 protected: |
14 virtual FilePath::StringType TestDBName() const OVERRIDE; | |
15 | |
16 class SetShouldContain : public std::unary_function<const std::string&, | 90 class SetShouldContain : public std::unary_function<const std::string&, |
17 std::set<std::string> > { | 91 std::set<std::string> > { |
18 public: | 92 public: |
19 explicit SetShouldContain(const ACMatches& matched_urls); | 93 explicit SetShouldContain(const ACMatches& matched_urls); |
| 94 |
20 void operator()(const std::string& expected); | 95 void operator()(const std::string& expected); |
| 96 |
21 std::set<std::string> LeftOvers() const { return matches_; } | 97 std::set<std::string> LeftOvers() const { return matches_; } |
22 | 98 |
23 private: | 99 private: |
24 std::set<std::string> matches_; | 100 std::set<std::string> matches_; |
25 }; | 101 }; |
26 | 102 |
27 virtual void SetUp() OVERRIDE; | 103 void SetUp(); |
| 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(); |
28 | 110 |
29 // Runs an autocomplete query on |text| and checks to see that the returned | 111 // Runs an autocomplete query on |text| and checks to see that the returned |
30 // results' destination URLs match those provided. |expected_urls| does not | 112 // results' destination URLs match those provided. |expected_urls| does not |
31 // need to be in sorted order. | 113 // need to be in sorted order. |
32 void RunTest(const string16 text, | 114 void RunTest(const string16 text, |
33 std::vector<std::string> expected_urls, | 115 std::vector<std::string> expected_urls, |
34 bool can_inline_top_result, | 116 bool can_inline_top_result, |
35 string16 expected_fill_into_edit); | 117 string16 expected_fill_into_edit); |
36 | 118 |
| 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 |
37 ACMatches ac_matches_; // The resulting matches after running RunTest. | 129 ACMatches ac_matches_; // The resulting matches after running RunTest. |
38 | 130 |
39 private: | 131 private: |
40 scoped_refptr<HistoryQuickProvider> provider_; | 132 scoped_refptr<HistoryQuickProvider> provider_; |
41 }; | 133 }; |
42 | 134 |
43 void HistoryQuickProviderTest::SetUp() { | 135 void HistoryQuickProviderTest::SetUp() { |
44 InMemoryURLIndexBaseTest::SetUp(); | 136 profile_.reset(new TestingProfile()); |
45 LoadIndex(); | 137 profile_->CreateHistoryService(true, false); |
46 DCHECK(url_index_->index_available()); | 138 profile_->CreateBookmarkModel(true); |
47 provider_ = new HistoryQuickProvider(NULL, profile_.get()); | 139 profile_->BlockUntilBookmarkModelLoaded(); |
| 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(); |
48 } | 146 } |
49 | 147 |
50 FilePath::StringType HistoryQuickProviderTest::TestDBName() const { | 148 void HistoryQuickProviderTest::TearDown() { |
51 return FILE_PATH_LITERAL("history_quick_provider_test.db.txt"); | 149 provider_ = NULL; |
| 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 } |
52 } | 193 } |
53 | 194 |
54 HistoryQuickProviderTest::SetShouldContain::SetShouldContain( | 195 HistoryQuickProviderTest::SetShouldContain::SetShouldContain( |
55 const ACMatches& matched_urls) { | 196 const ACMatches& matched_urls) { |
56 for (ACMatches::const_iterator iter = matched_urls.begin(); | 197 for (ACMatches::const_iterator iter = matched_urls.begin(); |
57 iter != matched_urls.end(); ++iter) | 198 iter != matched_urls.end(); ++iter) |
58 matches_.insert(iter->destination_url.spec()); | 199 matches_.insert(iter->destination_url.spec()); |
59 } | 200 } |
60 | 201 |
61 void HistoryQuickProviderTest::SetShouldContain::operator()( | 202 void HistoryQuickProviderTest::SetShouldContain::operator()( |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 spans_b[0].style); | 425 spans_b[0].style); |
285 EXPECT_EQ(2U, spans_b[1].offset); | 426 EXPECT_EQ(2U, spans_b[1].offset); |
286 EXPECT_EQ(ACMatchClassification::URL, spans_b[1].style); | 427 EXPECT_EQ(ACMatchClassification::URL, spans_b[1].style); |
287 EXPECT_EQ(3U, spans_b[2].offset); | 428 EXPECT_EQ(3U, spans_b[2].offset); |
288 EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL, | 429 EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL, |
289 spans_b[2].style); | 430 spans_b[2].style); |
290 } | 431 } |
291 | 432 |
292 // HQPOrderingTest ------------------------------------------------------------- | 433 // HQPOrderingTest ------------------------------------------------------------- |
293 | 434 |
| 435 TestURLInfo ordering_test_db[] = { |
| 436 {"http://www.teamliquid.net/tlpd/korean/games/21648_bisu_vs_iris", "", 6, 3, |
| 437 256}, |
| 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 }; |
| 469 |
294 class HQPOrderingTest : public HistoryQuickProviderTest { | 470 class HQPOrderingTest : public HistoryQuickProviderTest { |
295 protected: | 471 protected: |
296 virtual FilePath::StringType TestDBName() const OVERRIDE; | 472 virtual void GetTestData(size_t* data_count, |
| 473 TestURLInfo** test_data) OVERRIDE; |
297 }; | 474 }; |
298 | 475 |
299 FilePath::StringType HQPOrderingTest::TestDBName() const { | 476 void HQPOrderingTest::GetTestData(size_t* data_count, TestURLInfo** test_data) { |
300 return FILE_PATH_LITERAL("history_quick_provider_ordering_test.db.txt"); | 477 DCHECK(data_count); |
| 478 DCHECK(test_data); |
| 479 *data_count = arraysize(ordering_test_db); |
| 480 *test_data = &ordering_test_db[0]; |
301 } | 481 } |
302 | 482 |
303 TEST_F(HQPOrderingTest, TEMatch) { | 483 TEST_F(HQPOrderingTest, TEMatch) { |
304 std::vector<std::string> expected_urls; | 484 std::vector<std::string> expected_urls; |
305 expected_urls.push_back("http://techmeme.com/"); | 485 expected_urls.push_back("http://techmeme.com/"); |
306 expected_urls.push_back("http://www.teamliquid.net/"); | 486 expected_urls.push_back("http://www.teamliquid.net/"); |
307 expected_urls.push_back("http://www.teamliquid.net/tlpd"); | 487 expected_urls.push_back("http://www.teamliquid.net/tlpd"); |
308 RunTest(ASCIIToUTF16("te"), expected_urls, true, | 488 RunTest(ASCIIToUTF16("te"), expected_urls, true, |
309 ASCIIToUTF16("techmeme.com")); | 489 ASCIIToUTF16("techmeme.com")); |
310 } | 490 } |
311 | 491 |
312 TEST_F(HQPOrderingTest, TEAMatch) { | 492 TEST_F(HQPOrderingTest, TEAMatch) { |
313 std::vector<std::string> expected_urls; | 493 std::vector<std::string> expected_urls; |
314 expected_urls.push_back("http://www.teamliquid.net/"); | 494 expected_urls.push_back("http://www.teamliquid.net/"); |
315 expected_urls.push_back("http://www.teamliquid.net/tlpd"); | 495 expected_urls.push_back("http://www.teamliquid.net/tlpd"); |
316 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); | 496 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); |
317 RunTest(ASCIIToUTF16("tea"), expected_urls, true, | 497 RunTest(ASCIIToUTF16("tea"), expected_urls, true, |
318 ASCIIToUTF16("www.teamliquid.net")); | 498 ASCIIToUTF16("www.teamliquid.net")); |
319 } | 499 } |
320 | 500 |
OLD | NEW |