waft-test 0.18.1

Test scaffolding for Waft: a platform and a view to run an App under
Documentation
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Image codecs in memory: each answers what the test set, at once, and keeps what the app asked
//! of it, the bytes and files opened, the pictures decoded and the images encoded.

use std::cell::RefCell;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::Arc;

use waft_embedder::image_codecs::{
    AlphaType, HardwareImageCodecs, ImageCodecs, ImageError, ImageErrorKind, ImageFuture,
    ImageMetadata, ImageOrientation, ImagePictures, ImageSource, PixelBuffer, PixelFormat,
    PixelLayout,
};
use waft_embedder::{HardwareBuffer, HardwareBufferFormat, Size};

use crate::MockHardwareBuffer;
use crate::hardware_buffers::SYSTEM_GIVEN;

/// Image codecs that answer what the test set, at once, and keep what the app asked of them,
/// with hardware codecs where the test gives them. Every clone is the same codecs.
#[derive(Clone)]
pub struct MockImageCodecs(Rc<CodecsShared>);

struct CodecsShared {
    answers: RefCell<ImageAnswers>,
    asked: RefCell<Vec<CodecCall>>,
    hardware: Option<MockHardwareImageCodecs>,
}

/// What every image and every encoding answers.
#[derive(Clone, Debug, PartialEq)]
pub struct ImageAnswers {
    pub decoded_formats: Vec<String>,
    pub encoded_formats: Vec<String>,
    /// What an opened image says of itself, or why it did not open.
    pub opened: Result<ImageFacts, ImageError>,
    /// The colour of every decoded pixel, as the bytes of an RGBA pixel, premultiplied.
    pub pixels: Result<[u8; 4], ImageError>,
    /// What every encoding answers.
    pub encoded: Result<Vec<u8>, ImageError>,
}

/// What an image says of itself.
#[derive(Clone, Debug, PartialEq)]
pub struct ImageFacts {
    pub mime_type: String,
    pub size: Size,
    pub pictures: ImagePictures,
    pub orientation: ImageOrientation,
    pub metadata: ImageMetadata,
}

impl ImageFacts {
    /// A still image of `size` in `mime_type`, upright, its file saying nothing of how it was
    /// taken.
    pub fn still(mime_type: &str, size: Size) -> ImageFacts {
        ImageFacts {
            mime_type: mime_type.to_owned(),
            size,
            pictures: ImagePictures::Still,
            orientation: ImageOrientation::default(),
            metadata: ImageMetadata::default(),
        }
    }
}

/// What the app asked of the codecs, in order.
#[derive(Clone, Debug, PartialEq)]
pub enum CodecCall {
    Open(Vec<u8>),
    OpenFile(PathBuf),
    /// A picture decoded into the app's memory, or into a hardware buffer.
    Decode {
        index: u32,
        max_size: Option<Size>,
        hardware: bool,
    },
    /// Pixels of a size, or a hardware buffer's, encoded.
    Encode {
        width: u32,
        height: u32,
        format: String,
        quality: Option<f32>,
        hardware: bool,
    },
}

impl MockImageCodecs {
    /// Codecs that answer `answers`, with no hardware codecs.
    pub fn new(answers: ImageAnswers) -> MockImageCodecs {
        MockImageCodecs::of(answers, false)
    }

    /// Codecs that answer `answers`, with hardware codecs whose buffers are
    /// [`MockHardwareBuffer`]s.
    pub fn with_hardware(answers: ImageAnswers) -> MockImageCodecs {
        MockImageCodecs::of(answers, true)
    }

    fn of(answers: ImageAnswers, hardware: bool) -> MockImageCodecs {
        MockImageCodecs(Rc::new_cyclic(|shared| CodecsShared {
            answers: RefCell::new(answers),
            asked: RefCell::default(),
            hardware: hardware.then(|| MockHardwareImageCodecs(shared.clone())),
        }))
    }

    /// Each call answers `answers` from now on.
    pub fn set_answers(&self, answers: ImageAnswers) {
        *self.0.answers.borrow_mut() = answers;
    }

    /// What the app asked, in order.
    pub fn asked(&self) -> Vec<CodecCall> {
        self.0.asked.borrow().clone()
    }
}

impl CodecsShared {
    fn answers(&self) -> ImageAnswers {
        self.answers.borrow().clone()
    }

    fn ask(&self, call: CodecCall) {
        self.asked.borrow_mut().push(call);
    }

    /// An image of the facts the test set, or why it did not open.
    fn opened(self: &Rc<Self>) -> ImageFuture<Box<dyn ImageSource>> {
        let opened = self.answers().opened.map(|facts| {
            Box::new(MockImage {
                codecs: Rc::clone(self),
                facts,
            }) as Box<dyn ImageSource>
        });
        Box::pin(std::future::ready(opened))
    }

