1
use super::*;
2
use crate::spec::{ChartKind, Series, SeriesPoint};
3

            
4
/// The SVG test cannot cover this: SVGBackend emits `<text>` nodes and uses
5
/// the font only for layout, while BitMapBackend RASTERISES glyphs through
6
/// ab_glyph. The bars alone leave opaque pixels, so compare against the same
7
/// chart with an empty title: the caption's glyphs must add some.
8
#[test]
9
1
fn render_png_rasterises_label_glyphs() {
10
2
    let opaque = |spec: &ChartSpec| -> usize {
11
2
        let png = render_png(spec, KittyOpts::default())
12
2
            .expect("render_png returned None — drawing failed, not merely unstyled");
13
2
        let img = image::load_from_memory(&png).expect("not a decodable PNG");
14
512000
        img.to_rgba8().pixels().filter(|p| p.0[3] > 0xc0).count()
15
2
    };
16
1
    let titled = ChartSpec {
17
1
        title: "A long chart title that needs many glyphs".to_string(),
18
1
        ..sample(ChartKind::Bar)
19
1
    };
20
1
    let untitled = ChartSpec {
21
1
        title: String::new(),
22
1
        ..sample(ChartKind::Bar)
23
1
    };
24
1
    let (with, without) = (opaque(&titled), opaque(&untitled));
25
1
    assert!(
26
1
        with > without + 100,
27
        "caption added {} opaque pixels — its glyphs were not rasterised",
28
        with.saturating_sub(without)
29
    );
30
1
}
31

            
32
8
fn sample(kind: ChartKind) -> ChartSpec {
33
8
    ChartSpec {
34
8
        title: "Test".to_string(),
35
8
        kind,
36
8
        x_label: "X".to_string(),
37
8
        y_label: "Y".to_string(),
38
8
        series: vec![Series {
39
8
            label: "A".to_string(),
40
8
            commodity_symbol: "USD".to_string(),
41
8
            points: vec![
42
8
                SeriesPoint {
43
8
                    x: "Jan".to_string(),
44
8
                    y_num: 100,
45
8
                    y_denom: 1,
46
8
                },
47
8
                SeriesPoint {
48
8
                    x: "Feb".to_string(),
49
8
                    y_num: 200,
50
8
                    y_denom: 1,
51
8
                },
52
8
            ],
53
8
        }],
54
8
        notes: vec![],
55
8
    }
56
8
}
57

            
58
#[test]
59
1
fn render_png_produces_valid_png_magic_bytes() {
60
1
    let png = render_png(
61
1
        &sample(ChartKind::Line),
62
1
        KittyOpts {
63
1
            width_px: 320,
64
1
            height_px: 200,
65
1
            ..KittyOpts::default()
66
1
        },
67
1
    )
68
1
    .expect("bitmap render should succeed");
69
1
    assert_eq!(
70
1
        &png[..8],
71
1
        &[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a],
72
        "PNG magic bytes mismatch"
73
    );
74
1
}
75

            
76
#[test]
77
1
fn render_png_has_no_background() {
78
1
    let png = render_png(&sample(ChartKind::Bar), KittyOpts::default()).expect("render");
79
1
    let img = image::load_from_memory(&png).expect("png").to_rgba8();
80
1
    let last = (img.width() - 1, img.height() - 1);
81
1
    assert_eq!(img.get_pixel(0, 0).0[3], 0, "top-left corner is painted");
82
1
    assert_eq!(
83
1
        img.get_pixel(last.0, last.1).0[3],
84
1
        0,
85
        "bottom-right corner is painted"
86
    );
87
1
}
88

            
89
#[test]
90
1
fn render_png_on_a_background_is_opaque_in_that_colour() {
91
1
    let opts = KittyOpts {
92
1
        background: Some([0x00, 0x00, 0xaa]),
93
1
        ..KittyOpts::default()
94
1
    };
95
1
    let png = render_png(&sample(ChartKind::Bar), opts).expect("render");
96
1
    let img = image::load_from_memory(&png).expect("png").to_rgba8();
97
1
    let last = (img.width() - 1, img.height() - 1);
98
1
    assert_eq!(img.get_pixel(0, 0).0, [0x00, 0x00, 0xaa, 0xff]);
99
1
    assert_eq!(img.get_pixel(last.0, last.1).0, [0x00, 0x00, 0xaa, 0xff]);
100
1
    assert!(
101
256000
        img.pixels().all(|p| p.0[3] == 0xff),
102
        "a pixel is translucent"
103
    );
104
1
}
105

            
106
#[test]
107
1
fn render_png_text_height_grows_the_glyphs() {
108
2
    let ink = |text_px: u32| -> usize {
109
2
        let opts = KittyOpts {
110
2
            text_px,
111
2
            background: Some([0xff; 3]),
112
2
            ..KittyOpts::default()
113
2
        };
114
2
        let titled = ChartSpec {
115
2
            series: vec![],
116
2
            ..sample(ChartKind::Bar)
117
2
        };
118
2
        let png = render_png(&titled, opts).expect("render");
119
2
        let img = image::load_from_memory(&png).expect("png").to_rgba8();
120
512000
        img.pixels().filter(|p| p.0[0] < 0xe0).count()
121
2
    };
122
1
    assert!(ink(22) > ink(11), "larger text left no more ink");
123
1
}
124

            
125
#[test]
126
1
fn ink_contrasts_with_the_background() {
127
1
    assert_eq!(ink(None), NEUTRAL);
128
1
    assert_eq!(ink(Some([0, 0, 0xaa])), RGBColor(235, 235, 235));
129
1
    assert_eq!(ink(Some([0xf0, 0xf0, 0xf0])), RGBColor(30, 30, 30));
130
1
}
131

            
132
#[test]
133
1
fn render_png_on_blue_draws_light_ink_not_grey() {
134
1
    let opts = KittyOpts {
135
1
        background: Some([0, 0, 0xaa]),
136
1
        ..KittyOpts::default()
137
1
    };
138
1
    let titled = ChartSpec {
139
1
        series: vec![],
140
1
        ..sample(ChartKind::Bar)
141
1
    };
142
1
    let png = render_png(&titled, opts).expect("render");
143
1
    let img = image::load_from_memory(&png).expect("png").to_rgba8();
144
1
    assert!(
145
12175
        img.pixels().any(|p| p.0[0] > 0xc8 && p.0[1] > 0xc8),
146
        "no light ink"
147
    );
148
1
}
149

            
150
#[test]
151
1
fn unblend_recovers_colour_and_alpha() {
152
1
    let half_red = unblend(&[255, 128, 128], &[128, 0, 0]);
153
1
    assert_eq!(half_red, [255, 0, 0, 128]);
154
1
    assert_eq!(unblend(&[255; 3], &[0; 3]), [0, 0, 0, 0]);
155
1
    assert_eq!(unblend(&[10, 20, 30], &[10, 20, 30]), [10, 20, 30, 255]);
156
1
}
157

            
158
#[test]
159
1
fn encode_png_apc_round_trips_payload() {
160
1
    let png = b"\x89PNG\r\n\x1a\nsome-fake-payload";
161
1
    let framed = encode_png_apc(png);
162
1
    let payload: String = framed
163
1
        .split("\x1b\\")
164
2
        .filter(|s| !s.is_empty())
165
1
        .map(|chunk| {
166
1
            let semi = chunk.find(';').expect("semicolon separator");
167
1
            &chunk[semi + 1..]
168
1
        })
169
        .collect();
170
1
    let decoded = BASE64.decode(payload).expect("decoded base64");
171
1
    assert_eq!(decoded, png);
172
1
}
173

            
174
#[test]
175
1
fn encode_png_apc_chunks_large_payload() {
176
    // Produce a PNG whose base64 encoding is > 4096 chars so
177
    // chunking logic is exercised.
178
1
    let png: Vec<u8> = (0..4000_u32).flat_map(u32::to_le_bytes).collect();
179
1
    let framed = encode_png_apc(&png);
180
1
    let start_markers = framed.matches("\x1b_G").count();
181
1
    assert!(
182
1
        start_markers >= 2,
183
        "expected at least 2 APC chunks, got {start_markers}"
184
    );
185
1
    assert!(framed.contains("m=1"), "intermediate chunks must set m=1");
186
1
    assert!(framed.contains("m=0"), "final chunk must set m=0");
187
1
}
188

            
189
#[test]
190
1
fn encode_png_apc_handles_empty_input() {
191
1
    assert!(encode_png_apc(&[]).is_empty());
192
1
}