Skip to content

Commit

Permalink
Add some render tests for image
Browse files Browse the repository at this point in the history
  • Loading branch information
fishrockz committed Mar 31, 2020
1 parent db774e5 commit 9f7462b
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions druid/src/widget/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,140 @@ impl Default for ImageData {
ImageData::empty()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn tall_paint() {
use crate::{tests::harness::Harness, WidgetId};

let _id_1 = WidgetId::next();
let image_data = ImageData {
pixels: vec![255, 255, 255, 0, 0, 0, 0, 0, 0, 255, 255, 255],
x_pixels: 2,
y_pixels: 2,
format: ImageFormat::Rgb,
};

let image_widget =
Image::new(image_data).interpolation_mode(InterpolationMode::NearestNeighbor);

Harness::create_full(
true,
image_widget,
Size::new(400., 600.),
|harness| {
harness.send_initial_events();
harness.just_layout();
harness.paint();
},
|target| {
let raw_pixels = target.into_raw();
assert_eq!(raw_pixels.len(), 400 * 600 * 4);

// being a tall widget with a square image the top and bottom rows will be
// the padding collor and the middle rows will not have any padding

// check that the middle row 400 pix wide is 200 black then 200 wight
let expecting: Vec<u8> = [
vec![0, 0, 0, 255].repeat(200),
vec![255, 255, 255, 255].repeat(200),
]
.concat();
assert_eq!(raw_pixels[400 * 300 * 4..400 * 301 * 4], expecting[..]);

// check that the hole of the last 100 rows are all the back ground color.
let expecting: Vec<u8> = vec![41, 41, 41, 255].repeat(400 * 100);
assert_eq!(
raw_pixels[400 * 600 * 4 - 4 * 400 * 100..400 * 600 * 4],
expecting[..]
);
},
)
}
#[test]
fn wide_paint() {
use crate::{tests::harness::Harness, WidgetId};
let _id_1 = WidgetId::next();
let image_data = ImageData {
pixels: vec![255, 255, 255, 0, 0, 0, 0, 0, 0, 255, 255, 255],
x_pixels: 2,
y_pixels: 2,
format: ImageFormat::Rgb,
};

let image_widget =
Image::new(image_data).interpolation_mode(InterpolationMode::NearestNeighbor);

Harness::create_full(
true,
image_widget,
Size::new(600., 400.),
|harness| {
harness.send_initial_events();
harness.just_layout();
harness.paint();
},
|target| {
let raw_pixels = target.into_raw();
assert_eq!(raw_pixels.len(), 400 * 600 * 4);

// being a wide widget every row will have some padding at the start and end
// the last row will be like this too and there will be now padding rows at the end.

// a middle row of 600 pixels is 100 padding 200 black, 200 wide and then 100 padding
let expecting: Vec<u8> = [
vec![41, 41, 41, 255].repeat(100),
vec![255, 255, 255, 255].repeat(200),
vec![0, 0, 0, 255].repeat(200),
vec![41, 41, 41, 255].repeat(100),
]
.concat();
assert_eq!(raw_pixels[199 * 600 * 4..200 * 600 * 4], expecting[..]);

// the final row of 600 pixels is 100 padding 200 black, 200 wide and then 100 padding
let expecting: Vec<u8> = [
vec![41, 41, 41, 255].repeat(100),
vec![0, 0, 0, 255].repeat(200),
vec![255, 255, 255, 255].repeat(200),
vec![41, 41, 41, 255].repeat(100),
]
.concat();
assert_eq!(raw_pixels[399 * 600 * 4..400 * 600 * 4], expecting[..]);
},
);
}
#[test]
fn to_png() {
use crate::{
tests::{harness::Harness, image_path_for_test},
WidgetId,
};
let _id_1 = WidgetId::next();
let image_data = ImageData {
pixels: vec![255, 255, 255, 0, 0, 0, 0, 0, 0, 255, 255, 255],
x_pixels: 2,
y_pixels: 2,
format: ImageFormat::Rgb,
};

let image_widget =
Image::new(image_data).interpolation_mode(InterpolationMode::NearestNeighbor);

Harness::create_full(
true,
image_widget,
Size::new(600., 400.),
|harness| {
harness.send_initial_events();
harness.just_layout();
harness.paint();
},
|target| {
target.into_png(image_path_for_test("to_png")).unwrap();
},
);
}
}

0 comments on commit 9f7462b

Please sign in to comment.