content/canvas/test/file_drawWindow_common.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/canvas/test/file_drawWindow_common.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,157 @@
     1.4 +function runDrawWindowTests(win, drawWindowFlags, transparentBackground) {
     1.5 +  const CANVAS_WIDTH = 200;
     1.6 +  const CANVAS_HEIGHT = 100;
     1.7 +
     1.8 +  function make_canvas() {
     1.9 +    var canvas = document.createElement("canvas");
    1.10 +    canvas.setAttribute("height", CANVAS_HEIGHT);
    1.11 +    canvas.setAttribute("width", CANVAS_WIDTH);
    1.12 +    document.body.appendChild(canvas);
    1.13 +    return canvas;
    1.14 +  }
    1.15 +
    1.16 +  var testCanvas = make_canvas();
    1.17 +  var refCanvas = make_canvas();
    1.18 +
    1.19 +  var testCx = testCanvas.getContext("2d");
    1.20 +  var refCx = refCanvas.getContext("2d");
    1.21 +
    1.22 +  var testWrapCx = SpecialPowers.wrap(testCx);
    1.23 +  var refWrapCx = SpecialPowers.wrap(refCx);
    1.24 +
    1.25 +  function clearRef(fillStyle) {
    1.26 +    refCx.setTransform(1, 0, 0, 1, 0, 0);
    1.27 +    refCx.fillStyle = fillStyle;
    1.28 +    refCx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
    1.29 +  }
    1.30 +  function clearTest(fillStyle) {
    1.31 +    testCx.setTransform(1, 0, 0, 1, 0, 0);
    1.32 +    testCx.fillStyle = fillStyle;
    1.33 +    testCx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
    1.34 +  }
    1.35 +  function clear(fillStyle) {
    1.36 +    clearRef(fillStyle);
    1.37 +    clearTest(fillStyle);
    1.38 +  }
    1.39 +
    1.40 +  // Basic tests of drawing the whole document on a background
    1.41 +
    1.42 +  clear("white");
    1.43 +  testWrapCx.drawWindow(win, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT,
    1.44 +                        "rgb(255, 255, 255)", drawWindowFlags);
    1.45 +  refCx.fillStyle = "fuchsia";
    1.46 +  refCx.fillRect(10, 10, 20, 20);
    1.47 +  refCx.fillStyle = "aqua";
    1.48 +  refCx.fillRect(50, 10, 20, 20);
    1.49 +  refCx.fillStyle = "yellow";
    1.50 +  refCx.fillRect(90, 10, 20, 20);
    1.51 +  assertSnapshots(testCanvas, refCanvas, true /* equal */,
    1.52 +                  "full draw of source on white background", "reference");
    1.53 +
    1.54 +  clearTest("white");
    1.55 +  testWrapCx.drawWindow(win, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT,
    1.56 +                        "rgb(255, 255, 0)", drawWindowFlags);
    1.57 +  assertSnapshots(testCanvas, refCanvas,
    1.58 +                  !transparentBackground /* not equal */,
    1.59 +                  "full draw of source on yellow background", "reference");
    1.60 +
    1.61 +  clearRef("yellow");
    1.62 +  refCx.fillStyle = "fuchsia";
    1.63 +  refCx.fillRect(10, 10, 20, 20);
    1.64 +  refCx.fillStyle = "aqua";
    1.65 +  refCx.fillRect(50, 10, 20, 20);
    1.66 +  refCx.fillStyle = "yellow";
    1.67 +  refCx.fillRect(90, 10, 20, 20);
    1.68 +
    1.69 +  assertSnapshots(testCanvas, refCanvas, transparentBackground /* equal */,
    1.70 +                  "full draw of source on yellow background", "reference");
    1.71 +
    1.72 +  // Test drawing a region within the document.
    1.73 +
    1.74 +  clear("white");
    1.75 +
    1.76 +  testCx.translate(17, 31);
    1.77 +  testWrapCx.drawWindow(win, 40, 0, 40, 40,
    1.78 +                        "white", drawWindowFlags);
    1.79 +
    1.80 +  refCx.fillStyle = "aqua";
    1.81 +  refCx.fillRect(17 + 10, 31 + 10, 20, 20);
    1.82 +
    1.83 +  assertSnapshots(testCanvas, refCanvas, true /* equal */,
    1.84 +                  "draw of subrect of source with matching background",
    1.85 +                  "reference");
    1.86 +
    1.87 +  clear("blue");
    1.88 +
    1.89 +  testCx.translate(17, 31);
    1.90 +  testWrapCx.drawWindow(win, 40, 0, 35, 45,
    1.91 +                        "green", drawWindowFlags);
    1.92 +
    1.93 +  if (transparentBackground) {
    1.94 +    refCx.fillStyle = "green";
    1.95 +  } else {
    1.96 +    refCx.fillStyle = "white";
    1.97 +  }
    1.98 +  refCx.fillRect(17, 31, 35, 45);
    1.99 +  refCx.fillStyle = "aqua";
   1.100 +  refCx.fillRect(17 + 10, 31 + 10, 20, 20);
   1.101 +
   1.102 +  assertSnapshots(testCanvas, refCanvas, true /* equal */,
   1.103 +                  "draw of subrect of source with different background",
   1.104 +                  "reference");
   1.105 +
   1.106 +  // Test transparency of background not disturbing what is behind
   1.107 +  clear("blue");
   1.108 +
   1.109 +  testCx.translate(17, 31);
   1.110 +  testWrapCx.drawWindow(win, 40, 0, 35, 45,
   1.111 +                        "transparent", drawWindowFlags);
   1.112 +
   1.113 +  if (!transparentBackground) {
   1.114 +    refCx.fillStyle = "white";
   1.115 +    refCx.fillRect(17, 31, 35, 45);
   1.116 +  }
   1.117 +  refCx.fillStyle = "aqua";
   1.118 +  refCx.fillRect(17 + 10, 31 + 10, 20, 20);
   1.119 +
   1.120 +  assertSnapshots(testCanvas, refCanvas, true /* equal */,
   1.121 +                  "draw of subrect of source with different background",
   1.122 +                  "reference");
   1.123 +
   1.124 +  // Test that multiple drawWindow calls draw at correct positions.
   1.125 +  clear("blue");
   1.126 +
   1.127 +  testCx.translate(9, 3);
   1.128 +  // 5, 8 is 5, 2 from the corner of the fuchsia square
   1.129 +  testWrapCx.drawWindow(win, 5, 8, 30, 25,
   1.130 +                        "maroon", drawWindowFlags);
   1.131 +  // 35, 0 is 15, 10 from the corner of the aqua square
   1.132 +  testWrapCx.drawWindow(win, 35, 0, 50, 40,
   1.133 +                        "transparent", drawWindowFlags);
   1.134 +  testCx.translate(15, 0);
   1.135 +  // 85, 5 is 5, 5 from the corner of the yellow square
   1.136 +  testWrapCx.drawWindow(win, 85, 5, 30, 25,
   1.137 +                        "transparent", drawWindowFlags);
   1.138 +
   1.139 +  if (transparentBackground) {
   1.140 +    refCx.fillStyle = "maroon";
   1.141 +    refCx.fillRect(9, 3, 30, 25);
   1.142 +    refCx.fillStyle = "fuchsia";
   1.143 +    refCx.fillRect(9 + 5, 3 + 2, 20, 20);
   1.144 +  } else {
   1.145 +    refCx.fillStyle = "white";
   1.146 +    refCx.fillRect(9, 3, 50, 40);
   1.147 +  }
   1.148 +  refCx.fillStyle = "aqua";
   1.149 +  refCx.fillRect(9 + 15, 3 + 10, 20, 20);
   1.150 +  if (!transparentBackground) {
   1.151 +    refCx.fillStyle = "white";
   1.152 +    refCx.fillRect(9 + 15, 3, 30, 25);
   1.153 +  }
   1.154 +  refCx.fillStyle = "yellow";
   1.155 +  refCx.fillRect(9 + 15 + 5, 3 + 0 + 5, 20, 20);
   1.156 +
   1.157 +  assertSnapshots(testCanvas, refCanvas, true /* equal */,
   1.158 +                  "multiple drawWindow calls on top of each other",
   1.159 +                  "reference");
   1.160 +}

mercurial