Files
nandi c790f96e58
CI / test (push) Waiting to run
Lock: add session lock from power button and chandra-ctl lock
- Lock the session from the power button or `chandra-ctl lock`
- Unlock with a face or the password; the password always works
- Add compositor lock module with tests, and a polkit unlock policy (org.chandra.unlock)
- Document the lock flow in docs/lock.md and link it from the README
- Add generated interface bindings for the compositor and widget components
2026-11-29 03:24:46 -08:00

311 lines
9.9 KiB
MoonBit

///|
fn test_tokens() -> @theme.Tokens {
let c = fn(r : Int, g : Int, b : Int) -> @types.Color { { r: r.to_byte(), g: g.to_byte(), b: b.to_byte(), a: b'\xff' } }
{
name: "t",
palette: {
desktop_top: c(1, 2, 3),
desktop_bottom: c(4, 5, 6),
surface: c(30, 30, 40),
surface_hover: c(50, 50, 60),
surface_popup: c(20, 20, 30),
foreground: c(250, 250, 250),
foreground_muted: c(150, 150, 150),
accent: c(80, 120, 255),
accent_foreground: c(255, 255, 255),
border: c(90, 90, 90),
},
font_size: 14U,
radius: 6U,
spacing: 6U,
panel_height: 40U,
}
}
///|
fn load_fixture(name : String) -> Result[Plasmoid, String] {
match load("compositor/wasm/testdata/" + name, test_tokens()) {
Ok(p) => Ok(p)
Err(_) => load("testdata/" + name, test_tokens())
}
}
///|
test "a plasmoid component loads and renders through wasmer" {
let p = load_fixture("clock-digital.component.wasm").unwrap()
assert_eq(p.id, "org.chandra.clock.digital")
assert_true(p.pref_w > 0 && p.pref_h > 0)
let ops = p.render(p.pref_w, p.pref_h, test_tokens())
assert_true(ops.length() > 0)
// The digital clock draws its time as text, in the theme's foreground colour.
let mut texts = 0
for op in ops {
match op {
Text(t) => {
texts = texts + 1
assert_true(t.text.length() >= 4)
assert_true(t.style.color.r == b'\xfa' || t.style.color.r == b'\x96') // theme foreground / muted
}
_ => ()
}
}
assert_true(texts >= 1)
p.unload()
}
///|
test "a bare core module loads too, and junk is rejected" {
let p = load_fixture("clock-digital.core.wasm").unwrap()
assert_eq(p.id, "org.chandra.clock.digital")
p.unload()
assert_true(load("/nonexistent.wasm", test_tokens()) is Err(_))
}
///| Load a panel module built by scripts/build-modules.sh.
fn load_module(name : String) -> Plasmoid {
match load("build/components/" + name + ".wasm", test_tokens()) {
Ok(p) => p
Err(_) => load("../../build/components/" + name + ".wasm", test_tokens()).unwrap()
}
}
///| Like load_module, for a module that may not be built (e.g. moon-disc, which has no source
/// in the tree): None, with a skip notice, when it isn't in build/components.
fn load_optional_module(name : String) -> Plasmoid? {
match load("build/components/" + name + ".wasm", test_tokens()) {
Ok(p) => Some(p)
Err(_) =>
match load("../../build/components/" + name + ".wasm", test_tokens()) {
Ok(p) => Some(p)
Err(_) => {
println("skip: \{name}.wasm is not in build/components (no source in the tree)")
None
}
}
}
}
///|
fn texts_of(ops : Array[@types.DrawOp]) -> Array[String] {
let out : Array[String] = []
for op in ops {
match op {
Text(t) => out.push(t.text)
_ => ()
}
}
out
}
///|
fn drain() -> Array[HostRequest] {
let out : Array[HostRequest] = []
while true {
match next_request() {
Some(r) => out.push(r)
None => break
}
}
out
}
///|
test "a module's own settings (module-config) are listed and set through wasmer" {
let p = load_module("clock-digital")
let items = p.config_items()
// 12-hour by default: Time format, Show AM/PM, Show date, then the calendar's two settings.
assert_eq(items.length(), 5)
assert_eq(items[0], { key: "format", label: "Time format", choices: ["24-hour", "12-hour"], value: "12-hour" })
assert_eq(items[1].choices.length(), 0) // an on/off switch
assert_true(p.set_config("format", "24-hour"))
assert_false(p.set_config("format", "24-hour")) // unchanged
assert_false(p.set_config("bogus", "x"))
assert_eq(p.config_items()[0].value, "24-hour")
assert_eq(p.config_items().length(), 4) // no AM/PM switch in 24-hour mode
let mut ampm = false
for op in p.render(p.pref_w, p.pref_h, test_tokens()) {
match op {
Text(t) => if t.text.has_suffix("AM") || t.text.has_suffix("PM") { ampm = true }
_ => ()
}
}
assert_false(ampm)
p.unload()
// Modules without the export have no settings, and setting one is a no-op.
let l = load_module("layout")
assert_eq(l.config_items().length(), 0)
assert_false(l.set_config("format", "12-hour"))
l.unload()
}
///|
test "layout module is a tile/float toggle" {
let p = load_module("layout")
let t = Table::new()
for name in ["Tiling (BSP)", "Monocle", "Floating"] {
t.str(name)
t.end_row()
}
publish(Layouts, t)
publish_state(0, false)
let labels = texts_of(p.render(120, 34, test_tokens()))
assert_true(labels.contains("Tile") && labels.contains("Float"))
assert_true(p.popup() is None)
// Tiling -> floating.
assert_true(p.on_event(false, PointerDown(5, 5)))
assert_eq(drain(), [SetLayout(2)])
// Floating -> tiling.
publish_state(2, false)
assert_true(p.on_event(false, PointerDown(5, 5)))
assert_eq(drain(), [SetLayout(0)])
// Monocle -> floating.
publish_state(1, false)
assert_true(p.on_event(false, PointerDown(5, 5)))
assert_eq(drain(), [SetLayout(2)])
p.unload()
}
///|
test "launcher module reads the host catalog, filters on typed keys and launches" {
let p = load_module("launcher")
let t = Table::new()
for a in [("a.term", "Terminal"), ("b.files", "Files")] {
t.str(a.0)
t.str(a.1)
t.str("comment")
t.str("System")
t.str("T")
t.end_row()
}
publish(Apps, t)
assert_true(p.on_event(false, PointerDown(1, 1)))
assert_true(p.popup() is Some(_))
for k in ["f", "i", "l", "e", "s"] {
ignore(p.on_event(true, KeyDown(k)))
}
match p.popup() {
Some(ps) => assert_true(texts_of(ps.ops).contains("Files"))
None => fail("popup closed early")
}
assert_true(p.on_event(true, KeyDown("Enter")))
assert_eq(drain(), [Launch("b.files")])
assert_true(p.popup() is None)
p.unload()
}
///|
test "the settings module's Panel tab lists host modules and asks to load, move, unload and browse" {
let p = load_module("session-settings")
assert_true(p.imports().contains("modules"))
let av = Table::new()
for m in [
("moon-disc", "Moon Phase (disc)", "/m/moon-disc.wasm", 0),
("tasks", "Task Manager", "/m/tasks.wasm", 1),
("clock-digital", "Digital Clock", "/m/clock-digital.wasm", 1),
] {
av.str(m.0)
av.str(m.1)
av.str(m.2)
av.int(m.3)
av.end_row()
}
publish(Modules, av)
let li = Table::new()
for i in [("w1", "clock-digital", "Digital Clock"), ("w2", "tasks", "Task Manager")] {
li.str(i.0)
li.str(i.1)
li.str(i.2)
li.end_row()
}
publish(Instances, li)
assert_true(p.on_event(false, PointerDown(1, 1)))
assert_true(p.on_event(true, KeyDown("3"))) // the Panel tab
match p.popup() {
None => fail("popup should be open")
Some(ps) => {
let ts = texts_of(ps.ops)
assert_true(ts.contains("Moon Phase (disc)"))
assert_true(ts.contains("Clocks"))
// The task manager is in the panel and one is enough: it is not offered again. A clock
// can go in twice, so it is.
assert_eq(ts.filter(t => t == "Task Manager").length(), 1)
assert_eq(ts.filter(t => t == "Digital Clock").length(), 2)
assert_true(ts.contains("Add another"))
assert_false(ts.contains("x1"))
}
}
// Below the tab bar (40): heading (28), the two panel rows (30 each; in the 390-wide popup the
// buttons are up at x 292-316, down 318-342, remove 354-378), heading (28), "Clocks" (22), two Add rows (42 each), Browse.
ignore(p.on_event(true, PointerDown(20, 82))) // w1's row itself: nothing
ignore(p.on_event(true, PointerDown(304, 82))) // w1 is first: up does nothing
ignore(p.on_event(true, PointerDown(330, 82))) // down
ignore(p.on_event(true, PointerDown(366, 112))) // w2's remove
ignore(p.on_event(true, PointerDown(20, 190))) // add the moon
ignore(p.on_event(true, PointerDown(160, 285))) // browse
assert_eq(drain(), [
FaceUnlock(-1), // opening on the Session tab reads the face unlock status
MoveInstance("w1", 1),
UnloadInstance("w2"),
LoadModule("/m/moon-disc.wasm"),
Browse,
])
// Keys: Backspace removes the highlighted panel module, PageUp moves it.
ignore(p.on_event(true, PointerMove(20, 112)))
assert_true(p.on_event(true, KeyDown("PageUp")))
assert_true(p.on_event(true, KeyDown("Backspace")))
assert_eq(drain(), [MoveInstance("w2", -1), UnloadInstance("w1")])
p.unload()
}
///|
test "interface explorer module toggles through the inspector capability" {
let p = load_module("inspector")
assert_true(p.imports().contains("inspector"))
publish_state(0, false)
assert_true(p.on_event(false, PointerDown(3, 3)))
assert_eq(drain(), [SetInspector(true)])
p.unload()
}
///|
test "moon and digital clock modules render from the clock capability" {
if load_optional_module("moon-disc") is Some(m) {
assert_true(m.render(36, 36, test_tokens()).length() > 3)
m.unload()
}
let c = load_module("clock-digital")
let ts = texts_of(c.render(120, 36, test_tokens()))
assert_true(ts[0].has_suffix("AM") || ts[0].has_suffix("PM")) // 12-hour by default
c.unload()
}
///|
test "osk module toggles through the osk capability" {
let p = load_module("osk")
assert_true(p.imports().contains("osk"))
publish_osk(false)
assert_true(texts_of(p.render(44, 34, test_tokens())).contains("KB"))
assert_true(p.popup() is None)
assert_true(p.on_event(false, PointerDown(5, 5)))
assert_eq(drain(), [SetOskVisible(true)])
publish_osk(true)
assert_true(p.on_event(false, PointerDown(5, 5)))
assert_eq(drain(), [SetOskVisible(false)])
p.unload()
}
///|
test "rotate module cycles orientation through the rotation capability" {
let p = load_module("rotate")
assert_true(p.imports().contains("rotation"))
publish_rotation(0)
assert_true(texts_of(p.render(44, 34, test_tokens())).contains("Rot"))
assert_true(p.popup() is None)
assert_true(p.on_event(false, PointerDown(5, 5)))
assert_eq(drain(), [SetRotation(1)])
publish_rotation(3)
assert_true(p.on_event(false, PointerDown(5, 5)))
assert_eq(drain(), [SetRotation(0)])
p.unload()
}