| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from tests.templates 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 |
def load(mock, parameters={}): |
| 24 |
mock.AddMethods( |
| 25 |
MAIN_IFACE, |
| 26 |
[ |
| 27 |
( |
| 28 |
"GetClient", |
| 29 |
"", |
| 30 |
"o", |
| 31 |
'ret = dbus.ObjectPath("/org/freedesktop/GeoClue2/Client/1")', |
| 32 |
), |
| 33 |
], |
| 34 |
) |
| 35 |
mock.AddObject( |
| 36 |
"/org/freedesktop/GeoClue2/Client/1", |
| 37 |
CLIENT_IFACE, |
| 38 |
{ |
| 39 |
"DesktopId": "", |
| 40 |
"DistanceThreshold": 0, |
| 41 |
"TimeThreshold": 0, |
| 42 |
"RequestedAccuracyLevel": 0, |
| 43 |
}, |
| 44 |
[ |
| 45 |
("Start", "", "", Start), |
| 46 |
("Stop", "", "", Stop), |
| 47 |
], |
| 48 |
) |
| 49 |
mock.client = mockobject.objects["/org/freedesktop/GeoClue2/Client/1"] |
| 50 |
mock.client.manager = mock |
| 51 |
mock.client.started = False |
| 52 |
mock.client.location = 0 |
| 53 |
mock.client.props = {"Latitude": 0, "Longitude": 0, "Accuracy": 0} |
| 54 |
|
| 55 |
mock.client.AddMethod(MOCK_IFACE, "ChangeLocation", "a{sv}", "", ChangeLocation) |
| 56 |
|
| 57 |
|
| 58 |
@dbus.service.method( |
| 59 |
CLIENT_IFACE, |
| 60 |
in_signature="", |
| 61 |
out_signature="", |
| 62 |
) |
| 63 |
def Start(self): |
| 64 |
logger.debug("Start()") |
| 65 |
self.started = True |
| 66 |
self.ChangeLocation(self.props) |
| 67 |
|
| 68 |
|
| 69 |
@dbus.service.method( |
| 70 |
CLIENT_IFACE, |
| 71 |
in_signature="", |
| 72 |
out_signature="", |
| 73 |
) |
| 74 |
def Stop(self): |
| 75 |
logger.debug("Stop()") |
| 76 |
self.started = False |
| 77 |
self.RemoveObject(f"/org/freedesktop/GeoClue2/Location/{self.location}") |
| 78 |
|
| 79 |
|
| 80 |
@dbus.service.method( |
| 81 |
MOCK_IFACE, |
| 82 |
in_signature="a{sv}", |
| 83 |
out_signature="", |
| 84 |
) |
| 85 |
def ChangeLocation(self, props): |
| 86 |
logger.debug(f"ChangeLocation({props})") |
| 87 |
|
| 88 |
self.props = props |
| 89 |
|
| 90 |
if not self.started: |
| 91 |
return |
| 92 |
|
| 93 |
old_path = "/" |
| 94 |
if self.location > 0: |
| 95 |
old_path = f"/org/freedesktop/GeoClue2/Location/{self.location}" |
| 96 |
self.location = self.location + 1 |
| 97 |
new_path = f"/org/freedesktop/GeoClue2/Location/{self.location}" |
| 98 |
|
| 99 |
self.AddObject( |
| 100 |
new_path, |
| 101 |
LOCATION_IFACE, |
| 102 |
{ |
| 103 |
"Latitude": props["Latitude"], |
| 104 |
"Longitude": props["Longitude"], |
| 105 |
"Accuracy": props["Accuracy"], |
| 106 |
}, |
| 107 |
[], |
| 108 |
) |
| 109 |
|
| 110 |
if old_path != "/": |
| 111 |
self.RemoveObject(old_path) |
| 112 |
|
| 113 |
self.EmitSignal( |
| 114 |
CLIENT_IFACE, |
| 115 |
"LocationUpdated", |
| 116 |
"oo", |
| 117 |
[ |
| 118 |
dbus.ObjectPath(old_path), |
| 119 |
dbus.ObjectPath(new_path), |
| 120 |
], |
| 121 |
) |