| 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/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/autocomplete/autocomplete.h" | 16 #include "chrome/browser/autocomplete/autocomplete.h" |
| 17 #include "chrome/browser/autocomplete/autocomplete_match.h" | 17 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 18 #include "chrome/browser/history/history.h" | 18 #include "chrome/browser/history/history.h" |
| 19 #include "chrome/browser/history/in_memory_url_index.h" | 19 #include "chrome/browser/history/in_memory_url_index.h" |
| 20 #include "chrome/browser/history/url_database.h" | 20 #include "chrome/browser/history/url_database.h" |
| 21 #include "chrome/browser/history/url_index_private_data.h" |
| 21 #include "chrome/browser/prefs/pref_service.h" | 22 #include "chrome/browser/prefs/pref_service.h" |
| 22 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 23 #include "chrome/test/base/testing_browser_process.h" | 24 #include "chrome/test/base/testing_browser_process.h" |
| 24 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
| 25 #include "content/test/test_browser_thread.h" | 26 #include "content/test/test_browser_thread.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 28 |
| 28 using base::Time; | 29 using base::Time; |
| 29 using base::TimeDelta; | 30 using base::TimeDelta; |
| 30 | 31 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 93 |
| 93 void operator()(const std::string& expected); | 94 void operator()(const std::string& expected); |
| 94 | 95 |
| 95 std::set<std::string> LeftOvers() const { return matches_; } | 96 std::set<std::string> LeftOvers() const { return matches_; } |
| 96 | 97 |
| 97 private: | 98 private: |
| 98 std::set<std::string> matches_; | 99 std::set<std::string> matches_; |
| 99 }; | 100 }; |
| 100 | 101 |
| 101 void SetUp(); | 102 void SetUp(); |
| 102 | 103 void TearDown(); |
| 103 void TearDown() { | |
| 104 provider_ = NULL; | |
| 105 } | |
| 106 | 104 |
| 107 virtual void GetTestData(size_t* data_count, TestURLInfo** test_data); | 105 virtual void GetTestData(size_t* data_count, TestURLInfo** test_data); |
| 108 | 106 |
| 109 // Fills test data into the history system. | 107 // Fills test data into the history system. |
| 110 void FillData(); | 108 void FillData(); |
| 111 | 109 |
| 112 // Runs an autocomplete query on |text| and checks to see that the returned | 110 // Runs an autocomplete query on |text| and checks to see that the returned |
| 113 // results' destination URLs match those provided. |expected_urls| does not | 111 // results' destination URLs match those provided. |expected_urls| does not |
| 114 // need to be in sorted order. | 112 // need to be in sorted order. |
| 115 void RunTest(const string16 text, | 113 void RunTest(const string16 text, |
| 116 std::vector<std::string> expected_urls, | 114 std::vector<std::string> expected_urls, |
| 117 bool can_inline_top_result, | 115 bool can_inline_top_result, |
| 118 string16 expected_fill_into_edit); | 116 string16 expected_fill_into_edit); |
| 119 | 117 |
| 118 // Pass-through functions to simplify our friendship with URLIndexPrivateData. |
| 119 bool UpdateURL(const history::URLRow& row); |
| 120 |
| 120 MessageLoopForUI message_loop_; | 121 MessageLoopForUI message_loop_; |
| 121 content::TestBrowserThread ui_thread_; | 122 content::TestBrowserThread ui_thread_; |
| 122 content::TestBrowserThread file_thread_; | 123 content::TestBrowserThread file_thread_; |
| 123 | 124 |
| 124 scoped_ptr<TestingProfile> profile_; | 125 scoped_ptr<TestingProfile> profile_; |
| 125 HistoryService* history_service_; | 126 HistoryService* history_service_; |
| 126 | 127 |
| 127 ACMatches ac_matches_; // The resulting matches after running RunTest. | 128 ACMatches ac_matches_; // The resulting matches after running RunTest. |
| 128 | 129 |
| 129 private: | 130 private: |
| 130 scoped_refptr<HistoryQuickProvider> provider_; | 131 scoped_refptr<HistoryQuickProvider> provider_; |
| 131 }; | 132 }; |
| 132 | 133 |
| 133 void HistoryQuickProviderTest::SetUp() { | 134 void HistoryQuickProviderTest::SetUp() { |
| 134 profile_.reset(new TestingProfile()); | 135 profile_.reset(new TestingProfile()); |
| 135 profile_->CreateHistoryService(true, false); | 136 profile_->CreateHistoryService(true, false); |
| 136 profile_->CreateBookmarkModel(true); | 137 profile_->CreateBookmarkModel(true); |
| 137 profile_->BlockUntilBookmarkModelLoaded(); | 138 profile_->BlockUntilBookmarkModelLoaded(); |
| 138 history_service_ = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 139 history_service_ = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 139 EXPECT_TRUE(history_service_); | 140 EXPECT_TRUE(history_service_); |
| 140 provider_ = new HistoryQuickProvider(this, profile_.get()); | 141 provider_ = new HistoryQuickProvider(this, profile_.get()); |
| 141 FillData(); | 142 FillData(); |
| 142 } | 143 } |
| 143 | 144 |
| 145 void HistoryQuickProviderTest::TearDown() { |
| 146 provider_ = NULL; |
| 147 } |
| 148 |
| 149 bool HistoryQuickProviderTest::UpdateURL(const history::URLRow& row) { |
| 150 history::InMemoryURLIndex* index = provider_->GetIndex(); |
| 151 DCHECK(index); |
| 152 history::URLIndexPrivateData* private_data = index->private_data(); |
| 153 DCHECK(private_data); |
| 154 return private_data->UpdateURL(row); |
| 155 } |
| 156 |
| 144 void HistoryQuickProviderTest::OnProviderUpdate(bool updated_matches) { | 157 void HistoryQuickProviderTest::OnProviderUpdate(bool updated_matches) { |
| 145 MessageLoop::current()->Quit(); | 158 MessageLoop::current()->Quit(); |
| 146 } | 159 } |
| 147 | 160 |
| 148 void HistoryQuickProviderTest::GetTestData(size_t* data_count, | 161 void HistoryQuickProviderTest::GetTestData(size_t* data_count, |
| 149 TestURLInfo** test_data) { | 162 TestURLInfo** test_data) { |
| 150 DCHECK(data_count); | 163 DCHECK(data_count); |
| 151 DCHECK(test_data); | 164 DCHECK(test_data); |
| 152 *data_count = arraysize(quick_test_db); | 165 *data_count = arraysize(quick_test_db); |
| 153 *test_data = &quick_test_db[0]; | 166 *test_data = &quick_test_db[0]; |
| 154 } | 167 } |
| 155 | 168 |
| 156 void HistoryQuickProviderTest::FillData() { | 169 void HistoryQuickProviderTest::FillData() { |
| 157 history::URLDatabase* db = history_service_->InMemoryDatabase(); | 170 history::URLDatabase* db = history_service_->InMemoryDatabase(); |
| 158 ASSERT_TRUE(db != NULL); | 171 ASSERT_TRUE(db != NULL); |
| 159 | |
| 160 history::InMemoryURLIndex* index = | |
| 161 new history::InMemoryURLIndex(FilePath()); | |
| 162 PrefService* prefs = profile_->GetPrefs(); | |
| 163 std::string languages(prefs->GetString(prefs::kAcceptLanguages)); | |
| 164 index->Init(db, languages); | |
| 165 size_t data_count = 0; | 172 size_t data_count = 0; |
| 166 TestURLInfo* test_data = NULL; | 173 TestURLInfo* test_data = NULL; |
| 167 GetTestData(&data_count, &test_data); | 174 GetTestData(&data_count, &test_data); |
| 168 for (size_t i = 0; i < data_count; ++i) { | 175 for (size_t i = 0; i < data_count; ++i) { |
| 169 const TestURLInfo& cur(test_data[i]); | 176 const TestURLInfo& cur(test_data[i]); |
| 170 const GURL current_url(cur.url); | 177 const GURL current_url(cur.url); |
| 171 Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now); | 178 Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now); |
| 172 | 179 |
| 173 history::URLRow url_info(current_url); | 180 history::URLRow url_info(current_url); |
| 181 url_info.set_id(i + 5000); |
| 174 url_info.set_title(UTF8ToUTF16(cur.title)); | 182 url_info.set_title(UTF8ToUTF16(cur.title)); |
| 175 url_info.set_visit_count(cur.visit_count); | 183 url_info.set_visit_count(cur.visit_count); |
| 176 url_info.set_typed_count(cur.typed_count); | 184 url_info.set_typed_count(cur.typed_count); |
| 177 url_info.set_last_visit(visit_time); | 185 url_info.set_last_visit(visit_time); |
| 178 url_info.set_hidden(false); | 186 url_info.set_hidden(false); |
| 179 index->UpdateURL(i, url_info); | 187 UpdateURL(url_info); |
| 180 } | 188 } |
| 181 | |
| 182 provider_->set_index(index); | |
| 183 } | 189 } |
| 184 | 190 |
| 185 HistoryQuickProviderTest::SetShouldContain::SetShouldContain( | 191 HistoryQuickProviderTest::SetShouldContain::SetShouldContain( |
| 186 const ACMatches& matched_urls) { | 192 const ACMatches& matched_urls) { |
| 187 for (ACMatches::const_iterator iter = matched_urls.begin(); | 193 for (ACMatches::const_iterator iter = matched_urls.begin(); |
| 188 iter != matched_urls.end(); ++iter) | 194 iter != matched_urls.end(); ++iter) |
| 189 matches_.insert(iter->destination_url.spec()); | 195 matches_.insert(iter->destination_url.spec()); |
| 190 } | 196 } |
| 191 | 197 |
| 192 void HistoryQuickProviderTest::SetShouldContain::operator()( | 198 void HistoryQuickProviderTest::SetShouldContain::operator()( |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 | 539 |
| 534 TEST_F(HQPOrderingTest, TEAMatch) { | 540 TEST_F(HQPOrderingTest, TEAMatch) { |
| 535 std::vector<std::string> expected_urls; | 541 std::vector<std::string> expected_urls; |
| 536 expected_urls.push_back("http://www.teamliquid.net/"); | 542 expected_urls.push_back("http://www.teamliquid.net/"); |
| 537 expected_urls.push_back("http://www.teamliquid.net/tlpd"); | 543 expected_urls.push_back("http://www.teamliquid.net/tlpd"); |
| 538 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); | 544 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); |
| 539 RunTest(ASCIIToUTF16("tea"), expected_urls, true, | 545 RunTest(ASCIIToUTF16("tea"), expected_urls, true, |
| 540 ASCIIToUTF16("www.teamliquid.net")); | 546 ASCIIToUTF16("www.teamliquid.net")); |
| 541 } | 547 } |
| 542 | 548 |
| OLD | NEW |