def _fetch_thumb_and_compare(self, url, expected_image): if url == missing_thumbnail_url: logger.error(f'It was not possible to fetch the remote layer WMS GetMap! thumb_url: {url}') return _, img = http_client.request(url) content = BytesIO(img) Image.open(content).verify() # verify that it is, in fact an image thumb = Image.open(content) diff = Image.new("RGB", thumb.size) mismatch = pixelmatch(thumb, expected_image, diff) if mismatch >= expected_image.size[0] * expected_image.size[1] * 0.01: logger.warn("Mismatch, it was not possible to bump the bg!") # Sometimes this test fails to fetch the OSM background with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.png', delete=False) as tmpfile: logger.error(f"Dumping thumb to: {tmpfile.name}") thumb.save(tmpfile) # Let's check that the thumb is valid at least with Image.open(tmpfile) as img: img.verify() with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.png', delete=False) as tmpfile: logger.error(f"Dumping diff to: {tmpfile.name}") diff.save(tmpfile) # Let's check that the thumb is valid at least with Image.open(tmpfile) as img: img.verify() else: self.assertTrue( mismatch < expected_image.size[0] * expected_image.size[1] * 0.01, "Expected test and pre-generated thumbnails to differ up to 1%", )
def test_wms_background_fetch_epsg4326(self): width = 240 height = 200 bbox = [-9072563.021775628, -9043899.136168687, 1492394.0457582686, 1507681.4514153039, "EPSG:3857"] try: image = GenericWMSBackground(thumbnail_width=width, thumbnail_height=height).fetch(bbox) except UnidentifiedImageError as e: logger.error(f"It was not possible to fetch the background: {e}") return expected_image = Image.open(f"{EXPECTED_RESULTS_DIR}background/wms_4326.png") diff = Image.new("RGB", image.size) mismatch = pixelmatch(image, expected_image, diff) if mismatch >= expected_image.size[0] * expected_image.size[1] * 0.01: logger.warn("Mismatch, it was not possible to bump the bg!") # Sometimes this test fails to fetch the OSM background with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.png', delete=False) as tmpfile: logger.error(f"Dumping image to: {tmpfile.name}") image.save(tmpfile) # Let's check that the thumb is valid at least with Image.open(tmpfile) as img: img.verify() with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.png', delete=False) as tmpfile: logger.error(f"Dumping diff to: {tmpfile.name}") diff.save(tmpfile) # Let's check that the thumb is valid at least with Image.open(tmpfile) as img: img.verify() else: self.assertTrue( mismatch < width * height * 0.01, "Expected test and pre-generated backgrounds to differ up to 1%" )
def test_tile_background_generic_fetch_zoom(self): width = 240 height = 200 bbox_3857 = [-8250483.072013094, -8221819.186406153, 4961221.562116772, 4985108.133455889, "EPSG:3857"] zooms = range(6, 13) expected_image_paths = [ f"{EXPECTED_RESULTS_DIR}background/wikimedia_zoom_{zoom}_outcome.png" for zoom in zooms ] background = GenericXYZBackground(thumbnail_width=width, thumbnail_height=height) for zoom, expected_image_path in zip(zooms, expected_image_paths): try: image = background.fetch(bbox_3857, zoom) expected_image = Image.open(expected_image_path) diff = Image.new("RGB", image.size) mismatch = pixelmatch(image, expected_image, diff) self.assertTrue( mismatch < width * height * 0.01, "Expected test and pre-generated backgrounds to differ up to 1%" ) except UnidentifiedImageError as e: logger.error(f"It was not possible to fetch the background: {e}")
def _fetch_and_compare_background(self, generator, bbox_3857, expected_image_path, zoom=None): try: image = generator.fetch(bbox_3857, zoom) except UnidentifiedImageError as e: logger.error(f"It was not possible to fetch the background: {e}") return expected_image = Image.open(expected_image_path) diff = Image.new("RGB", image.size) mismatch = pixelmatch(image, expected_image, diff) if mismatch >= expected_image.size[0] * expected_image.size[1] * 0.01: logger.warn("Mismatch, it was not possible to bump the bg!") # Sometimes this test fails to fetch the OSM background with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.png', delete=False) as tmpfile: logger.error(f"Dumping image to: {tmpfile.name}") image.save(tmpfile) # Let's check that the thumb is valid at least with Image.open(tmpfile) as img: img.verify() with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.png', delete=False) as tmpfile: logger.error(f"Dumping diff to: {tmpfile.name}") diff.save(tmpfile) # Let's check that the thumb is valid at least with Image.open(tmpfile) as img: img.verify() else: self.assertTrue( mismatch < expected_image.size[0] * expected_image.size[1] * 0.01, "Expected test and pre-generated backgrounds to differ up to 1%", )
def test_system_programs(self): test_cases = [ "Colourised", "DepthTesting", "BatcherDepth", "GeometryDepth", "RenderTarget", "ShaderUniforms", "ClearColour", "BatchingGeometry", "StencilTesting", "Text", "Sprites", "Transformations", "TransformationTree", "ImGuiDrawing", "Painting" ] myEnv = os.environ.copy() myEnv["PATH"] += ";../../;" for x in test_cases: with self.subTest(x, x=x): print(x) templateHandle = open("template.json", "r") template = templateHandle.read().replace("{TEST_CASE}", x) templateHandle.close() buildFileHandle = open("build.json", "w") buildFileHandle.write(template) buildFileHandle.close() subprocess.run([ "npx", "neko", "../../run.n", "build", "--release", "--verbose", "--gpu", "d3d11" ], shell=True, env=myEnv) test_proc = subprocess.Popen(["bin/windows/SystemTests.exe"]) time.sleep(3) win32gui.EnumWindows(enumHandler, f"screenshot_{x}.png") test_proc.terminate() test_proc.wait() img_a = Image.open(f"expected/{x}.png") img_b = Image.open(f"screenshot_{x}.png") img_c = Image.new("RGBA", img_a.size) idiff = (pixelmatch(img_a, img_b, img_c) / (img_a.width * img_a.height)) * 100 if idiff <= 5: os.remove(f"screenshot_{x}.png") else: os.rename(f"screenshot_{x}.png", f"screenshot_{x}_failed.png") img_c.save(f"diff_{x}.png") self.fail( f"expected image difference for {x} to be less than or equal to 5% but was {idiff}%" )
def comparison_thread(comp_list, i, ipfs, images, i1): for im in images: i2 = pillow_image.open(io.BytesIO(ipfs.cat(im.ipfsHash))) i2 = i2.resize(i1.size) logger.info(i1.size) logger.info(i2.size) mismatch = pixelmatch(i1, i2, None, includeAA=True) logger.info(mismatch) percentage = (prod(i1.size) - mismatch) * 100 / prod(i1.size) logger.info("percentage:\t" + str(percentage)) if percentage > SIMILARITY_THRESHOLD: comp_list.append({"ipfs": im.ipfsHash, "percentage": percentage})
def test_map_default_thumb(self): create_gs_thumbnail_geonode(self.map_composition, overwrite=True) if not self.map_composition.has_thumbnail(): logger.warn("It was not possible to dump the background!") logger.error( f"map_composition thumb: {self.map_composition.thumbnail_url}") else: _, img = http_client.request(self.map_composition.thumbnail_url) content = BytesIO(img) Image.open(content).verify() # verify that it is, in fact an image thumb = Image.open(content) diff = Image.new("RGB", thumb.size) expected_thumb = Image.open(EXPECTED_RESULTS_DIR + "thumbnails/default_map_thumb.png") mismatch = pixelmatch(thumb, expected_thumb, diff) if mismatch >= expected_thumb.size[0] * expected_thumb.size[ 1] * 0.01: logger.warn("Mismatch, it was not possible to bump the bg!") # Sometimes this test fails to fetch the OSM background with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.png', delete=False) as tmpfile: logger.error(f"Dumping thumb to: {tmpfile.name}") thumb.save(tmpfile) # Let's check that the thumb is valid at least with Image.open(tmpfile) as img: img.verify() with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.png', delete=False) as tmpfile: logger.error(f"Dumping diff to: {tmpfile.name}") diff.save(tmpfile) # Let's check that the thumb is valid at least with Image.open(tmpfile) as img: img.verify() else: self.assertTrue( mismatch < expected_thumb.size[0] * expected_thumb.size[1] * 0.01, "Expected test and pre-generated thumbnails to differ up to 1%", )
def test_system_programs(self): xvfb_proc = subprocess.Popen([ "Xvfb", ":99", "-screen", "0", "768x512x24", "-nolisten", "tcp", "-nolisten", "unix" ]) test_cases = [ "Colourised", "DepthTesting", "BatcherDepth", "GeometryDepth", "RenderTarget", "ShaderUniforms", "ClearColour", "BatchingGeometry", "StencilTesting", "Text", "Sprites", "Transformations", "TransformationTree", "ImGuiDrawing", "Painting" ] myEnv = os.environ.copy() myEnv["DISPLAY"] = ":99" for x in test_cases: with self.subTest(x, x=x): templateHandle = open("template.json", "r") template = templateHandle.read().replace("{TEST_CASE}", x) templateHandle.close() buildFileHandle = open("build.json", "w") buildFileHandle.write(template) buildFileHandle.close() subprocess.run([ "npx", "neko", "../../run.n", "build", "--release", "--verbose", "--gpu", "ogl3" ], env=myEnv) test_proc = subprocess.Popen(["bin/linux/SystemTests"], env=myEnv) time.sleep(3) subprocess.run([ "import", "-window", "System Tests", f"screenshot_{x}.png" ], env=myEnv) test_proc.terminate() test_proc.wait() img_a = Image.open(f"expected/{x}.png") img_b = Image.open(f"screenshot_{x}.png") img_c = Image.new("RGBA", img_a.size) idiff = (pixelmatch(img_a, img_b, img_c) / (img_a.width * img_a.height)) * 100 if idiff <= 5: os.remove(f"screenshot_{x}.png") else: os.rename(f"screenshot_{x}.png", f"screenshot_{x}_failed.png") img_c.save(f"diff_{x}.png") self.fail( f"expected image difference for {x} to be less than or equal to 5% but was {idiff}%" ) xvfb_proc.terminate() xvfb_proc.wait()