Files
lmxopcua/infra/tools/ui-consistency/check_native_dialog.py
T

29 lines
5.9 KiB
Python

"""Focused native adapter regressions; detached DOM fixtures, no application services."""
from pathlib import Path
import json
from playwright.sync_api import sync_playwright
source=Path('src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/wwwroot/js/dialog-focus.js').read_text();out=[]
with sync_playwright() as p:
b=p.chromium.launch(channel='chromium')
for state in ['normal','disabled','hidden','unfocusable','disconnected','existing-heading-tabindex']:
page=b.new_page();page.set_content('<main><h1>Stable title</h1><button id="trigger">Open</button><button id="next">Next</button></main><section id="dialog" tabindex="-1"><button data-initial-focus>Cancel</button><button>Delete</button></section>');page.add_script_tag(content=source)
if state=='unfocusable':page.evaluate('()=>{const e=document.createElement("span");e.id="trigger";e.tabIndex=0;document.querySelector("#trigger").replaceWith(e)}')
if state=='existing-heading-tabindex':page.locator('h1').evaluate('e=>e.setAttribute("tabindex","2")')
page.locator('#trigger').focus();page.evaluate('window.receiver={invokeMethodAsync:()=>Promise.resolve()};otDialog.activate("dialog",receiver,1,false)')
safe=page.locator('[data-initial-focus]').evaluate('e=>e===document.activeElement')
for _ in range(5):page.keyboard.press('Tab')
contained=page.locator('#dialog').evaluate('e=>e.contains(document.activeElement)')
if state in ['disabled','hidden']:page.locator('#trigger').evaluate('(e,s)=>e[s]=true',state)
if state=='unfocusable':page.locator('#trigger').evaluate('e=>e.removeAttribute("tabindex")')
if state in ['disconnected','existing-heading-tabindex']:page.locator('#trigger').evaluate('e=>e.remove()')
page.locator('#dialog').evaluate('e=>e.remove()');page.wait_for_function('!document.body.classList.contains("modal-open")')
record=page.evaluate('({trigger:document.activeElement===document.querySelector("#trigger"),heading:document.activeElement===document.querySelector("h1"),inert:!!document.querySelector("[inert]"),bodyLocked:document.body.classList.contains("modal-open")})');page.locator('#next').focus();record['headingTabindexAfterBlur']=page.locator('h1').get_attribute('tabindex');record.update(state=state,initialSafe=safe,nativeTabContained=contained);record['passed']=safe and contained and not record['inert'] and not record['bodyLocked'] and (record['trigger'] if state=='normal' else record['heading']) and record['headingTabindexAfterBlur']==('2' if state=='existing-heading-tabindex' else None);out.append(record);page.close()
page=b.new_page();page.set_content('<main><h1>Stable title</h1><button id="trigger">Open</button></main><section id="one" tabindex="-1"><button>Cancel</button></section><section id="two" tabindex="-1"><button>Cancel</button></section>');page.add_script_tag(content=source);page.locator('#trigger').focus();page.evaluate('window.calls=0;window.receiver={invokeMethodAsync:()=>{calls++;return Promise.resolve()}}');record=page.evaluate('({first:otDialog.activate("one",receiver,1,true),second:otDialog.activate("two",receiver,1,false)})');page.keyboard.press('Escape');record['busyCalls']=page.evaluate('calls');page.evaluate('otDialog.update("one",1,false)');page.keyboard.press('Escape');page.wait_for_function('calls===1');record['readyCalls']=page.evaluate('calls');page.evaluate('otDialog.deactivate("two")');record['firstStillLocked']=page.evaluate('document.body.classList.contains("modal-open")');page.evaluate('otDialog.deactivate("one")');record['restored']=page.evaluate('!document.querySelector("[inert]") && document.activeElement===document.querySelector("#trigger")');record['state']='single-active-and-busy';record['passed']=record['first'] and not record['second'] and record['busyCalls']==0 and record['readyCalls']==1 and record['firstStillLocked'] and record['restored'];out.append(record);page.close()
for mode in ['h2','landmark']:
page=b.new_page();heading='<h2>Stable page section</h2>' if mode=='h2' else ''
page.set_content('<main>'+heading+'<button id="trigger">Open</button><button id="next">Next</button></main><section id="dialog" role="dialog" tabindex="-1"><h2>Closing dialog title</h2><button>Cancel</button></section>');page.add_script_tag(content=source);page.locator('#trigger').focus();page.evaluate('window.receiver={invokeMethodAsync:()=>Promise.resolve()};otDialog.activate("dialog",receiver,1,false)');page.locator('#trigger').evaluate('e=>e.disabled=true');page.evaluate('otDialog.deactivate("dialog")');record=page.evaluate('({tag:document.activeElement.tagName,insideClosingDialog:!!document.activeElement.closest("[role=dialog]"),text:document.activeElement.textContent})');record.update(state='closing-heading-'+mode);record['passed']=not record['insideClosingDialog'] and record['tag']==('H2' if mode=='h2' else 'MAIN');out.append(record);page.close()
page=b.new_page();page.set_content('<main><h1>Stable page</h1><button id="trigger">Open</button></main><aside inert id="prior">Already unavailable</aside><section id="dialog" tabindex="-1"><button>Cancel</button></section>');page.add_script_tag(content=source);page.evaluate('document.body.classList.add("modal-open");window.receiver={invokeMethodAsync:()=>Promise.resolve()}');page.locator('#trigger').focus();page.evaluate('otDialog.activate("dialog",receiver,1,false);otDialog.deactivate("dialog")');record=page.evaluate('({priorInert:document.querySelector("#prior").inert,mainInert:document.querySelector("main").inert,priorModalOpen:document.body.classList.contains("modal-open"),trigger:document.activeElement===document.querySelector("#trigger")})');record.update(state='restore-existing-inert-body');record['passed']=record['priorInert'] and not record['mainInert'] and record['priorModalOpen'] and record['trigger'];out.append(record);page.close();b.close()
Path('docs/ui-consistency/evidence/ot01-03/native-dialog.json').write_text(json.dumps(out,indent=2));print({'cases':len(out),'passed':sum(r['passed'] for r in out)})
raise SystemExit(0 if all(r["passed"] for r in out) else 1)