Debian Codegraph @

tests/test_location.py

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