TableTower/tests/test_sheet_editor.gd
RobinFiredrake 2661744b06 Sheets phase B: main menu, Sheets list, sheet editor
- Main menu (Spreads, Sheets, Settings, Quit) on the theme background,
  with recent spreads and sheets; logo or ☰ returns to it.
- Sheets: data model, store, templates, snapshot undo; New sheet dialog
  (blank or a template); editor with folder tabs, snap-grid canvas on the
  spread background, field palette, inspector, Play/Build, status strip.
- Field kinds: Number, Text box (BBCode, live {@name}), Heading.
- PaletteList shared by the card and field palettes; shared top bar brand.
- Keys go to the screen that shows; theme reloads keep the screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 15:53:56 -06:00

125 lines
5 KiB
GDScript

extends AppTestBase
## The main menu and the sheet editor in the app: switching screens, making a
## sheet, adding boxes from the field palette, moving them on the snap grid,
## undo, tabs, number fields, saving, and keys staying on their own screen.
## Run: tools/run_tests.sh sheet_editor
func run() -> void:
if not await start_main(false):
return
await _test_menu()
await _test_new_sheet()
await _test_boxes()
await _test_tabs()
await _test_keys()
finish()
func _test_menu() -> void:
var screens := main.screens
_check(screens.showing(MainScreens.SPREAD), "tests start on the spread")
main.top_bar.brand_title.get_parent().gui_input.emit(_click())
_check(screens.menu.visible and not screens.spread_root.visible, "clicking the logo opens the main menu")
var labels: Array = screens.menu.buttons.keys()
_eq(labels, ["Spreads", "Sheets", "Settings", "Quit"], "the menu lists Spreads first, titles only")
(screens.menu.buttons["Sheets"] as Button).pressed.emit()
_check(screens.sheets.visible and screens.sheets.list_page.visible, "Sheets opens the sheet list")
await _frames()
func _test_new_sheet() -> void:
var sheets := main.screens.sheets
sheets.open_new_sheet()
await _frames()
var dialog: NewSheetDialog = null
for c in main.get_children():
if c is NewSheetDialog:
dialog = c
_check(dialog != null, "+ New sheet asks for a name")
dialog.name_edit.text = "Rusty Mule"
dialog._create()
await _frames()
var ed := sheets.editor
_check(ed.visible and ed.doc.name == "Rusty Mule" and ed.building(), "a blank sheet opens in Build")
_eq(sheets.store.list().size(), 1, "it is saved")
func _test_boxes() -> void:
var ed := main.screens.sheets.editor
(ed.palette.list.find_child("Number", true, false) as Button).pressed.emit()
await _frames()
_eq((ed.current_tab()["boxes"] as Array).size(), 1, "clicking Number adds a box")
var box: SheetBox = ed.current_tab()["boxes"][0]
_eq(ed.selected, box.id, "the new box is selected")
var spot := ed.canvas.global_position + ed.canvas.size / 2.0 + Vector2(17, 9)
var want := ed.canvas.drop_point(spot)
_check(ed.palette.drop_at(ed.palette._add.bind(SheetKinds.TEXT), spot), "dropping on the canvas adds there")
var text_box: SheetBox = ed.current_tab()["boxes"][1]
_eq(Vector2(text_box.x, text_box.y), Vector2(SheetKinds.snap(want.x), SheetKinds.snap(want.y)), "a dropped box snaps to the grid")
var view: SheetBoxView = ed.canvas.views[box.id]
view.begin_drag("move")
view.drag_to(Vector2(37, 21), false)
view.end_drag()
_eq(Vector2(box.x, box.y).posmod(SheetKinds.GRID), Vector2.ZERO, "moving snaps to the grid")
var snapped := Vector2(box.x, box.y)
view = ed.canvas.views[box.id]
view.begin_drag("move")
view.drag_to(Vector2(5, 3), true)
view.end_drag()
_eq(Vector2(box.x, box.y), snapped + Vector2(5, 3), "Alt places it off the grid")
ed.undo()
box = ed.doc.find_box(box.id)
_eq(Vector2(box.x, box.y), snapped, "undo puts the box back")
ed.redo()
ed.set_mode(Main.Mode.PLAY)
await _frames()
_check(ed.palette.notice.visible and not ed.inspector.visible, "Play greys the palette and hides the inspector")
view = ed.canvas.views[box.id]
(view.find_child("Plus", true, false) as Button).pressed.emit()
_eq(int(ed.doc.find_box(box.id).value), 1, "+ adds one in Play")
ed.undo()
_eq(int(ed.doc.find_box(box.id).value), 0, "a value change can be undone")
ed.save_now()
var saved := main.screens.sheets.store.load_sheet(ed.doc.id)
_eq((saved.tabs[0]["boxes"] as Array).size(), 2, "the sheet's boxes are saved")
ed.set_mode(Main.Mode.BUILD)
func _test_tabs() -> void:
var ed := main.screens.sheets.editor
ed.tabs_bar.add_tab()
await _frames()
_check(ed.doc.tabs.size() == 2 and ed.tab_id == str(ed.doc.tabs[1]["id"]), "+ Tab adds a tab and opens it")
_eq(ed.canvas.views.size(), 0, "a new tab has its own empty canvas")
var second := str(ed.doc.tabs[1]["id"])
ed.tabs_bar.move_tab(second, -1)
_eq(str(ed.doc.tabs[0]["id"]), second, "Move left moves it")
ed.tabs_bar.delete_tab(second)
_eq(ed.doc.tabs.size(), 1, "an empty tab deletes without asking")
_check(ed.tabs_bar.menu_for(str(ed.doc.tabs[0]["id"]))[3]["disabled"], "the last tab can't be deleted")
ed.undo()
_eq(ed.doc.tabs.size(), 2, "undo brings the tab back")
func _test_keys() -> void:
var ed := main.screens.sheets.editor
var spread_label := main.undo_redo.edits.undo_label() if main.undo_redo.edits else ""
var key := InputEventKey.new()
key.keycode = KEY_Z
key.ctrl_pressed = true
key.pressed = true
var step := ed.history.undo_label()
main.layout._shortcut_input(key)
_eq(ed.history.redo_label(), step, "Ctrl+Z on the Sheets screen undoes the sheet")
_eq(main.undo_redo.edits.undo_label() if main.undo_redo.edits else "", spread_label, "and leaves the spread alone")
ed.screen.show_list()
_check(ed.screen.list_page.visible and ed.doc == null, "← Sheets closes the sheet")
main.screens.show(MainScreens.SPREAD)
func _click() -> InputEventMouseButton:
var mb := InputEventMouseButton.new()
mb.button_index = MOUSE_BUTTON_LEFT
mb.pressed = true
return mb