summaryrefslogtreecommitdiff
path: root/rsvg-convert/tests/internal_predicates/png.rs
blob: f629b510fb637ea80ff36611648d117bc73d0a14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use png;
use predicates::prelude::*;
use predicates::reflection::{Case, Child, PredicateReflection, Product};
use std::fmt;
use std::io::BufReader;
use std::path::{Path, PathBuf};

use rsvg::surface_utils::shared_surface::{SharedImageSurface, SurfaceType};

use rsvg::test_utils::compare_surfaces::BufferDiff;
use rsvg::test_utils::reference_utils::{surface_from_png, Compare, Deviation, Reference};

/// Checks that the variable of type [u8] can be parsed as a PNG file.
#[derive(Debug)]
pub struct PngPredicate {}

impl PngPredicate {
    pub fn with_size(self: Self, w: u32, h: u32) -> SizePredicate<Self> {
        SizePredicate::<Self> { p: self, w, h }
    }

    pub fn with_contents<P: AsRef<Path>>(self: Self, reference: P) -> ReferencePredicate<Self> {
        let mut path = PathBuf::new();
        path.push(reference);
        ReferencePredicate::<Self> { p: self, path }
    }
}

impl Predicate<[u8]> for PngPredicate {
    fn eval(&self, data: &[u8]) -> bool {
        let decoder = png::Decoder::new(data);
        decoder.read_info().is_ok()
    }

    fn find_case<'a>(&'a self, _expected: bool, data: &[u8]) -> Option<Case<'a>> {
        let decoder = png::Decoder::new(data);
        match decoder.read_info() {
            Ok(_) => None,
            Err(e) => Some(Case::new(Some(self), false).add_product(Product::new("Error", e))),
        }
    }
}

impl PredicateReflection for PngPredicate {}

impl fmt::Display for PngPredicate {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "is a PNG")
    }
}

/// Extends a PngPredicate by a check for a given size of the PNG file.
#[derive(Debug)]
pub struct SizePredicate<PngPredicate> {
    p: PngPredicate,
    w: u32,
    h: u32,
}

impl SizePredicate<PngPredicate> {
    fn eval_info(&self, info: &png::Info) -> bool {
        info.width == self.w && info.height == self.h
    }

    fn find_case_for_info<'a>(&'a self, expected: bool, info: &png::Info) -> Option<Case<'a>> {
        if self.eval_info(info) == expected {
            let product = self.product_for_info(info);
            Some(Case::new(Some(self), false).add_product(product))
        } else {
            None
        }
    }

    fn product_for_info(&self, info: &png::Info) -> Product {
        let actual_size = format!("{} x {}", info.width, info.height);
        Product::new("actual size", actual_size)
    }
}

impl Predicate<[u8]> for SizePredicate<PngPredicate> {
    fn eval(&self, data: &[u8]) -> bool {
        let decoder = png::Decoder::new(data);
        match decoder.read_info() {
            Ok(reader) => self.eval_info(&reader.info()),
            _ => false,
        }
    }

    fn find_case<'a>(&'a self, expected: bool, data: &[u8]) -> Option<Case<'a>> {
        let decoder = png::Decoder::new(data);
        match decoder.read_info() {
            Ok(reader) => self.find_case_for_info(expected, reader.info()),
            Err(e) => Some(Case::new(Some(self), false).add_product(Product::new("Error", e))),
        }
    }
}

impl PredicateReflection for SizePredicate<PngPredicate> {
    fn children<'a>(&'a self) -> Box<dyn Iterator<Item = Child<'a>> + 'a> {
        let params = vec![Child::new("predicate", &self.p)];
        Box::new(params.into_iter())
    }
}

impl fmt::Display for SizePredicate<PngPredicate> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "is a PNG with size {} x {}", self.w, self.h)
    }
}

/// Extends a PngPredicate by a comparison to the contents of a reference file
#[derive(Debug)]
pub struct ReferencePredicate<PngPredicate> {
    p: PngPredicate,
    path: PathBuf,
}

impl ReferencePredicate<PngPredicate> {
    fn diff_acceptable(diff: &BufferDiff) -> bool {
        match diff {
            BufferDiff::DifferentSizes => false,
            BufferDiff::Diff(diff) => !diff.inacceptable(),
        }
    }

    fn diff_surface(&self, surface: &SharedImageSurface) -> Option<BufferDiff> {
        let reference = Reference::from_png(&self.path)
            .unwrap_or_else(|_| panic!("could not open {:?}", self.path));
        if let Ok(diff) = reference.compare(&surface) {
            if !Self::diff_acceptable(&diff) {
                return Some(diff);
            }
        }
        None
    }

    fn find_case_for_surface<'a>(
        &'a self,
        expected: bool,
        surface: &SharedImageSurface,
    ) -> Option<Case<'a>> {
        let diff = self.diff_surface(&surface);
        if diff.is_some() != expected {
            let product = self.product_for_diff(&diff.unwrap());
            Some(Case::new(Some(self), false).add_product(product))
        } else {
            None
        }
    }

    fn product_for_diff(&self, diff: &BufferDiff) -> Product {
        let difference = format!("{}", diff);
        Product::new("images differ", difference)
    }
}

impl Predicate<[u8]> for ReferencePredicate<PngPredicate> {
    fn eval(&self, data: &[u8]) -> bool {
        if let Ok(surface) = surface_from_png(&mut BufReader::new(data)) {
            let surface = SharedImageSurface::wrap(surface, SurfaceType::SRgb).unwrap();
            self.diff_surface(&surface).is_some()
        } else {
            false
        }
    }

    fn find_case<'a>(&'a self, expected: bool, data: &[u8]) -> Option<Case<'a>> {
        match surface_from_png(&mut BufReader::new(data)) {
            Ok(surface) => {
                let surface = SharedImageSurface::wrap(surface, SurfaceType::SRgb).unwrap();
                self.find_case_for_surface(expected, &surface)
            }
            Err(e) => Some(Case::new(Some(self), false).add_product(Product::new("Error", e))),
        }
    }
}

impl PredicateReflection for ReferencePredicate<PngPredicate> {
    fn children<'a>(&'a self) -> Box<dyn Iterator<Item = Child<'a>> + 'a> {
        let params = vec![Child::new("predicate", &self.p)];
        Box::new(params.into_iter())
    }
}

impl fmt::Display for ReferencePredicate<PngPredicate> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "is a PNG that matches the reference {}",
            self.path.display()
        )
    }
}