| 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" | 5 #include "chrome/browser/autocomplete/history_quick_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "chrome/browser/autocomplete/autocomplete_match.h" | 19 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 20 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 20 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| 21 #include "chrome/browser/autocomplete/autocomplete_result.h" | 21 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 22 #include "chrome/browser/autocomplete/history_url_provider.h" | 22 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 23 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 23 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 24 #include "chrome/browser/chrome_notification_types.h" |
| 24 #include "chrome/browser/history/history_backend.h" | 25 #include "chrome/browser/history/history_backend.h" |
| 25 #include "chrome/browser/history/history_database.h" | 26 #include "chrome/browser/history/history_database.h" |
| 26 #include "chrome/browser/history/history_service.h" | 27 #include "chrome/browser/history/history_service.h" |
| 27 #include "chrome/browser/history/history_service_factory.h" | 28 #include "chrome/browser/history/history_service_factory.h" |
| 28 #include "chrome/browser/history/in_memory_url_index.h" | 29 #include "chrome/browser/history/in_memory_url_index.h" |
| 29 #include "chrome/browser/history/url_database.h" | 30 #include "chrome/browser/history/url_database.h" |
| 30 #include "chrome/browser/history/url_index_private_data.h" | 31 #include "chrome/browser/history/url_index_private_data.h" |
| 31 #include "chrome/browser/search_engines/template_url.h" | 32 #include "chrome/browser/search_engines/template_url.h" |
| 32 #include "chrome/browser/search_engines/template_url_service.h" | 33 #include "chrome/browser/search_engines/template_url_service.h" |
| 33 #include "chrome/browser/search_engines/template_url_service_factory.h" | 34 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 34 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
| 35 #include "chrome/test/base/testing_browser_process.h" | 36 #include "chrome/test/base/testing_browser_process.h" |
| 36 #include "chrome/test/base/testing_profile.h" | 37 #include "chrome/test/base/testing_profile.h" |
| 37 #include "components/bookmarks/test/bookmark_test_helpers.h" | 38 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 39 #include "content/public/browser/notification_service.h" |
| 38 #include "content/public/test/test_browser_thread.h" | 40 #include "content/public/test/test_browser_thread.h" |
| 41 #include "content/public/test/test_utils.h" |
| 39 #include "sql/transaction.h" | 42 #include "sql/transaction.h" |
| 40 #include "testing/gtest/include/gtest/gtest.h" | 43 #include "testing/gtest/include/gtest/gtest.h" |
| 41 | 44 |
| 42 using base::ASCIIToUTF16; | 45 using base::ASCIIToUTF16; |
| 43 using base::Time; | 46 using base::Time; |
| 44 using base::TimeDelta; | 47 using base::TimeDelta; |
| 45 | 48 |
| 46 using content::BrowserThread; | 49 using content::BrowserThread; |
| 47 | 50 |
| 48 struct TestURLInfo { | 51 struct TestURLInfo { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 // Runs an autocomplete query on |text| and checks to see that the returned | 142 // Runs an autocomplete query on |text| and checks to see that the returned |
| 140 // results' destination URLs match those provided. |expected_urls| does not | 143 // results' destination URLs match those provided. |expected_urls| does not |
| 141 // need to be in sorted order. | 144 // need to be in sorted order. |
| 142 void RunTest(const base::string16 text, | 145 void RunTest(const base::string16 text, |
| 143 bool prevent_inline_autocomplete, | 146 bool prevent_inline_autocomplete, |
| 144 std::vector<std::string> expected_urls, | 147 std::vector<std::string> expected_urls, |
| 145 bool can_inline_top_result, | 148 bool can_inline_top_result, |
| 146 base::string16 expected_fill_into_edit, | 149 base::string16 expected_fill_into_edit, |
| 147 base::string16 autocompletion); | 150 base::string16 autocompletion); |
| 148 | 151 |
| 152 history::HistoryBackend* history_backend() { |
| 153 return history_service_->history_backend_; |
| 154 } |
| 155 |
| 149 base::MessageLoopForUI message_loop_; | 156 base::MessageLoopForUI message_loop_; |
| 150 content::TestBrowserThread ui_thread_; | 157 content::TestBrowserThread ui_thread_; |
| 151 content::TestBrowserThread file_thread_; | 158 content::TestBrowserThread file_thread_; |
| 152 | 159 |
| 153 scoped_ptr<TestingProfile> profile_; | 160 scoped_ptr<TestingProfile> profile_; |
| 154 HistoryService* history_service_; | 161 HistoryService* history_service_; |
| 155 | 162 |
| 156 ACMatches ac_matches_; // The resulting matches after running RunTest. | 163 ACMatches ac_matches_; // The resulting matches after running RunTest. |
| 157 | 164 |
| 158 scoped_refptr<HistoryQuickProvider> provider_; | 165 scoped_refptr<HistoryQuickProvider> provider_; |
| 159 }; | 166 }; |
| 160 | 167 |
| 161 void HistoryQuickProviderTest::SetUp() { | 168 void HistoryQuickProviderTest::SetUp() { |
| 162 profile_.reset(new TestingProfile()); | 169 profile_.reset(new TestingProfile()); |
| 163 ASSERT_TRUE(profile_->CreateHistoryService(true, false)); | 170 ASSERT_TRUE(profile_->CreateHistoryService(true, false)); |
| 164 profile_->CreateBookmarkModel(true); | 171 profile_->CreateBookmarkModel(true); |
| 165 test::WaitForBookmarkModelToLoad( | 172 test::WaitForBookmarkModelToLoad( |
| 166 BookmarkModelFactory::GetForProfile(profile_.get())); | 173 BookmarkModelFactory::GetForProfile(profile_.get())); |
| 167 profile_->BlockUntilHistoryIndexIsRefreshed(); | 174 profile_->BlockUntilHistoryIndexIsRefreshed(); |
| 168 history_service_ = | 175 history_service_ = |
| 169 HistoryServiceFactory::GetForProfile(profile_.get(), | 176 HistoryServiceFactory::GetForProfile(profile_.get(), |
| 170 Profile::EXPLICIT_ACCESS); | 177 Profile::EXPLICIT_ACCESS); |
| 171 EXPECT_TRUE(history_service_); | 178 EXPECT_TRUE(history_service_); |
| 172 provider_ = new HistoryQuickProvider(this, profile_.get()); | 179 provider_ = new HistoryQuickProvider(this, profile_.get()); |
| 173 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 180 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 174 profile_.get(), &HistoryQuickProviderTest::CreateTemplateURLService); | 181 profile_.get(), &HistoryQuickProviderTest::CreateTemplateURLService); |
| 175 FillData(); | 182 FillData(); |
| 176 provider_->GetIndex()->RebuildFromHistory( | 183 provider_->GetIndex()->RebuildFromHistory(history_backend()->db()); |
| 177 history_service_->history_backend_->db()); | |
| 178 } | 184 } |
| 179 | 185 |
| 180 void HistoryQuickProviderTest::TearDown() { | 186 void HistoryQuickProviderTest::TearDown() { |
| 181 provider_ = NULL; | 187 provider_ = NULL; |
| 182 } | 188 } |
| 183 | 189 |
| 184 void HistoryQuickProviderTest::GetTestData(size_t* data_count, | 190 void HistoryQuickProviderTest::GetTestData(size_t* data_count, |
| 185 TestURLInfo** test_data) { | 191 TestURLInfo** test_data) { |
| 186 DCHECK(data_count); | 192 DCHECK(data_count); |
| 187 DCHECK(test_data); | 193 DCHECK(test_data); |
| 188 *data_count = arraysize(quick_test_db); | 194 *data_count = arraysize(quick_test_db); |
| 189 *test_data = &quick_test_db[0]; | 195 *test_data = &quick_test_db[0]; |
| 190 } | 196 } |
| 191 | 197 |
| 192 void HistoryQuickProviderTest::FillData() { | 198 void HistoryQuickProviderTest::FillData() { |
| 193 sql::Connection& db(history_service_->history_backend_->db()->GetDB()); | 199 sql::Connection& db(history_backend()->db()->GetDB()); |
| 194 ASSERT_TRUE(db.is_open()); | 200 ASSERT_TRUE(db.is_open()); |
| 195 | 201 |
| 196 size_t data_count = 0; | 202 size_t data_count = 0; |
| 197 TestURLInfo* test_data = NULL; | 203 TestURLInfo* test_data = NULL; |
| 198 GetTestData(&data_count, &test_data); | 204 GetTestData(&data_count, &test_data); |
| 199 size_t visit_id = 1; | 205 size_t visit_id = 1; |
| 200 for (size_t i = 0; i < data_count; ++i) { | 206 for (size_t i = 0; i < data_count; ++i) { |
| 201 const TestURLInfo& cur(test_data[i]); | 207 const TestURLInfo& cur(test_data[i]); |
| 202 Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now); | 208 Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now); |
| 203 sql::Transaction transaction(&db); | 209 sql::Transaction transaction(&db); |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL, | 506 EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL, |
| 501 spans_b[0].style); | 507 spans_b[0].style); |
| 502 EXPECT_EQ(2U, spans_b[1].offset); | 508 EXPECT_EQ(2U, spans_b[1].offset); |
| 503 EXPECT_EQ(ACMatchClassification::URL, spans_b[1].style); | 509 EXPECT_EQ(ACMatchClassification::URL, spans_b[1].style); |
| 504 EXPECT_EQ(3U, spans_b[2].offset); | 510 EXPECT_EQ(3U, spans_b[2].offset); |
| 505 EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL, | 511 EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL, |
| 506 spans_b[2].style); | 512 spans_b[2].style); |
| 507 } | 513 } |
| 508 | 514 |
| 509 TEST_F(HistoryQuickProviderTest, DeleteMatch) { | 515 TEST_F(HistoryQuickProviderTest, DeleteMatch) { |
| 516 GURL test_url("http://slashdot.org/favorite_page.html"); |
| 510 std::vector<std::string> expected_urls; | 517 std::vector<std::string> expected_urls; |
| 511 expected_urls.push_back("http://slashdot.org/favorite_page.html"); | 518 expected_urls.push_back(test_url.spec()); |
| 512 // Fill up ac_matches_; we don't really care about the test yet. | 519 // Fill up ac_matches_; we don't really care about the test yet. |
| 513 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, | 520 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, |
| 514 ASCIIToUTF16("slashdot.org/favorite_page.html"), | 521 ASCIIToUTF16("slashdot.org/favorite_page.html"), |
| 515 ASCIIToUTF16(".org/favorite_page.html")); | 522 ASCIIToUTF16(".org/favorite_page.html")); |
| 516 EXPECT_EQ(1U, ac_matches_.size()); | 523 EXPECT_EQ(1U, ac_matches_.size()); |
| 524 EXPECT_TRUE(history_backend()->GetURL(test_url, NULL)); |
| 517 provider_->DeleteMatch(ac_matches_[0]); | 525 provider_->DeleteMatch(ac_matches_[0]); |
| 518 // Verify it's no longer an indexed visit. | 526 // Verify it's no longer an indexed visit. |
| 519 expected_urls.clear(); | 527 expected_urls.clear(); |
| 520 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, | 528 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, |
| 521 ASCIIToUTF16("NONE EXPECTED"), base::string16()); | 529 ASCIIToUTF16("NONE EXPECTED"), base::string16()); |
| 530 // Verify that the underlying URL is gone from the history database -- in a |
| 531 // consistent database, this implies that all visits must be gone as well. |
| 532 // Also check that a notification is fired (this is also used for waiting). |
| 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)); |
| 522 } | 538 } |
| 523 | 539 |
| 524 TEST_F(HistoryQuickProviderTest, PreventBeatingURLWhatYouTypedMatch) { | 540 TEST_F(HistoryQuickProviderTest, PreventBeatingURLWhatYouTypedMatch) { |
| 525 std::vector<std::string> expected_urls; | 541 std::vector<std::string> expected_urls; |
| 526 | 542 |
| 527 expected_urls.clear(); | 543 expected_urls.clear(); |
| 528 expected_urls.push_back("http://popularsitewithroot.com/"); | 544 expected_urls.push_back("http://popularsitewithroot.com/"); |
| 529 // If the user enters a hostname (no path) that he/she has visited | 545 // If the user enters a hostname (no path) that he/she has visited |
| 530 // before, we should make sure that all HistoryQuickProvider results | 546 // before, we should make sure that all HistoryQuickProvider results |
| 531 // have scores less than what HistoryURLProvider will assign the | 547 // have scores less than what HistoryURLProvider will assign the |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 | 724 |
| 709 TEST_F(HQPOrderingTest, TEAMatch) { | 725 TEST_F(HQPOrderingTest, TEAMatch) { |
| 710 std::vector<std::string> expected_urls; | 726 std::vector<std::string> expected_urls; |
| 711 expected_urls.push_back("http://www.teamliquid.net/"); | 727 expected_urls.push_back("http://www.teamliquid.net/"); |
| 712 expected_urls.push_back("http://www.teamliquid.net/tlpd"); | 728 expected_urls.push_back("http://www.teamliquid.net/tlpd"); |
| 713 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); | 729 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); |
| 714 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true, | 730 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true, |
| 715 ASCIIToUTF16("www.teamliquid.net"), | 731 ASCIIToUTF16("www.teamliquid.net"), |
| 716 ASCIIToUTF16("mliquid.net")); | 732 ASCIIToUTF16("mliquid.net")); |
| 717 } | 733 } |
| OLD | NEW |