Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / search_engines / template_url_service_test_util.cc
blobb7e3e2c3eee953a866f9e4fecaa849a2032a48d8
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/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"
21 namespace {
23 class TestingTemplateURLServiceClient : public ChromeTemplateURLServiceClient {
24 public:
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,
31 TemplateURLID id,
32 const base::string16& term) override {
33 *search_term_ = term;
36 private:
37 base::string16* search_term_;
39 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLServiceClient);
42 } // namespace
44 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
45 : changed_count_(0),
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();
64 ResetModel(false);
67 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
68 ClearModel();
69 profile_.reset();
71 // Flush the message loop to make application verifiers happy.
72 base::RunLoop().RunUntilIdle();
75 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
76 changed_count_++;
79 int TemplateURLServiceTestUtil::GetObserverCount() {
80 return changed_count_;
83 void TemplateURLServiceTestUtil::ResetObserverCount() {
84 changed_count_ = 0;
87 void TemplateURLServiceTestUtil::VerifyLoad() {
88 ASSERT_FALSE(model()->loaded());
89 model()->Load();
90 base::RunLoop().RunUntilIdle();
91 EXPECT_EQ(1, GetObserverCount());
92 ResetObserverCount();
95 void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
96 model()->ChangeToLoadedState();
97 // Initialize the web data service so that the database gets updated with
98 // any changes made.
100 model()->web_data_service_ = web_data_service_;
101 base::RunLoop().RunUntilIdle();
104 void TemplateURLServiceTestUtil::ClearModel() {
105 model_->Shutdown();
106 model_.reset();
107 search_terms_data_ = NULL;
110 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
111 if (model_)
112 ClearModel();
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),
120 &search_term_)),
121 NULL, NULL, base::Closure()));
122 model()->AddObserver(this);
123 changed_count_ = 0;
124 if (verify_load)
125 VerifyLoad();
128 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
129 base::string16 search_term;
130 search_term.swap(search_term_);
131 return 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(
141 bool enabled,
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());