Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: chrome/browser/search_engines/template_url_service_test_util.cc

Issue 376413002: Stop using TemplateURLServiceTestUtil to initialize TemplateURLServiceFactory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add DefaultSearchPrefTestUtil Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/search_engines/template_url_service_test_util.h" 5 #include "chrome/browser/search_engines/template_url_service_test_util.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/string_split.h" 9 #include "base/strings/string_split.h"
10 #include "base/threading/thread.h" 10 #include "base/threading/thread.h"
11 #include "chrome/browser/search_engines/chrome_template_url_service_client.h" 11 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
12 #include "chrome/browser/search_engines/template_url_service_factory.h"
13 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" 12 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
14 #include "chrome/browser/webdata/web_data_service_factory.h" 13 #include "chrome/browser/webdata/web_data_service_factory.h"
15 #include "chrome/test/base/testing_pref_service_syncable.h" 14 #include "chrome/test/base/testing_pref_service_syncable.h"
16 #include "chrome/test/base/testing_profile.h" 15 #include "chrome/test/base/testing_profile.h"
17 #include "components/google/core/browser/google_url_tracker.h" 16 #include "components/google/core/browser/google_url_tracker.h"
18 #include "components/search_engines/default_search_manager.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" 19 #include "components/search_engines/template_url_service.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 #if defined(OS_CHROMEOS) 22 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/google/google_brand_chromeos.h" 23 #include "chrome/browser/google/google_brand_chromeos.h"
24 #endif 24 #endif
25 25
26 // Trivial subclass of TemplateURLService that records the last invocation of 26 // Trivial subclass of TemplateURLService that records the last invocation of
27 // SetKeywordSearchTermsForURL. 27 // SetKeywordSearchTermsForURL.
28 class TestingTemplateURLService : public TemplateURLService { 28 class TestingTemplateURLService : public TemplateURLService {
29 public: 29 public:
30 static KeyedService* Build(content::BrowserContext* profile) {
31 return new TestingTemplateURLService(static_cast<Profile*>(profile));
32 }
33
34 explicit TestingTemplateURLService(Profile* profile) 30 explicit TestingTemplateURLService(Profile* profile)
35 : TemplateURLService( 31 : TemplateURLService(
36 profile->GetPrefs(), 32 profile->GetPrefs(),
37 scoped_ptr<SearchTermsData>(new UIThreadSearchTermsData(profile)), 33 scoped_ptr<SearchTermsData>(new UIThreadSearchTermsData(profile)),
38 WebDataServiceFactory::GetKeywordWebDataForProfile( 34 WebDataServiceFactory::GetKeywordWebDataForProfile(
39 profile, Profile::EXPLICIT_ACCESS), 35 profile, Profile::EXPLICIT_ACCESS),
40 scoped_ptr<TemplateURLServiceClient>( 36 scoped_ptr<TemplateURLServiceClient>(
41 new ChromeTemplateURLServiceClient(profile)), NULL, NULL, 37 new ChromeTemplateURLServiceClient(profile)), NULL, NULL,
42 base::Closure()) { 38 base::Closure()) {
43 } 39 }
(...skipping 11 matching lines...) Expand all
55 const base::string16& term) OVERRIDE { 51 const base::string16& term) OVERRIDE {
56 search_term_ = term; 52 search_term_ = term;
57 } 53 }
58 54
59 private: 55 private:
60 base::string16 search_term_; 56 base::string16 search_term_;
61 57
62 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); 58 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService);
63 }; 59 };
64 60
65 // TemplateURLServiceTestUtilBase ---------------------------------------------
66 61
67 TemplateURLServiceTestUtilBase::TemplateURLServiceTestUtilBase() 62 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
68 : changed_count_(0) { 63 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
64 changed_count_(0) {
69 } 65 }
70 66
71 TemplateURLServiceTestUtilBase::~TemplateURLServiceTestUtilBase() { 67 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
72 } 68 }
73 69
74 void TemplateURLServiceTestUtilBase::CreateTemplateUrlService() { 70 void TemplateURLServiceTestUtil::SetUp() {
71 // Make unique temp directory.
72 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
73 profile_.reset(new TestingProfile(temp_dir_.path()));
74
75 profile()->CreateWebDataService(); 75 profile()->CreateWebDataService();
76 76
77 TemplateURLService* service = static_cast<TemplateURLService*>( 77 model_.reset(new TestingTemplateURLService(profile()));
78 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 78 model_->AddObserver(this);
79 profile(), TestingTemplateURLService::Build)); 79
80 service->AddObserver(this); 80 #if defined(OS_CHROMEOS)
81 google_brand::chromeos::ClearBrandForCurrentSession();
82 #endif
81 } 83 }
82 84
83 void TemplateURLServiceTestUtilBase::OnTemplateURLServiceChanged() { 85 void TemplateURLServiceTestUtil::TearDown() {
86 ClearModel();
87 profile_.reset();
88
89 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
90
91 // Flush the message loop to make application verifiers happy.
92 base::RunLoop().RunUntilIdle();
93 }
94
95 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
84 changed_count_++; 96 changed_count_++;
85 } 97 }
86 98
87 int TemplateURLServiceTestUtilBase::GetObserverCount() { 99 int TemplateURLServiceTestUtil::GetObserverCount() {
88 return changed_count_; 100 return changed_count_;
89 } 101 }
90 102
91 void TemplateURLServiceTestUtilBase::ResetObserverCount() { 103 void TemplateURLServiceTestUtil::ResetObserverCount() {
92 changed_count_ = 0; 104 changed_count_ = 0;
93 } 105 }
94 106
95 void TemplateURLServiceTestUtilBase::VerifyLoad() { 107 void TemplateURLServiceTestUtil::VerifyLoad() {
96 ASSERT_FALSE(model()->loaded()); 108 ASSERT_FALSE(model()->loaded());
97 model()->Load(); 109 model()->Load();
98 base::RunLoop().RunUntilIdle(); 110 base::RunLoop().RunUntilIdle();
99 EXPECT_EQ(1, GetObserverCount()); 111 EXPECT_EQ(1, GetObserverCount());
100 ResetObserverCount(); 112 ResetObserverCount();
101 } 113 }
102 114
103 void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() { 115 void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
104 model()->ChangeToLoadedState(); 116 model()->ChangeToLoadedState();
105 // Initialize the web data service so that the database gets updated with 117 // Initialize the web data service so that the database gets updated with
106 // any changes made. 118 // any changes made.
107 119
108 model()->web_data_service_ = 120 model()->web_data_service_ =
109 WebDataServiceFactory::GetKeywordWebDataForProfile( 121 WebDataServiceFactory::GetKeywordWebDataForProfile(
110 profile(), Profile::EXPLICIT_ACCESS); 122 profile(), Profile::EXPLICIT_ACCESS);
111 base::RunLoop().RunUntilIdle(); 123 base::RunLoop().RunUntilIdle();
112 } 124 }
113 125
114 void TemplateURLServiceTestUtilBase::ClearModel() { 126 void TemplateURLServiceTestUtil::ClearModel() {
115 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( 127 model_->Shutdown();
116 profile(), NULL); 128 model_.reset();
117 } 129 }
118 130
119 void TemplateURLServiceTestUtilBase::ResetModel(bool verify_load) { 131 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
120 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 132 if (model_)
121 profile(), TestingTemplateURLService::Build); 133 ClearModel();
134 model_.reset(new TestingTemplateURLService(profile()));
122 model()->AddObserver(this); 135 model()->AddObserver(this);
123 changed_count_ = 0; 136 changed_count_ = 0;
124 if (verify_load) 137 if (verify_load)
125 VerifyLoad(); 138 VerifyLoad();
126 } 139 }
127 140
128 base::string16 TemplateURLServiceTestUtilBase::GetAndClearSearchTerm() { 141 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
129 return 142 return model_->GetAndClearSearchTerm();
130 static_cast<TestingTemplateURLService*>(model())->GetAndClearSearchTerm();
131 } 143 }
132 144
133 void TemplateURLServiceTestUtilBase::SetGoogleBaseURL( 145 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) {
134 const GURL& base_url) const {
135 DCHECK(base_url.is_valid()); 146 DCHECK(base_url.is_valid());
136 UIThreadSearchTermsData data(profile()); 147 UIThreadSearchTermsData data(profile());
137 UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec()); 148 UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec());
138 TemplateURLServiceFactory::GetForProfile(profile())->GoogleBaseURLChanged(); 149 model_->GoogleBaseURLChanged();
139 } 150 }
140 151
141 void TemplateURLServiceTestUtilBase::SetManagedDefaultSearchPreferences( 152 void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences(
142 bool enabled, 153 bool enabled,
143 const std::string& name, 154 const std::string& name,
144 const std::string& keyword, 155 const std::string& keyword,
145 const std::string& search_url, 156 const std::string& search_url,
146 const std::string& suggest_url, 157 const std::string& suggest_url,
147 const std::string& icon_url, 158 const std::string& icon_url,
148 const std::string& encodings, 159 const std::string& encodings,
149 const std::string& alternate_url, 160 const std::string& alternate_url,
150 const std::string& search_terms_replacement_key) { 161 const std::string& search_terms_replacement_key) {
151 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService(); 162 DefaultSearchPrefTestUtil::SetManagedPref(
152 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); 163 profile()->GetTestingPrefService(),
153 if (!enabled) { 164 enabled, name, keyword, search_url, suggest_url, icon_url, encodings,
154 value->SetBoolean(DefaultSearchManager::kDisabledByPolicy, true); 165 alternate_url, search_terms_replacement_key);
155 pref_service->SetManagedPref(
156 DefaultSearchManager::kDefaultSearchProviderDataPrefName,
157 value.release());
158 return;
159 }
160
161 EXPECT_FALSE(keyword.empty());
162 EXPECT_FALSE(search_url.empty());
163 value->Set(DefaultSearchManager::kShortName,
164 base::Value::CreateStringValue(name));
165 value->Set(DefaultSearchManager::kKeyword,
166 base::Value::CreateStringValue(keyword));
167 value->Set(DefaultSearchManager::kURL,
168 base::Value::CreateStringValue(search_url));
169 value->Set(DefaultSearchManager::kSuggestionsURL,
170 base::Value::CreateStringValue(suggest_url));
171 value->Set(DefaultSearchManager::kFaviconURL,
172 base::Value::CreateStringValue(icon_url));
173 value->Set(DefaultSearchManager::kSearchTermsReplacementKey,
174 base::Value::CreateStringValue(search_terms_replacement_key));
175
176 std::vector<std::string> encodings_items;
177 base::SplitString(encodings, ';', &encodings_items);
178 scoped_ptr<base::ListValue> encodings_list(new base::ListValue);
179 for (std::vector<std::string>::const_iterator it = encodings_items.begin();
180 it != encodings_items.end();
181 ++it) {
182 encodings_list->AppendString(*it);
183 }
184 value->Set(DefaultSearchManager::kInputEncodings, encodings_list.release());
185
186 scoped_ptr<base::ListValue> alternate_url_list(new base::ListValue());
187 if (!alternate_url.empty())
188 alternate_url_list->Append(base::Value::CreateStringValue(alternate_url));
189 value->Set(DefaultSearchManager::kAlternateURLs,
190 alternate_url_list.release());
191
192 pref_service->SetManagedPref(
193 DefaultSearchManager::kDefaultSearchProviderDataPrefName,
194 value.release());
195 } 166 }
196 167
197 void TemplateURLServiceTestUtilBase::RemoveManagedDefaultSearchPreferences() { 168 void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() {
198 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService(); 169 DefaultSearchPrefTestUtil::RemoveManagedPref(
199 pref_service->RemoveManagedPref( 170 profile()->GetTestingPrefService());
200 DefaultSearchManager::kDefaultSearchProviderDataPrefName);
201 } 171 }
202 172
203 TemplateURLService* TemplateURLServiceTestUtilBase::model() const { 173 TemplateURLService* TemplateURLServiceTestUtil::model() {
204 return TemplateURLServiceFactory::GetForProfile(profile()); 174 return model_.get();
205 } 175 }
206
207
208 // TemplateURLServiceTestUtil -------------------------------------------------
209
210 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
211 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
212 }
213
214 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
215 }
216
217 void TemplateURLServiceTestUtil::SetUp() {
218 // Make unique temp directory.
219 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
220 profile_.reset(new TestingProfile(temp_dir_.path()));
221
222 TemplateURLServiceTestUtilBase::CreateTemplateUrlService();
223
224 #if defined(OS_CHROMEOS)
225 google_brand::chromeos::ClearBrandForCurrentSession();
226 #endif
227 }
228
229 void TemplateURLServiceTestUtil::TearDown() {
230 profile_.reset();
231
232 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
233
234 // Flush the message loop to make application verifiers happy.
235 base::RunLoop().RunUntilIdle();
236 }
237
238 TestingProfile* TemplateURLServiceTestUtil::profile() const {
239 return profile_.get();
240 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698