| 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 |
| 7 |
|
| 8 |
import dbus.service |
| 9 |
from dataclasses import dataclass |
| 10 |
|
| 11 |
|
| 12 |
BUS_NAME = "org.freedesktop.impl.portal.Test" |
| 13 |
MAIN_OBJ = "/org/freedesktop/portal/desktop" |
| 14 |
SYSTEM_BUS = False |
| 15 |
MAIN_IFACE = "org.freedesktop.impl.portal.Print" |
| 16 |
VERSION = 3 |
| 17 |
|
| 18 |
|
| 19 |
logger = init_logger(__name__) |
| 20 |
|
| 21 |
|
| 22 |
@dataclass |
| 23 |
class PrintParameters: |
| 24 |
delay: int |
| 25 |
response: int |
| 26 |
results: dict |
| 27 |
expect_close: bool |
| 28 |
prepare_results: dict |
| 29 |
|
| 30 |
|
| 31 |
def load(mock, parameters={}): |
| 32 |
logger.debug(f"Loading parameters: {parameters}") |
| 33 |
|
| 34 |
assert not hasattr(mock, "print_params") |
| 35 |
mock.print_params = PrintParameters( |
| 36 |
delay=parameters.get("delay", 200), |
| 37 |
response=parameters.get("response", 0), |
| 38 |
results=parameters.get("results", {}), |
| 39 |
expect_close=parameters.get("expect-close", False), |
| 40 |
prepare_results=parameters.get("prepare-results", {}), |
| 41 |
) |
| 42 |
|
| 43 |
mock.AddProperties( |
| 44 |
MAIN_IFACE, |
| 45 |
dbus.Dictionary( |
| 46 |
{ |
| 47 |
"version": dbus.UInt32(parameters.get("version", VERSION)), |
| 48 |
} |
| 49 |
), |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
@dbus.service.method( |
| 54 |
MAIN_IFACE, |
| 55 |
in_signature="osssa{sv}a{sv}a{sv}", |
| 56 |
out_signature="ua{sv}", |
| 57 |
async_callbacks=("cb_success", "cb_error"), |
| 58 |
) |
| 59 |
def PreparePrint( |
| 60 |
self, |
| 61 |
handle, |
| 62 |
app_id, |
| 63 |
parent_window, |
| 64 |
title, |
| 65 |
settings, |
| 66 |
page_setup, |
| 67 |
options, |
| 68 |
cb_success, |
| 69 |
cb_error, |
| 70 |
): |
| 71 |
logger.debug( |
| 72 |
f"PreparePrint({handle}, {app_id}, {parent_window}, {title}, {settings}, {page_setup}, {options})" |
| 73 |
) |
| 74 |
params = self.print_params |
| 75 |
|
| 76 |
request = ImplRequest( |
| 77 |
self, |
| 78 |
BUS_NAME, |
| 79 |
handle, |
| 80 |
logger, |
| 81 |
cb_success, |
| 82 |
cb_error, |
| 83 |
) |
| 84 |
|
| 85 |
if params.expect_close: |
| 86 |
request.wait_for_close() |
| 87 |
else: |
| 88 |
request.respond( |
| 89 |
Response(params.response, params.prepare_results), delay=params.delay |
| 90 |
) |
| 91 |
|
| 92 |
|
| 93 |
@dbus.service.method( |
| 94 |
MAIN_IFACE, |
| 95 |
in_signature="osssha{sv}", |
| 96 |
out_signature="ua{sv}", |
| 97 |
async_callbacks=("cb_success", "cb_error"), |
| 98 |
) |
| 99 |
def Print( |
| 100 |
self, handle, app_id, parent_window, title, fd, options, cb_success, cb_error |
| 101 |
): |
| 102 |
logger.debug( |
| 103 |
f"Print({handle}, {app_id}, {parent_window}, {title}, {fd}, {options})" |
| 104 |
) |
| 105 |
params = self.print_params |
| 106 |
|
| 107 |
request = ImplRequest( |
| 108 |
self, |
| 109 |
BUS_NAME, |
| 110 |
handle, |
| 111 |
logger, |
| 112 |
cb_success, |
| 113 |
cb_error, |
| 114 |
) |
| 115 |
|
| 116 |
if params.expect_close: |
| 117 |
request.wait_for_close() |
| 118 |
else: |
| 119 |
request.respond(Response(params.response, params.results), delay=params.delay) |