# SPDX-License-Identifier: LGPL-2.1-or-later
"""The published interface matches the signatures wine-access checks."""
from pathlib import Path
import sys
import unittest
from xml.etree import ElementTree
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "wine_access"))
from service import INTERFACE, WineAccessInterface, ssml_status # noqa: E402
# From wine-access's README: method -> (input signature, output signature).
REQUIRED = {
"SpeakText": ("sb", "b"),
"CancelSpeech": ("", "b"),
"BrailleMessage": ("s", "b"),
"SpeakSsml": ("ss", "i"),
"PresentAccessibleEvent": ("sssssssiis", "b"),
# Optional review protocol version 1.
"RegisterWineReviewProvider": ("ou", "b"),
"PresentWineReviewFocus": ("ssstssb(iiii)", "b"),
}
class Backend:
def speak_text(self, text, interrupt):
return True
def cancel_speech(self):
raise RuntimeError("Orca failed")
def braille_message(self, text):
return text
def present_event(self, *arguments):
self.arguments = arguments
return True
class InterfaceTests(unittest.TestCase):
def test_signatures_match_helper_contract(self):
root = ElementTree.fromstring(WineAccessInterface.__dbus_xml__)
interface = root.find(f"interface[@name='{INTERFACE}']")
found = {}
for method in interface.findall("method"):
args = method.findall("arg")
found[method.get("name")] = (
"".join(a.get("type") for a in args if a.get("direction") == "in"),
"".join(a.get("type") for a in args if a.get("direction") == "out"))
self.assertEqual(found, REQUIRED)
def test_backend_errors_answer_false(self):
service = WineAccessInterface(Backend())
self.assertTrue(service.SpeakText("hi", False))
self.assertFalse(service.CancelSpeech())
self.assertFalse(service.BrailleMessage(""))
def test_event_arguments_pass_through_in_order(self):
backend = Backend()
service = WineAccessInterface(backend)
service.PresentAccessibleEvent("s", "focus", "n", "r", "v", "st", "d", 1, 2, "w")
self.assertEqual(backend.arguments, ("s", "focus", "n", "r", "v", "st", "d", 1, 2, "w"))
def test_ssml_status_codes(self):
self.assertEqual(ssml_status("
Hi
"), 87) self.assertEqual(ssml_status("