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