chromium/third_party/blink/web_tests/fast/canvas/draw-focus-if-needed.html

<!DOCTYPE HTML>
<head>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
</head>
<canvas id="canvas" class="output" width="300" height="350">
    <button id="button1"></button>
    <button id="button2"></button>
</canvas>
<script>
test(function(t) {

    document.getElementById("button1").focus();
    var canvas = document.getElementById("canvas").getContext("2d");

    assert_throws_js(TypeError, function() {
      canvas.drawFocusIfNeeded(null);
    });
    assert_throws_js(TypeError, function() {
      canvas.drawFocusIfNeeded();
    });

    canvas.beginPath();
    canvas.rect(50, 50, 200, 100);
    canvas.fillStyle = "#ccf";
    canvas.fill();
    // re-test null case after having defined a path (regression test for crbug.com/353248)
    assert_throws_js(TypeError, function() {
      canvas.drawFocusIfNeeded(null);
    });
    canvas.drawFocusIfNeeded(document.getElementById("button1"));

    canvas.beginPath();
    canvas.rect(50, 200, 200, 100);
    canvas.fillStyle = "#cfc";
    canvas.fill();
    canvas.drawFocusIfNeeded(document.getElementById("button2"));

    // The top rect's focus ring is tied to button1, which is focused.
    // It should have an outline in some color other than the background color.
    var imageData = canvas.getImageData(49, 50, 1, 1);
    var data = imageData.data;
    assert_true(data[0] != 0 || data[1] != 0 || data[2] != 0);

    // The bottom rect"s focus ring is tied to button2, which is not focused.
    imageData = canvas.getImageData(49, 200, 1, 1);
    data = imageData.data;
    assert_true(data[0] == 0 && data[1] == 0 && data[2] == 0);
}, 'Canvas test: drawFocusIfNeeded');
</script>
</body>