78 lines
7.5 KiB
Python
78 lines
7.5 KiB
Python
"""Native Chromium focus regression against actual production dialog-focus.js.
|
|
|
|
Uses a local synthetic document and recording receiver only; no application server,
|
|
network, database, operational service or package dependency is required.
|
|
"""
|
|
import argparse,json
|
|
from pathlib import Path
|
|
from playwright.sync_api import sync_playwright
|
|
parser=argparse.ArgumentParser();parser.add_argument('--output',type=Path,required=True);args=parser.parse_args()
|
|
source=Path(__file__).resolve().parents[3]/'src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/wwwroot/js/dialog-focus.js'
|
|
html='''<main><h1 tabindex="-1">Synthetic native focus fixture</h1><button id="trigger">Open synthetic dialog</button><button id="outside">Outside control</button></main>
|
|
<div id="modal" role="dialog" aria-modal="true" tabindex="-1"><button id="header">Close dialog</button><fieldset id="fields"><input id="input" aria-label="Synthetic name"><button id="save">Save</button><button id="cancel" data-initial-focus>Cancel</button></fieldset></div>'''
|
|
results=[]
|
|
with sync_playwright() as p:
|
|
browser=p.chromium.launch()
|
|
for case in ['busy-update','removed-control','document-key-gap','stale-generation','other-dialog-intent','idle-read','child-handled-escape','deactivated-cleanup','ancestor-shortcut']:
|
|
page=browser.new_page();errors=[];page.on('pageerror',lambda error:errors.append(str(error)));page.set_content(html);page.add_script_tag(content=source.read_text())
|
|
page.evaluate("window.calls=[];window.bodyEscapes=0;document.body.addEventListener('keydown',e=>{if(e.key==='Escape')bodyEscapes++;});window.leakedEscapes=0;window.addEventListener('keydown',e=>{if(e.key==='Escape')leakedEscapes++;});window.receiver={invokeMethodAsync:(name,generation)=>{calls.push({name,generation});return Promise.resolve();}};document.getElementById('trigger').focus();otDialog.activate('modal',receiver,1,false)")
|
|
checks=[]
|
|
def check(name,value):checks.append({'name':name,'passed':bool(value)})
|
|
within=lambda:page.locator('#modal').evaluate('el=>el.contains(document.activeElement)')
|
|
check('Safe initial Cancel focus',page.locator('#cancel').evaluate('n=>n===document.activeElement'))
|
|
if case in ['busy-update','document-key-gap']:
|
|
page.locator('#save').focus();page.evaluate("document.getElementById('fields').disabled=true;document.getElementById('header').disabled=true")
|
|
before=page.evaluate('document.activeElement.tagName')
|
|
page.evaluate("otDialog.update('modal',1,true)")
|
|
if case=='busy-update':check('Busy update reanchors native disabled-control focus',within())
|
|
else:page.evaluate('document.activeElement.blur()')
|
|
for _ in range(3):page.keyboard.press('Tab');check('Busy native Tab remains scoped with no usable controls',within())
|
|
page.keyboard.press('Shift+Tab');check('Busy reverse Tab remains scoped',within())
|
|
if case=='document-key-gap':page.evaluate('document.activeElement.blur()')
|
|
page.keyboard.press('Escape');check('Body-gap Escape precedes ancestor shortcuts',page.evaluate('bodyEscapes')==0);check('Busy Escape dispatches no close',page.evaluate('calls.length')==0);check('Busy Escape does not reach background shortcuts',page.evaluate('leakedEscapes')==0)
|
|
page.evaluate("document.getElementById('fields').disabled=false;document.getElementById('header').disabled=false;otDialog.update('modal',1,false)")
|
|
page.keyboard.press('Tab');check('Idle controls remain reachable after recovery',within())
|
|
page.keyboard.press('Escape');check('Idle Escape invokes current close once',page.evaluate('calls')==[{'name':'RequestCloseFromDom','generation':1}])
|
|
elif case=='removed-control':
|
|
page.locator('#save').focus();page.evaluate("document.getElementById('save').remove();otDialog.update('modal',1,false)")
|
|
check('Removal update restores safe current focus',page.locator('#cancel').evaluate('n=>n===document.activeElement'))
|
|
page.keyboard.press('Escape');check('Escape works after focused control removal',page.evaluate('calls.length')==1)
|
|
before='removed Save'
|
|
elif case=='stale-generation':
|
|
page.evaluate("otDialog.deactivate('modal',1);otDialog.activate('modal',receiver,2,false);document.getElementById('input').focus();otDialog.update('modal',1,true);otDialog.deactivate('modal',1)")
|
|
check('Stale update/deactivate cannot change current focus',page.locator('#input').evaluate('n=>n===document.activeElement'))
|
|
page.keyboard.press('Escape');check('Stale update cannot mark new dialog busy',page.evaluate('calls')==[{'name':'RequestCloseFromDom','generation':2}])
|
|
before='generation2'
|
|
elif case=='other-dialog-intent':
|
|
page.evaluate("const other=document.createElement('div');other.setAttribute('role','dialog');other.innerHTML='<button id=other>Other modal control</button>';document.body.append(other);document.getElementById('other').focus();otDialog.update('modal',1,true)")
|
|
check('Update does not steal deliberate other-dialog focus',page.locator('#other').evaluate('n=>n===document.activeElement'))
|
|
page.keyboard.press('Escape');check('Other dialog Escape is not intercepted',page.evaluate('calls.length')==0)
|
|
before='other dialog'
|
|
elif case=='child-handled-escape':
|
|
page.locator('#input').focus();page.locator('#input').evaluate("n=>n.addEventListener('keydown',e=>{if(e.key==='Escape')e.stopPropagation();})")
|
|
page.keyboard.press('Escape');check('Child widget retains handled Escape',page.evaluate('calls.length')==0 and page.evaluate('leakedEscapes')==0)
|
|
check('Child focus remains deliberate',page.locator('#input').evaluate('n=>n===document.activeElement'))
|
|
before='child handled Escape'
|
|
elif case=='ancestor-shortcut':
|
|
page.evaluate("window.ancestorEscapes=0;document.body.addEventListener('keydown',e=>{if(e.key==='Escape')ancestorEscapes++;})")
|
|
page.locator('#input').focus();page.keyboard.press('Escape')
|
|
check('Local Escape remains ahead of ancestor shortcuts',page.evaluate('ancestorEscapes')==0 and page.evaluate('calls.length')==1)
|
|
before='local ancestor shortcut'
|
|
elif case=='deactivated-cleanup':
|
|
page.evaluate("otDialog.deactivate('modal',1)");page.locator('#outside').focus();page.keyboard.press('Escape')
|
|
check('Deactivation removes document key interception',page.evaluate('calls.length')==0 and page.evaluate('leakedEscapes')==1)
|
|
check('Existing inert/body state is restored',page.evaluate("!document.querySelector('main').inert&&!document.body.classList.contains('modal-open')"))
|
|
before='deactivated'
|
|
else:
|
|
page.locator('#save').focus();page.locator('#save').evaluate("n=>n.setAttribute('aria-disabled','true')");page.evaluate("otDialog.update('modal',1,false)")
|
|
check('Read-only pending control retains focus',page.locator('#save').evaluate('n=>n===document.activeElement'))
|
|
page.evaluate('document.activeElement.blur()');page.keyboard.press('Escape');check('Dismissible read Escape survives body-focus gap',page.evaluate('calls')==[{'name':'RequestCloseFromDom','generation':1}])
|
|
check('Idle body-gap Escape precedes ancestor shortcuts',page.evaluate('bodyEscapes')==0)
|
|
before='aria-disabled read'
|
|
check('No page errors',not errors)
|
|
results.append({'case':case,'beforeUpdate':before,'checks':checks,'errors':errors,'calls':page.evaluate('calls'),'passed':all(c['passed']for c in checks)})
|
|
page.close()
|
|
browser.close()
|
|
args.output.parent.mkdir(parents=True,exist_ok=True);args.output.write_text(json.dumps({'boundary':'Production JS; real native DOM focus, inert, Tab/Shift+Tab/Escape; synthetic recording receiver; no network/host', 'scenes':results},indent=2)+'\n')
|
|
print(json.dumps({'scenes':len(results),'passed':sum(s['passed']for s in results),'checks':sum(len(s['checks'])for s in results)}));raise SystemExit(0 if all(s['passed']for s in results)else 1)
|