    /// What an encoding of `width` by `height` answers, kept as asked.
    fn encoded(
        &self,
        [width, height]: [u32; 2],
        format: &str,
        quality: Option<f32>,
        hardware: bool,
    ) -> ImageFuture<Vec<u8>> {
        self.ask(CodecCall::Encode {
            width,
            height,
            format: format.to_owned(),
            quality,
            hardware,
        });
        Box::pin(std::future::ready(self.answers().encoded))
    }
}

impl ImageCodecs for MockImageCodecs {
    fn decoded_formats(&self) -> Vec<String> {
        self.0.answers().decoded_formats
    }

    fn encoded_formats(&self) -> Vec<String> {
        self.0.answers().encoded_formats
    }

    fn open(&self, data: Arc<[u8]>) -> ImageFuture<Box<dyn ImageSource>> {
        self.0.ask(CodecCall::Open(data.to_vec()));
        self.0.opened()
    }

    fn open_file(&self, path: &Path) -> ImageFuture<Box<dyn ImageSource>> {
        self.0.ask(CodecCall::OpenFile(path.to_owned()));
        self.0.opened()
    }

    fn encode_pixels(
        &self,
        pixels: PixelBuffer,
        format: &str,
        quality: Option<f32>,
    ) -> ImageFuture<Vec<u8>> {
        self.0.encoded(pixels.layout().size, format, quality, false)
    }

    fn hardware_image_codecs(&self) -> Option<&dyn HardwareImageCodecs> {
        self.0
            .hardware
            .as_ref()
            .map(|hardware| hardware as &dyn HardwareImageCodecs)
    }
}

/// The hardware codecs of [`MockImageCodecs::with_hardware`].
struct MockHardwareImageCodecs(std::rc::Weak<CodecsShared>);

impl HardwareImageCodecs for MockHardwareImageCodecs {
    fn decode(
        &self,
        image: &dyn ImageSource,
        index: u32,
        max_size: Option<Size>,
    ) -> ImageFuture<Arc<dyn HardwareBuffer>> {
        let codecs = self
            .0
            .upgrade()
            .expect("the codecs hold their hardware codecs");
        codecs.ask(CodecCall::Decode {
            index,
            max_size,
            hardware: true,
        });
        let image: &dyn std::any::Any = image;
        let decoded = match image.downcast_ref::<MockImage>() {
            Some(image) => image.checked(index).map(|()| {
                let [width, height] = fitted(image.size_of_picture(index), max_size);
                Arc::new(MockHardwareBuffer {
                    width,
                    height,
                    format: HardwareBufferFormat::Bgra8,
                    usage: SYSTEM_GIVEN,
                }) as Arc<dyn HardwareBuffer>
            }),
            None => Err(ImageError::new(
                ImageErrorKind::Unsupported,
                "an image these codecs did not open",
            )),
        };
        Box::pin(std::future::ready(decoded))
    }

    fn encode(
        &self,
        buffer: &dyn HardwareBuffer,
        format: &str,
        quality: Option<f32>,
    ) -> ImageFuture<Vec<u8>> {
        let codecs = self
            .0
            .upgrade()
            .expect("the codecs hold their hardware codecs");
        codecs.encoded([buffer.width(), buffer.height()], format, quality, true)
    }
}

/// An image the app opened.
struct MockImage {
    codecs: Rc<CodecsShared>,
    facts: ImageFacts,
}

impl MockImage {
    /// Whether the image has picture `index`.
    fn checked(&self, index: u32) -> Result<(), ImageError> {
        if (index as usize) < self.facts.pictures.count() {
            Ok(())
        } else {
            Err(ImageError::new(
                ImageErrorKind::Other,
                format!("no picture {index}"),
            ))
        }
    }

    /// The size picture `index` decodes at: a page's own, and the image's otherwise.
    fn size_of_picture(&self, index: u32) -> Size {
        match &self.facts.pictures {
            ImagePictures::Pages(pages) => pages.pages[index as usize].size,
            ImagePictures::Still | ImagePictures::Animation(_) => self.facts.size,
        }
    }
}

impl ImageSource for MockImage {
    fn mime_type(&self) -> String {
        self.facts.mime_type.clone()
    }

    fn size(&self) -> Size {
        self.facts.size
    }

    fn pictures(&self) -> ImagePictures {
        self.facts.pictures.clone()
    }

    fn orientation(&self) -> ImageOrientation {
        self.facts.orientation
    }

    fn metadata(&self) -> ImageMetadata {
        self.facts.metadata.clone()
    }

