| 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 pytest |
| 8 |
import os |
| 9 |
import tempfile |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
|
| 13 |
@pytest.fixture |
| 14 |
def required_templates(): |
| 15 |
return {"wallpaper": {}} |
| 16 |
|
| 17 |
|
| 18 |
class TestWallpaper: |
| 19 |
def set_permission(self, dbus_con, app_id, permission): |
| 20 |
perm_store_intf = xdp.get_permission_store_iface(dbus_con) |
| 21 |
perm_store_intf.SetPermission( |
| 22 |
"wallpaper", |
| 23 |
True, |
| 24 |
"wallpaper", |
| 25 |
app_id, |
| 26 |
[permission], |
| 27 |
) |
| 28 |
|
| 29 |
def test_version(self, portals, dbus_con): |
| 30 |
xdp.check_version(dbus_con, "Wallpaper", 1) |
| 31 |
|
| 32 |
def test_wallpaper_uri(self, portals, dbus_con, app_id): |
| 33 |
wallpaper_intf = xdp.get_portal_iface(dbus_con, "Wallpaper") |
| 34 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 35 |
|
| 36 |
uri = "file:///test" |
| 37 |
show_preview = True |
| 38 |
set_on = "both" |
| 39 |
|
| 40 |
request = xdp.Request(dbus_con, wallpaper_intf) |
| 41 |
options = { |
| 42 |
"show-preview": show_preview, |
| 43 |
"set-on": set_on, |
| 44 |
} |
| 45 |
response = request.call( |
| 46 |
"SetWallpaperURI", |
| 47 |
parent_window="", |
| 48 |
uri=uri, |
| 49 |
options=options, |
| 50 |
) |
| 51 |
|
| 52 |
assert response |
| 53 |
assert response.response == 0 |
| 54 |
|
| 55 |
# Check the impl portal was called with the right args |
| 56 |
method_calls = mock_intf.GetMethodCalls("SetWallpaperURI") |
| 57 |
assert len(method_calls) > 0 |
| 58 |
_, args = method_calls[-1] |
| 59 |
assert args[1] == app_id |
| 60 |
assert args[2] == "" # parent window |
| 61 |
assert args[3] == uri |
| 62 |
assert args[4]["show-preview"] == show_preview |
| 63 |
assert args[4]["set-on"] == set_on |
| 64 |
|
| 65 |
def test_wallpaper_file(self, portals, dbus_con, app_id): |
| 66 |
wallpaper_intf = xdp.get_portal_iface(dbus_con, "Wallpaper") |
| 67 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 68 |
|
| 69 |
fd, _ = tempfile.mkstemp(prefix="wallpaper_mock", dir=Path.home()) |
| 70 |
os.write(fd, b"wallpaper_mock_file") |
| 71 |
|
| 72 |
show_preview = True |
| 73 |
|
| 74 |
request = xdp.Request(dbus_con, wallpaper_intf) |
| 75 |
options = { |
| 76 |
"show-preview": show_preview, |
| 77 |
} |
| 78 |
response = request.call( |
| 79 |
"SetWallpaperFile", |
| 80 |
parent_window="", |
| 81 |
fd=fd, |
| 82 |
options=options, |
| 83 |
) |
| 84 |
|
| 85 |
assert response |
| 86 |
assert response.response == 0 |
| 87 |
|
| 88 |
# Check the impl portal was called with the right args |
| 89 |
method_calls = mock_intf.GetMethodCalls("SetWallpaperURI") |
| 90 |
assert len(method_calls) > 0 |
| 91 |
_, args = method_calls[-1] |
| 92 |
assert args[1] == app_id |
| 93 |
assert args[2] == "" # parent window |
| 94 |
assert args[4]["show-preview"] == show_preview |
| 95 |
|
| 96 |
path = args[3] |
| 97 |
assert path.startswith("file:///") |
| 98 |
|
| 99 |
with open(path[7:]) as file: |
| 100 |
wallpaper_file_contents = file.read() |
| 101 |
assert wallpaper_file_contents == "wallpaper_mock_file" |
| 102 |
|
| 103 |
@pytest.mark.parametrize("template_params", ({"wallpaper": {"response": 1}},)) |
| 104 |
def test_wallpaper_cancel(self, portals, dbus_con, app_id): |
| 105 |
wallpaper_intf = xdp.get_portal_iface(dbus_con, "Wallpaper") |
| 106 |
|
| 107 |
uri = "file:///test" |
| 108 |
show_preview = True |
| 109 |
set_on = "both" |
| 110 |
|
| 111 |
request = xdp.Request(dbus_con, wallpaper_intf) |
| 112 |
options = { |
| 113 |
"show-preview": show_preview, |
| 114 |
"set-on": set_on, |
| 115 |
} |
| 116 |
response = request.call( |
| 117 |
"SetWallpaperURI", |
| 118 |
parent_window="", |
| 119 |
uri=uri, |
| 120 |
options=options, |
| 121 |
) |
| 122 |
|
| 123 |
assert response |
| 124 |
assert response.response == 1 |
| 125 |
|
| 126 |
def test_wallpaper_permission(self, portals, dbus_con, app_id): |
| 127 |
wallpaper_intf = xdp.get_portal_iface(dbus_con, "Wallpaper") |
| 128 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 129 |
|
| 130 |
self.set_permission(dbus_con, app_id, "no") |
| 131 |
|
| 132 |
uri = "file:///test" |
| 133 |
show_preview = True |
| 134 |
set_on = "both" |
| 135 |
|
| 136 |
request = xdp.Request(dbus_con, wallpaper_intf) |
| 137 |
options = { |
| 138 |
"show-preview": show_preview, |
| 139 |
"set-on": set_on, |
| 140 |
} |
| 141 |
response = request.call( |
| 142 |
"SetWallpaperURI", |
| 143 |
parent_window="", |
| 144 |
uri=uri, |
| 145 |
options=options, |
| 146 |
) |
| 147 |
|
| 148 |
assert response |
| 149 |
assert response.response == 2 |
| 150 |
|
| 151 |
# Check the impl portal was called with the right args |
| 152 |
method_calls = mock_intf.GetMethodCalls("SetWallpaperURI") |
| 153 |
assert len(method_calls) == 0 |