Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: chrome/browser/profile_resetter/automatic_profile_resetter_unittest.cc

Issue 50883003: Added the ability to specify AutomaticProfileResetter's evaluation program through field trials. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/profile_resetter/automatic_profile_resetter_factory.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 set_inject_data_through_variation_params(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),
vabr (Chromium) 2013/11/04 14:16:40 optional: For better reading and maintainability,
engedy 2013/11/04 16:19:38 Yes, there is actually a couple more of these magi
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 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
769 EXPECT_CALL(mock_delegate(), ShowPrompt()); 787 EXPECT_CALL(mock_delegate(), ShowPrompt());
770 EXPECT_CALL(resetter(), ReportStatistics(0x02u, 0x01u)); 788 EXPECT_CALL(resetter(), ReportStatistics(0x02u, 0x01u));
771 789
772 UnleashResetterAndWait(); 790 UnleashResetterAndWait();
773 791
774 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); 792 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue());
775 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); 793 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue());
776 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); 794 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue());
777 } 795 }
778 796
797 #if defined(GOOGLE_CHROME_BUILD)
798 TEST_F(AutomaticProfileResetterTest, ProgramSetThroughVariationParams) {
799 PreferenceHostedPromptMemento memento_in_prefs(profile());
800 LocalStateHostedPromptMemento memento_in_local_state(profile());
801 FileHostedPromptMementoSynchronous memento_in_file(profile());
802
803 EXPECT_EQ("", memento_in_prefs.ReadValue());
804 EXPECT_EQ("", memento_in_local_state.ReadValue());
805 EXPECT_EQ("", memento_in_file.ReadValue());
806
807 SetTestingProgram(ConstructProgram(true, true));
808 SetTestingHashSeed(kTestHashSeed);
809 set_inject_data_through_variation_params(true);
810
811 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
812 mock_delegate().ExpectCallsToGetterMethods();
813 EXPECT_CALL(mock_delegate(), ShowPrompt());
814 EXPECT_CALL(resetter(), ReportStatistics(0x03u, 0x01u));
815
816 UnleashResetterAndWait();
817
818 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue());
819 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue());
820 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue());
821 }
822 #endif
823
779 TEST_F(AutomaticProfileResetterTest, ConditionsSatisfiedAndInvalidMementos) { 824 TEST_F(AutomaticProfileResetterTest, ConditionsSatisfiedAndInvalidMementos) {
780 PreferenceHostedPromptMemento memento_in_prefs(profile()); 825 PreferenceHostedPromptMemento memento_in_prefs(profile());
781 LocalStateHostedPromptMemento memento_in_local_state(profile()); 826 LocalStateHostedPromptMemento memento_in_local_state(profile());
782 FileHostedPromptMementoSynchronous memento_in_file(profile()); 827 FileHostedPromptMementoSynchronous memento_in_file(profile());
783 828
784 memento_in_prefs.StoreValue(kTestInvalidMementoValue); 829 memento_in_prefs.StoreValue(kTestInvalidMementoValue);
785 memento_in_local_state.StoreValue(kTestInvalidMementoValue); 830 memento_in_local_state.StoreValue(kTestInvalidMementoValue);
786 memento_in_file.StoreValue(kTestInvalidMementoValue); 831 memento_in_file.StoreValue(kTestInvalidMementoValue);
787 832
788 SetTestingProgram(ConstructProgram(true, true)); 833 SetTestingProgram(ConstructProgram(true, true));
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 mock_delegate().ExpectCallsToDependenciesSetUpMethods(); 1063 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
1019 mock_delegate().ExpectCallsToGetterMethods(); 1064 mock_delegate().ExpectCallsToGetterMethods();
1020 uint32 expected_mask = 1065 uint32 expected_mask =
1021 HAS_EXPECTED_MODULE_DIGEST_1 | HAS_EXPECTED_MODULE_DIGEST_2; 1066 HAS_EXPECTED_MODULE_DIGEST_1 | HAS_EXPECTED_MODULE_DIGEST_2;
1022 EXPECT_CALL(resetter(), ReportStatistics(0x00u, expected_mask)); 1067 EXPECT_CALL(resetter(), ReportStatistics(0x00u, expected_mask));
1023 1068
1024 UnleashResetterAndWait(); 1069 UnleashResetterAndWait();
1025 } 1070 }
1026 1071
1027 } // namespace 1072 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/profile_resetter/automatic_profile_resetter_factory.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698