Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / sync / backup_rollback_controller_unittest.cc
blob2f378ab95447de291cc7c3019088eaeb79e88bd4
1 // Copyright 2014 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/sync/backup_rollback_controller.h"
7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "components/sync_driver/sync_prefs.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using ::testing::Return;
19 namespace {
21 #if defined(ENABLE_PRE_SYNC_BACKUP)
23 class MockSigninManagerWrapper : public SupervisedUserSigninManagerWrapper {
24 public:
25 MockSigninManagerWrapper() : SupervisedUserSigninManagerWrapper(NULL, NULL) {}
27 MOCK_CONST_METHOD0(GetEffectiveUsername, std::string());
30 class FakeSyncPrefs : public sync_driver::SyncPrefs {
31 public:
32 FakeSyncPrefs() : rollback_tries_left_(0) {}
34 virtual int GetRemainingRollbackTries() const OVERRIDE {
35 return rollback_tries_left_;
38 virtual void SetRemainingRollbackTries(int v) OVERRIDE {
39 rollback_tries_left_ = v;
42 private:
43 int rollback_tries_left_;
46 class BackupRollbackControllerTest : public testing::Test {
47 public:
48 void ControllerCallback(bool start_backup) {
49 if (start_backup)
50 backup_started_ = true;
51 else
52 rollback_started_ = true;
55 protected:
56 virtual void SetUp() OVERRIDE {
57 backup_started_ = false;
58 rollback_started_ = false;
60 EXPECT_CALL(signin_wrapper_, GetEffectiveUsername())
61 .WillRepeatedly(Return(""));
63 controller_.reset(new browser_sync::BackupRollbackController(
64 &fake_prefs_, &signin_wrapper_,
65 base::Bind(&BackupRollbackControllerTest::ControllerCallback,
66 base::Unretained(this), true),
67 base::Bind(&BackupRollbackControllerTest::ControllerCallback,
68 base::Unretained(this), false)));
71 void PumpLoop() {
72 base::RunLoop run_loop;
73 loop_.PostTask(FROM_HERE, run_loop.QuitClosure());
74 run_loop.Run();
77 MockSigninManagerWrapper signin_wrapper_;
78 FakeSyncPrefs fake_prefs_;
79 scoped_ptr<browser_sync::BackupRollbackController> controller_;
80 bool backup_started_;
81 bool rollback_started_;
82 base::MessageLoop loop_;
85 TEST_F(BackupRollbackControllerTest, StartBackup) {
86 EXPECT_TRUE(controller_->StartBackup());
87 PumpLoop();
88 EXPECT_TRUE(backup_started_);
91 TEST_F(BackupRollbackControllerTest, NoBackupIfDisabled) {
92 CommandLine::ForCurrentProcess()->AppendSwitch(
93 switches::kSyncDisableBackup);
95 base::RunLoop run_loop;
96 EXPECT_FALSE(controller_->StartBackup());
97 loop_.PostTask(FROM_HERE, run_loop.QuitClosure());
98 run_loop.Run();
99 EXPECT_FALSE(backup_started_);
102 TEST_F(BackupRollbackControllerTest, StartRollback) {
103 fake_prefs_.SetRemainingRollbackTries(1);
105 EXPECT_TRUE(controller_->StartRollback());
106 PumpLoop();
107 EXPECT_TRUE(rollback_started_);
108 EXPECT_EQ(0, fake_prefs_.GetRemainingRollbackTries());
111 TEST_F(BackupRollbackControllerTest, NoRollbackIfOutOfTries) {
112 fake_prefs_.SetRemainingRollbackTries(0);
114 EXPECT_FALSE(controller_->StartRollback());
115 PumpLoop();
116 EXPECT_FALSE(rollback_started_);
119 TEST_F(BackupRollbackControllerTest, NoRollbackIfUserSignedIn) {
120 fake_prefs_.SetRemainingRollbackTries(1);
121 EXPECT_CALL(signin_wrapper_, GetEffectiveUsername())
122 .Times(1)
123 .WillOnce(Return("test"));
124 EXPECT_FALSE(controller_->StartRollback());
125 EXPECT_EQ(0, fake_prefs_.GetRemainingRollbackTries());
127 PumpLoop();
128 EXPECT_FALSE(backup_started_);
129 EXPECT_FALSE(rollback_started_);
132 TEST_F(BackupRollbackControllerTest, NoRollbackIfDisabled) {
133 fake_prefs_.SetRemainingRollbackTries(1);
135 CommandLine::ForCurrentProcess()->AppendSwitch(
136 switches::kSyncDisableRollback);
137 EXPECT_FALSE(controller_->StartRollback());
138 EXPECT_EQ(0, fake_prefs_.GetRemainingRollbackTries());
140 PumpLoop();
141 EXPECT_FALSE(rollback_started_);
144 #endif
146 } // anonymous namespace