OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/profile_resetter/automatic_profile_resetter.h" | 5 #include "chrome/browser/profile_resetter/automatic_profile_resetter.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
12 #include "base/prefs/pref_registry_simple.h" | 12 #include "base/prefs/pref_registry_simple.h" |
13 #include "base/prefs/testing_pref_service.h" | 13 #include "base/prefs/testing_pref_service.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/test/test_simple_task_runner.h" | 15 #include "base/test/test_simple_task_runner.h" |
16 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" | 18 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" |
19 #include "chrome/browser/profile_resetter/automatic_profile_resetter_factory.h" | 19 #include "chrome/browser/profile_resetter/automatic_profile_resetter_factory.h" |
20 #include "chrome/browser/profile_resetter/automatic_profile_resetter_mementos.h" | 20 #include "chrome/browser/profile_resetter/automatic_profile_resetter_mementos.h" |
21 #include "chrome/browser/profile_resetter/jtl_foundation.h" | 21 #include "chrome/browser/profile_resetter/jtl_foundation.h" |
22 #include "chrome/browser/profile_resetter/jtl_instructions.h" | 22 #include "chrome/browser/profile_resetter/jtl_instructions.h" |
23 #include "chrome/test/base/scoped_testing_local_state.h" | 23 #include "chrome/test/base/scoped_testing_local_state.h" |
24 #include "chrome/test/base/testing_browser_process.h" | 24 #include "chrome/test/base/testing_browser_process.h" |
25 #include "chrome/test/base/testing_pref_service_syncable.h" | 25 #include "chrome/test/base/testing_pref_service_syncable.h" |
26 #include "chrome/test/base/testing_profile.h" | 26 #include "chrome/test/base/testing_profile.h" |
27 #include "components/user_prefs/pref_registry_syncable.h" | 27 #include "components/user_prefs/pref_registry_syncable.h" |
| 28 #include "components/variations/variations_associated_data.h" |
28 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
29 #include "content/public/test/test_browser_thread_bundle.h" | 30 #include "content/public/test/test_browser_thread_bundle.h" |
30 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
32 | 33 |
33 using testing::_; | 34 using testing::_; |
34 using testing::Invoke; | 35 using testing::Invoke; |
35 | 36 |
36 namespace { | 37 namespace { |
37 | 38 |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 // Test fixtures ------------------------------------------------------------- | 401 // Test fixtures ------------------------------------------------------------- |
401 | 402 |
402 class AutomaticProfileResetterTestBase : public testing::Test { | 403 class AutomaticProfileResetterTestBase : public testing::Test { |
403 protected: | 404 protected: |
404 explicit AutomaticProfileResetterTestBase( | 405 explicit AutomaticProfileResetterTestBase( |
405 const std::string& experiment_group_name) | 406 const std::string& experiment_group_name) |
406 : waiting_task_runner_(new base::TestSimpleTaskRunner), | 407 : waiting_task_runner_(new base::TestSimpleTaskRunner), |
407 local_state_(TestingBrowserProcess::GetGlobal()), | 408 local_state_(TestingBrowserProcess::GetGlobal()), |
408 profile_(new TestingProfile()), | 409 profile_(new TestingProfile()), |
409 experiment_group_name_(experiment_group_name), | 410 experiment_group_name_(experiment_group_name), |
| 411 inject_data_through_variation_params_(false), |
410 mock_delegate_(NULL) { | 412 mock_delegate_(NULL) { |
411 // Make sure the factory is not optimized away, so whatever preferences it | 413 // Make sure the factory is not optimized away, so whatever preferences it |
412 // wants to register will actually get registered. | 414 // wants to register will actually get registered. |
413 AutomaticProfileResetterFactory::GetInstance(); | 415 AutomaticProfileResetterFactory::GetInstance(); |
414 | 416 |
415 // Register some additional local state preferences for testing purposes. | 417 // Register some additional local state preferences for testing purposes. |
416 PrefRegistrySimple* local_state_registry = local_state_.Get()->registry(); | 418 PrefRegistrySimple* local_state_registry = local_state_.Get()->registry(); |
417 DCHECK(local_state_registry); | 419 DCHECK(local_state_registry); |
418 local_state_registry->RegisterStringPref(kTestPreferencePath, ""); | 420 local_state_registry->RegisterStringPref(kTestPreferencePath, ""); |
419 | 421 |
420 // Register some additional user preferences for testing purposes. | 422 // Register some additional user preferences for testing purposes. |
421 user_prefs::PrefRegistrySyncable* user_prefs_registry = | 423 user_prefs::PrefRegistrySyncable* user_prefs_registry = |
422 profile_->GetTestingPrefService()->registry(); | 424 profile_->GetTestingPrefService()->registry(); |
423 DCHECK(user_prefs_registry); | 425 DCHECK(user_prefs_registry); |
424 user_prefs_registry->RegisterStringPref( | 426 user_prefs_registry->RegisterStringPref( |
425 kTestPreferencePath, | 427 kTestPreferencePath, |
426 "", | 428 "", |
427 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 429 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
428 } | 430 } |
429 | 431 |
430 virtual void SetUp() OVERRIDE { | 432 virtual void SetUp() OVERRIDE { |
431 field_trials_.reset(new base::FieldTrialList(NULL)); | 433 field_trials_.reset(new base::FieldTrialList(NULL)); |
| 434 chrome_variations::testing::ClearAllVariationParams(); |
432 base::FieldTrialList::CreateFieldTrial(kAutomaticProfileResetStudyName, | 435 base::FieldTrialList::CreateFieldTrial(kAutomaticProfileResetStudyName, |
433 experiment_group_name_); | 436 experiment_group_name_); |
434 | 437 resetter_.reset( |
435 resetter_.reset(new testing::StrictMock<AutomaticProfileResetterUnderTest>( | 438 new testing::StrictMock<AutomaticProfileResetterUnderTest>(profile())); |
436 profile_.get())); | 439 mock_delegate_owned_.reset( |
437 | |
438 scoped_ptr<MockProfileResetterDelegate> mock_delegate( | |
439 new testing::StrictMock<MockProfileResetterDelegate>()); | 440 new testing::StrictMock<MockProfileResetterDelegate>()); |
440 mock_delegate_ = mock_delegate.get(); | 441 mock_delegate_ = mock_delegate_owned_.get(); |
441 resetter_->SetDelegateForTesting( | |
442 mock_delegate.PassAs<AutomaticProfileResetterDelegate>()); | |
443 resetter_->SetTaskRunnerForWaitingForTesting(waiting_task_runner_); | |
444 } | 442 } |
445 | 443 |
446 void SetTestingHashSeed(const std::string& hash_seed) { | 444 void SetTestingHashSeed(const std::string& hash_seed) { |
447 testing_hash_seed_ = hash_seed; | 445 testing_hash_seed_ = hash_seed; |
448 } | 446 } |
449 | 447 |
450 void SetTestingProgram(const std::string& source_code) { | 448 void SetTestingProgram(const std::string& source_code) { |
451 testing_program_ = source_code; | 449 testing_program_ = source_code; |
452 } | 450 } |
453 | 451 |
| 452 void AllowInjectingTestDataThroughVariationParams(bool value) { |
| 453 inject_data_through_variation_params_ = value; |
| 454 } |
| 455 |
454 void UnleashResetterAndWait() { | 456 void UnleashResetterAndWait() { |
455 resetter_->SetHashSeedForTesting(testing_hash_seed_); | 457 if (inject_data_through_variation_params_) { |
456 resetter_->SetProgramForTesting(testing_program_); | 458 std::map<std::string, std::string> variation_params; |
457 | 459 variation_params["program"] = testing_program_; |
| 460 variation_params["hash_seed"] = testing_hash_seed_; |
| 461 ASSERT_TRUE(chrome_variations::AssociateVariationParams( |
| 462 kAutomaticProfileResetStudyName, |
| 463 experiment_group_name_, |
| 464 variation_params)); |
| 465 } |
| 466 resetter_->Initialize(); |
| 467 resetter_->SetDelegateForTesting( |
| 468 mock_delegate_owned_.PassAs<AutomaticProfileResetterDelegate>()); |
| 469 resetter_->SetTaskRunnerForWaitingForTesting(waiting_task_runner_); |
| 470 if (!inject_data_through_variation_params_) { |
| 471 resetter_->SetProgramForTesting(testing_program_); |
| 472 resetter_->SetHashSeedForTesting(testing_hash_seed_); |
| 473 } |
458 resetter_->Activate(); | 474 resetter_->Activate(); |
459 | 475 |
460 if (waiting_task_runner_->HasPendingTask()) { | 476 if (waiting_task_runner_->HasPendingTask()) { |
461 EXPECT_EQ(base::TimeDelta::FromSeconds(55), | 477 ASSERT_EQ(base::TimeDelta::FromSeconds(55), |
462 waiting_task_runner_->NextPendingTaskDelay()); | 478 waiting_task_runner_->NextPendingTaskDelay()); |
463 waiting_task_runner_->RunPendingTasks(); | 479 waiting_task_runner_->RunPendingTasks(); |
464 } | 480 } |
465 base::RunLoop().RunUntilIdle(); | 481 base::RunLoop().RunUntilIdle(); |
466 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 482 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
467 base::RunLoop().RunUntilIdle(); | 483 base::RunLoop().RunUntilIdle(); |
468 } | 484 } |
469 | 485 |
470 TestingProfile* profile() { return profile_.get(); } | 486 TestingProfile* profile() { return profile_.get(); } |
471 TestingPrefServiceSimple* local_state() { return local_state_.Get(); } | 487 TestingPrefServiceSimple* local_state() { return local_state_.Get(); } |
472 | 488 |
473 MockProfileResetterDelegate& mock_delegate() { return *mock_delegate_; } | 489 MockProfileResetterDelegate& mock_delegate() { return *mock_delegate_; } |
474 AutomaticProfileResetterUnderTest& resetter() { return *resetter_; } | 490 AutomaticProfileResetterUnderTest& resetter() { return *resetter_; } |
475 | 491 |
476 private: | 492 private: |
477 content::TestBrowserThreadBundle thread_bundle_; | 493 content::TestBrowserThreadBundle thread_bundle_; |
478 scoped_refptr<base::TestSimpleTaskRunner> waiting_task_runner_; | 494 scoped_refptr<base::TestSimpleTaskRunner> waiting_task_runner_; |
479 ScopedTestingLocalState local_state_; | 495 ScopedTestingLocalState local_state_; |
480 scoped_ptr<TestingProfile> profile_; | 496 scoped_ptr<TestingProfile> profile_; |
481 scoped_ptr<base::FieldTrialList> field_trials_; | 497 scoped_ptr<base::FieldTrialList> field_trials_; |
482 std::string experiment_group_name_; | 498 std::string experiment_group_name_; |
483 std::string testing_program_; | 499 std::string testing_program_; |
484 std::string testing_hash_seed_; | 500 std::string testing_hash_seed_; |
| 501 bool inject_data_through_variation_params_; |
485 | 502 |
486 scoped_ptr<AutomaticProfileResetterUnderTest> resetter_; | 503 scoped_ptr<AutomaticProfileResetterUnderTest> resetter_; |
| 504 scoped_ptr<MockProfileResetterDelegate> mock_delegate_owned_; |
487 MockProfileResetterDelegate* mock_delegate_; | 505 MockProfileResetterDelegate* mock_delegate_; |
488 | 506 |
489 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterTestBase); | 507 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterTestBase); |
490 }; | 508 }; |
491 | 509 |
492 class AutomaticProfileResetterTest : public AutomaticProfileResetterTestBase { | 510 class AutomaticProfileResetterTest : public AutomaticProfileResetterTestBase { |
493 protected: | 511 protected: |
494 AutomaticProfileResetterTest() | 512 AutomaticProfileResetterTest() |
495 : AutomaticProfileResetterTestBase(kStudyEnabledGroupName) {} | 513 : AutomaticProfileResetterTestBase(kStudyEnabledGroupName) {} |
496 }; | 514 }; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 mock_delegate().ExpectCallsToGetterMethods(); | 612 mock_delegate().ExpectCallsToGetterMethods(); |
595 EXPECT_CALL(resetter(), ReportStatistics(0x02u, 0x01u)); | 613 EXPECT_CALL(resetter(), ReportStatistics(0x02u, 0x01u)); |
596 | 614 |
597 UnleashResetterAndWait(); | 615 UnleashResetterAndWait(); |
598 | 616 |
599 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); | 617 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); |
600 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); | 618 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); |
601 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); | 619 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); |
602 } | 620 } |
603 | 621 |
| 622 #if defined(GOOGLE_CHROME_BUILD) |
| 623 TEST_F(AutomaticProfileResetterTestDryRun, ProgramSetThroughVariationParams) { |
| 624 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 625 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 626 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 627 |
| 628 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 629 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 630 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 631 |
| 632 SetTestingProgram(ConstructProgram(true, true)); |
| 633 SetTestingHashSeed(kTestHashSeed); |
| 634 AllowInjectingTestDataThroughVariationParams(true); |
| 635 |
| 636 mock_delegate().ExpectCallsToDependenciesSetUpMethods(); |
| 637 mock_delegate().ExpectCallsToGetterMethods(); |
| 638 EXPECT_CALL(resetter(), ReportStatistics(0x03u, 0x01u)); |
| 639 |
| 640 UnleashResetterAndWait(); |
| 641 |
| 642 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); |
| 643 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); |
| 644 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); |
| 645 } |
| 646 #endif |
| 647 |
604 TEST_F(AutomaticProfileResetterTestDryRun, | 648 TEST_F(AutomaticProfileResetterTestDryRun, |
605 ConditionsSatisfiedAndInvalidMementos) { | 649 ConditionsSatisfiedAndInvalidMementos) { |
606 PreferenceHostedPromptMemento memento_in_prefs(profile()); | 650 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
607 LocalStateHostedPromptMemento memento_in_local_state(profile()); | 651 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
608 FileHostedPromptMementoSynchronous memento_in_file(profile()); | 652 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
609 | 653 |
610 memento_in_prefs.StoreValue(kTestInvalidMementoValue); | 654 memento_in_prefs.StoreValue(kTestInvalidMementoValue); |
611 memento_in_local_state.StoreValue(kTestInvalidMementoValue); | 655 memento_in_local_state.StoreValue(kTestInvalidMementoValue); |
612 memento_in_file.StoreValue(kTestInvalidMementoValue); | 656 memento_in_file.StoreValue(kTestInvalidMementoValue); |
613 | 657 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
769 EXPECT_CALL(mock_delegate(), ShowPrompt()); | 813 EXPECT_CALL(mock_delegate(), ShowPrompt()); |
770 EXPECT_CALL(resetter(), ReportStatistics(0x02u, 0x01u)); | 814 EXPECT_CALL(resetter(), ReportStatistics(0x02u, 0x01u)); |
771 | 815 |
772 UnleashResetterAndWait(); | 816 UnleashResetterAndWait(); |
773 | 817 |
774 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); | 818 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); |
775 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); | 819 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); |
776 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); | 820 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); |
777 } | 821 } |
778 | 822 |
| 823 #if defined(GOOGLE_CHROME_BUILD) |
| 824 TEST_F(AutomaticProfileResetterTest, ProgramSetThroughVariationParams) { |
| 825 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 826 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 827 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 828 |
| 829 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 830 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 831 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 832 |
| 833 SetTestingProgram(ConstructProgram(true, true)); |
| 834 SetTestingHashSeed(kTestHashSeed); |
| 835 AllowInjectingTestDataThroughVariationParams(true); |
| 836 |
| 837 mock_delegate().ExpectCallsToDependenciesSetUpMethods(); |
| 838 mock_delegate().ExpectCallsToGetterMethods(); |
| 839 EXPECT_CALL(mock_delegate(), ShowPrompt()); |
| 840 EXPECT_CALL(resetter(), ReportStatistics(0x03u, 0x01u)); |
| 841 |
| 842 UnleashResetterAndWait(); |
| 843 |
| 844 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); |
| 845 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); |
| 846 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); |
| 847 } |
| 848 #endif |
| 849 |
779 TEST_F(AutomaticProfileResetterTest, ConditionsSatisfiedAndInvalidMementos) { | 850 TEST_F(AutomaticProfileResetterTest, ConditionsSatisfiedAndInvalidMementos) { |
780 PreferenceHostedPromptMemento memento_in_prefs(profile()); | 851 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
781 LocalStateHostedPromptMemento memento_in_local_state(profile()); | 852 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
782 FileHostedPromptMementoSynchronous memento_in_file(profile()); | 853 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
783 | 854 |
784 memento_in_prefs.StoreValue(kTestInvalidMementoValue); | 855 memento_in_prefs.StoreValue(kTestInvalidMementoValue); |
785 memento_in_local_state.StoreValue(kTestInvalidMementoValue); | 856 memento_in_local_state.StoreValue(kTestInvalidMementoValue); |
786 memento_in_file.StoreValue(kTestInvalidMementoValue); | 857 memento_in_file.StoreValue(kTestInvalidMementoValue); |
787 | 858 |
788 SetTestingProgram(ConstructProgram(true, true)); | 859 SetTestingProgram(ConstructProgram(true, true)); |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1018 mock_delegate().ExpectCallsToDependenciesSetUpMethods(); | 1089 mock_delegate().ExpectCallsToDependenciesSetUpMethods(); |
1019 mock_delegate().ExpectCallsToGetterMethods(); | 1090 mock_delegate().ExpectCallsToGetterMethods(); |
1020 uint32 expected_mask = | 1091 uint32 expected_mask = |
1021 HAS_EXPECTED_MODULE_DIGEST_1 | HAS_EXPECTED_MODULE_DIGEST_2; | 1092 HAS_EXPECTED_MODULE_DIGEST_1 | HAS_EXPECTED_MODULE_DIGEST_2; |
1022 EXPECT_CALL(resetter(), ReportStatistics(0x00u, expected_mask)); | 1093 EXPECT_CALL(resetter(), ReportStatistics(0x00u, expected_mask)); |
1023 | 1094 |
1024 UnleashResetterAndWait(); | 1095 UnleashResetterAndWait(); |
1025 } | 1096 } |
1026 | 1097 |
1027 } // namespace | 1098 } // namespace |
OLD | NEW |