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 <algorithm> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/prefs/pref_service.h" | |
14 #include "base/run_loop.h" | |
15 #include "base/strings/string_number_conversions.h" | |
16 #include "base/strings/string_split.h" | |
17 #include "base/strings/string_util.h" | |
18 #include "base/strings/utf_string_conversions.h" | |
19 #include "base/test/values_test_util.h" | |
20 #include "base/values.h" | |
21 #include "chrome/app/chrome_command_ids.h" | |
22 #include "chrome/browser/chrome_notification_types.h" | |
23 #include "chrome/browser/extensions/extension_service_unittest.h" | |
24 #include "chrome/browser/google/google_util.h" | |
25 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h" | |
26 #include "chrome/browser/profile_resetter/profile_reset_global_error.h" | |
27 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | |
28 #include "chrome/browser/search_engines/template_url_service.h" | |
29 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
30 #include "chrome/browser/search_engines/template_url_service_test_util.h" | |
31 #include "chrome/browser/ui/global_error/global_error.h" | |
32 #include "chrome/browser/ui/global_error/global_error_service.h" | |
33 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | |
34 #include "chrome/common/pref_names.h" | |
35 #include "chrome/test/base/testing_pref_service_syncable.h" | |
36 #include "chrome/test/base/testing_profile.h" | |
37 #include "content/public/browser/notification_service.h" | |
38 #include "net/http/http_response_headers.h" | |
39 #include "net/url_request/test_url_fetcher_factory.h" | |
40 #include "testing/gmock/include/gmock/gmock.h" | |
41 #include "testing/gtest/include/gtest/gtest.h" | |
42 | |
43 #if defined(OS_WIN) | |
44 #include "chrome/browser/enumerate_modules_model_win.h" | |
45 #endif | |
46 | |
47 namespace { | |
48 | |
49 const char kTestBrandcode[] = "FOOBAR"; | |
50 | |
51 const char kTestHomepage[] = "http://google.com"; | |
52 const char kTestBrandedHomepage[] = "http://example.com"; | |
53 | |
54 const ProfileResetter::ResettableFlags kResettableAspectsForTest = | |
55 ProfileResetter::ALL & ~ProfileResetter::COOKIES_AND_SITE_DATA; | |
56 | |
57 // Helpers ------------------------------------------------------------------- | |
58 | |
59 // A testing version of the AutomaticProfileResetterDelegate that differs from | |
60 // the real one only in that it has its feedback reporting mocked out, and it | |
61 // will not reset COOKIES_AND_SITE_DATA, due to difficulties to set up some | |
62 // required URLRequestContexts in unit tests. | |
63 class AutomaticProfileResetterDelegateUnderTest | |
64 : public AutomaticProfileResetterDelegateImpl { | |
65 public: | |
66 explicit AutomaticProfileResetterDelegateUnderTest(Profile* profile) | |
67 : AutomaticProfileResetterDelegateImpl( | |
68 profile, kResettableAspectsForTest) {} | |
69 virtual ~AutomaticProfileResetterDelegateUnderTest() {} | |
70 | |
71 MOCK_CONST_METHOD1(SendFeedback, void(const std::string&)); | |
72 | |
73 private: | |
74 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateUnderTest); | |
75 }; | |
76 | |
77 class MockCallbackTarget { | |
78 public: | |
79 MockCallbackTarget() {} | |
80 ~MockCallbackTarget() {} | |
81 | |
82 MOCK_CONST_METHOD0(Run, void(void)); | |
83 | |
84 base::Closure CreateClosure() { | |
85 return base::Bind(&MockCallbackTarget::Run, base::Unretained(this)); | |
86 } | |
87 | |
88 private: | |
89 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget); | |
90 }; | |
91 | |
92 // Returns the details of the default search provider from |prefs| in a format | |
93 // suitable for usage as |expected_details| in ExpectDetailsMatch(). | |
94 scoped_ptr<base::DictionaryValue> GetDefaultSearchProviderDetailsFromPrefs( | |
95 const PrefService* prefs) { | |
96 const char kDefaultSearchProviderPrefix[] = "default_search_provider"; | |
97 scoped_ptr<base::DictionaryValue> pref_values_with_path_expansion( | |
98 prefs->GetPreferenceValues()); | |
99 const base::DictionaryValue* dsp_details = NULL; | |
100 EXPECT_TRUE(pref_values_with_path_expansion->GetDictionary( | |
101 kDefaultSearchProviderPrefix, &dsp_details)); | |
102 return scoped_ptr<base::DictionaryValue>( | |
103 dsp_details ? dsp_details->DeepCopy() : new base::DictionaryValue); | |
104 } | |
105 | |
106 // Verifies that the |details| of a search engine as provided by the delegate | |
107 // are correct in comparison to the |expected_details| coming from the Prefs. | |
108 void ExpectDetailsMatch(const base::DictionaryValue& expected_details, | |
109 const base::DictionaryValue& details) { | |
110 for (base::DictionaryValue::Iterator it(expected_details); !it.IsAtEnd(); | |
111 it.Advance()) { | |
112 SCOPED_TRACE(testing::Message("Key: ") << it.key()); | |
113 if (it.key() == "enabled" || it.key() == "synced_guid") { | |
114 // These attributes should not be present. | |
115 EXPECT_FALSE(details.HasKey(it.key())); | |
116 continue; | |
117 } | |
118 const base::Value* expected_value = &it.value(); | |
119 const base::Value* actual_value = NULL; | |
120 ASSERT_TRUE(details.Get(it.key(), &actual_value)); | |
121 | |
122 if (it.key() == "id") { | |
123 // Ignore ID as it is dynamically assigned by the TemplateURLService. | |
124 } else if (it.key() == "encodings") { | |
125 // Encoding list is stored in Prefs as a single string with tokens | |
126 // delimited by semicolons. | |
127 std::string expected_encodings; | |
128 ASSERT_TRUE(expected_value->GetAsString(&expected_encodings)); | |
129 const base::ListValue* actual_encodings_list = NULL; | |
130 ASSERT_TRUE(actual_value->GetAsList(&actual_encodings_list)); | |
131 std::vector<std::string> actual_encodings_vector; | |
132 for (base::ListValue::const_iterator it = actual_encodings_list->begin(); | |
133 it != actual_encodings_list->end(); ++it) { | |
134 std::string encoding; | |
135 ASSERT_TRUE((*it)->GetAsString(&encoding)); | |
136 actual_encodings_vector.push_back(encoding); | |
137 } | |
138 EXPECT_EQ(expected_encodings, JoinString(actual_encodings_vector, ';')); | |
139 } else { | |
140 // Everything else is the same format. | |
141 EXPECT_TRUE(actual_value->Equals(expected_value)) | |
142 << "Expected: " << *expected_value << ". Actual: " << *actual_value; | |
143 } | |
144 } | |
145 } | |
146 | |
147 // If |simulate_failure| is false, then replies to the pending request on | |
148 // |fetcher| with a brandcoded config that only specifies a home page URL. | |
149 // If |simulate_failure| is true, replies with 404. | |
150 void ServicePendingBrancodedConfigFetch(net::TestURLFetcher* fetcher, | |
151 bool simulate_failure) { | |
152 const char kBrandcodedXmlSettings[] = | |
153 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
154 "<response protocol=\"3.0\" server=\"prod\">" | |
155 "<app appid=\"{8A69D345-D564-463C-AFF1-A69D9E530F96}\" status=\"ok\">" | |
156 "<data index=\"skipfirstrunui-importsearch-defaultbrowser\" " | |
157 "name=\"install\" status=\"ok\">" | |
158 "{\"homepage\" : \"$1\"}" | |
159 "</data>" | |
160 "</app>" | |
161 "</response>"; | |
162 | |
163 fetcher->set_response_code(simulate_failure ? 404 : 200); | |
164 scoped_refptr<net::HttpResponseHeaders> response_headers( | |
165 new net::HttpResponseHeaders("")); | |
166 response_headers->AddHeader("Content-Type: text/xml"); | |
167 fetcher->set_response_headers(response_headers); | |
168 if (!simulate_failure) { | |
169 std::string response(kBrandcodedXmlSettings); | |
170 size_t placeholder_index = response.find("$1"); | |
171 ASSERT_NE(std::string::npos, placeholder_index); | |
172 response.replace(placeholder_index, 2, kTestBrandedHomepage); | |
173 fetcher->SetResponseString(response); | |
174 } | |
175 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
176 } | |
177 | |
178 | |
179 // Test fixture -------------------------------------------------------------- | |
180 | |
181 // ExtensionServiceTestBase sets up a TestingProfile with the ExtensionService, | |
182 // we then add the TemplateURLService, so the ProfileResetter can be exercised. | |
183 class AutomaticProfileResetterDelegateTest | |
184 : public ExtensionServiceTestBase, | |
185 public TemplateURLServiceTestUtilBase { | |
186 protected: | |
187 AutomaticProfileResetterDelegateTest() {} | |
188 virtual ~AutomaticProfileResetterDelegateTest() {} | |
189 | |
190 virtual void SetUp() OVERRIDE { | |
191 ExtensionServiceTestBase::SetUp(); | |
192 ExtensionServiceInitParams params = CreateDefaultInitParams(); | |
193 params.pref_file.clear(); // Prescribes a TestingPrefService to be created. | |
194 InitializeExtensionService(params); | |
195 TemplateURLServiceTestUtilBase::CreateTemplateUrlService(); | |
196 resetter_delegate_.reset( | |
197 new AutomaticProfileResetterDelegateUnderTest(profile())); | |
198 } | |
199 | |
200 virtual void TearDown() OVERRIDE { | |
201 resetter_delegate_.reset(); | |
202 ExtensionServiceTestBase::TearDown(); | |
203 } | |
204 | |
205 scoped_ptr<TemplateURL> CreateTestTemplateURL() { | |
206 TemplateURLData data; | |
207 | |
208 data.SetURL("http://example.com/search?q={searchTerms}"); | |
209 data.suggestions_url = "http://example.com/suggest?q={searchTerms}"; | |
210 data.instant_url = "http://example.com/instant?q={searchTerms}"; | |
211 data.image_url = "http://example.com/image?q={searchTerms}"; | |
212 data.search_url_post_params = "search-post-params"; | |
213 data.suggestions_url_post_params = "suggest-post-params"; | |
214 data.instant_url_post_params = "instant-post-params"; | |
215 data.image_url_post_params = "image-post-params"; | |
216 | |
217 data.favicon_url = GURL("http://example.com/favicon.ico"); | |
218 data.new_tab_url = "http://example.com/newtab.html"; | |
219 data.alternate_urls.push_back("http://example.com/s?q={searchTerms}"); | |
220 | |
221 data.short_name = base::ASCIIToUTF16("name"); | |
222 data.SetKeyword(base::ASCIIToUTF16("keyword")); | |
223 data.search_terms_replacement_key = "search-terms-replacment-key"; | |
224 data.prepopulate_id = 42; | |
225 data.input_encodings.push_back("UTF-8"); | |
226 data.safe_for_autoreplace = true; | |
227 | |
228 return scoped_ptr<TemplateURL>(new TemplateURL(profile(), data)); | |
229 } | |
230 | |
231 void ExpectNoPendingBrandcodedConfigFetch() { | |
232 EXPECT_FALSE(test_url_fetcher_factory_.GetFetcherByID(0)); | |
233 } | |
234 | |
235 void ExpectAndServicePendingBrandcodedConfigFetch(bool simulate_failure) { | |
236 net::TestURLFetcher* fetcher = test_url_fetcher_factory_.GetFetcherByID(0); | |
237 ASSERT_TRUE(fetcher); | |
238 EXPECT_THAT(fetcher->upload_data(), | |
239 testing::HasSubstr(kTestBrandcode)); | |
240 ServicePendingBrancodedConfigFetch(fetcher, simulate_failure); | |
241 } | |
242 | |
243 void ExpectResetPromptState(bool active) { | |
244 GlobalErrorService* global_error_service = | |
245 GlobalErrorServiceFactory::GetForProfile(profile()); | |
246 GlobalError* global_error = global_error_service-> | |
247 GetGlobalErrorByMenuItemCommandID(IDC_SHOW_SETTINGS_RESET_BUBBLE); | |
248 EXPECT_EQ(active, !!global_error); | |
249 } | |
250 | |
251 AutomaticProfileResetterDelegateUnderTest* resetter_delegate() { | |
252 return resetter_delegate_.get(); | |
253 } | |
254 | |
255 // TemplateURLServiceTestUtilBase: | |
256 virtual TestingProfile* profile() const OVERRIDE { return profile_.get(); } | |
257 | |
258 private: | |
259 net::TestURLFetcherFactory test_url_fetcher_factory_; | |
260 scoped_ptr<AutomaticProfileResetterDelegateUnderTest> resetter_delegate_; | |
261 | |
262 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateTest); | |
263 }; | |
264 | |
265 | |
266 // Tests --------------------------------------------------------------------- | |
267 | |
268 TEST_F(AutomaticProfileResetterDelegateTest, | |
269 TriggerAndWaitOnModuleEnumeration) { | |
270 // Expect ready_callback to be called just after the modules have been | |
271 // enumerated. Fail if it is not called. Note: as the EnumerateModulesModel is | |
272 // a global singleton, the callback might be invoked immediately if another | |
273 // test-case (e.g. the one below) has already performed module enumeration. | |
274 testing::StrictMock<MockCallbackTarget> mock_target; | |
275 EXPECT_CALL(mock_target, Run()); | |
276 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated( | |
277 mock_target.CreateClosure()); | |
278 resetter_delegate()->EnumerateLoadedModulesIfNeeded(); | |
279 base::RunLoop().RunUntilIdle(); | |
280 | |
281 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
282 | |
283 // Expect ready_callback to be posted immediately when the modules have | |
284 // already been enumerated. | |
285 EXPECT_CALL(mock_target, Run()); | |
286 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated( | |
287 mock_target.CreateClosure()); | |
288 base::RunLoop().RunUntilIdle(); | |
289 | |
290 #if defined(OS_WIN) | |
291 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
292 | |
293 // Expect ready_callback to be posted immediately even when the modules had | |
294 // already been enumerated when the delegate was constructed. | |
295 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( | |
296 new AutomaticProfileResetterDelegateImpl(profile(), | |
297 ProfileResetter::ALL)); | |
298 | |
299 EXPECT_CALL(mock_target, Run()); | |
300 late_resetter_delegate->RequestCallbackWhenLoadedModulesAreEnumerated( | |
301 mock_target.CreateClosure()); | |
302 base::RunLoop().RunUntilIdle(); | |
303 #endif | |
304 } | |
305 | |
306 TEST_F(AutomaticProfileResetterDelegateTest, GetLoadedModuleNameDigests) { | |
307 resetter_delegate()->EnumerateLoadedModulesIfNeeded(); | |
308 base::RunLoop().RunUntilIdle(); | |
309 scoped_ptr<base::ListValue> module_name_digests( | |
310 resetter_delegate()->GetLoadedModuleNameDigests()); | |
311 | |
312 // Just verify that each element looks like an MD5 hash in hexadecimal, and | |
313 // also that we have at least one element on Win. | |
314 ASSERT_TRUE(module_name_digests); | |
315 for (base::ListValue::const_iterator it = module_name_digests->begin(); | |
316 it != module_name_digests->end(); ++it) { | |
317 std::string digest_hex; | |
318 std::vector<uint8> digest_raw; | |
319 | |
320 ASSERT_TRUE((*it)->GetAsString(&digest_hex)); | |
321 ASSERT_TRUE(base::HexStringToBytes(digest_hex, &digest_raw)); | |
322 EXPECT_EQ(16u, digest_raw.size()); | |
323 } | |
324 #if defined(OS_WIN) | |
325 EXPECT_LE(1u, module_name_digests->GetSize()); | |
326 #endif | |
327 } | |
328 | |
329 TEST_F(AutomaticProfileResetterDelegateTest, LoadAndWaitOnTemplateURLService) { | |
330 // Expect ready_callback to be called just after the template URL service gets | |
331 // initialized. Fail if it is not called, or called too early. | |
332 testing::StrictMock<MockCallbackTarget> mock_target; | |
333 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded( | |
334 mock_target.CreateClosure()); | |
335 base::RunLoop().RunUntilIdle(); | |
336 | |
337 EXPECT_CALL(mock_target, Run()); | |
338 resetter_delegate()->LoadTemplateURLServiceIfNeeded(); | |
339 base::RunLoop().RunUntilIdle(); | |
340 | |
341 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
342 | |
343 // Expect ready_callback to be posted immediately when the template URL | |
344 // service is already initialized. | |
345 EXPECT_CALL(mock_target, Run()); | |
346 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded( | |
347 mock_target.CreateClosure()); | |
348 base::RunLoop().RunUntilIdle(); | |
349 | |
350 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
351 | |
352 // Expect ready_callback to be posted immediately even when the template URL | |
353 // service had already been initialized when the delegate was constructed. | |
354 scoped_ptr<AutomaticProfileResetterDelegate> late_resetter_delegate( | |
355 new AutomaticProfileResetterDelegateImpl(profile(), | |
356 ProfileResetter::ALL)); | |
357 | |
358 EXPECT_CALL(mock_target, Run()); | |
359 late_resetter_delegate->RequestCallbackWhenTemplateURLServiceIsLoaded( | |
360 mock_target.CreateClosure()); | |
361 base::RunLoop().RunUntilIdle(); | |
362 } | |
363 | |
364 TEST_F(AutomaticProfileResetterDelegateTest, | |
365 DefaultSearchProviderDataWhenNotManaged) { | |
366 TemplateURLService* template_url_service = | |
367 TemplateURLServiceFactory::GetForProfile(profile()); | |
368 TemplateURLServiceTestUtilBase::VerifyLoad(); | |
369 | |
370 // Check that the "managed state" and the details returned by the delegate are | |
371 // correct. We verify the details against the data stored by | |
372 // TemplateURLService into Prefs. | |
373 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); | |
374 TemplateURL* custom_dsp = owned_custom_dsp.get(); | |
375 template_url_service->Add(owned_custom_dsp.release()); | |
376 template_url_service->SetUserSelectedDefaultSearchProvider(custom_dsp); | |
377 | |
378 PrefService* prefs = profile()->GetPrefs(); | |
379 ASSERT_TRUE(prefs); | |
380 scoped_ptr<base::DictionaryValue> dsp_details( | |
381 resetter_delegate()->GetDefaultSearchProviderDetails()); | |
382 scoped_ptr<base::DictionaryValue> expected_dsp_details( | |
383 GetDefaultSearchProviderDetailsFromPrefs(prefs)); | |
384 | |
385 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); | |
386 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
387 } | |
388 | |
389 TEST_F(AutomaticProfileResetterDelegateTest, | |
390 DefaultSearchProviderDataWhenManaged) { | |
391 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; | |
392 const char kTestName[] = "name"; | |
393 const char kTestKeyword[] = "keyword"; | |
394 | |
395 TemplateURLServiceTestUtilBase::VerifyLoad(); | |
396 | |
397 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
398 | |
399 // Set managed preferences to emulate a default search provider set by policy. | |
400 SetManagedDefaultSearchPreferences( | |
401 true, kTestName, kTestKeyword, kTestSearchURL, std::string(), | |
402 std::string(), std::string(), std::string(), std::string()); | |
403 | |
404 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
405 scoped_ptr<base::DictionaryValue> dsp_details( | |
406 resetter_delegate()->GetDefaultSearchProviderDetails()); | |
407 // Checking that all details are correct is already done by the above test. | |
408 // Just make sure details are reported about the correct engine. | |
409 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url"); | |
410 | |
411 // Set managed preferences to emulate that having a default search provider is | |
412 // disabled by policy. | |
413 RemoveManagedDefaultSearchPreferences(); | |
414 SetManagedDefaultSearchPreferences( | |
415 false, std::string(), std::string(), std::string(), std::string(), | |
416 std::string(), std::string(), std::string(), std::string()); | |
417 | |
418 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails(); | |
419 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | |
420 EXPECT_TRUE(dsp_details->empty()); | |
421 } | |
422 | |
423 TEST_F(AutomaticProfileResetterDelegateTest, | |
424 GetPrepopulatedSearchProvidersDetails) { | |
425 TemplateURLService* template_url_service = | |
426 TemplateURLServiceFactory::GetForProfile(profile()); | |
427 TemplateURLServiceTestUtilBase::VerifyLoad(); | |
428 | |
429 scoped_ptr<base::ListValue> search_engines_details( | |
430 resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); | |
431 | |
432 // Do the same kind of verification as for GetDefaultSearchEngineDetails: | |
433 // subsequently set each pre-populated engine as the default, so we can verify | |
434 // that the details returned by the delegate about one particular engine are | |
435 // correct in comparison to what has been stored to the Prefs. | |
436 std::vector<TemplateURL*> prepopulated_engines = | |
437 template_url_service->GetTemplateURLs(); | |
438 | |
439 ASSERT_EQ(prepopulated_engines.size(), search_engines_details->GetSize()); | |
440 | |
441 for (size_t i = 0; i < search_engines_details->GetSize(); ++i) { | |
442 const base::DictionaryValue* details = NULL; | |
443 ASSERT_TRUE(search_engines_details->GetDictionary(i, &details)); | |
444 | |
445 std::string keyword; | |
446 ASSERT_TRUE(details->GetString("keyword", &keyword)); | |
447 TemplateURL* search_engine = | |
448 template_url_service->GetTemplateURLForKeyword( | |
449 base::ASCIIToUTF16(keyword)); | |
450 ASSERT_TRUE(search_engine); | |
451 template_url_service->SetUserSelectedDefaultSearchProvider( | |
452 prepopulated_engines[i]); | |
453 | |
454 PrefService* prefs = profile()->GetPrefs(); | |
455 ASSERT_TRUE(prefs); | |
456 scoped_ptr<base::DictionaryValue> expected_dsp_details( | |
457 GetDefaultSearchProviderDetailsFromPrefs(prefs)); | |
458 ExpectDetailsMatch(*expected_dsp_details, *details); | |
459 } | |
460 } | |
461 | |
462 TEST_F(AutomaticProfileResetterDelegateTest, | |
463 FetchAndWaitOnDefaultSettingsVanilla) { | |
464 google_util::BrandForTesting scoped_brand_for_testing((std::string())); | |
465 | |
466 // Expect ready_callback to be called just after empty brandcoded settings | |
467 // are loaded, given this is a vanilla build. Fail if it is not called, or | |
468 // called too early. | |
469 testing::StrictMock<MockCallbackTarget> mock_target; | |
470 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched( | |
471 mock_target.CreateClosure()); | |
472 base::RunLoop().RunUntilIdle(); | |
473 EXPECT_FALSE(resetter_delegate()->brandcoded_defaults()); | |
474 | |
475 EXPECT_CALL(mock_target, Run()); | |
476 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded(); | |
477 base::RunLoop().RunUntilIdle(); | |
478 ExpectNoPendingBrandcodedConfigFetch(); | |
479 | |
480 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
481 EXPECT_TRUE(resetter_delegate()->brandcoded_defaults()); | |
482 | |
483 // Expect ready_callback to be posted immediately when the brandcoded settings | |
484 // have already been loaded. | |
485 EXPECT_CALL(mock_target, Run()); | |
486 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched( | |
487 mock_target.CreateClosure()); | |
488 base::RunLoop().RunUntilIdle(); | |
489 | |
490 // No test for a new instance of AutomaticProfileResetterDelegate. That will | |
491 // need to fetch the brandcoded settings again. | |
492 } | |
493 | |
494 TEST_F(AutomaticProfileResetterDelegateTest, | |
495 FetchAndWaitOnDefaultSettingsBranded) { | |
496 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode); | |
497 | |
498 // Expect ready_callback to be called just after the brandcoded settings are | |
499 // downloaded. Fail if it is not called, or called too early. | |
500 testing::StrictMock<MockCallbackTarget> mock_target; | |
501 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched( | |
502 mock_target.CreateClosure()); | |
503 base::RunLoop().RunUntilIdle(); | |
504 EXPECT_FALSE(resetter_delegate()->brandcoded_defaults()); | |
505 | |
506 EXPECT_CALL(mock_target, Run()); | |
507 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded(); | |
508 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/); | |
509 base::RunLoop().RunUntilIdle(); | |
510 | |
511 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
512 const BrandcodedDefaultSettings* brandcoded_defaults = | |
513 resetter_delegate()->brandcoded_defaults(); | |
514 ASSERT_TRUE(brandcoded_defaults); | |
515 std::string homepage_url; | |
516 EXPECT_TRUE(brandcoded_defaults->GetHomepage(&homepage_url)); | |
517 EXPECT_EQ(kTestBrandedHomepage, homepage_url); | |
518 | |
519 // Expect ready_callback to be posted immediately when the brandcoded settings | |
520 // have already been downloaded. | |
521 EXPECT_CALL(mock_target, Run()); | |
522 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched( | |
523 mock_target.CreateClosure()); | |
524 base::RunLoop().RunUntilIdle(); | |
525 } | |
526 | |
527 TEST_F(AutomaticProfileResetterDelegateTest, | |
528 FetchAndWaitOnDefaultSettingsBrandedFailure) { | |
529 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode); | |
530 | |
531 // Expect ready_callback to be called just after the brandcoded settings have | |
532 // failed to download. Fail if it is not called, or called too early. | |
533 testing::StrictMock<MockCallbackTarget> mock_target; | |
534 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched( | |
535 mock_target.CreateClosure()); | |
536 base::RunLoop().RunUntilIdle(); | |
537 | |
538 EXPECT_CALL(mock_target, Run()); | |
539 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded(); | |
540 ExpectAndServicePendingBrandcodedConfigFetch(true /*simulate_failure*/); | |
541 base::RunLoop().RunUntilIdle(); | |
542 | |
543 testing::Mock::VerifyAndClearExpectations(&mock_target); | |
544 EXPECT_TRUE(resetter_delegate()->brandcoded_defaults()); | |
545 | |
546 // Expect ready_callback to be posted immediately when the brandcoded settings | |
547 // have already been attempted to be downloaded, but failed. | |
548 EXPECT_CALL(mock_target, Run()); | |
549 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched( | |
550 mock_target.CreateClosure()); | |
551 base::RunLoop().RunUntilIdle(); | |
552 } | |
553 | |
554 TEST_F(AutomaticProfileResetterDelegateTest, TriggerReset) { | |
555 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode); | |
556 | |
557 PrefService* prefs = profile()->GetPrefs(); | |
558 DCHECK(prefs); | |
559 prefs->SetString(prefs::kHomePage, kTestHomepage); | |
560 | |
561 testing::StrictMock<MockCallbackTarget> mock_target; | |
562 EXPECT_CALL(mock_target, Run()); | |
563 EXPECT_CALL(*resetter_delegate(), SendFeedback(testing::_)).Times(0); | |
564 resetter_delegate()->TriggerProfileSettingsReset( | |
565 false /*send_feedback*/, mock_target.CreateClosure()); | |
566 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/); | |
567 base::RunLoop().RunUntilIdle(); | |
568 | |
569 EXPECT_EQ(kTestBrandedHomepage, prefs->GetString(prefs::kHomePage)); | |
570 } | |
571 | |
572 TEST_F(AutomaticProfileResetterDelegateTest, | |
573 TriggerResetWithDefaultSettingsAlreadyLoaded) { | |
574 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode); | |
575 | |
576 PrefService* prefs = profile()->GetPrefs(); | |
577 DCHECK(prefs); | |
578 prefs->SetString(prefs::kHomePage, kTestHomepage); | |
579 | |
580 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded(); | |
581 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/); | |
582 base::RunLoop().RunUntilIdle(); | |
583 | |
584 testing::StrictMock<MockCallbackTarget> mock_target; | |
585 EXPECT_CALL(mock_target, Run()); | |
586 EXPECT_CALL(*resetter_delegate(), SendFeedback(testing::_)).Times(0); | |
587 resetter_delegate()->TriggerProfileSettingsReset( | |
588 false /*send_feedback*/, mock_target.CreateClosure()); | |
589 base::RunLoop().RunUntilIdle(); | |
590 | |
591 EXPECT_EQ(kTestBrandedHomepage, prefs->GetString(prefs::kHomePage)); | |
592 } | |
593 | |
594 TEST_F(AutomaticProfileResetterDelegateTest, | |
595 TriggerResetAndSendFeedback) { | |
596 google_util::BrandForTesting scoped_brand_for_testing(kTestBrandcode); | |
597 | |
598 PrefService* prefs = profile()->GetPrefs(); | |
599 DCHECK(prefs); | |
600 prefs->SetString(prefs::kHomePage, kTestHomepage); | |
601 | |
602 testing::StrictMock<MockCallbackTarget> mock_target; | |
603 EXPECT_CALL(mock_target, Run()); | |
604 EXPECT_CALL(*resetter_delegate(), | |
605 SendFeedback(testing::HasSubstr(kTestHomepage))); | |
606 | |
607 resetter_delegate()->TriggerProfileSettingsReset( | |
608 true /*send_feedback*/, mock_target.CreateClosure()); | |
609 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/); | |
610 base::RunLoop().RunUntilIdle(); | |
611 } | |
612 | |
613 TEST_F(AutomaticProfileResetterDelegateTest, ShowAndDismissPrompt) { | |
614 resetter_delegate()->TriggerPrompt(); | |
615 if (ProfileResetGlobalError::IsSupportedOnPlatform()) | |
616 ExpectResetPromptState(true /*active*/); | |
617 else | |
618 ExpectResetPromptState(false /*active*/); | |
619 resetter_delegate()->DismissPrompt(); | |
620 ExpectResetPromptState(false /*active*/); | |
621 resetter_delegate()->DismissPrompt(); | |
622 } | |
623 | |
624 } // namespace | |
OLD | NEW |