michael@0: function runDrawWindowTests(win, drawWindowFlags, transparentBackground) { michael@0: const CANVAS_WIDTH = 200; michael@0: const CANVAS_HEIGHT = 100; michael@0: michael@0: function make_canvas() { michael@0: var canvas = document.createElement("canvas"); michael@0: canvas.setAttribute("height", CANVAS_HEIGHT); michael@0: canvas.setAttribute("width", CANVAS_WIDTH); michael@0: document.body.appendChild(canvas); michael@0: return canvas; michael@0: } michael@0: michael@0: var testCanvas = make_canvas(); michael@0: var refCanvas = make_canvas(); michael@0: michael@0: var testCx = testCanvas.getContext("2d"); michael@0: var refCx = refCanvas.getContext("2d"); michael@0: michael@0: var testWrapCx = SpecialPowers.wrap(testCx); michael@0: var refWrapCx = SpecialPowers.wrap(refCx); michael@0: michael@0: function clearRef(fillStyle) { michael@0: refCx.setTransform(1, 0, 0, 1, 0, 0); michael@0: refCx.fillStyle = fillStyle; michael@0: refCx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); michael@0: } michael@0: function clearTest(fillStyle) { michael@0: testCx.setTransform(1, 0, 0, 1, 0, 0); michael@0: testCx.fillStyle = fillStyle; michael@0: testCx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); michael@0: } michael@0: function clear(fillStyle) { michael@0: clearRef(fillStyle); michael@0: clearTest(fillStyle); michael@0: } michael@0: michael@0: // Basic tests of drawing the whole document on a background michael@0: michael@0: clear("white"); michael@0: testWrapCx.drawWindow(win, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, michael@0: "rgb(255, 255, 255)", drawWindowFlags); michael@0: refCx.fillStyle = "fuchsia"; michael@0: refCx.fillRect(10, 10, 20, 20); michael@0: refCx.fillStyle = "aqua"; michael@0: refCx.fillRect(50, 10, 20, 20); michael@0: refCx.fillStyle = "yellow"; michael@0: refCx.fillRect(90, 10, 20, 20); michael@0: assertSnapshots(testCanvas, refCanvas, true /* equal */, michael@0: "full draw of source on white background", "reference"); michael@0: michael@0: clearTest("white"); michael@0: testWrapCx.drawWindow(win, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, michael@0: "rgb(255, 255, 0)", drawWindowFlags); michael@0: assertSnapshots(testCanvas, refCanvas, michael@0: !transparentBackground /* not equal */, michael@0: "full draw of source on yellow background", "reference"); michael@0: michael@0: clearRef("yellow"); michael@0: refCx.fillStyle = "fuchsia"; michael@0: refCx.fillRect(10, 10, 20, 20); michael@0: refCx.fillStyle = "aqua"; michael@0: refCx.fillRect(50, 10, 20, 20); michael@0: refCx.fillStyle = "yellow"; michael@0: refCx.fillRect(90, 10, 20, 20); michael@0: michael@0: assertSnapshots(testCanvas, refCanvas, transparentBackground /* equal */, michael@0: "full draw of source on yellow background", "reference"); michael@0: michael@0: // Test drawing a region within the document. michael@0: michael@0: clear("white"); michael@0: michael@0: testCx.translate(17, 31); michael@0: testWrapCx.drawWindow(win, 40, 0, 40, 40, michael@0: "white", drawWindowFlags); michael@0: michael@0: refCx.fillStyle = "aqua"; michael@0: refCx.fillRect(17 + 10, 31 + 10, 20, 20); michael@0: michael@0: assertSnapshots(testCanvas, refCanvas, true /* equal */, michael@0: "draw of subrect of source with matching background", michael@0: "reference"); michael@0: michael@0: clear("blue"); michael@0: michael@0: testCx.translate(17, 31); michael@0: testWrapCx.drawWindow(win, 40, 0, 35, 45, michael@0: "green", drawWindowFlags); michael@0: michael@0: if (transparentBackground) { michael@0: refCx.fillStyle = "green"; michael@0: } else { michael@0: refCx.fillStyle = "white"; michael@0: } michael@0: refCx.fillRect(17, 31, 35, 45); michael@0: refCx.fillStyle = "aqua"; michael@0: refCx.fillRect(17 + 10, 31 + 10, 20, 20); michael@0: michael@0: assertSnapshots(testCanvas, refCanvas, true /* equal */, michael@0: "draw of subrect of source with different background", michael@0: "reference"); michael@0: michael@0: // Test transparency of background not disturbing what is behind michael@0: clear("blue"); michael@0: michael@0: testCx.translate(17, 31); michael@0: testWrapCx.drawWindow(win, 40, 0, 35, 45, michael@0: "transparent", drawWindowFlags); michael@0: michael@0: if (!transparentBackground) { michael@0: refCx.fillStyle = "white"; michael@0: refCx.fillRect(17, 31, 35, 45); michael@0: } michael@0: refCx.fillStyle = "aqua"; michael@0: refCx.fillRect(17 + 10, 31 + 10, 20, 20); michael@0: michael@0: assertSnapshots(testCanvas, refCanvas, true /* equal */, michael@0: "draw of subrect of source with different background", michael@0: "reference"); michael@0: michael@0: // Test that multiple drawWindow calls draw at correct positions. michael@0: clear("blue"); michael@0: michael@0: testCx.translate(9, 3); michael@0: // 5, 8 is 5, 2 from the corner of the fuchsia square michael@0: testWrapCx.drawWindow(win, 5, 8, 30, 25, michael@0: "maroon", drawWindowFlags); michael@0: // 35, 0 is 15, 10 from the corner of the aqua square michael@0: testWrapCx.drawWindow(win, 35, 0, 50, 40, michael@0: "transparent", drawWindowFlags); michael@0: testCx.translate(15, 0); michael@0: // 85, 5 is 5, 5 from the corner of the yellow square michael@0: testWrapCx.drawWindow(win, 85, 5, 30, 25, michael@0: "transparent", drawWindowFlags); michael@0: michael@0: if (transparentBackground) { michael@0: refCx.fillStyle = "maroon"; michael@0: refCx.fillRect(9, 3, 30, 25); michael@0: refCx.fillStyle = "fuchsia"; michael@0: refCx.fillRect(9 + 5, 3 + 2, 20, 20); michael@0: } else { michael@0: refCx.fillStyle = "white"; michael@0: refCx.fillRect(9, 3, 50, 40); michael@0: } michael@0: refCx.fillStyle = "aqua"; michael@0: refCx.fillRect(9 + 15, 3 + 10, 20, 20); michael@0: if (!transparentBackground) { michael@0: refCx.fillStyle = "white"; michael@0: refCx.fillRect(9 + 15, 3, 30, 25); michael@0: } michael@0: refCx.fillStyle = "yellow"; michael@0: refCx.fillRect(9 + 15 + 5, 3 + 0 + 5, 20, 20); michael@0: michael@0: assertSnapshots(testCanvas, refCanvas, true /* equal */, michael@0: "multiple drawWindow calls on top of each other", michael@0: "reference"); michael@0: }