    fn decode_pixels(&self, index: u32, max_size: Option<Size>) -> ImageFuture<PixelBuffer> {
        self.codecs.ask(CodecCall::Decode {
            index,
            max_size,
            hardware: false,
        });
        let decoded = self.checked(index).and_then(|()| {
            let colour = self.codecs.answers().pixels?;
            Ok(pixels_of(
                fitted(self.size_of_picture(index), max_size),
                colour,
                PixelFormat::Rgba8,
            ))
        });
        Box::pin(std::future::ready(decoded))
    }
}

/// `size` scaled down to fit `max_size`, keeping its shape, in whole pixels, at least one each way.
fn fitted(size: Size, max_size: Option<Size>) -> [u32; 2] {
    let scale = max_size.map_or(1.0, |max| {
        (max.width() / size.width())
            .min(max.height() / size.height())
            .min(1.0)
    });
    let side = |length: f64| ((length * scale).round() as u32).max(1);
    [side(size.width()), side(size.height())]
}

/// `width` by `height` pixels of `colour`, four bytes each in `format`, premultiplied, packed.
pub(crate) fn pixels_of(
    [width, height]: [u32; 2],
    colour: [u8; 4],
    format: PixelFormat,
) -> PixelBuffer {
    let layout = PixelLayout::packed([width, height], format, AlphaType::Premultiplied);
    let bytes = colour.repeat(width as usize * height as usize);
    PixelBuffer::new(layout, bytes).expect("a packed layout of at least one pixel")
}

#[cfg(test)]
mod tests {
    use std::future::Future;
    use std::task::{Context, Poll, Waker};

    use super::*;

    fn now<T>(future: impl Future<Output = T>) -> T {
        let mut future = std::pin::pin!(future);
        match future
            .as_mut()
            .poll(&mut Context::from_waker(Waker::noop()))
        {
            Poll::Ready(output) => output,
            Poll::Pending => panic!("answers at once"),
        }
    }

    fn answers() -> ImageAnswers {
        ImageAnswers {
            decoded_formats: vec!["image/png".to_owned(), "image/heic".to_owned()],
            encoded_formats: vec!["image/png".to_owned()],
            opened: Ok(ImageFacts::still("image/heic", Size::new(400.0, 300.0))),
            pixels: Ok([255, 0, 0, 255]),
            encoded: Ok(b"encoded".to_vec()),
        }
    }

    #[test]
    fn an_image_opens_with_the_facts_the_test_set_and_decodes_to_fit() {
        let codecs = MockImageCodecs::new(answers());
        assert!(codecs.hardware_image_codecs().is_none());
        let image = now(codecs.open(Arc::from(&b"bytes"[..]))).unwrap();
        assert_eq!(image.mime_type(), "image/heic");
        assert_eq!(image.pictures(), ImagePictures::Still);
        let pixels = now(image.decode_pixels(0, Some(Size::new(200.0, 200.0)))).unwrap();
        assert_eq!(pixels.layout().size, [200, 150]);
        assert_eq!(&pixels.bytes()[..4], [255, 0, 0, 255]);
        let past = now(image.decode_pixels(1, None)).err().unwrap();
        assert_eq!(
            past.kind,
            ImageErrorKind::Other,
            "a picture it does not have"
        );
        let encoded = now(codecs.encode_pixels(pixels, "image/png", Some(0.5)));
        assert_eq!(encoded.unwrap(), b"encoded");
        assert_eq!(
            codecs.asked(),
            [
                CodecCall::Open(b"bytes".to_vec()),
                CodecCall::Decode {
                    index: 0,
                    max_size: Some(Size::new(200.0, 200.0)),
                    hardware: false
                },
                CodecCall::Decode {
                    index: 1,
                    max_size: None,
                    hardware: false
                },
                CodecCall::Encode {
                    width: 200,
                    height: 150,
                    format: "image/png".to_owned(),
                    quality: Some(0.5),
                    hardware: false
                },
            ]
        );
    }

    #[test]
    fn hardware_codecs_decode_into_frames_and_encode_from_them() {
        let codecs = MockImageCodecs::with_hardware(answers());
        let hardware = codecs.hardware_image_codecs().expect("hardware codecs");
        let image = now(codecs.open_file(Path::new("/data/photo.heic"))).unwrap();
        let frame = now(hardware.decode(image.as_ref(), 0, None)).unwrap();
        assert_eq!([frame.width(), frame.height()], [400, 300]);
        let pixels = now(frame.read_pixels()).unwrap();
        assert_eq!(pixels.layout().size, [400, 300]);
        assert_eq!(
            now(hardware.encode(frame.as_ref(), "image/png", None)).unwrap(),
            b"encoded"
        );
        assert_eq!(
            codecs.asked()[0],
            CodecCall::OpenFile("/data/photo.heic".into())
        );
    }
}