mod common;
use common::*;
use std::time::Duration;
use taut::{Color, Rect, Ui, gif, png};
const HAND: &[u8] = include_bytes!("pictures/hand.gif");
const MOVING: &[u8] = include_bytes!("pictures/moving.gif");
fn frame(all: &[u8], high: usize, frame: usize) -> (usize, Vec<u32>) {
let (width, pixels) = png::decode(all).unwrap();
(width, pixels[frame * high * width..(frame + 1) * high * width].to_vec())
}
fn shown(ui: &Window, wide: usize, high: usize) -> Vec<u32> {
(0..high).flat_map(|y| ui.screen.pixels[y * ui.screen.width..][..wide].to_vec()).collect()
}
fn on_page(frame: &[u32], page: u32) -> Vec<u32> {
frame.iter().map(|pixel| if pixel >> 24 == 0 { page } else { *pixel }).collect()
}
#[test]
fn a_film_shows_the_frame_that_is_due_and_asks_to_be_run_for_the_next() {
let mut ui = window(1.0);
ui.set_time(Duration::from_secs(5));
let animation = ui.add_animation(HAND).unwrap();
let (page, at) = (ui.theme.bg.0, std::cell::Cell::new(Rect::EMPTY));
let mut build = |ui: &mut Ui| {
at.set(ui.el().animation(animation).sense().show().rect);
ui.el().size(20.0, 20.0).fill(Color::rgb(0x336699)).show();
};
settle(&mut ui, &mut build);
assert_eq!((at.get().w, at.get().h, ui.wake_in()), (16.0, 12.0, Some(Duration::from_millis(70))));
assert_eq!(shown(&ui, 16, 12), on_page(&frame(include_bytes!("pictures/hand.frames.png"), 12, 0).1, page));
let mut time = 5000;
for (round, (frame_shown, shows)) in (0..3).flat_map(|round| [70u64, 100, 300, 50].into_iter().enumerate().map(move |frame| (round, frame))) {
ui.set_time(Duration::from_millis(time));
let changed = ui.run(&mut build);
assert_eq!(shown(&ui, 16, 12), on_page(&frame(include_bytes!("pictures/hand.frames.png"), 12, frame_shown).1, page), "round {round}, frame {frame_shown}");
assert!(changed.is_empty() || (changed.w <= 16.0 && changed.h <= 12.0 && changed.x == 0.0 && changed.y == 0.0), "{changed:?}");
assert_eq!(ui.wake_in(), Some(Duration::from_millis(shows)), "round {round}, frame {frame_shown}");
ui.set_time(Duration::from_millis(time + shows - 1));
assert!(ui.run(&mut build).is_empty() && ui.wake_in() == Some(Duration::from_millis(1)));
time += shows;
}
ui.set_time(Duration::from_millis(time));
ui.run(&mut build);
assert_eq!((ui.wake_in(), shown(&ui, 16, 12)), (None, on_page(&frame(include_bytes!("pictures/hand.frames.png"), 12, 3).1, page)));
ui.set_time(Duration::from_secs(1000));
assert!(ui.run(&mut build).is_empty() && ui.wake_in().is_none());
}
#[test]
fn a_film_is_shown_at_any_size_and_let_go_like_a_picture() {
let mut ui = window(1.0);
let (moving, hand) = (ui.add_animation(MOVING).unwrap(), ui.add_animation(HAND).unwrap());
settle(&mut ui, |ui| _ = ui.el().animation(moving).size(48.0, 32.0).show());
let first = frame(include_bytes!("pictures/moving.frames.png"), 16, 0).1;
assert_eq!(ui.screen.pixels[20 * ui.screen.width + 21], first[10 * 24 + 10]);
let mut both = |ui: &mut Ui| {
ui.el().animation(moving).show();
ui.el().animation(hand).show();
};
settle(&mut ui, &mut both);
assert_eq!(ui.wake_in(), Some(Duration::from_millis(70)));
ui.set_time(Duration::from_millis(180));
ui.run(&mut both);
assert_eq!(ui.wake_in(), Some(Duration::from_millis(20)));
ui.drop_animation(moving);
ui.drop_animation(hand);
let mut after = Rect::EMPTY;
settle(&mut ui, |ui| {
both(ui);
after = ui.el().size(4.0, 4.0).fill(Color::rgb(0x00ff00)).sense().show().rect;
});
assert_eq!((after.y, ui.wake_in()), (0.0, None));
assert!(ui.add_animation(b"GIF89a").is_none() && ui.add_animation(b"no picture at all").is_none());
let still = ui.add_animation(include_bytes!("pictures/still.gif")).unwrap();
settle(&mut ui, |ui| _ = ui.el().animation(still).show());
assert_eq!(ui.wake_in(), None);
}
#[test]
fn the_kinds_of_picture_files_are_told_apart_by_how_they_begin() {
let mut ui = window(1.0);
let (gif, mut at) = (ui.add_picture_file(MOVING).unwrap(), Rect::EMPTY);
settle(&mut ui, |ui| at = ui.el().picture(gif).sense().show().rect);
let (page, first) = (ui.theme.bg.0, gif::decode(MOVING).unwrap());
assert_eq!((at.w, at.h, first.0), (24.0, 16.0, 24));
assert_eq!(shown(&ui, 24, 16), on_page(&first.1, page));
for (webp, read) in
[(&include_bytes!("pictures/lossy.webp")[..], &include_bytes!("pictures/lossy.webp.png")[..]), (include_bytes!("pictures/exact.webp"), include_bytes!("pictures/exact.webp.png"))]
{
let picture = ui.add_picture_file(webp).unwrap();
settle(&mut ui, |ui| at = ui.el().picture(picture).sense().show().rect);
assert_eq!((at.w, at.h, shown(&ui, 61, 45)), (61.0, 45.0, png::decode(read).unwrap().1));
}
let bmp = ui.add_picture_file(include_bytes!("pictures/bmp-24.bmp")).unwrap();
settle(&mut ui, |ui| at = ui.el().picture(bmp).sense().show().rect);
assert!((at.w, at.h) == (23.0, 17.0) && shown(&ui, 23, 17) == png::decode(include_bytes!("pictures/bmp-24.bmp.png")).unwrap().1);
let (icon, wanted) = (ui.add_picture_file(include_bytes!("pictures/icon-png.ico")).unwrap(), png::decode(include_bytes!("pictures/icon-png.ico.png")).unwrap().1);
settle(&mut ui, |ui| at = ui.el().picture(icon).sense().show().rect);
assert!((at.w, at.h) == (23.0, 17.0) && (0..17).all(|y| ui.screen.pixels[y * ui.screen.width + 22] == wanted[y * 23 + 22]));
assert!(ui.add_picture_file(include_bytes!("pictures/plain.png")).is_some() && ui.add_picture_file(include_bytes!("pictures/plain.jpg")).is_some());
let none = [&b"GIF89a, but no more"[..], b"RIFF\x04\0\0\0WEBP", b"BM, but no more", b"\0\0\x01\0\x01\0", b""];
assert!(none.iter().all(|file| ui.add_picture_file(file).is_none()));
}