<style></style>
<script src="../../resources/js-test-pre.js"></script>
<p id="description"></p>
<div id="console"></div>
<p id="target"></p>
<script>
description("Test the computed style of the color() function.");

let styleElement = document.querySelector("style");
let stylesheet = styleElement.sheet;
let cssRule, declaration;

function testColorFunction(description, rule, expectedValue)
{
    debug("");
    debug(`TEST: ${description} --> ${rule}`);

    stylesheet.insertRule(`#target { color: ${rule}; }`, 0);
    cssRule = stylesheet.cssRules.item(0);

    declaration = window.getComputedStyle(document.getElementById("target"));
    shouldBe("declaration.getPropertyValue('color')", `'${expectedValue}'`);
    stylesheet.deleteRule(0);
}

testColorFunction("Basic sRGB white", "color(srgb 1 1 1)", "color(srgb 1 1 1)");
testColorFunction("White with lots of space", "color(    srgb         1      1 1       )", "color(srgb 1 1 1)");
testColorFunction("sRGB color", "color(srgb 0.25 0.5 0.75)", "color(srgb 0.25 0.5 0.75)");
testColorFunction("Different case for sRGB", "color(SrGb 0.25 0.5 0.75)", "color(srgb 0.25 0.5 0.75)");
testColorFunction("sRGB color with unnecessary decimals", "color(srgb 1.00000 0.500000 0.20)", "color(srgb 1 0.5 0.2)");

testColorFunction("sRGB white with 0.5 alpha", "color(srgb 1 1 1 / 0.5)", "color(srgb 1 1 1 / 0.5)");
testColorFunction("sRGB white with 0 alpha", "color(srgb 1 1 1 / 0)", "color(srgb 1 1 1 / 0)");
testColorFunction("sRGB white with 50% alpha", "color(srgb 1 1 1 / 50%)", "color(srgb 1 1 1 / 0.5)");
testColorFunction("sRGB white with 0% alpha", "color(srgb 1 1 1 / 0%)", "color(srgb 1 1 1 / 0)");

testColorFunction("Display P3 color", "color(display-p3 0.6 0.7 0.8)", "color(display-p3 0.6 0.7 0.8)");
testColorFunction("Different case for Display P3", "color(dIspLaY-P3 0.6 0.7 0.8)", "color(display-p3 0.6 0.7 0.8)");

debug("");
debug("");
debug("Fallback tests.")
debug("");

testColorFunction("Unknown color space should fallback", "color(unknown 1 2 3, red)", "color(unknown 1 2 3, red)");

debug("");
debug("");
debug("Clamping tests.")
debug("");

testColorFunction("sRGB color with negative component should not clamp to 0", "color(srgb -0.25 0.5 0.75)", "color(srgb -0.25 0.5 0.75)");
testColorFunction("sRGB color with component > 1 should not clamp", "color(srgb 0.25 1.5 0.75)", "color(srgb 0.25 1.5 0.75)");

testColorFunction("Display P3 color with negative component should not clamp to 0", "color(display-p3 0.5 -199 0.75)", "color(display-p3 0.5 -199 0.75)");
testColorFunction("Display P3 color with component > 1 should not clamp", "color(display-p3 184 1.00001 2347329746587)", "color(display-p3 184 1.00001 2347329700000)");

testColorFunction("Alpha > 1 should clamp", "color(srgb 0.1 0.2 0.3 / 1.9)", "color(srgb 0.1 0.2 0.3)");
testColorFunction("Negative alpha should clamp", "color(srgb 1 1 1 / -0.2)", "color(srgb 1 1 1 / 0)");

debug("");
debug("");
debug("Invalid property value tests.")
debug("");

testColorFunction("Empty", "color()", "rgb(0, 0, 0)");
testColorFunction("Bad color space", "color(banana 1 1 1)", "rgb(0, 0, 0)");
testColorFunction("Bad Display P3 color space", "color(displayp3 1 1 1)", "rgb(0, 0, 0)");
testColorFunction("No color space", "color(1 1 1)", "rgb(0, 0, 0)");
testColorFunction("Too many parameters", "color(srgb 1 1 1 1)", "rgb(0, 0, 0)");
testColorFunction("Way too many parameters", "color(srgb 1 1 1 1 1)", "rgb(0, 0, 0)");
testColorFunction("Bad parameters", "color(srgb 1 eggs 1)", "rgb(0, 0, 0)");
testColorFunction("Bad alpha", "color(srgb 1 1 1 / bacon)", "rgb(0, 0, 0)");
testColorFunction("Junk after alpha", "color(srgb 1 1 1 / 1 cucumber)", "rgb(0, 0, 0)");
testColorFunction("One missing component is 0", "color(srgb 1 1)", "rgb(0, 0, 0)");
testColorFunction("Two missing components are 0", "color(srgb 1)", "rgb(0, 0, 0)");
testColorFunction("All components missing", "color(srgb)", "rgb(0, 0, 0)");

debug("");

</script>
<script src="../../resources/js-test-post.js"></script>
