tests.rs raw

use super::*;

/// A 2x1 PNG: one opaque red pixel, one half-transparent blue one. Written
/// out by hand so the test depends on no fixture file.
fn tiny_png() -> Vec<u8> {
    let mut bytes = Vec::new();
    let encoder = image::codecs::png::PngEncoder::new(&mut bytes);
    let pixels: [u8; 8] = [255, 0, 0, 255, 0, 0, 255, 128];
    image::ImageEncoder::write_image(encoder, &pixels, 2, 1, image::ExtendedColorType::Rgba8)
        .expect("encodes");
    bytes
}

#[test]
fn decodes_png_to_straight_alpha_rgba() {
    let brush = decode_image(&tiny_png()).expect("decodes");
    assert_eq!((brush.image.width, brush.image.height), (2, 1));
    assert_eq!(brush.image.format, ImageFormat::Rgba8);
    // Straight, not premultiplied: the backends premultiply on their own
    // terms, and saying so wrongly would darken every translucent pixel.
    assert_eq!(brush.image.alpha_type, ImageAlphaType::Alpha);
    assert_eq!(brush.image.data.as_ref(), &[255, 0, 0, 255, 0, 0, 255, 128]);
}

#[test]
fn garbage_is_an_error_not_a_panic() {
    let error = decode_image(b"not an image at all").expect_err("rejects");
    assert!(!error.is_empty());
}