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 virtual int GetRemainingRollbackTries() const OVERRIDE
{
35 return rollback_tries_left_
;
38 virtual void SetRemainingRollbackTries(int v
) OVERRIDE
{
39 rollback_tries_left_
= v
;
43 int rollback_tries_left_
;
46 class BackupRollbackControllerTest
: public testing::Test
{
48 void ControllerCallback(bool start_backup
) {
50 backup_started_
= true;
52 rollback_started_
= true;
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)));
72 base::RunLoop run_loop
;
73 loop_
.PostTask(FROM_HERE
, run_loop
.QuitClosure());
77 MockSigninManagerWrapper signin_wrapper_
;
78 FakeSyncPrefs fake_prefs_
;
79 scoped_ptr
<browser_sync::BackupRollbackController
> controller_
;
81 bool rollback_started_
;
82 base::MessageLoop loop_
;
85 TEST_F(BackupRollbackControllerTest
, StartBackup
) {
86 EXPECT_TRUE(controller_
->StartBackup());
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());
99 EXPECT_FALSE(backup_started_
);
102 TEST_F(BackupRollbackControllerTest
, StartRollback
) {
103 fake_prefs_
.SetRemainingRollbackTries(1);
105 EXPECT_TRUE(controller_
->StartRollback());
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());
116 EXPECT_FALSE(rollback_started_
);
119 TEST_F(BackupRollbackControllerTest
, NoRollbackIfUserSignedIn
) {
120 fake_prefs_
.SetRemainingRollbackTries(1);
121 EXPECT_CALL(signin_wrapper_
, GetEffectiveUsername())
123 .WillOnce(Return("test"));
124 EXPECT_FALSE(controller_
->StartRollback());
125 EXPECT_EQ(0, fake_prefs_
.GetRemainingRollbackTries());
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());
141 EXPECT_FALSE(rollback_started_
);
146 } // anonymous namespace