OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/undo/undo_manager.h" | 5 #include "components/undo/undo_manager.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "components/undo/undo_manager_observer.h" | 10 #include "components/undo/undo_manager_observer.h" |
11 #include "components/undo/undo_operation.h" | 11 #include "components/undo/undo_operation.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 std::vector<UndoOperation*> ConvertToRawPtrVector( | |
17 const std::vector<std::unique_ptr<UndoOperation>>& args) { | |
18 std::vector<UndoOperation*> args_rawptrs; | |
19 for (auto i = args.begin(); i != args.end(); ++i) | |
20 args_rawptrs.push_back(i->get()); | |
21 return args_rawptrs; | |
22 } | |
23 | |
24 } // namespace | |
25 | |
26 // UndoManagerTestApi ---------------------------------------------------------- | |
27 | |
28 class UndoManagerTestApi { | |
29 public: | |
30 // Returns all UndoOperations that are awaiting Undo or Redo. | |
31 static std::vector<UndoOperation*> GetAllUndoOperations( | |
32 const UndoManager& undo_manager); | |
33 }; | |
sky
2017/01/24 18:15:52
private: DISALLOW_IMPLICIT_CONSTRUCTORS
ke.he
2017/01/25 15:03:51
Done.
| |
34 | |
35 std::vector<UndoOperation*> UndoManagerTestApi::GetAllUndoOperations( | |
36 const UndoManager& undo_manager) { | |
37 std::vector<UndoOperation*> result; | |
38 for (size_t i = 0; i < undo_manager.undo_actions_.size(); ++i) { | |
39 const std::vector<UndoOperation*> operations = | |
40 ConvertToRawPtrVector(undo_manager.undo_actions_[i]->undo_operations()); | |
41 result.insert(result.end(), operations.begin(), operations.end()); | |
42 } | |
43 for (size_t i = 0; i < undo_manager.redo_actions_.size(); ++i) { | |
44 const std::vector<UndoOperation*> operations = | |
45 ConvertToRawPtrVector(undo_manager.redo_actions_[i]->undo_operations()); | |
46 result.insert(result.end(), operations.begin(), operations.end()); | |
47 } | |
48 // Ensure that if an Undo is in progress the UndoOperations part of that | |
49 // UndoGroup are included in the returned set. This will ensure that any | |
50 // changes (such as renumbering) will be applied to any potentially | |
51 // unprocessed UndoOperations. | |
52 if (undo_manager.undo_in_progress_action_) { | |
53 const std::vector<UndoOperation*> operations = ConvertToRawPtrVector( | |
54 undo_manager.undo_in_progress_action_->undo_operations()); | |
55 result.insert(result.end(), operations.begin(), operations.end()); | |
56 } | |
57 | |
58 return result; | |
59 } | |
60 | |
61 namespace { | |
62 | |
16 class TestUndoOperation; | 63 class TestUndoOperation; |
17 | 64 |
18 // TestUndoService ------------------------------------------------------------- | 65 // TestUndoService ------------------------------------------------------------- |
19 | 66 |
20 class TestUndoService { | 67 class TestUndoService { |
21 public: | 68 public: |
22 TestUndoService(); | 69 TestUndoService(); |
23 ~TestUndoService(); | 70 ~TestUndoService(); |
24 | 71 |
25 void Redo(); | 72 void Redo(); |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 undo_service.TriggerOperation(); | 271 undo_service.TriggerOperation(); |
225 undo_service.undo_manager_.EndGroupingActions(); | 272 undo_service.undo_manager_.EndGroupingActions(); |
226 | 273 |
227 undo_service.TriggerOperation(); | 274 undo_service.TriggerOperation(); |
228 | 275 |
229 undo_service.undo_manager_.Undo(); | 276 undo_service.undo_manager_.Undo(); |
230 ASSERT_EQ(2U, undo_service.undo_manager_.undo_count()); | 277 ASSERT_EQ(2U, undo_service.undo_manager_.undo_count()); |
231 ASSERT_EQ(1U, undo_service.undo_manager_.redo_count()); | 278 ASSERT_EQ(1U, undo_service.undo_manager_.redo_count()); |
232 | 279 |
233 std::vector<UndoOperation*> all_operations = | 280 std::vector<UndoOperation*> all_operations = |
234 undo_service.undo_manager_.GetAllUndoOperations(); | 281 UndoManagerTestApi::GetAllUndoOperations(undo_service.undo_manager_); |
235 EXPECT_EQ(4U, all_operations.size()); | 282 EXPECT_EQ(4U, all_operations.size()); |
236 } | 283 } |
237 | 284 |
238 TEST(UndoServiceTest, ObserverCallbacks) { | 285 TEST(UndoServiceTest, ObserverCallbacks) { |
239 TestObserver observer; | 286 TestObserver observer; |
240 TestUndoService undo_service; | 287 TestUndoService undo_service; |
241 undo_service.undo_manager_.AddObserver(&observer); | 288 undo_service.undo_manager_.AddObserver(&observer); |
242 EXPECT_EQ(0, observer.state_change_count()); | 289 EXPECT_EQ(0, observer.state_change_count()); |
243 | 290 |
244 undo_service.TriggerOperation(); | 291 undo_service.TriggerOperation(); |
(...skipping 14 matching lines...) Expand all Loading... | |
259 undo_service.undo_manager_.Redo(); | 306 undo_service.undo_manager_.Redo(); |
260 int callback_count_after_redo = observer.state_change_count(); | 307 int callback_count_after_redo = observer.state_change_count(); |
261 EXPECT_GT(callback_count_after_redo, callback_count_after_undo); | 308 EXPECT_GT(callback_count_after_redo, callback_count_after_undo); |
262 | 309 |
263 undo_service.undo_manager_.RemoveObserver(&observer); | 310 undo_service.undo_manager_.RemoveObserver(&observer); |
264 undo_service.undo_manager_.Undo(); | 311 undo_service.undo_manager_.Undo(); |
265 EXPECT_EQ(callback_count_after_redo, observer.state_change_count()); | 312 EXPECT_EQ(callback_count_after_redo, observer.state_change_count()); |
266 } | 313 } |
267 | 314 |
268 } // namespace | 315 } // namespace |
OLD | NEW |