1
use super::*;
2
use image::{ImageBuffer, Rgba};
3
use ratatui::Terminal;
4
use ratatui::backend::TestBackend;
5
use ratatui::style::Color;
6

            
7
const CELL: (u16, u16) = (10, 20);
8
const BACKGROUND: Color = Color::Rgb(1, 2, 3);
9

            
10
4
fn striped_png(width: u32, height: u32) -> Vec<u8> {
11
19300
    let image = ImageBuffer::from_fn(width, height, |_, y| {
12
19300
        if y / 10 % 2 == 0 {
13
9700
            Rgba([255u8, 0, 0, 255])
14
        } else {
15
9600
            Rgba([0u8, 0, 255, 255])
16
        }
17
19300
    });
18
4
    let mut bytes = Vec::new();
19
4
    image
20
4
        .write_to(&mut std::io::Cursor::new(&mut bytes), ImageFormat::Png)
21
4
        .expect("png encodes");
22
4
    bytes
23
4
}
24

            
25
8
fn draw(chart: &Image, area: Rect) -> Terminal<TestBackend> {
26
8
    let mut terminal = Terminal::new(TestBackend::new(30, 12)).expect("test backend");
27
8
    terminal
28
8
        .draw(|frame| chart.render(area, frame.buffer_mut(), BACKGROUND))
29
8
        .expect("draws");
30
8
    terminal
31
8
}
32

            
33
#[test]
34
1
fn halfblocks_fill_every_cell_of_the_pane() {
35
1
    let size = Size::new(8, 4);
36
    let chart =
37
1
        Image::new(&striped_png(80, 80), ImageBackend::HalfBlocks, size, CELL).expect("chart");
38
1
    let terminal = draw(&chart, Rect::new(2, 1, 8, 4));
39
1
    let buffer = terminal.backend().buffer();
40
1
    for y in 1..5 {
41
4
        for x in 2..10 {
42
32
            let cell = &buffer[(x, y)];
43
32
            assert_eq!(cell.symbol(), "▀", "({x}, {y})");
44
32
            let (Color::Rgb(fr, _, fb), Color::Rgb(br, _, bb)) = (cell.fg, cell.bg) else {
45
                panic!("true colour expected at ({x}, {y})");
46
            };
47
32
            assert!(fr > fb && bb > br, "red over blue at ({x}, {y})");
48
        }
49
    }
50
1
    assert_eq!(buffer[(1, 1)].symbol(), " ");
51
1
}
52

            
53
#[test]
54
1
fn kitty_writes_the_transmit_sequence_and_placeholders() {
55
1
    let size = Size::new(8, 4);
56
1
    let chart = Image::new(
57
1
        &striped_png(80, 80),
58
1
        ImageBackend::Kitty { id: 7 },
59
1
        size,
60
1
        CELL,
61
1
    )
62
1
    .expect("chart");
63
1
    let terminal = draw(&chart, Rect::new(2, 1, 8, 4));
64
1
    let buffer = terminal.backend().buffer();
65
1
    let first = buffer[(2, 1)].symbol();
66
1
    assert!(first.contains("\x1b_G"), "{first:?}");
67
1
    assert!(first.contains("o=z"), "{first:?}");
68
1
    assert!(first.contains('\u{10EEEE}'), "{first:?}");
69
1
    assert!(buffer[(9, 4)].symbol().contains('\u{10EEEE}'));
70
1
    assert_eq!(buffer[(1, 1)].symbol(), " ");
71
1
}
72

            
73
#[test]
74
1
fn kitty_placeholders_survive_a_second_frame() {
75
1
    let size = Size::new(8, 4);
76
1
    let chart = Image::new(
77
1
        &striped_png(80, 80),
78
1
        ImageBackend::Kitty { id: 7 },
79
1
        size,
80
1
        CELL,
81
1
    )
82
1
    .expect("chart");
83
1
    let mut terminal = draw(&chart, Rect::new(2, 1, 8, 4));
84
1
    terminal
85
1
        .draw(|frame| chart.render(Rect::new(2, 1, 8, 4), frame.buffer_mut(), BACKGROUND))
86
1
        .expect("redraws");
87
1
    let buffer = terminal.backend().buffer();
88
1
    for (x, y) in [(2, 1), (9, 1), (2, 4), (9, 4)] {
89
4
        assert!(buffer[(x, y)].symbol().contains('\u{10EEEE}'), "({x}, {y})");
90
    }
91
1
}
92

            
93
#[test]
94
1
fn a_pane_without_cells_is_an_error() {
95
1
    let result = Image::new(
96
1
        &striped_png(10, 10),
97
1
        ImageBackend::HalfBlocks,
98
1
        Size::new(0, 3),
99
1
        CELL,
100
1
    );
101
1
    assert!(matches!(result, Err(ImageError::NoRoom)));
102
1
}
103

            
104
#[test]
105
1
fn bytes_that_are_not_a_png_are_an_error() {
106
1
    let result = Image::new(
107
1
        b"nope",
108
1
        ImageBackend::Kitty { id: 1 },
109
1
        Size::new(4, 2),
110
1
        CELL,
111
1
    );
112
1
    let err = result.err().expect("error");
113
1
    assert!(matches!(err, ImageError::NotPng(_)), "{err}");
114
1
}
115

            
116
/// An 80x80 PNG: `top` over the upper 40 rows, `bottom` over the lower 40.
117
3
fn two_tone_png(top: Rgba<u8>, bottom: Rgba<u8>) -> Vec<u8> {
118
19200
    let image = ImageBuffer::from_fn(80, 80, |_, y| if y < 40 { top } else { bottom });
119
3
    let mut bytes = Vec::new();
120
3
    image
121
3
        .write_to(&mut std::io::Cursor::new(&mut bytes), ImageFormat::Png)
122
3
        .expect("png encodes");
123
3
    bytes
124
3
}
125

            
126
const RED: Rgba<u8> = Rgba([255, 0, 0, 255]);
127
const BLUE: Rgba<u8> = Rgba([0, 0, 255, 255]);
128
const CLEAR: Rgba<u8> = Rgba([9, 9, 9, 0]);
129

            
130
/// The cells of the top-left 8x4 pane after drawing `png` as halfblocks: (glyph, fg, bg).
131
5
fn halfblock_cells(png: &[u8]) -> Vec<(String, Color, Color)> {
132
5
    let chart = Image::new(png, ImageBackend::HalfBlocks, Size::new(8, 4), CELL).expect("chart");
133
5
    let terminal = draw(&chart, Rect::new(0, 0, 8, 4));
134
5
    let buffer = terminal.backend().buffer();
135
5
    (0..4)
136
160
        .flat_map(|y| (0..8).map(move |x| (x, y)))
137
160
        .map(|(x, y)| {
138
160
            let cell = &buffer[(x, y)];
139
160
            (cell.symbol().to_string(), cell.fg, cell.bg)
140
160
        })
141
        .collect()
142
5
}
143

            
144
#[test]
145
1
fn a_transparent_png_shows_the_background_in_every_cell() {
146
1
    for cell in halfblock_cells(&two_tone_png(CLEAR, CLEAR)) {
147
32
        assert_eq!(cell, (" ".to_string(), BACKGROUND, BACKGROUND));
148
    }
149
1
}
150

            
151
#[test]
152
1
fn an_opaque_png_keeps_its_colours() {
153
1
    let cells = halfblock_cells(&two_tone_png(RED, RED));
154
1
    let red = Color::Rgb(255, 0, 0);
155
1
    for cell in cells {
156
32
        assert_eq!(cell, ("▀".to_string(), red, red));
157
    }
158
1
}
159

            
160
#[test]
161
1
fn a_transparent_bottom_half_leaves_the_background_under_the_top() {
162
    // Cell rows 2 and 3 are the transparent lower half.
163
1
    let cells = halfblock_cells(&two_tone_png(RED, CLEAR));
164
1
    let red = Color::Rgb(255, 0, 0);
165
1
    assert_eq!(cells[0], ("▀".to_string(), red, red));
166
1
    assert_eq!(cells[16], (" ".to_string(), BACKGROUND, BACKGROUND));
167
1
}
168

            
169
#[test]
170
1
fn a_transparent_top_half_draws_the_lower_block() {
171
    // Only the first half row is clear, so the top cell splits mid-cell.
172
6400
    let image = ImageBuffer::from_fn(80, 80, |_, y| if y < 10 { CLEAR } else { BLUE });
173
1
    let mut png = Vec::new();
174
1
    image
175
1
        .write_to(&mut std::io::Cursor::new(&mut png), ImageFormat::Png)
176
1
        .expect("png encodes");
177
1
    let cells = halfblock_cells(&png);
178
1
    assert_eq!(
179
1
        cells[0],
180
1
        ("▄".to_string(), Color::Rgb(0, 0, 255), BACKGROUND)
181
    );
182
1
    assert_eq!(cells[8].0, "▀");
183
1
}
184

            
185
#[test]
186
1
fn translucent_colours_are_rounded_not_truncated() {
187
1
    let image = ImageBuffer::from_pixel(80, 80, Rgba([3u8, 3, 3, 128]));
188
1
    let mut png = Vec::new();
189
1
    image
190
1
        .write_to(&mut std::io::Cursor::new(&mut png), ImageFormat::Png)
191
1
        .expect("png encodes");
192
1
    let grey = Color::Rgb(4, 4, 4);
193
1
    for cell in halfblock_cells(&png) {
194
32
        assert_eq!(cell, ("▀".to_string(), grey, grey));
195
    }
196
1
}
197

            
198
#[test]
199
1
fn kitty_delete_names_the_image_and_frees_it() {
200
1
    assert_eq!(kitty_delete(77), b"\x1b_Ga=d,d=I,i=77,q=2\x1b\\");
201
1
}