| 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 |
from enum import Enum, Flag |
| 9 |
|
| 10 |
|
| 11 |
class InhibitFlags(Flag): |
| 12 |
LOGOUT = 1 |
| 13 |
USER_SWITCH = 2 |
| 14 |
SUSPEND = 4 |
| 15 |
IDLE = 8 |
| 16 |
ALL = 16 - 1 |
| 17 |
|
| 18 |
|
| 19 |
class SessionState(Enum): |
| 20 |
RUNNING = 1 |
| 21 |
QUERY_END = 2 |
| 22 |
ENDING = 3 |
| 23 |
|
| 24 |
|
| 25 |
@pytest.fixture |
| 26 |
def required_templates(): |
| 27 |
return {"inhibit": {}} |
| 28 |
|
| 29 |
|
| 30 |
class TestInhibit: |
| 31 |
def set_permissions(self, dbus_con, app_id, permissions): |
| 32 |
perm_store_intf = xdp.get_permission_store_iface(dbus_con) |
| 33 |
perm_store_intf.SetPermission( |
| 34 |
"inhibit", |
| 35 |
True, |
| 36 |
"inhibit", |
| 37 |
app_id, |
| 38 |
permissions, |
| 39 |
) |
| 40 |
|
| 41 |
def test_version(self, portals, dbus_con): |
| 42 |
xdp.check_version(dbus_con, "Inhibit", 3) |
| 43 |
|
| 44 |
def test_basic(self, portals, dbus_con, app_id): |
| 45 |
inhibit_intf = xdp.get_portal_iface(dbus_con, "Inhibit") |
| 46 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 47 |
|
| 48 |
reason = "reason" |
| 49 |
flags = InhibitFlags.ALL |
| 50 |
|
| 51 |
request = xdp.Request(dbus_con, inhibit_intf) |
| 52 |
options = { |
| 53 |
"reason": reason, |
| 54 |
} |
| 55 |
response = request.call( |
| 56 |
"Inhibit", |
| 57 |
window="", |
| 58 |
flags=flags.value, |
| 59 |
options=options, |
| 60 |
) |
| 61 |
|
| 62 |
assert response |
| 63 |
assert response.response == 0 |
| 64 |
|
| 65 |
# Check the impl portal was called with the right args |
| 66 |
method_calls = mock_intf.GetMethodCalls("Inhibit") |
| 67 |
assert len(method_calls) > 0 |
| 68 |
_, args = method_calls[-1] |
| 69 |
assert args[1] == app_id |
| 70 |
assert args[2] == "" # parent window |
| 71 |
assert args[3] == flags.value |
| 72 |
assert args[4]["reason"] == reason |
| 73 |
|
| 74 |
@pytest.mark.parametrize("template_params", ({"inhibit": {"response": 1}},)) |
| 75 |
def test_cancel(self, portals, dbus_con, app_id): |
| 76 |
inhibit_intf = xdp.get_portal_iface(dbus_con, "Inhibit") |
| 77 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 78 |
|
| 79 |
reason = "reason" |
| 80 |
flags = InhibitFlags.ALL |
| 81 |
|
| 82 |
request = xdp.Request(dbus_con, inhibit_intf) |
| 83 |
options = { |
| 84 |
"reason": reason, |
| 85 |
} |
| 86 |
response = request.call( |
| 87 |
"Inhibit", |
| 88 |
window="", |
| 89 |
flags=flags.value, |
| 90 |
options=options, |
| 91 |
) |
| 92 |
|
| 93 |
# for some reason, the backend failing is still considered a success |
| 94 |
assert response |
| 95 |
assert response.response == 0 |
| 96 |
|
| 97 |
# Check the impl portal was called with the right args |
| 98 |
method_calls = mock_intf.GetMethodCalls("Inhibit") |
| 99 |
assert len(method_calls) > 0 |
| 100 |
_, args = method_calls[-1] |
| 101 |
assert args[1] == app_id |
| 102 |
assert args[2] == "" # parent window |
| 103 |
assert args[3] == flags.value |
| 104 |
assert args[4]["reason"] == reason |
| 105 |
|
| 106 |
@pytest.mark.parametrize("template_params", ({"inhibit": {"expect-close": True}},)) |
| 107 |
def test_close(self, portals, dbus_con, app_id): |
| 108 |
inhibit_intf = xdp.get_portal_iface(dbus_con, "Inhibit") |
| 109 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 110 |
|
| 111 |
reason = "reason" |
| 112 |
flags = InhibitFlags.ALL |
| 113 |
|
| 114 |
request = xdp.Request(dbus_con, inhibit_intf) |
| 115 |
request.schedule_close(1000) |
| 116 |
options = { |
| 117 |
"reason": reason, |
| 118 |
} |
| 119 |
request.call( |
| 120 |
"Inhibit", |
| 121 |
window="", |
| 122 |
flags=flags.value, |
| 123 |
options=options, |
| 124 |
) |
| 125 |
|
| 126 |
# Only true if the impl.Request was closed too |
| 127 |
assert request.closed |
| 128 |
|
| 129 |
# Check the impl portal was called with the right args |
| 130 |
method_calls = mock_intf.GetMethodCalls("Inhibit") |
| 131 |
assert len(method_calls) > 0 |
| 132 |
_, args = method_calls[-1] |
| 133 |
assert args[1] == app_id |
| 134 |
assert args[2] == "" # parent window |
| 135 |
assert args[3] == flags.value |
| 136 |
assert args[4]["reason"] == reason |
| 137 |
|
| 138 |
def test_permission(self, portals, dbus_con, app_id): |
| 139 |
inhibit_intf = xdp.get_portal_iface(dbus_con, "Inhibit") |
| 140 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 141 |
|
| 142 |
self.set_permissions(dbus_con, app_id, ["logout", "suspend"]) |
| 143 |
|
| 144 |
reason = "reason" |
| 145 |
flags = InhibitFlags.LOGOUT | InhibitFlags.SUSPEND | InhibitFlags.IDLE |
| 146 |
allowed_flags = InhibitFlags.LOGOUT | InhibitFlags.SUSPEND |
| 147 |
|
| 148 |
request = xdp.Request(dbus_con, inhibit_intf) |
| 149 |
options = { |
| 150 |
"reason": reason, |
| 151 |
} |
| 152 |
response = request.call( |
| 153 |
"Inhibit", |
| 154 |
window="", |
| 155 |
flags=flags.value, |
| 156 |
options=options, |
| 157 |
) |
| 158 |
|
| 159 |
assert response |
| 160 |
assert response.response == 0 |
| 161 |
|
| 162 |
method_calls = mock_intf.GetMethodCalls("Inhibit") |
| 163 |
_, args = method_calls[-1] |
| 164 |
assert args[3] == allowed_flags.value |
| 165 |
|
| 166 |
self.set_permissions(dbus_con, app_id, ["suspend"]) |
| 167 |
|
| 168 |
flags = InhibitFlags.LOGOUT | InhibitFlags.SUSPEND | InhibitFlags.IDLE |
| 169 |
allowed_flags = InhibitFlags.SUSPEND |
| 170 |
|
| 171 |
request = xdp.Request(dbus_con, inhibit_intf) |
| 172 |
options = { |
| 173 |
"reason": reason, |
| 174 |
} |
| 175 |
response = request.call( |
| 176 |
"Inhibit", |
| 177 |
window="", |
| 178 |
flags=flags.value, |
| 179 |
options=options, |
| 180 |
) |
| 181 |
|
| 182 |
assert response |
| 183 |
assert response.response == 0 |
| 184 |
|
| 185 |
method_calls = mock_intf.GetMethodCalls("Inhibit") |
| 186 |
_, args = method_calls[-1] |
| 187 |
assert args[3] == allowed_flags.value |
| 188 |
|
| 189 |
def test_monitor(self, portals, dbus_con, app_id): |
| 190 |
inhibit_intf = xdp.get_portal_iface(dbus_con, "Inhibit") |
| 191 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 192 |
|
| 193 |
changed_count = 0 |
| 194 |
|
| 195 |
request = xdp.Request(dbus_con, inhibit_intf) |
| 196 |
options = { |
| 197 |
"session_handle_token": "session_token0", |
| 198 |
} |
| 199 |
response = request.call( |
| 200 |
"CreateMonitor", |
| 201 |
window="", |
| 202 |
options=options, |
| 203 |
) |
| 204 |
|
| 205 |
assert response |
| 206 |
assert response.response == 0 |
| 207 |
|
| 208 |
session = xdp.Session.from_response(dbus_con, response) |
| 209 |
|
| 210 |
# Check the impl portal was called with the right args |
| 211 |
method_calls = mock_intf.GetMethodCalls("CreateMonitor") |
| 212 |
assert len(method_calls) > 0 |
| 213 |
_, args = method_calls[-1] |
| 214 |
assert args[1] == session.handle |
| 215 |
assert args[2] == app_id |
| 216 |
assert args[3] == "" # parent window |
| 217 |
|
| 218 |
def state_changed_cb(session_handle, state): |
| 219 |
nonlocal changed_count |
| 220 |
|
| 221 |
assert not state["screensaver-active"] |
| 222 |
assert state["session-state"] == SessionState.QUERY_END.value |
| 223 |
|
| 224 |
changed_count += 1 |
| 225 |
|
| 226 |
inhibit_intf.connect_to_signal("StateChanged", state_changed_cb) |
| 227 |
|
| 228 |
# wait for a Query End state change |
| 229 |
xdp.wait_for(lambda: changed_count == 1) |
| 230 |
assert not session.closed |
| 231 |
# and respond with QueryEndResponse |
| 232 |
inhibit_intf.QueryEndResponse(session.handle) |
| 233 |
|
| 234 |
# wait for another Query End state change |
| 235 |
xdp.wait_for(lambda: changed_count == 2) |
| 236 |
assert not session.closed |
| 237 |
|
| 238 |
# do not respond with QueryEndResponse and instead wait for >1s |
| 239 |
xdp.wait(1500) |
| 240 |
|
| 241 |
# the session should have gotten closed by now |
| 242 |
assert session.closed |