Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / sync / backup_rollback_controller_unittest.cc
blob50670f6ecadf6c7b1ebf30284b3c14a67b657cbd
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;
20 namespace {
22 #if defined(ENABLE_PRE_SYNC_BACKUP)
24 class MockSigninManagerWrapper : public SupervisedUserSigninManagerWrapper {
25 public:
26 MockSigninManagerWrapper() : SupervisedUserSigninManagerWrapper(NULL, NULL) {}
28 MOCK_CONST_METHOD0(GetEffectiveUsername, std::string());
31 class FakeSyncPrefs : public sync_driver::SyncPrefs {
32 public:
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; }
41 private:
42 int rollback_tries_left_;
45 class BackupRollbackControllerTest : public testing::Test {
46 public:
47 void ControllerCallback(bool start_backup) {
48 if (start_backup)
49 backup_started_ = true;
50 else
51 rollback_started_ = true;
54 protected:
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)));
70 void PumpLoop() {
71 base::RunLoop run_loop;
72 loop_.task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure());
73 run_loop.Run();
76 MockSigninManagerWrapper signin_wrapper_;
77 FakeSyncPrefs fake_prefs_;
78 scoped_ptr<browser_sync::BackupRollbackController> controller_;
79 bool backup_started_;
80 bool rollback_started_;
81 base::MessageLoop loop_;
84 TEST_F(BackupRollbackControllerTest, StartBackup) {
85 EXPECT_TRUE(controller_->StartBackup());
86 PumpLoop();
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());
97 run_loop.Run();
98 EXPECT_FALSE(backup_started_);
101 TEST_F(BackupRollbackControllerTest, StartRollback) {
102 fake_prefs_.SetRemainingRollbackTries(1);
104 EXPECT_TRUE(controller_->StartRollback());
105 PumpLoop();
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());
114 PumpLoop();
115 EXPECT_FALSE(rollback_started_);
118 TEST_F(BackupRollbackControllerTest, NoRollbackIfUserSignedIn) {
119 fake_prefs_.SetRemainingRollbackTries(1);
120 EXPECT_CALL(signin_wrapper_, GetEffectiveUsername())
121 .Times(1)
122 .WillOnce(Return("test"));
123 EXPECT_FALSE(controller_->StartRollback());
124 EXPECT_EQ(0, fake_prefs_.GetRemainingRollbackTries());
126 PumpLoop();
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());
139 PumpLoop();
140 EXPECT_FALSE(rollback_started_);
143 #endif
145 } // anonymous namespace