68 lines
3.9 KiB
Python
68 lines
3.9 KiB
Python
import re
|
|
from pathlib import Path
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
code=Path('dist/remote/BiImage.js').read_text()
|
|
|
|
def svg(w,h):
|
|
return f'<svg xmlns="http://www.w3.org/2000/svg" width="{w}" height="{h}" viewBox="0 0 {w} {h}"><rect width="100%" height="100%" fill="red"/></svg>'
|
|
|
|
with sync_playwright() as p:
|
|
browser=p.chromium.launch(headless=True,executable_path='/usr/bin/chromium',args=['--no-sandbox'])
|
|
page=browser.new_page(viewport={'width':600,'height':500})
|
|
urls=[]
|
|
def serve(route):
|
|
url=route.request.url
|
|
urls.append(url)
|
|
m=re.search(r'/rs:auto:(\d+):(\d+):0',url)
|
|
if m:
|
|
w,h=map(int,m.groups())
|
|
if not w: w=h*2
|
|
elif not h: h=w//2
|
|
else: w,h=400,200
|
|
route.fulfill(status=200,content_type='image/svg+xml',body=svg(w,h))
|
|
page.route('https://img.d.aiengines.ir/**',serve)
|
|
page.goto('about:blank')
|
|
page.add_script_tag(content=code)
|
|
# height fixed / width auto: image is rendered at 2:1 ratio, not cropped to a square.
|
|
page.evaluate('''() => {
|
|
let el=document.createElement('uikit-bi-image');
|
|
el.componentProps={src:'photos/landscape.png', height:60,width:'auto',alt:'test'};
|
|
document.body.appendChild(el);
|
|
window.el=el;
|
|
}''')
|
|
page.wait_for_function("window.el.image ? window.el.image.complete : window.el.querySelector('img').complete")
|
|
dims=page.evaluate("() => {const i=window.el.querySelector('img');return [i.getBoundingClientRect().width,i.getBoundingClientRect().height, i.currentSrc]}")
|
|
print('height-only',dims)
|
|
assert dims[0]==120 and dims[1]==60 and '/rs:auto:0:60:0/' in dims[2]
|
|
# width fixed / height auto
|
|
page.evaluate("() => {window.el.componentProps={src:'photos/landscape.png',width:80,height:'auto'};}")
|
|
page.wait_for_function("window.el.querySelector('img').complete && window.el.querySelector('img').naturalWidth===80")
|
|
dims=page.evaluate("() => {const i=window.el.querySelector('img');return [i.getBoundingClientRect().width,i.getBoundingClientRect().height,i.currentSrc]}")
|
|
print('width-only',dims)
|
|
assert dims[0]==80 and dims[1]==40 and '/rs:auto:80:0:0/' in dims[2]
|
|
# Responsive width, height follows intrinsic ratio.
|
|
page.evaluate('''() => {
|
|
const container=document.createElement('div');container.style.width='210px';
|
|
const e=document.createElement('uikit-bi-image');e.componentProps={src:'photos/landscape.png',style:{width:'100%',height:'auto'}};
|
|
container.appendChild(e);document.body.appendChild(container);
|
|
window.resp=e;window.wrapper=container;
|
|
}''')
|
|
page.wait_for_function("window.resp.querySelector('img').naturalWidth===210")
|
|
dims=page.evaluate("() => {const i=window.resp.querySelector('img');return [i.getBoundingClientRect().width,i.getBoundingClientRect().height,i.currentSrc]}")
|
|
print('responsive-initial',dims)
|
|
assert dims[0]==210 and dims[1]==105 and '/rs:auto:210:0:0/' in dims[2]
|
|
page.evaluate("window.wrapper.style.width='300px'")
|
|
page.wait_for_function("window.resp.querySelector('img').naturalWidth===300")
|
|
dims=page.evaluate("() => {const i=window.resp.querySelector('img');return [i.getBoundingClientRect().width,i.getBoundingClientRect().height,i.currentSrc]}")
|
|
print('responsive-resized',dims)
|
|
assert dims[0]==300 and dims[1]==150 and '/rs:auto:300:0:0/' in dims[2]
|
|
# Null src: gray box, no broken image icon.
|
|
page.evaluate("() => {window.el.componentProps={src:null,width:70,height:70}}")
|
|
gray=page.evaluate("() => {let i=window.el.querySelector('img');return [i.getAttribute('src'),getComputedStyle(i).backgroundColor,i.getBoundingClientRect().width]}")
|
|
print('gray',gray)
|
|
assert gray==[None,'rgb(229, 231, 235)',70]
|
|
assert len(urls)<12, f'unexpected repeated resizing: {urls}'
|
|
browser.close()
|
|
print('PASS: Chromium DOM sizing, aspect ratio, responsive observer, proxy URL and gray placeholder')
|