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/location.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "components/sync_driver/sync_prefs.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using ::testing::Return
;
22 #if defined(ENABLE_PRE_SYNC_BACKUP)
24 class MockSigninManagerWrapper
: public SupervisedUserSigninManagerWrapper
{
26 MockSigninManagerWrapper() : SupervisedUserSigninManagerWrapper(NULL
, NULL
) {}
28 MOCK_CONST_METHOD0(GetEffectiveUsername
, std::string());
31 class FakeSyncPrefs
: public sync_driver::SyncPrefs
{
33 FakeSyncPrefs() : rollback_tries_left_(0) {}
35 int GetRemainingRollbackTries() const override
{
36 return rollback_tries_left_
;
39 void SetRemainingRollbackTries(int v
) override
{ rollback_tries_left_
= v
; }
42 int rollback_tries_left_
;
45 class BackupRollbackControllerTest
: public testing::Test
{
47 void ControllerCallback(bool start_backup
) {
49 backup_started_
= true;
51 rollback_started_
= true;
55 void SetUp() override
{
56 backup_started_
= false;
57 rollback_started_
= false;
59 EXPECT_CALL(signin_wrapper_
, GetEffectiveUsername())
60 .WillRepeatedly(Return(""));
62 controller_
.reset(new browser_sync::BackupRollbackController(
63 &fake_prefs_
, &signin_wrapper_
,
64 base::Bind(&BackupRollbackControllerTest::ControllerCallback
,
65 base::Unretained(this), true),
66 base::Bind(&BackupRollbackControllerTest::ControllerCallback
,
67 base::Unretained(this), false)));
71 base::RunLoop run_loop
;
72 loop_
.task_runner()->PostTask(FROM_HERE
, run_loop
.QuitClosure());
76 MockSigninManagerWrapper signin_wrapper_
;
77 FakeSyncPrefs fake_prefs_
;
78 scoped_ptr
<browser_sync::BackupRollbackController
> controller_
;
80 bool rollback_started_
;
81 base::MessageLoop loop_
;
84 TEST_F(BackupRollbackControllerTest
, StartBackup
) {
85 EXPECT_TRUE(controller_
->StartBackup());
87 EXPECT_TRUE(backup_started_
);
90 TEST_F(BackupRollbackControllerTest
, NoBackupIfDisabled
) {
91 base::CommandLine::ForCurrentProcess()->AppendSwitch(
92 switches::kSyncDisableBackup
);
94 base::RunLoop run_loop
;
95 EXPECT_FALSE(controller_
->StartBackup());
96 loop_
.task_runner()->PostTask(FROM_HERE
, run_loop
.QuitClosure());
98 EXPECT_FALSE(backup_started_
);
101 TEST_F(BackupRollbackControllerTest
, StartRollback
) {
102 fake_prefs_
.SetRemainingRollbackTries(1);
104 EXPECT_TRUE(controller_
->StartRollback());
106 EXPECT_TRUE(rollback_started_
);
107 EXPECT_EQ(0, fake_prefs_
.GetRemainingRollbackTries());
110 TEST_F(BackupRollbackControllerTest
, NoRollbackIfOutOfTries
) {
111 fake_prefs_
.SetRemainingRollbackTries(0);
113 EXPECT_FALSE(controller_
->StartRollback());
115 EXPECT_FALSE(rollback_started_
);
118 TEST_F(BackupRollbackControllerTest
, NoRollbackIfUserSignedIn
) {
119 fake_prefs_
.SetRemainingRollbackTries(1);
120 EXPECT_CALL(signin_wrapper_
, GetEffectiveUsername())
122 .WillOnce(Return("test"));
123 EXPECT_FALSE(controller_
->StartRollback());
124 EXPECT_EQ(0, fake_prefs_
.GetRemainingRollbackTries());
127 EXPECT_FALSE(backup_started_
);
128 EXPECT_FALSE(rollback_started_
);
131 TEST_F(BackupRollbackControllerTest
, NoRollbackIfDisabled
) {
132 fake_prefs_
.SetRemainingRollbackTries(1);
134 base::CommandLine::ForCurrentProcess()->AppendSwitch(
135 switches::kSyncDisableRollback
);
136 EXPECT_FALSE(controller_
->StartRollback());
137 EXPECT_EQ(0, fake_prefs_
.GetRemainingRollbackTries());
140 EXPECT_FALSE(rollback_started_
);
145 } // anonymous namespace