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
3 // found in the LICENSE file.
5 #include "chrome/browser/search_engines/template_url_service_test_util.h"
7 #include "base/run_loop.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "chrome/browser/history/history_service_factory.h"
10 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
11 #include "chrome/test/base/testing_pref_service_syncable.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/search_engines/default_search_pref_test_util.h"
14 #include "components/search_engines/keyword_table.h"
15 #include "components/search_engines/keyword_web_data_service.h"
16 #include "components/search_engines/template_url_service.h"
17 #include "components/search_engines/testing_search_terms_data.h"
18 #include "components/webdata/common/web_database_service.h"
19 #include "testing/gtest/include/gtest/gtest.h"
23 class TestingTemplateURLServiceClient
: public ChromeTemplateURLServiceClient
{
25 TestingTemplateURLServiceClient(history::HistoryService
* history_service
,
26 base::string16
* search_term
)
27 : ChromeTemplateURLServiceClient(history_service
),
28 search_term_(search_term
) {}
30 void SetKeywordSearchTermsForURL(const GURL
& url
,
32 const base::string16
& term
) override
{
37 base::string16
* search_term_
;
39 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLServiceClient
);
44 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
46 search_terms_data_(NULL
) {
47 // Make unique temp directory.
48 EXPECT_TRUE(temp_dir_
.CreateUniqueTempDir());
49 profile_
.reset(new TestingProfile(temp_dir_
.path()));
51 scoped_refptr
<WebDatabaseService
> web_database_service
=
52 new WebDatabaseService(temp_dir_
.path().AppendASCII("webdata"),
53 base::ThreadTaskRunnerHandle::Get(),
54 base::ThreadTaskRunnerHandle::Get());
55 web_database_service
->AddTable(
56 scoped_ptr
<WebDatabaseTable
>(new KeywordTable()));
57 web_database_service
->LoadDatabase();
59 web_data_service_
= new KeywordWebDataService(
60 web_database_service
.get(), base::ThreadTaskRunnerHandle::Get(),
61 KeywordWebDataService::ProfileErrorCallback());
62 web_data_service_
->Init();
67 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
71 // Flush the message loop to make application verifiers happy.
72 base::RunLoop().RunUntilIdle();
75 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
79 int TemplateURLServiceTestUtil::GetObserverCount() {
80 return changed_count_
;
83 void TemplateURLServiceTestUtil::ResetObserverCount() {
87 void TemplateURLServiceTestUtil::VerifyLoad() {
88 ASSERT_FALSE(model()->loaded());
90 base::RunLoop().RunUntilIdle();
91 EXPECT_EQ(1, GetObserverCount());
95 void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
96 model()->ChangeToLoadedState();
97 // Initialize the web data service so that the database gets updated with
100 model()->web_data_service_
= web_data_service_
;
101 base::RunLoop().RunUntilIdle();
104 void TemplateURLServiceTestUtil::ClearModel() {
107 search_terms_data_
= NULL
;
110 void TemplateURLServiceTestUtil::ResetModel(bool verify_load
) {
113 search_terms_data_
= new TestingSearchTermsData("http://www.google.com/");
114 model_
.reset(new TemplateURLService(
115 profile()->GetPrefs(), scoped_ptr
<SearchTermsData
>(search_terms_data_
),
116 web_data_service_
.get(),
117 scoped_ptr
<TemplateURLServiceClient
>(new TestingTemplateURLServiceClient(
118 HistoryServiceFactory::GetForProfileIfExists(
119 profile(), ServiceAccessType::EXPLICIT_ACCESS
),
121 NULL
, NULL
, base::Closure()));
122 model()->AddObserver(this);
128 base::string16
TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
129 base::string16 search_term
;
130 search_term
.swap(search_term_
);
134 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL
& base_url
) {
135 DCHECK(base_url
.is_valid());
136 search_terms_data_
->set_google_base_url(base_url
.spec());
137 model_
->GoogleBaseURLChanged();
140 void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences(
142 const std::string
& name
,
143 const std::string
& keyword
,
144 const std::string
& search_url
,
145 const std::string
& suggest_url
,
146 const std::string
& icon_url
,
147 const std::string
& encodings
,
148 const std::string
& alternate_url
,
149 const std::string
& search_terms_replacement_key
) {
150 DefaultSearchPrefTestUtil::SetManagedPref(
151 profile()->GetTestingPrefService(),
152 enabled
, name
, keyword
, search_url
, suggest_url
, icon_url
, encodings
,
153 alternate_url
, search_terms_replacement_key
);
156 void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() {
157 DefaultSearchPrefTestUtil::RemoveManagedPref(
158 profile()->GetTestingPrefService());