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

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: Forward-declare more friends. 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_ = bookmark_client_.CreateModel(); 184 bookmark_model_ = bookmark_client_.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(), /*create_db=*/true);
Peter Kasting 2016/01/30 01:09:22 Nit: Don't add param name comments like this
rohitrao (ping after 24h) 2016/01/31 15:45:56 Removed.
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 bookmarks::TestBookmarkClient bookmark_client_; 227 bookmarks::TestBookmarkClient bookmark_client_;
237 scoped_ptr<bookmarks::BookmarkModel> bookmark_model_; 228 scoped_ptr<bookmarks::BookmarkModel> bookmark_model_;
238 TestSchemeClassifier scheme_classifier_; 229 TestSchemeClassifier scheme_classifier_;
239 SearchTermsData search_terms_data_; 230 SearchTermsData search_terms_data_;
240 scoped_ptr<InMemoryURLIndex> in_memory_url_index_; 231 scoped_ptr<InMemoryURLIndex> in_memory_url_index_;
241 history::HistoryService history_service_; 232 scoped_ptr<history::HistoryService> history_service_;
242 233
243 DISALLOW_COPY_AND_ASSIGN(FakeAutocompleteProviderClient); 234 DISALLOW_COPY_AND_ASSIGN(FakeAutocompleteProviderClient);
244 }; 235 };
245 236
246 } // namespace 237 } // namespace
247 238
248 class HistoryQuickProviderTest : public testing::Test { 239 class HistoryQuickProviderTest : public testing::Test {
249 public: 240 public:
250 HistoryQuickProviderTest() : pool_owner_(3, "Background Pool") {} 241 HistoryQuickProviderTest() {}
251 242
252 protected: 243 protected:
253 class SetShouldContain : public std::unary_function<const std::string&, 244 class SetShouldContain : public std::unary_function<const std::string&,
254 std::set<std::string> > { 245 std::set<std::string> > {
255 public: 246 public:
256 explicit SetShouldContain(const ACMatches& matched_urls); 247 explicit SetShouldContain(const ACMatches& matched_urls);
257 248
258 void operator()(const std::string& expected); 249 void operator()(const std::string& expected);
259 250
260 std::set<std::string> LeftOvers() const { return matches_; } 251 std::set<std::string> LeftOvers() const { return matches_; }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 // > message_loop(). 287 // > message_loop().
297 // Direct use of this object in tests is almost certainly not thread-safe. 288 // Direct use of this object in tests is almost certainly not thread-safe.
298 history::HistoryBackend* history_backend() { 289 history::HistoryBackend* history_backend() {
299 return client_->GetHistoryService()->history_backend_.get(); 290 return client_->GetHistoryService()->history_backend_.get();
300 } 291 }
301 292
302 // Call history_backend()->GetURL(url, NULL) on the history thread, returning 293 // Call history_backend()->GetURL(url, NULL) on the history thread, returning
303 // the result. 294 // the result.
304 bool GetURLProxy(const GURL& url); 295 bool GetURLProxy(const GURL& url);
305 296
306 // Helper functions to initialize the HistoryService.
307 bool InitializeHistoryService();
308 void CreateInMemoryURLIndex();
309 void BlockUntilHistoryProcessesPendingRequests();
310 void BlockUntilHistoryIndexIsRefreshed();
311
312 base::MessageLoop message_loop_; 297 base::MessageLoop message_loop_;
313 base::SequencedWorkerPoolOwner pool_owner_;
314 base::ScopedTempDir history_dir_;
315 scoped_ptr<FakeAutocompleteProviderClient> client_; 298 scoped_ptr<FakeAutocompleteProviderClient> client_;
316 299
317 ACMatches ac_matches_; // The resulting matches after running RunTest. 300 ACMatches ac_matches_; // The resulting matches after running RunTest.
318 301
319 scoped_refptr<HistoryQuickProvider> provider_; 302 scoped_refptr<HistoryQuickProvider> provider_;
320 }; 303 };
321 304
322 void HistoryQuickProviderTest::SetUp() { 305 void HistoryQuickProviderTest::SetUp() {
323 client_.reset(new FakeAutocompleteProviderClient()); 306 client_.reset(new FakeAutocompleteProviderClient());
324 ASSERT_TRUE(InitializeHistoryService()); 307 ASSERT_TRUE(client_->GetHistoryService());
325 FillData(); 308 FillData();
326 309
327 // |FillData()| must be called before |CreateInMemoryURLIndex()|. This will 310 // |FillData()| must be called before |RebuildInMemoryURLIndex()|. This will
328 // ensure that the index is properly populated with data from the database. 311 // ensure that the index is properly populated with data from the database.
329 CreateInMemoryURLIndex(); 312 RebuildInMemoryURLIndex(client_->GetInMemoryURLIndex(),
330 BlockUntilHistoryIndexIsRefreshed(); 313 client_->GetHistoryService());
331 // History index refresh creates rebuilt tasks to run on history thread. 314 // History index refresh creates rebuilt tasks to run on history thread.
332 // Block here to make sure that all of them are complete. 315 // Block here to make sure that all of them are complete.
333 BlockUntilHistoryProcessesPendingRequests(); 316 history::BlockUntilHistoryProcessesPendingRequests(
317 client_->GetHistoryService());
334 318
335 provider_ = new HistoryQuickProvider(client_.get()); 319 provider_ = new HistoryQuickProvider(client_.get());
336 } 320 }
337 321
338 void HistoryQuickProviderTest::TearDown() { 322 void HistoryQuickProviderTest::TearDown() {
339 provider_ = NULL; 323 provider_ = NULL;
340 // The InMemoryURLIndex must be explicitly shut down or it will DCHECK() in 324 // The InMemoryURLIndex must be explicitly shut down or it will DCHECK() in
341 // its destructor. 325 // its destructor.
342 client_->GetInMemoryURLIndex()->Shutdown(); 326 client_->GetInMemoryURLIndex()->Shutdown();
343 client_->set_in_memory_url_index(nullptr); 327 client_->set_in_memory_url_index(nullptr);
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 bool result = false; 474 bool result = false;
491 client_->GetHistoryService()->ScheduleDBTask( 475 client_->GetHistoryService()->ScheduleDBTask(
492 scoped_ptr<history::HistoryDBTask>(new GetURLTask(url, &result)), 476 scoped_ptr<history::HistoryDBTask>(new GetURLTask(url, &result)),
493 &task_tracker); 477 &task_tracker);
494 // Run the message loop until GetURLTask::DoneRunOnMainThread stops it. If 478 // Run the message loop until GetURLTask::DoneRunOnMainThread stops it. If
495 // the test hangs, DoneRunOnMainThread isn't being invoked correctly. 479 // the test hangs, DoneRunOnMainThread isn't being invoked correctly.
496 base::MessageLoop::current()->Run(); 480 base::MessageLoop::current()->Run();
497 return result; 481 return result;
498 } 482 }
499 483
500 bool HistoryQuickProviderTest::InitializeHistoryService() {
501 if (!history_dir_.CreateUniqueTempDir() ||
502 !client_->GetHistoryService()->Init(
503 false, client_->GetAcceptLanguages(),
504 history::TestHistoryDatabaseParamsForPath(history_dir_.path())))
505 return false;
506
507 BlockUntilHistoryProcessesPendingRequests();
508 return true;
509 }
510
511 void HistoryQuickProviderTest::CreateInMemoryURLIndex() {
512 scoped_ptr<InMemoryURLIndex> in_memory_url_index(new InMemoryURLIndex(
513 client_->GetBookmarkModel(), client_->GetHistoryService(),
514 pool_owner_.pool().get(), history_dir_.path(),
515 client_->GetAcceptLanguages(), SchemeSet()));
516 in_memory_url_index->Init();
517 in_memory_url_index->RebuildFromHistory(history_backend()->db());
518 client_->set_in_memory_url_index(std::move(in_memory_url_index));
519 }
520
521 void HistoryQuickProviderTest::BlockUntilHistoryProcessesPendingRequests() {
522 base::CancelableTaskTracker tracker;
523 client_->GetHistoryService()->ScheduleDBTask(
524 scoped_ptr<history::HistoryDBTask>(new QuitTask()), &tracker);
525 base::MessageLoop::current()->Run();
526 }
527
528 void HistoryQuickProviderTest::BlockUntilHistoryIndexIsRefreshed() {
529 InMemoryURLIndex* index = client_->GetInMemoryURLIndex();
530 if (!index || index->restored())
531 return;
532 base::RunLoop run_loop;
533 HistoryIndexRestoreObserver observer(run_loop.QuitClosure());
534 index->set_restore_cache_observer(&observer);
535 run_loop.Run();
536 index->set_restore_cache_observer(nullptr);
537 DCHECK(index->restored());
538 }
539
540 TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) { 484 TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) {
541 std::vector<std::string> expected_urls; 485 std::vector<std::string> expected_urls;
542 expected_urls.push_back("http://slashdot.org/favorite_page.html"); 486 expected_urls.push_back("http://slashdot.org/favorite_page.html");
543 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true, 487 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true,
544 ASCIIToUTF16("slashdot.org/favorite_page.html"), 488 ASCIIToUTF16("slashdot.org/favorite_page.html"),
545 ASCIIToUTF16(".org/favorite_page.html")); 489 ASCIIToUTF16(".org/favorite_page.html"));
546 } 490 }
547 491
548 TEST_F(HistoryQuickProviderTest, SingleMatchWithCursor) { 492 TEST_F(HistoryQuickProviderTest, SingleMatchWithCursor) {
549 std::vector<std::string> expected_urls; 493 std::vector<std::string> expected_urls;
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
972 916
973 TEST_F(HQPOrderingTest, TEAMatch) { 917 TEST_F(HQPOrderingTest, TEAMatch) {
974 std::vector<std::string> expected_urls; 918 std::vector<std::string> expected_urls;
975 expected_urls.push_back("http://www.teamliquid.net/"); 919 expected_urls.push_back("http://www.teamliquid.net/");
976 expected_urls.push_back("http://www.teamliquid.net/tlpd"); 920 expected_urls.push_back("http://www.teamliquid.net/tlpd");
977 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players"); 921 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players");
978 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true, 922 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true,
979 ASCIIToUTF16("www.teamliquid.net"), 923 ASCIIToUTF16("www.teamliquid.net"),
980 ASCIIToUTF16("mliquid.net")); 924 ASCIIToUTF16("mliquid.net"));
981 } 925 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698