gstreamer_gl/
flag_serde.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::{
4    prelude::*,
5    translate::{from_glib, ToGlibPtr},
6    FlagsClass,
7};
8use gst::bitflags_serde_impl;
9
10bitflags_serde_impl!(crate::GLAPI);
11bitflags_serde_impl!(crate::GLConfigSurfaceType, "v1_20");
12bitflags_serde_impl!(crate::GLDisplayType);
13bitflags_serde_impl!(crate::GLPlatform);
14bitflags_serde_impl!(crate::GLSLProfile);
15
16#[cfg(test)]
17mod tests {
18    macro_rules! check_serialize {
19        ($flags:expr, $expected:expr) => {
20            let actual = serde_json::to_string(&$flags).unwrap();
21            assert_eq!(actual, $expected);
22        };
23    }
24
25    macro_rules! check_deserialize {
26        ($ty:ty, $expected:expr, $json:expr) => {
27            let actual: $ty = serde_json::from_str(&$json).unwrap();
28            assert_eq!(actual, $expected);
29        };
30    }
31
32    macro_rules! check_roundtrip {
33        ($ty:ty, $flags:expr) => {
34            let json = serde_json::to_string(&$flags).unwrap();
35            let deserialized: $ty = serde_json::from_str(&json).unwrap();
36            assert_eq!(deserialized, $flags);
37        };
38    }
39
40    #[test]
41    fn test_serialize() {
42        gst::init().unwrap();
43
44        check_serialize!(crate::GLAPI::all(), "\"opengl+opengl3+gles1+gles2\"");
45        #[cfg(feature = "v1_20")]
46        check_serialize!(
47            crate::GLConfigSurfaceType::all(),
48            "\"window+pbuffer+pixmap\""
49        );
50        #[cfg(feature = "v1_24")]
51        check_serialize!(
52            crate::GLDisplayType::all(),
53            concat!(
54                "\"x11+wayland+cocoa+win32+dispmanx+egl+viv-fb+gbm+egl-device",
55                "+eagl+winrt+android+egl-surfaceless\""
56            )
57        );
58        check_serialize!(crate::GLPlatform::all(), "\"egl+glx+wgl+cgl+eagl\"");
59        check_serialize!(crate::GLSLProfile::all(), "\"es+core+compatibility\"");
60    }
61
62    #[test]
63    fn test_deserialize() {
64        gst::init().unwrap();
65
66        check_deserialize!(
67            crate::GLAPI,
68            crate::GLAPI::all(),
69            "\"opengl+opengl3+gles1+gles2\""
70        );
71        #[cfg(feature = "v1_20")]
72        check_deserialize!(
73            crate::GLConfigSurfaceType,
74            crate::GLConfigSurfaceType::all(),
75            "\"none+window+pbuffer+pixmap\""
76        );
77        #[cfg(feature = "v1_24")]
78        check_deserialize!(
79            crate::GLDisplayType,
80            crate::GLDisplayType::all(),
81            concat!(
82                "\"x11+wayland+cocoa+win32+dispmanx+egl+viv-fb+gbm+egl-device",
83                "+eagl+winrt+android+egl-surfaceless\""
84            )
85        );
86        check_deserialize!(
87            crate::GLPlatform,
88            crate::GLPlatform::all(),
89            "\"egl+glx+wgl+cgl+eagl\""
90        );
91        check_deserialize!(
92            crate::GLSLProfile,
93            crate::GLSLProfile::all(),
94            "\"es+core+compatibility\""
95        );
96    }
97
98    #[test]
99    fn test_serde_roundtrip() {
100        gst::init().unwrap();
101
102        check_roundtrip!(crate::GLAPI, crate::GLAPI::all());
103        #[cfg(feature = "v1_20")]
104        check_roundtrip!(
105            crate::GLConfigSurfaceType,
106            crate::GLConfigSurfaceType::all()
107        );
108        #[cfg(feature = "v1_20")]
109        check_roundtrip!(crate::GLDisplayType, crate::GLDisplayType::all());
110        check_roundtrip!(crate::GLPlatform, crate::GLPlatform::all());
111        check_roundtrip!(crate::GLSLProfile, crate::GLSLProfile::all());
112    }
113}