Merge pull request #10009 from dotCooCoo/fix/panel-harness-off-window

Tests: panel harness clicks fail on controls outside the window
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-28 17:28:34 -08:00
commit 6355b468a5
1 file changed
+86 -14
+86 -14
View File
@@ -102,9 +102,33 @@ impl Panel {
self.look(name)
}
/// The fixture's window in the latest frame: the room, or 800 by 600 points where the room is smaller. Dialogs and
/// menus open anywhere in it; the panel is laid out in the room.
fn window(&self) -> Rect {
self.ctx().content_rect()
}
/// `target`, the control named `name`, once its center is in the window: a pointer aimed at a center outside the
/// window reaches no control.
fn aim(&self, name: &str, target: Target) -> Target {
let window = self.window();
assert!(
window.contains(target.center()),
"{name:?} at {:?} has its center outside the window {window:?}, so a pointer aimed at it reaches no control",
target.rect
);
target
}
/// [`Panel::find`], then [`Panel::aim`].
fn find_aimed(&mut self, name: &str) -> Target {
let target = self.find(name);
self.aim(name, target)
}
/// Clicks the control named `name`.
pub(crate) fn click(&mut self, name: &str) {
let target = self.find(name);
let target = self.find_aimed(name);
self.fixture.click(&target);
self.after();
}
@@ -114,6 +138,7 @@ impl Panel {
let tree = self.fixture.tree();
let item = self.fixture.by_role(craft::ui::contract::Role::MENU_ITEM).into_iter().find(|t| tree.name_of(t.node) == Some(name));
let item = item.unwrap_or_else(|| panic!("no menu item {name:?} in {:?}", self.names()));
let item = self.aim(name, item);
self.fixture.click(&item);
self.after();
}
@@ -135,7 +160,7 @@ impl Panel {
/// [`Panel::click_role`] with `mods` held.
pub(crate) fn click_role_with(&mut self, name: &str, role: craft::ui::contract::Role, mods: Modifiers) {
self.frame();
let target = self.look_role(name, role);
let target = self.aim(name, self.look_role(name, role));
self.fixture.hold(mods).click(&target).hold(Modifiers::NONE);
self.after();
}
@@ -149,6 +174,7 @@ impl Panel {
self.fixture.by_role(role).into_iter().filter(|t| tree.name_of(t.node) == Some(name)).nth(index)
};
let target = target.unwrap_or_else(|| panic!("no {role:?} named {name:?} at {index} in {:?}", self.names()));
let target = self.aim(name, target);
self.fixture.click(&target);
self.after();
}
@@ -169,7 +195,7 @@ impl Panel {
/// Clicks the control named `name` with `mods` held.
pub(crate) fn click_with(&mut self, name: &str, mods: Modifiers) {
let target = self.find(name);
let target = self.find_aimed(name);
self.fixture.hold(mods).click(&target).hold(Modifiers::NONE);
self.after();
}
@@ -187,7 +213,7 @@ impl Panel {
/// Presses on the control named `name` and keeps the button down until [`Panel::release`].
pub(crate) fn press(&mut self, name: &str) {
let target = self.find(name);
let target = self.find_aimed(name);
self.fixture.press(&target);
self.after();
}
@@ -196,7 +222,7 @@ impl Panel {
/// the name (a slider beside its number box).
pub(crate) fn drag_hold_role(&mut self, name: &str, role: craft::ui::contract::Role, by: Vec2) {
self.frame();
let from = self.look_role(name, role).center();
let from = self.aim(name, self.look_role(name, role)).center();
self.drag_hold_between(from, from + by);
}
@@ -234,7 +260,7 @@ impl Panel {
.into_iter()
.flat_map(|role| self.fixture.by_role(role))
.find(|t| tree.name_of(t.node) == Some(name));
typed.unwrap_or_else(|| self.look(name))
self.aim(name, typed.unwrap_or_else(|| self.look(name)))
}
/// Types `text` into the focused field.
@@ -258,13 +284,13 @@ impl Panel {
/// Drags from the control named `from` to the one named `to`.
pub(crate) fn drag(&mut self, from: &str, to: &str, mods: Modifiers) {
let to = self.find(to).center();
let to = self.find_aimed(to).center();
self.drag_to(from, to, mods);
}
/// Drags from the control named `from` and releases at `to`.
pub(crate) fn drag_to(&mut self, from: &str, to: Pos2, mods: Modifiers) {
let from = self.find(from).center();
let from = self.find_aimed(from).center();
self.drag_between(from, to, mods);
}
@@ -278,7 +304,8 @@ impl Panel {
/// Clicks the pointer-only part `part` (a row's thumbnail, a step arrow) of the control named `name`.
pub(crate) fn click_part(&mut self, name: &str, part: impl Into<PartRef>, mods: Modifiers) {
let (target, part) = (self.find(name), part.into());
let at = part_rect_of_node(self.ctx(), target.node.into(), part).unwrap_or_else(|| panic!("{name:?} has no part {part:?}")).center();
let rect = part_rect_of_node(self.ctx(), target.node.into(), part).unwrap_or_else(|| panic!("{name:?} has no part {part:?}"));
let at = self.aim(&format!("{name}'s part {part:?}"), Target { node: target.node, rect }).center();
self.fixture.hold(mods).click_at(at).hold(Modifiers::NONE);
self.after();
}
@@ -291,21 +318,21 @@ impl Panel {
/// Two clicks on the control named `name`, close enough in time to be a double click.
pub(crate) fn double_click(&mut self, name: &str) {
let target = self.find(name);
let target = self.find_aimed(name);
self.fixture.click(&target).click(&target);
self.after();
}
/// A right click on the control named `name`.
pub(crate) fn secondary_click(&mut self, name: &str) {
let at = self.find(name).center();
let at = self.find_aimed(name).center();
self.secondary_click_at(at);
}
/// Right-clicks the zero-based control of `role`, including rows with identical names.
pub(crate) fn secondary_click_nth(&mut self, role: craft::ui::contract::Role, index: usize) {
self.frame();
let at = self.look_nth(role, index).center();
let at = self.aim(&format!("{role:?} at {index}"), self.look_nth(role, index)).center();
self.secondary_click_at(at);
}
@@ -337,9 +364,11 @@ impl Panel {
tree.keys().into_iter().any(|key| tree.role_of(key) == Some(Role::STATUS) && tree.name_of(key) == Some(text))
}
/// Whether some of the control named `name` can be seen in the latest frame.
/// Whether some of the control named `name` can be seen in the latest frame: inside its arena's clip and inside the
/// window.
pub(crate) fn in_view(&self, name: &str) -> bool {
self.fixture.visible(&self.look(name)).is_some()
let seen = self.fixture.visible(&self.look(name)).map(|t| t.rect.intersect(self.window()));
seen.is_some_and(|r| r.width() >= 0.0 && r.height() >= 0.0)
}
/// Whether the control named `name` takes input.
@@ -550,3 +579,46 @@ pub(crate) fn floated(mut app: VectorcraftApp, panel: &str, lang: &str, place: P
}
Shown { body, nodes, names }
}
#[cfg(test)]
mod tests {
use std::panic::{AssertUnwindSafe, catch_unwind};
use craft::ui::buttons::Button;
use craft::ui::contract::Show as _;
use egui::{Pos2, Rect, Ui, vec2};
use vectorcraft_engine::Session;
use super::{Panel, ROOM};
use crate::VectorcraftApp;
/// A button at the top of the room and one a room's height below it, past the bottom of the window.
fn show(_: &mut VectorcraftApp, ui: &mut Ui) {
Button::new("Above").show(ui);
ui.allocate_space(vec2(100.0, ROOM.y));
Button::new("Below").show(ui);
}
/// The message of the panic `drive` ends with, or `None` when it returns.
fn panic_of(drive: impl FnOnce()) -> Option<String> {
let payload = catch_unwind(AssertUnwindSafe(drive)).err()?;
payload.downcast_ref::<String>().cloned().or_else(|| payload.downcast_ref::<&str>().map(|s| (*s).to_owned()))
}
#[test]
fn a_control_below_the_window_is_out_of_view_and_cannot_be_clicked_or_pressed() {
let mut panel = Panel::new(VectorcraftApp::new(Session::new(), Default::default()), show);
// The fixture's window is as tall as the room and at least 800 points wide.
let window = Rect::from_min_size(Pos2::ZERO, ROOM.max(vec2(800.0, 600.0)));
let below = panel.rect("Below");
assert!(below.top() > window.bottom(), "the button is drawn below the window {window:?}: {below:?}");
assert!(panel.in_view("Above"), "the button at the top is in view");
assert!(!panel.in_view("Below"), "the button below the window is out of view: {below:?}");
assert_eq!(panic_of(|| panel.click("Above")), None, "the button at the top takes a click");
for (driver, message) in [("click", panic_of(|| panel.click("Below"))), ("press", panic_of(|| panel.press("Below")))] {
let message = message.unwrap_or_else(|| panic!("{driver} on the button below the window went ahead"));
let expected = format!("\"Below\" at {below:?} has its center outside the window {window:?}");
assert!(message.starts_with(&expected), "{driver} fails naming the control, its rect and the window: {message}");
}
}
}