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/message_loop/message_loop_proxy.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
10 #include "chrome/test/base/testing_pref_service_syncable.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/search_engines/default_search_pref_test_util.h"
13 #include "components/search_engines/keyword_table.h"
14 #include "components/search_engines/keyword_web_data_service.h"
15 #include "components/search_engines/template_url_service.h"
16 #include "components/search_engines/testing_search_terms_data.h"
17 #include "components/webdata/common/web_database_service.h"
18 #include "testing/gtest/include/gtest/gtest.h"
22 class TestingTemplateURLServiceClient : public ChromeTemplateURLServiceClient {
24 TestingTemplateURLServiceClient(Profile* profile,
25 base::string16* search_term)
26 : ChromeTemplateURLServiceClient(profile),
27 search_term_(search_term) {}
29 virtual void SetKeywordSearchTermsForURL(
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::MessageLoopProxy::current(),
54 base::MessageLoopProxy::current());
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::MessageLoopProxy::current(),
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>(
118 new TestingTemplateURLServiceClient(profile(), &search_term_)),
119 NULL, NULL, base::Closure()));
120 model()->AddObserver(this);
126 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
127 base::string16 search_term;
128 search_term.swap(search_term_);
132 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) {
133 DCHECK(base_url.is_valid());
134 search_terms_data_->set_google_base_url(base_url.spec());
135 model_->GoogleBaseURLChanged();
138 void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences(
140 const std::string& name,
141 const std::string& keyword,
142 const std::string& search_url,
143 const std::string& suggest_url,
144 const std::string& icon_url,
145 const std::string& encodings,
146 const std::string& alternate_url,
147 const std::string& search_terms_replacement_key) {
148 DefaultSearchPrefTestUtil::SetManagedPref(
149 profile()->GetTestingPrefService(),
150 enabled, name, keyword, search_url, suggest_url, icon_url, encodings,
151 alternate_url, search_terms_replacement_key);
154 void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() {
155 DefaultSearchPrefTestUtil::RemoveManagedPref(
156 profile()->GetTestingPrefService());