Files
Claude b4f7149c72
CI / test (push) Successful in 3m49s
Kiosk: --kiosk-hybrid keeps the desktop and locks only leaving the session
Panel, launcher, dock, desktop module and shortcuts stay; Log out and power
actions are refused while locked. Admin escape and chandra-ctl kiosk work as
in the strict kiosk.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hgvjzw8hHirxRsXSsE
2026-11-26 02:38:02 +00:00

210 lines
7.8 KiB
MoonBit

///| Kiosk mode (kiosk.mbt): the restart rules, the lockdown, the admin escape and chandra-ctl.
///|
test "kiosk restarts: first start, grace for the first window, quick restart, backoff" {
let k = @host.Kiosk::new("app")
let t0 = 1000000UL
// Nothing started yet: start now.
assert_true(k.should_start(t0, false))
k.started(t0)
// No window yet, within the grace: wait.
assert_false(k.should_start(t0 + 5000UL, false))
// The window shows, runs a while, then goes: started again right away.
assert_false(k.should_start(t0 + 6000UL, true))
assert_false(k.should_start(t0 + 60000UL, true))
assert_true(k.should_start(t0 + 61000UL, false))
assert_eq(k.fails, 0)
k.started(t0 + 61000UL)
// It dies within seconds, several times: 1 s, 2 s, 4 s between starts.
let mut t = t0 + 61000UL
for want in [1000UL, 2000UL, 4000UL] {
assert_false(k.should_start(t + 500UL, true))
assert_false(k.should_start(t + 1000UL, false)) // gone; the backoff starts here
assert_false(k.should_start(t + 1000UL + want - 1UL, false))
assert_true(k.should_start(t + 1000UL + want, false))
t = t + 1000UL + want
k.started(t)
}
assert_eq(k.starts, 5)
// Never shows a window: tried again after the grace (plus the backoff).
let k2 = @host.Kiosk::new("app")
assert_true(k2.should_start(t0, false))
k2.started(t0)
assert_false(k2.should_start(t0 + @host.kiosk_start_grace_ms - 1UL, false))
assert_true(k2.should_start(t0 + @host.kiosk_start_grace_ms, false))
// No command: never starts anything.
let k3 = @host.Kiosk::new("")
assert_false(k3.should_start(t0, false))
assert_eq(@host.kiosk_backoff_ms(0), 0UL)
assert_eq(@host.kiosk_backoff_ms(20), 30000UL)
}
///|
fn kiosk_press(comp : @host.ChandraCompositor, codes : Array[UInt], t : UInt) -> Unit {
for c in codes {
comp.handle_input(@nested.InputEvent::Key(c, true), t)
}
for i = codes.length() - 1; i >= 0; i = i - 1 {
comp.handle_input(@nested.InputEvent::Key(codes[i], false), t + 1U)
}
}
///| Ctrl+Alt+Shift+Escape, with the modifier state a backend reports alongside the keys.
fn kiosk_escape(comp : @host.ChandraCompositor, t : UInt) -> Unit {
for c in [29U, 56U, 42U] {
comp.handle_input(@nested.InputEvent::Key(c, true), t)
}
comp.handle_input(@nested.InputEvent::Modifiers(1U | 4U | 8U, 0U, 0U, 0U), t)
comp.handle_input(@nested.InputEvent::Key(1U, true), t)
comp.handle_input(@nested.InputEvent::Key(1U, false), t + 1U)
for c in [42U, 56U, 29U] {
comp.handle_input(@nested.InputEvent::Key(c, false), t + 1U)
}
comp.handle_input(@nested.InputEvent::Modifiers(0U, 0U, 0U, 0U), t + 1U)
}
///|
test "a locked kiosk: no panel, fullscreen windows, no shortcuts; the admin escape unlocks it" {
let socket_path = "/tmp/wayland-moon-kiosk-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
ignore(comp.compose_frame())
assert_true(comp.shell.panel_rect.h > 0) // a normal desktop has its panel
comp.start_kiosk("")
assert_true(comp.kiosk_locked())
let clients = open_windows(comp, socket_path, 2)
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
}
ignore(comp.compose_scene())
// No panel, and the focused window covers the whole screen, undecorated; the other waits behind.
assert_eq(comp.shell.panel_rect.h, 0)
assert_eq(comp.windows.size(), 2)
let full = fn() raise {
let out = []
for id, win in comp.windows {
if win.fullscreen {
assert_true(win.focused)
assert_eq((win.geometry.x, win.geometry.y, win.geometry.w, win.geometry.h), (0, 0, 1280, 720))
out.push(id)
}
}
out
}
let first = full()
assert_eq(first.length(), 1)
// Alt+Tab: the other window becomes the fullscreen one.
kiosk_press(comp, [56U, 15U], 5U)
comp.step()
let second = full()
assert_eq(second.length(), 1)
assert_true(second[0] != first[0])
// Super tap, Super+D, Print: nothing happens.
kiosk_press(comp, [125U], 10U)
assert_false(comp.shell.launcher_open())
assert_false(comp.overview_open())
kiosk_press(comp, [125U, 17U], 20U) // Super+W
assert_false(comp.overview_open())
// Ctrl+Alt+Shift+Escape: unlocked, the panel and the launcher are back, windows leave fullscreen.
kiosk_escape(comp, 30U)
assert_false(comp.kiosk_locked())
ignore(comp.compose_scene())
assert_true(comp.shell.panel_rect.h > 0)
comp.step()
for _, win in comp.windows {
assert_false(win.fullscreen)
}
kiosk_press(comp, [125U], 40U)
assert_true(comp.shell.launcher_open())
kiosk_press(comp, [1U], 50U) // Escape closes it
// And locked again.
kiosk_escape(comp, 60U)
assert_true(comp.kiosk_locked())
for _i = 0; _i < 3; _i = _i + 1 {
comp.step()
}
ignore(comp.compose_scene())
assert_eq(comp.shell.panel_rect.h, 0)
assert_false(comp.shell.launcher_open())
assert_eq(full().length(), 1)
ignore(clients)
comp.close()
}
///|
test "the kiosk starts its app, and again when its window is gone" {
let socket_path = "/tmp/wayland-moon-kiosk2-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
let out = "/tmp/chandra-kiosk-spawn-" + @sys.getpid().to_string()
comp.start_kiosk("sh -c 'echo started >> " + out + "'")
assert_true(comp.kiosk_waiting())
comp.step()
let mut text = ""
for _i = 0; _i < 100 && text.length() == 0; _i = _i + 1 {
@sys.sleep_ms(20)
text = @utf8.decode_lossy(@sys.read_file_bytes(out))
}
assert_eq(text, "started\n")
// It showed no window, but the grace isn't over: no second start.
comp.step()
@sys.sleep_ms(100)
assert_eq(@utf8.decode_lossy(@sys.read_file_bytes(out)), "started\n")
match comp.kiosk {
Some(k) => assert_eq(k.starts, 1)
None => fail("no kiosk")
}
ignore(@sys.spawn("rm -f " + out, ""))
comp.close()
}
///|
test "chandra-ctl kiosk: status, unlock, lock; an error outside kiosk mode" {
let (comp, path) = control_comp("kiosk")
assert_eq(ask(comp, path, "kiosk"), ["error not in kiosk mode (start with --kiosk)"])
comp.start_kiosk("")
assert_eq(ask(comp, path, "kiosk status"), ["ok locked starts=0 cmd="])
assert_eq(ask(comp, path, "kiosk unlock"), ["ok unlocked"])
assert_false(comp.kiosk_locked())
assert_eq(ask(comp, path, "kiosk lock"), ["ok locked"])
assert_true(comp.kiosk_locked())
assert_eq(ask(comp, path, "kiosk bogus"), ["error usage: kiosk [lock|unlock|status]"])
comp.close()
}
///|
test "a hybrid kiosk keeps the panel, the shortcuts and the layout; only leaving is refused" {
let socket_path = "/tmp/wayland-moon-kioskh-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
ignore(comp.compose_frame())
comp.start_kiosk("", hybrid=true)
assert_true(comp.kiosk_locked())
assert_false(comp.kiosk_strict())
let clients = open_windows(comp, socket_path, 2)
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
}
ignore(comp.compose_scene())
// The panel is there and windows are tiled as usual, not fullscreen.
assert_true(comp.shell.panel_rect.h > 0)
for _, win in comp.windows {
assert_false(win.fullscreen)
assert_true(win.geometry.w < 1280)
}
// The launcher opens with the Super tap.
kiosk_press(comp, [125U], 10U)
assert_true(comp.shell.launcher_open())
kiosk_press(comp, [1U], 12U)
// Log out is refused while locked, and works once the admin unlocked it.
comp.shell.actions.push(@shell.ShellAction::Logout)
comp.damage.mark_full()
ignore(comp.compose_scene())
assert_false(comp.quit_requested)
kiosk_escape(comp, 20U)
assert_false(comp.kiosk_locked())
comp.shell.actions.push(@shell.ShellAction::Logout)
comp.damage.mark_full()
ignore(comp.compose_scene())
assert_true(comp.quit_requested)
ignore(clients)
comp.close()
}