OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search_engines/default_search_pref_test_util.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/run_loop.h" | |
9 #include "base/strings/string_split.h" | 7 #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/template_url_service_factory.h" | |
13 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" | |
14 #include "chrome/browser/webdata/web_data_service_factory.h" | |
15 #include "chrome/test/base/testing_pref_service_syncable.h" | |
16 #include "chrome/test/base/testing_profile.h" | |
17 #include "components/google/core/browser/google_url_tracker.h" | |
18 #include "components/search_engines/default_search_manager.h" | 8 #include "components/search_engines/default_search_manager.h" |
19 #include "components/search_engines/template_url_service.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
21 | 10 |
22 #if defined(OS_CHROMEOS) | 11 // static |
23 #include "chrome/browser/google/google_brand_chromeos.h" | 12 scoped_ptr<base::DictionaryValue> |
24 #endif | 13 DefaultSearchPrefTestUtil::CreateDefaultSearchPreferenceValue( |
25 | |
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)); | |
32 } | |
33 | |
34 explicit TestingTemplateURLService(Profile* profile) | |
35 : TemplateURLService( | |
36 profile->GetPrefs(), | |
37 scoped_ptr<SearchTermsData>(new UIThreadSearchTermsData(profile)), | |
38 WebDataServiceFactory::GetKeywordWebDataForProfile( | |
39 profile, Profile::EXPLICIT_ACCESS), | |
40 scoped_ptr<TemplateURLServiceClient>( | |
41 new ChromeTemplateURLServiceClient(profile)), NULL, NULL, | |
42 base::Closure()) { | |
43 } | |
44 | |
45 base::string16 GetAndClearSearchTerm() { | |
46 base::string16 search_term; | |
47 search_term.swap(search_term_); | |
48 return search_term; | |
49 } | |
50 | |
51 protected: | |
52 virtual void SetKeywordSearchTermsForURL( | |
53 const TemplateURL* t_url, | |
54 const GURL& url, | |
55 const base::string16& term) OVERRIDE { | |
56 search_term_ = term; | |
57 } | |
58 | |
59 private: | |
60 base::string16 search_term_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); | |
63 }; | |
64 | |
65 // TemplateURLServiceTestUtilBase --------------------------------------------- | |
66 | |
67 TemplateURLServiceTestUtilBase::TemplateURLServiceTestUtilBase() | |
68 : changed_count_(0) { | |
69 } | |
70 | |
71 TemplateURLServiceTestUtilBase::~TemplateURLServiceTestUtilBase() { | |
72 } | |
73 | |
74 void TemplateURLServiceTestUtilBase::CreateTemplateUrlService() { | |
75 profile()->CreateWebDataService(); | |
76 | |
77 TemplateURLService* service = static_cast<TemplateURLService*>( | |
78 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
79 profile(), TestingTemplateURLService::Build)); | |
80 service->AddObserver(this); | |
81 } | |
82 | |
83 void TemplateURLServiceTestUtilBase::OnTemplateURLServiceChanged() { | |
84 changed_count_++; | |
85 } | |
86 | |
87 int TemplateURLServiceTestUtilBase::GetObserverCount() { | |
88 return changed_count_; | |
89 } | |
90 | |
91 void TemplateURLServiceTestUtilBase::ResetObserverCount() { | |
92 changed_count_ = 0; | |
93 } | |
94 | |
95 void TemplateURLServiceTestUtilBase::VerifyLoad() { | |
96 ASSERT_FALSE(model()->loaded()); | |
97 model()->Load(); | |
98 base::RunLoop().RunUntilIdle(); | |
99 EXPECT_EQ(1, GetObserverCount()); | |
100 ResetObserverCount(); | |
101 } | |
102 | |
103 void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() { | |
104 model()->ChangeToLoadedState(); | |
105 // Initialize the web data service so that the database gets updated with | |
106 // any changes made. | |
107 | |
108 model()->web_data_service_ = | |
109 WebDataServiceFactory::GetKeywordWebDataForProfile( | |
110 profile(), Profile::EXPLICIT_ACCESS); | |
111 base::RunLoop().RunUntilIdle(); | |
112 } | |
113 | |
114 void TemplateURLServiceTestUtilBase::ClearModel() { | |
115 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( | |
116 profile(), NULL); | |
117 } | |
118 | |
119 void TemplateURLServiceTestUtilBase::ResetModel(bool verify_load) { | |
120 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
121 profile(), TestingTemplateURLService::Build); | |
122 model()->AddObserver(this); | |
123 changed_count_ = 0; | |
124 if (verify_load) | |
125 VerifyLoad(); | |
126 } | |
127 | |
128 base::string16 TemplateURLServiceTestUtilBase::GetAndClearSearchTerm() { | |
129 return | |
130 static_cast<TestingTemplateURLService*>(model())->GetAndClearSearchTerm(); | |
131 } | |
132 | |
133 void TemplateURLServiceTestUtilBase::SetGoogleBaseURL( | |
134 const GURL& base_url) const { | |
135 DCHECK(base_url.is_valid()); | |
136 UIThreadSearchTermsData data(profile()); | |
137 UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec()); | |
138 TemplateURLServiceFactory::GetForProfile(profile())->GoogleBaseURLChanged(); | |
139 } | |
140 | |
141 void TemplateURLServiceTestUtilBase::SetManagedDefaultSearchPreferences( | |
142 bool enabled, | 14 bool enabled, |
143 const std::string& name, | 15 const std::string& name, |
144 const std::string& keyword, | 16 const std::string& keyword, |
145 const std::string& search_url, | 17 const std::string& search_url, |
146 const std::string& suggest_url, | 18 const std::string& suggest_url, |
147 const std::string& icon_url, | 19 const std::string& icon_url, |
148 const std::string& encodings, | 20 const std::string& encodings, |
149 const std::string& alternate_url, | 21 const std::string& alternate_url, |
150 const std::string& search_terms_replacement_key) { | 22 const std::string& search_terms_replacement_key) { |
151 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService(); | |
152 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); | 23 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
153 if (!enabled) { | 24 if (!enabled) { |
154 value->SetBoolean(DefaultSearchManager::kDisabledByPolicy, true); | 25 value->SetBoolean(DefaultSearchManager::kDisabledByPolicy, true); |
155 pref_service->SetManagedPref( | 26 return value.Pass(); |
156 DefaultSearchManager::kDefaultSearchProviderDataPrefName, | |
157 value.release()); | |
158 return; | |
159 } | 27 } |
160 | 28 |
161 EXPECT_FALSE(keyword.empty()); | 29 EXPECT_FALSE(keyword.empty()); |
162 EXPECT_FALSE(search_url.empty()); | 30 EXPECT_FALSE(search_url.empty()); |
163 value->Set(DefaultSearchManager::kShortName, | 31 value->Set(DefaultSearchManager::kShortName, |
164 base::Value::CreateStringValue(name)); | 32 base::Value::CreateStringValue(name)); |
165 value->Set(DefaultSearchManager::kKeyword, | 33 value->Set(DefaultSearchManager::kKeyword, |
166 base::Value::CreateStringValue(keyword)); | 34 base::Value::CreateStringValue(keyword)); |
167 value->Set(DefaultSearchManager::kURL, | 35 value->Set(DefaultSearchManager::kURL, |
168 base::Value::CreateStringValue(search_url)); | 36 base::Value::CreateStringValue(search_url)); |
(...skipping 12 matching lines...) Expand all Loading... |
181 ++it) { | 49 ++it) { |
182 encodings_list->AppendString(*it); | 50 encodings_list->AppendString(*it); |
183 } | 51 } |
184 value->Set(DefaultSearchManager::kInputEncodings, encodings_list.release()); | 52 value->Set(DefaultSearchManager::kInputEncodings, encodings_list.release()); |
185 | 53 |
186 scoped_ptr<base::ListValue> alternate_url_list(new base::ListValue()); | 54 scoped_ptr<base::ListValue> alternate_url_list(new base::ListValue()); |
187 if (!alternate_url.empty()) | 55 if (!alternate_url.empty()) |
188 alternate_url_list->Append(base::Value::CreateStringValue(alternate_url)); | 56 alternate_url_list->Append(base::Value::CreateStringValue(alternate_url)); |
189 value->Set(DefaultSearchManager::kAlternateURLs, | 57 value->Set(DefaultSearchManager::kAlternateURLs, |
190 alternate_url_list.release()); | 58 alternate_url_list.release()); |
191 | 59 return value.Pass(); |
192 pref_service->SetManagedPref( | |
193 DefaultSearchManager::kDefaultSearchProviderDataPrefName, | |
194 value.release()); | |
195 } | 60 } |
196 | |
197 void TemplateURLServiceTestUtilBase::RemoveManagedDefaultSearchPreferences() { | |
198 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService(); | |
199 pref_service->RemoveManagedPref( | |
200 DefaultSearchManager::kDefaultSearchProviderDataPrefName); | |
201 } | |
202 | |
203 TemplateURLService* TemplateURLServiceTestUtilBase::model() const { | |
204 return TemplateURLServiceFactory::GetForProfile(profile()); | |
205 } | |
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 } | |
OLD | NEW |