Test that a picture of pixels reaches Slint as wide, as high and in place

The length check of the pixels is in `Pixels::new`, but the Rgba arm of
`convert::image` had no test: a picture drawn with its width and height
swapped passed all 22 tests of voelin-ui. A 3 by 2 picture whose pixels
hold their own column and row is converted, and its Slint size and four of
its pixels are read back (the swap fails the test).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 5.5 committed 2026-10-24 19:43:20 +03:00
1 parent 56ce2cb739
commit f8447ec9f8
1 file changed
+31
+31
View File
@@ -77,3 +77,34 @@ pub(crate) fn model<V, S: for<'a> From<&'a V> + Clone + 'static>(rows: &[V]) ->
pub(crate) fn rows<S: Clone + 'static, V: From<S>>(model: &ModelRc<S>) -> Vec<V> {
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<u8> = (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));
}
}