dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / tests / web_test / fuzzy.lucb
#==============================================================================================
#
# fuzzy - Ladybird's fuzzy matching of reftest screenshots
#
# DESCRIPTION:
# Port of Tests/LibWeb/test-web/Fuzzy.cpp (lines 1-123) and Fuzzy.h: a reftest may carry
# `<meta name="fuzzy" content="[reference:]maxDifference=X-Y;totalPixels=X-Y">`
# (https://web-platform-tests.org/writing-tests/reftests.html#fuzzy-matching). Identical
# bitmaps match; otherwise the first fuzzy configuration whose reference is the one
# compared (or that names none) must contain both the largest channel difference and the
# number of differing pixels.
#
# Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
# Copyright (c) 2026 the luce-browser authors.
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## A fuzzy configuration that is not well formed (Error::from_string_view in Fuzzy.cpp).
let fuzzy_invalid: ErrorCode = ErrorCode.package(201)
## TestWeb::FuzzyRange: an inclusive range of a statistic.
struct FuzzyRange:
var minimum_value: u64
var maximum_value: u64
## FuzzyRange::contains.
func contains(value: u64) -> bool:
return value >= self.minimum_value and value <= self.maximum_value
## TestWeb::FuzzyMatch: one `<meta name=fuzzy>` configuration.
struct FuzzyMatch:
var reference: web_url.Url?
var color_value_error: FuzzyRange
var pixel_error_count: FuzzyRange
## AK::String::split(separator): the parts between the separators, empty ones left out
## (SplitBehavior::Nothing), as views of `text` in an allocated span that is not given back
## (the runner's allocations live as long as the process or its VM, DESIGN.md ยง3.3).
func split_skipping_empty(text: str, separator: u8) -> str[]!:
let all = try strings.split(text, separator)
var count: usize = 0
for part in all:
if part.length > 0:
all[count] = part
count += 1
return all[..<count]
## AK::String::to_number<u64>: a decimal number, with whitespace around it allowed
## (TrimWhitespace::Yes).
func parse_number_u64(text: str) -> u64?:
return strings.parse_u64(strings.trim(text))
## Ported from Tests/LibWeb/test-web/Fuzzy.cpp:17 TestWeb::fuzzy_screenshot_match.
# https://web-platform-tests.org/writing-tests/reftests.html#fuzzy-matching
func fuzzy_screenshot_match(test_url: str, reference: const web_url.Url*, bitmap_a: const gfx.Bitmap*, bitmap_b: const gfx.Bitmap*,
fuzzy_matches: const FuzzyMatch[], should_match: bool) -> bool:
if gfx.bitmap_width(bitmap_a) != gfx.bitmap_width(bitmap_b) or gfx.bitmap_height(bitmap_a) != gfx.bitmap_height(bitmap_b):
return false
# If the bitmaps are identical, we don't perform fuzzy matching.
let diff = gfx.bitmap_diff(bitmap_a, bitmap_b)
if diff.identical:
return true
# Find a single fuzzy config to apply.
var fuzzy_match: const FuzzyMatch*? = none
for candidate in &fuzzy_matches:
if let candidate_reference = candidate.reference:
if web_url.url_equals(&candidate_reference, *reference):
fuzzy_match = candidate
break
else:
fuzzy_match = candidate
break
if fuzzy_match == none:
if should_match:
io.stderr().write(f"{test_url}: Screenshot mismatch: pixel error count {diff.pixel_error_count}, with maximum error {diff.maximum_error}. (No fuzzy config defined)\n") catch failure: return false
return false
let match_ = fuzzy_match else trap("unreachable")
# Apply fuzzy matching.
let color_error_matches = match_.color_value_error.contains((u64)diff.maximum_error)
if not color_error_matches and should_match:
io.stderr().write(f"{test_url}: Fuzzy mismatch: maximum error {diff.maximum_error} is outside {match_.color_value_error.minimum_value}-{match_.color_value_error.maximum_value}\n") catch failure: return false
let pixel_error_matches = match_.pixel_error_count.contains(diff.pixel_error_count)
if not pixel_error_matches and should_match:
io.stderr().write(f"{test_url}: Fuzzy mismatch: pixel error count {diff.pixel_error_count} is outside {match_.pixel_error_count.minimum_value}-{match_.pixel_error_count.maximum_value}\n") catch failure: return false
return color_error_matches and pixel_error_matches
## Ported from Tests/LibWeb/test-web/Fuzzy.cpp:62 TestWeb::parse_fuzzy_range.
# https://web-platform-tests.org/writing-tests/reftests.html#fuzzy-matching
func parse_fuzzy_range(fuzzy_range: str) -> FuzzyRange!:
let range_parts = try split_skipping_empty(fuzzy_range, '-')
if range_parts.length == 0 or range_parts.length > 2:
error(fuzzy_invalid, "Invalid fuzzy range format")
let minimum_value = parse_number_u64(range_parts[0]) else error(fuzzy_invalid, "Fuzzy range value is not a valid integer")
var maximum_value = minimum_value
if range_parts.length == 2:
maximum_value = parse_number_u64(range_parts[1]) else error(fuzzy_invalid, "Fuzzy range value is not a valid integer")
if minimum_value > maximum_value:
error(fuzzy_invalid, "Fuzzy range minimum is higher than its maximum")
return FuzzyRange(minimum_value = minimum_value, maximum_value = maximum_value)
## Ported from Tests/LibWeb/test-web/Fuzzy.cpp:84 TestWeb::parse_fuzzy_match.
# https://web-platform-tests.org/writing-tests/reftests.html#fuzzy-matching
func parse_fuzzy_match(reference: web_url.Url?, content: str) -> FuzzyMatch!:
# Match configuration values. Two formats are supported:
# * Named: "maxDifference=(#X-)#Y;totalPixels=(#X-)#Y"
# * Unnamed: "(#X-)#Y;(#X-)#Y" (maxDifference and totalPixels are assumed in this order)
let config_parts = try split_skipping_empty(content, ';')
if config_parts.length != 2:
error(fuzzy_invalid, "Fuzzy configuration must have exactly two parameters")
var color_value_error: FuzzyRange? = none
var pixel_error_count: FuzzyRange? = none
for (i, config_part) in config_parts.indexed():
let trimmed = strings.trim(config_part)
if trimmed.length == 0:
error(fuzzy_invalid, "Fuzzy configuration value cannot be empty")
if let named_parts = strings.split_once(trimmed, "="):
if named_parts.0 == "maxDifference" and color_value_error == none:
color_value_error = try parse_fuzzy_range(named_parts.1)
elif named_parts.0 == "totalPixels" and pixel_error_count == none:
pixel_error_count = try parse_fuzzy_range(named_parts.1)
else:
error(fuzzy_invalid, "Invalid fuzzy configuration parameter")
else:
if i == 0 and color_value_error == none:
color_value_error = try parse_fuzzy_range(config_part)
elif i == 1 and pixel_error_count == none:
pixel_error_count = try parse_fuzzy_range(config_part)
else:
error(fuzzy_invalid, "Invalid fuzzy configuration parameter")
return FuzzyMatch(reference = reference,
color_value_error = color_value_error else trap("VERIFY: release_value"),
pixel_error_count = pixel_error_count else trap("VERIFY: release_value"))