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 |
| 34 private: |
| 35 DISALLOW_IMPLICIT_CONSTRUCTORS(UndoManagerTestApi); |
| 36 }; |
| 37 |
| 38 std::vector<UndoOperation*> UndoManagerTestApi::GetAllUndoOperations( |
| 39 const UndoManager& undo_manager) { |
| 40 std::vector<UndoOperation*> result; |
| 41 for (size_t i = 0; i < undo_manager.undo_actions_.size(); ++i) { |
| 42 const std::vector<UndoOperation*> operations = |
| 43 ConvertToRawPtrVector(undo_manager.undo_actions_[i]->undo_operations()); |
| 44 result.insert(result.end(), operations.begin(), operations.end()); |
| 45 } |
| 46 for (size_t i = 0; i < undo_manager.redo_actions_.size(); ++i) { |
| 47 const std::vector<UndoOperation*> operations = |
| 48 ConvertToRawPtrVector(undo_manager.redo_actions_[i]->undo_operations()); |
| 49 result.insert(result.end(), operations.begin(), operations.end()); |
| 50 } |
| 51 // Ensure that if an Undo is in progress the UndoOperations part of that |
| 52 // UndoGroup are included in the returned set. This will ensure that any |
| 53 // changes (such as renumbering) will be applied to any potentially |
| 54 // unprocessed UndoOperations. |
| 55 if (undo_manager.undo_in_progress_action_) { |
| 56 const std::vector<UndoOperation*> operations = ConvertToRawPtrVector( |
| 57 undo_manager.undo_in_progress_action_->undo_operations()); |
| 58 result.insert(result.end(), operations.begin(), operations.end()); |
| 59 } |
| 60 |
| 61 return result; |
| 62 } |
| 63 |
| 64 namespace { |
| 65 |
16 class TestUndoOperation; | 66 class TestUndoOperation; |
17 | 67 |
18 // TestUndoService ------------------------------------------------------------- | 68 // TestUndoService ------------------------------------------------------------- |
19 | 69 |
20 class TestUndoService { | 70 class TestUndoService { |
21 public: | 71 public: |
22 TestUndoService(); | 72 TestUndoService(); |
23 ~TestUndoService(); | 73 ~TestUndoService(); |
24 | 74 |
25 void Redo(); | 75 void Redo(); |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 undo_service.TriggerOperation(); | 274 undo_service.TriggerOperation(); |
225 undo_service.undo_manager_.EndGroupingActions(); | 275 undo_service.undo_manager_.EndGroupingActions(); |
226 | 276 |
227 undo_service.TriggerOperation(); | 277 undo_service.TriggerOperation(); |
228 | 278 |
229 undo_service.undo_manager_.Undo(); | 279 undo_service.undo_manager_.Undo(); |
230 ASSERT_EQ(2U, undo_service.undo_manager_.undo_count()); | 280 ASSERT_EQ(2U, undo_service.undo_manager_.undo_count()); |
231 ASSERT_EQ(1U, undo_service.undo_manager_.redo_count()); | 281 ASSERT_EQ(1U, undo_service.undo_manager_.redo_count()); |
232 | 282 |
233 std::vector<UndoOperation*> all_operations = | 283 std::vector<UndoOperation*> all_operations = |
234 undo_service.undo_manager_.GetAllUndoOperations(); | 284 UndoManagerTestApi::GetAllUndoOperations(undo_service.undo_manager_); |
235 EXPECT_EQ(4U, all_operations.size()); | 285 EXPECT_EQ(4U, all_operations.size()); |
236 } | 286 } |
237 | 287 |
238 TEST(UndoServiceTest, ObserverCallbacks) { | 288 TEST(UndoServiceTest, ObserverCallbacks) { |
239 TestObserver observer; | 289 TestObserver observer; |
240 TestUndoService undo_service; | 290 TestUndoService undo_service; |
241 undo_service.undo_manager_.AddObserver(&observer); | 291 undo_service.undo_manager_.AddObserver(&observer); |
242 EXPECT_EQ(0, observer.state_change_count()); | 292 EXPECT_EQ(0, observer.state_change_count()); |
243 | 293 |
244 undo_service.TriggerOperation(); | 294 undo_service.TriggerOperation(); |
(...skipping 14 matching lines...) Expand all Loading... |
259 undo_service.undo_manager_.Redo(); | 309 undo_service.undo_manager_.Redo(); |
260 int callback_count_after_redo = observer.state_change_count(); | 310 int callback_count_after_redo = observer.state_change_count(); |
261 EXPECT_GT(callback_count_after_redo, callback_count_after_undo); | 311 EXPECT_GT(callback_count_after_redo, callback_count_after_undo); |
262 | 312 |
263 undo_service.undo_manager_.RemoveObserver(&observer); | 313 undo_service.undo_manager_.RemoveObserver(&observer); |
264 undo_service.undo_manager_.Undo(); | 314 undo_service.undo_manager_.Undo(); |
265 EXPECT_EQ(callback_count_after_redo, observer.state_change_count()); | 315 EXPECT_EQ(callback_count_after_redo, observer.state_change_count()); |
266 } | 316 } |
267 | 317 |
268 } // namespace | 318 } // namespace |
OLD | NEW |