| 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 |
|
| 11 |
|
| 12 |
@pytest.fixture |
| 13 |
def required_templates(): |
| 14 |
return { |
| 15 |
"Clipboard": {}, |
| 16 |
"RemoteDesktop": {"force-clipboard-enabled": True}, |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
class TestClipboard: |
| 21 |
def test_version(self, portals, dbus_con): |
| 22 |
xdp.check_version(dbus_con, "Clipboard", 1) |
| 23 |
|
| 24 |
def start_session(self, dbus_con): |
| 25 |
clipboard_intf = xdp.get_portal_iface(dbus_con, "Clipboard") |
| 26 |
remotedesktop_intf = xdp.get_portal_iface(dbus_con, "RemoteDesktop") |
| 27 |
|
| 28 |
create_session_request = xdp.Request(dbus_con, remotedesktop_intf) |
| 29 |
create_session_response = create_session_request.call( |
| 30 |
"CreateSession", options={"session_handle_token": "1234"} |
| 31 |
) |
| 32 |
assert create_session_response |
| 33 |
assert create_session_response.response == 0 |
| 34 |
assert str(create_session_response.results["session_handle"]) |
| 35 |
|
| 36 |
session = xdp.Session.from_response(dbus_con, create_session_response) |
| 37 |
|
| 38 |
clipboard_intf.RequestClipboard(session.handle, {}) |
| 39 |
|
| 40 |
start_session_request = xdp.Request(dbus_con, remotedesktop_intf) |
| 41 |
start_session_response = start_session_request.call( |
| 42 |
"Start", session_handle=session.handle, parent_window="", options={} |
| 43 |
) |
| 44 |
|
| 45 |
assert start_session_response |
| 46 |
assert start_session_response.response == 0 |
| 47 |
|
| 48 |
return (session, start_session_response.results.get("clipboard_enabled")) |
| 49 |
|
| 50 |
def test_request_clipboard_and_start_session(self, portals, dbus_con): |
| 51 |
_, clipboard_enabled = self.start_session(dbus_con) |
| 52 |
|
| 53 |
assert clipboard_enabled |
| 54 |
|
| 55 |
@pytest.mark.parametrize( |
| 56 |
"template_params", ({"RemoteDesktop": {"force-clipboard-enabled": False}},) |
| 57 |
) |
| 58 |
def test_checks_clipboard_enabled(self, portals, dbus_con): |
| 59 |
clipboard_intf = xdp.get_portal_iface(dbus_con, "Clipboard") |
| 60 |
session, clipboard_enabled = self.start_session(dbus_con) |
| 61 |
|
| 62 |
assert not clipboard_enabled |
| 63 |
|
| 64 |
with pytest.raises(dbus.exceptions.DBusException): |
| 65 |
clipboard_intf.SetSelection(session.handle, {}) |
| 66 |
|
| 67 |
def test_set_selection(self, portals, dbus_con): |
| 68 |
clipboard_intf = xdp.get_portal_iface(dbus_con, "Clipboard") |
| 69 |
session, _ = self.start_session(dbus_con) |
| 70 |
|
| 71 |
clipboard_intf.SetSelection(session.handle, {}) |
| 72 |
|
| 73 |
def test_selection_write(self, portals, dbus_con): |
| 74 |
clipboard_intf = xdp.get_portal_iface(dbus_con, "Clipboard") |
| 75 |
session, _ = self.start_session(dbus_con) |
| 76 |
|
| 77 |
fd_object: dbus.types.UnixFd = clipboard_intf.SelectionWrite( |
| 78 |
session.handle, 1234 |
| 79 |
) |
| 80 |
assert fd_object |
| 81 |
|
| 82 |
fd = fd_object.take() |
| 83 |
assert fd |
| 84 |
|
| 85 |
bytes_written = os.write(fd, b"Clipboard") |
| 86 |
assert bytes_written > 0 |
| 87 |
|
| 88 |
clipboard_intf.SelectionWriteDone(session.handle, 1234, True) |
| 89 |
|
| 90 |
def test_selection_read(self, portals, dbus_con): |
| 91 |
clipboard_intf = xdp.get_portal_iface(dbus_con, "Clipboard") |
| 92 |
session, _ = self.start_session(dbus_con) |
| 93 |
|
| 94 |
fd_object: dbus.types.UnixFd = clipboard_intf.SelectionRead( |
| 95 |
session.handle, "mimetype" |
| 96 |
) |
| 97 |
assert fd_object |
| 98 |
|
| 99 |
fd = fd_object.take() |
| 100 |
assert fd |
| 101 |
|
| 102 |
clipboard_contents = os.read(fd, 1000) |
| 103 |
assert str(clipboard_contents) |