Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

33.5 KB
Raw Blame
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Cua AI, Inc.

//! Exact Windows UIA handling for Chromium's browser-owned debugging consent.

use std::time::{Duration, Instant};

use cua_driver_core::browser::{
    BrowserConsentOutcome, BrowserConsentRequest, BrowserRefusal, BrowserRefusalCode,
};
use windows::core::Interface;
use windows::Win32::Foundation::HWND;
use windows::Win32::UI::Accessibility::{
    IUIAutomationElement, IUIAutomationInvokePattern, UIA_InvokePatternId,
};
use windows::Win32::UI::WindowsAndMessaging::GetWindowThreadProcessId;

use crate::uia::UiaNode;

const CHROMIUM_DIALOG_BUTTON_CLASS: &str = "MdTextButton";

#[derive(Clone, Debug, PartialEq, Eq)]
struct ConsentButtonCandidate {
    element_ptr: usize,
    rect: (i32, i32, i32, i32),
    has_keyboard_focus: bool,
}

fn release_nodes(nodes: &[UiaNode]) {
    for node in nodes.iter().filter(|node| node.element_ptr != 0) {
        unsafe { drop(IUIAutomationElement::from_raw(node.element_ptr as *mut _)) };
    }
}

// Own references before crossing the blocking-task boundary, including cancellation.
struct ConsentNodes(Vec<UiaNode>);

impl Drop for ConsentNodes {
    fn drop(&mut self) {
        release_nodes(&self.0);
    }
}

fn is_in_web_content(nodes: &[UiaNode], node: &UiaNode) -> bool {
    let mut parent = node.parent_element_index;
    for _ in 0..nodes.len() {
        let Some(parent_index) = parent else {
            return false;
        };
        let Some(parent_node) = nodes
            .iter()
            .find(|candidate| candidate.element_index == Some(parent_index))
        else {
            return false;
        };
        if parent_node.control_type.eq_ignore_ascii_case("Document") {
            return true;
        }
        parent = parent_node.parent_element_index;
    }
    true
}

fn is_trusted_prompt_node(nodes: &[UiaNode], node: &UiaNode) -> bool {
    !node.in_web_content
        && !node.control_type.eq_ignore_ascii_case("Document")
        && !is_in_web_content(nodes, node)
}

fn node_name(node: &UiaNode) -> Option<&str> {
    node.name
        .as_deref()
        .map(str::trim)
        .filter(|name| !name.is_empty())
}

fn subtree_end(nodes: &[UiaNode], root_index: usize) -> usize {
    let root_depth = nodes[root_index].depth;
    nodes
        .iter()
        .enumerate()
        .skip(root_index + 1)
        .find(|(_, node)| node.depth <= root_depth)
        .map_or(nodes.len(), |(index, _)| index)
}

fn has_matching_pane_ancestor(nodes: &[UiaNode], window_index: usize, name: &str) -> bool {
    let mut descendant_depth = nodes[window_index].depth;
    for ancestor in nodes[..window_index].iter().rev() {
        if ancestor.depth >= descendant_depth {
            continue;
        }
        descendant_depth = ancestor.depth;
        if is_trusted_prompt_node(nodes, ancestor)
            && ancestor.control_type == "Pane"
            && node_name(ancestor) == Some(name)
        {
            return true;
        }
    }
    false
}

/// Bind a pane-rooted native prompt to its own title.
///
/// Chromium has exposed this modal with several UIA topologies: Edge 152 put
/// one title label directly under the pane and another deeper in the dialog
/// body, while Chrome and Edge 153 expose only the dialog-body title label
/// below the prompt pane. The stable binding is that a trusted native `Text`
/// inside the pane repeats the pane's own accessible name and that the pane
/// does not wrap another native window (which would be the Window-rooted
/// topology or an unrelated surface).
fn has_pane_rooted_title_binding(nodes: &[UiaNode], pane_index: usize, name: &str) -> bool {
    let end = subtree_end(nodes, pane_index);
    let descendants = &nodes[(pane_index + 1)..end];
    let has_title = descendants.iter().any(|node| {
        is_trusted_prompt_node(nodes, node)
            && node.control_type == "Text"
            && node_name(node) == Some(name)
    });
    let has_nested_window = descendants
        .iter()
        .any(|node| is_trusted_prompt_node(nodes, node) && node.control_type == "Window");
    has_title && !has_nested_window
}

