Moves shared history test helpers into util classes.

BUG=None

Review URL: https://codereview.chromium.org/1646893003

Cr-Commit-Position: refs/heads/master@{#372681}
diff --git a/components/history.gypi b/components/history.gypi
index 20f647c..88f1a12 100644
--- a/components/history.gypi
+++ b/components/history.gypi
@@ -188,6 +188,8 @@
         'history/core/test/history_client_fake_bookmarks.cc',
         'history/core/test/history_client_fake_bookmarks.h',
         'history/core/test/history_unittest_base.cc',
+        'history/core/test/history_service_test_util.cc',
+        'history/core/test/history_service_test_util.h',
         'history/core/test/history_unittest_base.h',
         'history/core/test/test_history_database.cc',
         'history/core/test/test_history_database.h',
diff --git a/components/history/core/browser/history_service.h b/components/history/core/browser/history_service.h
index 1cf2812..9c8af636 100644
--- a/components/history/core/browser/history_service.h
+++ b/components/history/core/browser/history_service.h
@@ -581,10 +581,13 @@
   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 @@
     "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..c0a4a282
--- /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_
diff --git a/components/omnibox.gypi b/components/omnibox.gypi
index f5322084..77cb05a2 100644
--- a/components/omnibox.gypi
+++ b/components/omnibox.gypi
@@ -183,6 +183,8 @@
         # Note: sources list duplicated in GN build.
         'omnibox/browser/history_index_restore_observer.cc',
         'omnibox/browser/history_index_restore_observer.h',
+        'omnibox/browser/in_memory_url_index_test_util.cc',
+        'omnibox/browser/in_memory_url_index_test_util.h',
         'omnibox/browser/mock_autocomplete_provider_client.cc',
         'omnibox/browser/mock_autocomplete_provider_client.h',
         'omnibox/browser/test_scheme_classifier.cc',
diff --git a/components/omnibox/browser/BUILD.gn b/components/omnibox/browser/BUILD.gn
index 329a1af..1d08593 100644
--- a/components/omnibox/browser/BUILD.gn
+++ b/components/omnibox/browser/BUILD.gn
@@ -161,6 +161,8 @@
   sources = [
     "history_index_restore_observer.cc",
     "history_index_restore_observer.h",
+    "in_memory_url_index_test_util.cc",
+    "in_memory_url_index_test_util.h",
     "mock_autocomplete_provider_client.cc",
     "mock_autocomplete_provider_client.h",
     "test_scheme_classifier.cc",
@@ -170,6 +172,7 @@
   deps = [
     ":browser",
     "//base",
+    "//components/history/core/browser",
     "//components/metrics/proto",
     "//components/search_engines",
     "//net",
diff --git a/components/omnibox/browser/history_quick_provider_unittest.cc b/components/omnibox/browser/history_quick_provider_unittest.cc
index cec085b..8531c0e 100644
--- a/components/omnibox/browser/history_quick_provider_unittest.cc
+++ b/components/omnibox/browser/history_quick_provider_unittest.cc
@@ -26,17 +26,16 @@
 #include "components/bookmarks/test/test_bookmark_client.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_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"
+#include "components/history/core/test/history_service_test_util.h"
 #include "components/metrics/proto/omnibox_event.pb.h"
 #include "components/omnibox/browser/autocomplete_match.h"
 #include "components/omnibox/browser/autocomplete_result.h"
-#include "components/omnibox/browser/history_index_restore_observer.h"
 #include "components/omnibox/browser/history_url_provider.h"
 #include "components/omnibox/browser/in_memory_url_index.h"
+#include "components/omnibox/browser/in_memory_url_index_test_util.h"
 #include "components/omnibox/browser/mock_autocomplete_provider_client.h"
 #include "components/omnibox/browser/test_scheme_classifier.h"
 #include "components/omnibox/browser/url_index_private_data.h"
@@ -179,31 +178,21 @@
   DISALLOW_COPY_AND_ASSIGN(GetURLTask);
 };
 
-class QuitTask : public history::HistoryDBTask {
- public:
-  QuitTask() {}
-
-  bool RunOnDBThread(history::HistoryBackend* backend,
-                     history::HistoryDatabase* db) override {
-    return true;
-  }
-
-  void DoneRunOnMainThread() override {
-    base::MessageLoop::current()->QuitWhenIdle();
-  }
-
- private:
-  ~QuitTask() override {}
-
-  DISALLOW_COPY_AND_ASSIGN(QuitTask);
-};
-
 class FakeAutocompleteProviderClient : public MockAutocompleteProviderClient {
  public:
-  FakeAutocompleteProviderClient() {
+  FakeAutocompleteProviderClient() : pool_owner_(3, "Background Pool") {
     bookmark_model_ = bookmarks::TestBookmarkClient::CreateModel();
     set_template_url_service(
         make_scoped_ptr(new TemplateURLService(nullptr, 0)));
+    if (history_dir_.CreateUniqueTempDir()) {
+      history_service_ = history::CreateHistoryService(
+          history_dir_.path(), GetAcceptLanguages(), true);
+    }
+
+    in_memory_url_index_.reset(new InMemoryURLIndex(
+        bookmark_model_.get(), history_service_.get(), pool_owner_.pool().get(),
+        history_dir_.path(), GetAcceptLanguages(), SchemeSet()));
+    in_memory_url_index_->Init();
   }
 
   const AutocompleteSchemeClassifier& GetSchemeClassifier() const override {
@@ -215,7 +204,7 @@
   }
 
   history::HistoryService* GetHistoryService() override {
-    return &history_service_;
+    return history_service_.get();
   }
 
   bookmarks::BookmarkModel* GetBookmarkModel() override {
@@ -233,11 +222,13 @@
   }
 
  private:
+  base::SequencedWorkerPoolOwner pool_owner_;
+  base::ScopedTempDir history_dir_;
   scoped_ptr<bookmarks::BookmarkModel> bookmark_model_;
   TestSchemeClassifier scheme_classifier_;
   SearchTermsData search_terms_data_;
   scoped_ptr<InMemoryURLIndex> in_memory_url_index_;
-  history::HistoryService history_service_;
+  scoped_ptr<history::HistoryService> history_service_;
 
   DISALLOW_COPY_AND_ASSIGN(FakeAutocompleteProviderClient);
 };
@@ -246,7 +237,7 @@
 
 class HistoryQuickProviderTest : public testing::Test {
  public:
-  HistoryQuickProviderTest() : pool_owner_(3, "Background Pool") {}
+  HistoryQuickProviderTest() {}
 
  protected:
   class SetShouldContain : public std::unary_function<const std::string&,
@@ -302,15 +293,7 @@
   // the result.
   bool GetURLProxy(const GURL& url);
 
-  // Helper functions to initialize the HistoryService.
-  bool InitializeHistoryService();
-  void CreateInMemoryURLIndex();
-  void BlockUntilHistoryProcessesPendingRequests();
-  void BlockUntilHistoryIndexIsRefreshed();
-
   base::MessageLoop message_loop_;
-  base::SequencedWorkerPoolOwner pool_owner_;
-  base::ScopedTempDir history_dir_;
   scoped_ptr<FakeAutocompleteProviderClient> client_;
 
   ACMatches ac_matches_;  // The resulting matches after running RunTest.
@@ -320,16 +303,20 @@
 
 void HistoryQuickProviderTest::SetUp() {
   client_.reset(new FakeAutocompleteProviderClient());
-  ASSERT_TRUE(InitializeHistoryService());
+  ASSERT_TRUE(client_->GetHistoryService());
   FillData();
 
-  // |FillData()| must be called before |CreateInMemoryURLIndex()|. This will
+  // |FillData()| must be called before |RebuildFromHistory()|. This will
   // ensure that the index is properly populated with data from the database.
-  CreateInMemoryURLIndex();
-  BlockUntilHistoryIndexIsRefreshed();
+  InMemoryURLIndex* url_index = client_->GetInMemoryURLIndex();
+  url_index->RebuildFromHistory(
+      client_->GetHistoryService()->history_backend_->db());
+  BlockUntilInMemoryURLIndexIsRefreshed(url_index);
+
   // History index refresh creates rebuilt tasks to run on history thread.
   // Block here to make sure that all of them are complete.
-  BlockUntilHistoryProcessesPendingRequests();
+  history::BlockUntilHistoryProcessesPendingRequests(
+      client_->GetHistoryService());
 
   provider_ = new HistoryQuickProvider(client_.get());
 }
@@ -496,46 +483,6 @@
   return result;
 }
 
-bool HistoryQuickProviderTest::InitializeHistoryService() {
-  if (!history_dir_.CreateUniqueTempDir() ||
-      !client_->GetHistoryService()->Init(
-          false, client_->GetAcceptLanguages(),
-          history::TestHistoryDatabaseParamsForPath(history_dir_.path())))
-    return false;
-
-  BlockUntilHistoryProcessesPendingRequests();
-  return true;
-}
-
-void HistoryQuickProviderTest::CreateInMemoryURLIndex() {
-  scoped_ptr<InMemoryURLIndex> in_memory_url_index(new InMemoryURLIndex(
-      client_->GetBookmarkModel(), client_->GetHistoryService(),
-      pool_owner_.pool().get(), history_dir_.path(),
-      client_->GetAcceptLanguages(), SchemeSet()));
-  in_memory_url_index->Init();
-  in_memory_url_index->RebuildFromHistory(history_backend()->db());
-  client_->set_in_memory_url_index(std::move(in_memory_url_index));
-}
-
-void HistoryQuickProviderTest::BlockUntilHistoryProcessesPendingRequests() {
-  base::CancelableTaskTracker tracker;
-  client_->GetHistoryService()->ScheduleDBTask(
-      scoped_ptr<history::HistoryDBTask>(new QuitTask()), &tracker);
-  base::MessageLoop::current()->Run();
-}
-
-void HistoryQuickProviderTest::BlockUntilHistoryIndexIsRefreshed() {
-  InMemoryURLIndex* index = client_->GetInMemoryURLIndex();
-  if (!index || index->restored())
-    return;
-  base::RunLoop run_loop;
-  HistoryIndexRestoreObserver observer(run_loop.QuitClosure());
-  index->set_restore_cache_observer(&observer);
-  run_loop.Run();
-  index->set_restore_cache_observer(nullptr);
-  DCHECK(index->restored());
-}
-
 TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) {
   std::vector<std::string> expected_urls;
   expected_urls.push_back("http://slashdot.org/favorite_page.html");
diff --git a/components/omnibox/browser/history_url_provider_unittest.cc b/components/omnibox/browser/history_url_provider_unittest.cc
index 3799eb0b..c75618e 100644
--- a/components/omnibox/browser/history_url_provider_unittest.cc
+++ b/components/omnibox/browser/history_url_provider_unittest.cc
@@ -16,10 +16,9 @@
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
 #include "base/time/time.h"
-#include "components/history/core/browser/history_database_params.h"
 #include "components/history/core/browser/history_service.h"
 #include "components/history/core/browser/url_database.h"
-#include "components/history/core/test/test_history_database.h"
+#include "components/history/core/test/history_service_test_util.h"
 #include "components/metrics/proto/omnibox_event.pb.h"
 #include "components/metrics/proto/omnibox_input_type.pb.h"
 #include "components/omnibox/browser/autocomplete_match.h"
@@ -42,6 +41,8 @@
 
 namespace {
 
+const char kDefaultAcceptLanguages[] = "en-US,en,ko";
+
 struct TestURLInfo {
   const char* url;
   const char* title;
@@ -153,30 +154,15 @@
   {"http://7.com/5a", "Five A", 8, 0, 64},  // never typed.
 };
 
-class QuitTask : public history::HistoryDBTask {
- public:
-  QuitTask() {}
-
-  bool RunOnDBThread(history::HistoryBackend* backend,
-                     history::HistoryDatabase* db) override {
-    return true;
-  }
-
-  void DoneRunOnMainThread() override {
-    base::MessageLoop::current()->QuitWhenIdle();
-  }
-
- private:
-  ~QuitTask() override {}
-
-  DISALLOW_COPY_AND_ASSIGN(QuitTask);
-};
-
 class FakeAutocompleteProviderClient : public MockAutocompleteProviderClient {
  public:
-  FakeAutocompleteProviderClient() {
+  FakeAutocompleteProviderClient(bool create_history_db) {
     set_template_url_service(
         make_scoped_ptr(new TemplateURLService(nullptr, 0)));
+    if (history_dir_.CreateUniqueTempDir()) {
+      history_service_ = history::CreateHistoryService(
+          history_dir_.path(), kDefaultAcceptLanguages, create_history_db);
+    }
   }
 
   const AutocompleteSchemeClassifier& GetSchemeClassifier() const override {
@@ -188,13 +174,14 @@
   }
 
   history::HistoryService* GetHistoryService() override {
-    return &history_service_;
+    return history_service_.get();
   }
 
  private:
   TestSchemeClassifier scheme_classifier_;
   SearchTermsData search_terms_data_;
-  history::HistoryService history_service_;
+  base::ScopedTempDir history_dir_;
+  scoped_ptr<history::HistoryService> history_service_;
 
   DISALLOW_COPY_AND_ASSIGN(FakeAutocompleteProviderClient);
 };
@@ -223,13 +210,11 @@
 
  protected:
   // testing::Test
-  void SetUp() override {
-    ASSERT_TRUE(SetUpImpl(false));
-  }
+  void SetUp() override { ASSERT_TRUE(SetUpImpl(true)); }
   void TearDown() override;
 
   // Does the real setup.
-  bool SetUpImpl(bool no_db) WARN_UNUSED_RESULT;
+  bool SetUpImpl(bool create_history_db) WARN_UNUSED_RESULT;
 
   // Fills test data into the history system.
   void FillData();
@@ -255,15 +240,9 @@
                    expected_urls, num_results, &type);
   }
 
-  // Helper functions to initialize the HistoryService.
-  bool InitializeHistoryService(bool delete_file, bool no_db);
-  void BlockUntilHistoryProcessesPendingRequests();
-
   base::MessageLoop message_loop_;
-  base::ScopedTempDir history_dir_;
   ACMatches matches_;
   scoped_ptr<FakeAutocompleteProviderClient> client_;
-  history::HistoryService* history_service_;
   scoped_refptr<HistoryURLProvider> autocomplete_;
   // Should the matches be sorted and duplicates removed?
   bool sort_matches_;
@@ -274,9 +253,7 @@
 
 class HistoryURLProviderTestNoDB : public HistoryURLProviderTest {
  protected:
-  void SetUp() override {
-    ASSERT_TRUE(SetUpImpl(true));
-  }
+  void SetUp() override { ASSERT_TRUE(SetUpImpl(false)); }
 };
 
 class HistoryURLProviderTestNoSearchProvider : public HistoryURLProviderTest {
@@ -297,38 +274,12 @@
     base::MessageLoop::current()->QuitWhenIdle();
 }
 
-bool HistoryURLProviderTest::InitializeHistoryService(
-    bool delete_file, bool no_db) {
-  if (!history_dir_.CreateUniqueTempDir())
-    return false;
-
-  history_service_ = client_->GetHistoryService();
-  if (!history_service_->Init(
-          no_db, std::string(),
-          history::TestHistoryDatabaseParamsForPath(history_dir_.path())))
-    return false;
-
-  if (!no_db)
-    BlockUntilHistoryProcessesPendingRequests();
-
-  return true;
-}
-
-void HistoryURLProviderTest::BlockUntilHistoryProcessesPendingRequests() {
-  base::CancelableTaskTracker tracker;
-  client_->GetHistoryService()->ScheduleDBTask(
-      scoped_ptr<history::HistoryDBTask>(new QuitTask()), &tracker);
-  base::MessageLoop::current()->Run();
-}
-
-bool HistoryURLProviderTest::SetUpImpl(bool no_db) {
-  client_.reset(new FakeAutocompleteProviderClient());
-
-  if (!InitializeHistoryService(true, no_db))
+bool HistoryURLProviderTest::SetUpImpl(bool create_history_db) {
+  client_.reset(new FakeAutocompleteProviderClient(create_history_db));
+  if (!client_->GetHistoryService())
     return false;
   EXPECT_CALL(*client_, GetAcceptLanguages())
-      .WillRepeatedly(testing::Return("en-US,en,ko"));
-
+      .WillRepeatedly(testing::Return(kDefaultAcceptLanguages));
   autocomplete_ = new HistoryURLProvider(client_.get(), this);
   FillData();
   return true;
@@ -351,16 +302,16 @@
   for (size_t i = 0; i < arraysize(test_db); ++i) {
     const TestURLInfo& cur = test_db[i];
     const GURL current_url(cur.url);
-    history_service_->AddPageWithDetails(
+    client_->GetHistoryService()->AddPageWithDetails(
         current_url, base::UTF8ToUTF16(cur.title), cur.visit_count,
         cur.typed_count, now - TimeDelta::FromDays(cur.age_in_days), false,
         history::SOURCE_BROWSED);
   }
 
-  history_service_->AddPageWithDetails(
+  client_->GetHistoryService()->AddPageWithDetails(
       GURL("http://pa/"), base::UTF8ToUTF16("pa"), 0, 0,
       Time::Now() -
-      TimeDelta::FromDays(history::kLowQualityMatchAgeLimitInDays - 1),
+          TimeDelta::FromDays(history::kLowQualityMatchAgeLimitInDays - 1),
       false, history::SOURCE_BROWSED);
 }
 
@@ -548,9 +499,9 @@
     {"http://redirects/C", 10}
   };
   for (size_t i = 0; i < arraysize(test_cases); ++i) {
-    history_service_->AddPageWithDetails(GURL(test_cases[i].url),
-        ASCIIToUTF16("Title"), test_cases[i].count, test_cases[i].count,
-        Time::Now(), false, history::SOURCE_BROWSED);
+    client_->GetHistoryService()->AddPageWithDetails(
+        GURL(test_cases[i].url), ASCIIToUTF16("Title"), test_cases[i].count,
+        test_cases[i].count, Time::Now(), false, history::SOURCE_BROWSED);
   }
 
   // Create a B->C->A redirect chain, but set the visit counts such that they
@@ -561,9 +512,9 @@
   redirects_to_a.push_back(GURL(test_cases[1].url));
   redirects_to_a.push_back(GURL(test_cases[2].url));
   redirects_to_a.push_back(GURL(test_cases[0].url));
-  history_service_->AddPage(GURL(test_cases[0].url), base::Time::Now(),
-      NULL, 0, GURL(), redirects_to_a, ui::PAGE_TRANSITION_TYPED,
-      history::SOURCE_BROWSED, true);
+  client_->GetHistoryService()->AddPage(
+      GURL(test_cases[0].url), base::Time::Now(), NULL, 0, GURL(),
+      redirects_to_a, ui::PAGE_TRANSITION_TYPED, history::SOURCE_BROWSED, true);
 
   // Because all the results are part of a redirect chain with other results,
   // all but the first one (A) should be culled. We should get the default
@@ -697,7 +648,8 @@
 // second passes.
 TEST_F(HistoryURLProviderTest, EmptyVisits) {
   // Wait for history to create the in memory DB.
-  BlockUntilHistoryProcessesPendingRequests();
+  history::BlockUntilHistoryProcessesPendingRequests(
+      client_->GetHistoryService());
 
   AutocompleteInput input(
       ASCIIToUTF16("pa"), base::string16::npos, std::string(), GURL(),
@@ -973,9 +925,10 @@
     {"http://foobar.com/", 10}
   };
   for (size_t i = 0; i < arraysize(test_cases); ++i) {
-    history_service_->AddPageWithDetails(GURL(test_cases[i].url),
-        base::UTF8ToUTF16("Title"), test_cases[i].count, test_cases[i].count,
-        Time::Now(), false, history::SOURCE_BROWSED);
+    client_->GetHistoryService()->AddPageWithDetails(
+        GURL(test_cases[i].url), base::UTF8ToUTF16("Title"),
+        test_cases[i].count, test_cases[i].count, Time::Now(), false,
+        history::SOURCE_BROWSED);
   }
 
   // We should not see search URLs when typing a previously used query.
diff --git a/components/omnibox/browser/in_memory_url_index_test_util.cc b/components/omnibox/browser/in_memory_url_index_test_util.cc
new file mode 100644
index 0000000..cdbd629b
--- /dev/null
+++ b/components/omnibox/browser/in_memory_url_index_test_util.cc
@@ -0,0 +1,20 @@
+// 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/omnibox/browser/in_memory_url_index_test_util.h"
+
+#include "base/run_loop.h"
+#include "components/omnibox/browser/history_index_restore_observer.h"
+#include "components/omnibox/browser/in_memory_url_index.h"
+
+void BlockUntilInMemoryURLIndexIsRefreshed(InMemoryURLIndex* index) {
+  if (!index || index->restored())
+    return;
+  base::RunLoop run_loop;
+  HistoryIndexRestoreObserver observer(run_loop.QuitClosure());
+  index->set_restore_cache_observer(&observer);
+  run_loop.Run();
+  index->set_restore_cache_observer(nullptr);
+  DCHECK(index->restored());
+}
diff --git a/components/omnibox/browser/in_memory_url_index_test_util.h b/components/omnibox/browser/in_memory_url_index_test_util.h
new file mode 100644
index 0000000..42e3d10ed
--- /dev/null
+++ b/components/omnibox/browser/in_memory_url_index_test_util.h
@@ -0,0 +1,14 @@
+// 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_OMNIBOX_BROWSER_IN_MEMORY_URL_INDEX_TEST_UTIL_H_
+#define COMPONENTS_OMNIBOX_BROWSER_IN_MEMORY_URL_INDEX_TEST_UTIL_H_
+
+class InMemoryURLIndex;
+
+// If the given |index| is in the process of restoring, blocks until the restore
+// is complete.
+void BlockUntilInMemoryURLIndexIsRefreshed(InMemoryURLIndex* index);
+
+#endif  // COMPONENTS_OMNIBOX_BROWSER_IN_MEMORY_URL_INDEX_TEST_UTIL_H_