Debian Codegraph @

tests/test_location.py

raw
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2
3 import tests.xdp_utils as xdp
4
5 import pytest
6 import dbus
7
8
9 @pytest.fixture
10 def required_templates():
11 return {
12 "geoclue2": {},
13 "access": {},
14 }
15
16
17 class TestLocation:
18 def test_version(self, portals, dbus_con):
19 xdp.check_version(dbus_con, "Location", 1)
20
21 def get_geoclue_mock(self, dbus_con_sys):
22 geoclue_manager_proxy = dbus_con_sys.get_object(
23 "org.freedesktop.GeoClue2",
24 "/org/freedesktop/GeoClue2/Manager",
25 )
26 geoclue_manager = dbus.Interface(
27 geoclue_manager_proxy, "org.freedesktop.GeoClue2.Manager"
28 )
29 geoclue_client_proxy = dbus_con_sys.get_object(
30 "org.freedesktop.GeoClue2", geoclue_manager.GetClient()
31 )
32 geoclue_mock = dbus.Interface(
33 geoclue_client_proxy, "org.freedesktop.GeoClue2.Mock"
34 )
35 return geoclue_mock
36
37 @pytest.mark.parametrize("required_templates", ({},))
38 def test_no_geoclue(self, portals, dbus_con):
39 location_intf = xdp.get_portal_iface(dbus_con, "Location")
40
41 session = xdp.Session(
42 dbus_con,
43 location_intf.CreateSession({"session_handle_token": "session_token0"}),
44 )
45
46 start_session_request = xdp.Request(dbus_con, location_intf)
47 start_session_response = start_session_request.call(
48 "Start",
49 session_handle=session.handle,
50 parent_window="window-hndl",
51 options={},
52 )
53
54 assert start_session_response
55 assert start_session_response.response == 2
56
57 def test_session_update(self, portals, dbus_con, dbus_con_sys):
58 location_intf = xdp.get_portal_iface(dbus_con, "Location")
59 geoclue_mock_intf = self.get_geoclue_mock(dbus_con_sys)
60
61 location_updated = False
62 updated_count = 0
63
64 session = xdp.Session(
65 dbus_con,
66 location_intf.CreateSession({"session_handle_token": "session_token0"}),
67 )
68
69 def cb_location_updated(session_handle, location):
70 nonlocal location_updated
71 nonlocal updated_count
72
73 if updated_count == 0:
74 assert location["Latitude"] == 0
75 assert location["Longitude"] == 0
76 assert location["Accuracy"] == 0
77 elif updated_count == 1:
78 assert location["Latitude"] == 11
79 assert location["Longitude"] == 22
80 assert location["Accuracy"] == 3
81
82 updated_count += 1
83 location_updated = True
84
85 location_intf.connect_to_signal("LocationUpdated", cb_location_updated)
86
87 start_session_request = xdp.Request(dbus_con, location_intf)
88 start_session_response = start_session_request.call(
89 "Start",
90 session_handle=session.handle,
91 parent_window="window-hndl",
92 options={},
93 )
94
95 assert start_session_response
96 assert start_session_response.response == 0
97
98 xdp.wait_for(lambda: location_updated)
99 location_updated = False
100
101 assert updated_count == 1
102
103 geoclue_mock_intf.ChangeLocation(
104 {
105 "Latitude": dbus.UInt32(11),
106 "Longitude": dbus.UInt32(22),
107 "Accuracy": dbus.UInt32(3),
108 }
109 )
110
111 xdp.wait_for(lambda: location_updated)
112 location_updated = False
113
114 assert updated_count == 2
115
116 def test_bad_accuracy(self, portals, dbus_con):
117 location_intf = xdp.get_portal_iface(dbus_con, "Location")
118 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
119 location_intf.CreateSession(
120 {
121 "session_handle_token": "session_token0",
122 "accuracy": dbus.UInt32(22),
123 }
124 )
125 assert (
126 excinfo.value.get_dbus_name()
127 == "org.freedesktop.portal.Error.InvalidArgument"
128 )