Files
slate32/test/settings_calibration.lua
T
evan 393ce3c0ed refactor: split the Lua runtime, share test stubs, add a Makefile
lua_app.cpp had grown to 701 lines holding every binding, the module loader and the
app lifecycle, so new bindings landed wherever the cursor was. Each Lua table now has
its own file under bindings/, and the app is recovered from the lua_State's extra
space instead of a file-static, so a second state cannot reach the wrong app.

Three tests each redeclared the binding surface, which broke twice this session when
a binding changed; test/fake_device.lua is now the single stub. `make test` runs all
four suites and pins Lua 5.4, matching the vendored interpreter rather than the 5.2
the tests had silently been using.
2026-08-01 11:17:42 -04:00

82 lines
2.8 KiB
Lua

-- Run: lua test/settings_calibration.lua
-- Drives the settings app on a desktop Lua, through the shared fake device.
package.path = "sdcard/lib/?.lua;test/?.lua;" .. package.path
local device = require("fake_device").install()
dofile("sdcard/apps/settings/main.lua")
-- Menu rows: pad 12, gap 8, title 8 tall, buttons 24 tall.
local ROTATION_ROW = {x = 100, y = 70}
local CALIBRATE_ROW = {x = 100, y = 38}
local function tap(point)
on_touch_down(point.x, point.y)
on_touch_up(point.x, point.y)
end
-- A perfectly linear panel spanning raw 200..3800 over 320x480 must round-trip
-- to those same extremes from the two inset samples.
local inset, w, h = 30, 320, 480
local function raw(pixel, size) return 200 + (3800 - 200) * pixel / size end
local s1 = {x = raw(inset, w), y = raw(inset, h)}
local s2 = {x = raw(w - inset, w), y = raw(h - inset, h)}
local x0, y0, x1, y1 = computeCalibration(s1, s2, w, h, inset)
assert(math.abs(x0 - 200) <= 1, "x0 " .. x0)
assert(math.abs(y0 - 200) <= 1, "y0 " .. y0)
assert(math.abs(x1 - 3800) <= 1, "x1 " .. x1)
assert(math.abs(y1 - 3800) <= 1, "y1 " .. y1)
-- A flipped panel (raw decreasing with pixel) must yield a descending range.
local s3 = {x = 3800 - raw(inset, w) + 200, y = s1.y}
local s4 = {x = 3800 - raw(w - inset, w) + 200, y = s2.y}
local fx0, _, fx1 = computeCalibration(s3, s4, w, h, inset)
assert(fx0 > fx1, "flipped axis should descend")
-- Rotation cycles through the four quarter turns and wraps back to 0.
setup()
for _, expected in ipairs({90, 180, 270, 0}) do
tap(ROTATION_ROW)
assert(sys.getRotation() == expected, "rotation " .. sys.getRotation())
end
-- Calibration collects one sample per target and saves on the second release.
setup()
tap(CALIBRATE_ROW)
on_tick() -- release after the menu tap arms sampling
device.raw = {s1.x, s1.y}
on_tick()
device.raw = nil
on_tick()
device.raw = {s2.x, s2.y}
on_tick()
device.raw = nil
on_tick()
local saved = device.calibration
assert(saved, "calibration was not saved")
assert(math.abs(saved[1] - 200) <= 1, "saved x0 " .. saved[1])
-- Open networks connect directly from scan results.
setup()
device.networks = {{ssid = "qemu", rssi = -25, secure = false}}
tap{x = 100, y = 104} -- wifi
tap{x = 100, y = 56} -- scan
on_tick()
tap{x = 100, y = 38} -- qemu
assert(device.connected, "open network should connect without a keyboard")
assert(device.connected[1] == "qemu" and device.connected[2] == "", "open wifi connect")
-- Secure networks route through the keyboard and preserve typed punctuation.
setup()
device.networks = {{ssid = "secure", rssi = -40, secure = true}}
tap{x = 100, y = 104}
tap{x = 100, y = 56}
on_tick()
tap{x = 100, y = 38}
tap{x = 12, y = 80} -- q
tap{x = 200, y = 180} -- connect
assert(device.connected[1] == "secure" and device.connected[2] == "q", "secure wifi password")
print("ok")