TableTower/tests/test_sheet_editor.gd

369 lines
17 KiB
GDScript3
Raw Normal View History

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()
await _test_kinds()
await _test_links()
await _test_overlap()
await _test_export()
await _test_text_tools()
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).posmod(SheetKinds.GRID), Vector2.ZERO, "a dropped box snaps to the grid")
_check(Vector2(text_box.x, text_box.y).distance_to(want) < 400, "near where it was dropped")
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")
await _frames()
var page_x := ed.canvas.area.position.x
view = ed.canvas.views[box.id]
view.begin_drag("move")
view.drag_to(Vector2(ed.canvas.size.x * 1.5, 0), false)
view.end_drag()
ed.canvas._fit_area()
_eq(ed.canvas.area.position.x, page_x, "moving a box far right doesn't shift the page")
_check(ed.canvas.holder.custom_minimum_size.x > box.x + box.w + ed.canvas.size.x * 0.9, "the canvas keeps a screen of room past the box")
ed.canvas.pan_to(Vector2(300, 0))
await _frames()
_eq(ed.canvas.scroll_horizontal, 300, "panning scrolls the canvas")
ed.undo()
box = ed.doc.find_box(box.id)
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)
## The example sheet: every kind draws in Play and Build; clicks and typing
## change values (and undo); lists add, remove and go over their pool; ⓘ
## opens the details panel; dice roll; a setting change redraws the box.
func _test_kinds() -> void:
var sheets := main.screens.sheets
main.screens.show(MainScreens.SHEETS)
var doc := SheetDemo.create("kinds-demo")
sheets.store.save(doc)
sheets.open_doc(doc)
var ed := sheets.editor
ed.set_mode(Main.Mode.PLAY)
for tab in ed.doc.tabs:
ed.show_tab(str(tab["id"]))
_eq(ed.canvas.views.size(), (tab["boxes"] as Array).size(), "every box on %s draws" % tab["name"])
ed.set_mode(Main.Mode.BUILD)
_eq(ed.canvas.views.size(), (ed.current_tab()["boxes"] as Array).size(), "and draws in Build")
ed.set_mode(Main.Mode.PLAY)
ed.show_tab(str(ed.doc.tabs[0]["id"]))
var stress := _box(ed, "Stress")
_press(ed, stress, "Mark4")
_eq(int(stress.value), 4, "clicking the 4th tick box marks four")
ed.undo()
_eq(int(_box(ed, "Stress").value), 2, "undo puts the ticks back")
var talents := _box(ed, "Talents")
_press(ed, talents, "Add")
_eq((_box(ed, "Talents").value as Array).size(), 4, "+ Add adds an entry")
var cost: LineEdit = _view(ed, "Talents").find_child("Row3", true, false).find_child("Cost", true, false)
cost.text = "9"
cost.text_changed.emit("9")
var pool: Label = _view(ed, "Talents").find_child("Pool", true, false)
_check(pool.text.contains("over by 7"), "the pool shows red past zero: " + pool.text)
_press(ed, _box(ed, "Talents"), "Details")
_check(ed.details.visible and ed.details.index == 0, "ⓘ opens the details panel")
var details: TextEdit = ed.details.find_child("DetailsText", true, false)
details.text = "New text"
details.text_changed.emit()
_eq(str((_box(ed, "Talents").value as Array)[0]["details"]), "New text", "typing in the panel saves the details")
var row: Control = _view(ed, "Talents").find_child("Row0", true, false)
_check(row.tooltip_text.begins_with("Shield Wall\nNew text"), "hovering an entry shows its details")
_press(ed, _box(ed, "Conditions"), "Poisoned")
_check((_box(ed, "Conditions").value as Array).has("Poisoned"), "a condition chip switches on")
_press(ed, _box(ed, "Longsword"), "Roll")
_check(str(_box(ed, "Longsword").value).begins_with("Rolled"), "the dice button rolls")
(_view(ed, "Strength").find_child("Plus", true, false) as Button).pressed.emit()
var shown: Label = _view(ed, "STR mod").find_child("Value", true, false)
_eq(shown.text, "2", "a formula follows the number it reads (15 → still 2)")
(_view(ed, "Strength").find_child("Plus", true, false) as Button).pressed.emit()
shown = _view(ed, "STR mod").find_child("Value", true, false)
_eq(shown.text, "3", "and changes when it should (16 → 3)")
ed.set_mode(Main.Mode.BUILD)
ed.select(talents.id)
var chips: Button = ed.inspector.find_child("chips", true, false)
chips.pressed.emit()
_eq(str(_box(ed, "Talents").config["style"]), "chips", "the inspector switches a list to chips")
_check(_view(ed, "Talents").find_child("Chip0", true, false) != null, "and the box shows chips")
sheets.show_list()
## A sheet on the spread: its node draws, a roll writes its boxes (not the
## locked one), undo takes the node away and back, double-click opens it.
func _test_links() -> void:
var sheets := main.screens.sheets
main.screens.show(MainScreens.SPREAD)
var doc := sheets.store.create("Linked")
var boxes: Array = doc.tabs[0]["boxes"]
for spec: Array in [["short_text", "Background", "background", ""], ["number", "HP", "hp", 10], ["number", "Luck", "luck", 3]]:
var b := SheetKinds.make_box(spec[0], doc.new_box_id(), Vector2(0, boxes.size() * 96))
b.title = spec[1]
b.var_name = spec[2]
b.value = spec[3]
b.locked = spec[1] == "Luck"
boxes.append(b)
sheets.store.save(doc)
main.set_mode(Main.Mode.BUILD)
var card := main.sheet_links.add_card(doc.id)
await _frames()
_check(card.kind == RollTable.KIND_SHEET and card.sheet_id == doc.id and card.name == "Linked", "placing a sheet adds a Sheet card for it")
_check(main.card_for(card.id).find_child("SheetPreview", true, false) != null, "the card shows the sheet's picture")
var t := main.editing.add_table()
t.entries.resize(1)
t.entries[0].effects = '@background = "Soldier"; @hp -= 4; @luck = 0'
t.entries[0].pipes = [card.id]
main.set_mode(Main.Mode.PLAY)
await main.rolling.start_roll(t.id, true)
await _frames()
var after := sheets.store.load_sheet(doc.id)
_eq(str(after.tabs[0]["boxes"][0].value), "Soldier", "a roll writes text into the sheet")
_eq(int(after.tabs[0]["boxes"][1].value), 6, "and numbers (HP 10 - 4)")
_eq(int(after.tabs[0]["boxes"][2].value), 3, "a locked box keeps its value")
_eq(int(main.persistent_vars["luck"]["value"]), 3, "and its variable goes back too")
_check((main.sheet_links.recent.get(doc.id, {}) as Dictionary).has(boxes[0].id), "the changed boxes are marked for a highlight")
var headline := TrayText.sheet_step(main.current.get("all", []))
_check(str(headline.get("text", "")).contains("Soldier"), "a thread into the card ends on the sheet text: %s / %s" % [str(headline.get("text", "")).left(120), str((main.current.get("all", []) as Array).size())])
main.sheet_links.open(card.id)
var ed := sheets.editor
_check(main.screens.showing(MainScreens.SHEETS) and ed.doc.id == doc.id and ed.top_bar.back_button.visible, "opening the card shows the sheet with a way back")
_check(not ed.building(), "in the spread's mode (Play)")
ed.back_to_spread()
_check(main.screens.showing(MainScreens.SPREAD), "← goes back to the spread")
main.set_mode(Main.Mode.BUILD)
var old := RollTable.create(RollTable.KIND_SHEET, TowerBoard.new_id("t"), "Hero")
old.entries[0].text = "[b]Hero[/b] {@background}"
main.data.tables.append(old)
main._board_changed()
await _frames()
_check(old.sheet_id != "", "an old template Sheet card gets a sheet file")
var made := sheets.store.load_sheet(old.sheet_id)
_eq(str(made.tabs[0]["boxes"][0].value), "[b]Hero[/b] {@background}", "holding its template in a text box")
## Boxes don't overlap: a new one lands on a free spot; a drag stops before
## covering another; Alt places it anywhere.
func _test_overlap() -> void:
var sheets := main.screens.sheets
main.screens.show(MainScreens.SHEETS)
sheets.open_doc(sheets.store.create("Tidy"))
var ed := sheets.editor
ed.set_mode(Main.Mode.BUILD)
var a := ed.add_box("number", Vector2(32, 32))
var b := ed.add_box("number", Vector2(32, 32))
_check(not a.rect().intersects(b.rect().grow(-0.5)), "a second box dropped on the first lands beside it")
var view: SheetBoxView = ed.canvas.views[b.id]
view.begin_drag("move")
view.drag_to(a.rect().position - b.rect().position, false)
view.end_drag()
_check(not a.rect().intersects(b.rect().grow(-0.5)), "dragging onto another box stops short")
view = ed.canvas.views[b.id]
view.begin_drag("move")
view.drag_to(a.rect().position - b.rect().position, true)
view.end_drag()
_eq(b.rect().position, a.rect().position, "Alt lets it overlap")
sheets.show_list()
func _box(ed: SheetEditor, title: String) -> SheetBox:
for tab in ed.doc.tabs:
for b: SheetBox in tab["boxes"]:
if b.title == title:
return b
return null
func _view(ed: SheetEditor, title: String) -> SheetBoxView:
return ed.canvas.views.get(_box(ed, title).id)
func _press(ed: SheetEditor, box: SheetBox, child: String) -> void:
var b: Button = (ed.canvas.views[box.id] as SheetBoxView).find_child(child, true, false)
b.pressed.emit()
func _click() -> InputEventMouseButton:
var mb := InputEventMouseButton.new()
mb.button_index = MOUSE_BUTTON_LEFT
mb.pressed = true
return mb
func _test_export() -> void:
var sheets := main.screens.sheets
var doc := SheetDemo.create("export-demo")
sheets.store.save(doc)
sheets.open_doc(doc)
var ed := sheets.editor
ed.set_mode(Main.Mode.PLAY)
var bar: SheetTopBar = ed.top_bar
_check(bar.export_button.is_visible_in_tree(), "Export ▾ shows in Play")
var popup := bar.export_button.get_popup()
popup.about_to_popup.emit()
_eq(range(popup.item_count).map(func(i: int) -> String: return popup.get_item_text(i)), SheetExportMenu.ITEMS, "Export ▾ offers share file, print page and PNG")
var page := SheetExportMenu.page_html(ed)
_check(page.contains(doc.name) and page.count("<section>") == doc.tabs.size(), "the print page has the sheet, one section per tab")
_check(SheetExportMenu.capture(ed.canvas) == null, "headless runs have no picture to take")
sheets.show_list()
_check(sheets.list_page.find_child("ImportSheet", true, false) != null, "the Sheets list can import a sheet file")
sheets.open_new_sheet()
await _frames()
for c in main.get_children():
if c is NewSheetDialog:
_check(c.footer.find_child("Import", false, false) != null, "the New sheet dialog can import too")
c.close()
## A text box being edited shows the formatting toolbar; B wraps the
## selection; "Link a Lore Entry…" links it; the link shows as a reference,
## hovers an excerpt and opens the entry in the side panel.
func _test_text_tools() -> void:
var sheets := main.screens.sheets
var page := main.data.add_page("Abbey of Vel", "Ruined. The monks left in a hurry, and something stayed behind in the crypt. The bell still rings at night, though no rope hangs from it and no one climbs the tower.")
var doc := sheets.store.create("Notes")
var note := SheetKinds.make_box(SheetKinds.TEXT, doc.new_box_id(), Vector2(0, 0))
note.value = "Go to the abbey"
doc.tabs[0]["boxes"].append(note)
sheets.store.save(doc)
sheets.open_doc(doc)
var ed := sheets.editor
ed.set_mode(Main.Mode.PLAY)
var view: SheetBoxView = ed.canvas.views[note.id]
var shown: RichTextLabel = view.find_child("Shown", true, false)
shown.gui_input.emit(_click())
var toolbar: SheetTextToolbar = view.find_child("Toolbar", true, false)
_check(toolbar.is_visible_in_tree(), "editing a text box shows the toolbar")
var edit: TextEdit = view.find_child("Edit", true, false)
edit.select(0, 0, 0, 2)
(toolbar.find_child("Bold", false, false) as Button).pressed.emit()
_eq(str(note.value), "[b]Go[/b] to the abbey", "B wraps the selection in [b]")
edit.select(0, 17, 0, 22)
toolbar.pick_lore()
var picker: LoreLinkPicker = null
for child in main.get_children():
if child is LoreLinkPicker:
picker = child
_check(picker != null, "Link a Lore Entry… opens the picker")
picker.search.text = "abbey"
picker.fill()
(picker.find_child(page.id.validate_node_name(), true, false) as Button).pressed.emit()
note = ed.doc.find_box(note.id)
_eq(str(note.value), "[b]Go[/b] to the [url=lore:%s]abbey[/url]" % page.id, "picking an entry links the selected words")
shown = ed.canvas.views[note.id].find_child("Shown", true, false)
_check(shown.text.contains("[bgcolor=#%s]" % Style.REF_BG.to_html(false)), "the link shows in the reference colours")
_check(SheetLoreLinks.excerpt(page).ends_with("…"), "the hover excerpt is cut with …")
shown.meta_clicked.emit("lore:" + page.id)
_check(ed.details.visible and ed.details.lore_id == page.id, "clicking the link opens the side panel")
var title: Label = ed.details.find_child("Title", true, false)
_eq(title.text, "Abbey of Vel", "with the Lore Entry")
ed.details.close()
main.data.remove_page(page.id)
sheets.show_list()