Debian Codegraph @

tests/test_wallpaper.py

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