OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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/profile_resetter/automatic_profile_resetter_delegate.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/prefs/pref_service.h" | |
11 #include "base/run_loop.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "base/test/values_test_util.h" | |
15 #include "chrome/browser/chrome_notification_types.h" | |
16 #include "chrome/browser/search_engines/template_url_service.h" | |
17 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
18 #include "chrome/browser/search_engines/template_url_service_test_util.h" | |
19 #include "chrome/common/pref_names.h" | |
20 #include "chrome/test/base/testing_pref_service_syncable.h" | |
21 #include "chrome/test/base/testing_profile.h" | |
22 #include "content/public/browser/notification_service.h" | |
23 #include "testing/gmock/include/gmock/gmock.h" | |
24 #include "testing/gtest/include/gtest/gtest.h" | |
25 | |
26 #if defined(OS_WIN) | |
27 #include "chrome/browser/enumerate_modules_model_win.h" | |
28 #endif | |
29 | |
30 using base::UTF8ToUTF16; | |
31 | |
32 namespace { | |
33 | |
34 // Keep this value in sync with prefs::kDefaultSearchProviderURL and friends. | |
35 const char kDefaultSearchProviderPrefix[] = "default_search_provider"; | |
36 | |
37 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; | |
38 const char kTestSuggestURL[] = "http://example.com/suggest?q={searchTerms}"; | |
39 const char kTestInstantURL[] = "http://example.com/instant?q={searchTerms}"; | |
40 const char kTestImageURL[] = "http://example.com/image?q={searchTerms}"; | |
41 const char kTestSearchURLPostParams[] = "search-post-params"; | |
42 const char kTestSuggestURLPostParams[] = "suggest-post-params"; | |
43 const char kTestInstantURLPostParams[] = "instant-post-params"; | |
44 const char kTestImageURLPostParams[] = "image-post-params"; | |
45 | |
46 const char kTestIconURL[] = "http://example.com/favicon.ico"; | |
47 const char kTestNewTabURL[] = "http://example.com/newtab.html"; | |
48 const char kTestAlternateURL[] = "http://example.com/s?q={searchTerms}"; | |
49 | |
50 const char kTestName[] = "name"; | |
51 const char kTestKeyword[] = "keyword"; | |
52 const char kTestSearchTermReplacementKey[] = "key"; | |
53 const char kTestPrepopulateId[] = "2"; | |
54 const char kTestEncoding[] = "UTF-8"; | |
55 | |
56 // Test fixtures ------------------------------------------------------------- | |
57 | |
58 class GenericTestBase : public testing::Test { | |
59 protected: | |
60 GenericTestBase() {} | |
61 | |
62 virtual void SetUp() { profile_.reset(new TestingProfile()); } | |
63 | |
64 TestingProfile* profile() { return profile_.get(); } | |
65 | |
66 private: | |
67 content::TestBrowserThreadBundle thread_bundle_; | |
68 scoped_ptr<TestingProfile> profile_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(GenericTestBase); | |
71 }; | |
72 | |
73 class TemplateURLSpecificTestBase : public testing::Test { | |
74 protected: | |
75 TemplateURLSpecificTestBase() {} | |
76 | |
77 virtual void SetUp() { test_util_.SetUp(); } | |
78 | |
79 virtual void TearDown() { test_util_.TearDown(); } | |
80 | |
81 TestingProfile* profile() { return test_util_.profile(); } | |
82 | |
83 TemplateURL* CreateTestTemplateURL() { | |
84 TemplateURLData data; | |
85 | |
86 data.SetURL(kTestSearchURL); | |
87 data.suggestions_url = kTestSuggestURL; | |
88 data.instant_url = kTestInstantURL; | |
89 data.image_url = kTestImageURL; | |
90 data.search_url_post_params = kTestSearchURLPostParams; | |
91 data.suggestions_url_post_params = kTestSuggestURLPostParams; | |
92 data.instant_url_post_params = kTestInstantURLPostParams; | |
93 data.image_url_post_params = kTestImageURLPostParams; | |
94 | |
95 data.favicon_url = GURL(kTestIconURL); | |
96 data.new_tab_url = kTestNewTabURL; | |
97 data.alternate_urls.push_back(kTestAlternateURL); | |
98 | |
99 data.short_name = base::UTF8ToUTF16(kTestName); | |
100 data.SetKeyword(base::UTF8ToUTF16(kTestKeyword)); | |
101 data.search_terms_replacement_key = kTestSearchTermReplacementKey; | |
102 EXPECT_TRUE(base::StringToInt(kTestPrepopulateId, &data.prepopulate_id)); | |
103 data.input_encodings.push_back(kTestEncoding); | |
104 data.safe_for_autoreplace = true; | |
105 | |
106 return new TemplateURL(profile(), data); | |
107 } | |
108 | |
109 TemplateURLServiceTestUtil test_util_; | |
110 | |
111 private: | |
112 DISALLOW_COPY_AND_ASSIGN(TemplateURLSpecificTestBase); | |
113 }; | |
114 | |
115 template <class BaseTestFixture> | |
116 class ResetterDelegateMixin : public BaseTestFixture { | |
117 protected: | |
118 ResetterDelegateMixin() {} | |
119 | |
120 virtual void SetUp() OVERRIDE { | |
121 BaseTestFixture::SetUp(); | |
122 resetter_delegate_.reset( | |
123 new AutomaticProfileResetterDelegateImpl(BaseTestFixture::profile())); | |
124 } | |
125 | |
126 virtual void TearDown() OVERRIDE { | |
127 resetter_delegate_.reset(); | |
128 BaseTestFixture::TearDown(); | |
129 } | |
130 | |
131 AutomaticProfileResetterDelegate* resetter_delegate() { | |
132 return resetter_delegate_.get(); | |
133 } | |
134 | |
135 private: | |
136 scoped_ptr<AutomaticProfileResetterDelegate> resetter_delegate_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(ResetterDelegateMixin); | |
139 }; | |
140 | |
141 typedef ResetterDelegateMixin<GenericTestBase> | |
142 AutomaticProfileResetterDelegateTest; | |
143 | |
144 typedef ResetterDelegateMixin<TemplateURLSpecificTestBase> | |
145 AutomaticProfileResetterDelegateTestTemplateURLs; | |
146 | |
147 // Helper classes and functions ---------------------------------------------- | |
148 | |
149 // Verifies that the |details| of a search engine as provided by the delegate | |
150 // are correct in comparison to the |expected_details| coming from the Prefs. | |
151 void VerifyDetails(const base::DictionaryValue& expected_details, | |
152 const base::DictionaryValue& details) { | |
153 const char* keys_to_verify[] = { | |
154 "search_url", "search_terms_replacement_key", | |
155 "suggest_url", "instant_url", | |
156 "image_url", "new_tab_url", | |
157 "icon_url", "search_url_post_params", | |
158 "suggest_url_post_params", "instant_url_post_params", | |
159 "image_url_post_params", "name", | |
160 "keyword", "encodings", | |
161 "prepopulate_id", "alternate_urls"}; | |
162 | |
163 for (size_t i = 0; i < arraysize(keys_to_verify); ++i) { | |
164 SCOPED_TRACE(testing::Message() << "Key: " << keys_to_verify[i]); | |
165 const base::Value* expected_value; | |
166 const base::Value* actual_value; | |
167 ASSERT_TRUE(expected_details.Get(keys_to_verify[i], &expected_value)); | |
168 EXPECT_TRUE(details.Get(keys_to_verify[i], &actual_value) && | |
vasilii
2013/10/14 16:43:33
Split this condition.
engedy
2013/10/14 19:35:50
Done.
| |
169 actual_value->Equals(expected_value)); | |
170 } | |
171 } | |
172 | |
173 class MockCallbackTarget { | |
174 public: | |
175 MockCallbackTarget() {} | |
176 | |
177 MOCK_CONST_METHOD0(Run, void(void)); | |
178 | |
179 base::Closure CreateClosure() { | |
180 return base::Closure( | |
181 base::Bind(&MockCallbackTarget::Run, base::Unretained(this))); | |
182 } | |
183 | |
184 private: | |
185 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget); | |
186 }; | |
187 | |
188 // Tests --------------------------------------------------------------------- | |
189 | |
190 TEST_F(AutomaticProfileResetterDelegateTest, | |
191 TriggerAndWaitOnModuleEnumeration) { | |
192 testing::StrictMock<MockCallbackTarget> mock_target; | |
193 | |
194 // Expect ready_callback to be called just after the modules have been | |
195 // enumerated. Fail if it is not called, or called too early. | |
196 resetter_delegate()->WaitOnEnumerationOfLoadedModules( | |
197 mock_target.CreateClosure()); | |
198 base::RunLoop().RunUntilIdle(); | |
199 | |
200 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
vasilii
2013/10/14 16:43:33
I think it would fail anyway in the line above.
engedy
2013/10/14 19:35:50
You are correct, done.
| |
201 | |
202 EXPECT_CALL(mock_target, Run()); | |
203 resetter_delegate()->EnumerateLoadedModulesIfNeeded(); | |
204 base::RunLoop().RunUntilIdle(); | |
205 | |
206 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
207 | |
208 // Expect ready_callback to be posted immediately when the modules have | |
209 // already been enumerated. | |
210 EXPECT_CALL(mock_target, Run()); | |
211 resetter_delegate()->WaitOnEnumerationOfLoadedModules( | |
212 mock_target.CreateClosure()); | |
213 base::RunLoop().RunUntilIdle(); | |
214 | |
215 #if defined(OS_WIN) | |
216 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
217 | |
218 // Expect ready_callback to be posted immediately even when the modules had | |
219 // already been enumerated when the delegate was constructed. | |
220 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( | |
221 new AutomaticProfileResetterDelegateImpl(profile())); | |
222 | |
223 EXPECT_CALL(mock_target, Run()); | |
224 late_resetter_delegate->WaitOnEnumerationOfLoadedModules( | |
225 mock_target.CreateClosure()); | |
226 base::RunLoop().RunUntilIdle(); | |
227 #endif | |
228 } | |
229 | |
230 TEST_F(AutomaticProfileResetterDelegateTest, GetLoadedModuleNameDigests) { | |
231 resetter_delegate()->EnumerateLoadedModulesIfNeeded(); | |
232 base::RunLoop().RunUntilIdle(); | |
233 scoped_ptr<base::ListValue> module_name_digests( | |
234 resetter_delegate()->GetLoadedModuleNameDigests()); | |
235 | |
236 ASSERT_TRUE(module_name_digests); | |
237 #if defined(OS_WIN) | |
238 scoped_ptr<base::ListValue> module_list; | |
239 module_list.reset(EnumerateModulesModel::GetInstance()->GetModuleList()); | |
240 ASSERT_TRUE(module_list); | |
241 EXPECT_EQ(module_list->GetSize(), module_name_digests->GetSize()); | |
242 #endif | |
243 } | |
244 | |
245 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | |
246 LoadAndWaitOnTemplateURLService) { | |
247 testing::StrictMock<MockCallbackTarget> mock_target; | |
248 | |
249 // Expect ready_callback to be called just after the template URL service gets | |
250 // initialized. Fail if it is not called, or called too early. | |
251 resetter_delegate()->WaitOnTemplateURLService(mock_target.CreateClosure()); | |
252 base::RunLoop().RunUntilIdle(); | |
253 | |
254 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
255 | |
256 EXPECT_CALL(mock_target, Run()); | |
257 resetter_delegate()->LoadTemplateURLServiceIfNeeded(); | |
258 base::RunLoop().RunUntilIdle(); | |
259 | |
260 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
261 | |
262 // Expect ready_callback to be posted immediately when the template URL | |
263 // service is already initialized. | |
264 EXPECT_CALL(mock_target, Run()); | |
265 resetter_delegate()->WaitOnTemplateURLService(mock_target.CreateClosure()); | |
266 base::RunLoop().RunUntilIdle(); | |
267 | |
268 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
269 | |
270 // Expect ready_callback to be posted immediately even when the template URL | |
271 // service had already been initialized when the delegate was constructed. | |
272 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( | |
273 new AutomaticProfileResetterDelegateImpl(profile())); | |
274 | |
275 EXPECT_CALL(mock_target, Run()); | |
276 late_resetter_delegate->WaitOnTemplateURLService(mock_target.CreateClosure()); | |
277 base::RunLoop().RunUntilIdle(); | |
278 } | |
279 | |
280 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | |
281 GetDefaultSearchProviderDetails) { | |
282 TemplateURLService* template_url_service = test_util_.model(); | |
283 test_util_.VerifyLoad(); | |
284 | |
285 // Create a custom search provider, and make it the default. Note that this | |
286 // will update all data related to the default search provider in Prefs. | |
287 TemplateURL* custom_dsp = CreateTestTemplateURL(); | |
288 template_url_service->Add(custom_dsp); | |
289 template_url_service->SetDefaultSearchProvider(custom_dsp); | |
290 | |
291 scoped_ptr<base::DictionaryValue> dsp_details( | |
292 resetter_delegate()->GetDefaultSearchProviderDetails()); | |
293 | |
294 // Verify above details against the user preferences that have been stored by | |
295 // TemplateURLService. We leverage on the fact that the names for all these | |
296 // preferences start with the same prefix. | |
297 PrefService* prefs = profile()->GetPrefs(); | |
298 ASSERT_TRUE(prefs); | |
299 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion( | |
300 prefs->GetPreferenceValues()); | |
301 base::DictionaryValue* expected_dsp_details; | |
302 ASSERT_TRUE(pref_values_with_path_expansion->GetDictionary( | |
303 kDefaultSearchProviderPrefix, &expected_dsp_details)); | |
304 VerifyDetails(*expected_dsp_details, *dsp_details); | |
305 } | |
306 | |
307 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | |
308 IsDefaultSearchProviderManaged) { | |
309 test_util_.VerifyLoad(); | |
310 | |
311 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
312 | |
313 // Enable having a default search provider, and also set one from policy. | |
314 test_util_.SetManagedDefaultSearchPreferences( | |
315 true, kTestName, kTestKeyword, kTestSearchURL, "", "", "", "", ""); | |
316 | |
317 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
318 scoped_ptr<base::DictionaryValue> dsp_details( | |
319 resetter_delegate()->GetDefaultSearchProviderDetails()); | |
320 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url"); | |
321 | |
322 // Disable having a default search provider, but nevertheless, set one from | |
323 // policy. | |
324 test_util_.RemoveManagedDefaultSearchPreferences(); | |
325 test_util_.SetManagedDefaultSearchPreferences( | |
326 false, kTestName, kTestKeyword, kTestSearchURL, "", "", "", "", ""); | |
327 | |
328 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
329 EXPECT_EQ(NULL, resetter_delegate()->GetDefaultSearchProviderDetails()); | |
330 | |
331 // Disable having a default search provider, and set an invalid one from | |
332 // policy. | |
333 test_util_.RemoveManagedDefaultSearchPreferences(); | |
334 test_util_.SetManagedDefaultSearchPreferences( | |
335 true, "", "", "", "", "", "", "", ""); | |
336 | |
337 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
338 EXPECT_EQ(NULL, resetter_delegate()->GetDefaultSearchProviderDetails()); | |
339 } | |
340 | |
341 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | |
342 DISABLED_IsDefaultSearchProviderManagedSubtle) { | |
vasilii
2013/10/14 16:43:33
We shouldn't commit the broken test.
engedy
2013/10/14 19:35:50
Agreed. I wanted to have this here for the review,
vasilii
2013/10/17 10:57:57
Either fix it or remove it.
| |
343 test_util_.VerifyLoad(); | |
344 | |
345 // Only set a single policy to disable having a default search provider. | |
346 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService(); | |
347 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled, | |
348 new base::FundamentalValue(false)); | |
349 test_util_.model()->Observe( | |
350 chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED, | |
351 content::NotificationService::AllSources(), | |
352 content::NotificationService::NoDetails()); | |
353 | |
354 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
355 EXPECT_EQ(NULL, resetter_delegate()->GetDefaultSearchProviderDetails()); | |
356 } | |
357 | |
358 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | |
359 GetPrepopulatedSearchProvidersDetails) { | |
360 TemplateURLService* template_url_service = test_util_.model(); | |
361 test_util_.VerifyLoad(); | |
362 | |
363 scoped_ptr<base::ListValue> prepopulated_search_engines_details( | |
364 resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); | |
365 | |
366 // Do the same kind of verification as for GetDefaultSearchEngineDetails: | |
367 // subsequently set each pre-populated engine as the default, so we can verify | |
368 // that the details returned by the delegate about one particular engine are | |
369 // correct in comparison to what has been stored to the Prefs. | |
370 std::vector<TemplateURL*> prepopulated_engines = | |
371 template_url_service->GetTemplateURLs(); | |
372 | |
373 ASSERT_EQ(prepopulated_engines.size(), | |
374 prepopulated_search_engines_details->GetSize()); | |
375 | |
376 // This assumes that the order of engines are preserved. | |
377 for (size_t i = 0; i < prepopulated_engines.size(); ++i) { | |
378 template_url_service->SetDefaultSearchProvider(prepopulated_engines[i]); | |
379 | |
380 PrefService* prefs = profile()->GetPrefs(); | |
381 ASSERT_TRUE(prefs); | |
382 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion( | |
383 prefs->GetPreferenceValues()); | |
vasilii
2013/10/14 16:43:33
Why don't you use GetPreferenceValue("default_sear
engedy
2013/10/14 19:35:50
That is not possible here, because preferences are
| |
384 base::DictionaryValue* expected_dsp_details; | |
385 ASSERT_TRUE(pref_values_with_path_expansion->GetDictionary( | |
386 kDefaultSearchProviderPrefix, &expected_dsp_details)); | |
387 | |
388 base::DictionaryValue* details; | |
389 ASSERT_TRUE( | |
390 prepopulated_search_engines_details->GetDictionary(i, &details)); | |
391 | |
392 SCOPED_TRACE(testing::Message() << "Details: " << *details); | |
393 VerifyDetails(*expected_dsp_details, *details); | |
394 } | |
395 } | |
396 | |
397 } // namespace | |
OLD | NEW |