Skip to content

Truly independent web browser

10.9 KB
Raw Blame
"""State + action invariants — AXEnabled, AXFocused, accessibilityActionNames.

The macOS surface is narrower than AT-SPI2 here: no platform-level "focusable" bit (focus is binary AXFocused), and no
"sensitive" state. The wrapper exposes:

* AXEnabled  ←  *not* data->is_disabled
* AXFocused  ←  data->is_focused (or, for the document, being the focused UI element)
* AXPress action  ←  for button/link/checkbox/radio/menuitem/tab roles only"""

from __future__ import annotations

import pathlib
import sys
import unittest

sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))

from ApplicationServices import AXUIElementCopyActionNames  # noqa: E402
from ApplicationServices import AXUIElementSetAttributeValue  # noqa: E402
from ApplicationServices import kAXEnabledAttribute  # noqa: E402
from ApplicationServices import kAXFocusedAttribute  # noqa: E402
from ApplicationServices import kAXFocusedUIElementAttribute  # noqa: E402
from ApplicationServices import kAXRoleAttribute  # noqa: E402
from ApplicationServices import kAXValueAttribute  # noqa: E402
from harness import AccessibilityBridgeMacTestCase  # noqa: E402
from harness import wait_for  # noqa: E402
from harness import wait_for_descendant_by_role  # noqa: E402
from harness.ladybird import _ax_attr  # noqa: E402


def _action_names(elem):
    """Return the list of action names this element advertises. Empty list on any error."""
    if elem is None:
        return []
    try:
        err, names = AXUIElementCopyActionNames(elem, None)
    except Exception:
        return []
    return list(names) if err == 0 and names else []


class EnabledStateTests(AccessibilityBridgeMacTestCase):
    """AXEnabled tracks the wrapper's `!data->is_disabled` rule."""

    FIXTURE = "states.html"

    def test_normal_button_is_enabled(self):
        """A plain <button> reports AXEnabled=YES."""
        btn = wait_for_descendant_by_role(self.web, "AXButton", title="Normal button")
        self.assertIsNotNone(btn, "expected 'Normal button' in the tree")
        self.assertEqual(_ax_attr(btn, kAXEnabledAttribute), True)

    def test_disabled_button_is_not_enabled(self):
        """<button disabled> reports AXEnabled=NO. This is the canary for the wrapper's
        `data->is_disabled ? @NO : @YES` translation."""
        btn = wait_for_descendant_by_role(self.web, "AXButton", title="Disabled button")
        self.assertIsNotNone(btn, "expected 'Disabled button' in the tree")
        self.assertEqual(_ax_attr(btn, kAXEnabledAttribute), False)

    def test_link_is_enabled(self):
        """<a href> reports AXEnabled=YES. (Links don't currently track is_disabled in WebContent.)"""
        link = wait_for_descendant_by_role(self.web, "AXLink")
        self.assertIsNotNone(link, "expected an AXLink in the tree")
        self.assertEqual(_ax_attr(link, kAXEnabledAttribute), True)

    def test_text_input_is_enabled(self):
        """<input type=text> reports AXEnabled=YES."""
        entry = wait_for_descendant_by_role(self.web, "AXTextField", title="Text field")
        self.assertIsNotNone(entry, "expected the 'Text field' input in the tree")
        self.assertEqual(_ax_attr(entry, kAXEnabledAttribute), True)

    def test_checkbox_is_enabled(self):
        """<input type=checkbox> reports AXEnabled=YES."""
        cb = wait_for_descendant_by_role(self.web, "AXCheckBox", title="Checkbox")
        self.assertIsNotNone(cb, "expected the 'Checkbox' input in the tree")
        self.assertEqual(_ax_attr(cb, kAXEnabledAttribute), True)


class FocusedStateTests(AccessibilityBridgeMacTestCase):
    """AXFocused on individual elements tracks data->is_focused; the document reports it exactly when it's the page's
    focused UI element."""

    FIXTURE = "states.html"

    def test_normal_button_is_not_focused_initially(self):
        """A page-load with no autofocus should have AXFocused=NO on every interactive element. Specifically
        defending the regression where every read of AXFocused returned YES (used to bounce VoiceOver)."""
        btn = wait_for_descendant_by_role(self.web, "AXButton", title="Normal button")
        self.assertIsNotNone(btn)
        # Could be either False or NO (False); the wrapper returns @YES/@NO which PyObjC marshals to True/False.
        self.assertEqual(_ax_attr(btn, kAXFocusedAttribute), False)

    def test_web_area_is_focused_while_nothing_in_the_page_is(self):
        """With nothing in the page focused, the application's focused UI element is the AXWebArea — and the web area
        reports AXFocused=YES itself. Defends the regression where it reported NO: VoiceOver then set AXFocused on it
        after every tree update, announcing the web area over and over and never reading the page."""
        focused = wait_for(
            lambda: _ax_attr(self.app, kAXFocusedUIElementAttribute), description="the application's focused element"
        )
        self.assertIsNotNone(focused, "the application reports no focused UI element")
        self.assertEqual(_ax_attr(focused, kAXRoleAttribute), "AXWebArea")
        self.assertEqual(_ax_attr(focused, kAXFocusedAttribute), True)
        self.assertEqual(_ax_attr(self.web, kAXFocusedAttribute), True)


