Skip to content

Commit

Permalink
tests/py: Add fixtures for specifying required templates and params
Browse files Browse the repository at this point in the history
The required_templates fixtures specifies to templates which are
required to be mocked by the test case. By default this is the portal
implementation of the portal that's being tested. The return value is a
map where the values are the parameters which will be passed to the
template.

The template_params fixture allows to override the parameters of
templates. This is especially useful when combined with the
@pytest.mark.parametrize annotation.

The clipboard tests are adjusted to make use of this.
  • Loading branch information
swick committed Oct 17, 2024
1 parent 0989e99 commit 0221cfd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 31 deletions.
33 changes: 30 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ def params() -> dict[str, Any]:
return {}


@pytest.fixture
def template_params(portal_name, params) -> dict[str, dict[str, Any]]:
"""
Default fixture for overriding the parameters which should be passed to the
mocking templates. Use required_templates to specify the default parameters
and override it for specific test cases via
@pytest.mark.parametrize("template_params", ({"Template": {"foo": "bar"}},))
"""
return {portal_name: params}


@pytest.fixture
def required_templates(portal_name, portal_has_impl) -> dict[str, dict[str, Any]]:
"""
Default fixture for enumerating the mocking templates the test case requires
to be started. This is a map from a name of a template in the templates
directory to the parameters which should be passed to the template.
"""
if portal_has_impl:
return {portal_name: {}}

return {}


@pytest.fixture
def app_id():
"""
Expand All @@ -68,12 +94,13 @@ def app_id():


@pytest.fixture
def portal_mock(dbus_test_case, portal_name, params, portal_has_impl, app_id) -> PortalMock:
def portal_mock(dbus_test_case, portal_name, required_templates, template_params, app_id) -> PortalMock:
"""
Fixture yielding a PortalMock object with the impl started, if applicable.
"""
pmock = PortalMock(dbus_test_case, portal_name, app_id)
if portal_has_impl:
pmock.start_template(portal_name, params)
for template, params in required_templates.items():
params = template_params.get(template, params)
pmock.start_template(template, params)
pmock.start_xdp()
return pmock
52 changes: 24 additions & 28 deletions tests/test_clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ def portal_name():
return "Clipboard"


@pytest.fixture
def required_templates():
return {
"Clipboard": {},
"RemoteDesktop": {"force-clipboard-enabled": True},
}


class TestClipboard:
def test_version(self, portal_mock):
portal_mock.check_version(1)

def start_session(self, pmock, params={}):
pmock.start_template("Clipboard", params=params)
pmock.start_template("RemoteDesktop", params=params)
pmock.start_xdp()

def start_session(self, pmock):
create_session_request = pmock.create_request("RemoteDesktop")
create_session_response = create_session_request.call(
"CreateSession", options={"session_handle_token": "1234"}
Expand All @@ -43,36 +47,30 @@ def start_session(self, pmock, params={}):

return (session, start_session_response.results.get("clipboard_enabled"))

def test_request_clipboard_and_start_session(self, dbus_test_case):
pmock = PortalMock(dbus_test_case, "Clipboard")
params = {"force-clipboard-enabled": True}
_, clipboard_enabled = self.start_session(pmock, params)
def test_request_clipboard_and_start_session(self, portal_mock):
_, clipboard_enabled = self.start_session(portal_mock)

assert clipboard_enabled

def test_clipboard_checks_clipboard_enabled(self, dbus_test_case):
pmock = PortalMock(dbus_test_case, "Clipboard")
session, clipboard_enabled = self.start_session(pmock)
clipboard_interface = pmock.get_dbus_interface()
@pytest.mark.parametrize("template_params", ({"RemoteDesktop": {"force-clipboard-enabled": False}},))
def test_clipboard_checks_clipboard_enabled(self, portal_mock):
session, clipboard_enabled = self.start_session(portal_mock)
clipboard_interface = portal_mock.get_dbus_interface()

assert not clipboard_enabled

with pytest.raises(dbus.exceptions.DBusException):
clipboard_interface.SetSelection(session.handle, {})

def test_clipboard_set_selection(self, dbus_test_case):
pmock = PortalMock(dbus_test_case, "Clipboard")
params = {"force-clipboard-enabled": True}
session, _ = self.start_session(pmock, params)
clipboard_interface = pmock.get_dbus_interface()
def test_clipboard_set_selection(self, portal_mock):
session, _ = self.start_session(portal_mock)
clipboard_interface = portal_mock.get_dbus_interface()

clipboard_interface.SetSelection(session.handle, {})

def test_clipboard_selection_write(self, dbus_test_case):
pmock = PortalMock(dbus_test_case, "Clipboard")
params = {"force-clipboard-enabled": True}
session, _ = self.start_session(pmock, params)
clipboard_interface = pmock.get_dbus_interface()
def test_clipboard_selection_write(self, portal_mock):
session, _ = self.start_session(portal_mock)
clipboard_interface = portal_mock.get_dbus_interface()

fd_object: dbus.types.UnixFd = clipboard_interface.SelectionWrite(
session.handle, 1234
Expand All @@ -87,11 +85,9 @@ def test_clipboard_selection_write(self, dbus_test_case):

clipboard_interface.SelectionWriteDone(session.handle, 1234, True)

def test_clipboard_selection_read(self, dbus_test_case):
pmock = PortalMock(dbus_test_case, "Clipboard")
params = {"force-clipboard-enabled": True}
session, _ = self.start_session(pmock, params)
clipboard_interface = pmock.get_dbus_interface()
def test_clipboard_selection_read(self, portal_mock):
session, _ = self.start_session(portal_mock)
clipboard_interface = portal_mock.get_dbus_interface()

fd_object: dbus.types.UnixFd = clipboard_interface.SelectionRead(
session.handle, "mimetype"
Expand Down

0 comments on commit 0221cfd

Please sign in to comment.