use bevy_ecs::prelude::*;
use bevy_ecs::system::IntoSystem;
use windows::Win32::Foundation::{POINT, SIZE};
use wintf::ecs::*;
#[test]
fn test_sync_window_pos() {
use bevy_ecs::schedule::Schedule;
let mut world = World::new();
world.insert_resource(wintf::ecs::world::FrameCount(1)); let mut schedule = Schedule::default();
schedule.add_systems(wintf::ecs::window_pos_sync_system);
let entity = world
.spawn((Window::default(), Visual::default(), WindowPos::default()))
.id();
world.flush();
world.entity_mut(entity).insert((
layout::Arrangement {
offset: layout::Offset { x: 100.0, y: 50.0 },
scale: layout::LayoutScale::default(),
size: layout::Size {
width: 800.0,
height: 600.0,
},
},
layout::GlobalArrangement {
transform: windows_numerics::Matrix3x2::identity(),
bounds: layout::D2DRect {
left: 100.0,
top: 50.0,
right: 900.0,
bottom: 650.0,
},
},
));
schedule.run(&mut world);
let window_pos = world.entity(entity).get::<WindowPos>().unwrap();
assert_eq!(window_pos.position, Some(POINT { x: 100, y: 50 }));
assert_eq!(window_pos.size, Some(SIZE { cx: 800, cy: 600 }));
}
#[test]
fn test_is_self_initiated_default_false() {
assert!(
!wintf::ecs::is_self_initiated(),
"is_self_initiated() の初期値は false であるべき"
);
}
#[test]
fn test_is_self_initiated_flag_lifecycle() {
assert!(!wintf::ecs::is_self_initiated());
assert!(!wintf::ecs::is_self_initiated());
}
#[test]
fn test_skip_invalid_bounds() {
let mut world = World::new();
let entity = world
.spawn((
Window::default(),
layout::LayoutRoot,
layout::GlobalArrangement {
transform: windows_numerics::Matrix3x2::identity(),
bounds: layout::D2DRect {
left: 0.0,
top: 0.0,
right: 0.0,
bottom: 0.0,
},
},
Visual::default(),
WindowPos {
position: Some(POINT { x: 100, y: 100 }),
size: Some(SIZE { cx: 800, cy: 600 }),
..Default::default()
},
))
.id();
let mut system = IntoSystem::into_system(wintf::ecs::window_pos_sync_system);
system.initialize(&mut world);
let _ = system.run((), &mut world);
system.apply_deferred(&mut world);
let window_pos = world.entity(entity).get::<WindowPos>().unwrap();
assert_eq!(window_pos.position, Some(POINT { x: 100, y: 100 }));
assert_eq!(window_pos.size, Some(SIZE { cx: 800, cy: 600 }));
}
#[test]
fn test_echo_back_flow() {
use bevy_ecs::schedule::Schedule;
let mut world = World::new();
world.insert_resource(wintf::ecs::world::FrameCount(1)); let mut schedule = Schedule::default();
schedule.add_systems(wintf::ecs::window_pos_sync_system);
let entity = world
.spawn((Window::default(), Visual::default(), WindowPos::default()))
.id();
world.flush();
world.entity_mut(entity).insert((
layout::Arrangement {
offset: layout::Offset { x: 100.0, y: 50.0 },
scale: layout::LayoutScale::default(),
size: layout::Size {
width: 800.0,
height: 600.0,
},
},
layout::GlobalArrangement {
transform: windows_numerics::Matrix3x2::identity(),
bounds: layout::D2DRect {
left: 100.0,
top: 50.0,
right: 900.0,
bottom: 650.0,
},
},
));
schedule.run(&mut world);
let window_pos = world.entity(entity).get::<WindowPos>().unwrap();
assert_eq!(window_pos.position, Some(POINT { x: 100, y: 50 }));
assert_eq!(window_pos.size, Some(SIZE { cx: 800, cy: 600 }));
{
let mut entity_mut = world.entity_mut(entity);
let mut window_pos = entity_mut.get_mut::<WindowPos>().unwrap();
let bypass = window_pos.bypass_change_detection();
bypass.position = Some(POINT { x: 100, y: 50 });
bypass.size = Some(SIZE { cx: 800, cy: 600 });
}
let window_pos = world.entity(entity).get::<WindowPos>().unwrap();
assert_eq!(window_pos.position, Some(POINT { x: 100, y: 50 }));
assert_eq!(window_pos.size, Some(SIZE { cx: 800, cy: 600 }));
}
#[test]
fn test_reverse_flow_simulation() {
let mut world = World::new();
let entity = world
.spawn((
Window::default(),
WindowPos {
position: Some(POINT { x: 100, y: 50 }),
size: Some(SIZE { cx: 800, cy: 600 }),
..Default::default()
},
))
.id();
let new_position = POINT { x: 150, y: 100 };
let new_size = SIZE { cx: 1024, cy: 768 };
{
let mut entity_mut = world.entity_mut(entity);
let mut window_pos = entity_mut.get_mut::<WindowPos>().unwrap();
window_pos.position = Some(new_position);
window_pos.size = Some(new_size);
}
let window_pos = world.entity(entity).get::<WindowPos>().unwrap();
assert_eq!(window_pos.position, Some(new_position));
assert_eq!(window_pos.size, Some(new_size));
}
#[test]
fn test_visual_partial_eq_optimization() {
let visual1 = Visual {
opacity: 0.5,
..Default::default()
};
let visual2 = Visual {
opacity: 0.5,
..Default::default()
};
let visual3 = Visual {
opacity: 0.8,
..Default::default()
};
assert_eq!(
visual1, visual2,
"同じ値を持つVisualはPartialEqでtrueを返すべき"
);
assert_ne!(
visual1, visual3,
"異なる値を持つVisualはPartialEqでfalseを返すべき"
);
}