use super::*;
use crate::ecs::types::{Point, SizeI};
use bevy_ecs::world::World;
use windows_numerics::Matrix3x2;
use crate::ecs::types::Rect;
fn make_global_arrangement(left: f32, top: f32, right: f32, bottom: f32) -> GlobalArrangement {
GlobalArrangement {
transform: Matrix3x2::translation(left, top),
bounds: Rect {
left,
top,
right,
bottom,
},
}
}
#[test]
fn test_hit_test_mode_default() {
assert_eq!(HitTestMode::default(), HitTestMode::Bounds);
}
#[test]
fn test_hit_test_none_constructor() {
let hit_test = HitTest::none();
assert_eq!(hit_test.mode, HitTestMode::None);
}
#[test]
fn test_hit_test_bounds_constructor() {
let hit_test = HitTest::bounds();
assert_eq!(hit_test.mode, HitTestMode::Bounds);
}
#[test]
fn test_hit_test_default() {
let hit_test = HitTest::default();
assert_eq!(hit_test.mode, HitTestMode::Bounds);
}
#[test]
fn test_hit_test_entity_hit_inside() {
let mut world = World::new();
let entity = world
.spawn((
make_global_arrangement(10.0, 20.0, 110.0, 70.0), HitTest::bounds(),
))
.id();
let point = PhysicalPoint::new(50.0, 40.0);
assert!(hit_test_entity(&world, entity, point));
}
#[test]
fn test_hit_test_entity_miss_outside() {
let mut world = World::new();
let entity = world
.spawn((
make_global_arrangement(10.0, 20.0, 110.0, 70.0),
HitTest::bounds(),
))
.id();
let point = PhysicalPoint::new(200.0, 200.0);
assert!(!hit_test_entity(&world, entity, point));
}
#[test]
fn test_hit_test_entity_skip_none_mode() {
let mut world = World::new();
let entity = world
.spawn((
make_global_arrangement(10.0, 20.0, 110.0, 70.0),
HitTest::none(), ))
.id();
let point = PhysicalPoint::new(50.0, 40.0);
assert!(!hit_test_entity(&world, entity, point));
}
#[test]
fn test_hit_test_entity_no_component_defaults_to_bounds() {
let mut world = World::new();
let entity = world
.spawn(make_global_arrangement(10.0, 20.0, 110.0, 70.0))
.id();
let point = PhysicalPoint::new(50.0, 40.0);
assert!(hit_test_entity(&world, entity, point));
}
#[test]
fn test_hit_test_entity_no_global_arrangement() {
let mut world = World::new();
let entity = world.spawn(HitTest::bounds()).id();
let point = PhysicalPoint::new(50.0, 40.0);
assert!(!hit_test_entity(&world, entity, point));
}
#[test]
fn test_hit_test_front_priority() {
let mut world = World::new();
let back = world
.spawn(make_global_arrangement(0.0, 0.0, 100.0, 100.0))
.id();
let front = world
.spawn(make_global_arrangement(20.0, 20.0, 80.0, 80.0))
.id();
let root = world.spawn_empty().id();
world.entity_mut(root).add_children(&[back, front]);
let point = PhysicalPoint::new(50.0, 50.0);
let result = hit_test(&world, root, point);
assert_eq!(result, Some(front));
}
#[test]
fn test_hit_test_back_only() {
let mut world = World::new();
let back = world
.spawn(make_global_arrangement(0.0, 0.0, 100.0, 100.0))
.id();
let front = world
.spawn(make_global_arrangement(50.0, 50.0, 100.0, 100.0))
.id();
let root = world.spawn_empty().id();
world.entity_mut(root).add_children(&[back, front]);
let point = PhysicalPoint::new(10.0, 10.0);
let result = hit_test(&world, root, point);
assert_eq!(result, Some(back));
}
#[test]
fn test_hit_test_none() {
let mut world = World::new();
let entity = world
.spawn(make_global_arrangement(10.0, 10.0, 60.0, 60.0))
.id();
let root = world.spawn_empty().id();
world.entity_mut(root).add_children(&[entity]);
let point = PhysicalPoint::new(200.0, 200.0);
let result = hit_test(&world, root, point);
assert_eq!(result, None);
}
#[test]
fn test_hit_test_skip_none_mode() {
let mut world = World::new();
let back = world
.spawn(make_global_arrangement(0.0, 0.0, 100.0, 100.0))
.id();
let overlay = world
.spawn((
make_global_arrangement(0.0, 0.0, 100.0, 100.0),
HitTest::none(),
))
.id();
let root = world.spawn_empty().id();
world.entity_mut(root).add_children(&[back, overlay]);
let point = PhysicalPoint::new(50.0, 50.0);
let result = hit_test(&world, root, point);
assert_eq!(result, Some(back));
}
#[test]
fn test_hit_test_child_priority_over_parent() {
let mut world = World::new();
let parent = world
.spawn(make_global_arrangement(0.0, 0.0, 100.0, 100.0))
.id();
let child = world
.spawn(make_global_arrangement(10.0, 10.0, 90.0, 90.0))
.id();
world.entity_mut(parent).add_children(&[child]);
let point = PhysicalPoint::new(50.0, 50.0);
let result = hit_test(&world, parent, point);
assert_eq!(result, Some(child));
}
#[test]
fn test_hit_test_in_window_coordinate_conversion() {
let mut world = World::new();
let window = world
.spawn((
make_global_arrangement(100.0, 200.0, 500.0, 500.0),
WindowPos {
position: Some(Point { x: 100, y: 200 }),
size: Some(SizeI {
width: 400,
height: 300,
}),
..Default::default()
},
))
.id();
let widget = world
.spawn(make_global_arrangement(150.0, 250.0, 250.0, 300.0)) .id();
world.entity_mut(window).add_children(&[widget]);
let client_point = PhysicalPoint::new(50.0, 50.0);
let result = hit_test_in_window(&world, window, client_point);
assert_eq!(result, Some(widget));
}
#[test]
fn test_hit_test_in_window_no_window_pos() {
let mut world = World::new();
let window = world
.spawn(make_global_arrangement(0.0, 0.0, 100.0, 100.0))
.id();
let client_point = PhysicalPoint::new(50.0, 50.0);
let result = hit_test_in_window(&world, window, client_point);
assert_eq!(result, None);
}
#[test]
fn test_hit_test_in_window_position_none() {
let mut world = World::new();
let window = world
.spawn((
make_global_arrangement(0.0, 0.0, 100.0, 100.0),
WindowPos {
position: None, ..Default::default()
},
))
.id();
let client_point = PhysicalPoint::new(50.0, 50.0);
let result = hit_test_in_window(&world, window, client_point);
assert_eq!(result, None);
}