icedwright-semantics 0.0.2

The icedwright semantic tree: inference from recorded widget operations, stable node paths, hit testing, diffs and YAML
Documentation
//! Queries on finished trees: hit testing, diffs, paths across snapshots and the builder.

use icedwright_protocol::WindowId;
use icedwright_semantics::{Layer, NodePath, SemanticTree, TreeBuilder, diff, infer};
use icedwright_types::{Checked, Point, Role, role};

fn todo(first: &str, done: bool) -> SemanticTree {
    TreeBuilder::window("Todo")
        .child(role::List, "", |list| {
            list.test_id("todo-list")
                .child(role::ListItem, "", |item| {
                    item.child(role::CheckBox, first, |c| c.checked(done))
                        .child(role::Button, "Delete", |b| b)
                })
                .child(role::ListItem, "", |item| item.text("Walk the dog"))
        })
        .build()
}

fn id_of(tree: &SemanticTree, role: Role, name: &str) -> Option<icedwright_semantics::NodeId> {
    tree.iter()
        .find(|n| n.role == role && n.name.as_deref() == Some(name))
        .map(|n| n.id)
}

#[test]
fn builder_trees_have_layout_names_and_paths() {
    let tree = todo("Buy milk", false);
    let checkbox = id_of(&tree, Role::CheckBox, "Buy milk").and_then(|id| tree.get(id));
    assert_eq!(
        checkbox.and_then(|n| n.states.checked),
        Some(Checked::False)
    );
    assert!(checkbox.is_some_and(|n| n.bounds.area() > 0.0 && n.visible_bounds.is_some()));
    let paths: Vec<String> = tree
        .iter()
        .filter(|n| !n.ignored)
        .map(|n| n.path.to_string())
        .collect();
    assert_eq!(
        paths,
        [
            "window \"Todo\"",
            "window \"Todo\" / #todo-list",
            "window \"Todo\" / #todo-list / listitem \"Buy milk Delete\"",
            "window \"Todo\" / #todo-list / listitem \"Buy milk Delete\" / checkbox \"Buy milk\"",
            "window \"Todo\" / #todo-list / listitem \"Buy milk Delete\" / checkbox \"Buy milk\" / text \"Buy milk\"",
            "window \"Todo\" / #todo-list / listitem \"Buy milk Delete\" / button \"Delete\"",
            "window \"Todo\" / #todo-list / listitem \"Buy milk Delete\" / button \"Delete\" / text \"Delete\"",
            "window \"Todo\" / #todo-list / listitem \"Walk the dog\"",
            "window \"Todo\" / #todo-list / listitem \"Walk the dog\" / text \"Walk the dog\"",
        ]
    );
}

#[test]
fn find_path_works_across_snapshots() {
    let before = todo("Buy milk", false);
    let after = todo("Buy milk", true);
    for node in before.iter().filter(|n| !n.ignored) {
        let found = after.find_path(&node.path).and_then(|id| after.get(id));
        assert_eq!(found.map(|n| n.role), Some(node.role), "{}", node.path);
    }
    let missing: NodePath = "window \"Todo\" / button \"Nope\""
        .parse()
        .unwrap_or_default();
    assert_eq!(after.find_path(&missing), None);
}

#[test]
fn diff_reports_added_removed_and_changed_by_path() {
    let before = todo("Buy milk", false);
    let after = todo("Buy milk", true);
    let changes = diff(&before, &after);
    assert!(changes.added.is_empty() && changes.removed.is_empty());
    assert_eq!(changes.changed.len(), 1);
    assert_eq!(changes.changed[0].fields, ["states"]);
    assert!(
        changes.changed[0]
            .path
            .to_string()
            .ends_with("checkbox \"Buy milk\"")
    );

    let renamed = todo("Buy bread", false);
    let changes = diff(&before, &renamed);
    assert!(
        changes
            .added
            .iter()
            .any(|p| p.to_string().ends_with("checkbox \"Buy bread\""))
    );
    assert!(
        changes
            .removed
            .iter()
            .any(|p| p.to_string().ends_with("checkbox \"Buy milk\""))
    );
    assert!(diff(&before, &before).is_empty());
}

#[test]
fn hit_test_prefers_higher_layers_then_paint_order() {
    let tree = TreeBuilder::window("Cube")
        .child(role::Button, "Under", |b| b.bounds(0.0, 0.0, 100.0, 100.0))
        .child(role::Button, "Later sibling", |b| {
            b.bounds(50.0, 50.0, 100.0, 100.0)
        })
        .overlay(1, |layer| {
            layer.child(role::Menu, "Menu", |m| m.bounds(80.0, 80.0, 50.0, 50.0))
        })
        .build();
    let hit = |x: f32, y: f32| {
        tree.hit_test(WindowId(0), Point::new(x, y))
            .and_then(|id| tree.get(id))
            .and_then(|n| n.name.clone())
    };
    assert_eq!(hit(10.0, 10.0).as_deref(), Some("Under"));
    assert_eq!(hit(60.0, 60.0).as_deref(), Some("Later sibling"));
    assert_eq!(hit(90.0, 90.0).as_deref(), Some("Menu"));
    assert_eq!(hit(700.0, 500.0).as_deref(), Some("Cube"));
    assert_eq!(tree.hit_test(WindowId(1), Point::new(10.0, 10.0)), None);
    let menu = id_of(&tree, Role::Menu, "Menu").and_then(|id| tree.get(id));
    assert_eq!(menu.map(|n| n.layer), Some(Layer::Overlay(1)));
}

#[test]
fn builder_raw_is_what_infer_reads() {
    let builder = TreeBuilder::window("A").and_window("B").text("In B");
    let tree = builder.build();
    assert_eq!(tree.roots().len(), 2);
    assert_eq!(infer(&builder.raw()), tree);
    let in_b = tree.iter().find(|n| n.role == Role::Text).map(|n| n.window);
    assert_eq!(in_b, Some(WindowId(1)));
    assert_eq!(tree.text_content(tree.roots()[1]), "In B");
}

#[test]
fn ancestors_walk_up_to_the_window() {
    let tree = todo("Buy milk", false);
    let Some(button) = id_of(&tree, Role::Button, "Delete") else {
        panic!("no button");
    };
    let roles: Vec<Role> = tree
        .ancestors(button)
        .filter_map(|id| tree.get(id).map(|n| n.role))
        .collect();
    assert_eq!(roles, [Role::ListItem, Role::List, Role::Window]);
}