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" | 20 #include "base/strings/stringprintf.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/test/sequenced_worker_pool_owner.h" | |
| 24 #include "components/bookmarks/browser/bookmark_model.h" | 22 #include "components/bookmarks/browser/bookmark_model.h" |
| 25 #include "components/bookmarks/test/bookmark_test_helpers.h" | 23 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 26 #include "components/bookmarks/test/test_bookmark_client.h" | 24 #include "components/bookmarks/test/test_bookmark_client.h" |
| 27 #include "components/history/core/browser/history_backend.h" | 25 #include "components/history/core/browser/history_backend.h" |
| 28 #include "components/history/core/browser/history_database.h" | 26 #include "components/history/core/browser/history_database.h" |
| 29 #include "components/history/core/browser/history_service.h" | 27 #include "components/history/core/browser/history_service.h" |
| 30 #include "components/history/core/browser/history_service_observer.h" | 28 #include "components/history/core/browser/history_service_observer.h" |
| 31 #include "components/history/core/browser/url_database.h" | 29 #include "components/history/core/browser/url_database.h" |
| 32 #include "components/history/core/test/history_service_test_util.h" | 30 #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" | 31 #include "components/omnibox/browser/autocomplete_match.h" |
| 35 #include "components/omnibox/browser/autocomplete_result.h" | 32 #include "components/omnibox/browser/autocomplete_result.h" |
| 33 #include "components/omnibox/browser/fake_autocomplete_provider_client.h" | |
| 34 #include "components/omnibox/browser/history_test_util.h" | |
| 36 #include "components/omnibox/browser/history_url_provider.h" | 35 #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" | 36 #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" | 37 #include "components/prefs/pref_service.h" |
| 43 #include "components/search_engines/search_terms_data.h" | 38 #include "components/search_engines/search_terms_data.h" |
| 44 #include "sql/transaction.h" | 39 #include "sql/transaction.h" |
| 45 #include "testing/gtest/include/gtest/gtest.h" | 40 #include "testing/gtest/include/gtest/gtest.h" |
| 46 | 41 |
| 47 using base::ASCIIToUTF16; | 42 using base::ASCIIToUTF16; |
| 48 using base::Time; | 43 using base::Time; |
| 49 using base::TimeDelta; | 44 using base::TimeDelta; |
| 50 | 45 |
| 51 namespace { | 46 namespace { |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 171 | 166 |
| 172 private: | 167 private: |
| 173 ~GetURLTask() override {} | 168 ~GetURLTask() override {} |
| 174 | 169 |
| 175 bool* result_storage_; | 170 bool* result_storage_; |
| 176 const GURL url_; | 171 const GURL url_; |
| 177 | 172 |
| 178 DISALLOW_COPY_AND_ASSIGN(GetURLTask); | 173 DISALLOW_COPY_AND_ASSIGN(GetURLTask); |
| 179 }; | 174 }; |
| 180 | 175 |
| 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 | 176 } // namespace |
| 233 | 177 |
| 234 class HistoryQuickProviderTest : public testing::Test { | 178 class HistoryQuickProviderTest : public testing::Test { |
| 235 public: | 179 public: |
| 236 HistoryQuickProviderTest() {} | 180 HistoryQuickProviderTest() {} |
| 237 | 181 |
| 238 protected: | 182 protected: |
| 239 class SetShouldContain { | 183 class SetShouldContain { |
| 240 public: | 184 public: |
| 241 explicit SetShouldContain(const ACMatches& matched_urls); | 185 explicit SetShouldContain(const ACMatches& matched_urls); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 sql::Connection& db(history_backend()->db()->GetDB()); | 284 sql::Connection& db(history_backend()->db()->GetDB()); |
| 341 ASSERT_TRUE(db.is_open()); | 285 ASSERT_TRUE(db.is_open()); |
| 342 | 286 |
| 343 size_t data_count = 0; | 287 size_t data_count = 0; |
| 344 TestURLInfo* test_data = NULL; | 288 TestURLInfo* test_data = NULL; |
| 345 GetTestData(&data_count, &test_data); | 289 GetTestData(&data_count, &test_data); |
| 346 size_t visit_id = 1; | 290 size_t visit_id = 1; |
| 347 for (size_t i = 0; i < data_count; ++i) { | 291 for (size_t i = 0; i < data_count; ++i) { |
| 348 const TestURLInfo& cur(test_data[i]); | 292 const TestURLInfo& cur(test_data[i]); |
| 349 Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now); | 293 Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now); |
| 350 sql::Transaction transaction(&db); | |
| 351 | 294 |
| 352 // Add URL. | 295 history::AddURLToDBForTests(&db, i + 1, cur.url, cur.title, cur.visit_count, |
| 353 transaction.Begin(); | 296 cur.typed_count, visit_time); |
| 354 std::string sql_cmd_line = base::StringPrintf( | |
| 355 "INSERT INTO \"urls\" VALUES(%" PRIuS ", \'%s\', \'%s\', %d, %d, %" | |
| 356 PRId64 ", 0, 0)", | |
| 357 i + 1, cur.url.c_str(), cur.title.c_str(), cur.visit_count, | |
| 358 cur.typed_count, visit_time.ToInternalValue()); | |
| 359 sql::Statement sql_stmt(db.GetUniqueStatement(sql_cmd_line.c_str())); | |
| 360 EXPECT_TRUE(sql_stmt.Run()); | |
| 361 transaction.Commit(); | |
| 362 | 297 |
| 363 // Add visits. | 298 auto add_visit = [&db, &visit_id, &visit_time, |
| 364 for (int j = 0; j < cur.visit_count; ++j) { | 299 &i](ui::PageTransition transtition) mutable { |
|
Peter Kasting
2016/10/24 22:28:29
Nit: Can just use [&]
dyaroshev
2016/10/26 22:29:13
I thought it wasn't allowed. Done.
| |
| 365 // Assume earlier visits are at one-day intervals. | 300 // Assume earlier visits are at one-day intervals. |
| 366 visit_time -= TimeDelta::FromDays(1); | 301 visit_time -= TimeDelta::FromDays(1); |
| 367 transaction.Begin(); | 302 history::AddVisitToDBForTests(&db, visit_id++, i + 1, visit_time, |
| 368 // Mark the most recent |cur.typed_count| visits as typed. | 303 transtition); |
| 369 std::string sql_cmd_line = base::StringPrintf( | 304 }; |
| 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 | 305 |
| 376 sql::Statement sql_stmt(db.GetUniqueStatement(sql_cmd_line.c_str())); | 306 // Mark the most recent |cur.typed_count| visits as typed. |
| 377 EXPECT_TRUE(sql_stmt.Run()); | 307 for (int j = 0; j < cur.typed_count; ++j) |
| 378 transaction.Commit(); | 308 add_visit(ui::PAGE_TRANSITION_TYPED); |
| 379 } | 309 |
| 310 for (int j = cur.typed_count; j < cur.visit_count; ++j) | |
| 311 add_visit(ui::PAGE_TRANSITION_LINK); | |
| 380 } | 312 } |
| 381 } | 313 } |
| 382 | 314 |
| 383 HistoryQuickProviderTest::SetShouldContain::SetShouldContain( | 315 HistoryQuickProviderTest::SetShouldContain::SetShouldContain( |
| 384 const ACMatches& matched_urls) { | 316 const ACMatches& matched_urls) { |
| 385 for (ACMatches::const_iterator iter = matched_urls.begin(); | 317 for (ACMatches::const_iterator iter = matched_urls.begin(); |
| 386 iter != matched_urls.end(); ++iter) | 318 iter != matched_urls.end(); ++iter) |
| 387 matches_.insert(iter->destination_url.spec()); | 319 matches_.insert(iter->destination_url.spec()); |
| 388 } | 320 } |
| 389 | 321 |
| (...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 898 | 830 |
| 899 TEST_F(HQPOrderingTest, TEAMatch) { | 831 TEST_F(HQPOrderingTest, TEAMatch) { |
| 900 std::vector<std::string> expected_urls; | 832 std::vector<std::string> expected_urls; |
| 901 expected_urls.push_back("http://www.teamliquid.net/"); | 833 expected_urls.push_back("http://www.teamliquid.net/"); |
| 902 expected_urls.push_back("http://www.teamliquid.net/tlpd"); | 834 expected_urls.push_back("http://www.teamliquid.net/tlpd"); |
| 903 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); | 835 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); |
| 904 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true, | 836 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true, |
| 905 ASCIIToUTF16("www.teamliquid.net"), | 837 ASCIIToUTF16("www.teamliquid.net"), |
| 906 ASCIIToUTF16("mliquid.net")); | 838 ASCIIToUTF16("mliquid.net")); |
| 907 } | 839 } |
| OLD | NEW |