Skip to content

Commit

Permalink
Add regression test for recursive despawn on ui nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
StrikeForceZero committed Mar 30, 2024
1 parent 704f3ef commit 59be93c
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions crates/bevy_ui/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,4 +1134,85 @@ mod tests {
}
}
}

struct DespawnTestEntityReference {
parent_entity: Entity,
child1_entity: Entity,
child2_entity: Entity,
}
fn recursive_despawn_setup() -> (World, Schedule, DespawnTestEntityReference) {
let (mut world, mut ui_schedule) = setup_ui_test_world();


let mut child1_entity = None;
let mut child2_entity = None;
let parent_entity = world.spawn(NodeBundle::default()).with_children(|children| {
child1_entity = Some(children.spawn(NodeBundle::default()).with_children(|children| {
child2_entity = Some(children.spawn(NodeBundle::default()).id());
}).id());
}).id();

ui_schedule.run(&mut world);

let ui_surface = world.get_resource::<UiSurface>().unwrap();
// 1 for root node, 1 for implicit viewport node
// 2 children
assert_eq!(ui_surface.taffy.total_node_count(), 4);

(world, ui_schedule, DespawnTestEntityReference {
parent_entity,
child1_entity: child1_entity.expect("expected child 1"),
child2_entity: child2_entity.expect("expected child 2"),
})
}

#[test]
fn test_recursive_despawn_on_parent() {
let (mut world, mut ui_schedule, DespawnTestEntityReference {
parent_entity,
child1_entity,
child2_entity
}) = recursive_despawn_setup();

let ui_surface = world.get_resource::<UiSurface>().unwrap();

let parent_taffy = *ui_surface.entity_to_taffy.get(&parent_entity).unwrap();
let child1_taffy = *ui_surface.entity_to_taffy.get(&child1_entity).unwrap();
let child2_taffy = *ui_surface.entity_to_taffy.get(&child2_entity).unwrap();
assert_eq!(ui_surface.taffy.parent(child2_taffy), Some(child1_taffy));
assert_eq!(ui_surface.taffy.parent(child1_taffy), Some(parent_taffy));

world.commands().entity(parent_entity).despawn_recursive();

ui_schedule.run(&mut world);

let ui_surface = world.get_resource::<UiSurface>().unwrap();
// all nodes should be removed
assert_eq!(ui_surface.taffy.total_node_count(), 0);
}

#[test]
fn test_recursive_despawn_on_child() {
let (mut world, mut ui_schedule, DespawnTestEntityReference {
parent_entity,
child1_entity,
child2_entity
}) = recursive_despawn_setup();

let ui_surface = world.get_resource::<UiSurface>().unwrap();

let parent_taffy = *ui_surface.entity_to_taffy.get(&parent_entity).unwrap();
let child1_taffy = *ui_surface.entity_to_taffy.get(&child1_entity).unwrap();
let child2_taffy = *ui_surface.entity_to_taffy.get(&child2_entity).unwrap();
assert_eq!(ui_surface.taffy.parent(child2_taffy), Some(child1_taffy));
assert_eq!(ui_surface.taffy.parent(child1_taffy), Some(parent_taffy));

world.commands().entity(child1_entity).despawn_recursive();

ui_schedule.run(&mut world);

let ui_surface = world.get_resource::<UiSurface>().unwrap();
// only root node and implicit viewport left
assert_eq!(ui_surface.taffy.total_node_count(), 2);
}
}

0 comments on commit 59be93c

Please sign in to comment.