use taim_core::widget::tree::{self, DirtyFlags, Revision, Tree};
fn propagate_from_children(parent: &mut Tree) -> bool {
let child_dirty = parent
.children
.iter()
.fold(DirtyFlags::empty(), |acc, child| acc | child.dirty());
if child_dirty.is_empty() {
return false;
}
let before = parent.dirty();
parent.mark_dirty(child_dirty);
parent.dirty() != before
}
#[test]
fn a_fresh_tree_and_its_children_start_fully_dirty() {
let mut tree = Tree::empty();
tree.children.push(Tree::empty());
tree.children.push(Tree::empty());
assert_eq!(tree.dirty(), DirtyFlags::all());
for child in &tree.children {
assert_eq!(child.dirty(), DirtyFlags::all());
}
}
#[test]
fn clearing_a_childs_flags_does_not_affect_the_parent() {
let mut tree = Tree::empty();
tree.children.push(Tree::empty());
tree.clear_dirty(DirtyFlags::all());
tree.children[0].clear_dirty(DirtyFlags::LAYOUT);
assert_eq!(tree.dirty(), DirtyFlags::empty());
assert_eq!(
tree.children[0].dirty(),
DirtyFlags::PAINT | DirtyFlags::SEMANTICS
);
}
#[test]
fn propagation_only_occurs_when_a_child_is_actually_dirty() {
let mut tree = Tree::empty();
tree.children.push(Tree::empty());
tree.clear_dirty(DirtyFlags::all());
tree.children[0].clear_dirty(DirtyFlags::all());
assert!(!propagate_from_children(&mut tree));
assert_eq!(tree.dirty(), DirtyFlags::empty());
}
#[test]
fn propagation_carries_the_narrowest_flags_that_are_actually_dirty() {
let mut tree = Tree::empty();
tree.children.push(Tree::empty());
tree.clear_dirty(DirtyFlags::all());
tree.children[0].clear_dirty(DirtyFlags::all());
tree.children[0].mark_dirty(DirtyFlags::PAINT);
assert!(propagate_from_children(&mut tree));
assert_eq!(tree.dirty(), DirtyFlags::PAINT);
}
#[test]
fn propagation_from_multiple_children_unions_their_flags() {
let mut tree = Tree::empty();
tree.children.push(Tree::empty());
tree.children.push(Tree::empty());
tree.clear_dirty(DirtyFlags::all());
tree.children[0].clear_dirty(DirtyFlags::all());
tree.children[0].mark_dirty(DirtyFlags::LAYOUT);
tree.children[1].clear_dirty(DirtyFlags::all());
tree.children[1].mark_dirty(DirtyFlags::SEMANTICS);
assert!(propagate_from_children(&mut tree));
assert_eq!(tree.dirty(), DirtyFlags::LAYOUT | DirtyFlags::SEMANTICS);
assert!(!tree.dirty().contains(DirtyFlags::PAINT));
}
#[test]
fn mark_dirty_always_advances_the_revision_even_with_empty_flags() {
let mut tree = Tree::empty();
let before = tree.revision();
tree.mark_dirty(DirtyFlags::empty());
assert!(tree.revision().wrapping_after(before));
}
#[test]
fn revision_wrapping_after_is_the_only_correct_way_to_order_revisions() {
let mut tree = Tree::empty();
let first = tree.revision();
tree.mark_dirty(tree::DirtyFlags::LAYOUT);
let second = tree.revision();
tree.mark_dirty(tree::DirtyFlags::PAINT);
let third = tree.revision();
assert!(second.wrapping_after(first));
assert!(third.wrapping_after(second));
assert!(third.wrapping_after(first));
assert!(!first.wrapping_after(second));
}
#[test]
fn revision_starts_at_the_documented_initial_value_for_every_new_node() {
let a = Tree::empty();
let b = Tree::empty();
assert_eq!(a.revision(), Revision::INITIAL);
assert_eq!(a.revision(), b.revision());
}