1 use zbus::{self, fdo};
2 use zbus_macros::{dbus_interface, dbus_proxy, DBusError};
3
4 #[test]
test_proxy()5 fn test_proxy() {
6 #[dbus_proxy(
7 interface = "org.freedesktop.zbus.Test",
8 default_service = "org.freedesktop.zbus",
9 default_path = "/org/freedesktop/zbus/test"
10 )]
11 trait Test {
12 /// comment for a_test()
13 fn a_test(&self, val: &str) -> zbus::Result<u32>;
14
15 #[dbus_proxy(name = "CheckRENAMING")]
16 fn check_renaming(&self) -> zbus::Result<Vec<u8>>;
17
18 #[dbus_proxy(property)]
19 fn property(&self) -> fdo::Result<Vec<String>>;
20
21 #[dbus_proxy(property)]
22 fn set_property(&self, val: u16) -> fdo::Result<()>;
23 }
24 }
25
26 #[test]
test_derive_error()27 fn test_derive_error() {
28 #[derive(Debug, DBusError)]
29 #[dbus_error(prefix = "org.freedesktop.zbus")]
30 enum Test {
31 ZBus(zbus::Error),
32 SomeExcuse,
33 #[dbus_error(name = "I.Am.Sorry.Dave")]
34 IAmSorryDave(String),
35 LetItBe {
36 desc: String,
37 },
38 }
39 }
40
41 #[test]
test_interface()42 fn test_interface() {
43 use zbus::Interface;
44
45 struct Test<'a, T> {
46 something: &'a str,
47 generic: T,
48 }
49
50 #[dbus_interface(name = "org.freedesktop.zbus.Test")]
51 impl<T: 'static> Test<'static, T>
52 where
53 T: serde::ser::Serialize + zvariant::Type,
54 {
55 /// Testing `no_arg` documentation is reflected in XML.
56 fn no_arg(&self) {
57 unimplemented!()
58 }
59
60 fn str_u32(&self, val: &str) -> zbus::fdo::Result<u32> {
61 val.parse()
62 .map_err(|e| zbus::fdo::Error::Failed(format!("Invalid val: {}", e)))
63 }
64
65 // TODO: naming output arguments after "RFC: Structural Records #2584"
66 fn many_output(&self) -> zbus::fdo::Result<(&T, String)> {
67 Ok((&self.generic, self.something.to_string()))
68 }
69
70 fn pair_output(&self) -> zbus::fdo::Result<((u32, String),)> {
71 unimplemented!()
72 }
73
74 #[dbus_interface(name = "CheckVEC")]
75 fn check_vec(&self) -> Vec<u8> {
76 unimplemented!()
77 }
78
79 /// Testing my_prop documentation is reflected in XML.
80 ///
81 /// And that too.
82 #[dbus_interface(property)]
83 fn my_prop(&self) -> u16 {
84 unimplemented!()
85 }
86
87 #[dbus_interface(property)]
88 fn set_my_prop(&self, _val: u16) {
89 unimplemented!()
90 }
91
92 /// Emit a signal.
93 #[dbus_interface(signal)]
94 fn signal(&self, arg: u8, other: &str) -> zbus::Result<()>;
95 }
96
97 const EXPECTED_XML: &str = r#"<interface name="org.freedesktop.zbus.Test">
98 <!--
99 Testing `no_arg` documentation is reflected in XML.
100 -->
101 <method name="NoArg">
102 </method>
103 <method name="StrU32">
104 <arg name="val" type="s" direction="in"/>
105 <arg type="u" direction="out"/>
106 </method>
107 <method name="ManyOutput">
108 <arg type="u" direction="out"/>
109 <arg type="s" direction="out"/>
110 </method>
111 <method name="PairOutput">
112 <arg type="(us)" direction="out"/>
113 </method>
114 <method name="CheckVEC">
115 <arg type="ay" direction="out"/>
116 </method>
117 <!--
118 Emit a signal.
119 -->
120 <signal name="Signal">
121 <arg name="arg" type="y"/>
122 <arg name="other" type="s"/>
123 </signal>
124 <!--
125 Testing my_prop documentation is reflected in XML.
126
127 And that too.
128 -->
129 <property name="MyProp" type="q" access="readwrite"/>
130 </interface>
131 "#;
132 let t = Test {
133 something: &"somewhere",
134 generic: 42u32,
135 };
136 let mut xml = String::new();
137 t.introspect_to_writer(&mut xml, 0);
138 assert_eq!(xml, EXPECTED_XML);
139
140 assert_eq!(Test::<u32>::name(), "org.freedesktop.zbus.Test");
141
142 if false {
143 // check compilation
144 let c = zbus::Connection::new_session().unwrap();
145 let m = zbus::Message::method(None, None, "/", None, "StrU32", &(42,)).unwrap();
146 let _ = t.call(&c, &m, "StrU32").unwrap();
147 t.signal(23, "ergo sum").unwrap();
148 }
149 }
150