| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# |
| 3 |
# This file is formatted with Python Black |
| 4 |
# mypy: disable-error-code="misc" |
| 5 |
|
| 6 |
from tests.templates.xdp_utils import Response, init_logger, ImplRequest, ImplSession |
| 7 |
|
| 8 |
import dbus.service |
| 9 |
from gi.repository import GLib |
| 10 |
from enum import Enum |
| 11 |
from dbusmock import MOCK_IFACE |
| 12 |
from dataclasses import dataclass |
| 13 |
|
| 14 |
|
| 15 |
BUS_NAME = "org.freedesktop.impl.portal.Test" |
| 16 |
MAIN_OBJ = "/org/freedesktop/portal/desktop" |
| 17 |
SYSTEM_BUS = False |
| 18 |
MAIN_IFACE = "org.freedesktop.impl.portal.Inhibit" |
| 19 |
VERSION = 3 |
| 20 |
|
| 21 |
|
| 22 |
logger = init_logger(__name__) |
| 23 |
|
| 24 |
|
| 25 |
class SessionState(Enum): |
| 26 |
RUNNING = 1 |
| 27 |
QUERY_END = 2 |
| 28 |
ENDING = 3 |
| 29 |
|
| 30 |
|
| 31 |
@dataclass |
| 32 |
class InhibitParameters: |
| 33 |
delay: int |
| 34 |
response: int |
| 35 |
expect_close: bool |
| 36 |
|
| 37 |
|
| 38 |
def load(mock, parameters={}): |
| 39 |
logger.debug(f"Loading parameters: {parameters}") |
| 40 |
|
| 41 |
assert not hasattr(mock, "inhibit_params") |
| 42 |
mock.inhibit_params = InhibitParameters( |
| 43 |
delay=parameters.get("delay", 200), |
| 44 |
response=parameters.get("response", 0), |
| 45 |
expect_close=parameters.get("expect-close", False), |
| 46 |
) |
| 47 |
|
| 48 |
mock.AddProperties( |
| 49 |
MAIN_IFACE, |
| 50 |
dbus.Dictionary( |
| 51 |
{ |
| 52 |
"version": dbus.UInt32(parameters.get("version", VERSION)), |
| 53 |
} |
| 54 |
), |
| 55 |
) |
| 56 |
mock.sessions: dict[str, ImplSession] = {} |
| 57 |
mock.session_timers = {} |
| 58 |
|
| 59 |
|
| 60 |
@dbus.service.method( |
| 61 |
MAIN_IFACE, |
| 62 |
in_signature="ossua{sv}", |
| 63 |
out_signature="", |
| 64 |
async_callbacks=("cb_success", "cb_error"), |
| 65 |
) |
| 66 |
def Inhibit(self, handle, app_id, window, flags, options, cb_success, cb_error): |
| 67 |
logger.debug(f"Inhibit({handle}, {app_id}, {window}, {flags}, {options})") |
| 68 |
params = self.inhibit_params |
| 69 |
|
| 70 |
request = ImplRequest( |
| 71 |
self, |
| 72 |
BUS_NAME, |
| 73 |
handle, |
| 74 |
logger, |
| 75 |
cb_success, |
| 76 |
cb_error, |
| 77 |
) |
| 78 |
|
| 79 |
if params.expect_close: |
| 80 |
request.wait_for_close() |
| 81 |
else: |
| 82 |
request.respond(Response(params.response, {}), delay=params.delay) |
| 83 |
|
| 84 |
|
| 85 |
@dbus.service.method( |
| 86 |
MOCK_IFACE, |
| 87 |
in_signature="s", |
| 88 |
out_signature="", |
| 89 |
) |
| 90 |
def ArmTimer(self, session_handle): |
| 91 |
self.EmitSignal( |
| 92 |
MAIN_IFACE, |
| 93 |
"StateChanged", |
| 94 |
"oa{sv}", |
| 95 |
[ |
| 96 |
session_handle, |
| 97 |
{ |
| 98 |
"screensaver-active": False, |
| 99 |
"session-state": SessionState.QUERY_END.value, |
| 100 |
}, |
| 101 |
], |
| 102 |
) |
| 103 |
|
| 104 |
def close_session(): |
| 105 |
session = self.sessions[session_handle] |
| 106 |
session.close() |
| 107 |
self.sessions[session_handle] = None |
| 108 |
|
| 109 |
if session_handle in self.session_timers: |
| 110 |
GLib.source_remove(self.session_timers[session_handle]) |
| 111 |
self.session_timers[session_handle] = GLib.timeout_add(700, close_session) |
| 112 |
|
| 113 |
|
| 114 |
@dbus.service.method( |
| 115 |
MAIN_IFACE, |
| 116 |
in_signature="ooss", |
| 117 |
out_signature="u", |
| 118 |
async_callbacks=("cb_success", "cb_error"), |
| 119 |
) |
| 120 |
def CreateMonitor(self, handle, session_handle, app_id, window, cb_success, cb_error): |
| 121 |
logger.debug(f"CreateMonitor({handle}, {session_handle}, {app_id}, {window})") |
| 122 |
params = self.inhibit_params |
| 123 |
|
| 124 |
session = ImplSession(self, BUS_NAME, session_handle, app_id).export() |
| 125 |
self.sessions[session_handle] = session |
| 126 |
|
| 127 |
# This is irregular: the backend doesn't return the results vardict |
| 128 |
def internal_cb_success(response, results): |
| 129 |
cb_success(response) |
| 130 |
|
| 131 |
request = ImplRequest( |
| 132 |
self, |
| 133 |
BUS_NAME, |
| 134 |
handle, |
| 135 |
logger, |
| 136 |
internal_cb_success, |
| 137 |
cb_error, |
| 138 |
) |
| 139 |
|
| 140 |
if params.expect_close: |
| 141 |
request.wait_for_close() |
| 142 |
else: |
| 143 |
|
| 144 |
def arm_timer(): |
| 145 |
self.ArmTimer(session_handle) |
| 146 |
|
| 147 |
request.respond( |
| 148 |
Response(params.response, {}), delay=params.delay, done_cb=arm_timer |
| 149 |
) |
| 150 |
|
| 151 |
|
| 152 |
@dbus.service.method( |
| 153 |
MAIN_IFACE, |
| 154 |
in_signature="o", |
| 155 |
out_signature="", |
| 156 |
) |
| 157 |
def QueryEndResponse(self, session_handle): |
| 158 |
try: |
| 159 |
logger.debug(f"QueryEndResponse({session_handle})") |
| 160 |
|
| 161 |
self.ArmTimer(session_handle) |
| 162 |
|
| 163 |
except Exception as e: |
| 164 |
logger.critical(e) |