fn native_prompt_surfaces(nodes: &[UiaNode]) -> Vec<(usize, usize)> {
    nodes
        .iter()
        .enumerate()
        .filter_map(|(root_index, root)| {
            if !is_trusted_prompt_node(nodes, root) || root.depth == 0 {
                return None;
            }
            let name = node_name(root)?;
            let end = subtree_end(nodes, root_index);
            match root.control_type.as_str() {
                "Window" => {
                    if !has_matching_pane_ancestor(nodes, root_index, name) {
                        return None;
                    }
                    nodes[(root_index + 1)..end]
                        .iter()
                        .any(|node| {
                            is_trusted_prompt_node(nodes, node)
                                && node.control_type == "Text"
                                && node_name(node) == Some(name)
                        })
                        .then_some((root_index, end))
                }
                "Pane" if root.depth > 1 => has_pane_rooted_title_binding(nodes, root_index, name)
                    .then_some((root_index, end)),
                _ => None,
            }
        })
        .collect()
}

fn native_prompt_surface_present(nodes: &[UiaNode]) -> bool {
    !native_prompt_surfaces(nodes).is_empty()
}

fn native_button_properties(element_ptr: usize) -> Result<(String, bool), BrowserRefusal> {
    let element = unsafe { IUIAutomationElement::from_raw(element_ptr as *mut _) };
    let properties = unsafe {
        element.CurrentClassName().and_then(|class_name| {
            element
                .CurrentHasKeyboardFocus()
                .map(|focused| (class_name.to_string(), focused.as_bool()))
        })
    };
    std::mem::forget(element);
    properties.map_err(|error| {
        BrowserRefusal::new(
            BrowserRefusalCode::BrowserWrongTargetRefused,
            format!("could not prove a native Chromium consent button: {error}"),
        )
    })
}

fn edge_gap(first: (i32, i32, i32, i32), second: (i32, i32, i32, i32)) -> Option<i64> {
    let (first_left, first_top, first_right, first_bottom) = first;
    let (second_left, second_top, second_right, second_bottom) = second;
    let same_row = first_top == second_top && first_bottom == second_bottom;
    if !same_row {
        return None;
    }
    if first_right <= second_left {
        Some(i64::from(second_left) - i64::from(first_right))
    } else if second_right <= first_left {
        Some(i64::from(first_left) - i64::from(second_right))
    } else {
        None
    }
}

