Truly independent web browser
"""Line-boundary text invariants.
The key problem to prevent regressing: leaf-like containers like list items must return the full flattened text for
textAtOffset(LineBoundary) — not the first visual sub-line. Paragraphs with wrapping content, by contrast, *do* return
visual sub-lines (flat review depends on this)."""
from __future__ import annotations
import pathlib
import sys
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
import gi
gi.require_version("Atspi", "2.0")
from gi.repository import Atspi # noqa: E402
from harness import AccessibilityBridgeTestCase # noqa: E402
from harness import find_all_by_role # noqa: E402
class LeafLikeLineBoundaryTests(AccessibilityBridgeTestCase):
"""Leaf-like containers with LineBoundary must return full flattened text.
Guards against regressing an I-key problem: on a wrapping list item, Orca calls get_string_at_offset(li, 0, LINE)
and expects the full item text. A previous buggy implementation returned only the first visual sub-line (often just
the first character)."""
FIXTURE = "listitems.html"
def test_simple_listitem_at_LINE_returns_full_text(self):
items = find_all_by_role(self.doc, "list item")
apple = next(
(li for li in items if Atspi.Text.get_text(li, 0, Atspi.Text.get_character_count(li)) == "Apple"), None
)
self.assertIsNotNone(apple, "expected 'Apple' list item")
r = Atspi.Text.get_string_at_offset(apple, 0, Atspi.TextGranularity.LINE)
self.assertEqual(r.content, "Apple")
self.assertEqual(r.start_offset, 0)
self.assertEqual(r.end_offset, 5)
def test_wrapping_listitem_at_LINE_returns_full_text_not_first_char(self):
"""Narrow-width listitem whose text wraps one-char-per-line must still speak as 'ABC', not 'A'."""
items = find_all_by_role(self.doc, "list item")
abc = next(
(li for li in items if Atspi.Text.get_text(li, 0, Atspi.Text.get_character_count(li)) == "ABC"), None
)
self.assertIsNotNone(abc, "expected 'ABC' narrow list item")
r = Atspi.Text.get_string_at_offset(abc, 0, Atspi.TextGranularity.LINE)
self.assertEqual(r.content, "ABC", f"narrow listitem must return full text; got {r.content!r}")
self.assertEqual(r.start_offset, 0)
self.assertEqual(r.end_offset, 3)
def test_listitem_at_LINE_at_every_offset_returns_full_text(self):
"""For a leaf-like container, LINE at any valid offset returns the same whole text."""
items = find_all_by_role(self.doc, "list item")
banana = next(
(li for li in items if Atspi.Text.get_text(li, 0, Atspi.Text.get_character_count(li)) == "Banana"), None
)
self.assertIsNotNone(banana)
length = Atspi.Text.get_character_count(banana)
for offset in range(length):
r = Atspi.Text.get_string_at_offset(banana, offset, Atspi.TextGranularity.LINE)
self.assertNotEqual(r.start_offset, -1, f"LINE at valid offset {offset} returned the past-end sentinel")
self.assertEqual(r.content, "Banana", f"LINE at offset {offset} must be full text; got {r.content!r}")
class ParagraphLineBoundaryTests(AccessibilityBridgeTestCase):
"""Paragraphs with wrapping content *do* return visual sub-lines.
This is the other side of the contract: flat review's get_visible_lines iterates per-visual-line — relying on this
exact behavior for paragraphs."""
FIXTURE = "paragraphs.html"
def test_plain_paragraph_at_LINE_returns_full_text(self):
"""Single-line paragraph: the visual line *is* the full text."""
ps = find_all_by_role(self.doc, "paragraph")
plain = next(
(p for p in ps if Atspi.Text.get_text(p, 0, Atspi.Text.get_character_count(p)).startswith("Plain")), None
)
self.assertIsNotNone(plain)
r = Atspi.Text.get_string_at_offset(plain, 0, Atspi.TextGranularity.LINE)
self.assertEqual(r.content, "Plain paragraph with no inline objects.")
def test_wrapping_paragraph_at_LINE_returns_only_first_visual_line(self):
"""Multi-line paragraph — LINE at offset 0 returns only the first visual line, not the full text."""
ps = find_all_by_role(self.doc, "paragraph")
wrapping = next(
(
p
for p in ps
if "deliberately long paragraph" in Atspi.Text.get_text(p, 0, Atspi.Text.get_character_count(p))
),
None,
)
self.assertIsNotNone(wrapping, "expected a visibly-wrapped paragraph fixture")
full_len = Atspi.Text.get_character_count(wrapping)
r = Atspi.Text.get_string_at_offset(wrapping, 0, Atspi.TextGranularity.LINE)
self.assertLess(
r.end_offset,
full_len,
f"wrapping paragraph must return a sub-line, not the whole text "
f"(got end_offset={r.end_offset}, total={full_len})",
)
self.assertEqual(r.start_offset, 0)
def test_superscript_stays_on_one_visual_line(self):
"""A superscript shifts the baseline within a line; the half-line-height tolerance keeps it one line."""
ps = find_all_by_role(self.doc, "paragraph")
supp = next(
(p for p in ps if Atspi.Text.get_text(p, 0, Atspi.Text.get_character_count(p)).startswith("E = mc")),
None,
)
self.assertIsNotNone(supp, "expected the superscript paragraph fixture")
full = Atspi.Text.get_text(supp, 0, Atspi.Text.get_character_count(supp))
r = Atspi.Text.get_string_at_offset(supp, 0, Atspi.TextGranularity.LINE)
self.assertEqual(r.content, full, "a superscript must not split the visual line")
def test_tall_inline_image_stays_on_one_visual_line(self):
"""A baseline-aligned image taller than the text has its top well above the text's top. Its y must come from the
line it sits on, not from its own top edge -- or the paragraph reads as three visual lines: the text before the
image, the image, and the text after it."""
ps = find_all_by_role(self.doc, "paragraph")
tall = next(
(
p
for p in ps
if Atspi.Text.get_text(p, 0, Atspi.Text.get_character_count(p)).startswith("Text before a tall")
),
None,
)
self.assertIsNotNone(tall, "expected the tall-inline-image paragraph fixture")
full = Atspi.Text.get_text(tall, 0, Atspi.Text.get_character_count(tall))
self.assertIn("\ufffc", full, "the image must occupy an embedded-object character in the paragraph's text")
r = Atspi.Text.get_string_at_offset(tall, 0, Atspi.TextGranularity.LINE)
self.assertEqual(r.content, full, "a tall inline image must not split the visual line")
def test_tall_inline_image_on_a_later_line_stays_on_its_line(self):
"""The same with the image on the paragraph's second line, where the text's y is measured from the paragraph's
first line and the image's own top edge isn't even that line's top."""
ps = find_all_by_role(self.doc, "paragraph")
later = next(
(
p
for p in ps
if Atspi.Text.get_text(p, 0, Atspi.Text.get_character_count(p)).startswith("A first line long enough")
),
None,
)
self.assertIsNotNone(later, "expected the later-line tall-image paragraph fixture")
full = Atspi.Text.get_text(later, 0, Atspi.Text.get_character_count(later))
image = full.find("\ufffc")
self.assertGreater(image, 0, "the image must occupy an embedded-object character in the paragraph's text")
first = Atspi.Text.get_string_at_offset(later, 0, Atspi.TextGranularity.LINE)
self.assertNotIn("\ufffc", first.content, "the fixture must wrap before the image")
line = Atspi.Text.get_string_at_offset(later, image, Atspi.TextGranularity.LINE)
# The nowrap span keeps "the tall", the image and "inline" on one visual line, whatever else wraps around them.
self.assertIn("the tall \ufffc inline", line.content, "a tall inline image must not split its visual line")
def test_nested_span_paragraph_wraps_into_visual_lines(self):
"""Wrapping text inside nested <span>s must still split into visual sub-lines.
append_offset_ys must promote nested ignored generics recursively, the same way build_hypertext builds
the offset space. If it only promotes one level, the nested text has no resolvable y, the wrap is never seen,
and get_string_at_offset(0, LINE) collapses the whole paragraph into a single line."""
ps = find_all_by_role(self.doc, "paragraph")
nested = next(
(p for p in ps if "nested run" in Atspi.Text.get_text(p, 0, Atspi.Text.get_character_count(p))),
None,
)
self.assertIsNotNone(nested, "expected the nested-span paragraph fixture")
full_len = Atspi.Text.get_character_count(nested)
r = Atspi.Text.get_string_at_offset(nested, 0, Atspi.TextGranularity.LINE)
self.assertEqual(r.start_offset, 0)
self.assertLess(
r.end_offset,
full_len,
f"nested-span paragraph must wrap into visual sub-lines (end_offset={r.end_offset}, total={full_len})",
)
if __name__ == "__main__":
unittest.main()