| 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 pytest |
| 8 |
import dbus |
| 9 |
import os |
| 10 |
from pathlib import Path |
| 11 |
import stat |
| 12 |
|
| 13 |
|
| 14 |
SVG_IMAGE_DATA = """<?xml version="1.0" encoding="UTF-8"?> |
| 15 |
<svg xmlns="http://www.w3.org/2000/svg" height="16px" width="16px"/> |
| 16 |
""" |
| 17 |
|
| 18 |
DESKTOP_FILE = b"""[Desktop Entry] |
| 19 |
Version=1.0 |
| 20 |
Name=Dynamic Launcher Example |
| 21 |
Exec=true %u |
| 22 |
Type=Application |
| 23 |
""" |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture |
| 27 |
def required_templates(): |
| 28 |
# ensure that we have a flatpak executable |
| 29 |
# The dynamiclauncher checks that the thing in the exec line exists and is |
| 30 |
# executable, so for flatpak, it needs the flatpak executable |
| 31 |
flatpak_exec = Path(os.environ["HOME"]) / "exec" / "flatpak" |
| 32 |
flatpak_exec.parent.mkdir(parents=True) |
| 33 |
flatpak_exec.touch(mode=0o777 | stat.S_IEXEC) |
| 34 |
os.environ["PATH"] += os.pathsep + flatpak_exec.parent.absolute().as_posix() |
| 35 |
|
| 36 |
return {"dynamiclauncher": {}} |
| 37 |
|
| 38 |
|
| 39 |
class TestDynamicLauncher: |
| 40 |
def test_version(self, portals, dbus_con): |
| 41 |
"""tests the version of the interface""" |
| 42 |
|
| 43 |
xdp.check_version(dbus_con, "DynamicLauncher", 1) |
| 44 |
|
| 45 |
def test_basic(self, portals, dbus_con, xdp_app_info): |
| 46 |
app_id = xdp_app_info.app_id |
| 47 |
"""test that the backend receives the expected data""" |
| 48 |
|
| 49 |
dynlauncher_intf = xdp.get_portal_iface(dbus_con, "DynamicLauncher") |
| 50 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 51 |
|
| 52 |
app_name = "App Name" |
| 53 |
bytes = SVG_IMAGE_DATA.encode("utf-8") |
| 54 |
|
| 55 |
request = xdp.Request(dbus_con, dynlauncher_intf) |
| 56 |
options = { |
| 57 |
"modal": False, |
| 58 |
} |
| 59 |
response = request.call( |
| 60 |
"PrepareInstall", |
| 61 |
parent_window="", |
| 62 |
name=app_name, |
| 63 |
icon_v=dbus.Struct( |
| 64 |
("bytes", dbus.ByteArray(bytes, variant_level=1)), |
| 65 |
signature="sv", |
| 66 |
variant_level=1, |
| 67 |
), |
| 68 |
options=options, |
| 69 |
) |
| 70 |
|
| 71 |
assert response |
| 72 |
assert response.response == 0 |
| 73 |
assert response.results["name"] == app_name |
| 74 |
token = response.results["token"] |
| 75 |
|
| 76 |
# Check the impl portal was called with the right args |
| 77 |
method_calls = mock_intf.GetMethodCalls("PrepareInstall") |
| 78 |
assert len(method_calls) > 0 |
| 79 |
_, args = method_calls[-1] |
| 80 |
assert args[2] == "" # parent window |
| 81 |
assert args[3] == app_name # name |
| 82 |
# args[4] == icon |
| 83 |
assert not args[5]["modal"] |
| 84 |
|
| 85 |
desktop_file_name = app_id + ".ExampleApp.desktop" |
| 86 |
|
| 87 |
try: |
| 88 |
dynlauncher_intf.Install( |
| 89 |
token, |
| 90 |
desktop_file_name, |
| 91 |
DESKTOP_FILE, |
| 92 |
{}, |
| 93 |
) |
| 94 |
except dbus.exceptions.DBusException as e: |
| 95 |
# Unsupported on snap and linyaps |
| 96 |
assert e.get_dbus_name() == "org.freedesktop.portal.Error.InvalidArgument" |
| 97 |
assert isinstance(xdp_app_info, (xdp.AppInfoSnap, xdp.AppInfoLinyaps)) |
| 98 |
return |
| 99 |
assert not isinstance(xdp_app_info, (xdp.AppInfoSnap, xdp.AppInfoLinyaps)) |
| 100 |
|
| 101 |
file = Path(os.environ["XDG_DATA_HOME"]) / "applications" / desktop_file_name |
| 102 |
assert file.exists() |