3 import "testhelper.js" as Helper
5 id:canvas; width:100;height:50; renderTarget:Canvas.Image
7 name: "composite"; when: windowShown
8 function test_clearRect() {
9 var ctx = canvas.getContext('2d');
11 ctx.fillStyle = '#f00';
12 ctx.fillRect(0, 0, 100, 50);
13 ctx.globalCompositeOperation = 'destination-atop';
14 ctx.clearRect(0, 0, 100, 50);
15 verify(Helper.comparePixel(ctx, 50,25, 0,0,0,0));
18 function test_clip_data() {
19 return [ {compsite:"copy"},
20 {compsite:"destination-atop"},
21 {compsite:"destination-in"},
22 {compsite:"destination-out"},
23 {compsite:"destination-over"},
25 {compsite:"source-atop"},
26 {compsite:"source-in"},
27 {compsite:"source-out"},
28 {compsite:"source-over"},
33 function test_clip(data) {
34 var ctx = canvas.getContext('2d');
36 ctx.fillStyle = '#0f0';
37 ctx.fillRect(0, 0, 100, 50);
38 ctx.globalCompositeOperation = data.compsite;
39 ctx.rect(-20, -20, 10, 10);
41 ctx.fillStyle = '#f00';
42 ctx.fillRect(0, 0, 50, 50);
43 verify(Helper.comparePixel(ctx, 25,25, 0,255,0,255));
44 verify(Helper.comparePixel(ctx, 75,25, 0,255,0,255));
47 function test_globalAlpha() {
48 var ctx = canvas.getContext('2d');
50 compare(ctx.globalAlpha, 1.0);
52 ctx.fillStyle = '#0f0';
53 ctx.fillRect(0, 0, 100, 50);
54 ctx.globalAlpha = 0.01; // avoid any potential alpha=0 optimisations
55 ctx.fillStyle = '#f00';
56 ctx.fillRect(0, 0, 100, 50);
57 //verify(Helper.comparePixel(ctx, 50,25, 2,253,0,255, 2));
60 ctx.globalAlpha = 0.5;
61 var a = ctx.globalAlpha; // might not be exactly 0.5, if it is rounded/quantised, so remember for future comparisons
62 ctx.globalAlpha = Infinity;
63 compare(ctx.globalAlpha, a);
64 ctx.globalAlpha = -Infinity;
65 compare(ctx.globalAlpha, a);
66 ctx.globalAlpha = NaN;
67 compare(ctx.globalAlpha, a);
69 ctx.globalAlpha = 0.5;
70 a = ctx.globalAlpha; // might not be exactly 0.5, if it is rounded/quantised, so remember for future comparisons
71 ctx.globalAlpha = 1.1;
72 compare(ctx.globalAlpha, a);
73 ctx.globalAlpha = -0.1;
74 compare(ctx.globalAlpha, a);
76 compare(ctx.globalAlpha, 0);
78 compare(ctx.globalAlpha, 1);
82 function test_operation() {
83 var ctx = canvas.getContext('2d');
85 ctx.globalCompositeOperation = 'xor';
86 ctx.globalCompositeOperation = 'Source-over';
87 compare(ctx.globalCompositeOperation, 'xor');
90 ctx.globalCompositeOperation = 'xor';
91 ctx.globalCompositeOperation = 'clear';
92 compare(ctx.globalCompositeOperation, 'xor');
95 ctx.globalCompositeOperation = 'xor';
96 ctx.globalCompositeOperation = 'darker';
97 compare(ctx.globalCompositeOperation, 'xor');
100 compare(ctx.globalCompositeOperation, 'source-over');
104 var modes = ['source-atop', 'source-in', 'source-out', 'source-over',
105 'destination-atop', 'destination-in', 'destination-out', 'destination-over',
106 'lighter', 'copy', 'xor'];
107 for (var i = 0; i < modes.length; ++i)
109 ctx.globalCompositeOperation = modes[i];
110 compare(ctx.globalCompositeOperation, modes[i]);
114 ctx.globalCompositeOperation = 'xor';
115 ctx.globalCompositeOperation = 'highlight';
116 compare(ctx.globalCompositeOperation, 'xor');
119 ctx.globalCompositeOperation = 'xor';
120 ctx.globalCompositeOperation = 'source-over\\0';
121 compare(ctx.globalCompositeOperation, 'xor');
124 ctx.globalCompositeOperation = 'xor';
125 ctx.globalCompositeOperation = 'over';
126 compare(ctx.globalCompositeOperation, 'xor');
130 ctx.globalCompositeOperation = 'xor';
131 ctx.globalCompositeOperation = 'nonexistent';
132 compare(ctx.globalCompositeOperation, 'xor');
135 function test_solid() {
137 var ctx = canvas.getContext('2d');
139 ctx.fillStyle = Qt.rgba(0, 1, 1, 1.0);
140 ctx.fillRect(0, 0, 100, 50);
141 ctx.globalCompositeOperation = 'copy';
142 ctx.fillStyle = Qt.rgba(1, 1, 0, 1.0);
143 ctx.fillRect(0, 0, 100, 50);
144 //verify(Helper.comparePixel(ctx, 50,25, 255,255,0, 5));
147 ctx.fillStyle = 'rgba(0, 255, 255, 1.0)';
148 ctx.fillRect(0, 0, 100, 50);
149 ctx.globalCompositeOperation = 'destination-atop';
150 ctx.fillStyle = 'rgba(255, 255, 0, 1.0)';
151 ctx.fillRect(0, 0, 100, 50);
152 //verify(Helper.comparePixel(ctx, 50,25, 0,255,255,255, 5));
155 ctx.fillStyle = 'rgba(0, 255, 255, 1.0)';
156 ctx.fillRect(0, 0, 100, 50);
157 ctx.globalCompositeOperation = 'destination-in';
158 ctx.fillStyle = 'rgba(255, 255, 0, 1.0)';
159 ctx.fillRect(0, 0, 100, 50);
160 //verify(Helper.comparePixel(ctx, 50,25, 0,255,255,255, 5));
163 ctx.fillStyle = 'rgba(0, 255, 255, 1.0)';
164 ctx.fillRect(0, 0, 100, 50);
165 ctx.globalCompositeOperation = 'destination-out';
166 ctx.fillStyle = 'rgba(255, 255, 0, 1.0)';
167 ctx.fillRect(0, 0, 100, 50);
168 verify(Helper.comparePixel(ctx, 50,25, 0,0,0,0, 5));
172 ctx.fillStyle = 'rgba(0, 255, 255, 1.0)';
173 ctx.fillRect(0, 0, 100, 50);
174 ctx.globalCompositeOperation = 'destination-over';
175 ctx.fillStyle = 'rgba(255, 255, 0, 1.0)';
176 ctx.fillRect(0, 0, 100, 50);
177 //verify(Helper.comparePixel(ctx, 50,25, 0,255,255,255, 5));
180 ctx.fillStyle = 'rgba(0, 255, 255, 1.0)';
181 ctx.fillRect(0, 0, 100, 50);
182 ctx.globalCompositeOperation = 'lighter';
183 ctx.fillStyle = 'rgba(255, 255, 0, 1.0)';
184 ctx.fillRect(0, 0, 100, 50);
185 //verify(Helper.comparePixel(ctx, 50,25, 255,255,255,255, 5));
189 ctx.fillStyle = 'rgba(0, 255, 255, 1.0)';
190 ctx.fillRect(0, 0, 100, 50);
191 ctx.globalCompositeOperation = 'source-atop';
192 ctx.fillStyle = 'rgba(255, 255, 0, 1.0)';
193 ctx.fillRect(0, 0, 100, 50);
194 //verify(Helper.comparePixel(ctx, 50,25, 255,255,0, 5));
198 ctx.fillStyle = 'rgba(0, 255, 255, 1.0)';
199 ctx.fillRect(0, 0, 100, 50);
200 ctx.globalCompositeOperation = 'source-in';
201 ctx.fillStyle = 'rgba(255, 255, 0, 1.0)';
202 ctx.fillRect(0, 0, 100, 50);
203 //verify(Helper.comparePixel(ctx, 50,25, 255,255,0, 5));
207 ctx.fillStyle = 'rgba(0, 255, 255, 1.0)';
208 ctx.fillRect(0, 0, 100, 50);
209 ctx.globalCompositeOperation = 'source-out';
210 ctx.fillStyle = 'rgba(255, 255, 0, 1.0)';
211 ctx.fillRect(0, 0, 100, 50);
212 // verify(Helper.comparePixel(ctx, 50,25, 0,0,0,0, 5));
216 ctx.fillStyle = 'rgba(0, 255, 255, 1.0)';
217 ctx.fillRect(0, 0, 100, 50);
218 ctx.globalCompositeOperation = 'source-over';
219 ctx.fillStyle = 'rgba(255, 255, 0, 1.0)';
220 ctx.fillRect(0, 0, 100, 50);
221 //verify(Helper.comparePixel(ctx, 50,25, 255,255,0, 5));
224 ctx.fillStyle = 'rgba(0, 255, 255, 1.0)';
225 ctx.fillRect(0, 0, 100, 50);
226 ctx.globalCompositeOperation = 'xor';
227 ctx.fillStyle = 'rgba(255, 255, 0, 1.0)';
228 ctx.fillRect(0, 0, 100, 50);
229 //verify(Helper.comparePixel(ctx, 50,25, 0,0,0,0, 5));
231 function test_transparent() {
234 var ctx = canvas.getContext('2d');
236 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
237 ctx.fillRect(0, 0, 100, 50);
238 ctx.globalCompositeOperation = 'copy';
239 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
240 ctx.fillRect(0, 0, 100, 50);
241 verify(Helper.comparePixel(ctx, 50,25, 0,0,255,191, 5));
244 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
245 ctx.fillRect(0, 0, 100, 50);
246 ctx.globalCompositeOperation = 'copy';
247 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
248 ctx.fillRect(0, 0, 100, 50);
249 verify(Helper.comparePixel(ctx, 50,25, 0,0,255,191, 5));
252 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
253 ctx.fillRect(0, 0, 100, 50);
254 ctx.globalCompositeOperation = 'destination-in';
255 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
256 ctx.fillRect(0, 0, 100, 50);
257 verify(Helper.comparePixel(ctx, 50,25, 0,255,0,95, 5));
260 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
261 ctx.fillRect(0, 0, 100, 50);
262 ctx.globalCompositeOperation = 'destination-out';
263 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
264 ctx.fillRect(0, 0, 100, 50);
265 verify(Helper.comparePixel(ctx, 50,25, 0,255,0,31, 5));
268 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
269 ctx.fillRect(0, 0, 100, 50);
270 ctx.globalCompositeOperation = 'destination-over';
271 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
272 ctx.fillRect(0, 0, 100, 50);
273 verify(Helper.comparePixel(ctx, 50,25, 0,145,109,223, 5));
277 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
278 ctx.fillRect(0, 0, 100, 50);
279 ctx.globalCompositeOperation = 'lighter';
280 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
281 ctx.fillRect(0, 0, 100, 50);
282 verify(Helper.comparePixel(ctx, 50,25, 0,127,191,255, 5));
285 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
286 ctx.fillRect(0, 0, 100, 50);
287 ctx.globalCompositeOperation = 'source-atop';
288 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
289 ctx.fillRect(0, 0, 100, 50);
290 verify(Helper.comparePixel(ctx, 50,25, 0,63,191,127, 5));
293 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
294 ctx.fillRect(0, 0, 100, 50);
295 ctx.globalCompositeOperation = 'source-in';
296 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
297 ctx.fillRect(0, 0, 100, 50);
298 verify(Helper.comparePixel(ctx, 50,25, 0,0,255,95, 5));
301 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
302 ctx.fillRect(0, 0, 100, 50);
303 ctx.globalCompositeOperation = 'source-out';
304 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
305 ctx.fillRect(0, 0, 100, 50);
306 verify(Helper.comparePixel(ctx, 50,25, 0,0,255,95, 5));
310 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
311 ctx.fillRect(0, 0, 100, 50);
312 ctx.globalCompositeOperation = 'source-over';
313 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
314 ctx.fillRect(0, 0, 100, 50);
315 verify(Helper.comparePixel(ctx, 50,25, 0,36,218,223, 5));
318 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
319 ctx.fillRect(0, 0, 100, 50);
320 ctx.globalCompositeOperation = 'xor';
321 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
322 ctx.fillRect(0, 0, 100, 50);
323 verify(Helper.comparePixel(ctx, 50,25, 0,63,191,127, 5));
327 function test_uncovered() {
329 var ctx = canvas.getContext('2d');
331 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
332 ctx.fillRect(0, 0, 100, 50);
333 ctx.globalCompositeOperation = 'copy';
334 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
335 ctx.translate(0, 25);
336 ctx.fillRect(0, 50, 100, 50);
337 verify(Helper.comparePixel(ctx, 50,25, 0,0,0,0, 5));
340 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
341 ctx.fillRect(0, 0, 100, 50);
342 ctx.globalCompositeOperation = 'destination-atop';
343 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
344 ctx.translate(0, 25);
345 ctx.fillRect(0, 50, 100, 50);
346 verify(Helper.comparePixel(ctx, 50,25, 0,0,0,0, 5));
351 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
352 ctx.fillRect(0, 0, 100, 50);
353 ctx.globalCompositeOperation = 'destination-in';
354 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
355 ctx.translate(0, 25);
356 ctx.fillRect(0, 50, 100, 50);
357 verify(Helper.comparePixel(ctx, 50,25, 0,0,0,0, 5));
360 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
361 ctx.fillRect(0, 0, 100, 50);
362 ctx.globalCompositeOperation = 'source-in';
363 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
364 ctx.translate(0, 25);
365 ctx.fillRect(0, 50, 100, 50);
366 verify(Helper.comparePixel(ctx, 50,25, 0,0,0,0, 5));
369 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
370 ctx.fillRect(0, 0, 100, 50);
371 ctx.globalCompositeOperation = 'source-out';
372 ctx.fillStyle = 'rgba(0, 0, 255, 0.75)';
373 ctx.translate(0, 25);
374 ctx.fillRect(0, 50, 100, 50);
375 verify(Helper.comparePixel(ctx, 50,25, 0,0,0,0, 5));