use std::borrow::Cow;
use icedwright_protocol::{
AnnotationSource, RawAnnotation, RawCall, RawKey, RawNode, RawTree, RawWindow, RawWindowInfo,
WidgetId, WindowId,
};
use icedwright_semantics::{
Kind, Layer, NameSource, Node, NodeId, OperationKind, SemanticTree, StateSource, TreeWarning,
Value, infer,
};
use icedwright_types::{
Checked, Point, Rect, Role, ScaleFactor, Size, StateName, StateValue, Vector,
};
fn rect(x: f32, y: f32, w: f32, h: f32) -> Rect {
Rect::new(Point::new(x, y), Size::new(w, h))
}
fn window(children: Vec<RawNode>) -> RawTree {
let size = Size::new(800.0, 600.0);
RawTree {
windows: vec![RawWindow {
id: Some(WindowId(0)),
info: Some(RawWindowInfo {
id: WindowId(0),
title: "Test".to_owned(),
size,
scale_factor: ScaleFactor::default(),
}),
root: RawNode::new(RawCall::Group, Rect::new(Point::ORIGIN, size))
.with_children(children),
}],
}
}
fn text(content: &str, bounds: Rect) -> RawNode {
RawNode::new(
RawCall::Text {
content: content.to_owned(),
},
bounds,
)
}
fn container(bounds: Rect, children: Vec<RawNode>) -> RawNode {
RawNode::new(RawCall::Container, bounds).with_children(children)
}
fn focusable(focused: bool, ordinal: u32, bounds: Rect) -> RawNode {
RawNode::new(RawCall::Focusable { focused, ordinal }, bounds)
}
fn annotation(role: Option<Role>, name: Option<&str>) -> RawAnnotation {
RawAnnotation {
role,
name: name.map(str::to_owned),
..RawAnnotation::default()
}
}
fn annotated(annotation: RawAnnotation, bounds: Rect, children: Vec<RawNode>) -> RawNode {
RawNode::new(RawCall::Annotation(Box::new(annotation)), bounds).with_children(children)
}
fn visible(tree: &SemanticTree) -> Vec<&Node> {
tree.iter()
.filter(|n| !n.ignored && n.role != Role::Window)
.collect()
}
fn only(tree: &SemanticTree, role: Role) -> &Node {
let found: Vec<&Node> = tree.iter().filter(|n| n.role == role).collect();
assert_eq!(found.len(), 1, "{role}: {found:#?}");
found[0]
}
#[test]
fn merge_leaf_joins_a_text_input_and_its_focusable() {
let bounds = rect(10.0, 10.0, 200.0, 30.0);
let id = WidgetId::Custom("search".to_owned());
let tree = infer(&window(vec![
RawNode::new(
RawCall::TextInput {
text: "milk".to_owned(),
},
bounds,
)
.with_id(id.clone()),
focusable(true, 3, bounds).with_id(id.clone()),
]));
let node = only(&tree, Role::TextBox);
assert_eq!(node.value, Some(Value::Text("milk".to_owned())));
assert!(node.sources.visible_text);
assert_eq!(node.states.focused, Some(true));
assert!(node.states.focusable);
assert_eq!(node.focusable_ordinal, Some(3));
assert_eq!(node.widget_id, Some(id));
assert_eq!(node.kind, Kind::Inferred(OperationKind::TextInput));
assert_eq!(node.states.multiline, None);
}
#[test]
fn merge_leaf_does_not_join_different_bounds_or_two_texts() {
let tree = infer(&window(vec![
RawNode::new(
RawCall::TextInput {
text: String::new(),
},
rect(0.0, 0.0, 10.0, 10.0),
),
focusable(false, 0, rect(0.0, 20.0, 10.0, 10.0)),
text("a", rect(0.0, 40.0, 10.0, 10.0)),
text("b", rect(0.0, 40.0, 10.0, 10.0)),
]));
assert_eq!(tree.iter().filter(|n| n.role == Role::TextBox).count(), 2);
assert_eq!(tree.iter().filter(|n| n.role == Role::Text).count(), 2);
}
#[test]
fn absorb_makes_one_button_with_its_text_as_a_child() {
let bounds = rect(10.0, 10.0, 80.0, 30.0);
let tree = infer(&window(vec![annotated(
annotation(Some(Role::Button), None),
bounds,
vec![container(
bounds,
vec![text("Delete", rect(20.0, 15.0, 60.0, 20.0))],
)],
)]));
let button = only(&tree, Role::Button);
assert_eq!(button.name.as_deref(), Some("Delete"));
assert_eq!(button.name_source, NameSource::Content);
assert_eq!(button.children.len(), 1);
let child = tree.get(button.children[0]).map(|n| n.role);
assert_eq!(child, Some(Role::Text));
assert!(button.sources.annotation && button.sources.inferred);
assert_eq!(visible(&tree).len(), 2);
}
#[test]
fn absorb_takes_focus_and_value_from_a_text_input() {
let bounds = rect(0.0, 0.0, 200.0, 30.0);
let mut field = annotation(Some(Role::TextBox), Some("New item"));
field.placeholder = Some("What needs doing?".to_owned());
let tree = infer(&window(vec![annotated(
field,
bounds,
vec![
RawNode::new(
RawCall::TextInput {
text: "Milk".to_owned(),
},
bounds,
)
.with_id(WidgetId::Unique(4)),
focusable(false, 0, bounds).with_id(WidgetId::Unique(4)),
],
)]));
let node = only(&tree, Role::TextBox);
assert_eq!(node.name.as_deref(), Some("New item"));
assert_eq!(node.value, Some(Value::Text("Milk".to_owned())));
assert_eq!(node.states.focused, Some(false));
assert_eq!(node.widget_id, Some(WidgetId::Unique(4)));
assert_eq!(node.placeholder.as_deref(), Some("What needs doing?"));
}
#[test]
fn renumbering_rewrites_unique_widget_ids_and_leaves_custom_ones() {
let bounds = rect(0.0, 0.0, 200.0, 30.0);
let input = |id: WidgetId, y: f32| {
RawNode::new(
RawCall::TextInput {
text: String::new(),
},
rect(0.0, y, 200.0, 30.0),
)
.with_id(id)
};
let mut tree = infer(&window(vec![
input(WidgetId::Unique(41), 0.0),
input(WidgetId::Custom("search".to_owned()), 40.0),
focusable(false, 0, bounds).with_id(WidgetId::Unique(7)),
]));
tree.renumber_widget_ids(|n| n + 1000);
let ids: Vec<WidgetId> = tree.iter().filter_map(|n| n.widget_id.clone()).collect();
assert!(ids.contains(&WidgetId::Unique(1041)), "{ids:?}");
assert!(
ids.contains(&WidgetId::Custom("search".to_owned())),
"{ids:?}"
);
assert!(!ids.contains(&WidgetId::Unique(41)), "{ids:?}");
}
#[test]
fn absorb_stops_at_different_bounds_and_at_several_children() {
let tree = infer(&window(vec![
annotated(
annotation(Some(Role::Group), Some("One")),
rect(0.0, 0.0, 100.0, 100.0),
vec![container(rect(0.0, 0.0, 50.0, 50.0), vec![])],
),
annotated(
annotation(Some(Role::Group), Some("Two")),
rect(0.0, 100.0, 100.0, 100.0),
vec![
container(rect(0.0, 100.0, 100.0, 100.0), vec![]),
text("x", rect(0.0, 100.0, 10.0, 10.0)),
],
),
]));
let groups: Vec<&Node> = tree.iter().filter(|n| n.role == Role::Group).collect();
assert_eq!(groups[0].children.len(), 1);
assert_eq!(groups[1].children.len(), 2);
assert!(!groups[0].sources.inferred);
}
#[test]
fn stack_annotations_let_the_app_win_over_a_component() {
let bounds = rect(0.0, 0.0, 80.0, 30.0);
let mut component = annotation(Some(Role::Button), Some("Save"));
component.source = AnnotationSource::Component;
component.kind = Some(Cow::Borrowed("iced_cube::button"));
component.states = vec![(StateName::Disabled, StateValue::Bool(true))];
component.tooltip = Some("Save the note".to_owned());
let app = annotation(None, Some("Save note"));
let tree = infer(&window(vec![annotated(
component,
bounds,
vec![annotated(app, bounds, vec![])],
)]));
let button = only(&tree, Role::Button);
assert_eq!(button.name.as_deref(), Some("Save note"));
assert_eq!(button.tooltip.as_deref(), Some("Save the note"));
assert_eq!(button.states.disabled, Some(true));
assert_eq!(
button.states.source(StateName::Disabled),
Some(StateSource::Component)
);
assert_eq!(button.kind, Kind::Annotated(Cow::Borrowed("app")));
assert!(button.sources.annotation && button.sources.component);
assert!(tree.warnings().is_empty());
}
#[test]
fn stack_annotations_keep_different_roles_apart_at_equal_bounds() {
let bounds = rect(0.0, 0.0, 80.0, 30.0);
let tree = infer(&window(vec![annotated(
annotation(Some(Role::Link), None),
bounds,
vec![annotated(
annotation(Some(Role::Button), Some("Go")),
bounds,
vec![],
)],
)]));
let link = only(&tree, Role::Link);
let button = only(&tree, Role::Button);
assert_eq!(link.children, vec![button.id]);
assert_eq!(button.name.as_deref(), Some("Go"));
assert!(tree.warnings().is_empty());
}
#[test]
fn a_one_item_list_keeps_its_item_at_equal_bounds() {
let bounds = rect(0.0, 0.0, 200.0, 24.0);
let tree = infer(&window(vec![annotated(
annotation(Some(Role::List), Some("Notes")),
bounds,
vec![container(
bounds,
vec![annotated(
annotation(Some(Role::ListItem), Some("Only")),
bounds,
vec![text("Only", bounds)],
)],
)],
)]));
let list = only(&tree, Role::List);
let item = only(&tree, Role::ListItem);
assert_eq!(list.name.as_deref(), Some("Notes"));
assert_eq!(item.name.as_deref(), Some("Only"));
assert_eq!(list.children, vec![item.id]);
assert_eq!(item.bounds, list.bounds);
}
#[test]
fn stack_annotations_do_not_merge_at_different_bounds() {
let tree = infer(&window(vec![annotated(
annotation(Some(Role::List), None),
rect(0.0, 0.0, 100.0, 100.0),
vec![annotated(
annotation(Some(Role::ListItem), Some("a")),
rect(0.0, 0.0, 100.0, 20.0),
vec![],
)],
)]));
assert_eq!(visible(&tree).len(), 2);
}
#[test]
fn per_call_roles() {
let tree = infer(&window(vec![
text("Hello", rect(0.0, 0.0, 50.0, 20.0)),
focusable(false, 0, rect(0.0, 20.0, 200.0, 100.0)),
RawNode::new(
RawCall::Scrollable {
content_bounds: rect(0.0, 120.0, 200.0, 400.0),
translation: Vector::ZERO,
ordinal: 0,
},
rect(0.0, 120.0, 200.0, 200.0),
),
container(rect(0.0, 320.0, 10.0, 10.0), vec![]).with_id(WidgetId::Custom("box".to_owned())),
container(rect(0.0, 330.0, 10.0, 10.0), vec![]),
]));
let text_node = only(&tree, Role::Text);
assert_eq!(text_node.name.as_deref(), Some("Hello"));
assert_eq!(text_node.name_source, NameSource::Text);
let editor = only(&tree, Role::TextBox);
assert_eq!(editor.states.multiline, Some(true));
assert_eq!(
editor.states.source(StateName::Multiline),
Some(StateSource::Inferred)
);
assert!(only(&tree, Role::ScrollView).scroll.is_some());
let generics: Vec<&Node> = tree.iter().filter(|n| n.role == Role::Generic).collect();
assert_eq!(generics.len(), 2);
assert!(!generics[0].ignored, "a generic with an id is kept");
assert!(generics[1].ignored, "a bare generic is ignored");
}
#[test]
fn ignore_generic_keeps_named_and_related_generics() {
let mut keyed = annotation(None, None);
keyed.owns = vec![RawKey::Name("x".to_owned())];
let mut target = annotation(Some(Role::Menu), None);
target.key = Some(RawKey::Name("x".to_owned()));
let tree = infer(&window(vec![
annotated(
annotation(None, Some("Named")),
rect(0.0, 0.0, 10.0, 10.0),
vec![],
),
annotated(keyed, rect(0.0, 10.0, 10.0, 10.0), vec![]),
annotated(target, rect(0.0, 20.0, 10.0, 10.0), vec![]),
annotated(annotation(None, None), rect(0.0, 30.0, 10.0, 10.0), vec![]),
]));
let generics: Vec<bool> = tree
.iter()
.filter(|n| n.role == Role::Generic)
.map(|n| n.ignored)
.collect();
assert_eq!(generics, [false, false, true]);
}
#[test]
fn viewport_shifts_and_clips_like_iced_selector() {
let tree = infer(&window(vec![
RawNode::new(
RawCall::Scrollable {
content_bounds: rect(0.0, 0.0, 400.0, 2000.0),
translation: Vector::new(0.0, 300.0),
ordinal: 0,
},
rect(0.0, 0.0, 400.0, 400.0),
)
.with_children(vec![
text("Shown", rect(0.0, 350.0, 100.0, 20.0)),
text("Partly", rect(0.0, 690.0, 100.0, 20.0)),
text("Gone", rect(0.0, 1000.0, 100.0, 20.0)),
]),
]));
let by_name = |name: &str| tree.iter().find(|n| n.name.as_deref() == Some(name));
let shown = by_name("Shown");
assert_eq!(shown.map(|n| n.bounds), Some(rect(0.0, 50.0, 100.0, 20.0)));
assert_eq!(
shown.and_then(|n| n.visible_bounds),
Some(rect(0.0, 50.0, 100.0, 20.0))
);
let partly = by_name("Partly");
assert_eq!(
partly.and_then(|n| n.visible_bounds),
Some(rect(0.0, 390.0, 100.0, 10.0))
);
let gone = by_name("Gone");
assert_eq!(gone.map(|n| n.bounds), Some(rect(0.0, 700.0, 100.0, 20.0)));
assert_eq!(gone.and_then(|n| n.visible_bounds), None);
assert_eq!(gone.map(|n| n.states.offscreen), Some(true));
assert_eq!(gone.map(|n| n.scroll_ancestors.len()), Some(1));
}
#[test]
fn nested_scrollables_are_both_ancestors() {
let inner = RawNode::new(
RawCall::Scrollable {
content_bounds: rect(0.0, 0.0, 200.0, 800.0),
translation: Vector::new(0.0, 100.0),
ordinal: 1,
},
rect(0.0, 0.0, 200.0, 200.0),
)
.with_children(vec![text("Deep", rect(0.0, 150.0, 50.0, 20.0))]);
let tree = infer(&window(vec![
RawNode::new(
RawCall::Scrollable {
content_bounds: rect(0.0, 0.0, 400.0, 1200.0),
translation: Vector::new(0.0, 0.0),
ordinal: 0,
},
rect(0.0, 0.0, 400.0, 400.0),
)
.with_children(vec![inner]),
]));
let deep = only(&tree, Role::Text);
let ordinals: Vec<u32> = deep.scroll_ancestors.iter().map(|s| s.ordinal).collect();
assert_eq!(ordinals, [0, 1]);
assert_eq!(deep.bounds, rect(0.0, 50.0, 50.0, 20.0));
}
#[test]
fn overlays_reset_the_viewport_and_take_their_layer() {
let tree = infer(&window(vec![
RawNode::new(
RawCall::Scrollable {
content_bounds: rect(0.0, 0.0, 400.0, 2000.0),
translation: Vector::new(0.0, 300.0),
ordinal: 0,
},
rect(0.0, 0.0, 400.0, 400.0),
),
RawNode::new(RawCall::Overlay { depth: 1 }, rect(0.0, 0.0, 800.0, 600.0))
.with_children(vec![text("Menu item", rect(10.0, 10.0, 80.0, 20.0))]),
]));
let item = only(&tree, Role::Text);
assert_eq!(item.layer, Layer::Overlay(1));
assert_eq!(item.bounds, rect(10.0, 10.0, 80.0, 20.0));
assert!(item.scroll_ancestors.is_empty());
assert_eq!(item.parent, tree.roots().first().copied());
assert_eq!(only(&tree, Role::ScrollView).layer, Layer::Base);
}
#[test]
fn derive_hidden_from_zero_size_explicit_and_ancestors() {
let mut hidden = annotation(Some(Role::Group), Some("Hover tools"));
hidden.states = vec![(StateName::Hidden, StateValue::Bool(true))];
let tree = infer(&window(vec![
text("Zero", rect(0.0, 0.0, 0.0, 0.0)),
annotated(
hidden,
rect(0.0, 10.0, 100.0, 50.0),
vec![text("Inside", rect(0.0, 20.0, 50.0, 10.0))],
),
container(
rect(0.0, 0.0, 0.0, 0.0),
vec![text("Under a zero generic", rect(0.0, 70.0, 50.0, 10.0))],
),
text("Plain", rect(0.0, 90.0, 50.0, 10.0)),
]));
let hidden_of = |name: &str| {
tree.iter()
.find(|n| n.name.as_deref() == Some(name))
.map(|n| (n.states.hidden, n.states.source(StateName::Hidden)))
};
assert_eq!(hidden_of("Zero"), Some((true, Some(StateSource::Derived))));
assert_eq!(
hidden_of("Hover tools"),
Some((true, Some(StateSource::Annotation)))
);
assert_eq!(
hidden_of("Inside"),
Some((true, Some(StateSource::Derived)))
);
assert_eq!(
hidden_of("Under a zero generic"),
Some((false, Some(StateSource::Derived)))
);
assert_eq!(
hidden_of("Plain"),
Some((false, Some(StateSource::Derived)))
);
}
fn modal_tree() -> SemanticTree {
let mut dialog = annotation(Some(Role::Dialog), Some("Delete Buy milk?"));
dialog.states = vec![(StateName::Modal, StateValue::Bool(true))];
dialog.owns = vec![RawKey::Name("help".to_owned())];
let mut panel = annotation(Some(Role::Menu), Some("Help"));
panel.key = Some(RawKey::Name("help".to_owned()));
infer(&window(vec![
annotated(
annotation(Some(Role::Toolbar), Some("Tools")),
rect(0.0, 0.0, 800.0, 40.0),
vec![],
),
annotated(
dialog,
rect(200.0, 100.0, 400.0, 200.0),
vec![annotated(
annotation(Some(Role::Button), Some("Delete")),
rect(220.0, 250.0, 80.0, 30.0),
vec![],
)],
),
annotated(panel, rect(600.0, 0.0, 100.0, 100.0), vec![]),
RawNode::new(RawCall::Overlay { depth: 1 }, rect(0.0, 0.0, 800.0, 600.0)).with_children(
vec![annotated(
annotation(Some(Role::Tooltip), Some("Tip")),
rect(0.0, 0.0, 50.0, 20.0),
vec![],
)],
),
]))
}
#[test]
fn derive_inert_outside_the_topmost_modal() {
let tree = modal_tree();
let inert = |role: Role| only(&tree, role).states.inert;
assert!(inert(Role::Toolbar));
assert!(!inert(Role::Dialog));
assert!(!inert(Role::Button), "the modal's descendants stay live");
assert!(!inert(Role::Menu), "an owned panel stays live");
assert!(
!inert(Role::Tooltip),
"an overlay above the modal stays live"
);
assert!(!inert(Role::Window));
assert_eq!(
only(&tree, Role::Toolbar).states.source(StateName::Inert),
Some(StateSource::Derived)
);
}
#[test]
fn derive_inert_does_nothing_without_a_modal() {
let tree = infer(&window(vec![annotated(
annotation(Some(Role::Dialog), Some("Popover")),
rect(0.0, 0.0, 10.0, 10.0),
vec![],
)]));
assert!(tree.iter().all(|n| !n.states.inert));
}
#[test]
fn resolve_keys_fills_owner_and_reports_unresolved_keys() {
let tree = modal_tree();
let dialog = only(&tree, Role::Dialog);
let menu = only(&tree, Role::Menu);
assert_eq!(dialog.relations.owns, [menu.id]);
assert_eq!(menu.relations.owner, Some(dialog.id));
let mut lost = annotation(Some(Role::Button), Some("Open"));
lost.controls = vec![RawKey::Name("nowhere".to_owned())];
let tree = infer(&window(vec![annotated(
lost,
rect(0.0, 0.0, 10.0, 10.0),
vec![],
)]));
assert!(only(&tree, Role::Button).relations.controls.is_empty());
assert!(matches!(
tree.warnings(),
[TreeWarning::UnresolvedKey { key, at }]
if key == "nowhere" && at.to_string() == "window \"Test\" / button \"Open\""
));
}
#[test]
fn owns_cycles_are_dropped() {
let mut a = annotation(Some(Role::Button), Some("A"));
a.key = Some(RawKey::Name("a".to_owned()));
a.owns = vec![RawKey::Name("b".to_owned())];
let mut b = annotation(Some(Role::Menu), Some("B"));
b.key = Some(RawKey::Name("b".to_owned()));
b.owns = vec![RawKey::Name("a".to_owned())];
let tree = infer(&window(vec![
annotated(a, rect(0.0, 0.0, 10.0, 10.0), vec![]),
annotated(b, rect(0.0, 20.0, 10.0, 10.0), vec![]),
]));
assert_eq!(only(&tree, Role::Button).relations.owns.len(), 1);
assert!(only(&tree, Role::Menu).relations.owns.is_empty());
assert!(matches!(tree.warnings(), [TreeWarning::OwnsCycle { .. }]));
}
#[test]
fn invalid_levels_test_ids_and_unknown_roles_warn() {
let mut heading = annotation(Some(Role::Heading), Some("Title"));
heading.level = Some(0);
let mut row = annotation(Some(Role::ListItem), Some("Row"));
row.test_id = Some("row 3".to_owned());
let tree = infer(&window(vec![
annotated(heading, rect(0.0, 0.0, 10.0, 10.0), vec![]),
annotated(row, rect(0.0, 10.0, 10.0, 10.0), vec![]),
annotated(
annotation(Some(Role::Unknown), Some("Future")),
rect(0.0, 20.0, 10.0, 10.0),
vec![],
),
]));
assert_eq!(only(&tree, Role::Heading).set.level, None);
assert!(matches!(
tree.warnings(),
[
TreeWarning::InvalidLevel { .. },
TreeWarning::InvalidTestId { id, .. },
TreeWarning::UnknownRole { .. },
] if id == "row 3"
));
}
#[test]
fn annotation_states_carry_their_source() {
let mut checkbox = annotation(Some(Role::CheckBox), Some("Show completed"));
checkbox.states = vec![(StateName::Checked, StateValue::Mixed)];
let tree = infer(&window(vec![annotated(
checkbox,
rect(0.0, 0.0, 10.0, 10.0),
vec![],
)]));
let node = only(&tree, Role::CheckBox);
assert_eq!(node.states.checked, Some(Checked::Mixed));
assert_eq!(
node.states.source(StateName::Checked),
Some(StateSource::Annotation)
);
assert_eq!(node.states.source(StateName::Disabled), None);
assert_eq!(node.states.disabled, None);
}
fn named(tree: &SemanticTree, role: Role) -> (Option<String>, NameSource) {
let node = only(tree, role);
(node.name.clone(), node.name_source)
}
#[test]
fn names_follow_the_order() {
let mut labelled = annotation(Some(Role::TextBox), None);
labelled.labelled_by = vec![RawKey::Name("l1".to_owned()), RawKey::Name("l2".to_owned())];
let mut label_one = annotation(Some(Role::Text), None);
label_one.key = Some(RawKey::Name("l1".to_owned()));
let mut label_two = annotation(Some(Role::Text), None);
label_two.key = Some(RawKey::Name("l2".to_owned()));
let tree = infer(&window(vec![
annotated(
label_one,
rect(0.0, 0.0, 50.0, 10.0),
vec![text("Server", rect(0.0, 0.0, 50.0, 10.0))],
),
annotated(
label_two,
rect(0.0, 10.0, 50.0, 10.0),
vec![text("address", rect(0.0, 10.0, 50.0, 10.0))],
),
annotated(labelled, rect(60.0, 0.0, 100.0, 20.0), vec![]),
]));
assert_eq!(
named(&tree, Role::TextBox),
(Some("Server address".to_owned()), NameSource::LabelledBy)
);
let mut field_label = annotation(Some(Role::Text), None);
field_label.label_for = vec![RawKey::Name("f".to_owned())];
let mut field = annotation(Some(Role::SearchBox), None);
field.key = Some(RawKey::Name("f".to_owned()));
let tree = infer(&window(vec![
annotated(
field_label,
rect(0.0, 0.0, 50.0, 10.0),
vec![text("Find", rect(0.0, 0.0, 50.0, 10.0))],
),
annotated(field, rect(60.0, 0.0, 100.0, 20.0), vec![]),
]));
assert_eq!(
named(&tree, Role::SearchBox),
(Some("Find".to_owned()), NameSource::LabelFor)
);
let mut icon = annotation(Some(Role::Button), None);
icon.tooltip = Some("New item".to_owned());
let tree = infer(&window(vec![annotated(
icon,
rect(0.0, 0.0, 20.0, 20.0),
vec![],
)]));
assert_eq!(
named(&tree, Role::Button),
(Some("New item".to_owned()), NameSource::Tooltip)
);
assert_eq!(only(&tree, Role::Button).description, None);
let mut input = annotation(Some(Role::TextBox), None);
input.placeholder = Some("Search".to_owned());
let tree = infer(&window(vec![annotated(
input,
rect(0.0, 0.0, 20.0, 20.0),
vec![],
)]));
assert_eq!(
named(&tree, Role::TextBox),
(Some("Search".to_owned()), NameSource::Placeholder)
);
let tree = infer(&window(vec![RawNode::new(
RawCall::TextInput {
text: "Search".to_owned(),
},
rect(0.0, 0.0, 20.0, 20.0),
)]));
assert_eq!(
named(&tree, Role::TextBox),
(None, NameSource::None),
"visible text is no name"
);
let mut both = annotation(Some(Role::Button), Some("Explicit"));
both.tooltip = Some("Tip".to_owned());
let tree = infer(&window(vec![annotated(
both,
rect(0.0, 0.0, 20.0, 20.0),
vec![],
)]));
assert_eq!(
named(&tree, Role::Button),
(Some("Explicit".to_owned()), NameSource::Annotation)
);
assert_eq!(
only(&tree, Role::Button).description.as_deref(),
Some("Tip")
);
}
#[test]
fn content_names_join_visible_descendant_text_and_skip_hidden() {
let row = annotated(
annotation(Some(Role::ListItem), None),
rect(0.0, 0.0, 300.0, 30.0),
vec![
text("Buy", rect(0.0, 0.0, 30.0, 30.0)),
text("never seen", rect(0.0, 0.0, 0.0, 0.0)),
container(
rect(40.0, 0.0, 100.0, 30.0),
vec![text(" milk ", rect(40.0, 0.0, 30.0, 30.0))],
),
],
);
let group = annotated(
annotation(Some(Role::Group), None),
rect(0.0, 40.0, 300.0, 30.0),
vec![text("Not a name", rect(0.0, 40.0, 30.0, 30.0))],
);
let tree = infer(&window(vec![row, group]));
assert_eq!(
named(&tree, Role::ListItem),
(Some("Buy milk".to_owned()), NameSource::Content)
);
assert_eq!(named(&tree, Role::Group), (None, NameSource::None));
let item = only(&tree, Role::ListItem).id;
assert_eq!(tree.text_content(item), "Buy milk");
}
#[test]
fn labelled_by_cycles_are_safe() {
let mut a = annotation(Some(Role::TextBox), None);
a.key = Some(RawKey::Name("a".to_owned()));
a.labelled_by = vec![RawKey::Name("b".to_owned())];
let mut b = annotation(Some(Role::SearchBox), None);
b.key = Some(RawKey::Name("b".to_owned()));
b.labelled_by = vec![RawKey::Name("a".to_owned())];
let tree = infer(&window(vec![
annotated(
a,
rect(0.0, 0.0, 10.0, 10.0),
vec![text("A text", rect(0.0, 0.0, 10.0, 10.0))],
),
annotated(
b,
rect(0.0, 20.0, 10.0, 10.0),
vec![text("B text", rect(0.0, 20.0, 10.0, 10.0))],
),
]));
assert_eq!(named(&tree, Role::TextBox).0.as_deref(), Some("B text"));
assert_eq!(named(&tree, Role::SearchBox).0.as_deref(), Some("A text"));
}
#[test]
fn described_by_and_annotation_fields_arrive() {
let mut hint = annotation(Some(Role::Text), None);
hint.key = Some(RawKey::Name("hint".to_owned()));
let mut field = annotation(Some(Role::TextBox), Some("Email"));
field.described_by = vec![RawKey::Name("hint".to_owned())];
field.text_size = Some(14.0);
field.carets = vec![(3, Point::new(9.0, 0.0)), (0, Point::new(0.0, 0.0))];
field.props.insert("unsaved".to_owned(), "true".to_owned());
field.cursor_seen = Some(icedwright_types::CursorSeen::Levitating);
field.position = Some((2, 5));
let tree = infer(&window(vec![
annotated(
hint,
rect(0.0, 0.0, 10.0, 10.0),
vec![text("We never share it", rect(0.0, 0.0, 10.0, 10.0))],
),
annotated(field, rect(0.0, 20.0, 10.0, 10.0), vec![]),
]));
let node = only(&tree, Role::TextBox);
assert_eq!(node.description.as_deref(), Some("We never share it"));
assert_eq!(node.text_size, Some(14.0));
let caret = node.text_geometry.as_ref().and_then(|g| g.caret(3));
assert_eq!(caret, Some(Point::new(9.0, 0.0)));
assert_eq!(node.props.get("unsaved").map(String::as_str), Some("true"));
assert_eq!(
node.cursor_seen,
Some(icedwright_types::CursorSeen::Levitating)
);
assert_eq!((node.set.position, node.set.size), (Some(2), Some(5)));
}
#[test]
fn paths_use_indices_test_ids_and_skip_generics() {
let mut list = annotation(Some(Role::List), None);
list.test_id = Some("todo-list".to_owned());
let item = |y: f32, name: &str| {
annotated(
annotation(Some(Role::ListItem), None),
rect(0.0, y, 300.0, 30.0),
vec![text(name, rect(0.0, y, 30.0, 30.0))],
)
};
let tree = infer(&window(vec![container(
rect(0.0, 0.0, 400.0, 400.0),
vec![annotated(
list,
rect(0.0, 0.0, 400.0, 300.0),
vec![
item(0.0, "Buy milk"),
item(40.0, "Buy milk"),
item(80.0, "Walk"),
],
)],
)]));
let paths: Vec<String> = tree
.iter()
.filter(|n| n.role == Role::ListItem)
.map(|n| n.path.to_string())
.collect();
assert_eq!(
paths,
[
"window \"Test\" / #todo-list / listitem \"Buy milk\"",
"window \"Test\" / #todo-list / listitem \"Buy milk\"[1]",
"window \"Test\" / #todo-list / listitem \"Walk\"",
]
);
}
#[test]
fn owned_subtrees_print_under_their_owner() {
let tree = modal_tree();
assert_eq!(
only(&tree, Role::Menu).path.to_string(),
"window \"Test\" / dialog \"Delete Buy milk?\" / menu \"Help\""
);
let below: Vec<Role> = tree
.descendants(only(&tree, Role::Dialog).id)
.filter_map(|id| tree.get(id).map(|n| n.role))
.collect();
assert_eq!(below, [Role::Button, Role::Menu], "descendants follow owns");
}
#[test]
fn unnamed_text_boxes_keep_their_path_when_typed_into() {
let path_with = |typed: &str| {
let tree = infer(&window(vec![
RawNode::new(
RawCall::TextInput {
text: typed.to_owned(),
},
rect(0.0, 0.0, 100.0, 20.0),
),
RawNode::new(
RawCall::TextInput {
text: String::new(),
},
rect(0.0, 30.0, 100.0, 20.0),
),
]));
tree.iter()
.filter(|n| n.role == Role::TextBox)
.map(|n| n.path.to_string())
.collect::<Vec<_>>()
};
assert_eq!(path_with("Placeholder"), path_with("Typed text"));
assert_eq!(path_with("x")[1], "window \"Test\" / textbox[1]");
}
#[test]
fn window_roots_carry_the_title_and_window_id() {
let tree = infer(&window(vec![]));
let root = tree.roots().first().and_then(|id| tree.get(*id));
assert_eq!(root.map(|n| n.name.as_deref()), Some(Some("Test")));
assert_eq!(tree.window(WindowId(0)), tree.roots().first().copied());
assert_eq!(tree.window(WindowId(9)), None);
assert_eq!(
root.map(|n| n.path.to_string()).as_deref(),
Some("window \"Test\"")
);
}
#[test]
fn raw_paths_map_back_to_nodes() {
let bounds = rect(10.0, 10.0, 80.0, 30.0);
let tree = infer(&window(vec![
text("First", rect(0.0, 0.0, 10.0, 10.0)),
annotated(
annotation(Some(Role::Button), Some("Go")),
bounds,
vec![container(
bounds,
vec![text("Go", rect(20.0, 15.0, 20.0, 10.0))],
)],
),
]));
let button = only(&tree, Role::Button).id;
assert_eq!(tree.by_raw(&[0, 1]), Some(button));
assert_eq!(
tree.by_raw(&[0, 1, 0]),
Some(button),
"the absorbed container"
);
let text_id = tree.by_raw(&[0, 1, 0, 0]);
assert_eq!(
text_id.and_then(|id| tree.get(id)).map(|n| n.role),
Some(Role::Text)
);
assert_eq!(
tree.by_raw(&[0, 0])
.and_then(|id| tree.get(id))
.and_then(|n| n.name.clone())
.as_deref(),
Some("First")
);
assert_eq!(tree.by_raw(&[3]), None);
let _: Option<NodeId> = tree.by_raw(&[0]);
}
#[test]
fn a_round_tripped_recording_infers_the_same_tree() -> Result<(), serde_json::Error> {
let raw = window(vec![
annotated(
annotation(Some(Role::Dialog), Some("Confirm")),
rect(0.0, 0.0, 100.0, 100.0),
vec![text("Sure?", rect(0.0, 0.0, 50.0, 10.0))],
),
focusable(true, 0, rect(0.0, 200.0, 100.0, 100.0)),
]);
let json = serde_json::to_string(&raw)?;
let back: RawTree = serde_json::from_str(&json)?;
assert_eq!(infer(&back), infer(&raw));
let tree = infer(&raw);
let tree_json = serde_json::to_string(&tree)?;
assert_eq!(serde_json::from_str::<SemanticTree>(&tree_json)?, tree);
Ok(())
}