Debian Codegraph @

tests/message_dispatcher_tests.adb

raw
1 --
2 -- D_Bus/Ada - An Ada binding to D-Bus
3 --
4 -- Copyright (C) 2020 Reto Buerki <[email protected]>
5 --
6 -- This program is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU General Public License
8 -- as published by the Free Software Foundation; either version 2
9 -- of the License, or (at your option) any later version.
10 --
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU General Public License for more details.
15 --
16 -- You should have received a copy of the GNU General Public License
17 -- along with this program; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 -- USA.
20 --
21 -- As a special exception, if other files instantiate generics from this
22 -- unit, or you link this unit with other files to produce an
23 -- executable this unit does not by itself cause the resulting
24 -- executable to be covered by the GNU General Public License. This
25 -- exception does not however invalidate any other reasons why the
26 -- executable file might be covered by the GNU Public License.
27 --
28
29 with Ahven;
30
31 with D_Bus.Messages;
32 with D_Bus.Connection;
33 with D_Bus.Arguments.Basic;
34 with D_Bus.Message_Dispatcher.Proc;
35 with D_Bus.Message_Dispatcher.Object;
36 with D_Bus.Types;
37
38 package body Message_Dispatcher_Tests is
39
40 use Ahven;
41 use D_Bus;
42
43 -------------------------------------------------------------------------
44
45 procedure Initialize (T : in out Testcase) is
46 begin
47 T.Set_Name (Name => "Message dispatchers");
48 T.Add_Test_Routine
49 (Routine => Proc_Dispatcher'Access,
50 Name => "Procedure-based dispatching");
51 T.Add_Test_Routine
52 (Routine => OO_Dispatcher'Access,
53 Name => "Object-based dispatching");
54 end Initialize;
55
56 -------------------------------------------------------------------------
57
58 procedure OO_Dispatcher
59 is
60 type Object_Type is new
61 D_Bus.Message_Dispatcher.Object.Handler_Type
62 with record
63 Called : Boolean := False;
64 end record;
65
66 ----------------------------------------------------------------------
67
68 overriding
69 procedure Dispatch
70 (O : in out Object_Type;
71 In_Mg : D_Bus.Messages.Message_Type);
72
73 procedure Dispatch
74 (O : in out Object_Type;
75 In_Mg : D_Bus.Messages.Message_Type)
76 is
77 pragma Unreferenced (In_Mg);
78 begin
79 O.Called := True;
80 end Dispatch;
81
82 use D_Bus.Arguments;
83 use D_Bus.Arguments.Basic;
84 use type D_Bus.Types.Obj_Path;
85
86 Object : aliased Object_Type;
87 Success : Boolean;
88 D : Message_Dispatcher.Object.OO_Dispatcher_Type;
89 Dummy : Argument_List_Type;
90 Conn : constant Connection.Connection_Type
91 := Connection.Connect (Bus => Bus_Session);
92 begin
93 Connection.Add_Match
94 (Connection => Conn,
95 Rule => "type='signal'");
96 D.Register_Signal_Handler
97 (Iface => "dbus.ada.msg_dispatcher",
98 Signal_Name => "event",
99 Handler => Object'Unchecked_Access);
100
101 Connection.Send_Signal
102 (Connection => Conn,
103 Object_Name => +"/",
104 Iface => "dbus.ada.msg_dispatcher",
105 Name => "event",
106 Args => +True);
107
108 for I in 1 .. 10 loop
109 D.Peek (Conn => Conn,
110 Success => Success);
111 exit when not Success or Object.Called;
112 delay 0.1;
113 end loop;
114
115 Assert (Condition => Success,
116 Message => "Not success");
117 Assert (Condition => Object.Called,
118 Message => "Object not called");
119
120 Object.Called := False;
121 D.Unregister_Signal_Handler
122 (Iface => "dbus.ada.msg_dispatcher",
123 Signal_Name => "event",
124 Handler => Object'Unchecked_Access);
125
126 Connection.Send_Signal
127 (Connection => Conn,
128 Object_Name => +"/",
129 Iface => "dbus.ada.msg_dispatcher",
130 Name => "event",
131 Args => +True);
132
133 for I in 1 .. 10 loop
134 D.Peek (Conn => Conn,
135 Success => Success);
136 exit when not Success or Object.Called;
137 delay 0.1;
138 end loop;
139
140 Assert (Condition => Success,
141 Message => "Not success");
142 Assert (Condition => not Object.Called,
143 Message => "Object called");
144 end OO_Dispatcher;
145
146 -------------------------------------------------------------------------
147
148 procedure Proc_Dispatcher
149 is
150 Handled : Boolean := False;
151
152 ----------------------------------------------------------------------
153
154 procedure Handle (In_Msg : D_Bus.Messages.Message_Type);
155 procedure Handle (In_Msg : D_Bus.Messages.Message_Type)
156 is
157 pragma Unreferenced (In_Msg);
158 begin
159 Handled := True;
160 end Handle;
161
162 use D_Bus.Arguments;
163 use D_Bus.Arguments.Basic;
164 use type D_Bus.Types.Obj_Path;
165
166 Success : Boolean;
167 D : Message_Dispatcher.Proc.Proc_Dispatcher_Type;
168 Dummy : Argument_List_Type;
169 Conn : constant Connection.Connection_Type
170 := Connection.Connect (Bus => Bus_Session);
171 begin
172 Connection.Add_Match
173 (Connection => Conn,
174 Rule => "type='signal'");
175 D.Register_Signal_Handler
176 (Iface => "dbus.ada.msg_dispatcher",
177 Signal_Name => "event",
178 Handler => Handle'Unrestricted_Access);
179
180 Connection.Send_Signal
181 (Connection => Conn,
182 Object_Name => +"/",
183 Iface => "dbus.ada.msg_dispatcher",
184 Name => "event",
185 Args => +True);
186
187 for I in 1 .. 10 loop
188 D.Peek (Conn => Conn,
189 Success => Success);
190 exit when not Success or Handled;
191 delay 0.1;
192 end loop;
193
194 Assert (Condition => Success,
195 Message => "Not success");
196 Assert (Condition => Handled,
197 Message => "Not handled");
198
199 Handled := False;
200 D.Unregister_Signal_Handler
201 (Iface => "dbus.ada.msg_dispatcher",
202 Signal_Name => "event",
203 Handler => Handle'Unrestricted_Access);
204
205 Connection.Send_Signal
206 (Connection => Conn,
207 Object_Name => +"/",
208 Iface => "dbus.ada.msg_dispatcher",
209 Name => "event",
210 Args => +True);
211
212 for I in 1 .. 10 loop
213 D.Peek (Conn => Conn,
214 Success => Success);
215 exit when not Success or Handled;
216 delay 0.1;
217 end loop;
218
219 Assert (Condition => Success,
220 Message => "Not success");
221 Assert (Condition => not Handled,
222 Message => "Handled");
223 end Proc_Dispatcher;
224
225 end Message_Dispatcher_Tests;