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.xdp_utils 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 InputcaptureSession:
33 def __init__(self, dbus_con):
34 self.dbus_con = dbus_con
35 self.type = None
36 self.session = None
37 self.session_handle = None
38 self.current_zone_set = []
39
40 def create_legacy(self, capabilities=0x7):
41 """
42 Call CreateSession for the given capabilities and return the
43 (response, results) tuple.
44 """
45
46 assert self.type is None
47 self.type = "legacy"
48
49 inputcapture_intf = xdp.get_portal_iface(self.dbus_con, "InputCapture")
50 mock_intf = xdp.get_mock_iface(self.dbus_con)
51
52 request = xdp.Request(self.dbus_con, inputcapture_intf)
53
54 capabilities = dbus.UInt32(capabilities, variant_level=1)
55 session_handle_token = dbus.String(f"session{next(counter)}", variant_level=1)
56
57 options = dbus.Dictionary(
58 {
59 "capabilities": capabilities,
60 "session_handle_token": session_handle_token,
61 },
62 signature="sv",
63 )
64
65 response = request.call("CreateSession", parent_window="", options=options)
66
67 assert response
68 assert response.response == 0
69 assert "session_handle" in response.results
70 assert "capabilities" in response.results
71 caps = response.results["capabilities"]
72 # Returned capabilities must be a subset of the requested ones
73 assert caps & ~capabilities == 0
74
75 self.session = xdp.Session(self.dbus_con, response.results["session_handle"])
76 self.session_handle = self.session.handle
77
78 # Check the impl portal was called with the right args
79 method_calls = mock_intf.GetMethodCalls("Start")
80 assert len(method_calls) > 0
81 _, args = method_calls[-1]
82 assert args[3] == "" # parent window
83 assert args[4]["capabilities"] == capabilities
84
85 return response
86
87 def create(self):
88 """
89 Call CreateSession for the given capabilities and return the
90 (response, results) tuple.
91 """
92
93 assert self.type is None
94 self.type = "modern"
95
96 inputcapture_intf = xdp.get_portal_iface(self.dbus_con, "InputCapture")
97
98 session_handle_token = dbus.String(f"session{next(counter)}", variant_level=1)
99
100 result = inputcapture_intf.CreateSession2(
101 {"session_handle_token": session_handle_token}
102 )
103 self.session = xdp.Session(self.dbus_con, result["session_handle"])
104 self.session_handle = self.session.handle
105
106 def start(
107 self,
108 capabilities=0x7,
109 persist_mode: xdp.SessionPersistenceMode | None = None,
110 restore_token: str | None = None,
111 validate_token_data: bool = True,
112 ):
113 assert self.type == "modern"
114
115 inputcapture_intf = xdp.get_portal_iface(self.dbus_con, "InputCapture")
116 mock_intf = xdp.get_mock_iface(self.dbus_con)
117
118 capabilities = dbus.UInt32(capabilities, variant_level=1)
119 request = xdp.Request(self.dbus_con, inputcapture_intf)
120 options = {
121 "capabilities": capabilities,
122 }
123 if persist_mode is not None:
124 options["persist_mode"] = dbus.UInt32(persist_mode, variant_level=1)
125 if restore_token is not None:
126 options["restore_token"] = restore_token
127 response = request.call(
128 "Start",
129 session_handle=self.session_handle,
130 parent_window="",
131 options=options,
132 )
133
134 assert response
135 assert response.response == 0
136 assert "capabilities" in response.results
137 caps = response.results["capabilities"]
138 # Returned capabilities must be a subset of the requested ones
139 assert caps & ~capabilities == 0
140
141 # Check the impl portal was called with the right args
142 method_calls = mock_intf.GetMethodCalls("Start")
143 assert len(method_calls) > 0
144 _, args = method_calls[-1]
145 assert args[3] == "" # parent window
146 options = args[4]
147 assert options["capabilities"] == capabilities
148 if persist_mode is not None:
149 assert options["persist_mode"] == dbus.UInt32(persist_mode, variant_level=1)
150 if restore_token is not None and validate_token_data:
151 restore_data = options["restore_data"]
152 assert restore_data is not None
153 # Vendor-name and version are hardcoded in the template
154 assert restore_data[0] == "GNOME"
155 assert restore_data[1] == dbus.UInt32(7)
156
157 return response
158
159 def get_zones(self):
160 """
161 Call GetZones and return the (response, results) tuple.
162 """
163 inputcapture_intf = xdp.get_portal_iface(self.dbus_con, "InputCapture")
164 mock_intf = xdp.get_mock_iface(self.dbus_con)
165
166 request = xdp.Request(self.dbus_con, inputcapture_intf)
167 options: Any = {}
168 response = request.call(
169 "GetZones", session_handle=self.session_handle, options=options
170 )
171
172 assert response
173 assert response.response == 0
174 assert "zones" in response.results
175 assert "zone_set" in response.results
176
177 self.current_zone_set = response.results["zone_set"]
178
179 # Check the impl portal was called with the right args
180 method_calls = mock_intf.GetMethodCalls("GetZones")
181 assert len(method_calls) > 0
182 _, args = method_calls[-1]
183 assert args[0] == request.handle
184 assert args[1] == self.session_handle
185
186 return response
187
188 def set_pointer_barriers(self, barriers):
189 inputcapture_intf = xdp.get_portal_iface(self.dbus_con, "InputCapture")
190 mock_intf = xdp.get_mock_iface(self.dbus_con)
191
192 request = xdp.Request(self.dbus_con, inputcapture_intf)
193 options: Any = {}
194 response = request.call(
195 "SetPointerBarriers",
196 session_handle=self.session_handle,
197 options=options,
198 barriers=barriers,
199 zone_set=self.current_zone_set,
200 )
201 assert response
202 assert response.response == 0
203 assert "failed_barriers" in response.results
204
205 # Check the impl portal was called with the right args
206 method_calls = mock_intf.GetMethodCalls("SetPointerBarriers")
207 assert len(method_calls) > 0
208 _, args = method_calls[-1]
209 assert args[0] == request.handle
210 assert args[1] == self.session_handle
211 assert args[4] == barriers
212 assert args[5] == self.current_zone_set
213
214 return response
215
216 def connect_to_eis(self):
217 inputcapture_intf = xdp.get_portal_iface(self.dbus_con, "InputCapture")
218 mock_intf = xdp.get_mock_iface(self.dbus_con)
219
220 fd = inputcapture_intf.ConnectToEIS(
221 self.session_handle, dbus.Dictionary({}, signature="sv")
222 )
223
224 # Our dbusmock template sends HELLO
225 eis_socket = socket.fromfd(fd.take(), socket.AF_UNIX, socket.SOCK_STREAM)
226 hello = eis_socket.recv(10)
227 assert hello == b"HELLO"
228
229 method_calls = mock_intf.GetMethodCalls("ConnectToEIS")
230 assert len(method_calls) > 0
231 _, args = method_calls[-1]
232 assert args[0] == self.session_handle
233
234 return eis_socket
235
236 def enable(self):
237 inputcapture_intf = xdp.get_portal_iface(self.dbus_con, "InputCapture")
238 mock_intf = xdp.get_mock_iface(self.dbus_con)
239
240 inputcapture_intf.Enable(
241 self.session_handle, dbus.Dictionary({}, signature="sv")
242 )
243
244 method_calls = mock_intf.GetMethodCalls("Enable")
245 assert len(method_calls) > 0
246 _, args = method_calls[-1]
247 assert args[0] == self.session_handle
248
249 def disable(self):
250 inputcapture_intf = xdp.get_portal_iface(self.dbus_con, "InputCapture")
251 mock_intf = xdp.get_mock_iface(self.dbus_con)
252
253 inputcapture_intf.Disable(
254 self.session_handle, dbus.Dictionary({}, signature="sv")
255 )
256
257 method_calls = mock_intf.GetMethodCalls("Disable")
258 assert len(method_calls) > 0
259 _, args = method_calls[-1]
260 assert args[0] == self.session_handle
261
262 def release(self, activation_id: int, cursor_position=None):
263 inputcapture_intf = xdp.get_portal_iface(self.dbus_con, "InputCapture")
264 mock_intf = xdp.get_mock_iface(self.dbus_con)
265
266 options = {"activation_id": dbus.UInt32(activation_id)}
267 if cursor_position:
268 options["cursor_position"] = dbus.Struct(
269 list(cursor_position), signature="dd", variant_level=1
270 )
271
272 inputcapture_intf.Release(
273 self.session_handle, dbus.Dictionary(options, signature="sv")
274 )
275
276 method_calls = mock_intf.GetMethodCalls("Release")
277 assert len(method_calls) > 0
278 _, args = method_calls[-1]
279 assert args[0] == self.session_handle
280 assert "activation_id" in args[2]
281 aid = args[2]["activation_id"]
282 assert aid == activation_id
283 if cursor_position:
284 assert "cursor_position" in args[2]
285 pos = args[2]["cursor_position"]
286 assert pos == cursor_position
287
288
289 class TestInputCapture:
290 def test_version(self, portals, dbus_con):
291 xdp.check_version(dbus_con, "InputCapture", 2)
292
293 @pytest.mark.parametrize(
294 "template_params",
295 (
296 {
297 "inputcapture": {
298 "supported_capabilities": 0b101, # KEYBOARD, POINTER, TOUCH
299 },
300 },
301 ),
302 )
303 def test_supported_capabilities(self, portals, dbus_con):
304 properties_intf = xdp.get_iface(dbus_con, "org.freedesktop.DBus.Properties")
305
306 caps = properties_intf.Get(
307 "org.freedesktop.portal.InputCapture", "SupportedCapabilities"
308 )
309 assert caps == 0b101
310
311 def test_create_session(self, portals, dbus_con):
312 session = InputcaptureSession(dbus_con)
313 session.create() # KEYBOARD
314 session.start(capabilities=0b1) # KEYBOARD
315 session = InputcaptureSession(dbus_con)
316 session.create_legacy(capabilities=0b1) # KEYBOARD
317
318 @pytest.mark.parametrize(
319 "template_params",
320 (
321 {
322 "inputcapture": {
323 "capabilities": 0b110, # TOUCH, POINTER
324 "supported_capabilities": 0b111, # TOUCH, POINTER, KEYBOARD
325 },
326 },
327 ),
328 )
329 def test_create_session_limited_caps(self, portals, dbus_con):
330 # Request more caps than are supported
331 session = InputcaptureSession(dbus_con)
332 session.create()
333 response, results = session.start(capabilities=0b111)
334 caps = results["capabilities"]
335 # Returned capabilities must the ones we set up in the params
336 assert caps == 0b110
337
338 @pytest.mark.parametrize(
339 "template_params",
340 (
341 {
342 "inputcapture": {
343 "default-zone": dbus.Array(
344 [dbus.Struct(z, signature="uuii") for z in default_zones()],
345 signature="(uuii)",
346 variant_level=1,
347 )
348 },
349 },
350 ),
351 )
352 def test_get_zones(self, portals, dbus_con, zones):
353 mock_intf = xdp.get_mock_iface(dbus_con)
354
355 session = InputcaptureSession(dbus_con)
356 session.create()
357 response, results = session.start()
358 response, results = session.get_zones()
359 for z1, z2 in zip(results["zones"], zones):
360 assert z1 == z2
361
362 # Check the impl portal was called with the right args
363 method_calls = mock_intf.GetMethodCalls("Start")
364 assert len(method_calls) == 1
365 method_calls = mock_intf.GetMethodCalls("GetZones")
366 assert len(method_calls) == 1
367
368 @pytest.mark.parametrize(
369 "template_params",
370 (
371 {
372 "inputcapture": {
373 "default-zone": dbus.Array(
374 [dbus.Struct(z, signature="uuii") for z in default_zones()],
375 signature="(uuii)",
376 variant_level=1,
377 )
378 },
379 },
380 ),
381 )
382 def test_set_pointer_barriers(self, portals, dbus_con, zones):
383 mock_intf = xdp.get_mock_iface(dbus_con)
384
385 session = InputcaptureSession(dbus_con)
386 session.create()
387 response, results = session.start()
388 response, results = session.get_zones()
389
390 barriers = [
391 {
392 "barrier_id": dbus.UInt32(10, variant_level=1),
393 "position": dbus.Struct(
394 [0, 0, 0, 768], signature="iiii", variant_level=1
395 ),
396 },
397 {
398 "barrier_id": dbus.UInt32(11, variant_level=1),
399 "position": dbus.Struct(
400 [0, 0, 1024, 0], signature="iiii", variant_level=1
401 ),
402 },
403 {
404 "barrier_id": dbus.UInt32(12, variant_level=1),
405 "position": dbus.Struct(
406 [1024, 0, 1024, 768], signature="iiii", variant_level=1
407 ),
408 },
409 {
410 "barrier_id": dbus.UInt32(13, variant_level=1),
411 "position": dbus.Struct(
412 [0, 768, 1024, 768], signature="iiii", variant_level=1
413 ),
414 },
415 {
416 "barrier_id": dbus.UInt32(14, variant_level=1),
417 "position": dbus.Struct(
418 [100, 768, 500, 768], signature="iiii", variant_level=1
419 ),
420 },
421 {
422 "barrier_id": dbus.UInt32(15, variant_level=1),
423 "position": dbus.Struct(
424 [1024, 0, 1024, 480], signature="iiii", variant_level=1
425 ),
426 },
427 {
428 "barrier_id": dbus.UInt32(16, variant_level=1),
429 "position": dbus.Struct(
430 [1024 + 640, 0, 1024 + 640, 480], signature="iiii", variant_level=1
431 ),
432 },
433 # invalid ones
434 {
435 "barrier_id": dbus.UInt32(20, variant_level=1),
436 "position": dbus.Struct(
437 [0, 1, 3, 4], signature="iiii", variant_level=1
438 ),
439 },
440 {
441 "barrier_id": dbus.UInt32(21, variant_level=1),
442 "position": dbus.Struct(
443 [0, 1, 1024, 1], signature="iiii", variant_level=1
444 ),
445 },
446 {
447 "barrier_id": dbus.UInt32(22, variant_level=1),
448 "position": dbus.Struct(
449 [1, 0, 1, 768], signature="iiii", variant_level=1
450 ),
451 },
452 {
453 "barrier_id": dbus.UInt32(23, variant_level=1),
454 "position": dbus.Struct(
455 [1023, 0, 1023, 768], signature="iiii", variant_level=1
456 ),
457 },
458 {
459 "barrier_id": dbus.UInt32(24, variant_level=1),
460 "position": dbus.Struct(
461 [0, 0, 1050, 0], signature="iiii", variant_level=1
462 ),
463 },
464 ]
465 response, results = session.set_pointer_barriers(barriers=barriers)
466 failed_barriers = results["failed_barriers"]
467 assert all([id >= 20 for id in failed_barriers])
468
469 for id in [b["barrier_id"] for b in barriers if b["barrier_id"] >= 20]:
470 assert id in failed_barriers
471
472 # Check the impl portal was called with the right args
473 method_calls = mock_intf.GetMethodCalls("Start")
474 assert len(method_calls) == 1
475 method_calls = mock_intf.GetMethodCalls("GetZones")
476 assert len(method_calls) == 1
477 method_calls = mock_intf.GetMethodCalls("SetPointerBarriers")
478 assert len(method_calls) == 1
479 _, args = method_calls.pop(0)
480 assert args[4] == barriers
481 assert args[5] == session.current_zone_set
482
483 def test_connect_to_eis(self, portals, dbus_con):
484 session = InputcaptureSession(dbus_con)
485 session.create()
486 session.start()
487 session.get_zones()
488
489 # The default zone is 1920x1080
490 barriers = [
491 {
492 "barrier_id": dbus.UInt32(10, variant_level=1),
493 "position": dbus.Struct(
494 [0, 0, 1920, 0], signature="iiii", variant_level=1
495 ),
496 },
497 ]
498 session.set_pointer_barriers(barriers)
499
500 session.connect_to_eis()
501
502 def test_enable_disable(self, portals, dbus_con):
503 mock_intf = xdp.get_mock_iface(dbus_con)
504
505 session = InputcaptureSession(dbus_con)
506 session.create()
507 session.start()
508 session.get_zones()
509
510 # The default zone is 1920x1080
511 barriers = [
512 {
513 "barrier_id": dbus.UInt32(10, variant_level=1),
514 "position": dbus.Struct(
515 [0, 0, 1920, 0], signature="iiii", variant_level=1
516 ),
517 },
518 ]
519 session.set_pointer_barriers(barriers)
520 session.connect_to_eis()
521
522 # Disable before enable should be a noop
523 session.disable()
524
525 method_calls = mock_intf.GetMethodCalls("Disable")
526 assert len(method_calls) == 1
527
528 session.enable()
529 method_calls = mock_intf.GetMethodCalls("Enable")
530 assert len(method_calls) == 1
531
532 session.disable()
533 method_calls = mock_intf.GetMethodCalls("Disable")
534 assert len(method_calls) == 2
535
536 @pytest.mark.parametrize(
537 "template_params",
538 (
539 {
540 "inputcapture": {
541 "disable-delay": 200,
542 },
543 },
544 ),
545 )
546 def test_disable_signal(self, portals, dbus_con):
547 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
548
549 session = InputcaptureSession(dbus_con)
550 session.create()
551 session.start()
552 session.get_zones()
553 # The default zone is 1920x1080
554 barriers = [
555 {
556 "barrier_id": dbus.UInt32(10, variant_level=1),
557 "position": dbus.Struct(
558 [0, 0, 1920, 0], signature="iiii", variant_level=1
559 ),
560 },
561 ]
562 session.set_pointer_barriers(barriers)
563 session.connect_to_eis()
564
565 disabled_signal_received = False
566
567 def cb_disabled(session_handle, options):
568 nonlocal disabled_signal_received
569 disabled_signal_received = True
570 assert session_handle == session_handle
571
572 inputcapture_intf.connect_to_signal("Disabled", cb_disabled)
573 session.enable()
574 xdp.wait_for(lambda: disabled_signal_received)
575
576 @pytest.mark.parametrize(
577 "template_params",
578 (
579 {
580 "inputcapture": {
581 "activated-delay": 200,
582 "deactivated-delay": 300,
583 },
584 },
585 ),
586 )
587 def test_activated_signal(self, portals, dbus_con):
588 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
589
590 session = InputcaptureSession(dbus_con)
591 session.create()
592 session.start()
593 session.get_zones()
594 # The default zone is 1920x1080
595 barriers = [
596 {
597 "barrier_id": dbus.UInt32(10, variant_level=1),
598 "position": dbus.Struct(
599 [0, 0, 1920, 0], signature="iiii", variant_level=1
600 ),
601 },
602 ]
603 session.set_pointer_barriers(barriers)
604 session.connect_to_eis()
605
606 disabled_signal_received = False
607 activated_signal_received = False
608 deactivated_signal_received = False
609
610 def cb_disabled(session_handle, options):
611 nonlocal disabled_signal_received
612 disabled_signal_received = True
613
614 def cb_activated(session_handle, options):
615 nonlocal activated_signal_received
616 activated_signal_received = True
617 assert session_handle == session_handle
618 assert "activation_id" in options
619 assert "barrier_id" in options
620 assert options["barrier_id"] == 10 # template uses first barrier
621 assert "cursor_position" in options
622 assert options["cursor_position"] == (
623 10.0,
624 20.0,
625 ) # template uses x+10, y+20 of first barrier
626
627 def cb_deactivated(session_handle, options):
628 nonlocal deactivated_signal_received
629 deactivated_signal_received = True
630 assert session_handle == session_handle
631 assert "activation_id" in options
632 assert "cursor_position" in options
633 assert options["cursor_position"] == (
634 10.0,
635 20.0,
636 ) # template uses x+10, y+20 of first barrier
637
638 inputcapture_intf.connect_to_signal("Activated", cb_activated)
639 inputcapture_intf.connect_to_signal("Deactivated", cb_deactivated)
640 inputcapture_intf.connect_to_signal("Disabled", cb_disabled)
641
642 session.enable()
643
644 xdp.wait_for(lambda: activated_signal_received and deactivated_signal_received)
645 assert not disabled_signal_received
646
647 # Disabling should not trigger the signal
648 session.disable()
649 assert not disabled_signal_received
650
651 @pytest.mark.parametrize(
652 "template_params",
653 (
654 {
655 "inputcapture": {
656 "zones-changed-delay": 200,
657 },
658 },
659 ),
660 )
661 def test_zones_changed_signal(self, portals, dbus_con):
662 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
663
664 session = InputcaptureSession(dbus_con)
665 session.create()
666 session.start()
667 session.get_zones()
668 # The default zone is 1920x1080
669 barriers = [
670 {
671 "barrier_id": dbus.UInt32(10, variant_level=1),
672 "position": dbus.Struct(
673 [0, 0, 1920, 0], signature="iiii", variant_level=1
674 ),
675 },
676 ]
677 session.set_pointer_barriers(barriers)
678 session.connect_to_eis()
679
680 zones_changed_signal_received = False
681
682 def cb_zones_changed(session_handle, options):
683 nonlocal zones_changed_signal_received
684 zones_changed_signal_received = True
685 assert session_handle == session_handle
686
687 inputcapture_intf.connect_to_signal("ZonesChanged", cb_zones_changed)
688 session.enable()
689 xdp.wait_for(lambda: zones_changed_signal_received)
690
691 @pytest.mark.parametrize(
692 "template_params",
693 (
694 {
695 "inputcapture": {
696 "activated-delay": 200,
697 "deactivated-delay": 1000,
698 "disabled-delay": 1200,
699 },
700 },
701 ),
702 )
703 def test_release(self, portals, dbus_con):
704 inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture")
705
706 session = InputcaptureSession(dbus_con)
707 session.create()
708 session.start()
709 session.get_zones()
710 # The default zone is 1920x1080
711 barriers = [
712 {
713 "barrier_id": dbus.UInt32(10, variant_level=1),
714 "position": dbus.Struct(
715 [0, 0, 1920, 0], signature="iiii", variant_level=1
716 ),
717 },
718 ]
719 session.set_pointer_barriers(barriers)
720 session.connect_to_eis()
721
722 disabled_signal_received = False
723 activated_signal_received = False
724 deactivated_signal_received = False
725 activation_id = None
726
727 def cb_disabled(session_handle, options):
728 nonlocal disabled_signal_received
729 disabled_signal_received = True
730
731 def cb_activated(session_handle, options):
732 nonlocal activated_signal_received, activation_id
733 activated_signal_received = True
734 activation_id = options["activation_id"]
735
736 def cb_deactivated(session_handle, options):
737 nonlocal deactivated_signal_received
738 deactivated_signal_received = True
739
740 inputcapture_intf.connect_to_signal("Disabled", cb_disabled)
741 inputcapture_intf.connect_to_signal("Activated", cb_activated)
742 inputcapture_intf.connect_to_signal("Deactivated", cb_deactivated)
743
744 session.enable()
745
746 xdp.wait_for(lambda: activated_signal_received)
747 assert activation_id is not None
748 assert not deactivated_signal_received
749 assert not disabled_signal_received
750
751 session.release(cursor_position=(10.0, 50.0), activation_id=activation_id)
752
753 # XDP should filter any signals the implementation may
754 # send after Release().
755
756 mainloop = GLib.MainLoop()
757 GLib.timeout_add(1000, mainloop.quit)
758 mainloop.run()
759
760 # Release() implies deactivated
761 assert not deactivated_signal_received
762 assert not disabled_signal_received
763
764 @pytest.mark.parametrize(
765 "mode",
766 [xdp.SessionPersistenceMode.TRANSIENT, xdp.SessionPersistenceMode.PERSISTENT],
767 )
768 def test_persistence(self, portals, dbus_con, mode):
769 def create_persistent_session(
770 persist_mode: xdp.SessionPersistenceMode,
771 restore_token: str | None,
772 validate_token_data: bool = True,
773 ):
774 ic_session = InputcaptureSession(dbus_con)
775 ic_session.create()
776 response = ic_session.start(
777 restore_token=restore_token,
778 persist_mode=persist_mode,
779 validate_token_data=validate_token_data,
780 )
781 restore_token = response.results.get("restore_token")
782 session = xdp.Session.from_response(dbus_con, response)
783 session.close()
784 return restore_token
785
786 token1 = create_persistent_session(persist_mode=mode, restore_token=None)
787 assert token1 is not None
788
789 # Current implementation keeps the restore token for the same data
790 token2 = create_persistent_session(persist_mode=mode, restore_token=token1)
791 assert token2 is not None
792 assert token2 == token1
793
794 # A new session with different token data
795 token3 = create_persistent_session(persist_mode=mode, restore_token=None)
796 assert token3 is not None
797 assert token3 != token1
798
799 # First session again
800 token4 = create_persistent_session(persist_mode=mode, restore_token=token1)
801 assert token4 is not None
802 assert token4 == token1
803
804 # End the first session
805 token5 = create_persistent_session(
806 persist_mode=xdp.SessionPersistenceMode.NONE, restore_token=token1
807 )
808 assert token5 is None
809
810 # token1 is invalid now
811 token6 = create_persistent_session(
812 persist_mode=xdp.SessionPersistenceMode.PERSISTENT,
813 restore_token=token1,
814 validate_token_data=False,
815 )
816 assert token6 is not None
817 assert token6 != token1
818
819 # but token3 is still there
820 token7 = create_persistent_session(persist_mode=mode, restore_token=token3)
821 assert token7 is not None
822 assert token7 == token3