use std::path::PathBuf;
use windows::Win32::Graphics::Imaging::IWICBitmapSource;
use windows::Win32::System::Com::{CoInitializeEx, CoUninitialize, COINIT_MULTITHREADED};
use windows_core::Interface;
use wintf::ecs::widget::bitmap_source::systems::load_bitmap_source;
use wintf::ecs::widget::bitmap_source::WicCore;
fn test_asset_path(filename: &str) -> PathBuf {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
PathBuf::from(manifest_dir)
.join("tests")
.join("assets")
.join(filename)
}
fn with_com_initialized<T, F: FnOnce() -> T>(f: F) -> T {
unsafe {
let _ = CoInitializeEx(None, COINIT_MULTITHREADED);
}
let result = f();
unsafe {
CoUninitialize();
}
result
}
#[test]
fn test_load_png_with_alpha() {
with_com_initialized(|| {
let wic_core = WicCore::new().expect("WicCore creation failed");
let path = test_asset_path("test_8x8_rgba.png");
let result = load_bitmap_source(wic_core.factory(), &path);
assert!(
result.is_ok(),
"PNG loading should succeed: {:?}",
result.err()
);
let source = result.unwrap();
let (width, height) = get_bitmap_size(&source);
assert_eq!(width, 8, "width should be 8");
assert_eq!(height, 8, "height should be 8");
});
}
#[test]
fn test_load_png_16x16() {
with_com_initialized(|| {
let wic_core = WicCore::new().expect("WicCore creation failed");
let path = test_asset_path("test_16x16_rgba.png");
let result = load_bitmap_source(wic_core.factory(), &path);
assert!(
result.is_ok(),
"16x16 PNG loading should succeed: {:?}",
result.err()
);
let source = result.unwrap();
let (width, height) = get_bitmap_size(&source);
assert_eq!(width, 16, "width should be 16");
assert_eq!(height, 16, "height should be 16");
});
}
#[test]
fn test_load_bmp_without_alpha() {
with_com_initialized(|| {
let wic_core = WicCore::new().expect("WicCore creation failed");
let path = test_asset_path("test_8x8_rgb.bmp");
let result = load_bitmap_source(wic_core.factory(), &path);
assert!(
result.is_ok(),
"BMP loading should succeed (PBGRA32 conversion): {:?}",
result.err()
);
let source = result.unwrap();
let (width, height) = get_bitmap_size(&source);
assert_eq!(width, 8, "width should be 8");
assert_eq!(height, 8, "height should be 8");
let format = get_pixel_format(&source);
let expected_format =
windows::core::GUID::from_u128(0x6fddc324_4e03_4bfe_b185_3d77768dc910);
assert_eq!(format, expected_format, "pixel format should be PBGRA32");
});
}
#[test]
fn test_load_invalid_file() {
with_com_initialized(|| {
let wic_core = WicCore::new().expect("WicCore creation failed");
let path = test_asset_path("invalid.bin");
let result = load_bitmap_source(wic_core.factory(), &path);
assert!(result.is_err(), "Invalid file should fail to load");
});
}
#[test]
fn test_load_nonexistent_file() {
with_com_initialized(|| {
let wic_core = WicCore::new().expect("WicCore creation failed");
let path = test_asset_path("nonexistent_file.png");
let result = load_bitmap_source(wic_core.factory(), &path);
assert!(result.is_err(), "Nonexistent file should fail to load");
});
}
fn get_bitmap_size(source: &IWICBitmapSource) -> (u32, u32) {
unsafe {
let mut width = 0u32;
let mut height = 0u32;
source
.GetSize(&mut width, &mut height)
.expect("GetSize failed");
(width, height)
}
}
fn get_pixel_format(source: &IWICBitmapSource) -> windows::core::GUID {
unsafe { source.GetPixelFormat().expect("GetPixelFormat failed") }
}
#[test]
fn test_wic_core_creation_in_integration() {
with_com_initialized(|| {
let result = WicCore::new();
assert!(
result.is_ok(),
"WicCore creation should succeed in integration test"
);
let wic_core = result.unwrap();
assert!(
!wic_core.factory().as_raw().is_null(),
"factory should not be null"
);
});
}
#[test]
fn test_wic_core_clone_in_integration() {
with_com_initialized(|| {
let wic_core = WicCore::new().expect("WicCore creation failed");
let cloned = wic_core.clone();
assert!(
!wic_core.factory().as_raw().is_null(),
"original factory should be valid"
);
assert!(
!cloned.factory().as_raw().is_null(),
"cloned factory should be valid"
);
});
}