Files
token/test/test_accessibility.py

66 lines
2.9 KiB
Python

import sys
from pathlib import Path
import unittest
from unittest.mock import patch
import subprocess
sys.path.insert(0, str(Path(__file__).parents[1] / "vm"))
import accessibility
import desktop
class AccessibilityTests(unittest.TestCase):
def setUp(self):
self.target = {"id": "1", "path": [0, 0, 2], "app": "editor", "name": "Input", "role": "text", "bounds": [10, 20, 100, 40]}
self.saved = {"observationId": "abc", "size": [800, 600], "targets": [self.target]}
self.current = {"targets": [self.target]}
def test_badges_stay_in_their_menu_rows(self):
for y in (31, 59, 87, 223):
left, top = accessibility.badge_position([0, y, 166, 27], 22, 18, 1280, 800)
self.assertGreaterEqual(left, 0)
self.assertGreaterEqual(top, y)
self.assertLessEqual(top + 18, y + 27)
def test_badge_clamped_to_screen(self):
left, top = accessibility.badge_position([1275, 795, 5, 5], 22, 18, 1280, 800)
self.assertLessEqual(left + 22, 1280)
self.assertLessEqual(top + 18, 800)
def test_center(self):
self.assertEqual(accessibility.resolve_target(self.saved, self.current, "abc", "1", 800, 600), (60, 40))
def test_target_with_states_and_value(self):
target_with_states = {**self.target, "states": ["focused", "editable"], "value": "test text"}
saved = {"observationId": "abc", "size": [800, 600], "targets": [target_with_states]}
current = {"targets": [target_with_states]}
self.assertEqual(accessibility.resolve_target(saved, current, "abc", "1", 800, 600), (60, 40))
def test_stale_and_missing(self):
for observation, target, width in [("old", "1", 800), ("abc", "2", 800), ("abc", "1", 900)]:
with self.assertRaises(ValueError):
accessibility.resolve_target(self.saved, self.current, observation, target, width, 600)
with self.assertRaises(ValueError):
accessibility.resolve_target(self.saved, {"targets": []}, "abc", "1", 800, 600)
def test_moved_target(self):
moved = {**self.target, "bounds": [100, 100, 100, 40]}
with self.assertRaises(ValueError):
accessibility.resolve_target(self.saved, {"targets": [moved]}, "abc", "1", 800, 600)
def test_timeout_fallback(self):
with patch.object(subprocess, "run", side_effect=subprocess.TimeoutExpired("scan", 3)):
result = accessibility.collect(800, 600)
self.assertEqual(result["targets"], [])
self.assertIn("unavailable", result["warning"])
def test_target_must_be_first(self):
target = {"type": "click_target", "target": "1", "observationId": "abc"}
desktop.validate({"actions": [target]}, 800, 600)
with self.assertRaises(ValueError):
desktop.validate({"actions": [{"type": "keys", "keys": ["Return"]}, target]}, 800, 600)
if __name__ == "__main__":
unittest.main()