[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / chrome / browser / search_engines / template_url_service_test_util.cc
blobc5e15e33a436c0bbb3ff72d20f895a16086aef68
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/bind.h"
8 #include "base/run_loop.h"
9 #include "base/threading/thread.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/google/google_url_tracker.h"
12 #include "chrome/browser/search_engines/search_terms_data.h"
13 #include "chrome/browser/search_engines/template_url_service.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h"
15 #include "chrome/browser/webdata/web_data_service_factory.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_pref_service_syncable.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "content/public/browser/notification_service.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/google/google_util_chromeos.h"
24 #endif
26 // Trivial subclass of TemplateURLService that records the last invocation of
27 // SetKeywordSearchTermsForURL.
28 class TestingTemplateURLService : public TemplateURLService {
29 public:
30 static KeyedService* Build(content::BrowserContext* profile) {
31 return new TestingTemplateURLService(static_cast<Profile*>(profile));
34 explicit TestingTemplateURLService(Profile* profile)
35 : TemplateURLService(profile) {
38 base::string16 GetAndClearSearchTerm() {
39 base::string16 search_term;
40 search_term.swap(search_term_);
41 return search_term;
44 protected:
45 virtual void SetKeywordSearchTermsForURL(
46 const TemplateURL* t_url,
47 const GURL& url,
48 const base::string16& term) OVERRIDE {
49 search_term_ = term;
52 private:
53 base::string16 search_term_;
55 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService);
58 // TemplateURLServiceTestUtilBase ---------------------------------------------
60 TemplateURLServiceTestUtilBase::TemplateURLServiceTestUtilBase()
61 : changed_count_(0) {
64 TemplateURLServiceTestUtilBase::~TemplateURLServiceTestUtilBase() {
67 void TemplateURLServiceTestUtilBase::CreateTemplateUrlService() {
68 profile()->CreateWebDataService();
70 TemplateURLService* service = static_cast<TemplateURLService*>(
71 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
72 profile(), TestingTemplateURLService::Build));
73 service->AddObserver(this);
76 void TemplateURLServiceTestUtilBase::OnTemplateURLServiceChanged() {
77 changed_count_++;
80 int TemplateURLServiceTestUtilBase::GetObserverCount() {
81 return changed_count_;
84 void TemplateURLServiceTestUtilBase::ResetObserverCount() {
85 changed_count_ = 0;
88 void TemplateURLServiceTestUtilBase::VerifyLoad() {
89 ASSERT_FALSE(model()->loaded());
90 model()->Load();
91 base::RunLoop().RunUntilIdle();
92 EXPECT_EQ(1, GetObserverCount());
93 ResetObserverCount();
96 void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() {
97 model()->ChangeToLoadedState();
98 // Initialize the web data service so that the database gets updated with
99 // any changes made.
101 model()->service_ = WebDataService::FromBrowserContext(profile());
102 base::RunLoop().RunUntilIdle();
105 void TemplateURLServiceTestUtilBase::ClearModel() {
106 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
107 profile(), NULL);
110 void TemplateURLServiceTestUtilBase::ResetModel(bool verify_load) {
111 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
112 profile(), TestingTemplateURLService::Build);
113 model()->AddObserver(this);
114 changed_count_ = 0;
115 if (verify_load)
116 VerifyLoad();
119 base::string16 TemplateURLServiceTestUtilBase::GetAndClearSearchTerm() {
120 return
121 static_cast<TestingTemplateURLService*>(model())->GetAndClearSearchTerm();
124 void TemplateURLServiceTestUtilBase::SetGoogleBaseURL(
125 const GURL& base_url) const {
126 DCHECK(base_url.is_valid());
127 UIThreadSearchTermsData data(profile());
128 GoogleURLTracker::UpdatedDetails urls(GURL(data.GoogleBaseURLValue()),
129 base_url);
130 UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec());
131 content::NotificationService::current()->Notify(
132 chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
133 content::Source<Profile>(profile()),
134 content::Details<GoogleURLTracker::UpdatedDetails>(&urls));
137 void TemplateURLServiceTestUtilBase::SetManagedDefaultSearchPreferences(
138 bool enabled,
139 const std::string& name,
140 const std::string& keyword,
141 const std::string& search_url,
142 const std::string& suggest_url,
143 const std::string& icon_url,
144 const std::string& encodings,
145 const std::string& alternate_url,
146 const std::string& search_terms_replacement_key) {
147 if (enabled) {
148 EXPECT_FALSE(keyword.empty());
149 EXPECT_FALSE(search_url.empty());
151 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
152 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled,
153 base::Value::CreateBooleanValue(enabled));
154 pref_service->SetManagedPref(prefs::kDefaultSearchProviderName,
155 base::Value::CreateStringValue(name));
156 pref_service->SetManagedPref(prefs::kDefaultSearchProviderKeyword,
157 base::Value::CreateStringValue(keyword));
158 pref_service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL,
159 base::Value::CreateStringValue(search_url));
160 pref_service->SetManagedPref(prefs::kDefaultSearchProviderSuggestURL,
161 base::Value::CreateStringValue(suggest_url));
162 pref_service->SetManagedPref(prefs::kDefaultSearchProviderIconURL,
163 base::Value::CreateStringValue(icon_url));
164 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEncodings,
165 base::Value::CreateStringValue(encodings));
166 scoped_ptr<base::ListValue> alternate_url_list(new base::ListValue());
167 if (!alternate_url.empty())
168 alternate_url_list->Append(base::Value::CreateStringValue(alternate_url));
169 pref_service->SetManagedPref(prefs::kDefaultSearchProviderAlternateURLs,
170 alternate_url_list.release());
171 pref_service->SetManagedPref(
172 prefs::kDefaultSearchProviderSearchTermsReplacementKey,
173 base::Value::CreateStringValue(search_terms_replacement_key));
174 model()->Observe(chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED,
175 content::NotificationService::AllSources(),
176 content::NotificationService::NoDetails());
179 void TemplateURLServiceTestUtilBase::RemoveManagedDefaultSearchPreferences() {
180 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
181 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEnabled);
182 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderName);
183 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderKeyword);
184 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSearchURL);
185 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSuggestURL);
186 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderIconURL);
187 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEncodings);
188 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderAlternateURLs);
189 pref_service->RemoveManagedPref(
190 prefs::kDefaultSearchProviderSearchTermsReplacementKey);
191 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderID);
192 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderPrepopulateID);
193 model()->Observe(chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED,
194 content::NotificationService::AllSources(),
195 content::NotificationService::NoDetails());
198 TemplateURLService* TemplateURLServiceTestUtilBase::model() const {
199 return TemplateURLServiceFactory::GetForProfile(profile());
203 // TemplateURLServiceTestUtil -------------------------------------------------
205 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
206 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
209 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
212 void TemplateURLServiceTestUtil::SetUp() {
213 // Make unique temp directory.
214 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
215 profile_.reset(new TestingProfile(temp_dir_.path()));
217 TemplateURLServiceTestUtilBase::CreateTemplateUrlService();
219 #if defined(OS_CHROMEOS)
220 google_util::chromeos::ClearBrandForCurrentSession();
221 #endif
224 void TemplateURLServiceTestUtil::TearDown() {
225 profile_.reset();
227 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
229 // Flush the message loop to make application verifiers happy.
230 base::RunLoop().RunUntilIdle();
233 TestingProfile* TemplateURLServiceTestUtil::profile() const {
234 return profile_.get();