template_url_service_test_util.cc revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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. 4 5#include "chrome/browser/search_engines/template_url_service_test_util.h" 6 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" 21 22#if defined(OS_CHROMEOS) 23#include "chrome/browser/google/google_util_chromeos.h" 24#endif 25 26// Trivial subclass of TemplateURLService that records the last invocation of 27// SetKeywordSearchTermsForURL. 28class TestingTemplateURLService : public TemplateURLService { 29 public: 30 static KeyedService* Build(content::BrowserContext* profile) { 31 return new TestingTemplateURLService(static_cast<Profile*>(profile)); 32 } 33 34 explicit TestingTemplateURLService(Profile* profile) 35 : TemplateURLService(profile) { 36 } 37 38 base::string16 GetAndClearSearchTerm() { 39 base::string16 search_term; 40 search_term.swap(search_term_); 41 return search_term; 42 } 43 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; 50 } 51 52 private: 53 base::string16 search_term_; 54 55 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); 56}; 57 58// TemplateURLServiceTestUtilBase --------------------------------------------- 59 60TemplateURLServiceTestUtilBase::TemplateURLServiceTestUtilBase() 61 : changed_count_(0) { 62} 63 64TemplateURLServiceTestUtilBase::~TemplateURLServiceTestUtilBase() { 65} 66 67void TemplateURLServiceTestUtilBase::CreateTemplateUrlService() { 68 profile()->CreateWebDataService(); 69 70 TemplateURLService* service = static_cast<TemplateURLService*>( 71 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 72 profile(), TestingTemplateURLService::Build)); 73 service->AddObserver(this); 74} 75 76void TemplateURLServiceTestUtilBase::OnTemplateURLServiceChanged() { 77 changed_count_++; 78} 79 80int TemplateURLServiceTestUtilBase::GetObserverCount() { 81 return changed_count_; 82} 83 84void TemplateURLServiceTestUtilBase::ResetObserverCount() { 85 changed_count_ = 0; 86} 87 88void TemplateURLServiceTestUtilBase::VerifyLoad() { 89 ASSERT_FALSE(model()->loaded()); 90 model()->Load(); 91 base::RunLoop().RunUntilIdle(); 92 EXPECT_EQ(1, GetObserverCount()); 93 ResetObserverCount(); 94} 95 96void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() { 97 model()->ChangeToLoadedState(); 98 // Initialize the web data service so that the database gets updated with 99 // any changes made. 100 101 model()->service_ = WebDataService::FromBrowserContext(profile()); 102 base::RunLoop().RunUntilIdle(); 103} 104 105void TemplateURLServiceTestUtilBase::ClearModel() { 106 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( 107 profile(), NULL); 108} 109 110void 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(); 117} 118 119base::string16 TemplateURLServiceTestUtilBase::GetAndClearSearchTerm() { 120 return 121 static_cast<TestingTemplateURLService*>(model())->GetAndClearSearchTerm(); 122} 123 124void 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)); 135} 136 137void 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()); 150 } 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()); 177} 178 179void 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()); 196} 197 198TemplateURLService* TemplateURLServiceTestUtilBase::model() const { 199 return TemplateURLServiceFactory::GetForProfile(profile()); 200} 201 202 203// TemplateURLServiceTestUtil ------------------------------------------------- 204 205TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() 206 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 207} 208 209TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { 210} 211 212void TemplateURLServiceTestUtil::SetUp() { 213 // Make unique temp directory. 214 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 215 profile_.reset(new TestingProfile(temp_dir_.path())); 216 217 TemplateURLServiceTestUtilBase::CreateTemplateUrlService(); 218 219#if defined(OS_CHROMEOS) 220 google_util::chromeos::ClearBrandForCurrentSession(); 221#endif 222} 223 224void TemplateURLServiceTestUtil::TearDown() { 225 profile_.reset(); 226 227 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); 228 229 // Flush the message loop to make application verifiers happy. 230 base::RunLoop().RunUntilIdle(); 231} 232 233TestingProfile* TemplateURLServiceTestUtil::profile() const { 234 return profile_.get(); 235} 236