148 lines
5.6 KiB
Lua
148 lines
5.6 KiB
Lua
-- lib-core.panel-test v0.1.0 — 10 assertions
|
|
-- Exercises: registry lifecycle, theme, errors, pause-gate, context-menu dispatch.
|
|
-- Render paths are NOT tested here: engine.render.* is blocked in test-mode
|
|
-- (assert_in_render_phase guard in engine_lua_render.c). Input dispatch is
|
|
-- exercised via panel._dispatch_event_for_test(event).
|
|
|
|
local panel = require("lib-core.panel")
|
|
local T = engine.test
|
|
|
|
local M = {}
|
|
|
|
-- -----------------------------------------------------------------------
|
|
-- Shared dummy widget factories
|
|
-- -----------------------------------------------------------------------
|
|
|
|
local function make_widget(overrides)
|
|
local w = {
|
|
title = "Test Widget",
|
|
render = function(_ctx) end,
|
|
handle_input = function(_ctx, _event) end,
|
|
}
|
|
if overrides then
|
|
for k, v in pairs(overrides) do w[k] = v end
|
|
end
|
|
return w
|
|
end
|
|
|
|
-- -----------------------------------------------------------------------
|
|
-- Test suite
|
|
-- -----------------------------------------------------------------------
|
|
|
|
function M.run_tests(_ctx)
|
|
|
|
-- ------------------------------------------------------------------
|
|
-- 1. register + open / close round-trip
|
|
-- ------------------------------------------------------------------
|
|
panel.register("w1", make_widget())
|
|
panel.open("w1")
|
|
T.assert(panel.is_open(), "register + open makes is_open() true")
|
|
|
|
-- ------------------------------------------------------------------
|
|
-- 2. close after open works
|
|
-- ------------------------------------------------------------------
|
|
panel.close()
|
|
T.assert(not panel.is_open(), "close() after open makes is_open() false")
|
|
|
|
-- ------------------------------------------------------------------
|
|
-- 3. toggle twice → opens then closes
|
|
-- ------------------------------------------------------------------
|
|
panel.toggle("w1")
|
|
T.assert(panel.is_open(), "toggle (first) opens panel")
|
|
panel.toggle("w1")
|
|
T.assert(not panel.is_open(), "toggle (second) closes panel")
|
|
|
|
-- ------------------------------------------------------------------
|
|
-- 4. open("unknown_id") → loud-error
|
|
-- ------------------------------------------------------------------
|
|
local ok_unknown = pcall(panel.open, "does_not_exist")
|
|
T.assert(not ok_unknown,
|
|
"panel.open with unknown widget_id raises an error")
|
|
|
|
-- ------------------------------------------------------------------
|
|
-- 5. set_theme with valid override + get_theme reads merged
|
|
-- ------------------------------------------------------------------
|
|
panel.set_theme({ padding = 16 })
|
|
local t = panel.get_theme()
|
|
T.equals(t.padding, 16, "set_theme override is visible via get_theme")
|
|
|
|
-- Restore for subsequent tests
|
|
panel.set_theme({ padding = 8 })
|
|
|
|
-- ------------------------------------------------------------------
|
|
-- 6. set_theme with unknown key → loud-error
|
|
-- ------------------------------------------------------------------
|
|
local ok_bad_key = pcall(panel.set_theme, { nonexistent_key = 99 })
|
|
T.assert(not ok_bad_key,
|
|
"set_theme with unknown key raises an error")
|
|
|
|
-- ------------------------------------------------------------------
|
|
-- 7. register without render function → loud-error
|
|
-- ------------------------------------------------------------------
|
|
local ok_no_render = pcall(panel.register, "w_bad", {
|
|
title = "Bad",
|
|
handle_input = function() end,
|
|
-- render intentionally missing
|
|
})
|
|
T.assert(not ok_no_render,
|
|
"panel.register without render function raises an error")
|
|
|
|
-- ------------------------------------------------------------------
|
|
-- 8. pause_on_open=true widget makes is_pausing()=true; close → false
|
|
-- ------------------------------------------------------------------
|
|
panel.register("w_pause", make_widget({ pause_on_open = true }))
|
|
panel.open("w_pause")
|
|
T.assert(panel.is_pausing(),
|
|
"is_pausing() is true when active widget has pause_on_open=true")
|
|
panel.close()
|
|
T.assert(not panel.is_pausing(),
|
|
"is_pausing() is false after close()")
|
|
|
|
-- ------------------------------------------------------------------
|
|
-- 9. show_context_menu + simulated click on action row → callback invoked
|
|
-- ------------------------------------------------------------------
|
|
|
|
-- Need an active widget for context-menu dispatch path
|
|
panel.open("w1")
|
|
|
|
local callback_fired = false
|
|
|
|
-- The context-menu renders at a known position; compute the click coords:
|
|
-- show_context_menu is called at (100, 100). With padding=8, row_height=24:
|
|
-- row 1 spans y=100..(100+24). Use x=120, y=110 (inside row 1).
|
|
panel.show_context_menu(100, 100, {
|
|
{
|
|
label = "Do Thing",
|
|
callback = function(_info)
|
|
callback_fired = true
|
|
end,
|
|
},
|
|
})
|
|
|
|
-- Simulate a left-click inside the first action row
|
|
panel._dispatch_event_for_test({
|
|
kind = "click",
|
|
x = 120,
|
|
y = 110,
|
|
button = "left",
|
|
})
|
|
|
|
T.assert(callback_fired,
|
|
"context-menu action callback is invoked on click inside its row")
|
|
|
|
-- ------------------------------------------------------------------
|
|
-- 10. non-pause widget: is_pausing() = false
|
|
-- ------------------------------------------------------------------
|
|
panel.close() -- close w1 from test 9
|
|
panel.open("w1") -- w1 has no pause_on_open
|
|
T.assert(not panel.is_pausing(),
|
|
"is_pausing() is false for widget without pause_on_open")
|
|
|
|
-- Cleanup
|
|
panel.close()
|
|
panel.unregister("w1")
|
|
panel.unregister("w_pause")
|
|
end
|
|
|
|
return M
|