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 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, app_id):
42 background_intf = xdp.get_portal_iface(dbus_con, "Background")
43 desktop_file = self.get_autostart_path(app_id)
44
45 reason = "Testing portals"
46
47 request = xdp.Request(dbus_con, background_intf)
48 options = {
49 "reason": reason,
50 }
51 response = request.call(
52 "RequestBackground",
53 parent_window="",
54 options=options,
55 )
56
57 assert response
58 assert response.response == 0
59 assert response.results["background"]
60 assert not response.results["autostart"]
61
62 assert not desktop_file.exists()
63
64 def test_autostart_desktopfile(self, portals, dbus_con, app_id):
65 background_intf = xdp.get_portal_iface(dbus_con, "Background")
66
67 reason = "Testing portals"
68 autostart = True
69 commandline = ["/bin/true", "test"]
70 dbus_activatable = True
71
72 request = xdp.Request(dbus_con, background_intf)
73 options = {
74 "reason": reason,
75 "autostart": autostart,
76 "commandline": commandline,
77 "dbus-activatable": dbus_activatable,
78 }
79 response = request.call(
80 "RequestBackground",
81 parent_window="",
82 options=options,
83 )
84
85 assert response
86 assert response.response == 0
87 assert response.results["background"]
88 assert response.results["autostart"]
89
90 keyfile = self.get_autostart_keyfile(app_id)
91 assert keyfile.get_string("Desktop Entry", "Type") == "Application"
92 assert keyfile.get_string("Desktop Entry", "Name") == app_id
93 assert keyfile.get_string("Desktop Entry", "X-XDP-Autostart") == app_id
94 assert keyfile.get_string("Desktop Entry", "Exec") == "/bin/true test"
95 assert keyfile.get_boolean("Desktop Entry", "DBusActivatable")
96
97 def test_autostart_disable(self, portals, dbus_con, app_id):
98 background_intf = xdp.get_portal_iface(dbus_con, "Background")
99 desktop_file = self.get_autostart_path(app_id)
100
101 reason = "Testing portals"
102 autostart = True
103
104 request = xdp.Request(dbus_con, background_intf)
105 options = {
106 "reason": reason,
107 "autostart": autostart,
108 }
109 response = request.call(
110 "RequestBackground",
111 parent_window="",
112 options=options,
113 )
114
115 assert response
116 assert response.response == 0
117 assert response.results["background"]
118 assert response.results["autostart"]
119
120 assert desktop_file.exists()
121
122 request = xdp.Request(dbus_con, background_intf)
123 options = {
124 "reason": reason,
125 }
126 response = request.call(
127 "RequestBackground",
128 parent_window="",
129 options=options,
130 )
131
132 assert response
133 assert response.response == 0
134 assert response.results["background"]
135 assert not response.results["autostart"]
136
137 assert not desktop_file.exists()
138
139 def test_long_reason(self, portals, dbus_con, app_id):
140 background_intf = xdp.get_portal_iface(dbus_con, "Background")
141
142 reason = (
143 "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
144 + "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
145 + "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
146 )
147 autostart = True
148 commandline = ["/bin/true", "test"]
149 dbus_activatable = True
150
151 request = xdp.Request(dbus_con, background_intf)
152 options = {
153 "reason": reason,
154 "autostart": autostart,
155 "commandline": commandline,
156 "dbus-activatable": dbus_activatable,
157 }
158 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
159 request.call(
160 "RequestBackground",
161 parent_window="",
162 options=options,
163 )
164 assert (
165 excinfo.value.get_dbus_name()
166 == "org.freedesktop.portal.Error.InvalidArgument"
167 )