Debian Codegraph @

tests/templates/geoclue2.py

raw symbols SCIP
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 # mypy: disable-error-code="misc"
3
4 from tests.templates.xdp_utils import init_logger
5
6 import dbus.service
7 import dbus
8
9 from dbusmock import mockobject
10
11 BUS_NAME = "org.freedesktop.GeoClue2"
12 MAIN_OBJ = "/org/freedesktop/GeoClue2/Manager"
13 MAIN_IFACE = "org.freedesktop.GeoClue2.Manager"
14 CLIENT_IFACE = "org.freedesktop.GeoClue2.Client"
15 LOCATION_IFACE = "org.freedesktop.GeoClue2.Location"
16 MOCK_IFACE = "org.freedesktop.GeoClue2.Mock"
17 SYSTEM_BUS = True
18 VERSION = 1
19
20 logger = init_logger(__name__)
21
22
23 class GeoClueClient(mockobject.DBusMockObject):
24 def __init__(self, *args, **kwargs):
25 super(GeoClueClient, self).__init__(*args, **kwargs)
26
27 self.started = False
28 self.location = 0
29 self.data = {"Latitude": 0, "Longitude": 0, "Accuracy": 0}
30
31 @dbus.service.method(
32 CLIENT_IFACE,
33 in_signature="",
34 out_signature="",
35 )
36 def Start(self):
37 logger.debug("Start()")
38 self.started = True
39 self.ChangeLocation(self.data)
40
41 @dbus.service.method(
42 CLIENT_IFACE,
43 in_signature="",
44 out_signature="",
45 )
46 def Stop(self):
47 logger.debug("Stop()")
48 self.started = False
49 self.RemoveObject(f"/org/freedesktop/GeoClue2/Location/{self.location}")
50
51 @dbus.service.method(
52 MOCK_IFACE,
53 in_signature="a{sv}",
54 out_signature="",
55 )
56 def ChangeLocation(self, data):
57 logger.debug(f"ChangeLocation({data})")
58
59 self.data = data
60
61 if not self.started:
62 return
63
64 old_path = "/"
65 if self.location > 0:
66 old_path = f"/org/freedesktop/GeoClue2/Location/{self.location}"
67 self.location = self.location + 1
68 new_path = f"/org/freedesktop/GeoClue2/Location/{self.location}"
69
70 self.AddObject(
71 new_path,
72 LOCATION_IFACE,
73 {
74 "Latitude": data["Latitude"],
75 "Longitude": data["Longitude"],
76 "Accuracy": data["Accuracy"],
77 },
78 [],
79 )
80
81 if old_path != "/":
82 self.RemoveObject(old_path)
83
84 self.EmitSignal(
85 CLIENT_IFACE,
86 "LocationUpdated",
87 "oo",
88 [
89 dbus.ObjectPath(old_path),
90 dbus.ObjectPath(new_path),
91 ],
92 )
93
94
95 def load(mock, parameters={}):
96 mock.AddMethods(
97 MAIN_IFACE,
98 [
99 (
100 "GetClient",
101 "",
102 "o",
103 'ret = dbus.ObjectPath("/org/freedesktop/GeoClue2/Client/1")',
104 ),
105 ],
106 )
107 mock.AddObject(
108 "/org/freedesktop/GeoClue2/Client/1",
109 CLIENT_IFACE,
110 {
111 "DesktopId": "",
112 "DistanceThreshold": 0,
113 "TimeThreshold": 0,
114 "RequestedAccuracyLevel": 0,
115 },
116 [],
117 mock_class=GeoClueClient,
118 )