Revert "Create a platform-independent ToolbarActionsBar"
[chromium-blink-merge.git] / chrome / browser / search_engines / template_url_service_test_util.cc
blob1d3748c1fde0733719ab108207133ee221274c47
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(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>(
118 new TestingTemplateURLServiceClient(
119 HistoryServiceFactory::GetForProfileIfExists(
120 profile(), Profile::EXPLICIT_ACCESS),
121 &search_term_)),
122 NULL, NULL, base::Closure()));
123 model()->AddObserver(this);
124 changed_count_ = 0;
125 if (verify_load)
126 VerifyLoad();
129 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
130 base::string16 search_term;
131 search_term.swap(search_term_);
132 return search_term;
135 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) {
136 DCHECK(base_url.is_valid());
137 search_terms_data_->set_google_base_url(base_url.spec());
138 model_->GoogleBaseURLChanged();
141 void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences(
142 bool enabled,
143 const std::string& name,
144 const std::string& keyword,
145 const std::string& search_url,
146 const std::string& suggest_url,
147 const std::string& icon_url,
148 const std::string& encodings,
149 const std::string& alternate_url,
150 const std::string& search_terms_replacement_key) {
151 DefaultSearchPrefTestUtil::SetManagedPref(
152 profile()->GetTestingPrefService(),
153 enabled, name, keyword, search_url, suggest_url, icon_url, encodings,
154 alternate_url, search_terms_replacement_key);
157 void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() {
158 DefaultSearchPrefTestUtil::RemoveManagedPref(
159 profile()->GetTestingPrefService());