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 "base/metrics/field_trial.h" |
| 6 #include "base/run_loop.h" |
| 7 #include "chrome/browser/profile_resetter/automatic_profile_resetter.h" |
| 8 #include "chrome/browser/profile_resetter/automatic_profile_resetter_factory.h" |
| 9 #include "chrome/browser/profile_resetter/automatic_profile_resetter_mementos.h" |
| 10 #include "chrome/common/extensions/value_builder.h" |
| 11 #include "chrome/test/base/scoped_testing_local_state.h" |
| 12 #include "chrome/test/base/testing_browser_process.h" |
| 13 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 using testing::_; |
| 20 using testing::Invoke; |
| 21 using testing::Return; |
| 22 |
| 23 namespace profile_resetter { |
| 24 namespace { |
| 25 |
| 26 const char kAutomaticProfileResetStudyName[] = "AutomaticProfileReset"; |
| 27 const char kStudyDisabledGroupName[] = "Disabled"; |
| 28 const char kStudyDryRunGroupName[] = "DryRun"; |
| 29 const char kStudyEnabledGroupName[] = "Enabled"; |
| 30 |
| 31 const char kTestHashSeed[] = "testing-hash-seed"; |
| 32 const char kTestMementoValue[] = "testing-menento-value"; |
| 33 |
| 34 // Helpers ------------------------------------------------------------------ |
| 35 |
| 36 class MockProfileResetterDelegate : public AutomaticProfileResetterDelegate { |
| 37 public: |
| 38 MockProfileResetterDelegate() {} |
| 39 virtual ~MockProfileResetterDelegate() {} |
| 40 |
| 41 MOCK_METHOD0(ShowPrompt, void()); |
| 42 MOCK_METHOD2(ReportStatistics, void(uint32, uint32)); |
| 43 |
| 44 private: |
| 45 DISALLOW_COPY_AND_ASSIGN(MockProfileResetterDelegate); |
| 46 }; |
| 47 |
| 48 class FileHostedPromptMementoSynchronous : protected FileHostedPromptMemento { |
| 49 public: |
| 50 explicit FileHostedPromptMementoSynchronous(Profile* profile) |
| 51 : FileHostedPromptMemento(profile) {} |
| 52 |
| 53 std::string ReadValue() const { |
| 54 std::string result; |
| 55 FileHostedPromptMemento::ReadValue(base::Bind(&AssignArgumentTo, &result)); |
| 56 base::RunLoop().RunUntilIdle(); |
| 57 return result; |
| 58 } |
| 59 |
| 60 void StoreValue(const std::string& value) { |
| 61 FileHostedPromptMemento::StoreValue(value); |
| 62 base::RunLoop().RunUntilIdle(); |
| 63 } |
| 64 |
| 65 private: |
| 66 static void AssignArgumentTo(std::string* target, const std::string& value) { |
| 67 *target = value; |
| 68 } |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(FileHostedPromptMementoSynchronous); |
| 71 }; |
| 72 |
| 73 // Test fixtures ------------------------------------------------------------- |
| 74 |
| 75 class AutomaticProfileResetterTestBase : public testing::Test { |
| 76 protected: |
| 77 explicit AutomaticProfileResetterTestBase( |
| 78 const std::string& experiment_group_name) |
| 79 : local_state_(TestingBrowserProcess::GetGlobal()), |
| 80 experiment_group_name_(experiment_group_name), |
| 81 mock_delegate_(NULL) { |
| 82 // Make sure the factory is not optimized away, so prefs get registered. |
| 83 AutomaticProfileResetterFactory::GetInstance(); |
| 84 } |
| 85 |
| 86 virtual void SetUp() OVERRIDE { |
| 87 profile_.reset(new TestingProfile()); |
| 88 field_trials_.reset(new base::FieldTrialList(NULL)); |
| 89 base::FieldTrialList::CreateFieldTrial(kAutomaticProfileResetStudyName, |
| 90 experiment_group_name_); |
| 91 mock_delegate_ = new testing::StrictMock<MockProfileResetterDelegate>(); |
| 92 resetter_.reset(new AutomaticProfileResetter(profile_.get())); |
| 93 } |
| 94 |
| 95 void UnleashResetterAndWait() { |
| 96 resetter_->Initialize(); |
| 97 resetter_->SetDelegateForTesting(mock_delegate_); // Takes ownership. |
| 98 resetter_->SetHashSeedForTesting(kTestHashSeed); |
| 99 resetter_->SetProgramForTesting(""); // TODO(engedy). |
| 100 base::RunLoop().RunUntilIdle(); |
| 101 } |
| 102 |
| 103 TestingProfile* profile() { return profile_.get(); } |
| 104 |
| 105 MockProfileResetterDelegate& mock_delegate() { return *mock_delegate_; } |
| 106 AutomaticProfileResetter* resetter() { return resetter_.get(); } |
| 107 |
| 108 private: |
| 109 content::TestBrowserThreadBundle thread_bundle_; |
| 110 ScopedTestingLocalState local_state_; |
| 111 scoped_ptr<TestingProfile> profile_; |
| 112 scoped_ptr<base::FieldTrialList> field_trials_; |
| 113 std::string experiment_group_name_; |
| 114 std::string fake_program_; |
| 115 |
| 116 scoped_ptr<AutomaticProfileResetter> resetter_; |
| 117 MockProfileResetterDelegate* mock_delegate_; |
| 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterTestBase); |
| 120 }; |
| 121 |
| 122 class AutomaticProfileResetterTest : public AutomaticProfileResetterTestBase { |
| 123 protected: |
| 124 AutomaticProfileResetterTest() |
| 125 : AutomaticProfileResetterTestBase(kStudyEnabledGroupName) {} |
| 126 }; |
| 127 |
| 128 class AutomaticProfileResetterDryRun : public AutomaticProfileResetterTestBase { |
| 129 protected: |
| 130 AutomaticProfileResetterDryRun() |
| 131 : AutomaticProfileResetterTestBase(kStudyDryRunGroupName) {} |
| 132 }; |
| 133 |
| 134 class AutomaticProfileResetterTestDisabled |
| 135 : public AutomaticProfileResetterTestBase { |
| 136 protected: |
| 137 AutomaticProfileResetterTestDisabled() |
| 138 : AutomaticProfileResetterTestBase(kStudyDisabledGroupName) {} |
| 139 }; |
| 140 |
| 141 // Tests --------------------------------------------------------------------- |
| 142 |
| 143 TEST_F(AutomaticProfileResetterTestDisabled, NothingIsDoneWhenDisabled) { |
| 144 // Nothing is expected to be done. |
| 145 UnleashResetterAndWait(); |
| 146 } |
| 147 |
| 148 TEST_F(AutomaticProfileResetterTest, ConditionNotSatisfied) { |
| 149 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 150 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 151 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 152 |
| 153 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 154 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 155 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 156 |
| 157 // The preference value that would trigger reset is not set, so ShowPrompt() |
| 158 // is not expected to be called, and mementos are not expected to be stored. |
| 159 |
| 160 UnleashResetterAndWait(); |
| 161 |
| 162 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 163 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 164 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 165 } |
| 166 |
| 167 TEST_F(AutomaticProfileResetterTest, ConditionSatisfied) { |
| 168 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 169 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 170 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 171 |
| 172 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 173 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 174 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 175 |
| 176 // TODO(engedy): Fake satisfied conditions. |
| 177 |
| 178 EXPECT_CALL(mock_delegate(), ShowPrompt()); |
| 179 |
| 180 UnleashResetterAndWait(); |
| 181 |
| 182 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); |
| 183 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); |
| 184 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); |
| 185 } |
| 186 |
| 187 TEST_F(AutomaticProfileResetterTest, ConditionNotSatisfiedButManagedPref) { |
| 188 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 189 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 190 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 191 |
| 192 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 193 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 194 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 195 |
| 196 // TODO(engedy): Fake satisfied conditions, but with a managed pref. |
| 197 |
| 198 // No prompt is expected to be shown. |
| 199 |
| 200 UnleashResetterAndWait(); |
| 201 |
| 202 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 203 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 204 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 205 } |
| 206 |
| 207 TEST_F(AutomaticProfileResetterTest, PrefHostedMementoPreventsPrompt) { |
| 208 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 209 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 210 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 211 |
| 212 memento_in_prefs.StoreValue(kTestMementoValue); |
| 213 |
| 214 // No prompt is expected to be shown. |
| 215 |
| 216 UnleashResetterAndWait(); |
| 217 |
| 218 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); |
| 219 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 220 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 221 } |
| 222 |
| 223 TEST_F(AutomaticProfileResetterTest, LocalStateHostedMementoPreventsPrompt) { |
| 224 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 225 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 226 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 227 |
| 228 memento_in_local_state.StoreValue(kTestMementoValue); |
| 229 |
| 230 // No prompt is expected to be shown. |
| 231 |
| 232 UnleashResetterAndWait(); |
| 233 |
| 234 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 235 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); |
| 236 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 237 } |
| 238 |
| 239 TEST_F(AutomaticProfileResetterTest, FileHostedMementoPreventsPrompt) { |
| 240 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 241 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 242 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 243 |
| 244 memento_in_file.StoreValue(kTestMementoValue); |
| 245 |
| 246 // No prompt is expected to be shown. |
| 247 |
| 248 UnleashResetterAndWait(); |
| 249 |
| 250 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 251 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 252 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); |
| 253 } |
| 254 |
| 255 } // namespace |
| 256 } // namespace profile_resetter |
OLD | NEW |