Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: components/omnibox/browser/history_quick_provider_unittest.cc

Issue 1646893003: Moves shared history test helpers into util classes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@history
Patch Set: ASAN. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <set> 11 #include <set>
12 #include <string> 12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/files/scoped_temp_dir.h" 15 #include "base/files/scoped_temp_dir.h"
16 #include "base/format_macros.h" 16 #include "base/format_macros.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/scoped_ptr.h" 18 #include "base/memory/scoped_ptr.h"
19 #include "base/message_loop/message_loop.h" 19 #include "base/message_loop/message_loop.h"
20 #include "base/prefs/pref_service.h" 20 #include "base/prefs/pref_service.h"
21 #include "base/strings/stringprintf.h" 21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
23 #include "base/test/sequenced_worker_pool_owner.h" 23 #include "base/test/sequenced_worker_pool_owner.h"
24 #include "components/bookmarks/browser/bookmark_model.h" 24 #include "components/bookmarks/browser/bookmark_model.h"
25 #include "components/bookmarks/test/bookmark_test_helpers.h" 25 #include "components/bookmarks/test/bookmark_test_helpers.h"
26 #include "components/bookmarks/test/test_bookmark_client.h" 26 #include "components/bookmarks/test/test_bookmark_client.h"
27 #include "components/history/core/browser/history_backend.h" 27 #include "components/history/core/browser/history_backend.h"
28 #include "components/history/core/browser/history_database.h" 28 #include "components/history/core/browser/history_database.h"
29 #include "components/history/core/browser/history_database_params.h"
30 #include "components/history/core/browser/history_service.h" 29 #include "components/history/core/browser/history_service.h"
31 #include "components/history/core/browser/history_service_observer.h" 30 #include "components/history/core/browser/history_service_observer.h"
32 #include "components/history/core/browser/url_database.h" 31 #include "components/history/core/browser/url_database.h"
33 #include "components/history/core/test/test_history_database.h" 32 #include "components/history/core/test/history_service_test_util.h"
34 #include "components/metrics/proto/omnibox_event.pb.h" 33 #include "components/metrics/proto/omnibox_event.pb.h"
35 #include "components/omnibox/browser/autocomplete_match.h" 34 #include "components/omnibox/browser/autocomplete_match.h"
36 #include "components/omnibox/browser/autocomplete_result.h" 35 #include "components/omnibox/browser/autocomplete_result.h"
37 #include "components/omnibox/browser/history_index_restore_observer.h"
38 #include "components/omnibox/browser/history_url_provider.h" 36 #include "components/omnibox/browser/history_url_provider.h"
39 #include "components/omnibox/browser/in_memory_url_index.h" 37 #include "components/omnibox/browser/in_memory_url_index.h"
38 #include "components/omnibox/browser/in_memory_url_index_test_util.h"
40 #include "components/omnibox/browser/mock_autocomplete_provider_client.h" 39 #include "components/omnibox/browser/mock_autocomplete_provider_client.h"
41 #include "components/omnibox/browser/test_scheme_classifier.h" 40 #include "components/omnibox/browser/test_scheme_classifier.h"
42 #include "components/omnibox/browser/url_index_private_data.h" 41 #include "components/omnibox/browser/url_index_private_data.h"
43 #include "components/search_engines/search_terms_data.h" 42 #include "components/search_engines/search_terms_data.h"
44 #include "components/search_engines/template_url.h" 43 #include "components/search_engines/template_url.h"
45 #include "components/search_engines/template_url_service.h" 44 #include "components/search_engines/template_url_service.h"
46 #include "sql/transaction.h" 45 #include "sql/transaction.h"
47 #include "testing/gtest/include/gtest/gtest.h" 46 #include "testing/gtest/include/gtest/gtest.h"
48 47
49 using base::ASCIIToUTF16; 48 using base::ASCIIToUTF16;
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 171
173 private: 172 private:
174 ~GetURLTask() override {} 173 ~GetURLTask() override {}
175 174
176 bool* result_storage_; 175 bool* result_storage_;
177 const GURL url_; 176 const GURL url_;
178 177
179 DISALLOW_COPY_AND_ASSIGN(GetURLTask); 178 DISALLOW_COPY_AND_ASSIGN(GetURLTask);
180 }; 179 };
181 180
182 class QuitTask : public history::HistoryDBTask {
183 public:
184 QuitTask() {}
185
186 bool RunOnDBThread(history::HistoryBackend* backend,
187 history::HistoryDatabase* db) override {
188 return true;
189 }
190
191 void DoneRunOnMainThread() override {
192 base::MessageLoop::current()->QuitWhenIdle();
193 }
194
195 private:
196 ~QuitTask() override {}
197
198 DISALLOW_COPY_AND_ASSIGN(QuitTask);
199 };
200
201 class FakeAutocompleteProviderClient : public MockAutocompleteProviderClient { 181 class FakeAutocompleteProviderClient : public MockAutocompleteProviderClient {
202 public: 182 public:
203 FakeAutocompleteProviderClient() { 183 FakeAutocompleteProviderClient() : pool_owner_(3, "Background Pool") {
204 bookmark_model_ = bookmarks::TestBookmarkClient::CreateModel(); 184 bookmark_model_ = bookmarks::TestBookmarkClient::CreateModel();
205 set_template_url_service( 185 set_template_url_service(
206 make_scoped_ptr(new TemplateURLService(nullptr, 0))); 186 make_scoped_ptr(new TemplateURLService(nullptr, 0)));
187 if (history_dir_.CreateUniqueTempDir()) {
188 history_service_ = history::CreateHistoryService(
189 history_dir_.path(), GetAcceptLanguages(), true);
190 }
191
192 in_memory_url_index_.reset(new InMemoryURLIndex(
193 bookmark_model_.get(), history_service_.get(), pool_owner_.pool().get(),
194 history_dir_.path(), GetAcceptLanguages(), SchemeSet()));
195 in_memory_url_index_->Init();
207 } 196 }
208 197
209 const AutocompleteSchemeClassifier& GetSchemeClassifier() const override { 198 const AutocompleteSchemeClassifier& GetSchemeClassifier() const override {
210 return scheme_classifier_; 199 return scheme_classifier_;
211 } 200 }
212 201
213 const SearchTermsData& GetSearchTermsData() const override { 202 const SearchTermsData& GetSearchTermsData() const override {
214 return search_terms_data_; 203 return search_terms_data_;
215 } 204 }
216 205
217 history::HistoryService* GetHistoryService() override { 206 history::HistoryService* GetHistoryService() override {
218 return &history_service_; 207 return history_service_.get();
219 } 208 }
220 209
221 bookmarks::BookmarkModel* GetBookmarkModel() override { 210 bookmarks::BookmarkModel* GetBookmarkModel() override {
222 return bookmark_model_.get(); 211 return bookmark_model_.get();
223 } 212 }
224 213
225 InMemoryURLIndex* GetInMemoryURLIndex() override { 214 InMemoryURLIndex* GetInMemoryURLIndex() override {
226 return in_memory_url_index_.get(); 215 return in_memory_url_index_.get();
227 } 216 }
228 217
229 std::string GetAcceptLanguages() const override { return "en,en-US,ko"; } 218 std::string GetAcceptLanguages() const override { return "en,en-US,ko"; }
230 219
231 void set_in_memory_url_index(scoped_ptr<InMemoryURLIndex> index) { 220 void set_in_memory_url_index(scoped_ptr<InMemoryURLIndex> index) {
232 in_memory_url_index_ = std::move(index); 221 in_memory_url_index_ = std::move(index);
233 } 222 }
234 223
235 private: 224 private:
225 base::SequencedWorkerPoolOwner pool_owner_;
226 base::ScopedTempDir history_dir_;
236 scoped_ptr<bookmarks::BookmarkModel> bookmark_model_; 227 scoped_ptr<bookmarks::BookmarkModel> bookmark_model_;
237 TestSchemeClassifier scheme_classifier_; 228 TestSchemeClassifier scheme_classifier_;
238 SearchTermsData search_terms_data_; 229 SearchTermsData search_terms_data_;
239 scoped_ptr<InMemoryURLIndex> in_memory_url_index_; 230 scoped_ptr<InMemoryURLIndex> in_memory_url_index_;
240 history::HistoryService history_service_; 231 scoped_ptr<history::HistoryService> history_service_;
241 232
242 DISALLOW_COPY_AND_ASSIGN(FakeAutocompleteProviderClient); 233 DISALLOW_COPY_AND_ASSIGN(FakeAutocompleteProviderClient);
243 }; 234 };
244 235
245 } // namespace 236 } // namespace
246 237
247 class HistoryQuickProviderTest : public testing::Test { 238 class HistoryQuickProviderTest : public testing::Test {
248 public: 239 public:
249 HistoryQuickProviderTest() : pool_owner_(3, "Background Pool") {} 240 HistoryQuickProviderTest() {}
250 241
251 protected: 242 protected:
252 class SetShouldContain : public std::unary_function<const std::string&, 243 class SetShouldContain : public std::unary_function<const std::string&,
253 std::set<std::string> > { 244 std::set<std::string> > {
254 public: 245 public:
255 explicit SetShouldContain(const ACMatches& matched_urls); 246 explicit SetShouldContain(const ACMatches& matched_urls);
256 247
257 void operator()(const std::string& expected); 248 void operator()(const std::string& expected);
258 249
259 std::set<std::string> LeftOvers() const { return matches_; } 250 std::set<std::string> LeftOvers() const { return matches_; }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 // > message_loop(). 286 // > message_loop().
296 // Direct use of this object in tests is almost certainly not thread-safe. 287 // Direct use of this object in tests is almost certainly not thread-safe.
297 history::HistoryBackend* history_backend() { 288 history::HistoryBackend* history_backend() {
298 return client_->GetHistoryService()->history_backend_.get(); 289 return client_->GetHistoryService()->history_backend_.get();
299 } 290 }
300 291
301 // Call history_backend()->GetURL(url, NULL) on the history thread, returning 292 // Call history_backend()->GetURL(url, NULL) on the history thread, returning
302 // the result. 293 // the result.
303 bool GetURLProxy(const GURL& url); 294 bool GetURLProxy(const GURL& url);
304 295
305 // Helper functions to initialize the HistoryService.
306 bool InitializeHistoryService();
307 void CreateInMemoryURLIndex();
308 void BlockUntilHistoryProcessesPendingRequests();
309 void BlockUntilHistoryIndexIsRefreshed();
310
311 base::MessageLoop message_loop_; 296 base::MessageLoop message_loop_;
312 base::SequencedWorkerPoolOwner pool_owner_;
313 base::ScopedTempDir history_dir_;
314 scoped_ptr<FakeAutocompleteProviderClient> client_; 297 scoped_ptr<FakeAutocompleteProviderClient> client_;
315 298
316 ACMatches ac_matches_; // The resulting matches after running RunTest. 299 ACMatches ac_matches_; // The resulting matches after running RunTest.
317 300
318 scoped_refptr<HistoryQuickProvider> provider_; 301 scoped_refptr<HistoryQuickProvider> provider_;
319 }; 302 };
320 303
321 void HistoryQuickProviderTest::SetUp() { 304 void HistoryQuickProviderTest::SetUp() {
322 client_.reset(new FakeAutocompleteProviderClient()); 305 client_.reset(new FakeAutocompleteProviderClient());
323 ASSERT_TRUE(InitializeHistoryService()); 306 ASSERT_TRUE(client_->GetHistoryService());
324 FillData(); 307 FillData();
325 308
326 // |FillData()| must be called before |CreateInMemoryURLIndex()|. This will 309 // |FillData()| must be called before |RebuildFromHistory()|. This will
327 // ensure that the index is properly populated with data from the database. 310 // ensure that the index is properly populated with data from the database.
328 CreateInMemoryURLIndex(); 311 InMemoryURLIndex* url_index = client_->GetInMemoryURLIndex();
329 BlockUntilHistoryIndexIsRefreshed(); 312 url_index->RebuildFromHistory(
313 client_->GetHistoryService()->history_backend_->db());
314 BlockUntilInMemoryURLIndexIsRefreshed(url_index);
315
330 // History index refresh creates rebuilt tasks to run on history thread. 316 // History index refresh creates rebuilt tasks to run on history thread.
331 // Block here to make sure that all of them are complete. 317 // Block here to make sure that all of them are complete.
332 BlockUntilHistoryProcessesPendingRequests(); 318 history::BlockUntilHistoryProcessesPendingRequests(
319 client_->GetHistoryService());
333 320
334 provider_ = new HistoryQuickProvider(client_.get()); 321 provider_ = new HistoryQuickProvider(client_.get());
335 } 322 }
336 323
337 void HistoryQuickProviderTest::TearDown() { 324 void HistoryQuickProviderTest::TearDown() {
338 provider_ = NULL; 325 provider_ = NULL;
339 // The InMemoryURLIndex must be explicitly shut down or it will DCHECK() in 326 // The InMemoryURLIndex must be explicitly shut down or it will DCHECK() in
340 // its destructor. 327 // its destructor.
341 client_->GetInMemoryURLIndex()->Shutdown(); 328 client_->GetInMemoryURLIndex()->Shutdown();
342 client_->set_in_memory_url_index(nullptr); 329 client_->set_in_memory_url_index(nullptr);
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 bool result = false; 476 bool result = false;
490 client_->GetHistoryService()->ScheduleDBTask( 477 client_->GetHistoryService()->ScheduleDBTask(
491 scoped_ptr<history::HistoryDBTask>(new GetURLTask(url, &result)), 478 scoped_ptr<history::HistoryDBTask>(new GetURLTask(url, &result)),
492 &task_tracker); 479 &task_tracker);
493 // Run the message loop until GetURLTask::DoneRunOnMainThread stops it. If 480 // Run the message loop until GetURLTask::DoneRunOnMainThread stops it. If
494 // the test hangs, DoneRunOnMainThread isn't being invoked correctly. 481 // the test hangs, DoneRunOnMainThread isn't being invoked correctly.
495 base::MessageLoop::current()->Run(); 482 base::MessageLoop::current()->Run();
496 return result; 483 return result;
497 } 484 }
498 485
499 bool HistoryQuickProviderTest::InitializeHistoryService() {
500 if (!history_dir_.CreateUniqueTempDir() ||
501 !client_->GetHistoryService()->Init(
502 false, client_->GetAcceptLanguages(),
503 history::TestHistoryDatabaseParamsForPath(history_dir_.path())))
504 return false;
505
506 BlockUntilHistoryProcessesPendingRequests();
507 return true;
508 }
509
510 void HistoryQuickProviderTest::CreateInMemoryURLIndex() {
511 scoped_ptr<InMemoryURLIndex> in_memory_url_index(new InMemoryURLIndex(
512 client_->GetBookmarkModel(), client_->GetHistoryService(),
513 pool_owner_.pool().get(), history_dir_.path(),
514 client_->GetAcceptLanguages(), SchemeSet()));
515 in_memory_url_index->Init();
516 in_memory_url_index->RebuildFromHistory(history_backend()->db());
517 client_->set_in_memory_url_index(std::move(in_memory_url_index));
518 }
519
520 void HistoryQuickProviderTest::BlockUntilHistoryProcessesPendingRequests() {
521 base::CancelableTaskTracker tracker;
522 client_->GetHistoryService()->ScheduleDBTask(
523 scoped_ptr<history::HistoryDBTask>(new QuitTask()), &tracker);
524 base::MessageLoop::current()->Run();
525 }
526
527 void HistoryQuickProviderTest::BlockUntilHistoryIndexIsRefreshed() {
528 InMemoryURLIndex* index = client_->GetInMemoryURLIndex();
529 if (!index || index->restored())
530 return;
531 base::RunLoop run_loop;
532 HistoryIndexRestoreObserver observer(run_loop.QuitClosure());
533 index->set_restore_cache_observer(&observer);
534 run_loop.Run();
535 index->set_restore_cache_observer(nullptr);
536 DCHECK(index->restored());
537 }
538
539 TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) { 486 TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) {
540 std::vector<std::string> expected_urls; 487 std::vector<std::string> expected_urls;
541 expected_urls.push_back("http://slashdot.org/favorite_page.html"); 488 expected_urls.push_back("http://slashdot.org/favorite_page.html");
542 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, 489 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true,
543 ASCIIToUTF16("slashdot.org/favorite_page.html"), 490 ASCIIToUTF16("slashdot.org/favorite_page.html"),
544 ASCIIToUTF16(".org/favorite_page.html")); 491 ASCIIToUTF16(".org/favorite_page.html"));
545 } 492 }
546 493
547 TEST_F(HistoryQuickProviderTest, SingleMatchWithCursor) { 494 TEST_F(HistoryQuickProviderTest, SingleMatchWithCursor) {
548 std::vector<std::string> expected_urls; 495 std::vector<std::string> expected_urls;
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
971 918
972 TEST_F(HQPOrderingTest, TEAMatch) { 919 TEST_F(HQPOrderingTest, TEAMatch) {
973 std::vector<std::string> expected_urls; 920 std::vector<std::string> expected_urls;
974 expected_urls.push_back("http://www.teamliquid.net/"); 921 expected_urls.push_back("http://www.teamliquid.net/");
975 expected_urls.push_back("http://www.teamliquid.net/tlpd"); 922 expected_urls.push_back("http://www.teamliquid.net/tlpd");
976 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); 923 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players");
977 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true, 924 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true,
978 ASCIIToUTF16("www.teamliquid.net"), 925 ASCIIToUTF16("www.teamliquid.net"),
979 ASCIIToUTF16("mliquid.net")); 926 ASCIIToUTF16("mliquid.net"));
980 } 927 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/BUILD.gn ('k') | components/omnibox/browser/history_url_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698