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"
8 #include "base/run_loop.h"
9 #include "base/strings/string_split.h"
10 #include "base/threading/thread.h"
11 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
12 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
13 #include "chrome/browser/webdata/web_data_service_factory.h"
14 #include "chrome/test/base/testing_pref_service_syncable.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "components/google/core/browser/google_url_tracker.h"
17 #include "components/search_engines/default_search_manager.h"
18 #include "components/search_engines/default_search_pref_test_util.h"
19 #include "components/search_engines/template_url_service.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/google/google_brand_chromeos.h"
26 // Trivial subclass of TemplateURLService that records the last invocation of
27 // SetKeywordSearchTermsForURL.
28 class TestingTemplateURLService : public TemplateURLService {
30 explicit TestingTemplateURLService(Profile* profile)
33 scoped_ptr<SearchTermsData>(new UIThreadSearchTermsData(profile)),
34 WebDataServiceFactory::GetKeywordWebDataForProfile(
35 profile, Profile::EXPLICIT_ACCESS),
36 scoped_ptr<TemplateURLServiceClient>(
37 new ChromeTemplateURLServiceClient(profile)), NULL, NULL,
41 base::string16 GetAndClearSearchTerm() {
42 base::string16 search_term;
43 search_term.swap(search_term_);
48 virtual void SetKeywordSearchTermsForURL(
49 const TemplateURL* t_url,
51 const base::string16& term) OVERRIDE {
56 base::string16 search_term_;
58 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService);
62 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
63 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
65 // Make unique temp directory.
66 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
67 profile_.reset(new TestingProfile(temp_dir_.path()));
69 profile()->CreateWebDataService();
71 model_.reset(new TestingTemplateURLService(profile()));
72 model_->AddObserver(this);
74 #if defined(OS_CHROMEOS)
75 google_brand::chromeos::ClearBrandForCurrentSession();
79 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
83 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
85 // Flush the message loop to make application verifiers happy.
86 base::RunLoop().RunUntilIdle();
89 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
93 int TemplateURLServiceTestUtil::GetObserverCount() {
94 return changed_count_;
97 void TemplateURLServiceTestUtil::ResetObserverCount() {
101 void TemplateURLServiceTestUtil::VerifyLoad() {
102 ASSERT_FALSE(model()->loaded());
104 base::RunLoop().RunUntilIdle();
105 EXPECT_EQ(1, GetObserverCount());
106 ResetObserverCount();
109 void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
110 model()->ChangeToLoadedState();
111 // Initialize the web data service so that the database gets updated with
114 model()->web_data_service_ =
115 WebDataServiceFactory::GetKeywordWebDataForProfile(
116 profile(), Profile::EXPLICIT_ACCESS);
117 base::RunLoop().RunUntilIdle();
120 void TemplateURLServiceTestUtil::ClearModel() {
125 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
128 model_.reset(new TestingTemplateURLService(profile()));
129 model()->AddObserver(this);
135 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
136 return model_->GetAndClearSearchTerm();
139 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) {
140 DCHECK(base_url.is_valid());
141 UIThreadSearchTermsData data(profile());
142 UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec());
143 model_->GoogleBaseURLChanged();
146 void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences(
148 const std::string& name,
149 const std::string& keyword,
150 const std::string& search_url,
151 const std::string& suggest_url,
152 const std::string& icon_url,
153 const std::string& encodings,
154 const std::string& alternate_url,
155 const std::string& search_terms_replacement_key) {
156 DefaultSearchPrefTestUtil::SetManagedPref(
157 profile()->GetTestingPrefService(),
158 enabled, name, keyword, search_url, suggest_url, icon_url, encodings,
159 alternate_url, search_terms_replacement_key);
162 void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() {
163 DefaultSearchPrefTestUtil::RemoveManagedPref(
164 profile()->GetTestingPrefService());
167 TemplateURLService* TemplateURLServiceTestUtil::model() {