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.
5 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.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"
44 #include "chrome/browser/enumerate_modules_model_win.h"
49 const char kTestBrandcode
[] = "FOOBAR";
51 const char kTestHomepage
[] = "http://google.com";
52 const char kTestBrandedHomepage
[] = "http://example.com";
54 const ProfileResetter::ResettableFlags kResettableAspectsForTest
=
55 ProfileResetter::ALL
& ~ProfileResetter::COOKIES_AND_SITE_DATA
;
57 // Helpers -------------------------------------------------------------------
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
{
66 explicit AutomaticProfileResetterDelegateUnderTest(Profile
* profile
)
67 : AutomaticProfileResetterDelegateImpl(
68 profile
, kResettableAspectsForTest
) {}
69 virtual ~AutomaticProfileResetterDelegateUnderTest() {}
71 MOCK_CONST_METHOD1(SendFeedback
, void(const std::string
&));
74 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateUnderTest
);
77 class MockCallbackTarget
{
79 MockCallbackTarget() {}
80 ~MockCallbackTarget() {}
82 MOCK_CONST_METHOD0(Run
, void(void));
84 base::Closure
CreateClosure() {
85 return base::Bind(&MockCallbackTarget::Run
, base::Unretained(this));
89 DISALLOW_COPY_AND_ASSIGN(MockCallbackTarget
);
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
);
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();
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()));
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 if (it
.key() == "id") {
122 // Ignore ID as it is dynamically assigned by the TemplateURLService.
123 } else if (it
.key() == "encodings") {
124 // Encoding list is stored in Prefs as a single string with tokens
125 // delimited by semicolons.
126 std::string expected_encodings
;
127 ASSERT_TRUE(expected_value
->GetAsString(&expected_encodings
));
128 const base::ListValue
* actual_encodings_list
= NULL
;
129 ASSERT_TRUE(actual_value
->GetAsList(&actual_encodings_list
));
130 std::vector
<std::string
> actual_encodings_vector
;
131 for (base::ListValue::const_iterator it
= actual_encodings_list
->begin();
132 it
!= actual_encodings_list
->end(); ++it
) {
133 std::string encoding
;
134 ASSERT_TRUE((*it
)->GetAsString(&encoding
));
135 actual_encodings_vector
.push_back(encoding
);
137 EXPECT_EQ(expected_encodings
, JoinString(actual_encodings_vector
, ';'));
139 // Everything else is the same format.
140 EXPECT_TRUE(actual_value
->Equals(expected_value
));
145 // If |simulate_failure| is false, then replies to the pending request on
146 // |fetcher| with a brandcoded config that only specifies a home page URL.
147 // If |simulate_failure| is true, replies with 404.
148 void ServicePendingBrancodedConfigFetch(net::TestURLFetcher
* fetcher
,
149 bool simulate_failure
) {
150 const char kBrandcodedXmlSettings
[] =
151 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
152 "<response protocol=\"3.0\" server=\"prod\">"
153 "<app appid=\"{8A69D345-D564-463C-AFF1-A69D9E530F96}\" status=\"ok\">"
154 "<data index=\"skipfirstrunui-importsearch-defaultbrowser\" "
155 "name=\"install\" status=\"ok\">"
156 "{\"homepage\" : \"$1\"}"
161 fetcher
->set_response_code(simulate_failure
? 404 : 200);
162 scoped_refptr
<net::HttpResponseHeaders
> response_headers(
163 new net::HttpResponseHeaders(""));
164 response_headers
->AddHeader("Content-Type: text/xml");
165 fetcher
->set_response_headers(response_headers
);
166 if (!simulate_failure
) {
167 std::string
response(kBrandcodedXmlSettings
);
168 size_t placeholder_index
= response
.find("$1");
169 ASSERT_NE(std::string::npos
, placeholder_index
);
170 response
.replace(placeholder_index
, 2, kTestBrandedHomepage
);
171 fetcher
->SetResponseString(response
);
173 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
177 // Test fixture --------------------------------------------------------------
179 // ExtensionServiceTestBase sets up a TestingProfile with the ExtensionService,
180 // we then add the TemplateURLService, so the ProfileResetter can be exercised.
181 class AutomaticProfileResetterDelegateTest
182 : public ExtensionServiceTestBase
,
183 public TemplateURLServiceTestUtilBase
{
185 AutomaticProfileResetterDelegateTest() {}
186 virtual ~AutomaticProfileResetterDelegateTest() {}
188 virtual void SetUp() OVERRIDE
{
189 ExtensionServiceTestBase::SetUp();
190 ExtensionServiceInitParams params
= CreateDefaultInitParams();
191 params
.pref_file
.clear(); // Prescribes a TestingPrefService to be created.
192 InitializeExtensionService(params
);
193 TemplateURLServiceTestUtilBase::CreateTemplateUrlService();
194 resetter_delegate_
.reset(
195 new AutomaticProfileResetterDelegateUnderTest(profile()));
198 virtual void TearDown() OVERRIDE
{
199 resetter_delegate_
.reset();
200 ExtensionServiceTestBase::TearDown();
203 scoped_ptr
<TemplateURL
> CreateTestTemplateURL() {
204 TemplateURLData data
;
206 data
.SetURL("http://example.com/search?q={searchTerms}");
207 data
.suggestions_url
= "http://example.com/suggest?q={searchTerms}";
208 data
.instant_url
= "http://example.com/instant?q={searchTerms}";
209 data
.image_url
= "http://example.com/image?q={searchTerms}";
210 data
.search_url_post_params
= "search-post-params";
211 data
.suggestions_url_post_params
= "suggest-post-params";
212 data
.instant_url_post_params
= "instant-post-params";
213 data
.image_url_post_params
= "image-post-params";
215 data
.favicon_url
= GURL("http://example.com/favicon.ico");
216 data
.new_tab_url
= "http://example.com/newtab.html";
217 data
.alternate_urls
.push_back("http://example.com/s?q={searchTerms}");
219 data
.short_name
= base::ASCIIToUTF16("name");
220 data
.SetKeyword(base::ASCIIToUTF16("keyword"));
221 data
.search_terms_replacement_key
= "search-terms-replacment-key";
222 data
.prepopulate_id
= 42;
223 data
.input_encodings
.push_back("UTF-8");
224 data
.safe_for_autoreplace
= true;
226 return scoped_ptr
<TemplateURL
>(new TemplateURL(profile(), data
));
229 void ExpectNoPendingBrandcodedConfigFetch() {
230 EXPECT_FALSE(test_url_fetcher_factory_
.GetFetcherByID(0));
233 void ExpectAndServicePendingBrandcodedConfigFetch(bool simulate_failure
) {
234 net::TestURLFetcher
* fetcher
= test_url_fetcher_factory_
.GetFetcherByID(0);
235 ASSERT_TRUE(fetcher
);
236 EXPECT_THAT(fetcher
->upload_data(),
237 testing::HasSubstr(kTestBrandcode
));
238 ServicePendingBrancodedConfigFetch(fetcher
, simulate_failure
);
241 void ExpectResetPromptState(bool active
) {
242 GlobalErrorService
* global_error_service
=
243 GlobalErrorServiceFactory::GetForProfile(profile());
244 GlobalError
* global_error
= global_error_service
->
245 GetGlobalErrorByMenuItemCommandID(IDC_SHOW_SETTINGS_RESET_BUBBLE
);
246 EXPECT_EQ(active
, !!global_error
);
249 AutomaticProfileResetterDelegateUnderTest
* resetter_delegate() {
250 return resetter_delegate_
.get();
253 // TemplateURLServiceTestUtilBase:
254 virtual TestingProfile
* profile() const OVERRIDE
{ return profile_
.get(); }
257 net::TestURLFetcherFactory test_url_fetcher_factory_
;
258 scoped_ptr
<AutomaticProfileResetterDelegateUnderTest
> resetter_delegate_
;
260 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateTest
);
264 // Tests ---------------------------------------------------------------------
266 TEST_F(AutomaticProfileResetterDelegateTest
,
267 TriggerAndWaitOnModuleEnumeration
) {
268 // Expect ready_callback to be called just after the modules have been
269 // enumerated. Fail if it is not called. Note: as the EnumerateModulesModel is
270 // a global singleton, the callback might be invoked immediately if another
271 // test-case (e.g. the one below) has already performed module enumeration.
272 testing::StrictMock
<MockCallbackTarget
> mock_target
;
273 EXPECT_CALL(mock_target
, Run());
274 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated(
275 mock_target
.CreateClosure());
276 resetter_delegate()->EnumerateLoadedModulesIfNeeded();
277 base::RunLoop().RunUntilIdle();
279 testing::Mock::VerifyAndClearExpectations(&mock_target
);
281 // Expect ready_callback to be posted immediately when the modules have
282 // already been enumerated.
283 EXPECT_CALL(mock_target
, Run());
284 resetter_delegate()->RequestCallbackWhenLoadedModulesAreEnumerated(
285 mock_target
.CreateClosure());
286 base::RunLoop().RunUntilIdle();
289 testing::Mock::VerifyAndClearExpectations(&mock_target
);
291 // Expect ready_callback to be posted immediately even when the modules had
292 // already been enumerated when the delegate was constructed.
293 scoped_ptr
<AutomaticProfileResetterDelegate
> late_resetter_delegate(
294 new AutomaticProfileResetterDelegateImpl(profile(),
295 ProfileResetter::ALL
));
297 EXPECT_CALL(mock_target
, Run());
298 late_resetter_delegate
->RequestCallbackWhenLoadedModulesAreEnumerated(
299 mock_target
.CreateClosure());
300 base::RunLoop().RunUntilIdle();
304 TEST_F(AutomaticProfileResetterDelegateTest
, GetLoadedModuleNameDigests
) {
305 resetter_delegate()->EnumerateLoadedModulesIfNeeded();
306 base::RunLoop().RunUntilIdle();
307 scoped_ptr
<base::ListValue
> module_name_digests(
308 resetter_delegate()->GetLoadedModuleNameDigests());
310 // Just verify that each element looks like an MD5 hash in hexadecimal, and
311 // also that we have at least one element on Win.
312 ASSERT_TRUE(module_name_digests
);
313 for (base::ListValue::const_iterator it
= module_name_digests
->begin();
314 it
!= module_name_digests
->end(); ++it
) {
315 std::string digest_hex
;
316 std::vector
<uint8
> digest_raw
;
318 ASSERT_TRUE((*it
)->GetAsString(&digest_hex
));
319 ASSERT_TRUE(base::HexStringToBytes(digest_hex
, &digest_raw
));
320 EXPECT_EQ(16u, digest_raw
.size());
323 EXPECT_LE(1u, module_name_digests
->GetSize());
327 TEST_F(AutomaticProfileResetterDelegateTest
, LoadAndWaitOnTemplateURLService
) {
328 // Expect ready_callback to be called just after the template URL service gets
329 // initialized. Fail if it is not called, or called too early.
330 testing::StrictMock
<MockCallbackTarget
> mock_target
;
331 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded(
332 mock_target
.CreateClosure());
333 base::RunLoop().RunUntilIdle();
335 EXPECT_CALL(mock_target
, Run());
336 resetter_delegate()->LoadTemplateURLServiceIfNeeded();
337 base::RunLoop().RunUntilIdle();
339 testing::Mock::VerifyAndClearExpectations(&mock_target
);
341 // Expect ready_callback to be posted immediately when the template URL
342 // service is already initialized.
343 EXPECT_CALL(mock_target
, Run());
344 resetter_delegate()->RequestCallbackWhenTemplateURLServiceIsLoaded(
345 mock_target
.CreateClosure());
346 base::RunLoop().RunUntilIdle();
348 testing::Mock::VerifyAndClearExpectations(&mock_target
);
350 // Expect ready_callback to be posted immediately even when the template URL
351 // service had already been initialized when the delegate was constructed.
352 scoped_ptr
<AutomaticProfileResetterDelegate
> late_resetter_delegate(
353 new AutomaticProfileResetterDelegateImpl(profile(),
354 ProfileResetter::ALL
));
356 EXPECT_CALL(mock_target
, Run());
357 late_resetter_delegate
->RequestCallbackWhenTemplateURLServiceIsLoaded(
358 mock_target
.CreateClosure());
359 base::RunLoop().RunUntilIdle();
362 TEST_F(AutomaticProfileResetterDelegateTest
,
363 DefaultSearchProviderDataWhenNotManaged
) {
364 TemplateURLService
* template_url_service
=
365 TemplateURLServiceFactory::GetForProfile(profile());
366 TemplateURLServiceTestUtilBase::VerifyLoad();
368 // Check that the "managed state" and the details returned by the delegate are
369 // correct. We verify the details against the data stored by
370 // TemplateURLService into Prefs.
371 scoped_ptr
<TemplateURL
> owned_custom_dsp(CreateTestTemplateURL());
372 TemplateURL
* custom_dsp
= owned_custom_dsp
.get();
373 template_url_service
->Add(owned_custom_dsp
.release());
374 template_url_service
->SetDefaultSearchProvider(custom_dsp
);
376 PrefService
* prefs
= profile()->GetPrefs();
378 scoped_ptr
<base::DictionaryValue
> dsp_details(
379 resetter_delegate()->GetDefaultSearchProviderDetails());
380 scoped_ptr
<base::DictionaryValue
> expected_dsp_details(
381 GetDefaultSearchProviderDetailsFromPrefs(prefs
));
383 ExpectDetailsMatch(*expected_dsp_details
, *dsp_details
);
384 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged());
387 TEST_F(AutomaticProfileResetterDelegateTest
,
388 DefaultSearchProviderDataWhenManaged
) {
389 const char kTestSearchURL
[] = "http://example.com/search?q={searchTerms}";
390 const char kTestName
[] = "name";
391 const char kTestKeyword
[] = "keyword";
393 TemplateURLServiceTestUtilBase::VerifyLoad();
395 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged());
397 // Set managed preferences to emulate a default search provider set by policy.
398 SetManagedDefaultSearchPreferences(
399 true, kTestName
, kTestKeyword
, kTestSearchURL
, std::string(),
400 std::string(), std::string(), std::string(), std::string());
402 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
403 scoped_ptr
<base::DictionaryValue
> dsp_details(
404 resetter_delegate()->GetDefaultSearchProviderDetails());
405 // Checking that all details are correct is already done by the above test.
406 // Just make sure details are reported about the correct engine.
407 base::ExpectDictStringValue(kTestSearchURL
, *dsp_details
, "search_url");
409 // Set managed preferences to emulate that having a default search provider is
410 // disabled by policy.
411 RemoveManagedDefaultSearchPreferences();
412 SetManagedDefaultSearchPreferences(
413 true, std::string(), std::string(), std::string(), std::string(),
414 std::string(), std::string(), std::string(), std::string());
416 dsp_details
= resetter_delegate()->GetDefaultSearchProviderDetails();
417 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged());
418 EXPECT_TRUE(dsp_details
->empty());
421 TEST_F(AutomaticProfileResetterDelegateTest
,
422 GetPrepopulatedSearchProvidersDetails
) {
423 TemplateURLService
* template_url_service
=
424 TemplateURLServiceFactory::GetForProfile(profile());
425 TemplateURLServiceTestUtilBase::VerifyLoad();
427 scoped_ptr
<base::ListValue
> search_engines_details(
428 resetter_delegate()->GetPrepopulatedSearchProvidersDetails());
430 // Do the same kind of verification as for GetDefaultSearchEngineDetails:
431 // subsequently set each pre-populated engine as the default, so we can verify
432 // that the details returned by the delegate about one particular engine are
433 // correct in comparison to what has been stored to the Prefs.
434 std::vector
<TemplateURL
*> prepopulated_engines
=
435 template_url_service
->GetTemplateURLs();
437 ASSERT_EQ(prepopulated_engines
.size(), search_engines_details
->GetSize());
439 for (size_t i
= 0; i
< search_engines_details
->GetSize(); ++i
) {
440 const base::DictionaryValue
* details
= NULL
;
441 ASSERT_TRUE(search_engines_details
->GetDictionary(i
, &details
));
444 ASSERT_TRUE(details
->GetString("keyword", &keyword
));
445 TemplateURL
* search_engine
=
446 template_url_service
->GetTemplateURLForKeyword(
447 base::ASCIIToUTF16(keyword
));
448 ASSERT_TRUE(search_engine
);
449 template_url_service
->SetDefaultSearchProvider(prepopulated_engines
[i
]);
451 PrefService
* prefs
= profile()->GetPrefs();
453 scoped_ptr
<base::DictionaryValue
> expected_dsp_details(
454 GetDefaultSearchProviderDetailsFromPrefs(prefs
));
455 ExpectDetailsMatch(*expected_dsp_details
, *details
);
459 TEST_F(AutomaticProfileResetterDelegateTest
,
460 FetchAndWaitOnDefaultSettingsVanilla
) {
461 google_util::BrandForTesting
scoped_brand_for_testing((std::string()));
463 // Expect ready_callback to be called just after empty brandcoded settings
464 // are loaded, given this is a vanilla build. Fail if it is not called, or
466 testing::StrictMock
<MockCallbackTarget
> mock_target
;
467 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched(
468 mock_target
.CreateClosure());
469 base::RunLoop().RunUntilIdle();
470 EXPECT_FALSE(resetter_delegate()->brandcoded_defaults());
472 EXPECT_CALL(mock_target
, Run());
473 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded();
474 base::RunLoop().RunUntilIdle();
475 ExpectNoPendingBrandcodedConfigFetch();
477 testing::Mock::VerifyAndClearExpectations(&mock_target
);
478 EXPECT_TRUE(resetter_delegate()->brandcoded_defaults());
480 // Expect ready_callback to be posted immediately when the brandcoded settings
481 // have already been loaded.
482 EXPECT_CALL(mock_target
, Run());
483 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched(
484 mock_target
.CreateClosure());
485 base::RunLoop().RunUntilIdle();
487 // No test for a new instance of AutomaticProfileResetterDelegate. That will
488 // need to fetch the brandcoded settings again.
491 TEST_F(AutomaticProfileResetterDelegateTest
,
492 FetchAndWaitOnDefaultSettingsBranded
) {
493 google_util::BrandForTesting
scoped_brand_for_testing(kTestBrandcode
);
495 // Expect ready_callback to be called just after the brandcoded settings are
496 // downloaded. Fail if it is not called, or called too early.
497 testing::StrictMock
<MockCallbackTarget
> mock_target
;
498 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched(
499 mock_target
.CreateClosure());
500 base::RunLoop().RunUntilIdle();
501 EXPECT_FALSE(resetter_delegate()->brandcoded_defaults());
503 EXPECT_CALL(mock_target
, Run());
504 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded();
505 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/);
506 base::RunLoop().RunUntilIdle();
508 testing::Mock::VerifyAndClearExpectations(&mock_target
);
509 const BrandcodedDefaultSettings
* brandcoded_defaults
=
510 resetter_delegate()->brandcoded_defaults();
511 ASSERT_TRUE(brandcoded_defaults
);
512 std::string homepage_url
;
513 EXPECT_TRUE(brandcoded_defaults
->GetHomepage(&homepage_url
));
514 EXPECT_EQ(kTestBrandedHomepage
, homepage_url
);
516 // Expect ready_callback to be posted immediately when the brandcoded settings
517 // have already been downloaded.
518 EXPECT_CALL(mock_target
, Run());
519 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched(
520 mock_target
.CreateClosure());
521 base::RunLoop().RunUntilIdle();
524 TEST_F(AutomaticProfileResetterDelegateTest
,
525 FetchAndWaitOnDefaultSettingsBrandedFailure
) {
526 google_util::BrandForTesting
scoped_brand_for_testing(kTestBrandcode
);
528 // Expect ready_callback to be called just after the brandcoded settings have
529 // failed to download. Fail if it is not called, or called too early.
530 testing::StrictMock
<MockCallbackTarget
> mock_target
;
531 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched(
532 mock_target
.CreateClosure());
533 base::RunLoop().RunUntilIdle();
535 EXPECT_CALL(mock_target
, Run());
536 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded();
537 ExpectAndServicePendingBrandcodedConfigFetch(true /*simulate_failure*/);
538 base::RunLoop().RunUntilIdle();
540 testing::Mock::VerifyAndClearExpectations(&mock_target
);
541 EXPECT_TRUE(resetter_delegate()->brandcoded_defaults());
543 // Expect ready_callback to be posted immediately when the brandcoded settings
544 // have already been attempted to be downloaded, but failed.
545 EXPECT_CALL(mock_target
, Run());
546 resetter_delegate()->RequestCallbackWhenBrandcodedDefaultsAreFetched(
547 mock_target
.CreateClosure());
548 base::RunLoop().RunUntilIdle();
551 TEST_F(AutomaticProfileResetterDelegateTest
, TriggerReset
) {
552 google_util::BrandForTesting
scoped_brand_for_testing(kTestBrandcode
);
554 PrefService
* prefs
= profile()->GetPrefs();
556 prefs
->SetString(prefs::kHomePage
, kTestHomepage
);
558 testing::StrictMock
<MockCallbackTarget
> mock_target
;
559 EXPECT_CALL(mock_target
, Run());
560 EXPECT_CALL(*resetter_delegate(), SendFeedback(testing::_
)).Times(0);
561 resetter_delegate()->TriggerProfileSettingsReset(
562 false /*send_feedback*/, mock_target
.CreateClosure());
563 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/);
564 base::RunLoop().RunUntilIdle();
566 EXPECT_EQ(kTestBrandedHomepage
, prefs
->GetString(prefs::kHomePage
));
569 TEST_F(AutomaticProfileResetterDelegateTest
,
570 TriggerResetWithDefaultSettingsAlreadyLoaded
) {
571 google_util::BrandForTesting
scoped_brand_for_testing(kTestBrandcode
);
573 PrefService
* prefs
= profile()->GetPrefs();
575 prefs
->SetString(prefs::kHomePage
, kTestHomepage
);
577 resetter_delegate()->FetchBrandcodedDefaultSettingsIfNeeded();
578 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/);
579 base::RunLoop().RunUntilIdle();
581 testing::StrictMock
<MockCallbackTarget
> mock_target
;
582 EXPECT_CALL(mock_target
, Run());
583 EXPECT_CALL(*resetter_delegate(), SendFeedback(testing::_
)).Times(0);
584 resetter_delegate()->TriggerProfileSettingsReset(
585 false /*send_feedback*/, mock_target
.CreateClosure());
586 base::RunLoop().RunUntilIdle();
588 EXPECT_EQ(kTestBrandedHomepage
, prefs
->GetString(prefs::kHomePage
));
591 TEST_F(AutomaticProfileResetterDelegateTest
,
592 TriggerResetAndSendFeedback
) {
593 google_util::BrandForTesting
scoped_brand_for_testing(kTestBrandcode
);
595 PrefService
* prefs
= profile()->GetPrefs();
597 prefs
->SetString(prefs::kHomePage
, kTestHomepage
);
599 testing::StrictMock
<MockCallbackTarget
> mock_target
;
600 EXPECT_CALL(mock_target
, Run());
601 EXPECT_CALL(*resetter_delegate(),
602 SendFeedback(testing::HasSubstr(kTestHomepage
)));
604 resetter_delegate()->TriggerProfileSettingsReset(
605 true /*send_feedback*/, mock_target
.CreateClosure());
606 ExpectAndServicePendingBrandcodedConfigFetch(false /*simulate_failure*/);
607 base::RunLoop().RunUntilIdle();
610 TEST_F(AutomaticProfileResetterDelegateTest
, ShowAndDismissPrompt
) {
611 resetter_delegate()->TriggerPrompt();
612 if (ProfileResetGlobalError::IsSupportedOnPlatform())
613 ExpectResetPromptState(true /*active*/);
615 ExpectResetPromptState(false /*active*/);
616 resetter_delegate()->DismissPrompt();
617 ExpectResetPromptState(false /*active*/);
618 resetter_delegate()->DismissPrompt();