From 7120d54a46c9948e75c7a48e5a4f80b22d949756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sat, 28 Mar 2020 20:00:11 +0200 Subject: [PATCH] gstreamer/pad: Add tests for getrange functions --- gstreamer/src/pad.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/gstreamer/src/pad.rs b/gstreamer/src/pad.rs index 260388f5c..a6a0c0979 100644 --- a/gstreamer/src/pad.rs +++ b/gstreamer/src/pad.rs @@ -1612,6 +1612,63 @@ mod tests { } } + #[test] + fn test_getrange_function() { + ::init().unwrap(); + + let pad = ::Pad::new(Some("src"), ::PadDirection::Src); + pad.set_activate_function(|pad, _parent| { + pad.activate_mode(::PadMode::Pull, true) + .map_err(|err| err.into()) + }); + pad.set_getrange_function(|_pad, _parent, offset, _buffer, size| { + assert_eq!(offset, 0); + assert_eq!(size, 5); + let buffer = ::Buffer::from_slice(b"abcde"); + Ok(PadGetRangeSuccess::NewBuffer(buffer)) + }); + pad.set_active(true).unwrap(); + + let buffer = pad.get_range(0, 5).unwrap(); + let map = buffer.map_readable().unwrap(); + assert_eq!(&*map, b"abcde"); + + let mut buffer = ::Buffer::with_size(5).unwrap(); + pad.get_range_fill(0, buffer.get_mut().unwrap(), 5).unwrap(); + let map = buffer.map_readable().unwrap(); + assert_eq!(&*map, b"abcde"); + + pad.set_active(false).unwrap(); + drop(pad); + + let pad = ::Pad::new(Some("src"), ::PadDirection::Src); + pad.set_activate_function(|pad, _parent| { + pad.activate_mode(::PadMode::Pull, true) + .map_err(|err| err.into()) + }); + pad.set_getrange_function(|_pad, _parent, offset, buffer, size| { + assert_eq!(offset, 0); + assert_eq!(size, 5); + if let Some(buffer) = buffer { + buffer.copy_from_slice(0, b"fghij").unwrap(); + Ok(PadGetRangeSuccess::FilledBuffer) + } else { + let buffer = ::Buffer::from_slice(b"abcde"); + Ok(PadGetRangeSuccess::NewBuffer(buffer)) + } + }); + pad.set_active(true).unwrap(); + + let buffer = pad.get_range(0, 5).unwrap(); + let map = buffer.map_readable().unwrap(); + assert_eq!(&*map, b"abcde"); + + let mut buffer = ::Buffer::with_size(5).unwrap(); + pad.get_range_fill(0, buffer.get_mut().unwrap(), 5).unwrap(); + let map = buffer.map_readable().unwrap(); + assert_eq!(&*map, b"fghij"); + } + #[test] fn test_task() { ::init().unwrap();