diff options
Diffstat (limited to 'components/history')
4 files changed, 118 insertions, 1 deletions
diff --git a/components/history/core/browser/history_service.h b/components/history/core/browser/history_service.h index 1cf2812..9c8af63 100644 --- a/components/history/core/browser/history_service.h +++ b/components/history/core/browser/history_service.h @@ -581,10 +581,13 @@ class HistoryService : public syncer::SyncableService, public KeyedService { friend class ::HistoryQuickProviderTest; friend class HistoryServiceTest; friend class ::HistoryURLProvider; - friend class ::HistoryURLProviderTest; friend class ::InMemoryURLIndexTest; friend class ::SyncBookmarkDataTypeControllerTest; friend class ::TestingProfile; + friend scoped_ptr<HistoryService> CreateHistoryService( + const base::FilePath& history_dir, + const std::string& accept_languages, + bool create_db); // Called on shutdown, this will tell the history backend to complete and // will release pointers to it. No other functions should be called once diff --git a/components/history/core/test/BUILD.gn b/components/history/core/test/BUILD.gn index 104bf09..cd3d94f 100644 --- a/components/history/core/test/BUILD.gn +++ b/components/history/core/test/BUILD.gn @@ -13,6 +13,8 @@ static_library("test") { "history_backend_db_base_test.h", "history_client_fake_bookmarks.cc", "history_client_fake_bookmarks.h", + "history_service_test_util.cc", + "history_service_test_util.h", "history_unittest_base.cc", "history_unittest_base.h", "test_history_database.cc", diff --git a/components/history/core/test/history_service_test_util.cc b/components/history/core/test/history_service_test_util.cc new file mode 100644 index 0000000..b03c8b8 --- /dev/null +++ b/components/history/core/test/history_service_test_util.cc @@ -0,0 +1,74 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/history/core/test/history_service_test_util.h" + +#include "base/files/file_path.h" +#include "base/run_loop.h" +#include "components/history/core/browser/history_backend.h" +#include "components/history/core/browser/history_database.h" +#include "components/history/core/browser/history_database_params.h" +#include "components/history/core/browser/history_db_task.h" +#include "components/history/core/browser/history_service.h" +#include "components/history/core/browser/history_service_observer.h" +#include "components/history/core/browser/url_database.h" +#include "components/history/core/test/test_history_database.h" + +namespace { + +class QuitTask : public history::HistoryDBTask { + public: + QuitTask(const base::Closure& task) : task_(task) {} + + bool RunOnDBThread(history::HistoryBackend* backend, + history::HistoryDatabase* db) override { + return true; + } + + void DoneRunOnMainThread() override { task_.Run(); } + + private: + ~QuitTask() override {} + + base::Closure task_; + + DISALLOW_COPY_AND_ASSIGN(QuitTask); +}; + +} // namespace + +namespace history { + +scoped_ptr<HistoryService> CreateHistoryService( + const base::FilePath& history_dir, + const std::string& accept_languages, + bool create_db) { + scoped_ptr<HistoryService> history_service(new HistoryService()); + if (!history_service->Init( + !create_db, accept_languages, + history::TestHistoryDatabaseParamsForPath(history_dir))) { + return nullptr; + } + + if (create_db) + BlockUntilHistoryProcessesPendingRequests(history_service.get()); + return history_service; +} + +void BlockUntilHistoryProcessesPendingRequests( + HistoryService* history_service) { + base::RunLoop run_loop; + base::CancelableTaskTracker tracker; + history_service->ScheduleDBTask( + scoped_ptr<history::HistoryDBTask>(new QuitTask(run_loop.QuitClosure())), + &tracker); + run_loop.Run(); + + // Spin the runloop again until idle. The QuitTask above is destroyed via a + // task posted to the current message loop, so spinning allows the task to be + // properly destroyed. + base::RunLoop().RunUntilIdle(); +} + +} // namespace history diff --git a/components/history/core/test/history_service_test_util.h b/components/history/core/test/history_service_test_util.h new file mode 100644 index 0000000..c0a4a28 --- /dev/null +++ b/components/history/core/test/history_service_test_util.h @@ -0,0 +1,38 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_HISTORY_CORE_TEST_HISTORY_SERVICE_TEST_UTIL_H_ +#define COMPONENTS_HISTORY_CORE_TEST_HISTORY_SERVICE_TEST_UTIL_H_ + +#include <string> + +#include "base/macros.h" +#include "base/memory/scoped_ptr.h" + +namespace base { +class FilePath; +} + +namespace history { +class HistoryService; + +// Creates a new HistoryService that stores its data in |history_dir|. If +// |create_db| is false, the HistoryService will fail to initialize its +// database; this is useful for testing error conditions. This method spins the +// runloop before returning to ensure that any initialization-related tasks are +// run. +scoped_ptr<HistoryService> CreateHistoryService( + const base::FilePath& history_dir, + const std::string& accept_languages, + bool create_db); + +// Schedules a task on the history backend and runs a nested loop until the task +// is processed. This blocks the caller until the history service processes all +// pending requests. +void BlockUntilHistoryProcessesPendingRequests( + HistoryService* history_service); + +} // namespace history + +#endif // COMPONENTS_HISTORY_CORE_TEST_HISTORY_SERVICE_TEST_UTIL_H_ |