Publish the Wine Access protocol on Orca’s service connection and manage helpers per active Wine prefix. Add presentation, dialog capture, documentation, and tests. Preserve the Cthulhu default and shared helper code. Document the single-reader limit and verify Orca cleanup leaves Cthulhu-managed helpers alone. Retain integration artifacts on exceptional failures. Validation: 45 Orca unit tests passed; 11 existing test-environment checks passed; version metadata checks, Python compilation, and staged whitespace checks passed. Full Wine integration and live speech/braille remain unverified here because MinGW, Xvfb, and Orca are unavailable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
206 lines
8.5 KiB
Python
206 lines
8.5 KiB
Python
# SPDX-License-Identifier: LGPL-2.1-or-later
|
|
"""Presentation rules for helper events, without Orca or a main loop."""
|
|
|
|
from pathlib import Path
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "wine_access"))
|
|
|
|
from presenter import FOCUS_DEBOUNCE_MS, HOLD_MS, WineEvent, WineEventPresenter # noqa: E402
|
|
|
|
|
|
class Harness:
|
|
def __init__(self):
|
|
self.output = []
|
|
self.timers = {}
|
|
self.next_id = 1
|
|
self.now = 100.0
|
|
self.presenter = WineEventPresenter(
|
|
lambda message, interrupt: self.output.append(("present", message, interrupt)),
|
|
lambda text, inserted: self.output.append(("echo", text, inserted)),
|
|
self.schedule, self.cancel, lambda: self.now)
|
|
|
|
def schedule(self, milliseconds, callback):
|
|
assert milliseconds in (FOCUS_DEBOUNCE_MS, HOLD_MS)
|
|
handle, self.next_id = self.next_id, self.next_id + 1
|
|
self.timers[handle] = (milliseconds, callback)
|
|
return handle
|
|
|
|
def cancel(self, handle):
|
|
self.timers.pop(handle, None)
|
|
|
|
def fire(self):
|
|
"""Runs due timers in the order their delays would expire."""
|
|
while self.timers:
|
|
handle = min(self.timers, key=lambda h: (self.timers[h][0], h))
|
|
callback = self.timers.pop(handle)[1]
|
|
callback()
|
|
|
|
def send(self, source, kind, name="", role="", value="", states="", description="",
|
|
position=0, count=0, window="App"):
|
|
return self.presenter.handle(WineEvent(source, kind, name, role, value, states,
|
|
description, position, count, window))
|
|
|
|
|
|
class FocusTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.h = Harness()
|
|
|
|
def test_focus_waits_for_burst_and_names_new_window(self):
|
|
self.assertTrue(self.h.send("a", "focus", "OK", "button"))
|
|
self.assertEqual(self.h.output, [])
|
|
self.h.fire()
|
|
self.assertEqual(self.h.output, [("present", "OK, button, App", True)])
|
|
|
|
def test_only_last_focus_of_burst_is_spoken(self):
|
|
self.h.send("a", "focus", "First", "button")
|
|
self.h.send("b", "focus", "Second", "button")
|
|
self.h.fire()
|
|
self.assertEqual(self.h.output, [("present", "Second, button, App", True)])
|
|
|
|
def test_window_title_spoken_only_when_it_changes(self):
|
|
self.h.send("a", "focus", "OK", "button")
|
|
self.h.fire()
|
|
self.h.now += 1
|
|
self.h.send("b", "focus", "Cancel", "button")
|
|
self.h.fire()
|
|
self.assertEqual(self.h.output[-1], ("present", "Cancel, button", True))
|
|
|
|
def test_client_role_and_position_states(self):
|
|
self.h.send("a", "focus", "Item", "client", states="selected, focusable",
|
|
position=2, count=5)
|
|
self.h.fire()
|
|
self.assertEqual(self.h.output, [("present", "Item, selected, 2 of 5, App", True)])
|
|
|
|
def test_title_only_focus(self):
|
|
self.h.send("a", "focus")
|
|
self.h.fire()
|
|
self.assertEqual(self.h.output, [("present", "App", True)])
|
|
|
|
def test_value_within_burst_refines_pending_focus(self):
|
|
self.h.send("combo", "focus", "Size", "combo box", "Small")
|
|
self.h.send("combo", "value", "Size", "combo box", "Large")
|
|
self.h.fire()
|
|
self.assertEqual(self.h.output, [("present", "Size, combo box, Large, App", True)])
|
|
|
|
|
|
class ChangeTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.h = Harness()
|
|
self.h.send("a", "focus", "Size", "combo box", "Small")
|
|
self.h.fire()
|
|
self.h.output.clear()
|
|
self.h.now += 1
|
|
|
|
def test_value_change_of_focused_control(self):
|
|
self.assertTrue(self.h.send("a", "value", "Size", "combo box", "Large"))
|
|
self.assertEqual(self.h.output, [("present", "Size, combo box, Large", False)])
|
|
|
|
def test_background_value_is_ignored(self):
|
|
self.assertFalse(self.h.send("other", "value", "Volume", "slider", "5"))
|
|
self.assertEqual(self.h.output, [])
|
|
|
|
def test_selection_from_internal_list_in_same_window_is_accepted(self):
|
|
self.assertTrue(self.h.send("list", "selection", "Large", "list item"))
|
|
self.assertFalse(self.h.send("list2", "selection", "X", "list item", window="Other"))
|
|
self.assertEqual(self.h.output, [("present", "Large, list item", False)])
|
|
|
|
def test_duplicates_within_100ms_are_dropped(self):
|
|
self.h.send("a", "value", "Size", "combo box", "Large")
|
|
self.h.now += 0.05
|
|
self.assertFalse(self.h.send("a", "value", "Size", "combo box", "Large"))
|
|
self.h.now += 0.2
|
|
self.assertTrue(self.h.send("a", "value", "Size", "combo box", "Large"))
|
|
self.assertEqual(len(self.h.output), 2)
|
|
|
|
def test_unchanged_state_event_stays_quiet(self):
|
|
self.h.send("a", "state", "Size", "combo box", "Large")
|
|
self.h.now += 5
|
|
self.assertFalse(self.h.send("a", "state", "Size", "combo box", "Large"))
|
|
|
|
def test_unknown_event_type_rejected(self):
|
|
self.assertFalse(self.h.send("a", "bogus", "x"))
|
|
|
|
|
|
class EditorTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.h = Harness()
|
|
|
|
def test_text_flushes_pending_focus_first(self):
|
|
self.h.send("edit", "focus", "", "edit", "hello")
|
|
self.assertTrue(self.h.send("edit", "text", "h", states="kind=8 start=1"))
|
|
self.assertEqual(self.h.output, [("present", "edit, hello, App", True),
|
|
("echo", "h", True)])
|
|
self.assertEqual(self.h.timers, {})
|
|
|
|
def test_repeated_characters_are_never_merged(self):
|
|
self.h.send("edit", "focus", "", "edit")
|
|
self.h.fire()
|
|
for _ in range(3):
|
|
self.assertTrue(self.h.send("edit", "text", "l", states="kind=3"))
|
|
self.assertEqual([o for o in self.h.output if o[0] == "echo"],
|
|
[("echo", "l", False)] * 3)
|
|
|
|
def test_text_from_unfocused_source_ignored(self):
|
|
self.h.send("edit", "focus", "", "edit")
|
|
self.h.fire()
|
|
self.assertFalse(self.h.send("other", "text", "x", states="kind=8"))
|
|
|
|
def test_password_field_is_silent(self):
|
|
self.h.send("pw", "focus", "Password", "edit", "secret", description="password")
|
|
self.h.fire()
|
|
self.assertEqual(self.h.output, [("present", "Password, edit, password, App", True)])
|
|
self.assertFalse(self.h.send("pw", "text", "s", states="kind=8"))
|
|
self.h.now += 1
|
|
self.h.send("pw", "value", "Password", "edit", "secret2")
|
|
self.assertNotIn("secret", " ".join(o[1] for o in self.h.output))
|
|
|
|
def test_password_state_change_protects_field(self):
|
|
self.h.send("e", "focus", "PIN", "edit")
|
|
self.h.fire()
|
|
self.h.now += 1
|
|
self.h.send("e", "state", "PIN", "edit", states="password")
|
|
self.assertFalse(self.h.send("e", "text", "1", states="kind=8"))
|
|
|
|
def test_dialog_text_follows_pending_focus(self):
|
|
self.h.send("b", "focus", "Yes", "button")
|
|
self.h.presenter.present_after_focus("Save changes?", "App")
|
|
self.assertEqual(self.h.output, [])
|
|
self.h.fire()
|
|
self.assertEqual(self.h.output, [("present", "Yes, button, App", True),
|
|
("present", "Save changes?", False)])
|
|
|
|
def test_dialog_text_arriving_before_focus_waits_for_it(self):
|
|
self.h.presenter.present_after_focus("Save changes?", "App")
|
|
self.h.send("b", "focus", "Yes", "button")
|
|
self.h.fire()
|
|
self.assertEqual(self.h.output, [("present", "Yes, button, App", True),
|
|
("present", "Save changes?", False)])
|
|
|
|
def test_dialog_text_spoken_alone_if_no_focus_follows(self):
|
|
self.h.send("b", "focus", "Yes", "button")
|
|
self.h.fire()
|
|
self.h.presenter.present_after_focus("Save changes?", "App")
|
|
self.assertEqual(len(self.h.output), 1)
|
|
self.h.fire()
|
|
self.assertEqual(self.h.output[-1], ("present", "Save changes?", False))
|
|
|
|
def test_focus_in_other_window_does_not_take_dialog_text(self):
|
|
self.h.presenter.present_after_focus("Save changes?", "Dialog")
|
|
self.h.send("b", "focus", "OK", "button", window="Main")
|
|
self.h.fire()
|
|
self.assertEqual(self.h.output, [("present", "OK, button, Main", True),
|
|
("present", "Save changes?", False)])
|
|
|
|
def test_closed_presenter_ignores_events(self):
|
|
self.h.send("a", "focus", "OK", "button")
|
|
self.h.presenter.close()
|
|
self.assertEqual(self.h.timers, {})
|
|
self.assertFalse(self.h.send("a", "focus", "OK", "button"))
|
|
self.assertEqual(self.h.output, [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|