fn select_language_independent_actions(
    candidates: &[ConsentButtonCandidate],
) -> Result<(usize, usize), BrowserRefusal> {
    // Chromium builds this modal from one separated extra action plus the
    // standard OK/Cancel pair. Accessible names are localized, and the whole
    // footer mirrors for RTL locales, but adjacency and separation are stable.
    // Cancel focus is additional contradiction evidence when the browser is
    // active; an inactive browser legitimately reports no focused button.
    if candidates.len() != 3 {
        return Err(BrowserRefusal::new(
            BrowserRefusalCode::BrowserWrongTargetRefused,
            "the native Chromium consent prompt did not expose exactly three distinct dialog buttons",
        ));
    }
    let focused_indices = candidates
        .iter()
        .enumerate()
        .filter(|(_, candidate)| candidate.has_keyboard_focus)
        .map(|(index, _)| index)
        .collect::<Vec<_>>();
    if focused_indices.len() > 1 {
        return Err(BrowserRefusal::new(
            BrowserRefusalCode::BrowserWrongTargetRefused,
            "the native Chromium consent prompt exposed multiple focused dialog buttons",
        ));
    }

    let mut pairwise_gaps = Vec::new();
    for first in 0..candidates.len() {
        for second in (first + 1)..candidates.len() {
            if let Some(gap) = edge_gap(candidates[first].rect, candidates[second].rect) {
                pairwise_gaps.push((gap, first, second));
            }
        }
    }
    pairwise_gaps.sort_by_key(|(gap, _, _)| *gap);
    let [(standard_gap, first_standard, second_standard), (extra_gap, _, _), _] =
        pairwise_gaps.as_slice()
    else {
        return Err(BrowserRefusal::new(
            BrowserRefusalCode::BrowserWrongTargetRefused,
            "the native Chromium consent buttons did not form one exact non-overlapping dialog row",
        ));
    };
    let first_width = i64::from(candidates[*first_standard].rect.2)
        - i64::from(candidates[*first_standard].rect.0);
    let second_width = i64::from(candidates[*second_standard].rect.2)
        - i64::from(candidates[*second_standard].rect.0);
    if *standard_gap >= *extra_gap
        || *standard_gap > first_width.max(second_width)
        || *extra_gap < standard_gap.saturating_mul(2)
    {
        return Err(BrowserRefusal::new(
            BrowserRefusalCode::BrowserWrongTargetRefused,
            "the native Chromium consent prompt had no uniquely separated standard button pair",
        ));
    }

    let extra_index = (0..candidates.len())
        .find(|index| index != first_standard && index != second_standard)
        .expect("three candidates and a two-button pair");
    let first_to_extra = edge_gap(
        candidates[*first_standard].rect,
        candidates[extra_index].rect,
    );
    let second_to_extra = edge_gap(
        candidates[*second_standard].rect,
        candidates[extra_index].rect,
    );
    let allow_index = match (first_to_extra, second_to_extra) {
        (Some(first_gap), Some(second_gap)) if first_gap < second_gap => *first_standard,
        (Some(first_gap), Some(second_gap)) if second_gap < first_gap => *second_standard,
        _ => {
            return Err(BrowserRefusal::new(
                BrowserRefusalCode::BrowserWrongTargetRefused,
                "the native Chromium consent prompt had no unique standard button adjacent to the extra action",
            ));
        }
    };
    let cancel_index = if allow_index == *first_standard {
        *second_standard
    } else {
        *first_standard
    };
    if focused_indices
        .first()
        .is_some_and(|focused| *focused != cancel_index)
    {
        return Err(BrowserRefusal::new(
            BrowserRefusalCode::BrowserWrongTargetRefused,
            "the native Chromium consent prompt focus contradicted the structural cancel button",
        ));
    }
    Ok((
        candidates[allow_index].element_ptr,
        candidates[cancel_index].element_ptr,
    ))
}

fn exact_consent_actions_with<F>(
    nodes: &[UiaNode],
    mut properties: F,
) -> Result<Option<(usize, usize)>, BrowserRefusal>
where
    F: FnMut(usize) -> Result<(String, bool), BrowserRefusal>,
{
    let surfaces = native_prompt_surfaces(nodes);
    if surfaces.is_empty() {
        return Ok(None);
    }
    let mut matches = Vec::new();
    for (start, end) in surfaces {
        let mut candidates = Vec::new();
        for node in nodes[(start + 1)..end].iter().filter(|node| {
            is_trusted_prompt_node(nodes, node)
                && node.control_type == "Button"
                && node.actions.iter().any(|action| action == "invoke")
                && node.element_ptr != 0
                && node.rect.is_some()
        }) {
            let (class_name, has_keyboard_focus) = properties(node.element_ptr)?;
            if class_name != CHROMIUM_DIALOG_BUTTON_CLASS {
                continue;
            }
            let rect = node.rect.expect("filtered above");
            if rect.0 >= rect.2 || rect.1 >= rect.3 {
                continue;
            }
            if candidates
                .iter()
                .any(|candidate: &ConsentButtonCandidate| candidate.rect == rect)
            {
                continue;
            }
            candidates.push(ConsentButtonCandidate {
                element_ptr: node.element_ptr,
                rect,
                has_keyboard_focus,
            });
        }
        let (allow, cancel) = select_language_independent_actions(&candidates)?;
        let rect_of = |ptr: usize| {
            candidates
                .iter()
                .find(|candidate| candidate.element_ptr == ptr)
                .map(|candidate| candidate.rect)
        };
        let geometry = (node_name(&nodes[start]), rect_of(allow), rect_of(cancel));
        // The same prompt can bind twice in one walk (a nested surface, or
        // UIA repeating its rows with fresh element pointers). Its title and
        // exact button geometry identify it; any other prompt still refuses.
        if !matches
            .iter()
            .any(|(_, existing_geometry)| *existing_geometry == geometry)
        {
            matches.push(((allow, cancel), geometry));
        }
    }
    match matches.as_slice() {
        [(element, _)] => Ok(Some(*element)),
        _ => Err(BrowserRefusal::new(
            BrowserRefusalCode::BrowserWrongTargetRefused,
            "multiple bound native Chromium consent prompts exposed structural allow actions",
        )),
    }
}

