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
;
21 #if defined(ENABLE_PRE_SYNC_BACKUP)
23 class MockSigninManagerWrapper
: public SupervisedUserSigninManagerWrapper
{
25 MockSigninManagerWrapper() : SupervisedUserSigninManagerWrapper(NULL
, NULL
) {}
27 MOCK_CONST_METHOD0(GetEffectiveUsername
, std::string());
30 class FakeSyncPrefs
: public sync_driver::SyncPrefs
{
32 FakeSyncPrefs() : rollback_tries_left_(0) {}
34 int GetRemainingRollbackTries() const override
{
35 return rollback_tries_left_
;
38 void SetRemainingRollbackTries(int v
) override
{ rollback_tries_left_
= v
; }
41 int rollback_tries_left_
;
44 class BackupRollbackControllerTest
: public testing::Test
{
46 void ControllerCallback(bool start_backup
) {
48 backup_started_
= true;
50 rollback_started_
= true;
54 void SetUp() override
{
55 backup_started_
= false;
56 rollback_started_
= false;
58 EXPECT_CALL(signin_wrapper_
, GetEffectiveUsername())
59 .WillRepeatedly(Return(""));
61 controller_
.reset(new browser_sync::BackupRollbackController(
62 &fake_prefs_
, &signin_wrapper_
,
63 base::Bind(&BackupRollbackControllerTest::ControllerCallback
,
64 base::Unretained(this), true),
65 base::Bind(&BackupRollbackControllerTest::ControllerCallback
,
66 base::Unretained(this), false)));
70 base::RunLoop run_loop
;
71 loop_
.PostTask(FROM_HERE
, run_loop
.QuitClosure());
75 MockSigninManagerWrapper signin_wrapper_
;
76 FakeSyncPrefs fake_prefs_
;
77 scoped_ptr
<browser_sync::BackupRollbackController
> controller_
;
79 bool rollback_started_
;
80 base::MessageLoop loop_
;
83 TEST_F(BackupRollbackControllerTest
, StartBackup
) {
84 EXPECT_TRUE(controller_
->StartBackup());
86 EXPECT_TRUE(backup_started_
);
89 TEST_F(BackupRollbackControllerTest
, NoBackupIfDisabled
) {
90 base::CommandLine::ForCurrentProcess()->AppendSwitch(
91 switches::kSyncDisableBackup
);
93 base::RunLoop run_loop
;
94 EXPECT_FALSE(controller_
->StartBackup());
95 loop_
.PostTask(FROM_HERE
, run_loop
.QuitClosure());
97 EXPECT_FALSE(backup_started_
);
100 TEST_F(BackupRollbackControllerTest
, StartRollback
) {
101 fake_prefs_
.SetRemainingRollbackTries(1);
103 EXPECT_TRUE(controller_
->StartRollback());
105 EXPECT_TRUE(rollback_started_
);
106 EXPECT_EQ(0, fake_prefs_
.GetRemainingRollbackTries());
109 TEST_F(BackupRollbackControllerTest
, NoRollbackIfOutOfTries
) {
110 fake_prefs_
.SetRemainingRollbackTries(0);
112 EXPECT_FALSE(controller_
->StartRollback());
114 EXPECT_FALSE(rollback_started_
);
117 TEST_F(BackupRollbackControllerTest
, NoRollbackIfUserSignedIn
) {
118 fake_prefs_
.SetRemainingRollbackTries(1);
119 EXPECT_CALL(signin_wrapper_
, GetEffectiveUsername())
121 .WillOnce(Return("test"));
122 EXPECT_FALSE(controller_
->StartRollback());
123 EXPECT_EQ(0, fake_prefs_
.GetRemainingRollbackTries());
126 EXPECT_FALSE(backup_started_
);
127 EXPECT_FALSE(rollback_started_
);
130 TEST_F(BackupRollbackControllerTest
, NoRollbackIfDisabled
) {
131 fake_prefs_
.SetRemainingRollbackTries(1);
133 base::CommandLine::ForCurrentProcess()->AppendSwitch(
134 switches::kSyncDisableRollback
);
135 EXPECT_FALSE(controller_
->StartRollback());
136 EXPECT_EQ(0, fake_prefs_
.GetRemainingRollbackTries());
139 EXPECT_FALSE(rollback_started_
);
144 } // anonymous namespace