import re import unittest from tests.gates.test_static_contract import assert_source_contains from pathlib import Path from tests.support.reader_sources import read_reader_source ROOT = Path(__file__).parents[3] class ReaderAccessibilityTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.reader = read_reader_source(ROOT) cls.html = (ROOT / "static/reader.html").read_text(encoding="utf-8") cls.css = (ROOT / "static/reader.css").read_text(encoding="utf-8") def test_reader_controls_and_regions_have_accessible_contracts(self): for value in ('role="status"', 'id="page-number"', 'id="page-total"'): self.assertIn(value, self.html) for control_id, label in (('back', '返回'), ('page-number', '页码'), ('zoom', '缩放百分比')): with self.subTest(control=control_id): control = re.search(rf'<(?:button|input)\b[^>]*\bid="{control_id}"[^>]*>', self.html) self.assertIsNotNone(control) self.assertIn(f'aria-label="{label}"', control[0]) self.assertRegex(self.html, r']*\bid="bookmark-popover"[^>]*\brole="dialog"[^>]*\baria-modal="true"') for value in ('setAttribute("role", "region")', 'setAttribute("aria-label", `第 ${page} 页`)', 'focus({ preventScroll: true })', 'composedPath()'): assert_source_contains(self, value, self.reader) self.assertRegex(self.css, r'prefers-contrast\s*:\s*more') for value in ('aria-label="下载原文件"', 'role="tablist"', 'role="tab"', 'aria-controls="toc-panel"', 'role="tabpanel"'): self.assertIn(value, self.html)