fn exact_allow_button_with<F>(
    nodes: &[UiaNode],
    properties: F,
) -> Result<Option<usize>, BrowserRefusal>
where
    F: FnMut(usize) -> Result<(String, bool), BrowserRefusal>,
{
    Ok(exact_consent_actions_with(nodes, properties)?.map(|actions| actions.0))
}

fn exact_allow_button(nodes: &[UiaNode]) -> Result<Option<usize>, BrowserRefusal> {
    exact_allow_button_with(nodes, native_button_properties)
}

fn exact_cancel_button(nodes: &[UiaNode]) -> Result<Option<usize>, BrowserRefusal> {
    Ok(exact_consent_actions_with(nodes, native_button_properties)?.map(|actions| actions.1))
}

unsafe fn invoke(element_ptr: usize) -> Result<(), BrowserRefusal> {
    let element = IUIAutomationElement::from_raw(element_ptr as *mut _);
    let result = element
        .GetCurrentPattern(UIA_InvokePatternId)
        .and_then(|pattern| pattern.cast::<IUIAutomationInvokePattern>())
        .and_then(|pattern| pattern.Invoke());
    std::mem::forget(element);
    result.map_err(|error| {
        BrowserRefusal::new(
            BrowserRefusalCode::BrowserWrongTargetRefused,
            format!("the exact browser consent action failed: {error}"),
        )
    })
}

fn prove_window_owner(hwnd: u64, pid: u32) -> Result<(), BrowserRefusal> {
    let mut owner = 0u32;
    unsafe { GetWindowThreadProcessId(HWND(hwnd as *mut _), Some(&mut owner)) };
    if owner != pid {
        return Err(BrowserRefusal::new(
            BrowserRefusalCode::BrowserBindingStale,
            "the approved browser window changed ownership before consent",
        ));
    }
    Ok(())
}

pub fn dismiss(pid: u32, hwnd: u64) -> Result<bool, BrowserRefusal> {
    prove_window_owner(hwnd, pid)?;
    let deadline = Instant::now() + Duration::from_secs(2);
    let mut dismissed = false;
    loop {
        prove_window_owner(hwnd, pid)?;
        let tree = crate::uia::walk_tree(hwnd, None);
        let prompt_present = native_prompt_surface_present(&tree.nodes);
        if !prompt_present {
            release_nodes(&tree.nodes);
            return Ok(dismissed);
        }
        let cancel = exact_cancel_button(&tree.nodes);
        let invoked = match cancel {
            Ok(Some(element)) => unsafe { invoke(element) },
            Ok(None) => Err(BrowserRefusal::new(
                BrowserRefusalCode::BrowserWrongTargetRefused,
                "the exact remote-debugging consent prompt exposed no structural cancel action",
            )),
            Err(error) => Err(error),
        };
        release_nodes(&tree.nodes);
        invoked?;
        dismissed = true;
        if Instant::now() >= deadline {
            return Err(BrowserRefusal::new(
                BrowserRefusalCode::BrowserWrongTargetRefused,
                "the remote-debugging consent prompt remained after its exact cancel action",
            ));
        }
        std::thread::sleep(Duration::from_millis(100));
    }
}

