Chromium Code Reviews| 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 "components/omnibox/browser/history_quick_provider.h" | 5 #include "components/omnibox/browser/history_quick_provider.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <functional> | 10 #include <functional> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <set> | 12 #include <set> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/files/scoped_temp_dir.h" | |
| 17 #include "base/format_macros.h" | 16 #include "base/format_macros.h" |
| 18 #include "base/macros.h" | 17 #include "base/macros.h" |
| 19 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 20 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
| 21 #include "base/strings/stringprintf.h" | |
| 22 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/test/sequenced_worker_pool_owner.h" | |
| 24 #include "components/bookmarks/browser/bookmark_model.h" | 21 #include "components/bookmarks/browser/bookmark_model.h" |
| 25 #include "components/bookmarks/test/bookmark_test_helpers.h" | 22 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 26 #include "components/bookmarks/test/test_bookmark_client.h" | 23 #include "components/bookmarks/test/test_bookmark_client.h" |
| 27 #include "components/history/core/browser/history_backend.h" | 24 #include "components/history/core/browser/history_backend.h" |
| 28 #include "components/history/core/browser/history_database.h" | 25 #include "components/history/core/browser/history_database.h" |
| 29 #include "components/history/core/browser/history_service.h" | 26 #include "components/history/core/browser/history_service.h" |
| 30 #include "components/history/core/browser/history_service_observer.h" | 27 #include "components/history/core/browser/history_service_observer.h" |
| 31 #include "components/history/core/browser/url_database.h" | 28 #include "components/history/core/browser/url_database.h" |
| 32 #include "components/history/core/test/history_service_test_util.h" | 29 #include "components/history/core/test/history_service_test_util.h" |
| 33 #include "components/metrics/proto/omnibox_event.pb.h" | |
| 34 #include "components/omnibox/browser/autocomplete_match.h" | 30 #include "components/omnibox/browser/autocomplete_match.h" |
| 35 #include "components/omnibox/browser/autocomplete_result.h" | 31 #include "components/omnibox/browser/autocomplete_result.h" |
| 32 #include "components/omnibox/browser/fake_autocomplete_provider_client.h" | |
| 33 #include "components/omnibox/browser/history_test_util.h" | |
| 36 #include "components/omnibox/browser/history_url_provider.h" | 34 #include "components/omnibox/browser/history_url_provider.h" |
| 37 #include "components/omnibox/browser/in_memory_url_index.h" | |
| 38 #include "components/omnibox/browser/in_memory_url_index_test_util.h" | 35 #include "components/omnibox/browser/in_memory_url_index_test_util.h" |
| 39 #include "components/omnibox/browser/mock_autocomplete_provider_client.h" | |
| 40 #include "components/omnibox/browser/test_scheme_classifier.h" | |
| 41 #include "components/omnibox/browser/url_index_private_data.h" | |
| 42 #include "components/prefs/pref_service.h" | 36 #include "components/prefs/pref_service.h" |
| 43 #include "components/search_engines/search_terms_data.h" | 37 #include "components/search_engines/search_terms_data.h" |
| 44 #include "sql/transaction.h" | |
| 45 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 46 | 39 |
| 47 using base::ASCIIToUTF16; | 40 using base::ASCIIToUTF16; |
| 48 using base::Time; | |
| 49 using base::TimeDelta; | |
| 50 | 41 |
| 51 namespace { | 42 namespace { |
| 52 | 43 |
| 53 struct TestURLInfo { | 44 struct TestURLInfo { |
| 54 std::string url; | 45 std::string url; |
| 55 std::string title; | 46 std::string title; |
| 56 int visit_count; | 47 int visit_count; |
| 57 int typed_count; | 48 int typed_count; |
| 58 int days_from_now; | 49 int days_from_now; |
| 59 } quick_test_db[] = { | 50 } quick_test_db[] = { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 171 | 162 |
| 172 private: | 163 private: |
| 173 ~GetURLTask() override {} | 164 ~GetURLTask() override {} |
| 174 | 165 |
| 175 bool* result_storage_; | 166 bool* result_storage_; |
| 176 const GURL url_; | 167 const GURL url_; |
| 177 | 168 |
| 178 DISALLOW_COPY_AND_ASSIGN(GetURLTask); | 169 DISALLOW_COPY_AND_ASSIGN(GetURLTask); |
| 179 }; | 170 }; |
| 180 | 171 |
| 181 class FakeAutocompleteProviderClient : public MockAutocompleteProviderClient { | |
| 182 public: | |
| 183 FakeAutocompleteProviderClient() : pool_owner_(3, "Background Pool") { | |
| 184 bookmark_model_ = bookmarks::TestBookmarkClient::CreateModel(); | |
| 185 if (history_dir_.CreateUniqueTempDir()) { | |
| 186 history_service_ = | |
| 187 history::CreateHistoryService(history_dir_.GetPath(), true); | |
| 188 } | |
| 189 | |
| 190 in_memory_url_index_.reset(new InMemoryURLIndex( | |
| 191 bookmark_model_.get(), history_service_.get(), nullptr, | |
| 192 pool_owner_.pool().get(), history_dir_.GetPath(), SchemeSet())); | |
| 193 in_memory_url_index_->Init(); | |
| 194 } | |
| 195 | |
| 196 const AutocompleteSchemeClassifier& GetSchemeClassifier() const override { | |
| 197 return scheme_classifier_; | |
| 198 } | |
| 199 | |
| 200 const SearchTermsData& GetSearchTermsData() const override { | |
| 201 return search_terms_data_; | |
| 202 } | |
| 203 | |
| 204 history::HistoryService* GetHistoryService() override { | |
| 205 return history_service_.get(); | |
| 206 } | |
| 207 | |
| 208 bookmarks::BookmarkModel* GetBookmarkModel() override { | |
| 209 return bookmark_model_.get(); | |
| 210 } | |
| 211 | |
| 212 InMemoryURLIndex* GetInMemoryURLIndex() override { | |
| 213 return in_memory_url_index_.get(); | |
| 214 } | |
| 215 | |
| 216 void set_in_memory_url_index(std::unique_ptr<InMemoryURLIndex> index) { | |
| 217 in_memory_url_index_ = std::move(index); | |
| 218 } | |
| 219 | |
| 220 private: | |
| 221 base::SequencedWorkerPoolOwner pool_owner_; | |
| 222 base::ScopedTempDir history_dir_; | |
| 223 std::unique_ptr<bookmarks::BookmarkModel> bookmark_model_; | |
| 224 TestSchemeClassifier scheme_classifier_; | |
| 225 SearchTermsData search_terms_data_; | |
| 226 std::unique_ptr<InMemoryURLIndex> in_memory_url_index_; | |
| 227 std::unique_ptr<history::HistoryService> history_service_; | |
| 228 | |
| 229 DISALLOW_COPY_AND_ASSIGN(FakeAutocompleteProviderClient); | |
| 230 }; | |
| 231 | |
| 232 } // namespace | 172 } // namespace |
| 233 | 173 |
| 234 class HistoryQuickProviderTest : public testing::Test { | 174 class HistoryQuickProviderTest : public testing::Test { |
| 235 public: | 175 public: |
| 236 HistoryQuickProviderTest() {} | 176 HistoryQuickProviderTest() {} |
| 237 | 177 |
| 238 protected: | 178 protected: |
| 239 class SetShouldContain { | 179 class SetShouldContain { |
| 240 public: | 180 public: |
| 241 explicit SetShouldContain(const ACMatches& matched_urls); | 181 explicit SetShouldContain(const ACMatches& matched_urls); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 | 270 |
| 331 void HistoryQuickProviderTest::GetTestData(size_t* data_count, | 271 void HistoryQuickProviderTest::GetTestData(size_t* data_count, |
| 332 TestURLInfo** test_data) { | 272 TestURLInfo** test_data) { |
| 333 DCHECK(data_count); | 273 DCHECK(data_count); |
| 334 DCHECK(test_data); | 274 DCHECK(test_data); |
| 335 *data_count = arraysize(quick_test_db); | 275 *data_count = arraysize(quick_test_db); |
| 336 *test_data = &quick_test_db[0]; | 276 *test_data = &quick_test_db[0]; |
| 337 } | 277 } |
| 338 | 278 |
| 339 void HistoryQuickProviderTest::FillData() { | 279 void HistoryQuickProviderTest::FillData() { |
| 340 sql::Connection& db(history_backend()->db()->GetDB()); | 280 size_t data_count = 0; |
| 341 ASSERT_TRUE(db.is_open()); | 281 TestURLInfo* test_data = nullptr; |
| 282 GetTestData(&data_count, &test_data); | |
| 342 | 283 |
| 343 size_t data_count = 0; | 284 for (auto* it = test_data; it != test_data + data_count; ++it) { |
|
Peter Kasting
2016/10/27 00:59:39
Nit: May be out of scope for this patch, but it mi
dyaroshev
2016/10/27 06:09:21
Done.
| |
| 344 TestURLInfo* test_data = NULL; | 285 const TestURLInfo& info = *it; |
| 345 GetTestData(&data_count, &test_data); | |
| 346 size_t visit_id = 1; | |
| 347 for (size_t i = 0; i < data_count; ++i) { | |
| 348 const TestURLInfo& cur(test_data[i]); | |
| 349 Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now); | |
| 350 sql::Transaction transaction(&db); | |
| 351 | 286 |
| 352 // Add URL. | 287 history::URLRow row{GURL(info.url)}; |
| 353 transaction.Begin(); | 288 CHECK(row.url().is_valid()); |
| 354 std::string sql_cmd_line = base::StringPrintf( | 289 row.set_title(base::UTF8ToUTF16(info.title)); |
| 355 "INSERT INTO \"urls\" VALUES(%" PRIuS ", \'%s\', \'%s\', %d, %d, %" | 290 row.set_visit_count(info.visit_count); |
| 356 PRId64 ", 0, 0)", | 291 row.set_typed_count(info.typed_count); |
| 357 i + 1, cur.url.c_str(), cur.title.c_str(), cur.visit_count, | 292 row.set_last_visit(base::Time::Now() - |
| 358 cur.typed_count, visit_time.ToInternalValue()); | 293 base::TimeDelta::FromDays(info.days_from_now)); |
| 359 sql::Statement sql_stmt(db.GetUniqueStatement(sql_cmd_line.c_str())); | |
| 360 EXPECT_TRUE(sql_stmt.Run()); | |
| 361 transaction.Commit(); | |
| 362 | 294 |
| 363 // Add visits. | 295 AddFakeURLToHistoryDB(history_backend()->db(), row); |
| 364 for (int j = 0; j < cur.visit_count; ++j) { | |
| 365 // Assume earlier visits are at one-day intervals. | |
| 366 visit_time -= TimeDelta::FromDays(1); | |
| 367 transaction.Begin(); | |
| 368 // Mark the most recent |cur.typed_count| visits as typed. | |
| 369 std::string sql_cmd_line = base::StringPrintf( | |
| 370 "INSERT INTO \"visits\" VALUES(%" PRIuS ", %" PRIuS ", %" PRId64 | |
| 371 ", 0, %d, 0, 1)", | |
| 372 visit_id++, i + 1, visit_time.ToInternalValue(), | |
| 373 (j < cur.typed_count) ? ui::PAGE_TRANSITION_TYPED : | |
| 374 ui::PAGE_TRANSITION_LINK); | |
| 375 | |
| 376 sql::Statement sql_stmt(db.GetUniqueStatement(sql_cmd_line.c_str())); | |
| 377 EXPECT_TRUE(sql_stmt.Run()); | |
| 378 transaction.Commit(); | |
| 379 } | |
| 380 } | 296 } |
| 381 } | 297 } |
| 382 | 298 |
| 383 HistoryQuickProviderTest::SetShouldContain::SetShouldContain( | 299 HistoryQuickProviderTest::SetShouldContain::SetShouldContain( |
| 384 const ACMatches& matched_urls) { | 300 const ACMatches& matched_urls) { |
| 385 for (ACMatches::const_iterator iter = matched_urls.begin(); | 301 for (ACMatches::const_iterator iter = matched_urls.begin(); |
| 386 iter != matched_urls.end(); ++iter) | 302 iter != matched_urls.end(); ++iter) |
| 387 matches_.insert(iter->destination_url.spec()); | 303 matches_.insert(iter->destination_url.spec()); |
| 388 } | 304 } |
| 389 | 305 |
| (...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 833 std::string(), GURL(), | 749 std::string(), GURL(), |
| 834 metrics::OmniboxEventProto::INVALID_SPEC, false, | 750 metrics::OmniboxEventProto::INVALID_SPEC, false, |
| 835 false, true, true, true, TestSchemeClassifier()); | 751 false, true, true, true, TestSchemeClassifier()); |
| 836 provider_->Start(input, false); | 752 provider_->Start(input, false); |
| 837 EXPECT_TRUE(provider_->matches().empty()); | 753 EXPECT_TRUE(provider_->matches().empty()); |
| 838 } | 754 } |
| 839 | 755 |
| 840 // HQPOrderingTest ------------------------------------------------------------- | 756 // HQPOrderingTest ------------------------------------------------------------- |
| 841 | 757 |
| 842 TestURLInfo ordering_test_db[] = { | 758 TestURLInfo ordering_test_db[] = { |
| 843 {"http://www.teamliquid.net/tlpd/korean/games/21648_bisu_vs_iris", "", 6, 3, | 759 {"http://www.teamliquid.net/tlpd/korean/games/21648_bisu_vs_iris", "", 6, 3, |
| 844 256}, | 760 256}, |
| 845 {"http://www.amazon.com/", "amazon.com: online shopping for electronics, " | 761 {"http://www.amazon.com/", |
| 846 "apparel, computers, books, dvds & more", 20, 20, 10}, | 762 "amazon.com: online shopping for electronics, " |
| 847 {"http://www.teamliquid.net/forum/viewmessage.php?topic_id=52045&" | 763 "apparel, computers, books, dvds & more", |
| 848 "currentpage=83", "google images", 6, 6, 0}, | 764 20, 20, 10}, |
| 849 {"http://www.tempurpedic.com/", "tempur-pedic", 7, 7, 0}, | 765 {"http://www.teamliquid.net/forum/viewmessage.php?topic_id=52045&" |
| 850 {"http://www.teamfortress.com/", "", 5, 5, 6}, | 766 "currentpage=83", |
| 851 {"http://www.rottentomatoes.com/", "", 3, 3, 7}, | 767 "google images", 6, 6, 0}, |
| 852 {"http://music.google.com/music/listen?u=0#start_pl", "", 3, 3, 9}, | 768 {"http://www.tempurpedic.com/", "tempur-pedic", 7, 7, 0}, |
| 853 {"https://www.emigrantdirect.com/", "high interest savings account, high " | 769 {"http://www.teamfortress.com/", "", 5, 5, 6}, |
| 854 "yield savings - emigrantdirect", 5, 5, 3}, | 770 {"http://www.rottentomatoes.com/", "", 3, 3, 7}, |
| 855 {"http://store.steampowered.com/", "", 6, 6, 1}, | 771 {"http://music.google.com/music/listen?u=0#start_pl", "", 3, 3, 9}, |
| 856 {"http://techmeme.com/", "techmeme", 111, 110, 4}, | 772 {"https://www.emigrantdirect.com/", |
| 857 {"http://www.teamliquid.net/tlpd", "team liquid progaming database", 15, 15, | 773 "high interest savings account, high " |
| 858 2}, | 774 "yield savings - emigrantdirect", |
| 859 {"http://store.steampowered.com/", "the steam summer camp sale", 6, 6, 1}, | 775 5, 5, 3}, |
| 860 {"http://www.teamliquid.net/tlpd/korean/players", "tlpd - bw korean - player " | 776 {"http://store.steampowered.com/", "", 6, 6, 1}, |
| 861 "index", 25, 7, 219}, | 777 {"http://techmeme.com/", "techmeme", 111, 110, 4}, |
| 862 {"http://slashdot.org/", "slashdot: news for nerds, stuff that matters", 3, 3, | 778 {"http://www.teamliquid.net/tlpd", "team liquid progaming database", 15, 15, |
| 863 6}, | 779 2}, |
| 864 {"http://translate.google.com/", "google translate", 3, 3, 0}, | 780 {"http://store.steampowered.com/", "the steam summer camp sale", 6, 6, 1}, |
| 865 {"http://arstechnica.com/", "ars technica", 3, 3, 3}, | 781 {"http://www.teamliquid.net/tlpd/korean/players", |
| 866 {"http://www.rottentomatoes.com/", "movies | movie trailers | reviews - " | 782 "tlpd - bw korean - player " |
| 867 "rotten tomatoes", 3, 3, 7}, | 783 "index", |
| 868 {"http://www.teamliquid.net/", "team liquid - starcraft 2 and brood war pro " | 784 25, 7, 219}, |
| 869 "gaming news", 26, 25, 3}, | 785 {"http://slashdot.org/", "slashdot: news for nerds, stuff that matters", 3, |
| 870 {"http://metaleater.com/", "metaleater", 4, 3, 8}, | 786 3, 6}, |
| 871 {"http://half.com/", "half.com: textbooks , books , music , movies , games , " | 787 {"http://translate.google.com/", "google translate", 3, 3, 0}, |
| 872 "video games", 4, 4, 6}, | 788 {"http://arstechnica.com/", "ars technica", 3, 3, 3}, |
| 873 {"http://teamliquid.net/", "team liquid - starcraft 2 and brood war pro " | 789 {"http://www.rottentomatoes.com/", |
| 874 "gaming news", 8, 5, 9}, | 790 "movies | movie trailers | reviews - " |
| 791 "rotten tomatoes", | |
| 792 3, 3, 7}, | |
| 793 {"http://www.teamliquid.net/", | |
| 794 "team liquid - starcraft 2 and brood war pro " | |
| 795 "gaming news", | |
| 796 26, 25, 3}, | |
| 797 {"http://metaleater.com/", "metaleater", 4, 3, 8}, | |
| 798 {"http://half.com/", | |
| 799 "half.com: textbooks , books , music , movies , games , " | |
| 800 "video games", | |
| 801 4, 4, 6}, | |
| 802 {"http://teamliquid.net/", | |
| 803 "team liquid - starcraft 2 and brood war pro " | |
| 804 "gaming news", | |
| 805 8, 5, 9}, | |
| 875 }; | 806 }; |
| 876 | 807 |
| 877 class HQPOrderingTest : public HistoryQuickProviderTest { | 808 class HQPOrderingTest : public HistoryQuickProviderTest { |
| 878 protected: | 809 protected: |
| 879 void GetTestData(size_t* data_count, TestURLInfo** test_data) override; | 810 void GetTestData(size_t* data_count, TestURLInfo** test_data) override; |
| 880 }; | 811 }; |
| 881 | 812 |
| 882 void HQPOrderingTest::GetTestData(size_t* data_count, TestURLInfo** test_data) { | 813 void HQPOrderingTest::GetTestData(size_t* data_count, TestURLInfo** test_data) { |
| 883 DCHECK(data_count); | 814 DCHECK(data_count); |
| 884 DCHECK(test_data); | 815 DCHECK(test_data); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 898 | 829 |
| 899 TEST_F(HQPOrderingTest, TEAMatch) { | 830 TEST_F(HQPOrderingTest, TEAMatch) { |
| 900 std::vector<std::string> expected_urls; | 831 std::vector<std::string> expected_urls; |
| 901 expected_urls.push_back("http://www.teamliquid.net/"); | 832 expected_urls.push_back("http://www.teamliquid.net/"); |
| 902 expected_urls.push_back("http://www.teamliquid.net/tlpd"); | 833 expected_urls.push_back("http://www.teamliquid.net/tlpd"); |
| 903 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); | 834 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); |
| 904 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true, | 835 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true, |
| 905 ASCIIToUTF16("www.teamliquid.net"), | 836 ASCIIToUTF16("www.teamliquid.net"), |
| 906 ASCIIToUTF16("mliquid.net")); | 837 ASCIIToUTF16("mliquid.net")); |
| 907 } | 838 } |
| OLD | NEW |