Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
257 changes: 244 additions & 13 deletions crates/e2e-desktop-napi/src/actions/mod.rs

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion crates/e2e-desktop-napi/src/actions/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ pub(super) fn window_takes_point(target: &PointTarget, x: f64, y: f64) -> Outcom
Err(error) if error == ax::AX_CANNOT_COMPLETE => return Err(unread(error, "read the app's focused window")),
_ => None,
};
let holds = focused.as_ref().is_some_and(|focused| windows::holds_focus(focused, &window));
let holds = match &focused {
Some(focused) => super::holds(focused, &window, "read the app's focused window")?,
None => false,
};
let now = match focused.filter(|focused| *focused != window) {
None => Sheet::None,
Some(on) => Sheet::Key(on.sheet_key()),
Expand Down
29 changes: 19 additions & 10 deletions crates/e2e-desktop-napi/src/actions/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,30 @@ pub fn scroll_bar(element: &Element, vertical: bool) -> Option<Element> {
/// Scrolls a scroll area by moving its scroll bar, `pages` of its visible size.
pub(super) fn scroll(element: &Element, direction: &str, pages: f64) -> Outcome<()> {
let vertical = matches!(direction, "up" | "down");
// A node that scrolls itself (a scroll bar of its own, or the page action for the direction) is the one moved; one
// with neither, a list or table, scrolls through the scroll area that holds it. The enclosing pane comes last, so a
// swipe on a nested list never moves the pane around it.
let own = scroll_bar(element, vertical).is_some() || action_names(element, "scroll")?.iter().any(|name| name == page_action(direction));
// A node that scrolls itself along the axis is the one moved; one that does not, a list or table, scrolls through the
// scroll area that holds it. The enclosing pane comes last, so a swipe on a nested list never moves the pane around it.
let (_, role) = role_of(element, "scroll")?;
let own = scrolls_itself(role, scroll_bar(element, vertical).is_some(), &action_names(element, "scroll")?, direction);
let target = if own { None } else { enclosing_scroll_area(element, vertical) };
scroll_by(target.as_ref().unwrap_or(element), direction, pages).map_err(|failure| {
// An element that takes none of the ways to scroll: say so, not which attribute it lacks.
// An element that takes none of the ways to scroll: say so, not which attribute it lacks. Like a window with
// nothing to scroll, it is not actionable, not a capability the engine lacks.
if failure.code == codes::ACTION_NOT_SUPPORTED && failure.delivery == Delivery::NotDelivered {
Failure::refused(codes::ACTION_NOT_SUPPORTED, format!("nothing here scrolls {direction}"))
Failure::refused(codes::ACTION_FAILED, format!("nothing here scrolls {direction}"))
} else {
failure
}
})
}

/// Whether a node scrolls itself along `direction`, by the rule the snapshot offers `Scroll` with: a scroll bar for the
/// axis, or, for any role but a scroll area, the page action for the direction. SwiftUI gives every scroll area the page
/// actions, also one whose content fits or that scrolls only along the other axis, and the action then moves nothing
/// while it answers success.
pub(super) fn scrolls_itself(role: &str, bar: bool, actions: &[String], direction: &str) -> bool {
bar || (role != "scrollarea" && actions.iter().any(|name| name == page_action(direction)))
}

/// The nearest scroll area above `element`, a few levels up, with a scroll bar for the axis.
pub(super) fn enclosing_scroll_area(element: &Element, vertical: bool) -> Option<Element> {
let mut current = element.element("AXParent")?;
Expand Down Expand Up @@ -99,9 +108,9 @@ pub(super) fn scroll_by(element: &Element, direction: &str, pages: f64) -> Outco
return repeat(&bar, if forward { "AXIncrement" } else { "AXDecrement" }, ((pages * LINES_PER_PAGE).round() as usize).max(1));
}
let names = action_names(element, "scroll")?;
let action = page_action(direction);
if names.iter().any(|name| name == action) {
return repeat(element, action, (pages.round() as usize).max(1));
let (_, role) = role_of(element, "scroll")?;
if scrolls_itself(role, false, &names, direction) {
return repeat(element, page_action(direction), (pages.round() as usize).max(1));
}
Err(Failure::refused(codes::ACTION_NOT_SUPPORTED, format!("nothing here scrolls {direction}")))
Err(Failure::refused(codes::ACTION_FAILED, format!("nothing here scrolls {direction}")))
}
52 changes: 35 additions & 17 deletions crates/e2e-desktop-napi/src/actions/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,44 @@ pub(super) fn own_label(element: &Element) -> Result<Option<String>, AXError> {
/// The texts inside an element, in reading order, as the snapshot gathers a row's name from them.
pub(super) fn row_texts(element: &Element, depth: usize, texts: &mut Vec<String>) -> Result<(), AXError> {
// Every child is read, as the snapshot reads them: a label cut short could match a prefix of the one asked for.
let children = match element.elements("AXChildren", crate::snapshot::MAX_CHILDREN) {
Ok(children) => children,
Err(ax::AX_CANNOT_COMPLETE) => return Err(ax::AX_CANNOT_COMPLETE),
Err(_) => Vec::new(),
let children = |element: &Element| match element.elements("AXChildren", crate::snapshot::MAX_CHILDREN) {
Ok(children) => Ok(children),
Err(ax::AX_CANNOT_COMPLETE) => Err(ax::AX_CANNOT_COMPLETE),
Err(_) => Ok(Vec::new()),
};
let role = |element: &Element| match element.attribute("AXRole") {
Ok(value) => Ok(roles::role(value.text().unwrap_or_default(), None)),
Err(ax::AX_CANNOT_COMPLETE) => Err(ax::AX_CANNOT_COMPLETE),
Err(_) => Ok(roles::role("", None)),
};
if !children.is_empty() && depth >= ROW_TEXT_DEPTH {
gather_texts(element, depth, texts, &children, &role, &own_label)
}

/// The texts under `node`, gathered as the snapshot's walk gathers them for a name from content: every level is read,
/// a text's own children before the text itself, so a row holding "Invoice" and an untitled heading over "Paid" is
/// "Invoice Paid" here as it is in the snapshot. A scroll bar's parts are not read, as the snapshot reads none. A node
/// deeper than `ROW_TEXT_DEPTH` with children counts as a read the app did not answer.
pub(super) fn gather_texts<T>(
node: &T,
depth: usize,
texts: &mut Vec<String>,
children: &dyn Fn(&T) -> Result<Vec<T>, AXError>,
role: &dyn Fn(&T) -> Result<&'static str, AXError>,
label: &dyn Fn(&T) -> Result<Option<String>, AXError>,
) -> Result<(), AXError> {
let below = children(node)?;
if !below.is_empty() && depth >= ROW_TEXT_DEPTH {
return Err(ax::AX_CANNOT_COMPLETE);
}
for child in children {
let role = match child.attribute("AXRole") {
Ok(value) => value.text().unwrap_or_default().to_owned(),
Err(ax::AX_CANNOT_COMPLETE) => return Err(ax::AX_CANNOT_COMPLETE),
Err(_) => String::new(),
};
if roles::text(roles::role(&role, None)) {
if let Some(text) = own_label(&child)? {
texts.push(text);
}
} else {
row_texts(&child, depth + 1, texts)?;
for child in below {
let kind = role(&child)?;
if kind != "scrollbar" {
gather_texts(&child, depth + 1, texts, children, role, label)?;
}
if roles::text(kind)
&& let Some(text) = label(&child)?
{
texts.push(text);
}
}
Ok(())
Expand Down
38 changes: 26 additions & 12 deletions crates/e2e-desktop-napi/src/actions/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ pub(super) fn type_text(entry: &Entry, text: &str, physical: bool, deadline: Ins
// window, a sheet on this one included, or another app must not take the rest of the text with it. A field in a sheet
// that was there from the start keeps its sheet. Read before the field's own check, so a window a focus handler opens
// after it shows as a change at the first key, and one it opened before fails that check.
let typing_in = Element::application(entry.pid).element("AXFocusedWindow");
if typing_in.as_ref().is_some_and(|start| !windows::holds_focus(start, &window)) {
let typing_in = element_attribute(&Element::application(entry.pid), "AXFocusedWindow", "type")?;
if let Some(start) = &typing_in
&& !holds(start, &window, "type")?
{
return Err(Failure::refused(codes::ACTION_FAILED, "another window took the keyboard before typing began, so nothing was typed"));
}
// Coming forward can run a handler that opens a sheet or another window, and the app can move focus on: the keys
// would then go there. The field itself must hold the app's focus before the first key.
if Element::application(entry.pid).element("AXFocusedUIElement").as_ref() != Some(&entry.element) {
if element_attribute(&Element::application(entry.pid), "AXFocusedUIElement", "type")?.as_ref() != Some(&entry.element) {
return Err(Failure::refused(codes::ACTION_FAILED, "the field lost keyboard focus before typing began, so nothing was typed"));
}
// What an element holds, in characters, as the app answers it: a change says the app took the key sent last.
Expand All @@ -44,6 +46,8 @@ pub(super) fn type_text(entry: &Entry, text: &str, physical: bool, deadline: Ins
_ => None,
};
let focused_element = || Element::application(entry.pid).element("AXFocusedUIElement");
// The same read for a check a key waits on: a stall fails it, as a retry may get past it, never as focus that moved.
let focused_checked = || element_attribute(&Element::application(entry.pid), "AXFocusedUIElement", "type");
// The element that held the keyboard when the last key went, and its length then; unset before the first key. It is
// the field typing began in until a Tab, or a code field's next box, takes the keys within the window.
let sent_to: std::cell::RefCell<Option<(Option<Element>, Option<f64>)>> = std::cell::RefCell::new(None);
Expand Down Expand Up @@ -76,23 +80,33 @@ pub(super) fn type_text(entry: &Entry, text: &str, physical: bool, deadline: Ins
}
// The element the key will reach, read before the checks below, so nothing reads the app between them and the
// key: focus that moves while they run is caught by them, not followed. The first key goes only to the field.
let target = focused_element();
// Past the first key, an unanswered read only shortens the wait for the next key's effect.
let target = if last.is_none() { focused_checked()? } else { focused_element() };
let count = target.as_ref().and_then(&length);
if last.is_none() && target.as_ref() != Some(&entry.element) {
return Err(Failure::refused(codes::ACTION_FAILED, "the field lost keyboard focus before typing began, so nothing was typed"));
}
let app = Element::application(entry.pid);
let focused = app.element("AXFocusedWindow");
let focused = element_attribute(&Element::application(entry.pid), "AXFocusedWindow", "type")?;
let held = match &typing_in {
Some(start) => focused.as_ref() == Some(start),
None => focused.is_some_and(|focused| windows::holds_focus(&focused, &window)),
None => match &focused {
Some(focused) => holds(focused, &window, "type")?,
None => false,
},
};
let forward = !physical || app.boolean("AXFrontmost") == Some(true);
let forward = !physical || frontmost(entry.pid)?;
if held && forward {
// The first key goes only to the field, read once more as the last read before it: the reads above wait on
// the app, and a handler that moved focus to another field of the window meanwhile would take the key.
if last.is_none() && focused_element().as_ref() != Some(&entry.element) {
return Err(Failure::refused(codes::ACTION_FAILED, "the field lost keyboard focus before typing began, so nothing was typed"));
if last.is_none() {
// The first key goes only to the field, read once more: the reads above wait on the app, and a handler that
// moved focus to another field of the window meanwhile would take the key.
if focused_checked()?.as_ref() != Some(&entry.element) {
return Err(Failure::refused(codes::ACTION_FAILED, "the field lost keyboard focus before typing began, so nothing was typed"));
}
// That read waits on the app too, and a physical key goes to whatever app is frontmost: the frontmost check
// is the last read before it, as for `press`, so another app that came forward meanwhile is still caught.
if physical && !frontmost(entry.pid)? {
return Err(Failure::refused(codes::ACTION_FAILED, "another app came forward before typing began, so nothing was typed"));
}
}
sent_to.replace(Some((target, count)));
Ok(())
Expand Down
Loading
Loading