Debian Codegraph @

tests/test_background.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 dbus
8 import pytest
9 import os
10 from pathlib import Path
11 from gi.repository import GLib
12
13
14 @pytest.fixture
15 def required_templates():
16 return {"background": {}}
17
18
19 class TestBackground:
20 def get_autostart_path(self, app_id):
21 return Path(os.environ["XDG_CONFIG_HOME"]) / "autostart" / f"{app_id}.desktop"
22
23 def get_autostart_keyfile(self, app_id):
24 keyfile = GLib.KeyFile.new()
25
26 desktop_file_path = self.get_autostart_path(app_id)
27 with open(str(desktop_file_path.absolute())) as desktop_file:
28 desktop_file_contents = desktop_file.read()
29
30 assert keyfile.load_from_data(
31 desktop_file_contents,
32 len(desktop_file_contents),
33 GLib.KeyFileFlags.NONE,
34 )
35
36 return keyfile
37
38 def test_version(self, portals, dbus_con):
39 xdp.check_version(dbus_con, "Background", 2)
40
41 def test_request_background(self, portals, dbus_con, xdp_app_info):
42 app_id = xdp_app_info.app_id
43 background_intf = xdp.get_portal_iface(dbus_con, "Background")
44 desktop_file = self.get_autostart_path(app_id)
45
46 reason = "Testing portals"
47
48 request = xdp.Request(dbus_con, background_intf)
49 options = {
50 "reason": reason,
51 }
52 response = request.call(
53 "RequestBackground",
54 parent_window="",
55 options=options,
56 )
57
58 assert response
59 assert response.response == 0
60 assert response.results["background"]
61 assert not response.results["autostart"]
62
63 assert not desktop_file.exists()
64
65 def test_autostart_desktopfile(self, portals, dbus_con, xdp_app_info):
66 app_id = xdp_app_info.app_id
67 background_intf = xdp.get_portal_iface(dbus_con, "Background")
68
69 reason = "Testing portals"
70 autostart = True
71 commandline = ["/bin/true", "test"]
72 dbus_activatable = True
73
74 request = xdp.Request(dbus_con, background_intf)
75 options = {
76 "reason": reason,
77 "autostart": autostart,
78 "commandline": commandline,
79 "dbus-activatable": dbus_activatable,
80 }
81 response = request.call(
82 "RequestBackground",
83 parent_window="",
84 options=options,
85 )
86
87 assert response
88
89 # Unsupported on snap and linyaps
90 if isinstance(xdp_app_info, (xdp.AppInfoSnap, xdp.AppInfoLinyaps)):
91 assert response.response == 2
92 assert not response.results["autostart"]
93 return
94
95 assert response.response == 0
96 assert response.results["background"]
97
98 gapp_info = xdp_app_info.gapp_info()
99 autostart_name = gapp_info.get_name() if gapp_info else app_id
100
101 assert response.results["autostart"]
102 keyfile = self.get_autostart_keyfile(app_id)
103 assert keyfile.get_string("Desktop Entry", "Type") == "Application"
104 assert keyfile.get_string("Desktop Entry", "Name") == autostart_name
105 assert keyfile.get_string("Desktop Entry", "X-XDP-Autostart") == app_id
106 assert keyfile.get_boolean("Desktop Entry", "DBusActivatable")
107
108 exec = keyfile.get_string("Desktop Entry", "Exec")
109 if isinstance(xdp_app_info, xdp.AppInfoFlatpak):
110 assert exec == f"flatpak run --command=/bin/true {app_id} test"
111 else:
112 assert exec == "/bin/true test"
113
114 def test_autostart_disable(self, portals, dbus_con, xdp_app_info):
115 app_id = xdp_app_info.app_id
116 background_intf = xdp.get_portal_iface(dbus_con, "Background")
117 desktop_file = self.get_autostart_path(app_id)
118
119 reason = "Testing portals"
120 autostart = True
121
122 request = xdp.Request(dbus_con, background_intf)
123 options = {
124 "reason": reason,
125 "autostart": autostart,
126 }
127 response = request.call(
128 "RequestBackground",
129 parent_window="",
130 options=options,
131 )
132
133 assert response
134
135 # Unsupported on snap and linyaps
136 if isinstance(xdp_app_info, (xdp.AppInfoSnap, xdp.AppInfoLinyaps)):
137 assert response.response == 2
138 assert not response.results["autostart"]
139 return
140
141 assert response.response == 0
142 assert response.results["background"]
143 assert response.results["autostart"]
144
145 assert desktop_file.exists()
146
147 request = xdp.Request(dbus_con, background_intf)
148 options = {
149 "reason": reason,
150 }
151 response = request.call(
152 "RequestBackground",
153 parent_window="",
154 options=options,
155 )
156
157 assert response
158 assert response.response == 0
159 assert response.results["background"]
160 assert not response.results["autostart"]
161
162 assert not desktop_file.exists()
163
164 def test_long_reason(self, portals, dbus_con):
165 background_intf = xdp.get_portal_iface(dbus_con, "Background")
166
167 reason = (
168 "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
169 + "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
170 + "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
171 )
172 autostart = True
173 commandline = ["/bin/true", "test"]
174 dbus_activatable = True
175
176 request = xdp.Request(dbus_con, background_intf)
177 options = {
178 "reason": reason,
179 "autostart": autostart,
180 "commandline": commandline,
181 "dbus-activatable": dbus_activatable,
182 }
183 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
184 request.call(
185 "RequestBackground",
186 parent_window="",
187 options=options,
188 )
189 assert (
190 excinfo.value.get_dbus_name()
191 == "org.freedesktop.portal.Error.InvalidArgument"
192 )