feat: Vollständiges Undo/Redo mit Snapshot-Historie hinzufügen
- Undo/Redo für Segmente, Griffe, Richtung/Kante und Section-Änderungen - Snapshot-Historie mit 50 Einträgen, aktive Section wird wiederhergestellt - Strg+Z, Strg+Shift+Z und Strg+Y; Toolbar-Buttons zeigen den Zustand - Pan/Zoom bleiben bei Undo/Redo erhalten - Neue und geladene Kürläufe starten eine frische Historie - Tests auf 312 Assertions erweitert
This commit is contained in:
parent
1623fb2590
commit
976b0e98bf
11 changed files with 502 additions and 29 deletions
|
|
@ -26,6 +26,9 @@ $GODOT --headless --path rinkplanner --script res://tests/run_canvas_tests.gd
|
|||
|
||||
# Run whiteboard controller/scene tests
|
||||
$GODOT --headless --path rinkplanner --script res://tests/run_whiteboard_tests.gd
|
||||
|
||||
# Run undo/redo history tests
|
||||
$GODOT --headless --path rinkplanner --script res://tests/run_history_tests.gd
|
||||
```
|
||||
|
||||
Exit code `0` means all assertions passed. Failures are printed to stderr.
|
||||
|
|
|
|||
247
rinkplanner/tests/run_history_tests.gd
Normal file
247
rinkplanner/tests/run_history_tests.gd
Normal file
|
|
@ -0,0 +1,247 @@
|
|||
extends SceneTree
|
||||
|
||||
## Headless tests for the snapshot-based undo/redo history.
|
||||
##
|
||||
## Run with:
|
||||
## godot --headless --path rinkplanner --script res://tests/run_history_tests.gd
|
||||
|
||||
const WhiteboardScene := preload("res://scenes/whiteboard/whiteboard.tscn")
|
||||
|
||||
var _assertions: int = 0
|
||||
var _failures: PackedStringArray = PackedStringArray()
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
_run.call_deferred()
|
||||
|
||||
|
||||
func _run() -> void:
|
||||
var scene := WhiteboardScene.instantiate()
|
||||
var controller := scene as WhiteboardController
|
||||
if controller != null:
|
||||
controller.restore_autosave = false
|
||||
controller.autosave_enabled = false
|
||||
root.add_child(scene)
|
||||
await process_frame
|
||||
|
||||
_check(controller != null, "history scene has a controller")
|
||||
if controller == null:
|
||||
_finish()
|
||||
return
|
||||
|
||||
_check(controller.undo_button.disabled, "undo starts disabled")
|
||||
_check(controller.redo_button.disabled, "redo starts disabled")
|
||||
|
||||
var section := controller.get_active_section()
|
||||
controller.add_segment(
|
||||
Vector2(-10.0, 0.0),
|
||||
Vector2(0.0, 0.0),
|
||||
PathSegment.Direction.FORWARD,
|
||||
PathSegment.Edge.OUTSIDE
|
||||
)
|
||||
_check(section.segments.size() == 1, "segment was added")
|
||||
_check(not controller.undo_button.disabled, "undo is enabled after a change")
|
||||
_check(controller.redo_button.disabled, "redo stays disabled after a change")
|
||||
|
||||
_check(controller.undo(), "undo returns true")
|
||||
_check(
|
||||
controller.get_active_section().segments.is_empty(),
|
||||
"undo removes the added segment"
|
||||
)
|
||||
_check(controller.undo_button.disabled, "undo disables at history start")
|
||||
_check(not controller.redo_button.disabled, "redo is enabled after undo")
|
||||
|
||||
_check(controller.redo(), "redo returns true")
|
||||
_check(
|
||||
controller.get_active_section().segments.size() == 1,
|
||||
"redo restores the segment"
|
||||
)
|
||||
_check(controller.redo_button.disabled, "redo disables at history end")
|
||||
_check(not controller.undo_button.disabled, "undo is enabled again")
|
||||
|
||||
controller.undo_button.pressed.emit()
|
||||
_check(
|
||||
controller.get_active_section().segments.is_empty(),
|
||||
"undo button triggers undo"
|
||||
)
|
||||
controller.redo_button.pressed.emit()
|
||||
_check(
|
||||
controller.get_active_section().segments.size() == 1,
|
||||
"redo button triggers redo"
|
||||
)
|
||||
|
||||
var restored := controller.get_active_section().segments[0]
|
||||
controller.select_segment(restored.segment_id)
|
||||
controller.inside_button.pressed.emit()
|
||||
_check(
|
||||
restored.edge == PathSegment.Edge.INSIDE,
|
||||
"edge change is applied"
|
||||
)
|
||||
var origin_before := controller.rink_canvas.get_view_transform().origin_px
|
||||
_check(controller.undo(), "undo edge change")
|
||||
_check_vec(
|
||||
controller.rink_canvas.get_view_transform().origin_px,
|
||||
origin_before,
|
||||
"undo preserves the current pan/zoom view"
|
||||
)
|
||||
restored = controller.get_active_section().segments[0]
|
||||
_check(
|
||||
restored.edge == PathSegment.Edge.OUTSIDE,
|
||||
"undo restores the edge"
|
||||
)
|
||||
_check(controller.redo(), "redo edge change")
|
||||
restored = controller.get_active_section().segments[0]
|
||||
_check(
|
||||
restored.edge == PathSegment.Edge.INSIDE,
|
||||
"redo reapplies the edge"
|
||||
)
|
||||
|
||||
var first_section_id := controller.get_active_section().section_id
|
||||
var second := controller.add_section()
|
||||
_check(
|
||||
controller.get_routine().sections.size() == 2,
|
||||
"section was added"
|
||||
)
|
||||
_check(controller.undo(), "undo section add")
|
||||
_check(
|
||||
controller.get_routine().sections.size() == 1,
|
||||
"undo removes the added section"
|
||||
)
|
||||
_check(
|
||||
controller.get_active_section().section_id == first_section_id,
|
||||
"undo restores the previous active section"
|
||||
)
|
||||
_check(controller.redo(), "redo section add")
|
||||
_check(
|
||||
controller.get_routine().sections.size() == 2,
|
||||
"redo restores the section"
|
||||
)
|
||||
_check(
|
||||
controller.get_active_section().section_id == second.section_id,
|
||||
"redo restores the added section as active"
|
||||
)
|
||||
|
||||
controller.set_active_section(first_section_id)
|
||||
var old_title := controller.get_active_section().title
|
||||
controller.rename_section(first_section_id, "Eröffnung")
|
||||
_check(
|
||||
controller.get_active_section().title == "Eröffnung",
|
||||
"section rename is applied"
|
||||
)
|
||||
_check(controller.undo(), "undo section rename")
|
||||
_check(
|
||||
controller.get_active_section().title == old_title,
|
||||
"undo restores the section title"
|
||||
)
|
||||
_check(controller.redo(), "redo section rename")
|
||||
_check(
|
||||
controller.get_active_section().title == "Eröffnung",
|
||||
"redo reapplies the section title"
|
||||
)
|
||||
|
||||
var canvas := controller.rink_canvas
|
||||
var view := canvas.get_view_transform()
|
||||
var count_before_draw := controller.get_active_section().segments.size()
|
||||
controller.set_tool(WhiteboardController.Tool.LINE)
|
||||
_mouse_button(canvas, view.model_to_screen(Vector2(-15.0, 5.0)), true)
|
||||
_mouse_motion(canvas, view.model_to_screen(Vector2(-5.0, 5.0)))
|
||||
_mouse_button(canvas, view.model_to_screen(Vector2(-5.0, 5.0)), false)
|
||||
_check(
|
||||
controller.get_active_section().segments.size() == count_before_draw + 1,
|
||||
"drawing gesture adds a segment"
|
||||
)
|
||||
_check(controller.undo(), "undo drawing gesture")
|
||||
_check(
|
||||
controller.get_active_section().segments.size() == count_before_draw,
|
||||
"undo removes the drawn segment"
|
||||
)
|
||||
_check(controller.redo(), "redo drawing gesture")
|
||||
_check(
|
||||
controller.get_active_section().segments.size() == count_before_draw + 1,
|
||||
"redo restores the drawn segment"
|
||||
)
|
||||
|
||||
var arc := controller.add_segment(
|
||||
Vector2(0.0, -5.0),
|
||||
Vector2(10.0, -5.0),
|
||||
PathSegment.Direction.FORWARD,
|
||||
PathSegment.Edge.OUTSIDE,
|
||||
WhiteboardController.Tool.ARC_LEFT
|
||||
)
|
||||
controller.select_segment(arc.segment_id)
|
||||
var old_start := arc.curve.get_point_position(0)
|
||||
var new_start := Vector2(-3.0, -6.0)
|
||||
_mouse_button(canvas, view.model_to_screen(old_start), true)
|
||||
_mouse_motion(canvas, view.model_to_screen(new_start))
|
||||
_mouse_button(canvas, view.model_to_screen(new_start), false)
|
||||
var edited := controller.get_active_section().get_segment_by_id(arc.segment_id)
|
||||
_check(edited != null, "arc is still present after handle drag")
|
||||
if edited != null:
|
||||
_check_vec(
|
||||
edited.curve.get_point_position(0),
|
||||
new_start,
|
||||
"handle drag moves the arc start"
|
||||
)
|
||||
_check(controller.undo(), "undo handle drag")
|
||||
var undone := controller.get_active_section().get_segment_by_id(arc.segment_id)
|
||||
if undone != null:
|
||||
_check_vec(
|
||||
undone.curve.get_point_position(0),
|
||||
old_start,
|
||||
"undo restores the arc start"
|
||||
)
|
||||
_check(controller.redo(), "redo handle drag")
|
||||
var redone := controller.get_active_section().get_segment_by_id(arc.segment_id)
|
||||
if redone != null:
|
||||
_check_vec(
|
||||
redone.curve.get_point_position(0),
|
||||
new_start,
|
||||
"redo reapplies the arc start"
|
||||
)
|
||||
|
||||
controller.new_routine()
|
||||
_check(controller.undo_button.disabled, "new routine clears undo history")
|
||||
_check(controller.redo_button.disabled, "new routine clears redo history")
|
||||
|
||||
scene.queue_free()
|
||||
await process_frame
|
||||
_finish()
|
||||
|
||||
|
||||
func _mouse_button(canvas: RinkCanvas, position: Vector2, pressed: bool) -> void:
|
||||
var event := InputEventMouseButton.new()
|
||||
event.button_index = MOUSE_BUTTON_LEFT
|
||||
event.pressed = pressed
|
||||
event.position = position
|
||||
canvas._gui_input(event)
|
||||
|
||||
|
||||
func _mouse_motion(canvas: RinkCanvas, position: Vector2) -> void:
|
||||
var event := InputEventMouseMotion.new()
|
||||
event.position = position
|
||||
canvas._gui_input(event)
|
||||
|
||||
|
||||
func _check(condition: bool, message: String) -> void:
|
||||
_assertions += 1
|
||||
if not condition:
|
||||
_failures.append(message)
|
||||
|
||||
|
||||
func _check_vec(actual: Vector2, expected: Vector2, message: String) -> void:
|
||||
_check(
|
||||
actual.distance_to(expected) <= 0.001,
|
||||
"%s (expected %s, got %s)" % [message, expected, actual]
|
||||
)
|
||||
|
||||
|
||||
func _finish() -> void:
|
||||
if _failures.is_empty():
|
||||
print("PASS: %d assertions" % _assertions)
|
||||
quit(0)
|
||||
return
|
||||
|
||||
for failure: String in _failures:
|
||||
printerr("FAIL: %s" % failure)
|
||||
printerr("%d of %d assertions failed" % [_failures.size(), _assertions])
|
||||
quit(1)
|
||||
1
rinkplanner/tests/run_history_tests.gd.uid
Normal file
1
rinkplanner/tests/run_history_tests.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://b81wlgmomt3br
|
||||
|
|
@ -104,7 +104,9 @@ func _run() -> void:
|
|||
"status bar reports the segment count"
|
||||
)
|
||||
|
||||
controller.undo_last_segment()
|
||||
controller.undo()
|
||||
routine = controller.get_routine()
|
||||
section = controller.get_active_section()
|
||||
_check(
|
||||
section.segments.size() == 2,
|
||||
"undo removes the last segment"
|
||||
|
|
@ -120,7 +122,7 @@ func _run() -> void:
|
|||
PathSegment.Direction.FORWARD,
|
||||
PathSegment.Edge.OUTSIDE
|
||||
)
|
||||
_check(fourth.segment_id == "seg-004", "segment ids stay unique after undo")
|
||||
_check(fourth.segment_id == "seg-003", "segment ids are reused after undo")
|
||||
_check(routine.is_valid(), "routine stays valid after undo and redraw")
|
||||
|
||||
var canvas := controller.rink_canvas
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue