#![cfg(not(alloc_frugal))]
use rust_widgets::core::{ObjectId, Rect};
use rust_widgets::widget::runtime::{
self, dirty_rects, enable_damage_tracking_if_useful, repaint_mode, should_track_damage,
RepaintMode, AUTO_REPAINT_MIN_PIXELS,
};
use rust_widgets::widget::Widget;
fn mounted_window(width: u32, height: u32) -> ObjectId {
let mut group = rust_widgets::widget::container_widgets::groupbox::GroupBox::new(Rect::new(
0, 0, width, height,
));
group.base_mut().add_child(7);
group.base_mut().request_redraw();
let id = runtime::register(Box::new(group)).expect("registry");
assert!(runtime::set_geometry(id, Rect::new(0, 0, width, height)));
id
}
#[test]
fn a_mounted_window_tracks_damage_without_being_asked() {
let window = mounted_window(1280, 800);
assert_eq!(
repaint_mode(window),
RepaintMode::Adaptive,
"a 1280x800 surface with children must be enabled at mount, not left to the host"
);
runtime::with_widget(window, |widget| widget.request_redraw()).expect("widget");
let damaged = dirty_rects(window);
assert_eq!(damaged.len(), 1, "one control asked, so one region must be damaged");
assert_eq!(
(damaged[0].width, damaged[0].height),
(1280, 800),
"the fixture's own rect is what it damages"
);
runtime::unregister(window);
}
#[test]
fn a_small_surface_is_left_in_full_mode() {
const { assert!(400u64 * 300 < AUTO_REPAINT_MIN_PIXELS) };
let small = mounted_window(400, 300);
assert_eq!(
repaint_mode(small),
RepaintMode::Full,
"400x300 is below the threshold and must keep whole-frame painting"
);
assert!(!should_track_damage(small));
runtime::unregister(small);
}
#[test]
fn a_large_but_silent_surface_is_left_in_full_mode() {
let mut group = rust_widgets::widget::container_widgets::groupbox::GroupBox::new(Rect::new(
0, 0, 1280, 800,
));
group.base_mut().add_child(7);
let id = runtime::register(Box::new(group)).expect("registry");
assert!(runtime::set_geometry(id, Rect::new(0, 0, 1280, 800)));
assert_eq!(repaint_mode(id), RepaintMode::Full, "nothing ever asked to repaint this surface");
assert!(!enable_damage_tracking_if_useful(id));
runtime::unregister(id);
}
#[test]
fn mounting_a_child_into_a_live_window_keeps_the_decision() {
let parent = mounted_window(1280, 800);
assert_eq!(repaint_mode(parent), RepaintMode::Adaptive);
let child = rust_widgets::widget::base_widgets::label::Label::new(
"hello".to_string(),
Rect::new(0, 0, 200, 40),
);
child.request_redraw();
let child = runtime::register(Box::new(child)).expect("registry");
assert!(runtime::set_geometry(child, Rect::new(0, 0, 200, 40)));
assert!(
!should_track_damage(child),
"a small childless leaf must not be enabled even on a large surface"
);
assert_eq!(repaint_mode(parent), RepaintMode::Adaptive, "the container is unchanged");
runtime::unregister(child);
runtime::unregister(parent);
}