Path: blob/master/tests/core/object/test_undo_redo.h
10278 views
/**************************************************************************/1/* test_undo_redo.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/object/undo_redo.h"33#include "tests/test_macros.h"3435// Declared in global namespace because of GDCLASS macro warning (Windows):36// "Unqualified friend declaration referring to type outside of the nearest enclosing namespace37// is a Microsoft extension; add a nested name specifier".38class _TestUndoRedoObject : public Object {39GDCLASS(_TestUndoRedoObject, Object);40int property_value = 0;4142protected:43static void _bind_methods() {44ClassDB::bind_method(D_METHOD("set_property", "property"), &_TestUndoRedoObject::set_property);45ClassDB::bind_method(D_METHOD("get_property"), &_TestUndoRedoObject::get_property);46ADD_PROPERTY(PropertyInfo(Variant::INT, "property"), "set_property", "get_property");47}4849public:50void set_property(int value) { property_value = value; }51int get_property() const { return property_value; }52void add_to_property(int value) { property_value += value; }53void subtract_from_property(int value) { property_value -= value; }54};5556namespace TestUndoRedo {5758void set_property_action(UndoRedo *undo_redo, const String &name, _TestUndoRedoObject *test_object, int value, UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE) {59undo_redo->create_action(name, merge_mode);60undo_redo->add_do_property(test_object, "property", value);61undo_redo->add_undo_property(test_object, "property", test_object->get_property());62undo_redo->commit_action();63}6465void increment_property_action(UndoRedo *undo_redo, const String &name, _TestUndoRedoObject *test_object, int value, UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE) {66undo_redo->create_action(name, merge_mode);67undo_redo->add_do_method(callable_mp(test_object, &_TestUndoRedoObject::add_to_property).bind(value));68undo_redo->add_undo_method(callable_mp(test_object, &_TestUndoRedoObject::subtract_from_property).bind(value));69undo_redo->commit_action();70}7172TEST_CASE("[UndoRedo] Simple Property UndoRedo") {73GDREGISTER_CLASS(_TestUndoRedoObject);74UndoRedo *undo_redo = memnew(UndoRedo());7576_TestUndoRedoObject *test_object = memnew(_TestUndoRedoObject());7778CHECK(test_object->get_property() == 0);79CHECK(undo_redo->get_version() == 1);80CHECK(undo_redo->get_history_count() == 0);8182set_property_action(undo_redo, "Set Property", test_object, 10);8384CHECK(test_object->get_property() == 10);85CHECK(undo_redo->get_version() == 2);86CHECK(undo_redo->get_history_count() == 1);8788undo_redo->undo();8990CHECK(test_object->get_property() == 0);91CHECK(undo_redo->get_version() == 1);92CHECK(undo_redo->get_history_count() == 1);9394undo_redo->redo();9596CHECK(test_object->get_property() == 10);97CHECK(undo_redo->get_version() == 2);98CHECK(undo_redo->get_history_count() == 1);99100set_property_action(undo_redo, "Set Property", test_object, 100);101102CHECK(test_object->get_property() == 100);103CHECK(undo_redo->get_version() == 3);104CHECK(undo_redo->get_history_count() == 2);105106set_property_action(undo_redo, "Set Property", test_object, 1000);107108CHECK(test_object->get_property() == 1000);109CHECK(undo_redo->get_version() == 4);110CHECK(undo_redo->get_history_count() == 3);111112undo_redo->undo();113114CHECK(test_object->get_property() == 100);115CHECK(undo_redo->get_version() == 3);116CHECK(undo_redo->get_history_count() == 3);117118memdelete(test_object);119memdelete(undo_redo);120}121122TEST_CASE("[UndoRedo] Merge Property UndoRedo") {123GDREGISTER_CLASS(_TestUndoRedoObject);124UndoRedo *undo_redo = memnew(UndoRedo());125126_TestUndoRedoObject *test_object = memnew(_TestUndoRedoObject());127128CHECK(test_object->get_property() == 0);129CHECK(undo_redo->get_version() == 1);130CHECK(undo_redo->get_history_count() == 0);131132set_property_action(undo_redo, "Merge Action 1", test_object, 10, UndoRedo::MERGE_ALL);133134CHECK(test_object->get_property() == 10);135CHECK(undo_redo->get_version() == 2);136CHECK(undo_redo->get_history_count() == 1);137138set_property_action(undo_redo, "Merge Action 1", test_object, 100, UndoRedo::MERGE_ALL);139140CHECK(test_object->get_property() == 100);141CHECK(undo_redo->get_version() == 2);142CHECK(undo_redo->get_history_count() == 1);143144set_property_action(undo_redo, "Merge Action 1", test_object, 1000, UndoRedo::MERGE_ALL);145146CHECK(test_object->get_property() == 1000);147CHECK(undo_redo->get_version() == 2);148CHECK(undo_redo->get_history_count() == 1);149150memdelete(test_object);151memdelete(undo_redo);152}153154TEST_CASE("[UndoRedo] Merge Method UndoRedo") {155GDREGISTER_CLASS(_TestUndoRedoObject);156UndoRedo *undo_redo = memnew(UndoRedo());157158_TestUndoRedoObject *test_object = memnew(_TestUndoRedoObject());159160CHECK(test_object->get_property() == 0);161CHECK(undo_redo->get_version() == 1);162CHECK(undo_redo->get_history_count() == 0);163164increment_property_action(undo_redo, "Merge Increment 1", test_object, 10, UndoRedo::MERGE_ALL);165166CHECK(test_object->get_property() == 10);167CHECK(undo_redo->get_version() == 2);168CHECK(undo_redo->get_history_count() == 1);169170increment_property_action(undo_redo, "Merge Increment 1", test_object, 10, UndoRedo::MERGE_ALL);171172CHECK(test_object->get_property() == 20);173CHECK(undo_redo->get_version() == 2);174CHECK(undo_redo->get_history_count() == 1);175176increment_property_action(undo_redo, "Merge Increment 1", test_object, 10, UndoRedo::MERGE_ALL);177178CHECK(test_object->get_property() == 30);179CHECK(undo_redo->get_version() == 2);180CHECK(undo_redo->get_history_count() == 1);181182undo_redo->undo();183184CHECK(test_object->get_property() == 0);185CHECK(undo_redo->get_version() == 1);186CHECK(undo_redo->get_history_count() == 1);187188undo_redo->redo();189190CHECK(test_object->get_property() == 30);191CHECK(undo_redo->get_version() == 2);192CHECK(undo_redo->get_history_count() == 1);193194memdelete(test_object);195memdelete(undo_redo);196}197198} //namespace TestUndoRedo199200201