pub async fn handle(
    request: BrowserConsentRequest,
) -> Result<BrowserConsentOutcome, BrowserRefusal> {
    let pid = u32::try_from(request.pid).map_err(|_| {
        BrowserRefusal::new(
            BrowserRefusalCode::BrowserWrongTargetRefused,
            "the approved browser pid is outside the Windows process-id range",
        )
    })?;
    prove_window_owner(request.window_id, pid)?;
    let deadline = Instant::now() + Duration::from_secs(4);
    let mut saw_prompt = false;
    loop {
        prove_window_owner(request.window_id, pid)?;
        let hwnd = request.window_id;
        let tree = tokio::task::spawn_blocking(move || {
            ConsentNodes(crate::uia::walk_tree(hwnd, None).nodes)
        })
        .await
        .map_err(|error| {
            BrowserRefusal::new(
                BrowserRefusalCode::BrowserRouteUnavailable,
                format!("could not inspect the browser consent UI: {error}"),
            )
        })?;
        let prompt_present = native_prompt_surface_present(&tree.0);
        saw_prompt |= prompt_present;
        match exact_allow_button(&tree.0) {
            Ok(Some(element)) => {
                let invoked = request.action.perform(|| unsafe { invoke(element) });
                drop(tree);
                invoked?;
                return Ok(BrowserConsentOutcome::Accepted);
            }
            Ok(None) => drop(tree),
            Err(error) => {
                drop(tree);
                return Err(error);
            }
        }
        if saw_prompt && !prompt_present {
            return Err(BrowserRefusal::new(
                BrowserRefusalCode::BrowserConsentRevoked,
                "the person dismissed the browser consent prompt",
            ));
        }
        if Instant::now() >= deadline {
            return Err(BrowserRefusal::new(
                BrowserRefusalCode::BrowserWrongTargetRefused,
                format!(
                    "no exact Chromium remote-debugging consent prompt appeared for reconnect attempt {}",
                    request.attempt
                ),
            ));
        }
        tokio::time::sleep(Duration::from_millis(100)).await;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn node(control_type: &str, name: &str, actions: &[&str]) -> UiaNode {
        UiaNode {
            element_index: None,
            control_type: control_type.to_owned(),
            name: Some(name.to_owned()),
            value: None,
            automation_id: None,
            help_text: None,
            actions: actions.iter().map(|value| (*value).to_owned()).collect(),
            enabled: None,
            selected: None,
            element_ptr: 0,
            center_x: 0,
            center_y: 0,
            rect: None,
            msaa_role: None,
            depth: 0,
            parent_element_index: None,
            in_web_content: false,
        }
    }

    fn dialog_node(control_type: &str, name: &str, depth: usize) -> UiaNode {
        let mut node = node(control_type, name, &[]);
        node.depth = depth;
        node
    }

    fn button(name: &str, element_ptr: usize, rect: (i32, i32, i32, i32)) -> UiaNode {
        let mut node = node("Button", name, &["invoke"]);
        node.element_index = Some(element_ptr);
        node.element_ptr = element_ptr;
        node.rect = Some(rect);
        node.depth = 8;
        node
    }

    fn prompt(title: &str, labels: [&str; 3]) -> Vec<UiaNode> {
        vec![
            dialog_node("Pane", title, 2),
            dialog_node("Window", title, 3),
            dialog_node("Text", title, 7),
            button(labels[0], 11, (-6066, 343, -5886, 399)),
            button(labels[1], 12, (-5657, 343, -5560, 399)),
            button(labels[2], 13, (-5549, 343, -5452, 399)),
        ]
    }

    fn pane_rooted_prompt(title: &str, labels: [&str; 3]) -> Vec<UiaNode> {
        vec![
            dialog_node("Pane", title, 2),
            dialog_node("Text", title, 3),
            dialog_node("Text", title, 7),
            dialog_node("Text", "opaque explanatory surface", 10),
            button(labels[0], 11, (282, 293, 419, 325)),
            button(labels[1], 12, (574, 293, 629, 325)),
            button(labels[2], 13, (637, 293, 698, 325)),
        ]
    }

    /// Chrome 153.0.8010.53 and Edge 153.0.4234.48 on hosted Windows (#4121):
    /// the prompt pane holds only the nested dialog-body title label.
    fn chromium_153_prompt(title: &str, labels: [&str; 3]) -> Vec<UiaNode> {
        vec![
            dialog_node("Pane", title, 2),
            dialog_node("Text", title, 7),
            dialog_node("Text", "opaque explanatory surface", 10),
            dialog_node("Text", "opaque developer warning", 10),
            button(labels[0], 11, (286, 307, 429, 345)),
            button(labels[1], 12, (544, 307, 615, 345)),
            button(labels[2], 13, (623, 307, 694, 345)),
        ]
    }

    fn properties(element_ptr: usize) -> Result<(String, bool), BrowserRefusal> {
        Ok((CHROMIUM_DIALOG_BUTTON_CLASS.to_owned(), element_ptr == 13))
    }

    #[tokio::test]
    async fn cancelled_scan_releases_late_blocking_result() {
        use std::ffi::c_void;
        use windows::core::{IUnknown_Vtbl, GUID, HRESULT};

        // Only IUnknown::Release is used, as in the existing snapshot ownership tests.
        #[repr(C)]
        struct OwnedReference {
            vtable: *const IUnknown_Vtbl,
            released: Option<tokio::sync::oneshot::Sender<()>>,
        }
        unsafe extern "system" fn query(
            _: *mut c_void,
            _: *const GUID,
            _: *mut *mut c_void,
        ) -> HRESULT {
            HRESULT(0x8000_4002u32 as i32)
        }
        unsafe extern "system" fn retain(_: *mut c_void) -> u32 {
            2
        }
        unsafe extern "system" fn release(this: *mut c_void) -> u32 {
            let mut owned = unsafe { Box::from_raw(this as *mut OwnedReference) };
            let _ = owned.released.take().unwrap().send(());
            0
        }
        static VTABLE: IUnknown_Vtbl = IUnknown_Vtbl {
            QueryInterface: query,
            AddRef: retain,
            Release: release,
        };
        let (ready_tx, ready_rx) = tokio::sync::oneshot::channel();
        let (finish_tx, finish_rx) = std::sync::mpsc::channel();
        let (dropped_tx, dropped_rx) = tokio::sync::oneshot::channel();
        let scan = tokio::task::spawn_blocking(move || {
            let ptr = Box::into_raw(Box::new(OwnedReference {
                vtable: &VTABLE,
                released: Some(dropped_tx),
            }));
            let mut element = node("Button", "Allow", &["Invoke"]);
            element.element_ptr = ptr as usize;
            let owned = ConsentNodes(vec![element]);
            ready_tx.send(()).unwrap();
            finish_rx.recv().unwrap();
            owned
        });
        ready_rx.await.unwrap();
        drop(scan);
        finish_tx.send(()).unwrap();
        tokio::time::timeout(Duration::from_secs(2), dropped_rx)
            .await
            .unwrap()
            .unwrap();
    }

    #[test]
    fn matcher_is_language_independent_across_localized_native_dialogs() {
        for (title, labels) in [
            (
                "リモート デバッグを許可しますか?",
                ["設定でオフ", "許可", "キャンセル"],
            ),
            (
                "Remote-Debugging zulassen?",
                ["In Einstellungen deaktivieren", "Zulassen", "Abbrechen"],
            ),
            (
                "Autoriser le débogage à distance ?",
                ["Désactiver", "Autoriser", "Annuler"],
            ),
            (
                "هل تريد السماح بتصحيح الأخطاء عن بُعد؟",
                ["تعطيل", "سماح", "إلغاء"],
            ),
        ] {
            assert_eq!(
                exact_allow_button_with(&prompt(title, labels), properties).unwrap(),
                Some(12)
            );
        }
    }

    #[test]
    fn cancel_matcher_selects_the_structural_default_action() {
        let nodes = prompt(
            "Allow remote debugging?",
            ["Turn off in settings", "Allow", "Cancel"],
        );
        assert_eq!(
            exact_consent_actions_with(&nodes, properties).unwrap(),
            Some((12, 13))
        );
    }

    #[test]
    fn matcher_supports_pane_rooted_native_edge_prompt() {
        assert_eq!(
            exact_allow_button_with(
                &pane_rooted_prompt(
                    "¿Permitir la depuración remota?",
                    ["Desactivar", "Permitir", "Cancelar"],
                ),
                properties,
            )
            .unwrap(),
            Some(12)
        );
    }

    #[test]
    fn matcher_supports_chromium_153_pane_rooted_prompt() {
        let nodes = chromium_153_prompt(
            "Allow remote debugging?",
            ["Turn off in settings", "Allow", "Cancel"],
        );
        assert_eq!(
            exact_consent_actions_with(&nodes, properties).unwrap(),
            Some((12, 13))
        );
        assert_eq!(
            exact_allow_button_with(
                &chromium_153_prompt(
                    "Remote-Debugging zulassen?",
                    ["In Einstellungen deaktivieren", "Zulassen", "Abbrechen"],
                ),
                properties,
            )
            .unwrap(),
            Some(12)
        );
    }

    #[test]
    fn pane_rooted_prompt_requires_a_title_binding_and_no_nested_window() {
        let mut one_title_left = pane_rooted_prompt("opaque title", ["A", "B", "C"]);
        one_title_left[1].name = Some("different direct title".to_owned());
        assert_eq!(
            exact_allow_button_with(&one_title_left, properties).unwrap(),
            Some(12)
        );

        let mut no_title = pane_rooted_prompt("opaque title", ["A", "B", "C"]);
        no_title[1].name = Some("different direct title".to_owned());
        no_title[2].name = Some("different nested title".to_owned());
        assert_eq!(
            exact_allow_button_with(&no_title, properties).unwrap(),
            None
        );

        let mut untitled_153 = chromium_153_prompt("opaque title", ["A", "B", "C"]);
        untitled_153[1].name = Some("different title".to_owned());
        assert_eq!(
            exact_allow_button_with(&untitled_153, properties).unwrap(),
            None
        );

        let mut web_title = chromium_153_prompt("opaque title", ["A", "B", "C"]);
        web_title[1].in_web_content = true;
        assert_eq!(
            exact_allow_button_with(&web_title, properties).unwrap(),
            None
        );

        let mut nested_window = pane_rooted_prompt("opaque title", ["A", "B", "C"]);
        nested_window.insert(2, dialog_node("Window", "unrelated native window", 3));
        assert_eq!(
            exact_allow_button_with(&nested_window, properties).unwrap(),
            None
        );

        let mut browser_root = pane_rooted_prompt("opaque title", ["A", "B", "C"]);
        browser_root[0].depth = 1;
        assert_eq!(
            exact_allow_button_with(&browser_root, properties).unwrap(),
            None
        );
    }

    #[test]
    fn matcher_binds_one_prompt_reported_twice_with_the_same_geometry() {
        let mut nodes = pane_rooted_prompt("same title", ["A", "B", "C"]);
        let mut repeated = pane_rooted_prompt("same title", ["A", "B", "C"]);
        for node in &mut repeated {
            if node.element_ptr != 0 {
                node.element_ptr += 1000;
            }
        }
        nodes.extend(repeated);
        assert!(exact_allow_button_with(&nodes, properties)
            .unwrap()
            .is_some());
    }

    #[test]
    fn matcher_refuses_multiple_pane_rooted_native_prompts() {
        let mut nodes = pane_rooted_prompt("first opaque title", ["A", "B", "C"]);
        nodes.extend(pane_rooted_prompt("second opaque title", ["D", "E", "F"]));

        assert_eq!(
            exact_allow_button_with(&nodes, properties)
                .unwrap_err()
                .code,
            BrowserRefusalCode::BrowserWrongTargetRefused
        );
    }

    #[test]
    fn matcher_treats_nonempty_unicode_names_as_opaque_data() {
        for (title, labels) in [
            ("e\u{301}", ["é", "E\u{301}", "ë"]),
            ("हिन्दी", ["ไทย", "עברית", "فارسی"]),
            ("日本語", ["한국어", "简体中文", "繁體中文"]),
            ("\u{2067}العربية\u{2069}", ["\u{2066}A\u{2069}", "👩🏽‍💻", "𐐷"]),
            ("Հայերեն", ["ქართული", "አማርኛ", "বাংলা"]),
            ("A\u{200d}B", ["無", "⠿", "✅"]),
        ] {
            assert_eq!(
                exact_allow_button_with(&prompt(title, labels), properties).unwrap(),
                Some(12)
            );
        }
    }

    #[test]
    fn matcher_refuses_when_accessible_names_cannot_bind_the_native_surface() {
        let mut nodes = prompt("placeholder", ["A", "B", "C"]);
        for node in &mut nodes {
            node.name = None;
        }

        assert_eq!(exact_allow_button_with(&nodes, properties).unwrap(), None);
    }

    #[test]
    fn matcher_deduplicates_repeated_uia_walk_rows_by_exact_geometry() {
        let mut nodes = prompt("任何语言", ["A", "B", "C"]);
        let mut duplicate = nodes[4].clone();
        duplicate.element_ptr = 99;
        nodes.push(duplicate);
        assert_eq!(
            exact_allow_button_with(&nodes, properties).unwrap(),
            Some(12)
        );
    }

    #[test]
    fn matcher_is_direction_independent_for_rtl_dialog_layouts() {
        let mut nodes = prompt(
            "هل تريد السماح بتصحيح الأخطاء عن بُعد؟",
            ["تعطيل", "سماح", "إلغاء"],
        );
        nodes[3].rect = Some((600, 343, 780, 399));
        nodes[4].rect = Some((299, 343, 396, 399));
        nodes[5].rect = Some((191, 343, 288, 399));
        assert_eq!(
            exact_allow_button_with(&nodes, properties).unwrap(),
            Some(12)
        );
    }

    #[test]
    fn matcher_does_not_require_focus_when_the_browser_is_inactive() {
        let nodes = prompt(
            "¿Permitir la depuración remota?",
            ["Desactivar", "Permitir", "Cancelar"],
        );
        assert_eq!(
            exact_allow_button_with(&nodes, |_| {
                Ok((CHROMIUM_DIALOG_BUTTON_CLASS.to_owned(), false))
            })
            .unwrap(),
            Some(12)
        );
    }

    #[test]
    fn matcher_refuses_ambiguous_focus_or_button_geometry() {
        let nodes = prompt("Qualsiasi lingua", ["A", "B", "C"]);
        assert_eq!(
            exact_allow_button_with(&nodes, |element_ptr| {
                Ok((CHROMIUM_DIALOG_BUTTON_CLASS.to_owned(), element_ptr >= 12))
            })
            .unwrap_err()
            .code,
            BrowserRefusalCode::BrowserWrongTargetRefused
        );

        let mut equidistant = nodes;
        equidistant[3].rect = Some((-5700, 343, -5560, 399));
        assert_eq!(
            exact_allow_button_with(&equidistant, properties)
                .unwrap_err()
                .code,
            BrowserRefusalCode::BrowserWrongTargetRefused
        );
    }

    #[test]
    fn matcher_ignores_a_spoofed_prompt_inside_web_content() {
        let mut nodes = prompt("Permitir depuração remota?", ["A", "B", "C"]);
        for child in &mut nodes {
            child.in_web_content = true;
        }
        assert_eq!(exact_allow_button_with(&nodes, properties).unwrap(), None);
    }

    #[test]
    fn matcher_refuses_chromium_buttons_outside_the_bound_prompt_subtree() {
        let title = "opaque consent surface";
        let nodes = vec![
            dialog_node("Pane", title, 2),
            dialog_node("Window", title, 3),
            dialog_node("Text", title, 7),
            dialog_node("Pane", "unrelated native sibling", 2),
            button("A", 11, (-6066, 343, -5886, 399)),
            button("B", 12, (-5657, 343, -5560, 399)),
            button("C", 13, (-5549, 343, -5452, 399)),
        ];

        assert_eq!(
            exact_allow_button_with(&nodes, properties)
                .unwrap_err()
                .code,
            BrowserRefusalCode::BrowserWrongTargetRefused
        );
    }

    #[test]
    fn matcher_requires_the_matching_pane_to_be_a_window_ancestor() {
        let title = "opaque consent surface";
        let nodes = vec![
            dialog_node("Pane", title, 2),
            dialog_node("Pane", "different native container", 2),
            dialog_node("Window", title, 3),
            dialog_node("Text", title, 7),
            button("A", 11, (-6066, 343, -5886, 399)),
            button("B", 12, (-5657, 343, -5560, 399)),
            button("C", 13, (-5549, 343, -5452, 399)),
        ];

        assert_eq!(exact_allow_button_with(&nodes, properties).unwrap(), None);
    }

    #[test]
    fn matcher_requires_the_native_prompt_surface_and_chromium_button_class() {
        let mut nodes = prompt("원격 디버깅을 허용하시겠습니까?", ["A", "B", "C"]);
        nodes[0].name = Some("different native pane".to_owned());
        assert_eq!(exact_allow_button_with(&nodes, properties).unwrap(), None);

        let nodes = prompt("원격 디버깅을 허용하시겠습니까?", ["A", "B", "C"]);
        assert_eq!(
            exact_allow_button_with(&nodes, |element_ptr| {
                Ok((
                    if element_ptr == 12 {
                        "RendererButton"
                    } else {
                        CHROMIUM_DIALOG_BUTTON_CLASS
                    }
                    .to_owned(),
                    element_ptr == 13,
                ))
            })
            .unwrap_err()
            .code,
            BrowserRefusalCode::BrowserWrongTargetRefused
        );
    }
}