TableTower/tests/test_inspector_list.gd
RobinFiredrake 7ea2bbf3a8 Speed: lazy format menus, one-call dashed threads, kept inspector sections, queued card rebuilds
Profiling showed the delays come from Godot's own calls, not GDScript:
- Right-click format menus are made on the first right-click, not for
  every text field at build time.
- Dashed reference threads draw all their dashes in one call.
- Inspector rebuilds keep the closed sections and only remake open ones.
- Card rows are styled before they join the card.
- A Play/Build switch rebuilds cards in view at once and the rest a few
  per frame (CardRefreshQueue); threads redraw only now and then meanwhile.

Adds tests/perf_actions.gd, a timing tool for everyday actions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 16:20:31 -06:00

146 lines
6.4 KiB
GDScript

extends AppTestBase
## The inspector's card page as one list (InspectorList): a section for the
## card and one per result; selecting on the spread opens only that section;
## sections opened by hand stay open and edits go to the section clicked in;
## a result's weight, range and position (InspectorRange).
## Run: tools/run_tests.sh inspector_list
var insp: Inspector
func run() -> void:
if not await start_main(false):
return
main.set_anchors_and_offsets_preset(Control.PRESET_TOP_LEFT)
main.size = Vector2(1440, 900)
main.set_mode(Main.Mode.BUILD)
insp = main.inspector
main.data.load_dict({"format_version": 2, "id": "list-test", "name": "List test", "tables": [
{"id": "work", "name": "Workday Job", "x": 40, "y": 40, "dice": "1d100", "ranged": true,
"range_edit": "weights", "entries": [
{"id": "a", "text": "Result 1", "range": [1, 5], "weight": 5},
{"id": "b", "text": "Bob", "range": [6, 80], "weight": 75},
{"id": "c", "text": "Joe", "range": [81, 87], "weight": 7},
{"id": "d", "text": "Kevin", "range": [88, 100], "weight": 13}]}]})
main._rebuild_cards()
main.clear_inspector()
await _frames(4)
await _test_sections()
await _test_open_by_hand()
await _test_closed_sections_stay()
await _test_position_and_weight()
await _test_lore_tab()
await _test_mode_switch_queue()
finish()
func _t() -> RollTable:
return main.data.get_table("work")
func _open_keys() -> Array:
var out: Array = []
for k: String in insp.section_parts:
if insp.section_parts[k].get("built", false):
out.append(k)
return out
func _test_sections() -> void:
main.select_result("work", "b")
await _frames()
_eq(insp.section_parts.keys(), ["card", "a", "b", "c", "d"], "a section for the card and each result")
_eq(_open_keys(), ["b"], "selecting a result opens only its section")
_eq((insp.fields["text"] as LineEdit).text, "Bob", "the open section edits that result")
main.select_card("work")
await _frames()
_eq(_open_keys(), ["card"], "selecting the card opens only the card section")
_check(insp.fields.has("name"), "the card's fields are active")
var card := main.card_for("work")
var panel := card.get_theme_stylebox("panel") as StyleBoxFlat
_check(card._inspected and panel.border_color == Style.SELECTED, "the inspected card glows in the Selected colour")
_check(card.find_children("*", "CardMoveHandle", true, false).size() == 1, "Build shows a move handle on the header")
func _test_open_by_hand() -> void:
main.select_result("work", "a")
await _frames()
InspectorList.toggle(insp, "c")
await _frames()
_eq(_open_keys(), ["a", "c"], "a section opened by hand joins the open one")
_eq(str(insp.selection.get("eid")), "c", "the section just opened is active")
InspectorList.activate(insp, "a")
insp.set_entry_text("First")
_eq(_t().find_entry("a").text, "First", "edits go to the section made active")
_eq(_t().find_entry("c").text, "Joe", "the other open section is untouched")
await _frames()
_eq(_open_keys(), ["a", "c"], "an edit keeps both sections open")
main.select_result("work", "d")
await _frames()
_eq(_open_keys(), ["d"], "a click on the spread closes the rest again")
## A rebuild keeps closed sections (and refreshes their titles) but not open ones.
func _test_closed_sections_stay() -> void:
main.select_result("work", "b")
await _frames()
var closed: Control = insp.section_boxes["c"]
var open: Control = insp.section_boxes["b"]
main.add_entry("work")
await _frames()
var added := _t().entries[-1].id
_eq(insp.section_parts.keys(), ["card", "a", "b", "c", "d", added], "a new result gets its section, last")
_check(insp.section_boxes["c"] == closed and insp.section_boxes["b"] != open, "closed sections stay, the open one is built again")
_eq(insp.section_list.get_children(), insp.section_boxes.values(), "the sections are in the results' order, with none left over")
_t().find_entry("c").text = "Joan"
insp.rebuild()
_eq((insp.section_parts["c"]["title"] as Label).text, "Joan", "a kept section shows its new title")
_check(insp.section_boxes["c"] == closed, "and is the same one")
main.delete_entry("work", added)
await _frames()
_eq(insp.section_list.get_children(), insp.section_boxes.values(), "a deleted result's section goes")
_t().find_entry("c").text = "Joe"
func _test_position_and_weight() -> void:
var t := _t()
InspectorRange.set_weight(insp, t, t.find_entry("a"), 10)
_eq(Vector2i(t.find_entry("b").range_lo, t.find_entry("b").range_hi), Vector2i(11, 85), "a weight edit moves the ranges after it")
InspectorRange.move_to(insp, t, t.find_entry("d"), 0)
_eq(t.entries[0].id, "d", "Position moves the result")
_eq(t.entries[0].range_lo, 1, "Weights lay the ranges out again after a move")
RangeWeights.set_mode(t, RangeWeights.RANGES)
InspectorRange.set_range(insp, t, t.find_entry("d"), 120, 125)
_eq(t.entries.back().id, "d", "auto-sort puts an edited range in roll order")
## In Build the Lore Index is a tab beside the inspector; in Play it opens alone.
func _test_lore_tab() -> void:
SideTabs.show_tab(main, SideTabs.LORE)
_check(main.book.visible and not insp.visible, "Build: the Lore Index tab replaces the inspector")
_check(main.book._tabs.visible, "Build: the Lore Index shows the tab row")
main.select_result("work", "a")
await _frames()
_check(insp.visible and not main.book.visible, "selecting on the spread goes back to the Inspector tab")
main.set_mode(Main.Mode.PLAY)
main.book.toggle()
_check(main.book.visible and not insp.visible and not main.book._tabs.visible, "Play: the Lore Index opens alone, with no tabs")
main.book.toggle()
main.set_mode(Main.Mode.BUILD)
_check(insp.visible, "back in Build the inspector shows")
## A mode switch rebuilds the cards in view now and the rest afterwards.
func _test_mode_switch_queue() -> void:
main.set_mode(Main.Mode.PLAY)
var far_card := main.editing.add_table(RollTable.KIND_ROLL, "", Vector2(9000, 9000))
await _frames()
main.set_mode(Main.Mode.BUILD)
_check(main.card_refresh.is_waiting(far_card.id) and not main.card_refresh.is_waiting("work"), "only the card out of view waits")
main.select_card(far_card.id)
_check(not main.card_refresh.is_waiting(far_card.id), "selecting a waiting card rebuilds it at once")
main.set_mode(Main.Mode.PLAY)
main.set_mode(Main.Mode.BUILD)
await _frames()
_check(not main.card_refresh.is_waiting(far_card.id), "a waiting card is rebuilt soon after")
_eq(main.card_for(far_card.id).edit_ui.text_fields.size(), far_card.entries.size(), "with Build's text fields")