"""Screenshot pixels for wait_for_change: a channel Chrome rarely emits.""" import struct import sys import unittest import zlib from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "python")) from albedo_plugins.browser import pixels # noqa: E402 def png(colour, rows): def chunk(kind, data): body = kind + data return struct.pack(">I", len(data)) + body + struct.pack(">I", zlib.crc32(body)) header = struct.pack(">IIBBBBB", len(rows[0]) // 2, len(rows), 8, colour, 0, 0, 0) data = b"".join(b"\x00" + bytes(row) for row in rows) return ( b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IDAT", zlib.compress(data)) + chunk(b"IEND", b"") ) class PixelsTest(unittest.TestCase): def test_gray_alpha_blocks_compare_gray_only(self): # One gray level under two alphas: the same colour, seen twice. opaque = png(4, [[90, 255] * 4] * 4) clear = png(4, [[90, 0] * 4] * 4) self.assertEqual(pixels.blocks(opaque), [(90.0,)]) self.assertEqual(pixels.blocks(opaque), pixels.blocks(clear)) if __name__ == "__main__": unittest.main()