summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/file_drawWindow_common.js
blob: 6c699decab6ec1ae316b9f5614a7dba71755039a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const CANVAS_WIDTH = 200;
const CANVAS_HEIGHT = 100;

async function runDrawWindowTests(snapshotCallback, transparentBackground) {
  function make_canvas() {
    var canvas = document.createElement("canvas");
    canvas.setAttribute("height", CANVAS_HEIGHT);
    canvas.setAttribute("width", CANVAS_WIDTH);
    document.body.appendChild(canvas);
    return canvas;
  }

  var testCanvas = make_canvas();
  var refCanvas = make_canvas();

  var testCx = testCanvas.getContext("2d");
  var refCx = refCanvas.getContext("2d");

  var testWrapCx = SpecialPowers.wrap(testCx);
  var refWrapCx = SpecialPowers.wrap(refCx);

  function clearRef(fillStyle) {
    refCx.setTransform(1, 0, 0, 1, 0, 0);
    refCx.fillStyle = fillStyle;
    refCx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
  }
  function clearTest(fillStyle) {
    testCx.setTransform(1, 0, 0, 1, 0, 0);
    testCx.fillStyle = fillStyle;
    testCx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
  }
  function clear(fillStyle) {
    clearRef(fillStyle);
    clearTest(fillStyle);
  }

  // Basic tests of drawing the whole document on a background

  clear("white");
  await snapshotCallback(
    testWrapCx,
    0,
    0,
    CANVAS_WIDTH,
    CANVAS_HEIGHT,
    "rgb(255, 255, 255)"
  );
  refCx.fillStyle = "fuchsia";
  refCx.fillRect(10, 10, 20, 20);
  refCx.fillStyle = "aqua";
  refCx.fillRect(50, 10, 20, 20);
  refCx.fillStyle = "yellow";
  refCx.fillRect(90, 10, 20, 20);
  assertSnapshots(
    testCanvas,
    refCanvas,
    true /* equal */,
    null /*no fuzz*/,
    "full draw of source on white background",
    "reference"
  );

  clearTest("white");
  await snapshotCallback(
    testWrapCx,
    0,
    0,
    CANVAS_WIDTH,
    CANVAS_HEIGHT,
    "rgb(255, 255, 0)"
  );
  assertSnapshots(
    testCanvas,
    refCanvas,
    !transparentBackground /* not equal */,
    null /*no fuzz*/,
    "full draw of source on yellow background",
    "reference"
  );

  clearRef("yellow");
  refCx.fillStyle = "fuchsia";
  refCx.fillRect(10, 10, 20, 20);
  refCx.fillStyle = "aqua";
  refCx.fillRect(50, 10, 20, 20);
  refCx.fillStyle = "yellow";
  refCx.fillRect(90, 10, 20, 20);

  assertSnapshots(
    testCanvas,
    refCanvas,
    transparentBackground /* equal */,
    null /*no fuzz*/,
    "full draw of source on yellow background",
    "reference"
  );

  // Test drawing a region within the document.

  clear("white");

  testCx.translate(17, 31);
  await snapshotCallback(testWrapCx, 40, 0, 40, 40, "white");

  refCx.fillStyle = "aqua";
  refCx.fillRect(17 + 10, 31 + 10, 20, 20);

  assertSnapshots(
    testCanvas,
    refCanvas,
    true /* equal */,
    null /*no fuzz*/,
    "draw of subrect of source with matching background",
    "reference"
  );

  clear("blue");

  testCx.translate(17, 31);
  await snapshotCallback(testWrapCx, 40, 0, 35, 45, "green");

  if (transparentBackground) {
    refCx.fillStyle = "green";
  } else {
    refCx.fillStyle = "white";
  }
  refCx.fillRect(17, 31, 35, 45);
  refCx.fillStyle = "aqua";
  refCx.fillRect(17 + 10, 31 + 10, 20, 20);

  assertSnapshots(
    testCanvas,
    refCanvas,
    true /* equal */,
    null /*no fuzz*/,
    "draw of subrect of source with different background",
    "reference"
  );

  // Test transparency of background not disturbing what is behind
  clear("blue");

  testCx.translate(17, 31);
  await snapshotCallback(testWrapCx, 40, 0, 35, 45, "transparent");

  if (!transparentBackground) {
    refCx.fillStyle = "white";
    refCx.fillRect(17, 31, 35, 45);
  }
  refCx.fillStyle = "aqua";
  refCx.fillRect(17 + 10, 31 + 10, 20, 20);

  assertSnapshots(
    testCanvas,
    refCanvas,
    true /* equal */,
    null /*no fuzz*/,
    "draw of subrect of source with different background",
    "reference"
  );

  // Test that multiple drawWindow calls draw at correct positions.
  clear("blue");

  testCx.translate(9, 3);
  // 5, 8 is 5, 2 from the corner of the fuchsia square
  await snapshotCallback(testWrapCx, 5, 8, 30, 25, "maroon");
  // 35, 0 is 15, 10 from the corner of the aqua square
  await snapshotCallback(testWrapCx, 35, 0, 50, 40, "transparent");
  testCx.translate(15, 0);
  // 85, 5 is 5, 5 from the corner of the yellow square
  await snapshotCallback(testWrapCx, 85, 5, 30, 25, "transparent");

  if (transparentBackground) {
    refCx.fillStyle = "maroon";
    refCx.fillRect(9, 3, 30, 25);
    refCx.fillStyle = "fuchsia";
    refCx.fillRect(9 + 5, 3 + 2, 20, 20);
  } else {
    refCx.fillStyle = "white";
    refCx.fillRect(9, 3, 50, 40);
  }
  refCx.fillStyle = "aqua";
  refCx.fillRect(9 + 15, 3 + 10, 20, 20);
  if (!transparentBackground) {
    refCx.fillStyle = "white";
    refCx.fillRect(9 + 15, 3, 30, 25);
  }
  refCx.fillStyle = "yellow";
  refCx.fillRect(9 + 15 + 5, 3 + 0 + 5, 20, 20);

  assertSnapshots(
    testCanvas,
    refCanvas,
    true /* equal */,
    null /*no fuzz*/,
    "multiple drawWindow calls on top of each other",
    "reference"
  );
}