|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | +<script src="../../resources/js-test.js"></script> |
| 5 | +<style> |
| 6 | +canvas { |
| 7 | + display: block; |
| 8 | +} |
| 9 | +</style> |
| 10 | +</head> |
| 11 | +<body> |
| 12 | +<canvas id="canvas1" width="100" height="100"> |
| 13 | + <button id="btn1" tabindex="0">X</button> |
| 14 | +</canvas> |
| 15 | +<canvas id="canvas2" width="100" height="100" style="zoom: 2"> |
| 16 | + <button id="btn2" tabindex="0">X</button> |
| 17 | +</canvas> |
| 18 | +<canvas id="canvas3" width="100" height="100"> |
| 19 | + <button id="btn3" tabindex="0">X</button> |
| 20 | +</canvas> |
| 21 | +<script> |
| 22 | +description("This test ensures that the canvas drawFocusIfNeeded focus ring scales with CSS zoom and canvas scale."); |
| 23 | + |
| 24 | +function countFocusRingPixels(canvas, scale) { |
| 25 | + var context = canvas.getContext("2d"); |
| 26 | + context.clearRect(0, 0, canvas.width, canvas.height); |
| 27 | + |
| 28 | + if (scale) { |
| 29 | + context.save(); |
| 30 | + context.scale(scale, scale); |
| 31 | + } |
| 32 | + |
| 33 | + var button = canvas.querySelector("button"); |
| 34 | + context.beginPath(); |
| 35 | + context.rect(10, 10, 30, 30); |
| 36 | + button.focus(); |
| 37 | + context.drawFocusIfNeeded(button); |
| 38 | + button.blur(); |
| 39 | + |
| 40 | + if (scale) |
| 41 | + context.restore(); |
| 42 | + |
| 43 | + var imageData = context.getImageData(0, 0, canvas.width, canvas.height).data; |
| 44 | + var count = 0; |
| 45 | + for (var i = 3; i < imageData.length; i += 4) { |
| 46 | + if (imageData[i] > 0) |
| 47 | + count++; |
| 48 | + } |
| 49 | + return count; |
| 50 | +} |
| 51 | + |
| 52 | +var canvas1 = document.getElementById("canvas1"); |
| 53 | +var canvas2 = document.getElementById("canvas2"); |
| 54 | +var canvas3 = document.getElementById("canvas3"); |
| 55 | + |
| 56 | +var pixels1x = countFocusRingPixels(canvas1); |
| 57 | +var pixels2xZoom = countFocusRingPixels(canvas2); |
| 58 | +var pixels2xScale = countFocusRingPixels(canvas3, 2); |
| 59 | + |
| 60 | +// At 2x zoom the focus ring radius should be doubled, producing more painted |
| 61 | +// pixels. We check that the zoomed canvas has strictly more focus ring pixels. |
| 62 | +shouldBeTrue("pixels2xZoom > pixels1x"); |
| 63 | + |
| 64 | +// Similarly, ctx.scale(2, 2) should also produce a scaled |
| 65 | +// focus ring with more pixels than the unscaled version. |
| 66 | +shouldBeTrue("pixels2xScale > pixels1x"); |
| 67 | +</script> |
| 68 | +</body> |
| 69 | +</html> |
0 commit comments