| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# |
| 3 |
# This file is formatted with Python Black |
| 4 |
|
| 5 |
import tests.xdp_utils as xdp |
| 6 |
|
| 7 |
import dbus |
| 8 |
import pytest |
| 9 |
import os |
| 10 |
import tempfile |
| 11 |
from pathlib import Path |
| 12 |
from typing import Any |
| 13 |
|
| 14 |
|
| 15 |
PRINT_PREPARE_DATA = { |
| 16 |
"token": dbus.UInt32(1337), |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
@pytest.fixture |
| 21 |
def required_templates(): |
| 22 |
return { |
| 23 |
"print": { |
| 24 |
"prepare-results": PRINT_PREPARE_DATA, |
| 25 |
}, |
| 26 |
"lockdown": {}, |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
class TestPrint: |
| 31 |
def test_version(self, portals, dbus_con): |
| 32 |
xdp.check_version(dbus_con, "Print", 4) |
| 33 |
|
| 34 |
def test_prepare_print_basic(self, portals, dbus_con, xdp_app_info): |
| 35 |
app_id = xdp_app_info.app_id |
| 36 |
print_intf = xdp.get_portal_iface(dbus_con, "Print") |
| 37 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 38 |
|
| 39 |
title = "Test Title" |
| 40 |
settings: Any = {} |
| 41 |
page_setup: Any = {} |
| 42 |
options = { |
| 43 |
"modal": True, |
| 44 |
"accept_label": "Accept", |
| 45 |
"supported_output_file_formats": ["pdf"], |
| 46 |
} |
| 47 |
|
| 48 |
request = xdp.Request(dbus_con, print_intf) |
| 49 |
response = request.call( |
| 50 |
"PreparePrint", |
| 51 |
parent_window="", |
| 52 |
title=title, |
| 53 |
settings=settings, |
| 54 |
page_setup=page_setup, |
| 55 |
options=options, |
| 56 |
) |
| 57 |
|
| 58 |
assert response |
| 59 |
assert response.response == 0 |
| 60 |
|
| 61 |
# Check the impl portal was called with the right args |
| 62 |
method_calls = mock_intf.GetMethodCalls("PreparePrint") |
| 63 |
assert len(method_calls) == 1 |
| 64 |
_, args = method_calls.pop() |
| 65 |
assert args[1] == app_id |
| 66 |
assert args[2] == "" # parent window |
| 67 |
assert args[3] == title |
| 68 |
assert args[4] == settings |
| 69 |
assert args[5] == page_setup |
| 70 |
assert args[6]["modal"] == options["modal"] |
| 71 |
assert args[6]["accept_label"] == options["accept_label"] |
| 72 |
assert ( |
| 73 |
args[6]["supported_output_file_formats"] |
| 74 |
== options["supported_output_file_formats"] |
| 75 |
) |
| 76 |
|
| 77 |
@pytest.mark.parametrize("template_params", ({"print": {"response": 1}},)) |
| 78 |
def test_prepare_print_cancel(self, portals, dbus_con, xdp_app_info): |
| 79 |
app_id = xdp_app_info.app_id |
| 80 |
print_intf = xdp.get_portal_iface(dbus_con, "Print") |
| 81 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 82 |
|
| 83 |
title = "Test Title" |
| 84 |
|
| 85 |
request = xdp.Request(dbus_con, print_intf) |
| 86 |
response = request.call( |
| 87 |
"PreparePrint", |
| 88 |
parent_window="", |
| 89 |
title=title, |
| 90 |
settings={}, |
| 91 |
page_setup={}, |
| 92 |
options={}, |
| 93 |
) |
| 94 |
|
| 95 |
assert response |
| 96 |
assert response.response == 1 |
| 97 |
|
| 98 |
# Check the impl portal was called with the right args |
| 99 |
method_calls = mock_intf.GetMethodCalls("PreparePrint") |
| 100 |
assert len(method_calls) == 1 |
| 101 |
_, args = method_calls.pop() |
| 102 |
assert args[1] == app_id |
| 103 |
assert args[2] == "" # parent window |
| 104 |
assert args[3] == title |
| 105 |
|
| 106 |
@pytest.mark.parametrize("template_params", ({"print": {"expect-close": True}},)) |
| 107 |
def test_prepare_print_close(self, portals, dbus_con, xdp_app_info): |
| 108 |
app_id = xdp_app_info.app_id |
| 109 |
print_intf = xdp.get_portal_iface(dbus_con, "Print") |
| 110 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 111 |
|
| 112 |
title = "Test Title" |
| 113 |
|
| 114 |
request = xdp.Request(dbus_con, print_intf) |
| 115 |
request.schedule_close(1000) |
| 116 |
request.call( |
| 117 |
"PreparePrint", |
| 118 |
parent_window="", |
| 119 |
title=title, |
| 120 |
settings={}, |
| 121 |
page_setup={}, |
| 122 |
options={}, |
| 123 |
) |
| 124 |
|
| 125 |
# Only true if the impl.Request was closed too |
| 126 |
assert request.closed |
| 127 |
|
| 128 |
# Check the impl portal was called with the right args |
| 129 |
method_calls = mock_intf.GetMethodCalls("PreparePrint") |
| 130 |
assert len(method_calls) == 1 |
| 131 |
_, args = method_calls.pop() |
| 132 |
assert args[1] == app_id |
| 133 |
assert args[2] == "" # parent window |
| 134 |
assert args[3] == title |
| 135 |
|
| 136 |
@pytest.mark.parametrize( |
| 137 |
"template_params", ({"lockdown": {"disable-printing": True}},) |
| 138 |
) |
| 139 |
def test_prepare_print_lockdown(self, portals, dbus_con): |
| 140 |
print_intf = xdp.get_portal_iface(dbus_con, "Print") |
| 141 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 142 |
|
| 143 |
title = "Test Title" |
| 144 |
|
| 145 |
request = xdp.Request(dbus_con, print_intf) |
| 146 |
with pytest.raises(dbus.exceptions.DBusException) as excinfo: |
| 147 |
request.call( |
| 148 |
"PreparePrint", |
| 149 |
parent_window="", |
| 150 |
title=title, |
| 151 |
settings={}, |
| 152 |
page_setup={}, |
| 153 |
options={}, |
| 154 |
) |
| 155 |
assert ( |
| 156 |
excinfo.value.get_dbus_name() == "org.freedesktop.portal.Error.NotAllowed" |
| 157 |
) |
| 158 |
|
| 159 |
# Check the impl portal was not called |
| 160 |
method_calls = mock_intf.GetMethodCalls("PreparePrint") |
| 161 |
assert len(method_calls) == 0 |
| 162 |
|
| 163 |
def test_print_basic(self, portals, dbus_con, xdp_app_info): |
| 164 |
app_id = xdp_app_info.app_id |
| 165 |
print_intf = xdp.get_portal_iface(dbus_con, "Print") |
| 166 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 167 |
|
| 168 |
fd, file_path = tempfile.mkstemp(prefix="print_mock_file_", dir=Path.home()) |
| 169 |
os.write(fd, b"print_mock_file") |
| 170 |
|
| 171 |
title = "Test Title" |
| 172 |
options = { |
| 173 |
"modal": True, |
| 174 |
"token": "token", |
| 175 |
"supported_output_file_formats": ["svg"], |
| 176 |
} |
| 177 |
|
| 178 |
request = xdp.Request(dbus_con, print_intf) |
| 179 |
response = request.call( |
| 180 |
"Print", |
| 181 |
parent_window="", |
| 182 |
title=title, |
| 183 |
fd=fd, |
| 184 |
options=options, |
| 185 |
) |
| 186 |
|
| 187 |
assert response |
| 188 |
assert response.response == 0 |
| 189 |
|
| 190 |
# Check the impl portal was called with the right args |
| 191 |
method_calls = mock_intf.GetMethodCalls("Print") |
| 192 |
assert len(method_calls) == 1 |
| 193 |
_, args = method_calls.pop() |
| 194 |
assert args[1] == app_id |
| 195 |
assert args[2] == "" # parent window |
| 196 |
assert args[3] == title |
| 197 |
assert args[5]["modal"] == options["modal"] |
| 198 |
assert ( |
| 199 |
args[5]["supported_output_file_formats"] |
| 200 |
== options["supported_output_file_formats"] |
| 201 |
) |
| 202 |
|
| 203 |
backend_fd = args[4].take() |
| 204 |
ino = os.stat(f"/proc/self/fd/{fd}").st_ino |
| 205 |
ino_backend = os.stat(f"/proc/self/fd/{backend_fd}").st_ino |
| 206 |
os.close(fd) |
| 207 |
assert ino == ino_backend |
| 208 |
|
| 209 |
@pytest.mark.parametrize("template_params", ({"print": {"response": 1}},)) |
| 210 |
def test_print_cancel(self, portals, dbus_con, xdp_app_info): |
| 211 |
app_id = xdp_app_info.app_id |
| 212 |
print_intf = xdp.get_portal_iface(dbus_con, "Print") |
| 213 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 214 |
|
| 215 |
fd, _ = tempfile.mkstemp(prefix="print_mock_file_", dir=Path.home()) |
| 216 |
os.write(fd, b"print_mock_file") |
| 217 |
|
| 218 |
title = "Test Title" |
| 219 |
|
| 220 |
request = xdp.Request(dbus_con, print_intf) |
| 221 |
response = request.call( |
| 222 |
"Print", |
| 223 |
parent_window="", |
| 224 |
title=title, |
| 225 |
fd=fd, |
| 226 |
options={}, |
| 227 |
) |
| 228 |
|
| 229 |
assert response |
| 230 |
assert response.response == 1 |
| 231 |
|
| 232 |
# Check the impl portal was called with the right args |
| 233 |
method_calls = mock_intf.GetMethodCalls("Print") |
| 234 |
assert len(method_calls) == 1 |
| 235 |
_, args = method_calls.pop() |
| 236 |
assert args[1] == app_id |
| 237 |
|
| 238 |
@pytest.mark.parametrize("template_params", ({"print": {"expect-close": True}},)) |
| 239 |
def test_print_close(self, portals, dbus_con, xdp_app_info): |
| 240 |
app_id = xdp_app_info.app_id |
| 241 |
print_intf = xdp.get_portal_iface(dbus_con, "Print") |
| 242 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 243 |
|
| 244 |
fd, _ = tempfile.mkstemp(prefix="print_mock_file_", dir=Path.home()) |
| 245 |
os.write(fd, b"print_mock_file") |
| 246 |
|
| 247 |
title = "Test Title" |
| 248 |
|
| 249 |
request = xdp.Request(dbus_con, print_intf) |
| 250 |
request.schedule_close(1000) |
| 251 |
request.call( |
| 252 |
"Print", |
| 253 |
parent_window="", |
| 254 |
title=title, |
| 255 |
fd=fd, |
| 256 |
options={}, |
| 257 |
) |
| 258 |
|
| 259 |
# Only true if the impl.Request was closed too |
| 260 |
assert request.closed |
| 261 |
|
| 262 |
# Check the impl portal was called with the right args |
| 263 |
method_calls = mock_intf.GetMethodCalls("Print") |
| 264 |
assert len(method_calls) == 1 |
| 265 |
_, args = method_calls.pop() |
| 266 |
assert args[1] == app_id |
| 267 |
|
| 268 |
@pytest.mark.parametrize( |
| 269 |
"template_params", ({"lockdown": {"disable-printing": True}},) |
| 270 |
) |
| 271 |
def test_print_lockdown(self, portals, dbus_con): |
| 272 |
print_intf = xdp.get_portal_iface(dbus_con, "Print") |
| 273 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 274 |
|
| 275 |
fd, _ = tempfile.mkstemp(prefix="print_mock_file_", dir=Path.home()) |
| 276 |
os.write(fd, b"print_mock_file") |
| 277 |
|
| 278 |
title = "Test Title" |
| 279 |
|
| 280 |
request = xdp.Request(dbus_con, print_intf) |
| 281 |
with pytest.raises(dbus.exceptions.DBusException) as excinfo: |
| 282 |
request.call( |
| 283 |
"Print", |
| 284 |
parent_window="", |
| 285 |
title=title, |
| 286 |
fd=fd, |
| 287 |
options={}, |
| 288 |
) |
| 289 |
assert ( |
| 290 |
excinfo.value.get_dbus_name() == "org.freedesktop.portal.Error.NotAllowed" |
| 291 |
) |
| 292 |
|
| 293 |
# Check the impl portal was not called |
| 294 |
method_calls = mock_intf.GetMethodCalls("Print") |
| 295 |
assert len(method_calls) == 0 |
| 296 |
|
| 297 |
def test_print_prepare_and_print(self, portals, dbus_con): |
| 298 |
print_intf = xdp.get_portal_iface(dbus_con, "Print") |
| 299 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 300 |
|
| 301 |
title = "Test Title" |
| 302 |
|
| 303 |
fd, file_path = tempfile.mkstemp(prefix="print_mock_file_", dir=Path.home()) |
| 304 |
os.write(fd, b"print_mock_file") |
| 305 |
|
| 306 |
request = xdp.Request(dbus_con, print_intf) |
| 307 |
response = request.call( |
| 308 |
"PreparePrint", |
| 309 |
parent_window="", |
| 310 |
title=title, |
| 311 |
settings={}, |
| 312 |
page_setup={}, |
| 313 |
options={}, |
| 314 |
) |
| 315 |
|
| 316 |
assert response |
| 317 |
assert response.response == 0 |
| 318 |
assert response.results["token"] == PRINT_PREPARE_DATA["token"] |
| 319 |
|
| 320 |
# Check the impl portal was called with the right args |
| 321 |
method_calls = mock_intf.GetMethodCalls("PreparePrint") |
| 322 |
assert len(method_calls) == 1 |
| 323 |
|
| 324 |
options = { |
| 325 |
"token": response.results["token"], |
| 326 |
} |
| 327 |
|
| 328 |
request = xdp.Request(dbus_con, print_intf) |
| 329 |
response = request.call( |
| 330 |
"Print", |
| 331 |
parent_window="", |
| 332 |
title=title, |
| 333 |
fd=fd, |
| 334 |
options=options, |
| 335 |
) |
| 336 |
|
| 337 |
assert response |
| 338 |
assert response.response == 0 |
| 339 |
|
| 340 |
# Check the impl portal was called with the right args |
| 341 |
method_calls = mock_intf.GetMethodCalls("Print") |
| 342 |
assert len(method_calls) == 1 |
| 343 |
_, args = method_calls.pop() |
| 344 |
|
| 345 |
assert args[5]["token"] == PRINT_PREPARE_DATA["token"] |
| 346 |
|
| 347 |
backend_fd = args[4].take() |
| 348 |
ino = os.stat(f"/proc/self/fd/{fd}").st_ino |
| 349 |
ino_backend = os.stat(f"/proc/self/fd/{backend_fd}").st_ino |
| 350 |
os.close(fd) |
| 351 |
assert ino == ino_backend |