use super::*;
use iced::widget::{button, text};
use iced_test::{Simulator, simulator};
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone, PartialEq, Eq)]
enum Msg {
Pressed,
}
#[test]
fn width_reaches_the_closure() {
let captured: Arc<Mutex<Option<f32>>> = Arc::new(Mutex::new(None));
let captured_for_closure = Arc::clone(&captured);
let element = responsive_render(move |width: f32| {
*captured_for_closure.lock().unwrap() = Some(width);
AppLayout::new(text("body").into())
});
let _sim: Simulator<'_, Msg> =
Simulator::with_size(iced::Settings::default(), Size::new(800.0, 600.0), element);
let width = captured
.lock()
.unwrap()
.expect("responsive_render's closure must be called during layout");
assert!(
(0.0..=800.0).contains(&width),
"width should be a plausible fraction of the 800px window, got {width}"
);
}
#[test]
fn composition_matches_render_for_an_equivalent_layout() {
let btn = |label: &'static str| -> Element<'static, Msg> {
button(text(label)).on_press(Msg::Pressed).into()
};
let element = responsive_render(move |_width| AppLayout::new(btn("body")));
let mut sim = simulator(element);
sim.click("body").expect("body button should be reachable through responsive_render, exactly as through render() directly");
let msgs: Vec<Msg> = sim.into_messages().collect();
assert_eq!(
msgs,
vec![Msg::Pressed],
"responsive_render must produce the same interactive z-stack as render()"
);
}