Commit 83e906aa authored by rohitrao's avatar rohitrao Committed by Commit bot

Componentizes history_quick_provider_unittest.cc.

Removes dependencies on chrome/ and content/ and moves the file to
components/omnibox/browser.

BUG=581108
TEST=None

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

Cr-Commit-Position: refs/heads/master@{#371784}
parent fc36ae93
...@@ -32,7 +32,6 @@ ...@@ -32,7 +32,6 @@
'browser/android/thumbnail/scoped_ptr_expiring_cache_unittest.cc', 'browser/android/thumbnail/scoped_ptr_expiring_cache_unittest.cc',
'browser/app_controller_mac_unittest.mm', 'browser/app_controller_mac_unittest.mm',
'browser/autocomplete/builtin_provider_unittest.cc', 'browser/autocomplete/builtin_provider_unittest.cc',
'browser/autocomplete/history_quick_provider_unittest.cc',
'browser/autocomplete/in_memory_url_index_unittest.cc', 'browser/autocomplete/in_memory_url_index_unittest.cc',
'browser/autocomplete/search_provider_unittest.cc', 'browser/autocomplete/search_provider_unittest.cc',
'browser/autocomplete/shortcuts_backend_unittest.cc', 'browser/autocomplete/shortcuts_backend_unittest.cc',
......
...@@ -412,6 +412,7 @@ ...@@ -412,6 +412,7 @@
'omnibox/browser/base_search_provider_unittest.cc', 'omnibox/browser/base_search_provider_unittest.cc',
'omnibox/browser/bookmark_provider_unittest.cc', 'omnibox/browser/bookmark_provider_unittest.cc',
'omnibox/browser/clipboard_url_provider_unittest.cc', 'omnibox/browser/clipboard_url_provider_unittest.cc',
'omnibox/browser/history_quick_provider_unittest.cc',
'omnibox/browser/history_url_provider_unittest.cc', 'omnibox/browser/history_url_provider_unittest.cc',
'omnibox/browser/in_memory_url_index_types_unittest.cc', 'omnibox/browser/in_memory_url_index_types_unittest.cc',
'omnibox/browser/keyword_provider_unittest.cc', 'omnibox/browser/keyword_provider_unittest.cc',
......
...@@ -188,6 +188,7 @@ source_set("unit_tests") { ...@@ -188,6 +188,7 @@ source_set("unit_tests") {
"base_search_provider_unittest.cc", "base_search_provider_unittest.cc",
"bookmark_provider_unittest.cc", "bookmark_provider_unittest.cc",
"clipboard_url_provider_unittest.cc", "clipboard_url_provider_unittest.cc",
"history_quick_provider_unittest.cc",
"history_url_provider_unittest.cc", "history_url_provider_unittest.cc",
"in_memory_url_index_types_unittest.cc", "in_memory_url_index_types_unittest.cc",
"keyword_provider_unittest.cc", "keyword_provider_unittest.cc",
...@@ -206,6 +207,7 @@ source_set("unit_tests") { ...@@ -206,6 +207,7 @@ source_set("unit_tests") {
"//base", "//base",
"//base:prefs", "//base:prefs",
"//base:prefs_test_support", "//base:prefs_test_support",
"//base/test:test_support",
"//components/bookmarks/browser", "//components/bookmarks/browser",
"//components/bookmarks/test", "//components/bookmarks/test",
"//components/history/core/test", "//components/history/core/test",
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/files/scoped_temp_dir.h"
#include "base/format_macros.h" #include "base/format_macros.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
...@@ -19,32 +20,29 @@ ...@@ -19,32 +20,29 @@
#include "base/prefs/pref_service.h" #include "base/prefs/pref_service.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" #include "base/test/sequenced_worker_pool_owner.h"
#include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" #include "components/bookmarks/browser/bookmark_model.h"
#include "chrome/browser/autocomplete/in_memory_url_index_factory.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/search_engines/chrome_template_url_service_client.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/bookmarks/test/bookmark_test_helpers.h" #include "components/bookmarks/test/bookmark_test_helpers.h"
#include "components/bookmarks/test/test_bookmark_client.h"
#include "components/history/core/browser/history_backend.h" #include "components/history/core/browser/history_backend.h"
#include "components/history/core/browser/history_database.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.h"
#include "components/history/core/browser/history_service_observer.h" #include "components/history/core/browser/history_service_observer.h"
#include "components/history/core/browser/url_database.h" #include "components/history/core/browser/url_database.h"
#include "components/history/core/test/test_history_database.h"
#include "components/metrics/proto/omnibox_event.pb.h" #include "components/metrics/proto/omnibox_event.pb.h"
#include "components/omnibox/browser/autocomplete_match.h" #include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/autocomplete_result.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/history_url_provider.h"
#include "components/omnibox/browser/in_memory_url_index.h" #include "components/omnibox/browser/in_memory_url_index.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" #include "components/omnibox/browser/url_index_private_data.h"
#include "components/search_engines/search_terms_data.h" #include "components/search_engines/search_terms_data.h"
#include "components/search_engines/template_url.h" #include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h" #include "components/search_engines/template_url_service.h"
#include "content/public/test/test_browser_thread.h"
#include "content/public/test/test_utils.h"
#include "sql/transaction.h" #include "sql/transaction.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -52,8 +50,6 @@ using base::ASCIIToUTF16; ...@@ -52,8 +50,6 @@ using base::ASCIIToUTF16;
using base::Time; using base::Time;
using base::TimeDelta; using base::TimeDelta;
using content::BrowserThread;
namespace { namespace {
struct TestURLInfo { struct TestURLInfo {
...@@ -183,13 +179,75 @@ class GetURLTask : public history::HistoryDBTask { ...@@ -183,13 +179,75 @@ class GetURLTask : public history::HistoryDBTask {
DISALLOW_COPY_AND_ASSIGN(GetURLTask); 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() {
bookmark_model_ = bookmark_client_.CreateModel();
set_template_url_service(
make_scoped_ptr(new TemplateURLService(nullptr, 0)));
}
const AutocompleteSchemeClassifier& GetSchemeClassifier() const override {
return scheme_classifier_;
}
const SearchTermsData& GetSearchTermsData() const override {
return search_terms_data_;
}
history::HistoryService* GetHistoryService() override {
return &history_service_;
}
bookmarks::BookmarkModel* GetBookmarkModel() override {
return bookmark_model_.get();
}
InMemoryURLIndex* GetInMemoryURLIndex() override {
return in_memory_url_index_.get();
}
std::string GetAcceptLanguages() const override { return "en,en-US,ko"; }
void set_in_memory_url_index(scoped_ptr<InMemoryURLIndex> index) {
in_memory_url_index_ = std::move(index);
}
private:
bookmarks::TestBookmarkClient bookmark_client_;
scoped_ptr<bookmarks::BookmarkModel> bookmark_model_;
TestSchemeClassifier scheme_classifier_;
SearchTermsData search_terms_data_;
scoped_ptr<InMemoryURLIndex> in_memory_url_index_;
history::HistoryService history_service_;
DISALLOW_COPY_AND_ASSIGN(FakeAutocompleteProviderClient);
};
} // namespace } // namespace
class HistoryQuickProviderTest : public testing::Test { class HistoryQuickProviderTest : public testing::Test {
public: public:
HistoryQuickProviderTest() HistoryQuickProviderTest() : pool_owner_(3, "Background Pool") {}
: ui_thread_(BrowserThread::UI, &message_loop_),
file_thread_(BrowserThread::FILE, &message_loop_) {}
protected: protected:
class SetShouldContain : public std::unary_function<const std::string&, class SetShouldContain : public std::unary_function<const std::string&,
...@@ -205,17 +263,6 @@ class HistoryQuickProviderTest : public testing::Test { ...@@ -205,17 +263,6 @@ class HistoryQuickProviderTest : public testing::Test {
std::set<std::string> matches_; std::set<std::string> matches_;
}; };
static scoped_ptr<KeyedService> CreateTemplateURLService(
content::BrowserContext* context) {
Profile* profile = static_cast<Profile*>(context);
return make_scoped_ptr(new TemplateURLService(
profile->GetPrefs(), make_scoped_ptr(new SearchTermsData), NULL,
scoped_ptr<TemplateURLServiceClient>(new ChromeTemplateURLServiceClient(
HistoryServiceFactory::GetForProfile(
profile, ServiceAccessType::EXPLICIT_ACCESS))),
NULL, NULL, base::Closure()));
}
void SetUp() override; void SetUp() override;
void TearDown() override; void TearDown() override;
...@@ -249,20 +296,23 @@ class HistoryQuickProviderTest : public testing::Test { ...@@ -249,20 +296,23 @@ class HistoryQuickProviderTest : public testing::Test {
// > message_loop(). // > message_loop().
// Direct use of this object in tests is almost certainly not thread-safe. // Direct use of this object in tests is almost certainly not thread-safe.
history::HistoryBackend* history_backend() { history::HistoryBackend* history_backend() {
return history_service_->history_backend_.get(); return client_->GetHistoryService()->history_backend_.get();
} }
// Call history_backend()->GetURL(url, NULL) on the history thread, returning // Call history_backend()->GetURL(url, NULL) on the history thread, returning
// the result. // the result.
bool GetURLProxy(const GURL& url); bool GetURLProxy(const GURL& url);
base::MessageLoopForUI message_loop_; // Helper functions to initialize the HistoryService.
content::TestBrowserThread ui_thread_; bool InitializeHistoryService();
content::TestBrowserThread file_thread_; void CreateInMemoryURLIndex();
void BlockUntilHistoryProcessesPendingRequests();
void BlockUntilHistoryIndexIsRefreshed();
scoped_ptr<TestingProfile> profile_; base::MessageLoop message_loop_;
scoped_ptr<ChromeAutocompleteProviderClient> client_; base::SequencedWorkerPoolOwner pool_owner_;
history::HistoryService* history_service_; base::ScopedTempDir history_dir_;
scoped_ptr<FakeAutocompleteProviderClient> client_;
ACMatches ac_matches_; // The resulting matches after running RunTest. ACMatches ac_matches_; // The resulting matches after running RunTest.
...@@ -270,31 +320,27 @@ class HistoryQuickProviderTest : public testing::Test { ...@@ -270,31 +320,27 @@ class HistoryQuickProviderTest : public testing::Test {
}; };
void HistoryQuickProviderTest::SetUp() { void HistoryQuickProviderTest::SetUp() {
profile_.reset(new TestingProfile()); client_.reset(new FakeAutocompleteProviderClient());
client_.reset(new ChromeAutocompleteProviderClient(profile_.get())); ASSERT_TRUE(InitializeHistoryService());
ASSERT_TRUE(profile_->CreateHistoryService(true, false)); FillData();
profile_->CreateBookmarkModel(true);
bookmarks::test::WaitForBookmarkModelToLoad( // |FillData()| must be called before |CreateInMemoryURLIndex()|. This will
BookmarkModelFactory::GetForProfile(profile_.get())); // ensure that the index is properly populated with data from the database.
profile_->BlockUntilHistoryIndexIsRefreshed(); CreateInMemoryURLIndex();
BlockUntilHistoryIndexIsRefreshed();
// History index refresh creates rebuilt tasks to run on history thread. // History index refresh creates rebuilt tasks to run on history thread.
// Block here to make sure that all of them are complete. // Block here to make sure that all of them are complete.
profile_->BlockUntilHistoryProcessesPendingRequests(); BlockUntilHistoryProcessesPendingRequests();
history_service_ = HistoryServiceFactory::GetForProfile(
profile_.get(), ServiceAccessType::EXPLICIT_ACCESS);
EXPECT_TRUE(history_service_);
provider_ = new HistoryQuickProvider(client_.get()); provider_ = new HistoryQuickProvider(client_.get());
TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
profile_.get(), &HistoryQuickProviderTest::CreateTemplateURLService);
FillData();
InMemoryURLIndex* index =
InMemoryURLIndexFactory::GetForProfile(profile_.get());
EXPECT_TRUE(index);
index->RebuildFromHistory(history_backend()->db());
} }
void HistoryQuickProviderTest::TearDown() { void HistoryQuickProviderTest::TearDown() {
provider_ = NULL; provider_ = NULL;
// The InMemoryURLIndex must be explicitly shut down or it will DCHECK() in
// its destructor.
client_->GetInMemoryURLIndex()->Shutdown();
client_->set_in_memory_url_index(nullptr);
// History index rebuild task is created from main thread during SetUp, // History index rebuild task is created from main thread during SetUp,
// performed on DB thread and must be deleted on main thread. // performed on DB thread and must be deleted on main thread.
// Run main loop to process delete task, to prevent leaks. // Run main loop to process delete task, to prevent leaks.
...@@ -391,7 +437,7 @@ void HistoryQuickProviderTest::RunTestWithCursor( ...@@ -391,7 +437,7 @@ void HistoryQuickProviderTest::RunTestWithCursor(
AutocompleteInput input(text, cursor_position, std::string(), GURL(), AutocompleteInput input(text, cursor_position, std::string(), GURL(),
metrics::OmniboxEventProto::INVALID_SPEC, metrics::OmniboxEventProto::INVALID_SPEC,
prevent_inline_autocomplete, false, true, true, false, prevent_inline_autocomplete, false, true, true, false,
ChromeAutocompleteSchemeClassifier(profile_.get())); TestSchemeClassifier());
provider_->Start(input, false); provider_->Start(input, false);
EXPECT_TRUE(provider_->done()); EXPECT_TRUE(provider_->done());
...@@ -416,6 +462,7 @@ void HistoryQuickProviderTest::RunTestWithCursor( ...@@ -416,6 +462,7 @@ void HistoryQuickProviderTest::RunTestWithCursor(
if (expected_urls.empty()) if (expected_urls.empty())
return; return;
ASSERT_FALSE(ac_matches_.empty());
// Verify that we got the results in the order expected. // Verify that we got the results in the order expected.
int best_score = ac_matches_.begin()->relevance + 1; int best_score = ac_matches_.begin()->relevance + 1;
int i = 0; int i = 0;
...@@ -441,7 +488,7 @@ void HistoryQuickProviderTest::RunTestWithCursor( ...@@ -441,7 +488,7 @@ void HistoryQuickProviderTest::RunTestWithCursor(
bool HistoryQuickProviderTest::GetURLProxy(const GURL& url) { bool HistoryQuickProviderTest::GetURLProxy(const GURL& url) {
base::CancelableTaskTracker task_tracker; base::CancelableTaskTracker task_tracker;
bool result = false; bool result = false;
history_service_->ScheduleDBTask( client_->GetHistoryService()->ScheduleDBTask(
scoped_ptr<history::HistoryDBTask>(new GetURLTask(url, &result)), scoped_ptr<history::HistoryDBTask>(new GetURLTask(url, &result)),
&task_tracker); &task_tracker);
// Run the message loop until GetURLTask::DoneRunOnMainThread stops it. If // Run the message loop until GetURLTask::DoneRunOnMainThread stops it. If
...@@ -450,6 +497,46 @@ bool HistoryQuickProviderTest::GetURLProxy(const GURL& url) { ...@@ -450,6 +497,46 @@ bool HistoryQuickProviderTest::GetURLProxy(const GURL& url) {
return result; 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) { TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) {
std::vector<std::string> expected_urls; std::vector<std::string> expected_urls;
expected_urls.push_back("http://slashdot.org/favorite_page.html"); expected_urls.push_back("http://slashdot.org/favorite_page.html");
...@@ -681,7 +768,7 @@ TEST_F(HistoryQuickProviderTest, DeleteMatch) { ...@@ -681,7 +768,7 @@ TEST_F(HistoryQuickProviderTest, DeleteMatch) {
// InMemoryURLIndex) will drop any data they might have pertaining to the URL. // InMemoryURLIndex) will drop any data they might have pertaining to the URL.
// To ensure that the deletion has been propagated everywhere before we start // To ensure that the deletion has been propagated everywhere before we start
// verifying post-deletion states, first wait until we see the notification. // verifying post-deletion states, first wait until we see the notification.
WaitForURLsDeletedNotification(history_service_); WaitForURLsDeletedNotification(client_->GetHistoryService());
EXPECT_FALSE(GetURLProxy(test_url)); EXPECT_FALSE(GetURLProxy(test_url));
// Just to be on the safe side, explicitly verify that we have deleted enough // Just to be on the safe side, explicitly verify that we have deleted enough
...@@ -796,8 +883,7 @@ TEST_F(HistoryQuickProviderTest, CullSearchResults) { ...@@ -796,8 +883,7 @@ TEST_F(HistoryQuickProviderTest, CullSearchResults) {
data.SetShortName(ASCIIToUTF16("TestEngine")); data.SetShortName(ASCIIToUTF16("TestEngine"));
data.SetKeyword(ASCIIToUTF16("TestEngine")); data.SetKeyword(ASCIIToUTF16("TestEngine"));
data.SetURL("http://testsearch.com/?q={searchTerms}"); data.SetURL("http://testsearch.com/?q={searchTerms}");
TemplateURLService* template_url_service = TemplateURLService* template_url_service = client_->GetTemplateURLService();
TemplateURLServiceFactory::GetForProfile(profile_.get());
TemplateURL* template_url = new TemplateURL(data); TemplateURL* template_url = new TemplateURL(data);
template_url_service->Add(template_url); template_url_service->Add(template_url);
template_url_service->SetUserSelectedDefaultSearchProvider(template_url); template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
...@@ -817,10 +903,10 @@ TEST_F(HistoryQuickProviderTest, CullSearchResults) { ...@@ -817,10 +903,10 @@ TEST_F(HistoryQuickProviderTest, CullSearchResults) {
} }
TEST_F(HistoryQuickProviderTest, DoesNotProvideMatchesOnFocus) { TEST_F(HistoryQuickProviderTest, DoesNotProvideMatchesOnFocus) {
AutocompleteInput input( AutocompleteInput input(ASCIIToUTF16("popularsite"), base::string16::npos,
ASCIIToUTF16("popularsite"), base::string16::npos, std::string(), GURL(), std::string(), GURL(),
metrics::OmniboxEventProto::INVALID_SPEC, false, false, true, true, true, metrics::OmniboxEventProto::INVALID_SPEC, false,
ChromeAutocompleteSchemeClassifier(profile_.get())); false, true, true, true, TestSchemeClassifier());
provider_->Start(input, false); provider_->Start(input, false);
EXPECT_TRUE(provider_->matches().empty()); EXPECT_TRUE(provider_->matches().empty());
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment