| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# |
| 3 |
# This file is formatted with Python Black |
| 4 |
|
| 5 |
import tests.xdp_utils as xdp |
| 6 |
import tests.test_inputcapture as inputcapture |
| 7 |
|
| 8 |
import dbus |
| 9 |
import pytest |
| 10 |
import os |
| 11 |
from enum import Enum |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture |
| 15 |
def required_templates(): |
| 16 |
return { |
| 17 |
"Clipboard": {}, |
| 18 |
"RemoteDesktop": {"force-clipboard-enabled": True}, |
| 19 |
"InputCapture": { |
| 20 |
"force-clipboard-enabled": True, |
| 21 |
"activated-delay": 100, |
| 22 |
}, |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
class SessionType(Enum): |
| 27 |
REMOTE_DESKTOP = 1 |
| 28 |
INPUT_CAPTURE = 2 |
| 29 |
|
| 30 |
|
| 31 |
@pytest.mark.parametrize( |
| 32 |
"type", (SessionType.REMOTE_DESKTOP, SessionType.INPUT_CAPTURE) |
| 33 |
) |
| 34 |
class TestClipboard: |
| 35 |
def test_version(self, portals, dbus_con, type): |
| 36 |
xdp.check_version(dbus_con, "Clipboard", 1) |
| 37 |
|
| 38 |
def start_remote_desktop_session(self, dbus_con): |
| 39 |
clipboard_intf = xdp.get_portal_iface(dbus_con, "Clipboard") |
| 40 |
remotedesktop_intf = xdp.get_portal_iface(dbus_con, "RemoteDesktop") |
| 41 |
|
| 42 |
create_session_request = xdp.Request(dbus_con, remotedesktop_intf) |
| 43 |
create_session_response = create_session_request.call( |
| 44 |
"CreateSession", options={"session_handle_token": "1234"} |
| 45 |
) |
| 46 |
assert create_session_response |
| 47 |
assert create_session_response.response == 0 |
| 48 |
assert str(create_session_response.results["session_handle"]) |
| 49 |
|
| 50 |
session = xdp.Session.from_response(dbus_con, create_session_response) |
| 51 |
|
| 52 |
clipboard_intf.RequestClipboard(session.handle, {}) |
| 53 |
|
| 54 |
start_session_request = xdp.Request(dbus_con, remotedesktop_intf) |
| 55 |
start_session_response = start_session_request.call( |
| 56 |
"Start", session_handle=session.handle, parent_window="", options={} |
| 57 |
) |
| 58 |
|
| 59 |
assert start_session_response |
| 60 |
assert start_session_response.response == 0 |
| 61 |
|
| 62 |
return (session, start_session_response.results.get("clipboard_enabled")) |
| 63 |
|
| 64 |
def start_input_capture_session(self, dbus_con): |
| 65 |
inputcapture_intf = xdp.get_portal_iface(dbus_con, "InputCapture") |
| 66 |
|
| 67 |
session = inputcapture.InputcaptureSession(dbus_con) |
| 68 |
session.create() |
| 69 |
start_session_response = session.start() |
| 70 |
session.get_zones() |
| 71 |
# The default zone is 1920x1080 |
| 72 |
barriers = [ |
| 73 |
{ |
| 74 |
"barrier_id": dbus.UInt32(10, variant_level=1), |
| 75 |
"position": dbus.Struct( |
| 76 |
[0, 0, 1920, 0], signature="iiii", variant_level=1 |
| 77 |
), |
| 78 |
}, |
| 79 |
] |
| 80 |
session.set_pointer_barriers(barriers) |
| 81 |
session.connect_to_eis() |
| 82 |
|
| 83 |
activated_signal_received = False |
| 84 |
|
| 85 |
def cb_activated(session_handle, options): |
| 86 |
nonlocal activated_signal_received |
| 87 |
activated_signal_received = True |
| 88 |
|
| 89 |
inputcapture_intf.connect_to_signal("Activated", cb_activated) |
| 90 |
|
| 91 |
session.enable() |
| 92 |
|
| 93 |
xdp.wait_for(lambda: activated_signal_received) |
| 94 |
|
| 95 |
return ( |
| 96 |
session.session, |
| 97 |
start_session_response.results.get("clipboard_enabled"), |
| 98 |
) |
| 99 |
|
| 100 |
def start_session(self, dbus_con, type): |
| 101 |
if type == SessionType.REMOTE_DESKTOP: |
| 102 |
return self.start_remote_desktop_session(dbus_con) |
| 103 |
if type == SessionType.INPUT_CAPTURE: |
| 104 |
return self.start_input_capture_session(dbus_con) |
| 105 |
raise Exception("Unknown type") |
| 106 |
|
| 107 |
def test_request_clipboard_and_start_session(self, portals, dbus_con, type): |
| 108 |
_, clipboard_enabled = self.start_session(dbus_con, type) |
| 109 |
|
| 110 |
assert clipboard_enabled |
| 111 |
|
| 112 |
@pytest.mark.parametrize( |
| 113 |
"template_params", |
| 114 |
( |
| 115 |
{ |
| 116 |
"RemoteDesktop": {"force-clipboard-enabled": False}, |
| 117 |
"InputCapture": { |
| 118 |
"force-clipboard-enabled": False, |
| 119 |
"activated-delay": 100, |
| 120 |
}, |
| 121 |
}, |
| 122 |
), |
| 123 |
) |
| 124 |
def test_checks_clipboard_enabled(self, portals, dbus_con, type): |
| 125 |
clipboard_intf = xdp.get_portal_iface(dbus_con, "Clipboard") |
| 126 |
session, clipboard_enabled = self.start_session(dbus_con, type) |
| 127 |
|
| 128 |
assert not clipboard_enabled |
| 129 |
|
| 130 |
with pytest.raises(dbus.exceptions.DBusException): |
| 131 |
clipboard_intf.SetSelection(session.handle, {}) |
| 132 |
|
| 133 |
def test_set_selection(self, portals, dbus_con, type): |
| 134 |
clipboard_intf = xdp.get_portal_iface(dbus_con, "Clipboard") |
| 135 |
session, _ = self.start_session(dbus_con, type) |
| 136 |
|
| 137 |
clipboard_intf.SetSelection(session.handle, {}) |
| 138 |
|
| 139 |
def test_selection_write(self, portals, dbus_con, type): |
| 140 |
clipboard_intf = xdp.get_portal_iface(dbus_con, "Clipboard") |
| 141 |
session, _ = self.start_session(dbus_con, type) |
| 142 |
|
| 143 |
fd_object: dbus.types.UnixFd = clipboard_intf.SelectionWrite( |
| 144 |
session.handle, 1234 |
| 145 |
) |
| 146 |
assert fd_object |
| 147 |
|
| 148 |
fd = fd_object.take() |
| 149 |
assert fd |
| 150 |
|
| 151 |
bytes_written = os.write(fd, b"Clipboard") |
| 152 |
assert bytes_written > 0 |
| 153 |
|
| 154 |
clipboard_intf.SelectionWriteDone(session.handle, 1234, True) |
| 155 |
|
| 156 |
def test_selection_read(self, portals, dbus_con, type): |
| 157 |
clipboard_intf = xdp.get_portal_iface(dbus_con, "Clipboard") |
| 158 |
session, _ = self.start_session(dbus_con, type) |
| 159 |
|
| 160 |
fd_object: dbus.types.UnixFd = clipboard_intf.SelectionRead( |
| 161 |
session.handle, "mimetype" |
| 162 |
) |
| 163 |
assert fd_object |
| 164 |
|
| 165 |
fd = fd_object.take() |
| 166 |
assert fd |
| 167 |
|
| 168 |
clipboard_contents = os.read(fd, 1000) |
| 169 |
assert str(clipboard_contents) |