Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/object/test_undo_redo.h
10278 views
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
31
#pragma once
32
33
#include "core/object/undo_redo.h"
34
#include "tests/test_macros.h"
35
36
// Declared in global namespace because of GDCLASS macro warning (Windows):
37
// "Unqualified friend declaration referring to type outside of the nearest enclosing namespace
38
// is a Microsoft extension; add a nested name specifier".
39
class _TestUndoRedoObject : public Object {
40
GDCLASS(_TestUndoRedoObject, Object);
41
int property_value = 0;
42
43
protected:
44
static void _bind_methods() {
45
ClassDB::bind_method(D_METHOD("set_property", "property"), &_TestUndoRedoObject::set_property);
46
ClassDB::bind_method(D_METHOD("get_property"), &_TestUndoRedoObject::get_property);
47
ADD_PROPERTY(PropertyInfo(Variant::INT, "property"), "set_property", "get_property");
48
}
49
50
public:
51
void set_property(int value) { property_value = value; }
52
int get_property() const { return property_value; }
53
void add_to_property(int value) { property_value += value; }
54
void subtract_from_property(int value) { property_value -= value; }
55
};
56
57
namespace TestUndoRedo {
58
59
void set_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_property(test_object, "property", value);
62
undo_redo->add_undo_property(test_object, "property", test_object->get_property());
63
undo_redo->commit_action();
64
}
65
66
void increment_property_action(UndoRedo *undo_redo, const String &name, _TestUndoRedoObject *test_object, int value, UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE) {
67
undo_redo->create_action(name, merge_mode);
68
undo_redo->add_do_method(callable_mp(test_object, &_TestUndoRedoObject::add_to_property).bind(value));
69
undo_redo->add_undo_method(callable_mp(test_object, &_TestUndoRedoObject::subtract_from_property).bind(value));
70
undo_redo->commit_action();
71
}
72
73
TEST_CASE("[UndoRedo] Simple Property UndoRedo") {
74
GDREGISTER_CLASS(_TestUndoRedoObject);
75
UndoRedo *undo_redo = memnew(UndoRedo());
76
77
_TestUndoRedoObject *test_object = memnew(_TestUndoRedoObject());
78
79
CHECK(test_object->get_property() == 0);
80
CHECK(undo_redo->get_version() == 1);
81
CHECK(undo_redo->get_history_count() == 0);
82
83
set_property_action(undo_redo, "Set Property", test_object, 10);
84
85
CHECK(test_object->get_property() == 10);
86
CHECK(undo_redo->get_version() == 2);
87
CHECK(undo_redo->get_history_count() == 1);
88
89
undo_redo->undo();
90
91
CHECK(test_object->get_property() == 0);
92
CHECK(undo_redo->get_version() == 1);
93
CHECK(undo_redo->get_history_count() == 1);
94
95
undo_redo->redo();
96
97
CHECK(test_object->get_property() == 10);
98
CHECK(undo_redo->get_version() == 2);
99
CHECK(undo_redo->get_history_count() == 1);
100
101
set_property_action(undo_redo, "Set Property", test_object, 100);
102
103
CHECK(test_object->get_property() == 100);
104
CHECK(undo_redo->get_version() == 3);
105
CHECK(undo_redo->get_history_count() == 2);
106
107
set_property_action(undo_redo, "Set Property", test_object, 1000);
108
109
CHECK(test_object->get_property() == 1000);
110
CHECK(undo_redo->get_version() == 4);
111
CHECK(undo_redo->get_history_count() == 3);
112
113
undo_redo->undo();
114
115
CHECK(test_object->get_property() == 100);
116
CHECK(undo_redo->get_version() == 3);
117
CHECK(undo_redo->get_history_count() == 3);
118
119
memdelete(test_object);
120
memdelete(undo_redo);
121
}
122
123
TEST_CASE("[UndoRedo] Merge Property UndoRedo") {
124
GDREGISTER_CLASS(_TestUndoRedoObject);
125
UndoRedo *undo_redo = memnew(UndoRedo());
126
127
_TestUndoRedoObject *test_object = memnew(_TestUndoRedoObject());
128
129
CHECK(test_object->get_property() == 0);
130
CHECK(undo_redo->get_version() == 1);
131
CHECK(undo_redo->get_history_count() == 0);
132
133
set_property_action(undo_redo, "Merge Action 1", test_object, 10, UndoRedo::MERGE_ALL);
134
135
CHECK(test_object->get_property() == 10);
136
CHECK(undo_redo->get_version() == 2);
137
CHECK(undo_redo->get_history_count() == 1);
138
139
set_property_action(undo_redo, "Merge Action 1", test_object, 100, UndoRedo::MERGE_ALL);
140
141
CHECK(test_object->get_property() == 100);
142
CHECK(undo_redo->get_version() == 2);
143
CHECK(undo_redo->get_history_count() == 1);
144
145
set_property_action(undo_redo, "Merge Action 1", test_object, 1000, UndoRedo::MERGE_ALL);
146
147
CHECK(test_object->get_property() == 1000);
148
CHECK(undo_redo->get_version() == 2);
149
CHECK(undo_redo->get_history_count() == 1);
150
151
memdelete(test_object);
152
memdelete(undo_redo);
153
}
154
155
TEST_CASE("[UndoRedo] Merge Method UndoRedo") {
156
GDREGISTER_CLASS(_TestUndoRedoObject);
157
UndoRedo *undo_redo = memnew(UndoRedo());
158
159
_TestUndoRedoObject *test_object = memnew(_TestUndoRedoObject());
160
161
CHECK(test_object->get_property() == 0);
162
CHECK(undo_redo->get_version() == 1);
163
CHECK(undo_redo->get_history_count() == 0);
164
165
increment_property_action(undo_redo, "Merge Increment 1", test_object, 10, UndoRedo::MERGE_ALL);
166
167
CHECK(test_object->get_property() == 10);
168
CHECK(undo_redo->get_version() == 2);
169
CHECK(undo_redo->get_history_count() == 1);
170
171
increment_property_action(undo_redo, "Merge Increment 1", test_object, 10, UndoRedo::MERGE_ALL);
172
173
CHECK(test_object->get_property() == 20);
174
CHECK(undo_redo->get_version() == 2);
175
CHECK(undo_redo->get_history_count() == 1);
176
177
increment_property_action(undo_redo, "Merge Increment 1", test_object, 10, UndoRedo::MERGE_ALL);
178
179
CHECK(test_object->get_property() == 30);
180
CHECK(undo_redo->get_version() == 2);
181
CHECK(undo_redo->get_history_count() == 1);
182
183
undo_redo->undo();
184
185
CHECK(test_object->get_property() == 0);
186
CHECK(undo_redo->get_version() == 1);
187
CHECK(undo_redo->get_history_count() == 1);
188
189
undo_redo->redo();
190
191
CHECK(test_object->get_property() == 30);
192
CHECK(undo_redo->get_version() == 2);
193
CHECK(undo_redo->get_history_count() == 1);
194
195
memdelete(test_object);
196
memdelete(undo_redo);
197
}
198
199
} //namespace TestUndoRedo
200
201