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
use ::internal_prelude::works_everywhere::*;
use ::clients::stateless;


pub mod process_testing {
    use ::internal_prelude::works_everywhere::*;
    use ::imageflow_helpers::process_testing::*;
    use super::*;
    use ::clients::stateless;


    pub trait ProcTestContextExtras {
        fn write_json<T, P: AsRef<Path>>(&self, filename: P, info: &T)
            where T: serde::Serialize;

        fn create_blank_image_here<P: AsRef<Path>>(&self,
                                                   filename_without_ext: P,
                                                   w: u32,
                                                   h: u32,
                                                   encoder: s::EncoderPreset);
    }

    pub trait ProcOutputExtras {
        fn parse_stdout_as<T>(&self) -> std::result::Result<T, serde_json::error::Error>
            where T: serde::Deserialize;
    }

    impl ProcOutputExtras for ProcOutput {
        fn parse_stdout_as<T>(&self) -> std::result::Result<T, serde_json::error::Error>
            where T: serde::Deserialize
        {
            serde_json::from_slice(self.stdout_bytes())
        }
    }

    impl ProcTestContextExtras for ProcTestContext {
        fn write_json<T, P: AsRef<Path>>(&self, filename: P, info: &T)
            where T: serde::Serialize
        {
            let bytes = ::serde_json::to_vec_pretty(info).unwrap();
            self.write_file(filename, &bytes);
        }

        fn create_blank_image_here<P: AsRef<Path>>(&self,
                                                   filename_without_ext: P,
                                                   w: u32,
                                                   h: u32,
                                                   encoder: s::EncoderPreset) {
            let out = BlankImage {
                w: w,
                h: h,
                encoding: encoder,
                color: s::Color::Black
            }.generate();


            let mut path = self.working_dir().join(filename_without_ext);
            path.set_extension(&out.file_ext);

            self.write_file(path.file_name().unwrap().to_str().unwrap(), &out.bytes);
        }
    }
}

#[derive(Clone,Debug,PartialEq)]
pub enum TestImageSource {
    Url(String),
    Blank(BlankImage)
}

impl TestImageSource{
    pub fn get_bytes(&self) -> Vec<u8>{
        match *self{
            TestImageSource::Url(ref url) => {
                ::imageflow_helpers::fetching::fetch_bytes(url).unwrap()
            },
            TestImageSource::Blank(ref blank) => {
                blank.generate().bytes
            }
        }
    }
}
#[derive(Clone,Debug,PartialEq)]
pub struct BlankImage{
    pub w: u32,
    pub h: u32,
    pub color: s::Color,
    pub encoding: s::EncoderPreset
}

impl BlankImage{
    fn generate(&self) -> stateless::BuildOutput{
        // Invalid read here; the result of create_canvas is not being accessed correctly.
        let req = stateless::BuildRequest {
            inputs: vec![],
            framewise: s::Framewise::Steps(vec![s::Node::CreateCanvas {
                w: self.w as usize,
                h: self.h as usize,
                format: s::PixelFormat::Bgr24,
                color: self.color.clone(),
            },
            s::Node::Encode {
                io_id: 0,
                preset: self.encoding.clone(),
            }]),
            export_graphs_to: None, /* Some(std::path::PathBuf::from(format!("./{}/{}_debug", dir, filename_without_ext))) */
        };

        let result = ::clients::stateless::LibClient::new().build(req).unwrap();
        result.outputs.into_iter().next().unwrap()
    }
}