| 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/browser/profile_resetter/jtl_foundation.h" |
| 11 #include "chrome/browser/profile_resetter/jtl_instructions.h" |
| 12 #include "chrome/common/extensions/value_builder.h" |
| 13 #include "chrome/test/base/scoped_testing_local_state.h" |
| 14 #include "chrome/test/base/testing_browser_process.h" |
| 15 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 const char kAutomaticProfileResetStudyName[] = "AutomaticProfileReset"; |
| 25 const char kStudyDisabledGroupName[] = "Disabled"; |
| 26 const char kStudyDryRunGroupName[] = "DryRun"; |
| 27 const char kStudyEnabledGroupName[] = "Enabled"; |
| 28 |
| 29 const char kTestHashSeed[] = "testing-hash-seed"; |
| 30 const char kTestMementoValue[] = "01234567890123456789012345678901"; |
| 31 |
| 32 // Helpers ------------------------------------------------------------------ |
| 33 |
| 34 class MockProfileResetterDelegate : public AutomaticProfileResetterDelegate { |
| 35 public: |
| 36 MockProfileResetterDelegate() {} |
| 37 virtual ~MockProfileResetterDelegate() {} |
| 38 |
| 39 MOCK_METHOD0(ShowPrompt, void()); |
| 40 MOCK_METHOD2(ReportStatistics, void(uint32, uint32)); |
| 41 |
| 42 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(MockProfileResetterDelegate); |
| 44 }; |
| 45 |
| 46 class FileHostedPromptMementoSynchronous : protected FileHostedPromptMemento { |
| 47 public: |
| 48 explicit FileHostedPromptMementoSynchronous(Profile* profile) |
| 49 : FileHostedPromptMemento(profile) {} |
| 50 |
| 51 std::string ReadValue() const { |
| 52 std::string result; |
| 53 FileHostedPromptMemento::ReadValue(base::Bind(&AssignArgumentTo, &result)); |
| 54 base::RunLoop().RunUntilIdle(); |
| 55 return result; |
| 56 } |
| 57 |
| 58 void StoreValue(const std::string& value) { |
| 59 FileHostedPromptMemento::StoreValue(value); |
| 60 base::RunLoop().RunUntilIdle(); |
| 61 } |
| 62 |
| 63 private: |
| 64 static void AssignArgumentTo(std::string* target, const std::string& value) { |
| 65 *target = value; |
| 66 } |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(FileHostedPromptMementoSynchronous); |
| 69 }; |
| 70 |
| 71 std::string GetHash(const std::string& input) { |
| 72 return jtl_foundation::Hasher(kTestHashSeed).GetHash(input); |
| 73 } |
| 74 |
| 75 // Constructs a simple evaluation program to test that input/output works well. |
| 76 // It will emulate a scenario in which the reset criteria are satisfied as |
| 77 // specified by |emulate_satisfied_criterion_{1|2}|, and will set bits in the |
| 78 // combined status mask according to whether or not the memento value received |
| 79 // in the input was as expected. |
| 80 // |
| 81 // More specifically, the output of the program will be as follows: |
| 82 // { |
| 83 // "satisfied_criteria_mask_bit1": emulate_satisfied_criterion_1, |
| 84 // "satisfied_criteria_mask_bit2": emulate_satisfied_criterion_2, |
| 85 // "combined_status_mask_bit2": |
| 86 // (input["memento_value_in_prefs"] == kTestMementoValue), |
| 87 // "combined_status_mask_bit3": |
| 88 // (input["memento_value_in_local_state"] == kTestMementoValue), |
| 89 // "combined_status_mask_bit4": |
| 90 // (input["memento_value_in_file"] == kTestMementoValue), |
| 91 // "had_prompted_already": <OR-combination of above three>, |
| 92 // "memento_value_in_prefs": kTestMementoValue, |
| 93 // "memento_value_in_local_state": kTestMementoValue, |
| 94 // "memento_value_in_file": kTestMementoValue |
| 95 // } |
| 96 std::string ConstructProgram(bool emulate_satisfied_criterion_1, |
| 97 bool emulate_satisfied_criterion_2) { |
| 98 std::string bytecode; |
| 99 bytecode += OP_STORE_BOOL(GetHash("satisfied_criteria_mask_bit1"), |
| 100 (emulate_satisfied_criterion_1 ? VALUE_TRUE |
| 101 : VALUE_FALSE)); |
| 102 bytecode += OP_END_OF_SENTENCE; |
| 103 bytecode += OP_STORE_BOOL(GetHash("satisfied_criteria_mask_bit2"), |
| 104 (emulate_satisfied_criterion_2 ? VALUE_TRUE |
| 105 : VALUE_FALSE)); |
| 106 bytecode += OP_END_OF_SENTENCE; |
| 107 bytecode += OP_NAVIGATE(GetHash("memento_value_in_prefs")); |
| 108 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestMementoValue)); |
| 109 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit2"), VALUE_TRUE); |
| 110 bytecode += OP_STORE_BOOL(GetHash("had_prompted_already"), VALUE_TRUE); |
| 111 bytecode += OP_END_OF_SENTENCE; |
| 112 bytecode += OP_NAVIGATE(GetHash("memento_value_in_local_state")); |
| 113 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestMementoValue)); |
| 114 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit3"), VALUE_TRUE); |
| 115 bytecode += OP_STORE_BOOL(GetHash("had_prompted_already"), VALUE_TRUE); |
| 116 bytecode += OP_END_OF_SENTENCE; |
| 117 bytecode += OP_NAVIGATE(GetHash("memento_value_in_file")); |
| 118 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestMementoValue)); |
| 119 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit4"), VALUE_TRUE); |
| 120 bytecode += OP_STORE_BOOL(GetHash("had_prompted_already"), VALUE_TRUE); |
| 121 bytecode += OP_END_OF_SENTENCE; |
| 122 bytecode += OP_STORE_HASH(GetHash("memento_value_in_prefs"), |
| 123 kTestMementoValue); |
| 124 bytecode += OP_END_OF_SENTENCE; |
| 125 bytecode += OP_STORE_HASH(GetHash("memento_value_in_local_state"), |
| 126 kTestMementoValue); |
| 127 bytecode += OP_END_OF_SENTENCE; |
| 128 bytecode += OP_STORE_HASH(GetHash("memento_value_in_file"), |
| 129 kTestMementoValue); |
| 130 bytecode += OP_END_OF_SENTENCE; |
| 131 return bytecode; |
| 132 } |
| 133 |
| 134 // Test fixtures ------------------------------------------------------------- |
| 135 |
| 136 class AutomaticProfileResetterTestBase : public testing::Test { |
| 137 protected: |
| 138 explicit AutomaticProfileResetterTestBase( |
| 139 const std::string& experiment_group_name) |
| 140 : local_state_(TestingBrowserProcess::GetGlobal()), |
| 141 experiment_group_name_(experiment_group_name), |
| 142 mock_delegate_(NULL) { |
| 143 // Make sure the factory is not optimized away, so prefs get registered. |
| 144 AutomaticProfileResetterFactory::GetInstance(); |
| 145 } |
| 146 |
| 147 virtual void SetUp() OVERRIDE { |
| 148 profile_.reset(new TestingProfile()); |
| 149 field_trials_.reset(new base::FieldTrialList(NULL)); |
| 150 base::FieldTrialList::CreateFieldTrial(kAutomaticProfileResetStudyName, |
| 151 experiment_group_name_); |
| 152 mock_delegate_ = new testing::StrictMock<MockProfileResetterDelegate>(); |
| 153 resetter_.reset(new AutomaticProfileResetter(profile_.get())); |
| 154 } |
| 155 |
| 156 void SetTestingProgram(const std::string& source_code) { |
| 157 testing_program_ = source_code; |
| 158 } |
| 159 |
| 160 void UnleashResetterAndWait() { |
| 161 resetter_->Initialize(); |
| 162 resetter_->SetDelegateForTesting(mock_delegate_); // Takes ownership. |
| 163 resetter_->SetHashSeedForTesting(kTestHashSeed); |
| 164 resetter_->SetProgramForTesting(testing_program_); |
| 165 base::RunLoop().RunUntilIdle(); |
| 166 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 167 base::RunLoop().RunUntilIdle(); |
| 168 } |
| 169 |
| 170 TestingProfile* profile() { return profile_.get(); } |
| 171 |
| 172 MockProfileResetterDelegate& mock_delegate() { return *mock_delegate_; } |
| 173 AutomaticProfileResetter* resetter() { return resetter_.get(); } |
| 174 |
| 175 private: |
| 176 content::TestBrowserThreadBundle thread_bundle_; |
| 177 ScopedTestingLocalState local_state_; |
| 178 scoped_ptr<TestingProfile> profile_; |
| 179 scoped_ptr<base::FieldTrialList> field_trials_; |
| 180 std::string experiment_group_name_; |
| 181 std::string testing_program_; |
| 182 |
| 183 scoped_ptr<AutomaticProfileResetter> resetter_; |
| 184 MockProfileResetterDelegate* mock_delegate_; |
| 185 |
| 186 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterTestBase); |
| 187 }; |
| 188 |
| 189 class AutomaticProfileResetterTest : public AutomaticProfileResetterTestBase { |
| 190 protected: |
| 191 AutomaticProfileResetterTest() |
| 192 : AutomaticProfileResetterTestBase(kStudyEnabledGroupName) {} |
| 193 }; |
| 194 |
| 195 class AutomaticProfileResetterTestDryRun |
| 196 : public AutomaticProfileResetterTestBase { |
| 197 protected: |
| 198 AutomaticProfileResetterTestDryRun() |
| 199 : AutomaticProfileResetterTestBase(kStudyDryRunGroupName) {} |
| 200 }; |
| 201 |
| 202 class AutomaticProfileResetterTestDisabled |
| 203 : public AutomaticProfileResetterTestBase { |
| 204 protected: |
| 205 AutomaticProfileResetterTestDisabled() |
| 206 : AutomaticProfileResetterTestBase(kStudyDisabledGroupName) {} |
| 207 }; |
| 208 |
| 209 // Tests --------------------------------------------------------------------- |
| 210 |
| 211 TEST_F(AutomaticProfileResetterTestDisabled, NothingIsDoneWhenDisabled) { |
| 212 // Nothing is expected to be done. |
| 213 UnleashResetterAndWait(); |
| 214 } |
| 215 |
| 216 TEST_F(AutomaticProfileResetterTestDryRun, ConditionsNotSatisfied) { |
| 217 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 218 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 219 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 220 |
| 221 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 222 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 223 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 224 |
| 225 SetTestingProgram(ConstructProgram(false, false)); |
| 226 |
| 227 EXPECT_CALL(mock_delegate(), ReportStatistics(0x00u, 0x00u)); |
| 228 |
| 229 UnleashResetterAndWait(); |
| 230 |
| 231 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 232 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 233 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 234 } |
| 235 |
| 236 TEST_F(AutomaticProfileResetterTestDryRun, OneConditionSatisfied) { |
| 237 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 238 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 239 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 240 |
| 241 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 242 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 243 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 244 |
| 245 SetTestingProgram(ConstructProgram(true, false)); |
| 246 |
| 247 EXPECT_CALL(mock_delegate(), ReportStatistics(0x01u, 0x00u)); |
| 248 |
| 249 UnleashResetterAndWait(); |
| 250 |
| 251 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); |
| 252 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); |
| 253 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); |
| 254 } |
| 255 |
| 256 TEST_F(AutomaticProfileResetterTestDryRun, OtherConditionSatisfied) { |
| 257 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 258 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 259 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 260 |
| 261 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 262 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 263 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 264 |
| 265 SetTestingProgram(ConstructProgram(false, true)); |
| 266 |
| 267 EXPECT_CALL(mock_delegate(), ReportStatistics(0x02u, 0x00u)); |
| 268 |
| 269 UnleashResetterAndWait(); |
| 270 |
| 271 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); |
| 272 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); |
| 273 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); |
| 274 } |
| 275 |
| 276 TEST_F(AutomaticProfileResetterTest, ConditionsNotSatisfied) { |
| 277 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 278 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 279 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 280 |
| 281 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 282 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 283 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 284 |
| 285 SetTestingProgram(ConstructProgram(false, false)); |
| 286 |
| 287 EXPECT_CALL(mock_delegate(), ReportStatistics(0x00u, 0x00u)); |
| 288 |
| 289 UnleashResetterAndWait(); |
| 290 |
| 291 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 292 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 293 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 294 } |
| 295 |
| 296 TEST_F(AutomaticProfileResetterTest, BothConditionsSatisfied) { |
| 297 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 298 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 299 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 300 |
| 301 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 302 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 303 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 304 |
| 305 SetTestingProgram(ConstructProgram(true, true)); |
| 306 |
| 307 EXPECT_CALL(mock_delegate(), ShowPrompt()); |
| 308 EXPECT_CALL(mock_delegate(), ReportStatistics(0x03u, 0x00u)); |
| 309 |
| 310 UnleashResetterAndWait(); |
| 311 |
| 312 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); |
| 313 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); |
| 314 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); |
| 315 } |
| 316 |
| 317 TEST_F(AutomaticProfileResetterTest, PrefHostedMementoPreventsPrompt) { |
| 318 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 319 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 320 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 321 |
| 322 memento_in_prefs.StoreValue(kTestMementoValue); |
| 323 |
| 324 SetTestingProgram(ConstructProgram(true, true)); |
| 325 |
| 326 EXPECT_CALL(mock_delegate(), ReportStatistics(0x03u, 0x02u)); |
| 327 |
| 328 UnleashResetterAndWait(); |
| 329 |
| 330 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue()); |
| 331 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 332 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 333 } |
| 334 |
| 335 TEST_F(AutomaticProfileResetterTest, LocalStateHostedMementoPreventsPrompt) { |
| 336 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 337 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 338 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 339 |
| 340 memento_in_local_state.StoreValue(kTestMementoValue); |
| 341 |
| 342 SetTestingProgram(ConstructProgram(true, true)); |
| 343 |
| 344 EXPECT_CALL(mock_delegate(), ReportStatistics(0x03u, 0x04u)); |
| 345 |
| 346 UnleashResetterAndWait(); |
| 347 |
| 348 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 349 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue()); |
| 350 EXPECT_EQ("", memento_in_file.ReadValue()); |
| 351 } |
| 352 |
| 353 TEST_F(AutomaticProfileResetterTest, FileHostedMementoPreventsPrompt) { |
| 354 PreferenceHostedPromptMemento memento_in_prefs(profile()); |
| 355 LocalStateHostedPromptMemento memento_in_local_state(profile()); |
| 356 FileHostedPromptMementoSynchronous memento_in_file(profile()); |
| 357 |
| 358 memento_in_file.StoreValue(kTestMementoValue); |
| 359 |
| 360 SetTestingProgram(ConstructProgram(true, true)); |
| 361 |
| 362 EXPECT_CALL(mock_delegate(), ReportStatistics(0x03u, 0x08u)); |
| 363 |
| 364 UnleashResetterAndWait(); |
| 365 |
| 366 EXPECT_EQ("", memento_in_prefs.ReadValue()); |
| 367 EXPECT_EQ("", memento_in_local_state.ReadValue()); |
| 368 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue()); |
| 369 } |
| 370 |
| 371 } // namespace |
| OLD | NEW |