Files

46 lines
3.0 KiB
Python

import json,time
from pathlib import Path
import numpy as np
import pyautogui as pg,win32clipboard
from pywinauto import Desktop
R=Path(__file__).resolve().parent
W=4262948
w=Desktop(backend='win32').window(handle=W);w.set_focus()
canvas=next(c.rectangle() for c in w.children() if c.rectangle().width()==720 and c.rectangle().height()==408)
ROIS=[(185,275,445,313),(185,328,445,364),(472,349,705,394)]
checks=[]
def execute(code):
win32clipboard.OpenClipboard();win32clipboard.EmptyClipboard();win32clipboard.SetClipboardText(code,win32clipboard.CF_UNICODETEXT);win32clipboard.CloseClipboard()
r=w.rectangle();pg.click(r.left+180,r.top+915);pg.hotkey('ctrl','a');pg.hotkey('ctrl','v')
w.child_window(title='执行所有代码').click();time.sleep(.3)
def check(name,colors):
shot=pg.screenshot(region=(canvas.left,canvas.top,720,408));shot.save(str(R/f'sim-{name}.png'))
pixels=np.asarray(shot)
counts=[]
for (x1,y1,x2,y2),color in zip(ROIS,colors):
a=pixels[y1:y2,x1:x2,:3].astype(np.int16);r,g,b=a[:,:,0],a[:,:,1],a[:,:,2]
c=[int(((r>75)&(r<180)&(abs(r-g)<8)&(abs(r-b)<8)).sum()),int(((g>180)&(r<80)&(b<80)).sum()),int(((r>180)&(g<80)&(b<80)).sum())]
assert c[color]>20 and (color==1 or c[1]==0) and (color==2 or c[2]==0),(name,colors,c)
counts.append(c)
checks.append({'name':name,'colors':colors,'pixel_counts':counts,'passed':True,'screenshot':f'sim-{name}.png'})
print(name,'passed',flush=True)
check('boot-unknown',[0,0,0])
for task in range(7):
execute(f'current_task={task}\nlock_state=1\nebs_state=1')
check(f'task-{task}-green',[2 if task==0 else 1,1,1])
execute('current_task=2\nlock_state=2\nebs_state=2');check('unlocked-ebs-fault',[1,2,2])
execute('current_task=7\nlock_state=0\nebs_state=0');check('feedback-invalid',[0,0,0])
execute('current_task=2\nlock_state=1\nebs_state=1');check('request-baseline',[1,1,1])
execute('click bt4,1\nclick bt4,0');time.sleep(.2)
pg.screenshot(region=(canvas.left,canvas.top,720,408)).save(str(R/'sim-confirm-EBS-request.png'))
execute('click b0,1\nclick b0,0');check('after-confirm-request-still-VCU-task2',[1,1,1])
execute('get current_task');time.sleep(.2)
pg.screenshot().save(str(R/'sim-current-task-query.png'))
returned=[{'text':c.window_text(),'type':c.element_info.control_type} for c in Desktop(backend='uia').window(handle=W).descendants() if c.element_info.control_type in ('Text','DataItem','Edit')]
(R/'sim-returned-data.json').write_text(json.dumps(returned,ensure_ascii=False,indent=2),encoding='utf-8')
execute('page debug\ncurrent_task=6\nlock_state=2\nebs_state=2\npage dash');check('feedback-across-page',[1,2,2])
execute('click bt0,1\nclick bt0,0');execute('click b1,1\nclick b1,0');check('cancel-still-VCU-task6',[1,2,2])
execute('current_task=7\nlock_state=0\nebs_state=0');check('final-unknown',[0,0,0])
(R/'simulator-results.json').write_text(json.dumps({'scope':'Keyboard feedback simulation and simulated touch, not physical CAN/UART proof','checks':checks},ensure_ascii=False,indent=2),encoding='utf-8')
print(len(checks),'simulator checks passed')