test_undo_redo.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**************************************************************************/
  2. /* test_undo_redo.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_UNDO_REDO_H
  31. #define TEST_UNDO_REDO_H
  32. #include "core/object/undo_redo.h"
  33. #include "tests/test_macros.h"
  34. // Declared in global namespace because of GDCLASS macro warning (Windows):
  35. // "Unqualified friend declaration referring to type outside of the nearest enclosing namespace
  36. // is a Microsoft extension; add a nested name specifier".
  37. class _TestUndoRedoObject : public Object {
  38. GDCLASS(_TestUndoRedoObject, Object);
  39. int property_value = 0;
  40. protected:
  41. static void _bind_methods() {
  42. ClassDB::bind_method(D_METHOD("set_property", "property"), &_TestUndoRedoObject::set_property);
  43. ClassDB::bind_method(D_METHOD("get_property"), &_TestUndoRedoObject::get_property);
  44. ADD_PROPERTY(PropertyInfo(Variant::INT, "property"), "set_property", "get_property");
  45. }
  46. public:
  47. void set_property(int value) { property_value = value; }
  48. int get_property() const { return property_value; }
  49. void add_to_property(int value) { property_value += value; }
  50. void subtract_from_property(int value) { property_value -= value; }
  51. };
  52. namespace TestUndoRedo {
  53. void set_property_action(UndoRedo *undo_redo, const String &name, _TestUndoRedoObject *test_object, int value, UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE) {
  54. undo_redo->create_action(name, merge_mode);
  55. undo_redo->add_do_property(test_object, "property", value);
  56. undo_redo->add_undo_property(test_object, "property", test_object->get_property());
  57. undo_redo->commit_action();
  58. }
  59. void increment_property_action(UndoRedo *undo_redo, const String &name, _TestUndoRedoObject *test_object, int value, UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE) {
  60. undo_redo->create_action(name, merge_mode);
  61. undo_redo->add_do_method(callable_mp(test_object, &_TestUndoRedoObject::add_to_property).bind(value));
  62. undo_redo->add_undo_method(callable_mp(test_object, &_TestUndoRedoObject::subtract_from_property).bind(value));
  63. undo_redo->commit_action();
  64. }
  65. TEST_CASE("[UndoRedo] Simple Property UndoRedo") {
  66. GDREGISTER_CLASS(_TestUndoRedoObject);
  67. UndoRedo *undo_redo = memnew(UndoRedo());
  68. _TestUndoRedoObject *test_object = memnew(_TestUndoRedoObject());
  69. CHECK(test_object->get_property() == 0);
  70. CHECK(undo_redo->get_version() == 1);
  71. CHECK(undo_redo->get_history_count() == 0);
  72. set_property_action(undo_redo, "Set Property", test_object, 10);
  73. CHECK(test_object->get_property() == 10);
  74. CHECK(undo_redo->get_version() == 2);
  75. CHECK(undo_redo->get_history_count() == 1);
  76. undo_redo->undo();
  77. CHECK(test_object->get_property() == 0);
  78. CHECK(undo_redo->get_version() == 1);
  79. CHECK(undo_redo->get_history_count() == 1);
  80. undo_redo->redo();
  81. CHECK(test_object->get_property() == 10);
  82. CHECK(undo_redo->get_version() == 2);
  83. CHECK(undo_redo->get_history_count() == 1);
  84. set_property_action(undo_redo, "Set Property", test_object, 100);
  85. CHECK(test_object->get_property() == 100);
  86. CHECK(undo_redo->get_version() == 3);
  87. CHECK(undo_redo->get_history_count() == 2);
  88. set_property_action(undo_redo, "Set Property", test_object, 1000);
  89. CHECK(test_object->get_property() == 1000);
  90. CHECK(undo_redo->get_version() == 4);
  91. CHECK(undo_redo->get_history_count() == 3);
  92. undo_redo->undo();
  93. CHECK(test_object->get_property() == 100);
  94. CHECK(undo_redo->get_version() == 3);
  95. CHECK(undo_redo->get_history_count() == 3);
  96. memdelete(test_object);
  97. memdelete(undo_redo);
  98. }
  99. TEST_CASE("[UndoRedo] Merge Property UndoRedo") {
  100. GDREGISTER_CLASS(_TestUndoRedoObject);
  101. UndoRedo *undo_redo = memnew(UndoRedo());
  102. _TestUndoRedoObject *test_object = memnew(_TestUndoRedoObject());
  103. CHECK(test_object->get_property() == 0);
  104. CHECK(undo_redo->get_version() == 1);
  105. CHECK(undo_redo->get_history_count() == 0);
  106. set_property_action(undo_redo, "Merge Action 1", test_object, 10, UndoRedo::MERGE_ALL);
  107. CHECK(test_object->get_property() == 10);
  108. CHECK(undo_redo->get_version() == 2);
  109. CHECK(undo_redo->get_history_count() == 1);
  110. set_property_action(undo_redo, "Merge Action 1", test_object, 100, UndoRedo::MERGE_ALL);
  111. CHECK(test_object->get_property() == 100);
  112. CHECK(undo_redo->get_version() == 2);
  113. CHECK(undo_redo->get_history_count() == 1);
  114. set_property_action(undo_redo, "Merge Action 1", test_object, 1000, UndoRedo::MERGE_ALL);
  115. CHECK(test_object->get_property() == 1000);
  116. CHECK(undo_redo->get_version() == 2);
  117. CHECK(undo_redo->get_history_count() == 1);
  118. memdelete(test_object);
  119. memdelete(undo_redo);
  120. }
  121. TEST_CASE("[UndoRedo] Merge Method UndoRedo") {
  122. GDREGISTER_CLASS(_TestUndoRedoObject);
  123. UndoRedo *undo_redo = memnew(UndoRedo());
  124. _TestUndoRedoObject *test_object = memnew(_TestUndoRedoObject());
  125. CHECK(test_object->get_property() == 0);
  126. CHECK(undo_redo->get_version() == 1);
  127. CHECK(undo_redo->get_history_count() == 0);
  128. increment_property_action(undo_redo, "Merge Increment 1", test_object, 10, UndoRedo::MERGE_ALL);
  129. CHECK(test_object->get_property() == 10);
  130. CHECK(undo_redo->get_version() == 2);
  131. CHECK(undo_redo->get_history_count() == 1);
  132. increment_property_action(undo_redo, "Merge Increment 1", test_object, 10, UndoRedo::MERGE_ALL);
  133. CHECK(test_object->get_property() == 20);
  134. CHECK(undo_redo->get_version() == 2);
  135. CHECK(undo_redo->get_history_count() == 1);
  136. increment_property_action(undo_redo, "Merge Increment 1", test_object, 10, UndoRedo::MERGE_ALL);
  137. CHECK(test_object->get_property() == 30);
  138. CHECK(undo_redo->get_version() == 2);
  139. CHECK(undo_redo->get_history_count() == 1);
  140. undo_redo->undo();
  141. CHECK(test_object->get_property() == 0);
  142. CHECK(undo_redo->get_version() == 1);
  143. CHECK(undo_redo->get_history_count() == 1);
  144. undo_redo->redo();
  145. CHECK(test_object->get_property() == 30);
  146. CHECK(undo_redo->get_version() == 2);
  147. CHECK(undo_redo->get_history_count() == 1);
  148. memdelete(test_object);
  149. memdelete(undo_redo);
  150. }
  151. } //namespace TestUndoRedo
  152. #endif // TEST_UNDO_REDO_H