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/string_split.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "base/test/values_test_util.h" | |
16 #include "base/values.h" | |
17 #include "chrome/browser/chrome_notification_types.h" | |
18 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | |
19 #include "chrome/browser/search_engines/template_url_service.h" | |
20 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
21 #include "chrome/browser/search_engines/template_url_service_test_util.h" | |
22 #include "chrome/common/pref_names.h" | |
23 #include "chrome/test/base/testing_pref_service_syncable.h" | |
24 #include "chrome/test/base/testing_profile.h" | |
25 #include "content/public/browser/notification_service.h" | |
26 #include "testing/gmock/include/gmock/gmock.h" | |
27 #include "testing/gtest/include/gtest/gtest.h" | |
28 | |
29 #if defined(OS_WIN) | |
30 #include "chrome/browser/enumerate_modules_model_win.h" | |
31 #endif | |
32 | |
33 using testing::WhenSorted; | |
34 using testing::ElementsAreArray; | |
Peter Kasting
2013/10/16 00:40:21
Nit: Avoid using directives when they don't save l
engedy
2013/10/16 11:13:54
Done.
| |
35 | |
36 namespace { | |
37 | |
38 // Test fixtures ------------------------------------------------------------- | |
39 | |
40 class GenericTestBase : public testing::Test { | |
41 protected: | |
42 GenericTestBase() {} | |
43 | |
44 virtual void SetUp() { profile_.reset(new TestingProfile()); } | |
45 | |
46 TestingProfile* profile() { return profile_.get(); } | |
47 | |
48 private: | |
49 content::TestBrowserThreadBundle thread_bundle_; | |
50 scoped_ptr<TestingProfile> profile_; | |
51 | |
52 DISALLOW_COPY_AND_ASSIGN(GenericTestBase); | |
53 }; | |
54 | |
55 class TemplateURLSpecificTestBase : public testing::Test { | |
56 protected: | |
57 static const char kDefaultSearchProviderPrefix[]; | |
58 | |
59 TemplateURLSpecificTestBase() {} | |
60 | |
61 virtual void SetUp() { test_util_.SetUp(); } | |
62 | |
63 virtual void TearDown() { test_util_.TearDown(); } | |
64 | |
65 TestingProfile* profile() { return test_util_.profile(); } | |
66 | |
67 scoped_ptr<TemplateURL> CreateTestTemplateURL() { | |
68 TemplateURLData data; | |
69 | |
70 data.SetURL("http://example.com/search?q={searchTerms}"); | |
71 data.suggestions_url = "http://example.com/suggest?q={searchTerms}"; | |
72 data.instant_url = "http://example.com/instant?q={searchTerms}"; | |
73 data.image_url = "http://example.com/image?q={searchTerms}"; | |
74 data.search_url_post_params = "search-post-params"; | |
75 data.suggestions_url_post_params = "suggest-post-params"; | |
76 data.instant_url_post_params = "instant-post-params"; | |
77 data.image_url_post_params = "image-post-params"; | |
78 | |
79 data.favicon_url = GURL("http://example.com/favicon.ico"); | |
80 data.new_tab_url = "http://example.com/newtab.html"; | |
81 data.alternate_urls.push_back("http://example.com/s?q={searchTerms}"); | |
82 | |
83 data.short_name = base::UTF8ToUTF16("name"); | |
84 data.SetKeyword(base::UTF8ToUTF16("keyword")); | |
85 data.search_terms_replacement_key = "search-terms-replacment-key"; | |
86 data.prepopulate_id = 42; | |
87 data.input_encodings.push_back("UTF-8"); | |
88 data.safe_for_autoreplace = true; | |
89 | |
90 return scoped_ptr<TemplateURL>(new TemplateURL(profile(), data)); | |
91 } | |
92 | |
93 TemplateURLServiceTestUtil test_util_; | |
94 | |
95 private: | |
96 DISALLOW_COPY_AND_ASSIGN(TemplateURLSpecificTestBase); | |
97 }; | |
98 | |
99 template <class BaseTestFixture> | |
100 class ResetterDelegateMixin : public BaseTestFixture { | |
101 protected: | |
102 ResetterDelegateMixin() {} | |
103 | |
104 virtual void SetUp() OVERRIDE { | |
105 BaseTestFixture::SetUp(); | |
106 resetter_delegate_.reset( | |
107 new AutomaticProfileResetterDelegateImpl(BaseTestFixture::profile())); | |
108 } | |
109 | |
110 virtual void TearDown() OVERRIDE { | |
111 resetter_delegate_.reset(); | |
112 BaseTestFixture::TearDown(); | |
113 } | |
114 | |
115 AutomaticProfileResetterDelegate* resetter_delegate() { | |
116 return resetter_delegate_.get(); | |
117 } | |
118 | |
119 private: | |
120 scoped_ptr<AutomaticProfileResetterDelegate> resetter_delegate_; | |
121 | |
122 DISALLOW_COPY_AND_ASSIGN(ResetterDelegateMixin); | |
123 }; | |
124 | |
125 typedef ResetterDelegateMixin<GenericTestBase> | |
126 AutomaticProfileResetterDelegateTest; | |
127 | |
128 typedef ResetterDelegateMixin<TemplateURLSpecificTestBase> | |
129 AutomaticProfileResetterDelegateTestTemplateURLs; | |
130 | |
131 // Helper classes and functions ---------------------------------------------- | |
132 | |
133 // Returns the details of the default search provider from |prefs| in a format | |
134 // suitable for usage as |expected_details| in VerifyDetails(). | |
135 scoped_ptr<base::DictionaryValue> GetDefaultSearchProviderDetails( | |
136 const PrefService* prefs) { | |
137 const char kDefaultSearchProviderPrefix[] = "default_search_provider"; | |
138 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion( | |
139 prefs->GetPreferenceValues()); | |
140 const base::DictionaryValue* dsp_details = NULL; | |
141 EXPECT_TRUE(pref_values_with_path_expansion->GetDictionary( | |
142 kDefaultSearchProviderPrefix, &dsp_details)); | |
143 return dsp_details | |
144 ? scoped_ptr<base::DictionaryValue>(dsp_details->DeepCopy()) | |
Peter Kasting
2013/10/16 00:40:21
Nit: See comments in delegate .cc regarding wrappi
engedy
2013/10/16 11:13:54
Done.
| |
145 : scoped_ptr<base::DictionaryValue>(new base::DictionaryValue); | |
146 } | |
147 | |
148 // Verifies that the |details| of a search engine as provided by the delegate | |
149 // are correct in comparison to the |expected_details| coming from the Prefs. | |
150 void VerifyDetails(const base::DictionaryValue& expected_details, | |
151 const base::DictionaryValue& details) { | |
152 for (base::DictionaryValue::Iterator it(expected_details); !it.IsAtEnd(); | |
153 it.Advance()) { | |
154 SCOPED_TRACE(testing::Message() << "Key: " << it.key()); | |
155 if (it.key() == "enabled" || it.key() == "synced_guid") { | |
156 // These attributes should not be present. | |
157 EXPECT_FALSE(details.HasKey(it.key())); | |
158 continue; | |
159 } | |
160 const base::Value* expected_value = &it.value(); | |
161 const base::Value* actual_value = NULL; | |
162 ASSERT_TRUE(details.Get(it.key(), &actual_value)); | |
163 if (it.key() == "id") { | |
164 // Ignore ID as it is dynamically assigned by the TemplateURLService. | |
165 } else if (it.key() == "encodings") { | |
166 // Encoding list is stored in Prefs as a semicolon-separated string. | |
Peter Kasting
2013/10/16 00:40:21
This is sure ugly... I don't have much good advice
engedy
2013/10/16 11:13:54
Agreed that this is very ugly. Vasilii, as Owner,
| |
167 std::string expected_encodings; | |
168 ASSERT_TRUE(expected_value->GetAsString(&expected_encodings)); | |
169 std::vector<std::string> expected_encodings_vector; | |
170 base::SplitString(expected_encodings, ';', &expected_encodings_vector); | |
171 const base::ListValue* actual_encodings_list = NULL; | |
172 ASSERT_TRUE(actual_value->GetAsList(&actual_encodings_list)); | |
173 std::vector<std::string> actual_encodings_vector; | |
174 for (base::ListValue::const_iterator it = actual_encodings_list->begin(); | |
175 it != actual_encodings_list->end(); ++it) { | |
176 std::string encoding; | |
177 ASSERT_TRUE((*it)->GetAsString(&encoding)); | |
178 actual_encodings_vector.push_back(encoding); | |
179 } | |
180 std::sort(expected_encodings_vector.begin(), | |
181 expected_encodings_vector.end()); | |
182 EXPECT_THAT(actual_encodings_vector, | |
183 WhenSorted(ElementsAreArray(expected_encodings_vector))); | |
184 } else { | |
185 // Everything else is the same format. | |
186 EXPECT_TRUE(actual_value->Equals(expected_value)); | |
187 } | |
188 } | |
189 } | |
190 | |
191 class MockCallbackTarget { | |
192 public: | |
193 MockCallbackTarget() {} | |
194 | |
195 MOCK_CONST_METHOD0(Run, void(void)); | |
196 | |
197 base::Closure CreateClosure() { | |
198 return base::Closure( | |
199 base::Bind(&MockCallbackTarget::Run, base::Unretained(this))); | |
200 } | |
201 | |
202 private: | |
203 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget); | |
204 }; | |
205 | |
206 // Tests --------------------------------------------------------------------- | |
207 | |
208 TEST_F(AutomaticProfileResetterDelegateTest, | |
209 TriggerAndWaitOnModuleEnumeration) { | |
210 testing::StrictMock<MockCallbackTarget> mock_target; | |
211 | |
212 // Expect ready_callback to be called just after the modules have been | |
213 // enumerated. Fail if it is not called, or called too early. | |
214 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated( | |
215 mock_target.CreateClosure()); | |
216 base::RunLoop().RunUntilIdle(); | |
217 | |
218 EXPECT_CALL(mock_target, Run()); | |
219 resetter_delegate()->EnumerateLoadedModulesIfNeeded(); | |
220 base::RunLoop().RunUntilIdle(); | |
221 | |
222 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
223 | |
224 // Expect ready_callback to be posted immediately when the modules have | |
225 // already been enumerated. | |
226 EXPECT_CALL(mock_target, Run()); | |
227 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated( | |
228 mock_target.CreateClosure()); | |
229 base::RunLoop().RunUntilIdle(); | |
230 | |
231 #if defined(OS_WIN) | |
232 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
233 | |
234 // Expect ready_callback to be posted immediately even when the modules had | |
235 // already been enumerated when the delegate was constructed. | |
236 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( | |
237 new AutomaticProfileResetterDelegateImpl(profile())); | |
238 | |
239 EXPECT_CALL(mock_target, Run()); | |
240 late_resetter_delegate->RequestCallbackWhenLoadedModulesAreEnumerated( | |
241 mock_target.CreateClosure()); | |
242 base::RunLoop().RunUntilIdle(); | |
243 #endif | |
244 } | |
245 | |
246 TEST_F(AutomaticProfileResetterDelegateTest, GetLoadedModuleNameDigests) { | |
247 resetter_delegate()->EnumerateLoadedModulesIfNeeded(); | |
248 base::RunLoop().RunUntilIdle(); | |
249 scoped_ptr<base::ListValue> module_name_digests( | |
250 resetter_delegate()->GetLoadedModuleNameDigests()); | |
251 | |
252 // Just verify that each element looks like an MD5 hash in hexadecimal, and | |
253 // also that we have at least one element on Win. | |
254 ASSERT_TRUE(module_name_digests); | |
255 for (base::ListValue::const_iterator it = module_name_digests->begin(); | |
256 it != module_name_digests->end(); ++it) { | |
257 std::string digest_hex; | |
258 std::vector<uint8> digest_raw; | |
259 | |
260 ASSERT_TRUE((*it)->GetAsString(&digest_hex)); | |
261 ASSERT_TRUE(base::HexStringToBytes(digest_hex, &digest_raw)); | |
262 EXPECT_EQ(16u, digest_raw.size()); | |
263 } | |
264 #if defined(OS_WIN) | |
265 EXPECT_LE(1u, module_name_digests->GetSize()); | |
266 #endif | |
267 } | |
268 | |
269 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | |
270 LoadAndWaitOnTemplateURLService) { | |
271 testing::StrictMock<MockCallbackTarget> mock_target; | |
272 | |
273 // Expect ready_callback to be called just after the template URL service gets | |
274 // initialized. Fail if it is not called, or called too early. | |
275 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded( | |
276 mock_target.CreateClosure()); | |
277 base::RunLoop().RunUntilIdle(); | |
278 | |
279 EXPECT_CALL(mock_target, Run()); | |
280 resetter_delegate()->LoadTemplateURLServiceIfNeeded(); | |
281 base::RunLoop().RunUntilIdle(); | |
282 | |
283 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
284 | |
285 // Expect ready_callback to be posted immediately when the template URL | |
286 // service is already initialized. | |
287 EXPECT_CALL(mock_target, Run()); | |
288 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded( | |
289 mock_target.CreateClosure()); | |
290 base::RunLoop().RunUntilIdle(); | |
291 | |
292 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
293 | |
294 // Expect ready_callback to be posted immediately even when the template URL | |
295 // service had already been initialized when the delegate was constructed. | |
296 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( | |
297 new AutomaticProfileResetterDelegateImpl(profile())); | |
298 | |
299 EXPECT_CALL(mock_target, Run()); | |
300 late_resetter_delegate->RequestCallbackWhenTemplateURLServiceIsLoaded( | |
301 mock_target.CreateClosure()); | |
302 base::RunLoop().RunUntilIdle(); | |
303 } | |
304 | |
305 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | |
306 GetDefaultSearchProviderDetails) { | |
307 TemplateURLService* template_url_service = test_util_.model(); | |
308 test_util_.VerifyLoad(); | |
309 | |
310 // Create a custom search provider, and make it the default. Note that this | |
311 // will update all data related to the default search provider in Prefs. | |
312 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); | |
313 TemplateURL* custom_dsp = owned_custom_dsp.get(); | |
314 template_url_service->Add(owned_custom_dsp.release()); | |
315 template_url_service->SetDefaultSearchProvider(custom_dsp); | |
316 | |
317 scoped_ptr<base::DictionaryValue> dsp_details( | |
318 resetter_delegate()->GetDefaultSearchProviderDetails()); | |
319 | |
320 // Verify above details against the user preferences that have been stored by | |
321 // TemplateURLService. We leverage on the fact that the paths for all these | |
322 // preferences share the same first segment (before the '.'). Note, however, | |
323 // that preferences are originally stored without path expansion. | |
Peter Kasting
2013/10/16 00:40:21
Unfortunately, I still don't really know what this
engedy
2013/10/16 11:13:54
Done. On second read, this really does not make mu
| |
324 PrefService* prefs = profile()->GetPrefs(); | |
325 ASSERT_TRUE(prefs); | |
326 scoped_ptr<base::DictionaryValue> expected_dsp_details( | |
327 GetDefaultSearchProviderDetails(prefs)); | |
328 VerifyDetails(*expected_dsp_details, *dsp_details); | |
329 } | |
330 | |
331 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | |
332 IsDefaultSearchProviderManaged) { | |
333 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; | |
334 const char kTestName[] = "name"; | |
335 const char kTestKeyword[] = "keyword"; | |
336 | |
337 test_util_.VerifyLoad(); | |
338 | |
339 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
340 | |
341 // Enable having a default search provider, and also set one from policy. | |
342 test_util_.SetManagedDefaultSearchPreferences( | |
343 true, kTestName, kTestKeyword, kTestSearchURL, std::string(), | |
344 std::string(), std::string(), std::string(), std::string()); | |
345 | |
346 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
347 scoped_ptr<base::DictionaryValue> dsp_details( | |
348 resetter_delegate()->GetDefaultSearchProviderDetails()); | |
349 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url"); | |
350 | |
351 // Disable having a default search provider, but nevertheless, set one from | |
352 // policy. | |
353 test_util_.RemoveManagedDefaultSearchPreferences(); | |
354 test_util_.SetManagedDefaultSearchPreferences( | |
355 false, kTestName, kTestKeyword, kTestSearchURL, std::string(), | |
356 std::string(), std::string(), std::string(), std::string()); | |
357 | |
358 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails(); | |
359 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
360 EXPECT_TRUE(dsp_details->empty()); | |
361 | |
362 // Disable having a default search provider, and set an invalid one from | |
363 // policy. | |
364 test_util_.RemoveManagedDefaultSearchPreferences(); | |
365 test_util_.SetManagedDefaultSearchPreferences( | |
366 true, std::string(), std::string(), std::string(), std::string(), | |
367 std::string(), std::string(), std::string(), std::string()); | |
368 | |
369 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails(); | |
370 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
371 EXPECT_TRUE(dsp_details->empty()); | |
372 } | |
373 | |
374 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | |
375 DISABLED_IsDefaultSearchProviderManagedSubtle) { | |
376 test_util_.VerifyLoad(); | |
377 | |
378 // Only set a single policy to disable having a default search provider. | |
379 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService(); | |
380 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled, | |
381 new base::FundamentalValue(false)); | |
382 test_util_.model()->Observe( | |
383 chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED, | |
384 content::NotificationService::AllSources(), | |
385 content::NotificationService::NoDetails()); | |
386 | |
387 scoped_ptr<base::DictionaryValue> dsp_details( | |
388 resetter_delegate()->GetDefaultSearchProviderDetails()); | |
389 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
390 EXPECT_TRUE(dsp_details->empty()); | |
391 } | |
392 | |
393 TEST_F(AutomaticProfileResetterDelegateTestTemplateURLs, | |
394 GetPrepopulatedSearchProvidersDetails) { | |
395 TemplateURLService* template_url_service = test_util_.model(); | |
396 test_util_.VerifyLoad(); | |
397 | |
398 scoped_ptr<base::ListValue> search_engines_details( | |
399 resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); | |
400 | |
401 // Do the same kind of verification as for GetDefaultSearchEngineDetails: | |
402 // subsequently set each pre-populated engine as the default, so we can verify | |
403 // that the details returned by the delegate about one particular engine are | |
404 // correct in comparison to what has been stored to the Prefs. | |
405 std::vector<TemplateURL*> prepopulated_engines = | |
406 template_url_service->GetTemplateURLs(); | |
407 | |
408 ASSERT_EQ(prepopulated_engines.size(), search_engines_details->GetSize()); | |
409 | |
410 // This assumes that the engines pre-populated for the current locale all have | |
411 // a unique keyword specified. | |
Peter Kasting
2013/10/16 00:40:21
All engines in the database must always have uniqu
engedy
2013/10/16 11:13:54
Done.
| |
412 for (size_t i = 0; i < search_engines_details->GetSize(); ++i) { | |
413 const base::DictionaryValue* details = NULL; | |
414 ASSERT_TRUE(search_engines_details->GetDictionary(i, &details)); | |
415 | |
416 std::string keyword; | |
417 ASSERT_TRUE(details->GetString("keyword", &keyword)); | |
418 TemplateURL* search_engine = | |
419 template_url_service->GetTemplateURLForKeyword(UTF8ToUTF16(keyword)); | |
420 ASSERT_TRUE(search_engine); | |
421 template_url_service->SetDefaultSearchProvider(prepopulated_engines[i]); | |
422 | |
423 PrefService* prefs = profile()->GetPrefs(); | |
424 ASSERT_TRUE(prefs); | |
425 scoped_ptr<base::DictionaryValue> expected_dsp_details( | |
426 GetDefaultSearchProviderDetails(prefs)); | |
427 VerifyDetails(*expected_dsp_details, *details); | |
428 } | |
429 } | |
430 | |
431 } // namespace | |
OLD | NEW |