Debian Codegraph @

tests/test_inputcapture.py

raw
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 #
3 # This file is formatted with Python Black
4
5 import tests as xdp
6
7 import dbus
8 import pytest
9 import socket
10 from gi.repository import GLib
11 from itertools import count
12 from typing import Any
13
14
15 counter = count()
16
17
18 def default_zones():
19 return [(1024, 768, 0, 0), (640, 480, 1024, 0)]
20
21
22 @pytest.fixture
23 def required_templates():
24 return {"inputcapture": {}}
25
26
27 @pytest.fixture
28 def zones():
29 return default_zones()
30
31
32 class TestInputCapture:
33 def create_session(self, dbus_con, capabilities=0xF):
34 """
35 Call CreateSession for the given capabilities and return the
36 (response, results) tuple.
37 """
38 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
39 mock_intf = xdp.get_mock_iface(dbus_con)
40
41 request = xdp.Request(dbus_con, inputcapture_intf)
42
43 capabilities = dbus.UInt32(capabilities, variant_level=1)
44 session_handle_token = dbus.String(f"session{next(counter)}", variant_level=1)
45
46 options = dbus.Dictionary(
47 {
48 "capabilities": capabilities,
49 "session_handle_token": session_handle_token,
50 },
51 signature="sv",
52 )
53
54 response = request.call("CreateSession", parent_window="", options=options)
55
56 assert response
57 assert response.response == 0
58 assert "session_handle" in response.results
59 assert "capabilities" in response.results
60 caps = response.results["capabilities"]
61 # Returned capabilities must be a subset of the requested ones
62 assert caps & ~capabilities == 0
63
64 self.current_session_handle = response.results["session_handle"]
65
66 # Check the impl portal was called with the right args
67 method_calls = mock_intf.GetMethodCalls("CreateSession")
68 assert len(method_calls) > 0
69 _, args = method_calls[-1]
70 assert args[3] == "" # parent window
71 assert args[4]["capabilities"] == capabilities
72
73 return response
74
75 def get_zones(self, dbus_con):
76 """
77 Call GetZones and return the (response, results) tuple.
78 """
79 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
80 mock_intf = xdp.get_mock_iface(dbus_con)
81
82 request = xdp.Request(dbus_con, inputcapture_intf)
83 options: Any = {}
84 response = request.call(
85 "GetZones", session_handle=self.current_session_handle, options=options
86 )
87
88 assert response
89 assert response.response == 0
90 assert "zones" in response.results
91 assert "zone_set" in response.results
92
93 self.current_zone_set = response.results["zone_set"]
94
95 # Check the impl portal was called with the right args
96 method_calls = mock_intf.GetMethodCalls("GetZones")
97 assert len(method_calls) > 0
98 _, args = method_calls[-1]
99 assert args[0] == request.handle
100 assert args[1] == self.current_session_handle
101
102 return response
103
104 def set_pointer_barriers(self, dbus_con, barriers):
105 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
106 mock_intf = xdp.get_mock_iface(dbus_con)
107
108 request = xdp.Request(dbus_con, inputcapture_intf)
109 options: Any = {}
110 response = request.call(
111 "SetPointerBarriers",
112 session_handle=self.current_session_handle,
113 options=options,
114 barriers=barriers,
115 zone_set=self.current_zone_set,
116 )
117 assert response
118 assert response.response == 0
119 assert "failed_barriers" in response.results
120
121 # Check the impl portal was called with the right args
122 method_calls = mock_intf.GetMethodCalls("SetPointerBarriers")
123 assert len(method_calls) > 0
124 _, args = method_calls[-1]
125 assert args[0] == request.handle
126 assert args[1] == self.current_session_handle
127 assert args[4] == barriers
128 assert args[5] == self.current_zone_set
129
130 return response
131
132 def connect_to_eis(self, dbus_con):
133 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
134 mock_intf = xdp.get_mock_iface(dbus_con)
135
136 fd = inputcapture_intf.ConnectToEIS(
137 self.current_session_handle, dbus.Dictionary({}, signature="sv")
138 )
139
140 # Our dbusmock template sends HELLO
141 eis_socket = socket.fromfd(fd.take(), socket.AF_UNIX, socket.SOCK_STREAM)
142 hello = eis_socket.recv(10)
143 assert hello == b"HELLO"
144
145 method_calls = mock_intf.GetMethodCalls("ConnectToEIS")
146 assert len(method_calls) > 0
147 _, args = method_calls[-1]
148 assert args[0] == self.current_session_handle
149
150 return eis_socket
151
152 def enable(self, dbus_con):
153 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
154 mock_intf = xdp.get_mock_iface(dbus_con)
155
156 inputcapture_intf.Enable(
157 self.current_session_handle, dbus.Dictionary({}, signature="sv")
158 )
159
160 method_calls = mock_intf.GetMethodCalls("Enable")
161 assert len(method_calls) > 0
162 _, args = method_calls[-1]
163 assert args[0] == self.current_session_handle
164
165 def disable(self, dbus_con):
166 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
167 mock_intf = xdp.get_mock_iface(dbus_con)
168
169 inputcapture_intf.Disable(
170 self.current_session_handle, dbus.Dictionary({}, signature="sv")
171 )
172
173 method_calls = mock_intf.GetMethodCalls("Disable")
174 assert len(method_calls) > 0
175 _, args = method_calls[-1]
176 assert args[0] == self.current_session_handle
177
178 def release(self, dbus_con, activation_id: int, cursor_position=None):
179 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
180 mock_intf = xdp.get_mock_iface(dbus_con)
181
182 options = {"activation_id": dbus.UInt32(activation_id)}
183 if cursor_position:
184 options["cursor_position"] = dbus.Struct(
185 list(cursor_position), signature="dd", variant_level=1
186 )
187
188 inputcapture_intf.Release(
189 self.current_session_handle, dbus.Dictionary(options, signature="sv")
190 )
191
192 method_calls = mock_intf.GetMethodCalls("Release")
193 assert len(method_calls) > 0
194 _, args = method_calls[-1]
195 assert args[0] == self.current_session_handle
196 assert "activation_id" in args[2]
197 aid = args[2]["activation_id"]
198 assert aid == activation_id
199 if cursor_position:
200 assert "cursor_position" in args[2]
201 pos = args[2]["cursor_position"]
202 assert pos == cursor_position
203
204 def test_version(self, portals, dbus_con):
205 xdp.check_version(dbus_con, "InputCapture", 1)
206
207 @pytest.mark.parametrize(
208 "template_params",
209 (
210 {
211 "inputcapture": {
212 "supported_capabilities": 0b101, # KEYBOARD, POINTER, TOUCH
213 },
214 },
215 ),
216 )
217 def test_supported_capabilities(self, portals, dbus_con):
218 properties_intf = xdp.get_iface(dbus_con, "org.freedesktop.DBus.Properties")
219
220 caps = properties_intf.Get(
221 "org.freedesktop.portal.InputCapture", "SupportedCapabilities"
222 )
223 assert caps == 0b101
224
225 def test_create_session(self, portals, dbus_con):
226 mock_intf = xdp.get_mock_iface(dbus_con)
227
228 self.create_session(dbus_con, capabilities=0b1) # KEYBOARD
229
230 # Check the impl portal was called with the right args
231 method_calls = mock_intf.GetMethodCalls("CreateSession")
232 assert len(method_calls) == 1
233 _, args = method_calls.pop(0)
234 assert args[3] == "" # parent window
235 assert args[4]["capabilities"] == 0b1
236
237 @pytest.mark.parametrize(
238 "template_params",
239 (
240 {
241 "inputcapture": {
242 "capabilities": 0b110, # TOUCH, POINTER
243 "supported_capabilities": 0b111, # TOUCH, POINTER, KEYBOARD
244 },
245 },
246 ),
247 )
248 def test_create_session_limited_caps(self, portals, dbus_con):
249 mock_intf = xdp.get_mock_iface(dbus_con)
250
251 # Request more caps than are supported
252 response, results = self.create_session(dbus_con, capabilities=0b111)
253 caps = results["capabilities"]
254 # Returned capabilities must the ones we set up in the params
255 assert caps == 0b110
256
257 # Check the impl portal was called with the right args
258 method_calls = mock_intf.GetMethodCalls("CreateSession")
259 assert len(method_calls) == 1
260 _, args = method_calls.pop(0)
261 assert args[3] == "" # parent window
262 assert args[4]["capabilities"] == 0b111
263
264 @pytest.mark.parametrize(
265 "template_params",
266 (
267 {
268 "inputcapture": {
269 "default-zone": dbus.Array(
270 [dbus.Struct(z, signature="uuii") for z in default_zones()],
271 signature="(uuii)",
272 variant_level=1,
273 )
274 },
275 },
276 ),
277 )
278 def test_get_zones(self, portals, dbus_con, zones):
279 mock_intf = xdp.get_mock_iface(dbus_con)
280
281 response, results = self.create_session(dbus_con)
282 response, results = self.get_zones(dbus_con)
283 for z1, z2 in zip(results["zones"], zones):
284 assert z1 == z2
285
286 # Check the impl portal was called with the right args
287 method_calls = mock_intf.GetMethodCalls("CreateSession")
288 assert len(method_calls) == 1
289 method_calls = mock_intf.GetMethodCalls("GetZones")
290 assert len(method_calls) == 1
291
292 @pytest.mark.parametrize(
293 "template_params",
294 (
295 {
296 "inputcapture": {
297 "default-zone": dbus.Array(
298 [dbus.Struct(z, signature="uuii") for z in default_zones()],
299 signature="(uuii)",
300 variant_level=1,
301 )
302 },
303 },
304 ),
305 )
306 def test_set_pointer_barriers(self, portals, dbus_con, zones):
307 mock_intf = xdp.get_mock_iface(dbus_con)
308
309 response, results = self.create_session(dbus_con)
310 response, results = self.get_zones(dbus_con)
311
312 barriers = [
313 {
314 "barrier_id": dbus.UInt32(10, variant_level=1),
315 "position": dbus.Struct(
316 [0, 0, 0, 768], signature="iiii", variant_level=1
317 ),
318 },
319 {
320 "barrier_id": dbus.UInt32(11, variant_level=1),
321 "position": dbus.Struct(
322 [0, 0, 1024, 0], signature="iiii", variant_level=1
323 ),
324 },
325 {
326 "barrier_id": dbus.UInt32(12, variant_level=1),
327 "position": dbus.Struct(
328 [1024, 0, 1024, 768], signature="iiii", variant_level=1
329 ),
330 },
331 {
332 "barrier_id": dbus.UInt32(13, variant_level=1),
333 "position": dbus.Struct(
334 [0, 768, 1024, 768], signature="iiii", variant_level=1
335 ),
336 },
337 {
338 "barrier_id": dbus.UInt32(14, variant_level=1),
339 "position": dbus.Struct(
340 [100, 768, 500, 768], signature="iiii", variant_level=1
341 ),
342 },
343 {
344 "barrier_id": dbus.UInt32(15, variant_level=1),
345 "position": dbus.Struct(
346 [1024, 0, 1024, 480], signature="iiii", variant_level=1
347 ),
348 },
349 {
350 "barrier_id": dbus.UInt32(16, variant_level=1),
351 "position": dbus.Struct(
352 [1024 + 640, 0, 1024 + 640, 480], signature="iiii", variant_level=1
353 ),
354 },
355 # invalid ones
356 {
357 "barrier_id": dbus.UInt32(20, variant_level=1),
358 "position": dbus.Struct(
359 [0, 1, 3, 4], signature="iiii", variant_level=1
360 ),
361 },
362 {
363 "barrier_id": dbus.UInt32(21, variant_level=1),
364 "position": dbus.Struct(
365 [0, 1, 1024, 1], signature="iiii", variant_level=1
366 ),
367 },
368 {
369 "barrier_id": dbus.UInt32(22, variant_level=1),
370 "position": dbus.Struct(
371 [1, 0, 1, 768], signature="iiii", variant_level=1
372 ),
373 },
374 {
375 "barrier_id": dbus.UInt32(23, variant_level=1),
376 "position": dbus.Struct(
377 [1023, 0, 1023, 768], signature="iiii", variant_level=1
378 ),
379 },
380 {
381 "barrier_id": dbus.UInt32(24, variant_level=1),
382 "position": dbus.Struct(
383 [0, 0, 1050, 0], signature="iiii", variant_level=1
384 ),
385 },
386 ]
387 response, results = self.set_pointer_barriers(dbus_con, barriers=barriers)
388 failed_barriers = results["failed_barriers"]
389 assert all([id >= 20 for id in failed_barriers])
390
391 for id in [b["barrier_id"] for b in barriers if b["barrier_id"] >= 20]:
392 assert id in failed_barriers
393
394 # Check the impl portal was called with the right args
395 method_calls = mock_intf.GetMethodCalls("CreateSession")
396 assert len(method_calls) == 1
397 method_calls = mock_intf.GetMethodCalls("GetZones")
398 assert len(method_calls) == 1
399 method_calls = mock_intf.GetMethodCalls("SetPointerBarriers")
400 assert len(method_calls) == 1
401 _, args = method_calls.pop(0)
402 assert args[4] == barriers
403 assert args[5] == self.current_zone_set
404
405 def test_connect_to_eis(self, portals, dbus_con):
406 self.create_session(dbus_con)
407 self.get_zones(dbus_con)
408
409 # The default zone is 1920x1080
410 barriers = [
411 {
412 "barrier_id": dbus.UInt32(10, variant_level=1),
413 "position": dbus.Struct(
414 [0, 0, 1920, 0], signature="iiii", variant_level=1
415 ),
416 },
417 ]
418 self.set_pointer_barriers(dbus_con, barriers)
419
420 self.connect_to_eis(dbus_con)
421
422 def test_enable_disable(self, portals, dbus_con):
423 mock_intf = xdp.get_mock_iface(dbus_con)
424
425 self.create_session(dbus_con)
426 self.get_zones(dbus_con)
427
428 # The default zone is 1920x1080
429 barriers = [
430 {
431 "barrier_id": dbus.UInt32(10, variant_level=1),
432 "position": dbus.Struct(
433 [0, 0, 1920, 0], signature="iiii", variant_level=1
434 ),
435 },
436 ]
437 self.set_pointer_barriers(dbus_con, barriers)
438 self.connect_to_eis(dbus_con)
439
440 # Disable before enable should be a noop
441 self.disable(dbus_con)
442
443 method_calls = mock_intf.GetMethodCalls("Disable")
444 assert len(method_calls) == 1
445
446 self.enable(dbus_con)
447 method_calls = mock_intf.GetMethodCalls("Enable")
448 assert len(method_calls) == 1
449
450 self.disable(dbus_con)
451 method_calls = mock_intf.GetMethodCalls("Disable")
452 assert len(method_calls) == 2
453
454 @pytest.mark.parametrize(
455 "template_params",
456 (
457 {
458 "inputcapture": {
459 "disable-delay": 200,
460 },
461 },
462 ),
463 )
464 def test_disable_signal(self, portals, dbus_con):
465 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
466
467 self.create_session(dbus_con)
468 self.get_zones(dbus_con)
469 # The default zone is 1920x1080
470 barriers = [
471 {
472 "barrier_id": dbus.UInt32(10, variant_level=1),
473 "position": dbus.Struct(
474 [0, 0, 1920, 0], signature="iiii", variant_level=1
475 ),
476 },
477 ]
478 self.set_pointer_barriers(dbus_con, barriers)
479 self.connect_to_eis(dbus_con)
480
481 disabled_signal_received = False
482
483 def cb_disabled(session_handle, options):
484 nonlocal disabled_signal_received
485 disabled_signal_received = True
486 assert session_handle == session_handle
487
488 inputcapture_intf.connect_to_signal("Disabled", cb_disabled)
489 self.enable(dbus_con)
490 xdp.wait_for(lambda: disabled_signal_received)
491
492 @pytest.mark.parametrize(
493 "template_params",
494 (
495 {
496 "inputcapture": {
497 "activated-delay": 200,
498 "deactivated-delay": 300,
499 },
500 },
501 ),
502 )
503 def test_activated_signal(self, portals, dbus_con):
504 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
505
506 self.create_session(dbus_con)
507 self.get_zones(dbus_con)
508 # The default zone is 1920x1080
509 barriers = [
510 {
511 "barrier_id": dbus.UInt32(10, variant_level=1),
512 "position": dbus.Struct(
513 [0, 0, 1920, 0], signature="iiii", variant_level=1
514 ),
515 },
516 ]
517 self.set_pointer_barriers(dbus_con, barriers)
518 self.connect_to_eis(dbus_con)
519
520 disabled_signal_received = False
521 activated_signal_received = False
522 deactivated_signal_received = False
523
524 def cb_disabled(session_handle, options):
525 nonlocal disabled_signal_received
526 disabled_signal_received = True
527
528 def cb_activated(session_handle, options):
529 nonlocal activated_signal_received
530 activated_signal_received = True
531 assert session_handle == session_handle
532 assert "activation_id" in options
533 assert "barrier_id" in options
534 assert options["barrier_id"] == 10 # template uses first barrier
535 assert "cursor_position" in options
536 assert options["cursor_position"] == (
537 10.0,
538 20.0,
539 ) # template uses x+10, y+20 of first barrier
540
541 def cb_deactivated(session_handle, options):
542 nonlocal deactivated_signal_received
543 deactivated_signal_received = True
544 assert session_handle == session_handle
545 assert "activation_id" in options
546 assert "cursor_position" in options
547 assert options["cursor_position"] == (
548 10.0,
549 20.0,
550 ) # template uses x+10, y+20 of first barrier
551
552 inputcapture_intf.connect_to_signal("Activated", cb_activated)
553 inputcapture_intf.connect_to_signal("Deactivated", cb_deactivated)
554 inputcapture_intf.connect_to_signal("Disabled", cb_disabled)
555
556 self.enable(dbus_con)
557
558 xdp.wait_for(lambda: activated_signal_received and deactivated_signal_received)
559 assert not disabled_signal_received
560
561 # Disabling should not trigger the signal
562 self.disable(dbus_con)
563 assert not disabled_signal_received
564
565 @pytest.mark.parametrize(
566 "template_params",
567 (
568 {
569 "inputcapture": {
570 "zones-changed-delay": 200,
571 },
572 },
573 ),
574 )
575 def test_zones_changed_signal(self, portals, dbus_con):
576 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
577
578 self.create_session(dbus_con)
579 self.get_zones(dbus_con)
580 # The default zone is 1920x1080
581 barriers = [
582 {
583 "barrier_id": dbus.UInt32(10, variant_level=1),
584 "position": dbus.Struct(
585 [0, 0, 1920, 0], signature="iiii", variant_level=1
586 ),
587 },
588 ]
589 self.set_pointer_barriers(dbus_con, barriers)
590 self.connect_to_eis(dbus_con)
591
592 zones_changed_signal_received = False
593
594 def cb_zones_changed(session_handle, options):
595 nonlocal zones_changed_signal_received
596 zones_changed_signal_received = True
597 assert session_handle == session_handle
598
599 inputcapture_intf.connect_to_signal("ZonesChanged", cb_zones_changed)
600 self.enable(dbus_con)
601 xdp.wait_for(lambda: zones_changed_signal_received)
602
603 @pytest.mark.parametrize(
604 "template_params",
605 (
606 {
607 "inputcapture": {
608 "activated-delay": 200,
609 "deactivated-delay": 1000,
610 "disabled-delay": 1200,
611 },
612 },
613 ),
614 )
615 def test_release(self, portals, dbus_con):
616 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
617
618 self.create_session(dbus_con)
619 self.get_zones(dbus_con)
620 # The default zone is 1920x1080
621 barriers = [
622 {
623 "barrier_id": dbus.UInt32(10, variant_level=1),
624 "position": dbus.Struct(
625 [0, 0, 1920, 0], signature="iiii", variant_level=1
626 ),
627 },
628 ]
629 self.set_pointer_barriers(dbus_con, barriers)
630 self.connect_to_eis(dbus_con)
631
632 disabled_signal_received = False
633 activated_signal_received = False
634 deactivated_signal_received = False
635 activation_id = None
636
637 def cb_disabled(session_handle, options):
638 nonlocal disabled_signal_received
639 disabled_signal_received = True
640
641 def cb_activated(session_handle, options):
642 nonlocal activated_signal_received, activation_id
643 activated_signal_received = True
644 activation_id = options["activation_id"]
645
646 def cb_deactivated(session_handle, options):
647 nonlocal deactivated_signal_received
648 deactivated_signal_received = True
649
650 inputcapture_intf.connect_to_signal("Disabled", cb_activated)
651 inputcapture_intf.connect_to_signal("Activated", cb_activated)
652 inputcapture_intf.connect_to_signal("Deactivated", cb_deactivated)
653
654 self.enable(dbus_con)
655
656 xdp.wait_for(lambda: activated_signal_received)
657 assert activation_id is not None
658 assert not deactivated_signal_received
659 assert not disabled_signal_received
660
661 self.release(
662 dbus_con, cursor_position=(10.0, 50.0), activation_id=activation_id
663 )
664
665 # XDP should filter any signals the implementation may
666 # send after Release().
667
668 mainloop = GLib.MainLoop()
669 GLib.timeout_add(1000, mainloop.quit)
670 mainloop.run()
671
672 # Release() implies deactivated
673 assert not deactivated_signal_received
674 assert not disabled_signal_received