def testScreenshot(self): if not self._tab.screenshot_supported: logging.warning( 'Browser does not support screenshots, skipping test.') return self.Navigate('green_rect.html') pixel_ratio = self._tab.EvaluateJavaScript( 'window.devicePixelRatio || 1') screenshot = self._tab.Screenshot(5) assert screenshot is not None image_util.GetPixelColor(screenshot, 0 * pixel_ratio, 0 * pixel_ratio).AssertIsRGB(0, 255, 0, tolerance=2) image_util.GetPixelColor(screenshot, 31 * pixel_ratio, 31 * pixel_ratio).AssertIsRGB(0, 255, 0, tolerance=2) image_util.GetPixelColor(screenshot, 32 * pixel_ratio, 32 * pixel_ratio).AssertIsRGB(255, 255, 255, tolerance=2)
def testReadFromBase64Png(self): bmp = image_util.FromBase64Png(test_png) self.assertEquals(2, image_util.Width(bmp)) self.assertEquals(2, image_util.Height(bmp)) image_util.GetPixelColor(bmp, 0, 0).AssertIsRGB(255, 0, 0) image_util.GetPixelColor(bmp, 1, 1).AssertIsRGB(0, 255, 0) image_util.GetPixelColor(bmp, 0, 1).AssertIsRGB(0, 0, 255) image_util.GetPixelColor(bmp, 1, 0).AssertIsRGB(255, 255, 0)
def testReadFromPngFile(self): file_bmp = image_util.FromPngFile(test_png_path) self.assertEquals(2, image_util.Width(file_bmp)) self.assertEquals(2, image_util.Height(file_bmp)) image_util.GetPixelColor(file_bmp, 0, 0).AssertIsRGB(255, 0, 0) image_util.GetPixelColor(file_bmp, 1, 1).AssertIsRGB(0, 255, 0) image_util.GetPixelColor(file_bmp, 0, 1).AssertIsRGB(0, 0, 255) image_util.GetPixelColor(file_bmp, 1, 0).AssertIsRGB(255, 255, 0)
def testCrop(self): pixels = [0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 0, 1, 0, 1, 1, 0, 2, 1, 0, 3, 1, 0, 0, 2, 0, 1, 2, 0, 2, 2, 0, 3, 2, 0] bmp = image_util.FromRGBPixels(4, 3, pixels) bmp = image_util.Crop(bmp, 1, 2, 2, 1) self.assertEquals(2, image_util.Width(bmp)) self.assertEquals(1, image_util.Height(bmp)) image_util.GetPixelColor(bmp, 0, 0).AssertIsRGB(1, 2, 0) image_util.GetPixelColor(bmp, 1, 0).AssertIsRGB(2, 2, 0) self.assertEquals(image_util.Pixels(bmp), bytearray([1, 2, 0, 2, 2, 0]))
def _CompareScreenshotWithExpectation(expectation): """Compares a portion of the screenshot to the given expectation. Fails the test if a the screenshot does not match within the tolerance. Args: expectation: A dict defining an expected color region. It must contain 'location', 'size', and 'color' keys. See pixel_test_pages.py for examples. """ location = expectation["location"] size = expectation["size"] x0 = int(location[0] * device_pixel_ratio) x1 = int((location[0] + size[0]) * device_pixel_ratio) y0 = int(location[1] * device_pixel_ratio) y1 = int((location[1] + size[1]) * device_pixel_ratio) for x in range(x0, x1): for y in range(y0, y1): if (x < 0 or y < 0 or x >= image_util.Width(screenshot) or y >= image_util.Height(screenshot)): self.fail(('Expected pixel location [%d, %d] is out of range on ' + '[%d, %d] image') % (x, y, image_util.Width(screenshot), image_util.Height(screenshot))) actual_color = image_util.GetPixelColor(screenshot, x, y) expected_color = rgba_color.RgbaColor( expectation["color"][0], expectation["color"][1], expectation["color"][2], expectation["color"][3] if len(expectation["color"]) > 3 else 255) if not actual_color.IsEqual(expected_color, tolerance): self.fail('Expected pixel at ' + str(location) + ' (actual pixel (' + str(x) + ', ' + str(y) + ')) ' + ' to be ' + str(expectation["color"]) + " but got [" + str(actual_color.r) + ", " + str(actual_color.g) + ", " + str(actual_color.b) + ", " + str(actual_color.a) + "]")
def _CompareScreenshotSamples(screenshot, expectations, device_pixel_ratio): for expectation in expectations: location = expectation["location"] size = expectation["size"] x0 = int(location[0] * device_pixel_ratio) x1 = int((location[0] + size[0]) * device_pixel_ratio) y0 = int(location[1] * device_pixel_ratio) y1 = int((location[1] + size[1]) * device_pixel_ratio) for x in range(x0, x1): for y in range(y0, y1): if (x < 0 or y < 0 or x >= image_util.Width(screenshot) or y >= image_util.Height(screenshot)): raise page_test.Failure( ('Expected pixel location [%d, %d] is out of range on ' + '[%d, %d] image') % (x, y, image_util.Width(screenshot), image_util.Height(screenshot))) actual_color = image_util.GetPixelColor(screenshot, x, y) expected_color = rgba_color.RgbaColor(expectation["color"][0], expectation["color"][1], expectation["color"][2]) if not actual_color.IsEqual(expected_color, expectation["tolerance"]): raise page_test.Failure('Expected pixel at ' + str(location) + ' to be ' + str(expectation["color"]) + " but got [" + str(actual_color.r) + ", " + str(actual_color.g) + ", " + str(actual_color.b) + "]")
def CheckColorMatchAtLocation(expectedRGB, screenshot, x, y): pixel_value = image_util.GetPixelColor(screenshot, x, y) if not expectedRGB.IsEqual(pixel_value): error_message = ('Color mismatch at (%d, %d): expected (%d, %d, %d), ' + 'got (%d, %d, %d)') % ( x, y, expectedRGB.r, expectedRGB.g, expectedRGB.b, pixel_value.r, pixel_value.g, pixel_value.b) raise page_test.Failure(error_message)
def _CheckColorMatchAtLocation(self, expectedRGB, screenshot, x, y): pixel_value = image_util.GetPixelColor(screenshot, x, y) # Allow for off-by-one errors due to color conversion. tolerance = 1 if not expectedRGB.IsEqual(pixel_value, tolerance): error_message = ('Color mismatch at (%d, %d): expected (%d, %d, %d), ' + 'got (%d, %d, %d)') % ( x, y, expectedRGB.r, expectedRGB.g, expectedRGB.b, pixel_value.r, pixel_value.g, pixel_value.b) self.fail(error_message)
def _CheckColorMatchAtLocation(self, expectedRGB, screenshot, x, y): pixel_value = image_util.GetPixelColor(screenshot, x, y) # Allow for off-by-one errors due to color conversion. tolerance = 1 # Pixel 4 devices require a slightly higher tolerance. See # crbug.com/1166379. if self.tab.browser.platform.GetDeviceTypeName() == 'Pixel 4': tolerance = 5 if not expectedRGB.IsEqual(pixel_value, tolerance): error_message = ('Color mismatch at (%d, %d): expected (%d, %d, %d), ' + 'got (%d, %d, %d)') % ( x, y, expectedRGB.r, expectedRGB.g, expectedRGB.b, pixel_value.r, pixel_value.g, pixel_value.b) self.fail(error_message)
def _CompareScreenshotSamples(self, tab, screenshot, expected_colors, tolerance, device_pixel_ratio, test_machine_name): # First scan through the expected_colors and see if there are any scale # factor overrides that would preempt the device pixel ratio. This # is mainly a workaround for complex tests like the Maps test. for expectation in expected_colors: if 'scale_factor_overrides' in expectation: for override in expectation['scale_factor_overrides']: # Require exact matches to avoid confusion, because some # machine models and names might be subsets of others # (e.g. Nexus 5 vs Nexus 5X). if ('device_type' in override and (tab.browser.platform.GetDeviceTypeName() == override['device_type'])): logging.warning('Overriding device_pixel_ratio ' + str(device_pixel_ratio) + ' with scale factor ' + str(override['scale_factor']) + ' for device type ' + override['device_type']) device_pixel_ratio = override['scale_factor'] break if (test_machine_name and 'machine_name' in override and override["machine_name"] == test_machine_name): logging.warning('Overriding device_pixel_ratio ' + str(device_pixel_ratio) + ' with scale factor ' + str(override['scale_factor']) + ' for machine name ' + test_machine_name) device_pixel_ratio = override['scale_factor'] break # Only support one "scale_factor_overrides" in the expectation format. break for expectation in expected_colors: if "scale_factor_overrides" in expectation: continue location = expectation["location"] size = expectation["size"] x0 = int(location[0] * device_pixel_ratio) x1 = int((location[0] + size[0]) * device_pixel_ratio) y0 = int(location[1] * device_pixel_ratio) y1 = int((location[1] + size[1]) * device_pixel_ratio) for x in range(x0, x1): for y in range(y0, y1): if (x < 0 or y < 0 or x >= image_util.Width(screenshot) or y >= image_util.Height(screenshot)): self.fail(( 'Expected pixel location [%d, %d] is out of range on ' + '[%d, %d] image') % (x, y, image_util.Width(screenshot), image_util.Height(screenshot))) actual_color = image_util.GetPixelColor(screenshot, x, y) expected_color = rgba_color.RgbaColor( expectation["color"][0], expectation["color"][1], expectation["color"][2]) if not actual_color.IsEqual(expected_color, tolerance): self.fail('Expected pixel at ' + str(location) + ' (actual pixel (' + str(x) + ', ' + str(y) + ')) ' + ' to be ' + str(expectation["color"]) + " but got [" + str(actual_color.r) + ", " + str(actual_color.g) + ", " + str(actual_color.b) + "]")
def testDiff(self): file_bmp = image_util.FromPngFile(test_png_path) file_bmp_2 = image_util.FromPngFile(test_png_2_path) diff_bmp = image_util.Diff(file_bmp, file_bmp) self.assertEquals(2, image_util.Width(diff_bmp)) self.assertEquals(2, image_util.Height(diff_bmp)) image_util.GetPixelColor(diff_bmp, 0, 0).AssertIsRGB(0, 0, 0) image_util.GetPixelColor(diff_bmp, 1, 1).AssertIsRGB(0, 0, 0) image_util.GetPixelColor(diff_bmp, 0, 1).AssertIsRGB(0, 0, 0) image_util.GetPixelColor(diff_bmp, 1, 0).AssertIsRGB(0, 0, 0) diff_bmp = image_util.Diff(file_bmp, file_bmp_2) self.assertEquals(3, image_util.Width(diff_bmp)) self.assertEquals(3, image_util.Height(diff_bmp)) image_util.GetPixelColor(diff_bmp, 0, 0).AssertIsRGB(0, 255, 255) image_util.GetPixelColor(diff_bmp, 1, 1).AssertIsRGB(255, 0, 255) image_util.GetPixelColor(diff_bmp, 0, 1).AssertIsRGB(255, 255, 0) image_util.GetPixelColor(diff_bmp, 1, 0).AssertIsRGB(0, 0, 255) image_util.GetPixelColor(diff_bmp, 0, 2).AssertIsRGB(255, 255, 255) image_util.GetPixelColor(diff_bmp, 1, 2).AssertIsRGB(255, 255, 255) image_util.GetPixelColor(diff_bmp, 2, 0).AssertIsRGB(255, 255, 255) image_util.GetPixelColor(diff_bmp, 2, 1).AssertIsRGB(255, 255, 255) image_util.GetPixelColor(diff_bmp, 2, 2).AssertIsRGB(255, 255, 255)
def ColumnIsWhite(column): for row in xrange(img_height): pixel = image_util.GetPixelColor(screenshot, column, row) if pixel.r != 255 or pixel.g != 255 or pixel.b != 255: return False return True
def RowIsWhite(row): for col in xrange(img_width): pixel = image_util.GetPixelColor(screenshot, col, row) if pixel.r != 255 or pixel.g != 255 or pixel.b != 255: return False return True