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

Unified Diff: chrome/browser/profile_resetter/automatic_profile_resetter_unittest.cc

Issue 24533002: Added the AutomaticProfileResetter service. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/profile_resetter/automatic_profile_resetter_unittest.cc
diff --git a/chrome/browser/profile_resetter/automatic_profile_resetter_unittest.cc b/chrome/browser/profile_resetter/automatic_profile_resetter_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..798b3ca35aa3d251823d3903cb2bc2c2f941055d
--- /dev/null
+++ b/chrome/browser/profile_resetter/automatic_profile_resetter_unittest.cc
@@ -0,0 +1,256 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/metrics/field_trial.h"
+#include "base/run_loop.h"
+#include "chrome/browser/profile_resetter/automatic_profile_resetter.h"
+#include "chrome/browser/profile_resetter/automatic_profile_resetter_factory.h"
+#include "chrome/browser/profile_resetter/automatic_profile_resetter_mementos.h"
+#include "chrome/common/extensions/value_builder.h"
+#include "chrome/test/base/scoped_testing_local_state.h"
+#include "chrome/test/base/testing_browser_process.h"
+#include "chrome/test/base/testing_pref_service_syncable.h"
+#include "chrome/test/base/testing_profile.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using testing::_;
+using testing::Invoke;
+using testing::Return;
+
+namespace profile_resetter {
+namespace {
+
+const char kAutomaticProfileResetStudyName[] = "AutomaticProfileReset";
+const char kStudyDisabledGroupName[] = "Disabled";
+const char kStudyDryRunGroupName[] = "DryRun";
+const char kStudyEnabledGroupName[] = "Enabled";
+
+const char kTestHashSeed[] = "testing-hash-seed";
+const char kTestMementoValue[] = "testing-menento-value";
+
+// Helpers ------------------------------------------------------------------
+
+class MockProfileResetterDelegate : public AutomaticProfileResetterDelegate {
+ public:
+ MockProfileResetterDelegate() {}
+ virtual ~MockProfileResetterDelegate() {}
+
+ MOCK_METHOD0(ShowPrompt, void());
+ MOCK_METHOD2(ReportStatistics, void(uint32, uint32));
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockProfileResetterDelegate);
+};
+
+class FileHostedPromptMementoSynchronous : protected FileHostedPromptMemento {
+ public:
+ explicit FileHostedPromptMementoSynchronous(Profile* profile)
+ : FileHostedPromptMemento(profile) {}
+
+ std::string ReadValue() const {
+ std::string result;
+ FileHostedPromptMemento::ReadValue(base::Bind(&AssignArgumentTo, &result));
+ base::RunLoop().RunUntilIdle();
+ return result;
+ }
+
+ void StoreValue(const std::string& value) {
+ FileHostedPromptMemento::StoreValue(value);
+ base::RunLoop().RunUntilIdle();
+ }
+
+ private:
+ static void AssignArgumentTo(std::string* target, const std::string& value) {
+ *target = value;
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(FileHostedPromptMementoSynchronous);
+};
+
+// Test fixtures -------------------------------------------------------------
+
+class AutomaticProfileResetterTestBase : public testing::Test {
+ protected:
+ explicit AutomaticProfileResetterTestBase(
+ const std::string& experiment_group_name)
+ : local_state_(TestingBrowserProcess::GetGlobal()),
+ experiment_group_name_(experiment_group_name),
+ mock_delegate_(NULL) {
+ // Make sure the factory is not optimized away, so prefs get registered.
+ AutomaticProfileResetterFactory::GetInstance();
+ }
+
+ virtual void SetUp() OVERRIDE {
+ profile_.reset(new TestingProfile());
+ field_trials_.reset(new base::FieldTrialList(NULL));
+ base::FieldTrialList::CreateFieldTrial(kAutomaticProfileResetStudyName,
+ experiment_group_name_);
+ mock_delegate_ = new testing::StrictMock<MockProfileResetterDelegate>();
+ resetter_.reset(new AutomaticProfileResetter(profile_.get()));
+ }
+
+ void UnleashResetterAndWait() {
+ resetter_->Initialize();
+ resetter_->SetDelegateForTesting(mock_delegate_); // Takes ownership.
+ resetter_->SetHashSeedForTesting(kTestHashSeed);
+ resetter_->SetProgramForTesting(""); // TODO(engedy).
+ base::RunLoop().RunUntilIdle();
+ }
+
+ TestingProfile* profile() { return profile_.get(); }
+
+ MockProfileResetterDelegate& mock_delegate() { return *mock_delegate_; }
+ AutomaticProfileResetter* resetter() { return resetter_.get(); }
+
+ private:
+ content::TestBrowserThreadBundle thread_bundle_;
+ ScopedTestingLocalState local_state_;
+ scoped_ptr<TestingProfile> profile_;
+ scoped_ptr<base::FieldTrialList> field_trials_;
+ std::string experiment_group_name_;
+ std::string fake_program_;
+
+ scoped_ptr<AutomaticProfileResetter> resetter_;
+ MockProfileResetterDelegate* mock_delegate_;
+
+ DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterTestBase);
+};
+
+class AutomaticProfileResetterTest : public AutomaticProfileResetterTestBase {
+ protected:
+ AutomaticProfileResetterTest()
+ : AutomaticProfileResetterTestBase(kStudyEnabledGroupName) {}
+};
+
+class AutomaticProfileResetterDryRun : public AutomaticProfileResetterTestBase {
+ protected:
+ AutomaticProfileResetterDryRun()
+ : AutomaticProfileResetterTestBase(kStudyDryRunGroupName) {}
+};
+
+class AutomaticProfileResetterTestDisabled
+ : public AutomaticProfileResetterTestBase {
+ protected:
+ AutomaticProfileResetterTestDisabled()
+ : AutomaticProfileResetterTestBase(kStudyDisabledGroupName) {}
+};
+
+// Tests ---------------------------------------------------------------------
+
+TEST_F(AutomaticProfileResetterTestDisabled, NothingIsDoneWhenDisabled) {
+ // Nothing is expected to be done.
+ UnleashResetterAndWait();
+}
+
+TEST_F(AutomaticProfileResetterTest, ConditionNotSatisfied) {
+ PreferenceHostedPromptMemento memento_in_prefs(profile());
+ LocalStateHostedPromptMemento memento_in_local_state(profile());
+ FileHostedPromptMementoSynchronous memento_in_file(profile());
+
+ EXPECT_EQ("", memento_in_prefs.ReadValue());
+ EXPECT_EQ("", memento_in_local_state.ReadValue());
+ EXPECT_EQ("", memento_in_file.ReadValue());
+
+ // The preference value that would trigger reset is not set, so ShowPrompt()
+ // is not expected to be called, and mementos are not expected to be stored.
+
+ UnleashResetterAndWait();
+
+ EXPECT_EQ("", memento_in_prefs.ReadValue());
+ EXPECT_EQ("", memento_in_local_state.ReadValue());
+ EXPECT_EQ("", memento_in_file.ReadValue());
+}
+
+TEST_F(AutomaticProfileResetterTest, ConditionSatisfied) {
+ PreferenceHostedPromptMemento memento_in_prefs(profile());
+ LocalStateHostedPromptMemento memento_in_local_state(profile());
+ FileHostedPromptMementoSynchronous memento_in_file(profile());
+
+ EXPECT_EQ("", memento_in_prefs.ReadValue());
+ EXPECT_EQ("", memento_in_local_state.ReadValue());
+ EXPECT_EQ("", memento_in_file.ReadValue());
+
+ // TODO(engedy): Fake satisfied conditions.
+
+ EXPECT_CALL(mock_delegate(), ShowPrompt());
+
+ UnleashResetterAndWait();
+
+ EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue());
+ EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue());
+ EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue());
+}
+
+TEST_F(AutomaticProfileResetterTest, ConditionNotSatisfiedButManagedPref) {
+ PreferenceHostedPromptMemento memento_in_prefs(profile());
+ LocalStateHostedPromptMemento memento_in_local_state(profile());
+ FileHostedPromptMementoSynchronous memento_in_file(profile());
+
+ EXPECT_EQ("", memento_in_prefs.ReadValue());
+ EXPECT_EQ("", memento_in_local_state.ReadValue());
+ EXPECT_EQ("", memento_in_file.ReadValue());
+
+ // TODO(engedy): Fake satisfied conditions, but with a managed pref.
+
+ // No prompt is expected to be shown.
+
+ UnleashResetterAndWait();
+
+ EXPECT_EQ("", memento_in_prefs.ReadValue());
+ EXPECT_EQ("", memento_in_local_state.ReadValue());
+ EXPECT_EQ("", memento_in_file.ReadValue());
+}
+
+TEST_F(AutomaticProfileResetterTest, PrefHostedMementoPreventsPrompt) {
+ PreferenceHostedPromptMemento memento_in_prefs(profile());
+ LocalStateHostedPromptMemento memento_in_local_state(profile());
+ FileHostedPromptMementoSynchronous memento_in_file(profile());
+
+ memento_in_prefs.StoreValue(kTestMementoValue);
+
+ // No prompt is expected to be shown.
+
+ UnleashResetterAndWait();
+
+ EXPECT_EQ(kTestMementoValue, memento_in_prefs.ReadValue());
+ EXPECT_EQ("", memento_in_local_state.ReadValue());
+ EXPECT_EQ("", memento_in_file.ReadValue());
+}
+
+TEST_F(AutomaticProfileResetterTest, LocalStateHostedMementoPreventsPrompt) {
+ PreferenceHostedPromptMemento memento_in_prefs(profile());
+ LocalStateHostedPromptMemento memento_in_local_state(profile());
+ FileHostedPromptMementoSynchronous memento_in_file(profile());
+
+ memento_in_local_state.StoreValue(kTestMementoValue);
+
+ // No prompt is expected to be shown.
+
+ UnleashResetterAndWait();
+
+ EXPECT_EQ("", memento_in_prefs.ReadValue());
+ EXPECT_EQ(kTestMementoValue, memento_in_local_state.ReadValue());
+ EXPECT_EQ("", memento_in_file.ReadValue());
+}
+
+TEST_F(AutomaticProfileResetterTest, FileHostedMementoPreventsPrompt) {
+ PreferenceHostedPromptMemento memento_in_prefs(profile());
+ LocalStateHostedPromptMemento memento_in_local_state(profile());
+ FileHostedPromptMementoSynchronous memento_in_file(profile());
+
+ memento_in_file.StoreValue(kTestMementoValue);
+
+ // No prompt is expected to be shown.
+
+ UnleashResetterAndWait();
+
+ EXPECT_EQ("", memento_in_prefs.ReadValue());
+ EXPECT_EQ("", memento_in_local_state.ReadValue());
+ EXPECT_EQ(kTestMementoValue, memento_in_file.ReadValue());
+}
+
+} // namespace
+} // namespace profile_resetter

Powered by Google App Engine
This is Rietveld 408576698