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.h"
10 #include "base/bind_helpers.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/prefs/pref_registry_simple.h"
13 #include "base/prefs/testing_pref_service.h"
14 #include "base/run_loop.h"
15 #include "base/test/test_simple_task_runner.h"
16 #include "base/threading/sequenced_worker_pool.h"
17 #include "base/values.h"
18 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h"
19 #include "chrome/browser/profile_resetter/automatic_profile_resetter_factory.h"
20 #include "chrome/browser/profile_resetter/automatic_profile_resetter_mementos.h"
21 #include "chrome/browser/profile_resetter/jtl_foundation.h"
22 #include "chrome/browser/profile_resetter/jtl_instructions.h"
23 #include "chrome/test/base/scoped_testing_local_state.h"
24 #include "chrome/test/base/testing_browser_process.h"
25 #include "chrome/test/base/testing_pref_service_syncable.h"
26 #include "chrome/test/base/testing_profile.h"
27 #include "components/user_prefs/pref_registry_syncable.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/test/test_browser_thread_bundle.h"
30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h"
34 using testing::Invoke;
38 const char kAutomaticProfileResetStudyName[] = "AutomaticProfileReset";
39 const char kStudyDisabledGroupName[] = "Disabled";
40 const char kStudyDryRunGroupName[] = "DryRun";
41 const char kStudyEnabledGroupName[] = "Enabled";
43 const char kTestHashSeed[] = "testing-hash-seed";
44 const char kTestMementoValue[] = "01234567890123456789012345678901";
45 const char kTestInvalidMementoValue[] = "12345678901234567890123456789012";
47 const char kTestPreferencePath[] = "testing.preference";
48 const char kTestPreferenceValue[] = "testing-preference-value";
50 const char kSearchURLAttributeKey[] = "search_url";
51 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}";
52 const char kTestSearchURL2[] = "http://google.com/?q={searchTerms}";
54 const char kTestModuleDigest[] = "01234567890123456789012345678901";
55 const char kTestModuleDigest2[] = "12345678901234567890123456789012";
57 // Helpers ------------------------------------------------------------------
59 // A testing version of the AutomaticProfileResetter that differs from the real
60 // one only in that it has its statistics reporting mocked out for verification.
61 class AutomaticProfileResetterUnderTest : public AutomaticProfileResetter {
63 explicit AutomaticProfileResetterUnderTest(Profile* profile)
64 : AutomaticProfileResetter(profile) {}
65 virtual ~AutomaticProfileResetterUnderTest() {}
67 MOCK_METHOD2(ReportStatistics, void(uint32, uint32));
70 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterUnderTest);
73 class MockProfileResetterDelegate : public AutomaticProfileResetterDelegate {
75 MockProfileResetterDelegate()
76 : emulated_default_search_provider_is_managed_(false) {}
77 virtual ~MockProfileResetterDelegate() {}
79 MOCK_METHOD0(EnumerateLoadedModulesIfNeeded, void());
80 MOCK_CONST_METHOD1(RequestCallbackWhenLoadedModulesAreEnumerated,
81 void(const base::Closure&));
83 MOCK_METHOD0(LoadTemplateURLServiceIfNeeded, void());
84 MOCK_CONST_METHOD1(RequestCallbackWhenTemplateURLServiceIsLoaded,
85 void(const base::Closure&));
87 MOCK_CONST_METHOD0(OnGetLoadedModuleNameDigestsCalled, void());
88 MOCK_CONST_METHOD0(OnGetDefaultSearchProviderDetailsCalled, void());
89 MOCK_CONST_METHOD0(OnIsDefaultSearchProviderManagedCalled, void());
90 MOCK_CONST_METHOD0(OnGetPrepopulatedSearchProvidersDetailsCalled, void());
92 MOCK_METHOD0(ShowPrompt, void());
94 virtual scoped_ptr<base::ListValue>
95 GetLoadedModuleNameDigests() const OVERRIDE {
96 OnGetLoadedModuleNameDigestsCalled();
97 return scoped_ptr<base::ListValue>(
98 emulated_loaded_module_digests_.DeepCopy());
101 virtual scoped_ptr<base::DictionaryValue>
102 GetDefaultSearchProviderDetails() const OVERRIDE {
103 OnGetDefaultSearchProviderDetailsCalled();
104 return scoped_ptr<base::DictionaryValue>(
105 emulated_default_search_provider_details_.DeepCopy());
108 virtual bool IsDefaultSearchProviderManaged() const OVERRIDE {
109 OnIsDefaultSearchProviderManagedCalled();
110 return emulated_default_search_provider_is_managed_;
113 virtual scoped_ptr<base::ListValue>
114 GetPrepopulatedSearchProvidersDetails() const OVERRIDE {
115 OnGetPrepopulatedSearchProvidersDetailsCalled();
116 return scoped_ptr<base::ListValue>(
117 emulated_search_providers_details_.DeepCopy());
120 static void ClosureInvoker(const base::Closure& closure) { closure.Run(); }
122 void ExpectCallsToDependenciesSetUpMethods() {
123 EXPECT_CALL(*this, EnumerateLoadedModulesIfNeeded());
124 EXPECT_CALL(*this, LoadTemplateURLServiceIfNeeded());
125 EXPECT_CALL(*this, RequestCallbackWhenLoadedModulesAreEnumerated(_))
126 .WillOnce(Invoke(ClosureInvoker));
127 EXPECT_CALL(*this, RequestCallbackWhenTemplateURLServiceIsLoaded(_))
128 .WillOnce(Invoke(ClosureInvoker));
131 void ExpectCallsToGetterMethods() {
132 EXPECT_CALL(*this, OnGetLoadedModuleNameDigestsCalled());
133 EXPECT_CALL(*this, OnGetDefaultSearchProviderDetailsCalled());
134 EXPECT_CALL(*this, OnIsDefaultSearchProviderManagedCalled());
135 EXPECT_CALL(*this, OnGetPrepopulatedSearchProvidersDetailsCalled());
138 base::DictionaryValue& emulated_default_search_provider_details() {
139 return emulated_default_search_provider_details_;
142 base::ListValue& emulated_search_providers_details() {
143 return emulated_search_providers_details_;
146 base::ListValue& emulated_loaded_module_digests() {
147 return emulated_loaded_module_digests_;
150 void set_emulated_default_search_provider_is_managed(bool value) {
151 emulated_default_search_provider_is_managed_ = value;
155 base::DictionaryValue emulated_default_search_provider_details_;
156 base::ListValue emulated_search_providers_details_;
157 base::ListValue emulated_loaded_module_digests_;
158 bool emulated_default_search_provider_is_managed_;
160 DISALLOW_COPY_AND_ASSIGN(MockProfileResetterDelegate);
163 class FileHostedPromptMementoSynchronous : protected FileHostedPromptMemento {
165 explicit FileHostedPromptMementoSynchronous(Profile* profile)
166 : FileHostedPromptMemento(profile) {}
168 std::string ReadValue() const {
170 FileHostedPromptMemento::ReadValue(base::Bind(&AssignArgumentTo, &result));
171 base::RunLoop().RunUntilIdle();
175 void StoreValue(const std::string& value) {
176 FileHostedPromptMemento::StoreValue(value);
177 base::RunLoop().RunUntilIdle();
181 static void AssignArgumentTo(std::string* target, const std::string& value) {
185 DISALLOW_COPY_AND_ASSIGN(FileHostedPromptMementoSynchronous);
188 std::string GetHash(const std::string& input) {
189 return jtl_foundation::Hasher(kTestHashSeed).GetHash(input);
192 // Encodes a Boolean argument value into JTL bytecode.
193 std::string EncodeBool(bool value) { return value ? VALUE_TRUE : VALUE_FALSE; }
195 // Constructs a simple evaluation program to test that basic input/output works
196 // well. It will emulate a scenario in which the reset criteria are satisfied as
197 // prescribed by |emulate_satisfied_criterion_{1|2}|, and will set bits in the
198 // combined status mask according to whether or not the memento values received
199 // in the input were as expected.
201 // More specifically, the output of the program will be as follows:
203 // "satisfied_criteria_mask_bit1": emulate_satisfied_criterion_1,
204 // "satisfied_criteria_mask_bit2": emulate_satisfied_criterion_2,
205 // "combined_status_mask_bit1":
206 // (emulate_satisfied_criterion_1 || emulate_satisfied_criterion_2),
207 // "combined_status_mask_bit2":
208 // (input["memento_value_in_prefs"] == kTestMementoValue),
209 // "combined_status_mask_bit3":
210 // (input["memento_value_in_local_state"] == kTestMementoValue),
211 // "combined_status_mask_bit4":
212 // (input["memento_value_in_file"] == kTestMementoValue),
213 // "had_prompted_already": <OR-combination of above three>,
214 // "memento_value_in_prefs": kTestMementoValue,
215 // "memento_value_in_local_state": kTestMementoValue,
216 // "memento_value_in_file": kTestMementoValue
218 std::string ConstructProgram(bool emulate_satisfied_criterion_1,
219 bool emulate_satisfied_criterion_2) {
220 std::string bytecode;
221 bytecode += OP_STORE_BOOL(GetHash("satisfied_criteria_mask_bit1"),
222 EncodeBool(emulate_satisfied_criterion_1));
223 bytecode += OP_END_OF_SENTENCE;
224 bytecode += OP_STORE_BOOL(GetHash("satisfied_criteria_mask_bit2"),
225 EncodeBool(emulate_satisfied_criterion_2));
226 bytecode += OP_END_OF_SENTENCE;
227 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit1"),
228 EncodeBool(emulate_satisfied_criterion_1 ||
229 emulate_satisfied_criterion_2));
230 bytecode += OP_END_OF_SENTENCE;
231 bytecode += OP_NAVIGATE(GetHash("memento_value_in_prefs"));
232 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestMementoValue));
233 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit2"), VALUE_TRUE);
234 bytecode += OP_STORE_BOOL(GetHash("had_prompted_already"), VALUE_TRUE);
235 bytecode += OP_END_OF_SENTENCE;
236 bytecode += OP_NAVIGATE(GetHash("memento_value_in_local_state"));
237 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestMementoValue));
238 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit3"), VALUE_TRUE);
239 bytecode += OP_STORE_BOOL(GetHash("had_prompted_already"), VALUE_TRUE);
240 bytecode += OP_END_OF_SENTENCE;
241 bytecode += OP_NAVIGATE(GetHash("memento_value_in_file"));
242 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestMementoValue));
243 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit4"), VALUE_TRUE);
244 bytecode += OP_STORE_BOOL(GetHash("had_prompted_already"), VALUE_TRUE);
245 bytecode += OP_END_OF_SENTENCE;
246 bytecode += OP_STORE_HASH(GetHash("memento_value_in_prefs"),
248 bytecode += OP_END_OF_SENTENCE;
249 bytecode += OP_STORE_HASH(GetHash("memento_value_in_local_state"),
251 bytecode += OP_END_OF_SENTENCE;
252 bytecode += OP_STORE_HASH(GetHash("memento_value_in_file"),
254 bytecode += OP_END_OF_SENTENCE;
258 // Constructs another evaluation program to specifically test that local state
259 // and user preference values are included in the input as expected. We will
260 // re-purpose the output bitmasks to channel out information about the outcome
263 // More specifically, the output of the program will be as follows:
265 // "combined_status_mask_bit1":
266 // (input["preferences.testing.preference"] == kTestPreferenceValue)
267 // "combined_status_mask_bit2":
268 // (input["local_state.testing.preference"] == kTestPreferenceValue)
269 // "combined_status_mask_bit3": input["preferences_iuc.testing.preference"]
270 // "combined_status_mask_bit4": input["local_state_iuc.testing.preference"]
272 std::string ConstructProgramToCheckPreferences() {
273 std::string bytecode;
274 bytecode += OP_NAVIGATE(GetHash("preferences"));
275 bytecode += OP_NAVIGATE(GetHash("testing"));
276 bytecode += OP_NAVIGATE(GetHash("preference"));
277 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestPreferenceValue));
278 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit1"),
280 bytecode += OP_END_OF_SENTENCE;
281 bytecode += OP_NAVIGATE(GetHash("local_state"));
282 bytecode += OP_NAVIGATE(GetHash("testing"));
283 bytecode += OP_NAVIGATE(GetHash("preference"));
284 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestPreferenceValue));
285 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit2"),
287 bytecode += OP_END_OF_SENTENCE;
288 bytecode += OP_NAVIGATE(GetHash("preferences_iuc"));
289 bytecode += OP_NAVIGATE(GetHash("testing"));
290 bytecode += OP_NAVIGATE(GetHash("preference"));
291 bytecode += OP_COMPARE_NODE_BOOL(EncodeBool(true));
292 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit3"),
294 bytecode += OP_END_OF_SENTENCE;
295 bytecode += OP_NAVIGATE(GetHash("local_state_iuc"));
296 bytecode += OP_NAVIGATE(GetHash("testing"));
297 bytecode += OP_NAVIGATE(GetHash("preference"));
298 bytecode += OP_COMPARE_NODE_BOOL(EncodeBool(true));
299 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit4"),
301 bytecode += OP_END_OF_SENTENCE;
305 // Legend for the bitmask returned by the above program.
306 enum CombinedStatusMaskLegendForCheckingPreferences {
307 HAS_EXPECTED_USER_PREFERENCE = 1 << 0,
308 HAS_EXPECTED_LOCAL_STATE_PREFERENCE = 1 << 1,
309 USER_PREFERENCE_IS_USER_CONTROLLED = 1 << 2,
310 LOCAL_STATE_IS_USER_CONTROLLED = 1 << 3,
313 // Constructs yet another evaluation program to specifically test that default
314 // and pre-populated search engines are included in the input as expected. We
315 // will re-purpose the output bitmasks to channel out information about the
316 // outcome of the checks.
318 // More specifically, the output of the program will be as follows:
320 // "combined_status_mask_bit1":
321 // (input["default_search_provider.search_url"] == kTestSearchURL)
322 // "combined_status_mask_bit2": input["default_search_provider_iuc"]
323 // "combined_status_mask_bit3":
324 // (input["search_providers.*.search_url"] == kTestSearchURL)
325 // "combined_status_mask_bit4":
326 // (input["search_providers.*.search_url"] == kTestSearchURL2)
328 std::string ConstructProgramToCheckSearchEngines() {
329 std::string bytecode;
330 bytecode += OP_NAVIGATE(GetHash("default_search_provider"));
331 bytecode += OP_NAVIGATE(GetHash("search_url"));
332 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestSearchURL));
333 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit1"),
335 bytecode += OP_END_OF_SENTENCE;
336 bytecode += OP_NAVIGATE(GetHash("default_search_provider_iuc"));
337 bytecode += OP_COMPARE_NODE_BOOL(EncodeBool(true));
338 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit2"),
340 bytecode += OP_END_OF_SENTENCE;
341 bytecode += OP_NAVIGATE(GetHash("search_providers"));
342 bytecode += OP_NAVIGATE_ANY;
343 bytecode += OP_NAVIGATE(GetHash("search_url"));
344 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestSearchURL));
345 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit3"),
347 bytecode += OP_END_OF_SENTENCE;
348 bytecode += OP_NAVIGATE(GetHash("search_providers"));
349 bytecode += OP_NAVIGATE_ANY;
350 bytecode += OP_NAVIGATE(GetHash("search_url"));
351 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestSearchURL2));
352 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit4"),
354 bytecode += OP_END_OF_SENTENCE;
358 // Legend for the bitmask returned by the above program.
359 enum CombinedStatusMaskLegendForCheckingSearchEngines {
360 HAS_EXPECTED_DEFAULT_SEARCH_PROVIDER = 1 << 0,
361 DEFAULT_SEARCH_PROVIDER_IS_USER_CONTROLLED = 1 << 1,
362 HAS_EXPECTED_PREPOPULATED_SEARCH_PROVIDER_1 = 1 << 2,
363 HAS_EXPECTED_PREPOPULATED_SEARCH_PROVIDER_2 = 1 << 3,
366 // Constructs yet another evaluation program to specifically test that loaded
367 // module digests are included in the input as expected. We will re-purpose the
368 // output bitmasks to channel out information about the outcome of the checks.
370 // More specifically, the output of the program will be as follows:
372 // "combined_status_mask_bit1":
373 // (input["loaded_modules.*"] == kTestModuleDigest)
374 // "combined_status_mask_bit2":
375 // (input["loaded_modules.*"] == kTestModuleDigest2)
377 std::string ConstructProgramToCheckLoadedModuleDigests() {
378 std::string bytecode;
379 bytecode += OP_NAVIGATE(GetHash("loaded_modules"));
380 bytecode += OP_NAVIGATE_ANY;
381 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestModuleDigest));
382 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit1"),
384 bytecode += OP_END_OF_SENTENCE;
385 bytecode += OP_NAVIGATE(GetHash("loaded_modules"));
386 bytecode += OP_NAVIGATE_ANY;
387 bytecode += OP_COMPARE_NODE_HASH(GetHash(kTestModuleDigest2));
388 bytecode += OP_STORE_BOOL(GetHash("combined_status_mask_bit2"),
390 bytecode += OP_END_OF_SENTENCE;
394 // Legend for the bitmask returned by the above program.
395 enum CombinedStatusMaskLegendForCheckingLoadedModules {
396 HAS_EXPECTED_MODULE_DIGEST_1 = 1 << 0,
397 HAS_EXPECTED_MODULE_DIGEST_2 = 1 << 1,
400 // Test fixtures -------------------------------------------------------------
402 class AutomaticProfileResetterTestBase : public testing::Test {
404 explicit AutomaticProfileResetterTestBase(
405 const std::string& experiment_group_name)
406 : waiting_task_runner_(new base::TestSimpleTaskRunner),
407 local_state_(TestingBrowserProcess::GetGlobal()),
408 profile_(new TestingProfile()),
409 experiment_group_name_(experiment_group_name),
410 mock_delegate_(NULL) {
411 // Make sure the factory is not optimized away, so whatever preferences it
412 // wants to register will actually get registered.
413 AutomaticProfileResetterFactory::GetInstance();
415 // Register some additional local state preferences for testing purposes.
416 PrefRegistrySimple* local_state_registry = local_state_.Get()->registry();
417 DCHECK(local_state_registry);
418 local_state_registry->RegisterStringPref(kTestPreferencePath, "");
420 // Register some additional user preferences for testing purposes.
421 user_prefs::PrefRegistrySyncable* user_prefs_registry =
422 profile_->GetTestingPrefService()->registry();
423 DCHECK(user_prefs_registry);
424 user_prefs_registry->RegisterStringPref(
427 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
430 virtual void SetUp() OVERRIDE {
431 field_trials_.reset(new base::FieldTrialList(NULL));
432 base::FieldTrialList::CreateFieldTrial(kAutomaticProfileResetStudyName,
433 experiment_group_name_);
435 resetter_.reset(new testing::StrictMock<AutomaticProfileResetterUnderTest>(
438 scoped_ptr<MockProfileResetterDelegate> mock_delegate(
439 new testing::StrictMock<MockProfileResetterDelegate>());
440 mock_delegate_ = mock_delegate.get();
441 resetter_->SetDelegateForTesting(
442 mock_delegate.PassAs<AutomaticProfileResetterDelegate>());
443 resetter_->SetTaskRunnerForWaitingForTesting(waiting_task_runner_);
446 void SetTestingHashSeed(const std::string& hash_seed) {
447 testing_hash_seed_ = hash_seed;
450 void SetTestingProgram(const std::string& source_code) {
451 testing_program_ = source_code;
454 void UnleashResetterAndWait() {
455 resetter_->SetHashSeedForTesting(testing_hash_seed_);
456 resetter_->SetProgramForTesting(testing_program_);
458 resetter_->Activate();
460 if (waiting_task_runner_->HasPendingTask()) {
461 EXPECT_EQ(base::TimeDelta::FromSeconds(55),
462 waiting_task_runner_->NextPendingTaskDelay());
463 waiting_task_runner_->RunPendingTasks();
465 base::RunLoop().RunUntilIdle();
466 content::BrowserThread::GetBlockingPool()->FlushForTesting();
467 base::RunLoop().RunUntilIdle();
470 TestingProfile* profile() { return profile_.get(); }
471 TestingPrefServiceSimple* local_state() { return local_state_.Get(); }
473 MockProfileResetterDelegate& mock_delegate() { return *mock_delegate_; }
474 AutomaticProfileResetterUnderTest& resetter() { return *resetter_; }
477 content::TestBrowserThreadBundle thread_bundle_;
478 scoped_refptr<base::TestSimpleTaskRunner> waiting_task_runner_;
479 ScopedTestingLocalState local_state_;
480 scoped_ptr<TestingProfile> profile_;
481 scoped_ptr<base::FieldTrialList> field_trials_;
482 std::string experiment_group_name_;
483 std::string testing_program_;
484 std::string testing_hash_seed_;
486 scoped_ptr<AutomaticProfileResetterUnderTest> resetter_;
487 MockProfileResetterDelegate* mock_delegate_;
489 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterTestBase);
492 class AutomaticProfileResetterTest : public AutomaticProfileResetterTestBase {
494 AutomaticProfileResetterTest()
495 : AutomaticProfileResetterTestBase(kStudyEnabledGroupName) {}
498 class AutomaticProfileResetterTestDryRun
499 : public AutomaticProfileResetterTestBase {
501 AutomaticProfileResetterTestDryRun()
502 : AutomaticProfileResetterTestBase(kStudyDryRunGroupName) {}
505 class AutomaticProfileResetterTestDisabled
506 : public AutomaticProfileResetterTestBase {
508 AutomaticProfileResetterTestDisabled()
509 : AutomaticProfileResetterTestBase(kStudyDisabledGroupName) {}
512 // Tests ---------------------------------------------------------------------
514 TEST_F(AutomaticProfileResetterTestDisabled, NothingIsDoneWhenDisabled) {
515 PreferenceHostedPromptMemento memento_in_prefs(profile());
516 LocalStateHostedPromptMemento memento_in_local_state(profile());
517 FileHostedPromptMementoSynchronous memento_in_file(profile());
519 EXPECT_EQ("", memento_in_prefs.ReadValue());
520 EXPECT_EQ("", memento_in_local_state.ReadValue());
521 EXPECT_EQ("", memento_in_file.ReadValue());
523 SetTestingProgram(ConstructProgram(true, true));
524 SetTestingHashSeed(kTestHashSeed);
526 // No calls are expected to the delegate.
528 UnleashResetterAndWait();
530 EXPECT_EQ("", memento_in_prefs.ReadValue());
531 EXPECT_EQ("", memento_in_local_state.ReadValue());
532 EXPECT_EQ("", memento_in_file.ReadValue());
535 TEST_F(AutomaticProfileResetterTestDryRun, ConditionsNotSatisfied) {
536 PreferenceHostedPromptMemento memento_in_prefs(profile());
537 LocalStateHostedPromptMemento memento_in_local_state(profile());
538 FileHostedPromptMementoSynchronous memento_in_file(profile());
540 EXPECT_EQ("", memento_in_prefs.ReadValue());
541 EXPECT_EQ("", memento_in_local_state.ReadValue());
542 EXPECT_EQ("", memento_in_file.ReadValue());
544 SetTestingProgram(ConstructProgram(false, false));
545 SetTestingHashSeed(kTestHashSeed);
547 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
548 mock_delegate().ExpectCallsToGetterMethods();
549 EXPECT_CALL(resetter(), ReportStatistics(0x00u, 0x00u));
551 UnleashResetterAndWait();
553 EXPECT_EQ("", memento_in_prefs.ReadValue());
554 EXPECT_EQ("", memento_in_local_state.ReadValue());
555 EXPECT_EQ("", memento_in_file.ReadValue());
558 TEST_F(AutomaticProfileResetterTestDryRun, OneConditionSatisfied) {
559 PreferenceHostedPromptMemento memento_in_prefs(profile());
560 LocalStateHostedPromptMemento memento_in_local_state(profile());
561 FileHostedPromptMementoSynchronous memento_in_file(profile());
563 EXPECT_EQ("", memento_in_prefs.ReadValue());
564 EXPECT_EQ("", memento_in_local_state.ReadValue());
565 EXPECT_EQ("", memento_in_file.ReadValue());
567 SetTestingProgram(ConstructProgram(true, false));
568 SetTestingHashSeed(kTestHashSeed);
570 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
571 mock_delegate().ExpectCallsToGetterMethods();
572 EXPECT_CALL(resetter(), ReportStatistics(0x01u, 0x01u));
574 UnleashResetterAndWait();
576 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue());
577 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue());
578 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue());
581 TEST_F(AutomaticProfileResetterTestDryRun, OtherConditionSatisfied) {
582 PreferenceHostedPromptMemento memento_in_prefs(profile());
583 LocalStateHostedPromptMemento memento_in_local_state(profile());
584 FileHostedPromptMementoSynchronous memento_in_file(profile());
586 EXPECT_EQ("", memento_in_prefs.ReadValue());
587 EXPECT_EQ("", memento_in_local_state.ReadValue());
588 EXPECT_EQ("", memento_in_file.ReadValue());
590 SetTestingProgram(ConstructProgram(false, true));
591 SetTestingHashSeed(kTestHashSeed);
593 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
594 mock_delegate().ExpectCallsToGetterMethods();
595 EXPECT_CALL(resetter(), ReportStatistics(0x02u, 0x01u));
597 UnleashResetterAndWait();
599 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue());
600 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue());
601 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue());
604 TEST_F(AutomaticProfileResetterTestDryRun,
605 ConditionsSatisfiedAndInvalidMementos) {
606 PreferenceHostedPromptMemento memento_in_prefs(profile());
607 LocalStateHostedPromptMemento memento_in_local_state(profile());
608 FileHostedPromptMementoSynchronous memento_in_file(profile());
610 memento_in_prefs.StoreValue(kTestInvalidMementoValue);
611 memento_in_local_state.StoreValue(kTestInvalidMementoValue);
612 memento_in_file.StoreValue(kTestInvalidMementoValue);
614 SetTestingProgram(ConstructProgram(true, true));
615 SetTestingHashSeed(kTestHashSeed);
617 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
618 mock_delegate().ExpectCallsToGetterMethods();
619 EXPECT_CALL(resetter(), ReportStatistics(0x03u, 0x01u));
621 UnleashResetterAndWait();
623 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue());
624 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue());
625 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue());
628 TEST_F(AutomaticProfileResetterTestDryRun, AlreadyHadPrefHostedMemento) {
629 PreferenceHostedPromptMemento memento_in_prefs(profile());
630 LocalStateHostedPromptMemento memento_in_local_state(profile());
631 FileHostedPromptMementoSynchronous memento_in_file(profile());
633 memento_in_prefs.StoreValue(kTestMementoValue);
635 SetTestingProgram(ConstructProgram(true, true));
636 SetTestingHashSeed(kTestHashSeed);
638 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
639 mock_delegate().ExpectCallsToGetterMethods();
640 EXPECT_CALL(resetter(), ReportStatistics(0x03u, 0x03u));
642 UnleashResetterAndWait();
644 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue());
645 EXPECT_EQ("", memento_in_local_state.ReadValue());
646 EXPECT_EQ("", memento_in_file.ReadValue());
649 TEST_F(AutomaticProfileResetterTestDryRun, AlreadyHadLocalStateHostedMemento) {
650 PreferenceHostedPromptMemento memento_in_prefs(profile());
651 LocalStateHostedPromptMemento memento_in_local_state(profile());
652 FileHostedPromptMementoSynchronous memento_in_file(profile());
654 memento_in_local_state.StoreValue(kTestMementoValue);
656 SetTestingProgram(ConstructProgram(true, true));
657 SetTestingHashSeed(kTestHashSeed);
659 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
660 mock_delegate().ExpectCallsToGetterMethods();
661 EXPECT_CALL(resetter(), ReportStatistics(0x03u, 0x05u));
663 UnleashResetterAndWait();
665 EXPECT_EQ("", memento_in_prefs.ReadValue());
666 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue());
667 EXPECT_EQ("", memento_in_file.ReadValue());
670 TEST_F(AutomaticProfileResetterTestDryRun, AlreadyHadFileHostedMemento) {
671 PreferenceHostedPromptMemento memento_in_prefs(profile());
672 LocalStateHostedPromptMemento memento_in_local_state(profile());
673 FileHostedPromptMementoSynchronous memento_in_file(profile());
675 memento_in_file.StoreValue(kTestMementoValue);
677 SetTestingProgram(ConstructProgram(true, true));
678 SetTestingHashSeed(kTestHashSeed);
680 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
681 mock_delegate().ExpectCallsToGetterMethods();
682 EXPECT_CALL(resetter(), ReportStatistics(0x03u, 0x09u));
684 UnleashResetterAndWait();
686 EXPECT_EQ("", memento_in_prefs.ReadValue());
687 EXPECT_EQ("", memento_in_local_state.ReadValue());
688 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue());
691 TEST_F(AutomaticProfileResetterTestDryRun, DoNothingWhenResourcesAreMissing) {
692 PreferenceHostedPromptMemento memento_in_prefs(profile());
693 LocalStateHostedPromptMemento memento_in_local_state(profile());
694 FileHostedPromptMementoSynchronous memento_in_file(profile());
696 SetTestingProgram("");
697 SetTestingHashSeed("");
699 // No calls are expected to the delegate.
701 UnleashResetterAndWait();
703 EXPECT_EQ("", memento_in_prefs.ReadValue());
704 EXPECT_EQ("", memento_in_local_state.ReadValue());
705 EXPECT_EQ("", memento_in_file.ReadValue());
708 TEST_F(AutomaticProfileResetterTest, ConditionsNotSatisfied) {
709 PreferenceHostedPromptMemento memento_in_prefs(profile());
710 LocalStateHostedPromptMemento memento_in_local_state(profile());
711 FileHostedPromptMementoSynchronous memento_in_file(profile());
713 EXPECT_EQ("", memento_in_prefs.ReadValue());
714 EXPECT_EQ("", memento_in_local_state.ReadValue());
715 EXPECT_EQ("", memento_in_file.ReadValue());
717 SetTestingProgram(ConstructProgram(false, false));
718 SetTestingHashSeed(kTestHashSeed);
720 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
721 mock_delegate().ExpectCallsToGetterMethods();
722 EXPECT_CALL(resetter(), ReportStatistics(0x00u, 0x00u));
724 UnleashResetterAndWait();
726 EXPECT_EQ("", memento_in_prefs.ReadValue());
727 EXPECT_EQ("", memento_in_local_state.ReadValue());
728 EXPECT_EQ("", memento_in_file.ReadValue());
731 TEST_F(AutomaticProfileResetterTest, OneConditionSatisfied) {
732 PreferenceHostedPromptMemento memento_in_prefs(profile());
733 LocalStateHostedPromptMemento memento_in_local_state(profile());
734 FileHostedPromptMementoSynchronous memento_in_file(profile());
736 EXPECT_EQ("", memento_in_prefs.ReadValue());
737 EXPECT_EQ("", memento_in_local_state.ReadValue());
738 EXPECT_EQ("", memento_in_file.ReadValue());
740 SetTestingProgram(ConstructProgram(true, false));
741 SetTestingHashSeed(kTestHashSeed);
743 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
744 mock_delegate().ExpectCallsToGetterMethods();
745 EXPECT_CALL(mock_delegate(), ShowPrompt());
746 EXPECT_CALL(resetter(), ReportStatistics(0x01u, 0x01u));
748 UnleashResetterAndWait();
750 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue());
751 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue());
752 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue());
755 TEST_F(AutomaticProfileResetterTest, OtherConditionSatisfied) {
756 PreferenceHostedPromptMemento memento_in_prefs(profile());
757 LocalStateHostedPromptMemento memento_in_local_state(profile());
758 FileHostedPromptMementoSynchronous memento_in_file(profile());
760 EXPECT_EQ("", memento_in_prefs.ReadValue());
761 EXPECT_EQ("", memento_in_local_state.ReadValue());
762 EXPECT_EQ("", memento_in_file.ReadValue());
764 SetTestingProgram(ConstructProgram(false, true));
765 SetTestingHashSeed(kTestHashSeed);
767 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
768 mock_delegate().ExpectCallsToGetterMethods();
769 EXPECT_CALL(mock_delegate(), ShowPrompt());
770 EXPECT_CALL(resetter(), ReportStatistics(0x02u, 0x01u));
772 UnleashResetterAndWait();
774 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue());
775 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue());
776 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue());
779 TEST_F(AutomaticProfileResetterTest, ConditionsSatisfiedAndInvalidMementos) {
780 PreferenceHostedPromptMemento memento_in_prefs(profile());
781 LocalStateHostedPromptMemento memento_in_local_state(profile());
782 FileHostedPromptMementoSynchronous memento_in_file(profile());
784 memento_in_prefs.StoreValue(kTestInvalidMementoValue);
785 memento_in_local_state.StoreValue(kTestInvalidMementoValue);
786 memento_in_file.StoreValue(kTestInvalidMementoValue);
788 SetTestingProgram(ConstructProgram(true, true));
789 SetTestingHashSeed(kTestHashSeed);
791 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
792 mock_delegate().ExpectCallsToGetterMethods();
793 EXPECT_CALL(mock_delegate(), ShowPrompt());
794 EXPECT_CALL(resetter(), ReportStatistics(0x03u, 0x01u));
796 UnleashResetterAndWait();
798 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue());
799 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue());
800 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue());
803 TEST_F(AutomaticProfileResetterTest, PrefHostedMementoPreventsPrompt) {
804 PreferenceHostedPromptMemento memento_in_prefs(profile());
805 LocalStateHostedPromptMemento memento_in_local_state(profile());
806 FileHostedPromptMementoSynchronous memento_in_file(profile());
808 memento_in_prefs.StoreValue(kTestMementoValue);
810 SetTestingProgram(ConstructProgram(true, true));
811 SetTestingHashSeed(kTestHashSeed);
813 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
814 mock_delegate().ExpectCallsToGetterMethods();
815 EXPECT_CALL(resetter(), ReportStatistics(0x03u, 0x03u));
817 UnleashResetterAndWait();
819 EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue());
820 EXPECT_EQ("", memento_in_local_state.ReadValue());
821 EXPECT_EQ("", memento_in_file.ReadValue());
824 TEST_F(AutomaticProfileResetterTest, LocalStateHostedMementoPreventsPrompt) {
825 PreferenceHostedPromptMemento memento_in_prefs(profile());
826 LocalStateHostedPromptMemento memento_in_local_state(profile());
827 FileHostedPromptMementoSynchronous memento_in_file(profile());
829 memento_in_local_state.StoreValue(kTestMementoValue);
831 SetTestingProgram(ConstructProgram(true, true));
832 SetTestingHashSeed(kTestHashSeed);
834 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
835 mock_delegate().ExpectCallsToGetterMethods();
836 EXPECT_CALL(resetter(), ReportStatistics(0x03u, 0x05u));
838 UnleashResetterAndWait();
840 EXPECT_EQ("", memento_in_prefs.ReadValue());
841 EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue());
842 EXPECT_EQ("", memento_in_file.ReadValue());
845 TEST_F(AutomaticProfileResetterTest, FileHostedMementoPreventsPrompt) {
846 PreferenceHostedPromptMemento memento_in_prefs(profile());
847 LocalStateHostedPromptMemento memento_in_local_state(profile());
848 FileHostedPromptMementoSynchronous memento_in_file(profile());
850 memento_in_file.StoreValue(kTestMementoValue);
852 SetTestingProgram(ConstructProgram(true, true));
853 SetTestingHashSeed(kTestHashSeed);
855 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
856 mock_delegate().ExpectCallsToGetterMethods();
857 EXPECT_CALL(resetter(), ReportStatistics(0x03u, 0x09u));
859 UnleashResetterAndWait();
861 EXPECT_EQ("", memento_in_prefs.ReadValue());
862 EXPECT_EQ("", memento_in_local_state.ReadValue());
863 EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue());
866 TEST_F(AutomaticProfileResetterTest, DoNothingWhenResourcesAreMissing) {
867 PreferenceHostedPromptMemento memento_in_prefs(profile());
868 LocalStateHostedPromptMemento memento_in_local_state(profile());
869 FileHostedPromptMementoSynchronous memento_in_file(profile());
871 SetTestingProgram("");
872 SetTestingHashSeed("");
874 // No calls are expected to the delegate.
876 UnleashResetterAndWait();
878 EXPECT_EQ("", memento_in_prefs.ReadValue());
879 EXPECT_EQ("", memento_in_local_state.ReadValue());
880 EXPECT_EQ("", memento_in_file.ReadValue());
883 // Please see comments above ConstructProgramToCheckPreferences() to understand
884 // how the following tests work.
886 TEST_F(AutomaticProfileResetterTest, InputUserPreferencesCorrect) {
887 SetTestingProgram(ConstructProgramToCheckPreferences());
888 SetTestingHashSeed(kTestHashSeed);
890 PrefService* prefs = profile()->GetPrefs();
891 prefs->SetString(kTestPreferencePath, kTestPreferenceValue);
893 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
894 mock_delegate().ExpectCallsToGetterMethods();
895 uint32 expected_mask =
896 HAS_EXPECTED_USER_PREFERENCE | USER_PREFERENCE_IS_USER_CONTROLLED;
897 EXPECT_CALL(resetter(), ReportStatistics(0x00u, expected_mask));
899 UnleashResetterAndWait();
902 TEST_F(AutomaticProfileResetterTest, InputLocalStateCorrect) {
903 SetTestingProgram(ConstructProgramToCheckPreferences());
904 SetTestingHashSeed(kTestHashSeed);
906 PrefService* prefs = local_state();
907 prefs->SetString(kTestPreferencePath, kTestPreferenceValue);
909 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
910 mock_delegate().ExpectCallsToGetterMethods();
911 uint32 expected_mask =
912 HAS_EXPECTED_LOCAL_STATE_PREFERENCE | LOCAL_STATE_IS_USER_CONTROLLED;
913 EXPECT_CALL(resetter(), ReportStatistics(0x00u, expected_mask));
915 UnleashResetterAndWait();
918 TEST_F(AutomaticProfileResetterTest, InputManagedUserPreferencesCorrect) {
919 SetTestingProgram(ConstructProgramToCheckPreferences());
920 SetTestingHashSeed(kTestHashSeed);
922 TestingPrefServiceSyncable* prefs = profile()->GetTestingPrefService();
923 prefs->SetManagedPref(kTestPreferencePath,
924 new base::StringValue(kTestPreferenceValue));
926 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
927 mock_delegate().ExpectCallsToGetterMethods();
928 uint32 expected_mask = HAS_EXPECTED_USER_PREFERENCE;
929 EXPECT_CALL(resetter(), ReportStatistics(0x00u, expected_mask));
931 UnleashResetterAndWait();
934 TEST_F(AutomaticProfileResetterTest, InputManagedLocalStateCorrect) {
935 SetTestingProgram(ConstructProgramToCheckPreferences());
936 SetTestingHashSeed(kTestHashSeed);
938 TestingPrefServiceSimple* prefs = local_state();
939 prefs->SetManagedPref(kTestPreferencePath,
940 new base::StringValue(kTestPreferenceValue));
942 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
943 mock_delegate().ExpectCallsToGetterMethods();
944 uint32 expected_mask = HAS_EXPECTED_LOCAL_STATE_PREFERENCE;
945 EXPECT_CALL(resetter(), ReportStatistics(0x00u, expected_mask));
947 UnleashResetterAndWait();
950 // Please see comments above ConstructProgramToCheckSearchEngines() to
951 // understand how the following tests work.
953 TEST_F(AutomaticProfileResetterTest, InputDefaultSearchProviderCorrect) {
954 SetTestingProgram(ConstructProgramToCheckSearchEngines());
955 SetTestingHashSeed(kTestHashSeed);
957 mock_delegate().emulated_default_search_provider_details().SetString(
958 kSearchURLAttributeKey, kTestSearchURL);
960 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
961 mock_delegate().ExpectCallsToGetterMethods();
962 uint32 expected_mask = HAS_EXPECTED_DEFAULT_SEARCH_PROVIDER |
963 DEFAULT_SEARCH_PROVIDER_IS_USER_CONTROLLED;
964 EXPECT_CALL(resetter(), ReportStatistics(0x00u, expected_mask));
966 UnleashResetterAndWait();
969 TEST_F(AutomaticProfileResetterTest, InputSearchProviderManagedCorrect) {
970 SetTestingProgram(ConstructProgramToCheckSearchEngines());
971 SetTestingHashSeed(kTestHashSeed);
973 mock_delegate().emulated_default_search_provider_details().SetString(
974 kSearchURLAttributeKey, kTestSearchURL);
975 mock_delegate().set_emulated_default_search_provider_is_managed(true);
977 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
978 mock_delegate().ExpectCallsToGetterMethods();
979 uint32 expected_mask = HAS_EXPECTED_DEFAULT_SEARCH_PROVIDER;
980 EXPECT_CALL(resetter(), ReportStatistics(0x00u, expected_mask));
982 UnleashResetterAndWait();
985 TEST_F(AutomaticProfileResetterTest, InputSearchProvidersCorrect) {
986 SetTestingProgram(ConstructProgramToCheckSearchEngines());
987 SetTestingHashSeed(kTestHashSeed);
989 base::DictionaryValue* search_provider_1 = new base::DictionaryValue;
990 base::DictionaryValue* search_provider_2 = new base::DictionaryValue;
991 search_provider_1->SetString(kSearchURLAttributeKey, kTestSearchURL);
992 search_provider_2->SetString(kSearchURLAttributeKey, kTestSearchURL2);
993 mock_delegate().emulated_search_providers_details().Append(search_provider_1);
994 mock_delegate().emulated_search_providers_details().Append(search_provider_2);
996 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
997 mock_delegate().ExpectCallsToGetterMethods();
998 uint32 expected_mask = DEFAULT_SEARCH_PROVIDER_IS_USER_CONTROLLED |
999 HAS_EXPECTED_PREPOPULATED_SEARCH_PROVIDER_1 |
1000 HAS_EXPECTED_PREPOPULATED_SEARCH_PROVIDER_2;
1001 EXPECT_CALL(resetter(), ReportStatistics(0x00u, expected_mask));
1003 UnleashResetterAndWait();
1006 // Please see comments above ConstructProgramToCheckLoadedModuleDigests() to
1007 // understand how the following tests work.
1009 TEST_F(AutomaticProfileResetterTest, InputModuleDigestsCorrect) {
1010 SetTestingProgram(ConstructProgramToCheckLoadedModuleDigests());
1011 SetTestingHashSeed(kTestHashSeed);
1013 mock_delegate().emulated_loaded_module_digests().AppendString(
1015 mock_delegate().emulated_loaded_module_digests().AppendString(
1016 kTestModuleDigest2);
1018 mock_delegate().ExpectCallsToDependenciesSetUpMethods();
1019 mock_delegate().ExpectCallsToGetterMethods();
1020 uint32 expected_mask =
1021 HAS_EXPECTED_MODULE_DIGEST_1 | HAS_EXPECTED_MODULE_DIGEST_2;
1022 EXPECT_CALL(resetter(), ReportStatistics(0x00u, expected_mask));
1024 UnleashResetterAndWait();