class FocusedElementStateTests(AccessibilityBridgeMacTestCase):
    """Once an element in the page has the focus, it alone reports AXFocused=YES — not the AXWebArea too."""

    FIXTURE = "states.html"

    def test_web_area_is_not_focused_while_an_element_is(self):
        btn = wait_for_descendant_by_role(self.web, "AXButton", title="Normal button")
        self.assertIsNotNone(btn)
        self.assertEqual(AXUIElementSetAttributeValue(btn, kAXFocusedAttribute, True), 0, "setting AXFocused failed")
        self.assertTrue(
            wait_for(lambda: _ax_attr(btn, kAXFocusedAttribute) is True, description="the button to take focus"),
            "the button never reported AXFocused",
        )
        self.assertEqual(_ax_attr(self.web, kAXFocusedAttribute), False)


class ActionNameTests(AccessibilityBridgeMacTestCase):
    """accessibilityActionNames — read externally via AXUIElementCopyActionNames.

    Wrapper rule: only button/link/checkbox/radio/menuitem/tab advertise AXPress; everything else returns an empty
    action list. Disabled-ness does *not* remove the action from the list. VoiceOver shows greyed-out actions on
    disabled controls — so users know what *would* be available."""

    FIXTURE = "states.html"

    def test_button_advertises_press_action(self):
        """A button exposes AXPress — VoiceOver's VO+Space activation needs this."""
        btn = wait_for_descendant_by_role(self.web, "AXButton", title="Normal button")
        self.assertIsNotNone(btn)
        actions = _action_names(btn)
        self.assertIn("AXPress", actions, f"expected AXPress in button actions, got {actions}")

    def test_link_advertises_press_action(self):
        """A link exposes AXPress."""
        link = wait_for_descendant_by_role(self.web, "AXLink")
        self.assertIsNotNone(link)
        actions = _action_names(link)
        self.assertIn("AXPress", actions, f"expected AXPress in link actions, got {actions}")

    def test_checkbox_advertises_press_action(self):
        """Checkboxes expose AXPress (toggling them is a press, not a value-set)."""
        cb = wait_for_descendant_by_role(self.web, "AXCheckBox", title="Checkbox")
        self.assertIsNotNone(cb)
        actions = _action_names(cb)
        self.assertIn("AXPress", actions, f"expected AXPress in checkbox actions, got {actions}")

    def test_disabled_button_still_advertises_press_action(self):
        """A disabled button's action list is unchanged. Apple's design pattern is that AXEnabled tells AT clients the
        action is currently unavailable; the action list itself stays stable – so AT can still describe what *would*
        happen."""
        btn = wait_for_descendant_by_role(self.web, "AXButton", title="Disabled button")
        self.assertIsNotNone(btn, "expected 'Disabled button' in the tree")
        actions = _action_names(btn)
        self.assertIn("AXPress", actions, f"disabled button stripped AXPress: got {actions}")

    def test_text_input_does_not_advertise_press_action(self):
        """<input type=text> is not press-able — no AXPress in its action list. VoiceOver drives text fields via
        text-marker navigation, not actions."""
        entry = wait_for_descendant_by_role(self.web, "AXTextField", title="Text field")
        self.assertIsNotNone(entry)
        actions = _action_names(entry)
        self.assertNotIn(
            "AXPress", actions, f"text field shouldn't advertise AXPress (it's not a button/link), got {actions}"
        )


class CheckedValueTests(AccessibilityBridgeMacTestCase):
    """A checkable control's AXValue is its checked state as 0, 1 or 2 — what WebKit, Blink and Gecko expose, and what
    VoiceOver reads as the state."""

    FIXTURE = "states.html"

    def _checkbox(self, title):
        cb = wait_for_descendant_by_role(self.web, "AXCheckBox", title=title)
        self.assertIsNotNone(cb, f"expected the {title!r} checkbox in the tree")
        return cb

    def test_unchecked_checkbox_value_is_0(self):
        self.assertEqual(_ax_attr(self._checkbox("Checkbox"), kAXValueAttribute), 0)

    def test_checked_checkbox_value_is_1(self):
        self.assertEqual(_ax_attr(self._checkbox("Checked checkbox"), kAXValueAttribute), 1)

    def test_mixed_checkbox_value_is_2(self):
        self.assertEqual(_ax_attr(self._checkbox("Mixed checkbox"), kAXValueAttribute), 2)

    def test_unchecked_radio_value_is_0(self):
        radio = wait_for_descendant_by_role(self.web, "AXRadioButton", title="Radio")
        self.assertIsNotNone(radio, "expected the 'Radio' input in the tree")
        self.assertEqual(_ax_attr(radio, kAXValueAttribute), 0)

    def test_text_input_value_is_not_a_checked_state(self):
        entry = wait_for_descendant_by_role(self.web, "AXTextField", title="Text field")
        self.assertIsNotNone(entry, "expected the 'Text field' input in the tree")
        self.assertIn(_ax_attr(entry, kAXValueAttribute), (None, ""))


class SelectedStateTests(AccessibilityBridgeMacTestCase):
    """AXSelected follows the node's selection, as WebKit, Blink and Gecko report it; a tab, an AXRadioButton, carries
    the selection as its AXValue too, as Blink's AXValue does for a tab."""

    FIXTURE = "states.html"

    def _tab(self, title):
        tab = wait_for_descendant_by_role(self.web, "AXRadioButton", title=title)
        self.assertIsNotNone(tab, f"expected the {title!r} tab in the tree")
        return tab

    def test_selected_tab_is_selected(self):
        tab = self._tab("Selected tab")
        self.assertEqual(_ax_attr(tab, "AXSelected"), True)
        self.assertEqual(_ax_attr(tab, kAXValueAttribute), 1)

    def test_other_tab_is_not_selected(self):
        tab = self._tab("Other tab")
        self.assertEqual(_ax_attr(tab, "AXSelected"), False)
        self.assertEqual(_ax_attr(tab, kAXValueAttribute), 0)


if __name__ == "__main__":
    unittest.main()