#!/usr/bin/env python3 """Capture caret screenshots for crbug.com/353556446. Renders sampler.html, whose two inputs carry the same caret in CSS px but are scaled by transform: scale(0.1) and scale(10). The caret is painted red, so the capture can wait for a blink phase where it is visible and report how many device pixels wide it ended up. The CL is behind a kill switch, so both runs use the same binary: python3 capture_screenshots.py after python3 capture_screenshots.py before # adds --disable-blink-features """ import base64,json,subprocess,sys,time,urllib.request,websocket,functools,http.server,threading,os PORT=8792 handler=functools.partial(http.server.SimpleHTTPRequestHandler, directory=os.path.dirname(os.path.abspath(__file__))) srv=http.server.ThreadingHTTPServer(("127.0.0.1",PORT),handler) threading.Thread(target=srv.serve_forever,daemon=True).start() label=sys.argv[1] flags=[] if label=='after' else ['--disable-blink-features=CaretWidthIgnoresTransform'] port=9570 if label=='after' else 9571 p=subprocess.Popen([os.path.expanduser('~/chromium_2/src/out/Default/content_shell'), f'--remote-debugging-port={port}','--remote-allow-origins=*','--no-sandbox','--hide-scrollbars', '--force-device-scale-factor=1','about:blank']+flags,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) def dt(ws,c,m,params=None): c[0]+=1;msg={"id":c[0],"method":m} if params: msg["params"]=params ws.send(json.dumps(msg)) while True: r=json.loads(ws.recv()) if r.get("id")==c[0]: return r tabs=None for _ in range(60): try: tabs=json.loads(urllib.request.urlopen(f"http://127.0.0.1:{port}/json").read()) if [t for t in tabs if t['type']=='page']: break except Exception: time.sleep(0.5) ws=websocket.create_connection([t for t in tabs if t['type']=='page'][0]['webSocketDebuggerUrl']);c=[0] dt(ws,c,"Emulation.setFocusEmulationEnabled",{"enabled":True}) dt(ws,c,"Emulation.setDeviceMetricsOverride",{"width":340,"height":160,"deviceScaleFactor":1,"mobile":False}) dt(ws,c,"Page.navigate",{"url":f"http://127.0.0.1:{PORT}/sampler.html"}); time.sleep(1.5) def shoot(tag): os.makedirs(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'rendered'), exist_ok=True) for attempt in range(60): shot=dt(ws,c,"Page.captureScreenshot",{"format":"png","clip":{"x":0,"y":0,"width":340,"height":160,"scale":1}}) data=base64.b64decode(shot["result"]["data"]) path=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'rendered', f'{label}-{tag}.png') open(path,'wb').write(data) from PIL import Image im=Image.open(path).convert('RGB'); px=im.load(); w,h=im.size red=[(x,y) for y in range(h) for x in range(w) if px[x,y][0]-px[x,y][1]>40] if red: xs=sorted({x for x,_ in red}) print(label, tag, 'caret px columns:', len(xs), 'width-span:', xs[-1]-xs[0]+1, 'rows:', len({y for _,y in red})) return time.sleep(0.1) print(label, tag, 'caret not visible in any sample') shoot('down') dt(ws,c,"Runtime.evaluate",{"expression":"focusUp()"}); time.sleep(1.0) shoot('up') p.terminate() # When both runs exist, write the diff masks. HERE = os.path.dirname(os.path.abspath(__file__)) try: from PIL import Image, ImageChops for tag in ('down', 'up'): paths = [os.path.join(HERE, 'rendered', f'{l}-{tag}.png') for l in ('before', 'after')] if not all(os.path.exists(path) for path in paths): continue a = Image.open(paths[0]).convert('RGBA') b = Image.open(paths[1]).convert('RGBA') mask = ImageChops.difference(a, b).convert('L').point( lambda v: 255 if v > 24 else 0) out = Image.new('RGBA', a.size, (0, 0, 0, 0)) out.paste(Image.new('RGBA', a.size, (255, 0, 255, 255)), mask=mask) os.makedirs(os.path.join(HERE, 'diff'), exist_ok=True) out.save(os.path.join(HERE, 'diff', f'{tag}.png')) print(' diff', tag, 'changed_px=', sum(1 for v in mask.getdata() if v)) except ImportError: pass