NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / undo / undo_manager_test.cc
blob70215d7116c99e4a413eb09796e980155e277124
1 // Copyright 2013 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 "base/auto_reset.h"
6 #include "chrome/browser/undo/undo_manager.h"
7 #include "chrome/browser/undo/undo_operation.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace {
12 class TestUndoOperation;
14 // TestUndoService -------------------------------------------------------------
16 class TestUndoService {
17 public:
18 TestUndoService();
19 ~TestUndoService();
21 void Redo();
22 void TriggerOperation();
23 void RecordUndoCall();
25 UndoManager undo_manager_;
27 bool performing_redo_;
29 int undo_operation_count_;
30 int redo_operation_count_;
33 // TestUndoOperation -----------------------------------------------------------
35 class TestUndoOperation : public UndoOperation {
36 public:
37 explicit TestUndoOperation(TestUndoService* undo_service);
38 virtual ~TestUndoOperation();
40 // UndoOperation:
41 virtual void Undo() OVERRIDE;
42 virtual int GetUndoLabelId() const OVERRIDE;
43 virtual int GetRedoLabelId() const OVERRIDE;
45 private:
46 TestUndoService* undo_service_;
48 DISALLOW_COPY_AND_ASSIGN(TestUndoOperation);
51 TestUndoOperation::TestUndoOperation(TestUndoService* undo_service)
52 : undo_service_(undo_service) {
55 TestUndoOperation::~TestUndoOperation() {
58 void TestUndoOperation::Undo() {
59 undo_service_->TriggerOperation();
60 undo_service_->RecordUndoCall();
63 int TestUndoOperation::GetUndoLabelId() const {
64 return 0;
67 int TestUndoOperation::GetRedoLabelId() const {
68 return 0;
71 // TestUndoService -------------------------------------------------------------
73 TestUndoService::TestUndoService() : performing_redo_(false),
74 undo_operation_count_(0),
75 redo_operation_count_(0) {
78 TestUndoService::~TestUndoService() {
81 void TestUndoService::Redo() {
82 base::AutoReset<bool> incoming_changes(&performing_redo_, true);
83 undo_manager_.Redo();
86 void TestUndoService::TriggerOperation() {
87 scoped_ptr<TestUndoOperation> op(new TestUndoOperation(this));
88 undo_manager_.AddUndoOperation(op.PassAs<UndoOperation>());
91 void TestUndoService::RecordUndoCall() {
92 if (performing_redo_)
93 ++redo_operation_count_;
94 else
95 ++undo_operation_count_;
98 // Tests -----------------------------------------------------------------------
100 TEST(UndoServiceTest, AddUndoActions) {
101 TestUndoService undo_service;
103 undo_service.TriggerOperation();
104 undo_service.TriggerOperation();
105 EXPECT_EQ(2U, undo_service.undo_manager_.undo_count());
106 EXPECT_EQ(0U, undo_service.undo_manager_.redo_count());
109 TEST(UndoServiceTest, UndoMultipleActions) {
110 TestUndoService undo_service;
112 undo_service.TriggerOperation();
113 undo_service.TriggerOperation();
115 undo_service.undo_manager_.Undo();
116 EXPECT_EQ(1U, undo_service.undo_manager_.undo_count());
117 EXPECT_EQ(1U, undo_service.undo_manager_.redo_count());
119 undo_service.undo_manager_.Undo();
120 EXPECT_EQ(0U, undo_service.undo_manager_.undo_count());
121 EXPECT_EQ(2U, undo_service.undo_manager_.redo_count());
123 EXPECT_EQ(2, undo_service.undo_operation_count_);
124 EXPECT_EQ(0, undo_service.redo_operation_count_);
127 TEST(UndoServiceTest, RedoAction) {
128 TestUndoService undo_service;
130 undo_service.TriggerOperation();
132 undo_service.undo_manager_.Undo();
133 EXPECT_EQ(0U, undo_service.undo_manager_.undo_count());
134 EXPECT_EQ(1U, undo_service.undo_manager_.redo_count());
136 undo_service.Redo();
137 EXPECT_EQ(1U, undo_service.undo_manager_.undo_count());
138 EXPECT_EQ(0U, undo_service.undo_manager_.redo_count());
140 EXPECT_EQ(1, undo_service.undo_operation_count_);
141 EXPECT_EQ(1, undo_service.redo_operation_count_);
144 TEST(UndoServiceTest, GroupActions) {
145 TestUndoService undo_service;
147 // Add two operations in a single action.
148 undo_service.undo_manager_.StartGroupingActions();
149 undo_service.TriggerOperation();
150 undo_service.TriggerOperation();
151 undo_service.undo_manager_.EndGroupingActions();
153 // Check that only one action is created.
154 EXPECT_EQ(1U, undo_service.undo_manager_.undo_count());
155 EXPECT_EQ(0U, undo_service.undo_manager_.redo_count());
157 undo_service.undo_manager_.Undo();
158 EXPECT_EQ(0U, undo_service.undo_manager_.undo_count());
159 EXPECT_EQ(1U, undo_service.undo_manager_.redo_count());
161 undo_service.Redo();
162 EXPECT_EQ(1U, undo_service.undo_manager_.undo_count());
163 EXPECT_EQ(0U, undo_service.undo_manager_.redo_count());
165 // Check that both operations were called in Undo and Redo.
166 EXPECT_EQ(2, undo_service.undo_operation_count_);
167 EXPECT_EQ(2, undo_service.redo_operation_count_);
170 TEST(UndoServiceTest, SuspendUndoTracking) {
171 TestUndoService undo_service;
173 undo_service.undo_manager_.SuspendUndoTracking();
174 EXPECT_TRUE(undo_service.undo_manager_.IsUndoTrakingSuspended());
176 undo_service.TriggerOperation();
178 undo_service.undo_manager_.ResumeUndoTracking();
179 EXPECT_FALSE(undo_service.undo_manager_.IsUndoTrakingSuspended());
181 EXPECT_EQ(0U, undo_service.undo_manager_.undo_count());
182 EXPECT_EQ(0U, undo_service.undo_manager_.redo_count());
185 TEST(UndoServiceTest, RedoEmptyAfterNewAction) {
186 TestUndoService undo_service;
188 undo_service.TriggerOperation();
189 undo_service.undo_manager_.Undo();
190 EXPECT_EQ(0U, undo_service.undo_manager_.undo_count());
191 EXPECT_EQ(1U, undo_service.undo_manager_.redo_count());
193 undo_service.TriggerOperation();
194 EXPECT_EQ(1U, undo_service.undo_manager_.undo_count());
195 EXPECT_EQ(0U, undo_service.undo_manager_.redo_count());
198 TEST(UndoServiceTest, GetAllUndoOperations) {
199 TestUndoService undo_service;
201 undo_service.TriggerOperation();
203 undo_service.undo_manager_.StartGroupingActions();
204 undo_service.TriggerOperation();
205 undo_service.TriggerOperation();
206 undo_service.undo_manager_.EndGroupingActions();
208 undo_service.TriggerOperation();
210 undo_service.undo_manager_.Undo();
211 ASSERT_EQ(2U, undo_service.undo_manager_.undo_count());
212 ASSERT_EQ(1U, undo_service.undo_manager_.redo_count());
214 std::vector<UndoOperation*> all_operations =
215 undo_service.undo_manager_.GetAllUndoOperations();
216 EXPECT_EQ(4U, all_operations.size());
219 } // namespace