| 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 |
from typing import Any |
| 10 |
|
| 11 |
|
| 12 |
SCREENSHOT_DATA = dbus.Dictionary( |
| 13 |
{ |
| 14 |
"uri": "file:///screenshot.png", |
| 15 |
"color": (0.0, 1.0, 0.331), |
| 16 |
}, |
| 17 |
signature="sv", |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
@pytest.fixture |
| 22 |
def required_templates(): |
| 23 |
return { |
| 24 |
"access": {}, |
| 25 |
"screenshot": { |
| 26 |
"results": SCREENSHOT_DATA, |
| 27 |
}, |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
class TestScreenshot: |
| 32 |
def test_version(self, portals, dbus_con): |
| 33 |
xdp.check_version(dbus_con, "Screenshot", 2) |
| 34 |
|
| 35 |
@pytest.mark.parametrize("modal", [True, False]) |
| 36 |
@pytest.mark.parametrize("interactive", [True, False]) |
| 37 |
def test_screenshot_basic(self, portals, dbus_con, app_id, modal, interactive): |
| 38 |
screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot") |
| 39 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 40 |
|
| 41 |
request = xdp.Request(dbus_con, screenshot_intf) |
| 42 |
options = { |
| 43 |
"modal": modal, |
| 44 |
"interactive": interactive, |
| 45 |
} |
| 46 |
response = request.call( |
| 47 |
"Screenshot", |
| 48 |
parent_window="", |
| 49 |
options=options, |
| 50 |
) |
| 51 |
|
| 52 |
assert response |
| 53 |
assert response.response == 0 |
| 54 |
assert response.results["uri"] == SCREENSHOT_DATA["uri"] |
| 55 |
|
| 56 |
# Check the impl portal was called with the right args |
| 57 |
method_calls = mock_intf.GetMethodCalls("Screenshot") |
| 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]["modal"] == modal |
| 63 |
assert args[3]["interactive"] == interactive |
| 64 |
|
| 65 |
# check that args were forwarded to access portal correctly |
| 66 |
if not interactive: |
| 67 |
method_calls = mock_intf.GetMethodCalls("AccessDialog") |
| 68 |
assert len(method_calls) > 0 |
| 69 |
_, args = method_calls[-1] |
| 70 |
assert args[1] == app_id |
| 71 |
assert args[2] == "" # parent window |
| 72 |
assert args[6]["modal"] == modal |
| 73 |
|
| 74 |
@pytest.mark.parametrize( |
| 75 |
"template_params", ({"screenshot": {"expect-close": True}},) |
| 76 |
) |
| 77 |
def test_screenshot_close(self, portals, dbus_con): |
| 78 |
screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot") |
| 79 |
|
| 80 |
request = xdp.Request(dbus_con, screenshot_intf) |
| 81 |
request.schedule_close(1000) |
| 82 |
options = { |
| 83 |
"interactive": True, |
| 84 |
} |
| 85 |
request.call( |
| 86 |
"Screenshot", |
| 87 |
parent_window="", |
| 88 |
options=options, |
| 89 |
) |
| 90 |
|
| 91 |
# Only true if the impl.Request was closed too |
| 92 |
assert request.closed |
| 93 |
|
| 94 |
@pytest.mark.parametrize("template_params", ({"screenshot": {"response": 1}},)) |
| 95 |
def test_screenshot_cancel(self, portals, dbus_con, app_id): |
| 96 |
screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot") |
| 97 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 98 |
|
| 99 |
modal = True |
| 100 |
interactive = True |
| 101 |
|
| 102 |
request = xdp.Request(dbus_con, screenshot_intf) |
| 103 |
options = { |
| 104 |
"modal": modal, |
| 105 |
"interactive": interactive, |
| 106 |
} |
| 107 |
response = request.call( |
| 108 |
"Screenshot", |
| 109 |
parent_window="", |
| 110 |
options=options, |
| 111 |
) |
| 112 |
|
| 113 |
assert response |
| 114 |
assert response.response == 1 |
| 115 |
|
| 116 |
# Check the impl portal was called with the right args |
| 117 |
method_calls = mock_intf.GetMethodCalls("Screenshot") |
| 118 |
assert len(method_calls) > 0 |
| 119 |
_, args = method_calls[-1] |
| 120 |
assert args[1] == app_id |
| 121 |
assert args[2] == "" # parent window |
| 122 |
assert args[3]["modal"] == modal |
| 123 |
assert args[3]["interactive"] == interactive |
| 124 |
|
| 125 |
def test_pick_color_basic(self, portals, dbus_con, app_id): |
| 126 |
screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot") |
| 127 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 128 |
|
| 129 |
request = xdp.Request(dbus_con, screenshot_intf) |
| 130 |
options: Any = {} |
| 131 |
response = request.call( |
| 132 |
"PickColor", |
| 133 |
parent_window="", |
| 134 |
options=options, |
| 135 |
) |
| 136 |
|
| 137 |
assert response |
| 138 |
assert response.response == 0 |
| 139 |
assert response.results["color"] == SCREENSHOT_DATA["color"] |
| 140 |
|
| 141 |
# Check the impl portal was called with the right args |
| 142 |
method_calls = mock_intf.GetMethodCalls("PickColor") |
| 143 |
assert len(method_calls) > 0 |
| 144 |
_, args = method_calls[-1] |
| 145 |
assert args[1] == app_id |
| 146 |
assert args[2] == "" # parent window |
| 147 |
|
| 148 |
@pytest.mark.parametrize( |
| 149 |
"template_params", ({"screenshot": {"expect-close": True}},) |
| 150 |
) |
| 151 |
def test_pick_color_close(self, portals, dbus_con, app_id): |
| 152 |
screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot") |
| 153 |
|
| 154 |
request = xdp.Request(dbus_con, screenshot_intf) |
| 155 |
request.schedule_close(1000) |
| 156 |
options: Any = {} |
| 157 |
request.call( |
| 158 |
"PickColor", |
| 159 |
parent_window="", |
| 160 |
options=options, |
| 161 |
) |
| 162 |
|
| 163 |
# Only true if the impl.Request was closed too |
| 164 |
assert request.closed |
| 165 |
|
| 166 |
@pytest.mark.parametrize("template_params", ({"screenshot": {"response": 1}},)) |
| 167 |
def test_pick_color_cancel(self, portals, dbus_con, app_id): |
| 168 |
screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot") |
| 169 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 170 |
|
| 171 |
request = xdp.Request(dbus_con, screenshot_intf) |
| 172 |
options: Any = {} |
| 173 |
response = request.call( |
| 174 |
"PickColor", |
| 175 |
parent_window="", |
| 176 |
options=options, |
| 177 |
) |
| 178 |
|
| 179 |
assert response |
| 180 |
assert response.response == 1 |
| 181 |
|
| 182 |
# Check the impl portal was called with the right args |
| 183 |
method_calls = mock_intf.GetMethodCalls("PickColor") |
| 184 |
assert len(method_calls) > 0 |
| 185 |
_, args = method_calls[-1] |
| 186 |
assert args[1] == app_id |
| 187 |
assert args[2] == "" # parent window |