Skip to content

Commit

Permalink
updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhbooth committed Aug 22, 2022
1 parent 99e273d commit a3c8d06
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
16 changes: 9 additions & 7 deletions tests/templates/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from tests.templates import Response, init_template_logger, ImplRequest
import dbus.service
import dbus
import io
import tempfile
from gi.repository import GLib

BUS_NAME = "org.freedesktop.impl.portal.Test"
Expand Down Expand Up @@ -129,9 +129,9 @@ def SelectionWrite(

request = ImplRequest(self, BUS_NAME, handle)

# Open a file
fd = io.StringIO("")
response.results["fd"] = dbus.types.UnixFd(fd)
# Open a temporary file and pass its fd
tmp = tempfile.TemporaryFile()
response.results["fd"] = dbus.types.UnixFd(tmp.fileno())

if self.expect_close:

Expand Down Expand Up @@ -213,9 +213,11 @@ def SelectionRead(

request = ImplRequest(self, BUS_NAME, handle)

# Open a file
fd = io.StringIO("Clipboard")
response.results["fd"] = dbus.types.UnixFd(fd)
# Open a temporary file and pass its fd
tmp = tempfile.TemporaryFile()
tmp.write(b"Clipboard")
tmp.seek(0)
response.results["fd"] = dbus.types.UnixFd(tmp.fileno())

if self.expect_close:

Expand Down
24 changes: 16 additions & 8 deletions tests/test_clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import select


class TestClipboard(PortalTest):
def test_version(self):
self.check_version(1)
Expand Down Expand Up @@ -56,11 +57,15 @@ def test_clipboard_selection_write(self):
session_handle = self.create_session_handle()
clipboard_interface = self.get_dbus_interface()

selection_write_request = Request(self.dbus_con, clipboard_interface)
selection_write_response = selection_write_request.call(
"SelectionWrite", session_handle=session_handle, serial=4321, options={}
)
assert selection_write_response.response == 0
(unixFd, handle) = clipboard_interface.SelectionWrite(session_handle, 4321, {})

fd = unixFd.take()
assert fd

select.select([fd], [], []) # wait for file descriptor to be available
bytes_written = os.write(fd, b"Clipboard")

assert bytes_written > 0

selection_write_done_request = Request(self.dbus_con, clipboard_interface)
selection_write_done_response = selection_write_done_request.call(
Expand All @@ -77,11 +82,14 @@ def test_clipboard_selection_read(self):
session_handle = self.create_session_handle()
clipboard_interface = self.get_dbus_interface()

response = clipboard_interface.SelectionRead(session_handle, '', {})
(unixFd, handle) = clipboard_interface.SelectionRead(session_handle, "", {})

fd = unixFd.take()
select.select([fd], [], []) # wait for file descriptor to be available
assert fd

select.select([fd], [], []) # wait for file descriptor to be available
clipboard = os.read(fd, 1000)
assert str(clipboard)

def test_clipboard_throws_without_enable(self):
session_handle = self.create_session_handle(False) # clipboard not enabled
Expand Down

0 comments on commit a3c8d06

Please sign in to comment.