use bevy_ecs::prelude::*;
use windows::core::Result;
use wintf::com::dcomp::*;
use wintf::ecs::{GraphicsCore, VisualGraphics};
fn setup_graphics() -> Result<GraphicsCore> {
GraphicsCore::new()
}
#[test]
fn test_visual_graphics_has_parent_visual_field() -> Result<()> {
let graphics = setup_graphics()?;
let dcomp = graphics.dcomp().expect("dcomp device should exist");
let visual = dcomp.create_visual()?;
let vg = VisualGraphics::new(visual);
assert!(
vg.parent_visual().is_none(),
"Initial parent_visual should be None"
);
Ok(())
}
#[test]
fn test_visual_graphics_new_with_parent() -> Result<()> {
let graphics = setup_graphics()?;
let dcomp = graphics.dcomp().expect("dcomp device should exist");
let parent_visual = dcomp.create_visual()?;
let child_visual = dcomp.create_visual()?;
let vg = VisualGraphics::new_with_parent(child_visual.clone(), Some(parent_visual.clone()));
assert!(vg.parent_visual().is_some(), "parent_visual should be set");
Ok(())
}
#[test]
fn test_visual_graphics_set_parent_visual() -> Result<()> {
let graphics = setup_graphics()?;
let dcomp = graphics.dcomp().expect("dcomp device should exist");
let parent1 = dcomp.create_visual()?;
let parent2 = dcomp.create_visual()?;
let child = dcomp.create_visual()?;
let mut vg = VisualGraphics::new(child);
assert!(vg.parent_visual().is_none());
vg.set_parent_visual(Some(parent1.clone()));
assert!(vg.parent_visual().is_some());
vg.set_parent_visual(Some(parent2.clone()));
assert!(vg.parent_visual().is_some());
vg.set_parent_visual(None);
assert!(vg.parent_visual().is_none());
Ok(())
}
#[test]
fn test_visual_graphics_on_remove_simulation() -> Result<()> {
let graphics = setup_graphics()?;
let dcomp = graphics.dcomp().expect("dcomp device should exist");
let parent = dcomp.create_visual()?;
let child = dcomp.create_visual()?;
parent.add_visual(&child, false, None)?;
let vg = VisualGraphics::new_with_parent(child.clone(), Some(parent.clone()));
if let Some(p) = vg.parent_visual() {
if let Some(v) = vg.visual() {
let _ = p.remove_visual(v);
}
}
let result = parent.remove_visual(&child);
eprintln!("Remove after on_remove: {:?}", result);
Ok(())
}
#[test]
fn test_visual_graphics_ecs_lifecycle() -> Result<()> {
let graphics = setup_graphics()?;
let dcomp = graphics.dcomp().expect("dcomp device should exist");
let mut world = World::new();
let parent_visual = dcomp.create_visual()?;
let parent_vg = VisualGraphics::new(parent_visual.clone());
let parent_entity = world.spawn(parent_vg).id();
let child_visual = dcomp.create_visual()?;
parent_visual.add_visual(&child_visual, false, None)?;
let child_vg =
VisualGraphics::new_with_parent(child_visual.clone(), Some(parent_visual.clone()));
let child_entity = world.spawn(child_vg).id();
let child_vg_ref = world.get::<VisualGraphics>(child_entity).unwrap();
assert!(child_vg_ref.parent_visual().is_some());
world.despawn(child_entity);
assert!(world.get::<VisualGraphics>(parent_entity).is_some());
Ok(())
}