80 lines
4.0 KiB
Python
80 lines
4.0 KiB
Python
import shutil
|
|
import subprocess
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import Mock
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "ops"))
|
|
from owned_action_button import click_consecration, probe_commands, read_marker
|
|
|
|
|
|
class ButtonTest(unittest.TestCase):
|
|
command = dict(frame="ActionButton12", slot=12, spell_id=26573, label="g2-cast")
|
|
|
|
def run_click(self, status, geometry=(2560, 1440), command=None):
|
|
self.hover, self.click = Mock(), Mock()
|
|
reads = Mock(side_effect=([1140, 1400, 2560, 1440], status))
|
|
return click_consecration(command or self.command, geometry=lambda:geometry, inspect=reads,
|
|
hover=self.hover, click=self.click, clock=lambda:1002)
|
|
|
|
def test_confirmed_visible_rank_and_pointer(self):
|
|
for rank in (26573, 20923):
|
|
receipt = self.run_click([12,rank,1])
|
|
self.hover.assert_called_once_with(1140,1400)
|
|
self.click.assert_called_once_with(1140,1400)
|
|
self.assertEqual((receipt["spell_id"],receipt["resolved_spell_id"],receipt["input"],
|
|
receipt["click_monotonic_ns"]), (26573,rank,"left_click",1002))
|
|
|
|
def test_no_click_for_stale_frame_other_slot_rank_or_cooldown(self):
|
|
for status in ([11,20923,1],[12,123,1],[12,20923,0]):
|
|
with self.assertRaises(RuntimeError): self.run_click(status)
|
|
self.click.assert_not_called()
|
|
with self.assertRaises(RuntimeError): self.run_click([12,26573,1],(1920,1080))
|
|
self.click.assert_not_called()
|
|
with self.assertRaises(ValueError): self.run_click([12,20923,1],command=dict(self.command,slot=69))
|
|
self.click.assert_not_called()
|
|
|
|
@unittest.skipUnless(shutil.which("lua5.1"), "Lua 5.1")
|
|
def test_run_real_lua_queries_and_each_rejection(self):
|
|
pos, state = probe_commands("345678")
|
|
self.assertLessEqual(max(len(pos.encode()),len(state.encode())), 255)
|
|
bootstrap = """
|
|
local b={action=12,GetCenter=function() return 456,12 end, GetEffectiveScale=function()return 1 end}
|
|
ActionButton12=b
|
|
GetPhysicalScreenSize=function()return 2560,1440 end
|
|
format=string.format
|
|
UIErrorsFrame={AddMessage=function(_,s)print(s)end}
|
|
local hit, usable, start, duration, kind, id=true,true,0,0,'spell',20923
|
|
GetMouseFoci=function()return {hit and b or {}}end
|
|
IsUsableAction=function()return usable end
|
|
GetActionCooldown=function()return start,duration end
|
|
GetTime=function()return 32 end
|
|
GetActionInfo=function()return kind,id end
|
|
"""
|
|
lua = bootstrap + pos.removeprefix("/run ")+"\n"
|
|
for conditions in ("", "hit=false", "hit=true usable=false", "usable=true start=31 duration=8",
|
|
"start=0 duration=0 kind='item'"):
|
|
lua += conditions + "\ndo "+state.removeprefix("/run ")+" end\n"
|
|
result = subprocess.run(["lua5.1","-"], input=lua, capture_output=True, text=True, check=True)
|
|
self.assertEqual(result.stdout.splitlines(),["345678-855-1418-2560-1440","345678-12-20923-1",
|
|
"345678-12-20923-0","345678-12-20923-0","345678-12-20923-0","345678-12-0-1"])
|
|
|
|
@unittest.skipUnless(shutil.which("tesseract"), "tesseract")
|
|
def test_real_ocr_current_magenta_report_at_error_anchor(self):
|
|
image=Image.new("RGB",(1920,1080),(33,65,79))
|
|
draw=ImageDraw.Draw(image)
|
|
font=ImageFont.truetype("DejaVuSans.ttf",18)
|
|
draw.text((853,117),"345678-12-20923-1",fill=(251,0,251),font=font)
|
|
draw.text((853,141),"343434-14-11111-0",fill="yellow",font=font)
|
|
draw.text((51,912),"765432-12-20923-1",fill="magenta",font=font)
|
|
self.assertEqual(read_marker(image,"345678",3),[12,20923,1])
|
|
self.assertIsNone(read_marker(image,"765432",3))
|
|
self.assertIsNone(read_marker(image,"343434",3))
|
|
self.assertIsNone(read_marker(image,"345678",4))
|
|
|
|
|
|
if __name__=="__main__":unittest.main()
|