diff --git a/crates/voelin-ui/src/bind/convert.rs b/crates/voelin-ui/src/bind/convert.rs index 61cfd84..9814310 100644 --- a/crates/voelin-ui/src/bind/convert.rs +++ b/crates/voelin-ui/src/bind/convert.rs @@ -77,3 +77,34 @@ pub(crate) fn model From<&'a V> + Clone + 'static>(rows: &[V]) -> pub(crate) fn rows>(model: &ModelRc) -> Vec { slint::Model::iter(model).map(V::from).collect() } + +#[cfg(test)] +mod tests { + use super::*; + + /// A picture of pixels shows as wide and as high as it was made, with + /// every pixel in its place: a width that is not the height tells the two + /// apart, and every pixel holds its column and its row. + #[test] + fn a_picture_of_pixels_keeps_its_size_and_its_places() { + let (w, h) = (3u32, 2u32); + let bytes: Vec = (0..h) + .flat_map(|y| (0..w).flat_map(move |x| [x as u8 + 1, y as u8 + 10, 7, 255])) + .collect(); + let picture = image(&ImageSource::rgba(w, h, bytes)); + let size = picture.size(); + assert_eq!((size.width, size.height), (w, h)); + let pixels = picture.to_rgba8().expect("a picture made of pixels"); + let at = |x: u32, y: u32| { + let p = pixels.as_slice()[(y * w + x) as usize]; + (p.r, p.g, p.b, p.a) + }; + assert_eq!(at(0, 0), (1, 10, 7, 255)); + assert_eq!(at(2, 0), (3, 10, 7, 255)); + assert_eq!(at(0, 1), (1, 11, 7, 255)); + assert_eq!(at(2, 1), (3, 11, 7, 255)); + // Pixels of another length are no picture (and no panic). + let none = image(&ImageSource::rgba(w, h, vec![0u8; 23])); + assert_eq!((none.size().width, none.size().height), (0, 0)); + } +}