Files
chandra/compositor/host/touch_test.mbt
T
nandiandClaude Opus 5.5 7029c92e6e
Deploy to the handset / build-and-deploy (push) Successful in 2m21s
CI / test (push) Failing after 2m49s
Launcher: a thumb scrolls the app list
On a touchscreen a drag over the launcher's list scrolls it by pixels, a flick
keeps it going, and a tap still launches (or stops a moving list).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 22:40:32 -08:00

208 lines
7.6 KiB
MoonBit

///| Touch (touch.mbt): wl_touch for fingers on an app's content, the pointer everywhere else.
///|
/// A 540x1170 handset with one window, whose client has the pointer and (with `touch`) wl_touch.
fn touch_setup(tag : String, touch : Bool) -> (@host.ChandraCompositor, TestWaylandClient, UInt, UInt) raise {
let path = "/tmp/wayland-moon-touch-" + tag + "-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(path, @scene.ThemeTokens::moon_night(), 540, 1170).unwrap()
comp.handle_input(Touch(Present), 1U)
let (c, shm, surf, xdg, _, _) = open_and_configure_client(comp, path, "T", "t")
commit_shm_frame(comp, c, shm, surf, xdg, 1UL)
let seat = bind_global(c, 2U, "wl_seat", 7U)
let get = fn(op : UInt) {
let id = c.alloc_id()
let w = @wayland.WireWriter::new()
w.u32(id)
c.send(w.finish_msg(seat, op))
id
}
let ptr = get(0U)
let t = if touch { get(2U) } else { 0U }
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
ignore(drain_msgs(c))
(comp, c, ptr, t)
}
///|
fn window_middle(comp : @host.ChandraCompositor) -> (Double, Double) {
let g = comp.windows.get(1UL).unwrap().geometry
((g.x + g.w / 2).to_double(), (g.y + g.h / 2).to_double())
}
///|
test "a finger on an app that listens goes to it as a touch, and no pointer is faked" {
let (comp, c, ptr, t) = touch_setup("client", true)
let (x, y) = window_middle(comp)
comp.handle_input(Touch(Down(0, x, y)), 10U)
comp.handle_input(Touch(Motion(0, x, y - 60.0)), 20U)
comp.handle_input(Touch(Down(1, x + 40.0, y)), 25U) // a second finger
comp.handle_input(Touch(Up(1)), 28U)
comp.handle_input(Touch(Up(0)), 30U)
let msgs = drain_msgs(c)
assert_eq(
msgs.iter().filter(m => m.0 == t).map(m => m.1).to_array(),
[0U, 3U, 2U, 3U, 0U, 3U, 1U, 3U, 1U, 3U], // down motion down up up, each with its frame
)
assert_false(msgs.iter().any(m => m.0 == ptr && m.1 != 1U))
// Holding still is the app's to time: no right click.
comp.handle_input(Touch(Down(0, x, y)), 40U)
@sys.sleep_ms(520)
comp.step()
let msgs = drain_msgs(c)
assert_false(msgs.iter().any(m => m.0 == ptr))
// The screen turning off (or rotating) cancels.
comp.handle_input(Touch(Cancel), 50U)
comp.handle_input(Touch(Up(0)), 51U)
let msgs = drain_msgs(c)
assert_true(has_msg(msgs, t, 4U))
assert_false(has_msg(msgs, t, 1U))
comp.close()
}
///|
test "an app that doesn't listen to touches gets the pointer: tap, drag and hold" {
let (comp, c, ptr, _) = touch_setup("pointer", false)
let (x, y) = window_middle(comp)
let buttons = fn() { drain_msgs(c).iter().filter(m => m.0 == ptr && m.1 == 3U).count() }
comp.handle_input(Touch(Down(0, x, y)), 10U)
assert_eq(buttons(), 0)
comp.handle_input(Touch(Up(0)), 20U)
assert_eq(buttons(), 2) // pressed and released
comp.handle_input(Touch(Down(0, x, y)), 30U)
comp.handle_input(Touch(Motion(0, x + 5.0, y)), 31U)
assert_eq(buttons(), 0)
comp.handle_input(Touch(Motion(0, x + 30.0, y)), 32U)
assert_eq(buttons(), 1)
comp.handle_input(Touch(Up(0)), 33U)
assert_eq(buttons(), 1)
comp.handle_input(Touch(Down(0, x, y)), 40U)
assert_true(comp.wake_timeout(5000) <= 500)
@sys.sleep_ms(520)
comp.step()
assert_eq(buttons(), 2) // the right button
comp.handle_input(Touch(Up(0)), 41U)
assert_eq(buttons(), 0)
comp.close()
}
///|
test "the panel takes a finger as before, and the seat says it has a touchscreen" {
let (comp, c, _, t) = touch_setup("panel", true)
comp.handle_input(Touch(Down(0, 20.0, 1160.0)), 10U)
comp.handle_input(Touch(Up(0)), 20U)
assert_false(drain_msgs(c).iter().any(m => m.0 == t))
let (out, ok) = comp.control_command("input-touch down 0 10")
assert_false(ok)
assert_true(out[0].has_prefix("error usage"))
comp.close()
}
///|
test "a window moved by its own title bar follows the finger that asked for it" {
let (comp, c, shm, surf, xdg, tl) = csd_setup("touchmove")
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let seat = bind_global(c, 2U, "wl_seat", 7U)
let t = c.alloc_id()
let w = @wayland.WireWriter::new()
w.u32(t)
c.send(w.finish_msg(seat, 2U))
for _i = 0; _i < 4; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
let before = comp.windows.get(1UL).unwrap().geometry
let r = comp.windows.get(1UL).unwrap().client_rect()
let (px, py) = ((r.x + r.w / 2).to_double(), (r.y + 15).to_double())
comp.handle_input(Touch(Down(0, px, py)), 10U)
ignore(drain_msgs(c))
let mv = @wayland.WireWriter::new()
mv.u32(0U)
mv.u32(1U)
c.send(mv.finish_msg(tl, 5U))
for _i = 0; _i < 4; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
assert_true(comp.wm.drag is Some(_))
assert_true(has_msg(drain_msgs(c), t, 4U)) // its touch is cancelled
comp.handle_input(Touch(Motion(0, px + 60.0, py + 40.0)), 12U)
comp.handle_input(Touch(Up(0)), 13U)
let after = comp.windows.get(1UL).unwrap().geometry
assert_eq((after.x - before.x, after.y - before.y), (60, 40))
assert_true(comp.wm.drag is None)
comp.close()
}
///|
test "a finger scrolls the launcher's list: a drag moves it, a flick keeps it going, a tap launches" {
let comp = osk_comp("touchlist", enable_osk=false)
load_handset_modules(comp)
let data = "/tmp/chandra-touch-apps-" + @sys.getpid().to_string()
ignore(@sys.mkdir_p(data + "/applications"))
for i in 0..<30 {
ignore(
@sys.write_file_bytes(
data + "/applications/touch" + i.to_string() + ".desktop",
@sys.str_to_utf8_bytes(
"[Desktop Entry]\nType=Application\nName=Touch " + i.to_string() + "\nExec=true\n",
),
),
)
}
comp.shell.xdg_dirs = { ..comp.shell.xdg_dirs, data_home: data, data_dirs: [] }
comp.shell.toggle_launcher()
comp.shell.toggle_launcher()
comp.shell.toggle_launcher()
for _ in 0..<10 {
@sys.sleep_ms(30)
ignore(comp.compose_frame())
}
let pr = comp.shell.slot_popup_rect(comp.shell.slot_of("launcher"))
let st = comp.shell.launcher_state
assert_eq(comp.shell.apps.length(), 30)
let x = (pr.x + pr.w - 120).to_double()
let y = (pr.y + 300).to_double()
// Past the threshold the list follows the finger by pixels.
comp.handle_input(Touch(Down(0, x, y)), 10U)
comp.handle_input(Touch(Motion(0, x, y - 10.0)), 20U)
assert_eq((st.scroll, st.offset), (0, 0))
comp.handle_input(Touch(Motion(0, x, y - 60.0)), 30U)
assert_eq(st.scroll * 46 + st.offset, 60)
comp.handle_input(Touch(Motion(0, x, y - 50.0)), 300U)
assert_eq(st.scroll * 46 + st.offset, 50)
// Held still, then up: it stays, and nothing was launched.
comp.handle_input(Touch(Motion(0, x, y - 50.0)), 900U)
comp.handle_input(Touch(Up(0)), 910U)
assert_true(st.open)
assert_eq(st.scroll * 46 + st.offset, 50)
// A flick goes on after the lift, and a finger stops it without launching.
comp.handle_input(Touch(Down(0, x, y)), 1000U)
comp.handle_input(Touch(Motion(0, x, y - 40.0)), 1020U)
comp.handle_input(Touch(Motion(0, x, y - 80.0)), 1040U)
comp.handle_input(Touch(Up(0)), 1045U)
let at = st.scroll * 46 + st.offset
assert_eq(at, 130)
assert_true(comp.has_timed_work())
@sys.sleep_ms(30)
comp.step()
assert_true(st.scroll * 46 + st.offset > at)
comp.handle_input(Touch(Down(0, x, y)), 1100U)
let at = st.scroll * 46 + st.offset
comp.handle_input(Touch(Up(0)), 1120U)
@sys.sleep_ms(30)
comp.step()
assert_eq(st.scroll * 46 + st.offset, at)
assert_true(st.open)
// Never past the ends.
comp.handle_input(Touch(Down(0, x, y)), 2000U)
comp.handle_input(Touch(Motion(0, x, y + 5000.0)), 2010U)
comp.handle_input(Touch(Motion(0, x, y + 5000.0)), 2900U)
comp.handle_input(Touch(Up(0)), 2910U)
assert_eq((st.scroll, st.offset), (0, 0))
comp.close()
}