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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
|
# Copyright (c) 2010 Philip Taylor
# Released under the BSD license and W3C Test Suite License: see LICENSE.txt
- name: fallback.basic
desc: Fallback content is inserted into the DOM
testing:
- fallback
code: |
@assert canvas.childNodes.length === 1;
- name: fallback.multiple
desc: Fallback content with multiple elements
testing:
- fallback
fallback: '<p class="fallback">FAIL</p><p class="fallback">FAIL</p>'
code: |
@assert canvas.childNodes.length === 2;
- name: fallback.nested
desc: Fallback content containing another canvas (mostly testing parsers)
testing:
- fallback
fallback: '<canvas><p class="fallback">FAIL (fallback content)</p></canvas><p class="fallback">FAIL (fallback content)</p>'
code: |
@assert canvas.childNodes.length === 2;
- name: type.name
desc: HTMLCanvasElement type and toString
testing:
- canvas.type
code: |
@assert Object.prototype.toString.call(canvas) === '[object HTMLCanvasElement]';
- name: type.exists
desc: HTMLCanvasElement is a property of window
notes: &bindings Defined in "Web IDL" (draft)
testing:
- canvas.type
code: |
@assert window.HTMLCanvasElement;
- name: type.delete
desc: window.HTMLCanvasElement interface object is [[Configurable]]
notes: *bindings
testing:
- canvas.type
code: |
@assert delete window.HTMLCanvasElement === true;
@assert window.HTMLCanvasElement === undefined;
- name: type.prototype
desc: window.HTMLCanvasElement has prototype, which is { ReadOnly, DontDelete }. prototype has getContext, which is not
notes: *bindings
testing:
- canvas.type
code: |
@assert window.HTMLCanvasElement.prototype;
@assert window.HTMLCanvasElement.prototype.getContext;
window.HTMLCanvasElement.prototype = null;
@assert window.HTMLCanvasElement.prototype;
delete window.HTMLCanvasElement.prototype;
@assert window.HTMLCanvasElement.prototype;
window.HTMLCanvasElement.prototype.getContext = 1;
@assert window.HTMLCanvasElement.prototype.getContext === 1;
delete window.HTMLCanvasElement.prototype.getContext;
@assert window.HTMLCanvasElement.prototype.getContext === undefined;
- name: type.replace
desc: HTMLCanvasElement methods can be replaced, and the replacement methods used by canvases
notes: *bindings
testing:
- canvas.type
code: |
window.HTMLCanvasElement.prototype.getContext = function (name) { return 0; };
@assert canvas.getContext('2d') === 0;
- name: type.extend
desc: HTMLCanvasElement methods can be added, and the new methods used by canvases
notes: *bindings
testing:
- canvas.type
code: |
window.HTMLCanvasElement.prototype.getZero = function () { return 0; };
@assert canvas.getZero() === 0;
- name: size.attributes.idl.set.zero
desc: Setting width/height IDL attributes to 0
testing:
- size.width
- size.height
code: |
canvas.width = 0;
canvas.height = 0;
@assert canvas.width === 0;
@assert canvas.height === 0;
# expected: size 0 0 # can't generate zero-sized PNG
- name: size.attributes.idl
desc: Getting/setting width/height IDL attributes
testing:
- size.width
- size.height
webidl:
- es-unsigned-long
code: |
canvas.width = "100";
canvas.height = "100";
@assert canvas.width === 100;
@assert canvas.height === 100;
canvas.width = "+1.5e2";
canvas.height = "0x96";
@assert canvas.width === 150;
@assert canvas.height === 150;
canvas.width = 200 - Math.pow(2, 32);
canvas.height = 200 - Math.pow(2, 32);
@assert canvas.width === 200;
@assert canvas.height === 200;
canvas.width = 301.999;
canvas.height = 301.001;
@assert canvas.width === 301;
@assert canvas.height === 301;
canvas.width = "400x";
canvas.height = "foo";
@assert canvas.width === 0;
@assert canvas.height === 0;
- name: size.attributes.default
desc: Default width/height when attributes are missing
testing:
- size.default
- size.missing
canvas: ""
code: |
@assert canvas.width === 300;
@assert canvas.height === 150;
@assert !canvas.hasAttribute('width');
@assert !canvas.hasAttribute('height');
expected: size 300 150
- name: size.attributes.reflect.setidl
desc: Setting IDL attributes updates IDL and content attributes
testing:
- size.reflect
code: |
canvas.width = 120;
canvas.height = 60;
@assert canvas.getAttribute('width') === '120';
@assert canvas.getAttribute('height') === '60';
@assert canvas.width === 120;
@assert canvas.height === 60;
expected: size 120 60
- name: size.attributes.reflect.setidlzero
desc: Setting IDL attributes to 0 updates IDL and content attributes
testing:
- size.reflect
code: |
canvas.width = 0;
canvas.height = 0;
@assert canvas.getAttribute('width') === '0';
@assert canvas.getAttribute('height') === '0';
@assert canvas.width === 0;
@assert canvas.height === 0;
# expected: size 0 0 # can't generate zero-sized PNG
- name: size.attributes.reflect.setcontent
desc: Setting content attributes updates IDL and content attributes
testing:
- size.reflect
code: |
canvas.setAttribute('width', '120');
canvas.setAttribute('height', '60');
@assert canvas.getAttribute('width') === '120';
@assert canvas.getAttribute('height') === '60';
@assert canvas.width === 120;
@assert canvas.height === 60;
expected: size 120 60
- name: size.attributes.removed
desc: Removing content attributes reverts to default size
testing:
- size.missing
canvas: width="120" height="60"
code: |
@assert canvas.width === 120;
canvas.removeAttribute('width');
@assert canvas.width === 300;
expected: size 300 60
- meta: |
cases = [
("zero", "0", 0),
("empty", "", None),
("onlyspace", " ", None),
("space", " 100", 100),
("whitespace", "\n\t\f100", 100),
("plus", "+100", 100),
("minus", "-100", None),
("octal", "0100", 100),
("hex", "0x100", 0),
("exp", "100e1", 100),
("decimal", "100.999", 100),
("percent", "100%", 100),
("em", "100em", 100),
("junk", "#!?", None),
("trailingjunk", "100#!?", 100),
]
def gen(name, string, exp, code):
testing = ["size.nonnegativeinteger"]
if exp is None:
testing.append("size.error")
code += "@assert canvas.width === 300;\n@assert canvas.height === 150;\n"
expected = "size 300 150"
else:
code += "@assert canvas.width === %s;\n@assert canvas.height === %s;\n" % (exp, exp)
expected = "size %s %s" % (exp, exp)
# With "100%", Opera gets canvas.width = 100 but renders at 100% of the frame width,
# so check the CSS display width
code += '@assert window.getComputedStyle(canvas, null).getPropertyValue("width") === "%spx";\n' % (exp, )
code += "@assert canvas.getAttribute('width') === %r;\n" % string
code += "@assert canvas.getAttribute('height') === %r;\n" % string
if exp == 0:
expected = None # can't generate zero-sized PNGs for the expected image
return code, testing, expected
for name, string, exp in cases:
code = ""
code, testing, expected = gen(name, string, exp, code)
tests.append( {
"name": "size.attributes.parse.%s" % name,
"desc": "Parsing of non-negative integers",
"testing": testing,
"canvas": 'width="%s" height="%s"' % (string, string),
"code": code,
"expected": expected
} )
for name, string, exp in cases:
code = "canvas.setAttribute('width', %r);\ncanvas.setAttribute('height', %r);\n" % (string, string)
code, testing, expected = gen(name, string, exp, code)
tests.append( {
"name": "size.attributes.setAttribute.%s" % name,
"desc": "Parsing of non-negative integers in setAttribute",
"testing": testing,
"canvas": 'width="50" height="50"',
"code": code,
"expected": expected
} )
- name: size.attributes.style
desc: Canvas size is independent of CSS resizing
testing:
- size.css
canvas: 'width="50" height="30" style="width: 100px; height: 50px"'
code: |
@assert canvas.width === 50;
@assert canvas.height === 30;
expected: size 100 50
- name: size.large
DISABLED: |
"User agents may impose implementation-specific limits on otherwise unconstrained
inputs, e.g. to prevent denial of service attacks, to guard against running out of memory,
or to work around platform-specific limitations."
testing:
- size.width
- size.height
notes: Not sure how reasonable this is, but the spec doesn't say there's an upper limit on the size.
code: |
var n = 2147483647; // 2^31 - 1, which should be supported by any sensible definition of "long"
canvas.width = n;
canvas.height = n;
@assert canvas.width === n;
@assert canvas.height === n;
# expected: size 2147483647 2147483647 # not a good idea to generate the expected image in this case...
- name: initial.colour
desc: Initial state is transparent black
testing:
- initial.colour
notes: |
Output should be transparent black (not transparent anything-else), but manual
verification can only confirm that it's transparent - it's not possible to make
the actual blackness visible.
code: |
@assert pixel 20,20 == 0,0,0,0;
expected: size 100 50 # transparent
- name: initial.reset.different
desc: Changing size resets canvas to transparent black
testing:
- initial.reset
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 50, 50);
@assert pixel 20,20 == 255,0,0,255;
canvas.width = 50;
@assert pixel 20,20 == 0,0,0,0;
expected: size 50 50 # transparent
- name: initial.reset.same
desc: Setting size (not changing the value) resets canvas to transparent black
testing:
- initial.reset
code: |
canvas.width = 100;
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 50, 50);
@assert pixel 20,20 == 255,0,0,255;
canvas.width = 100;
@assert pixel 20,20 == 0,0,0,0;
expected: size 100 50 # transparent
- name: initial.reset.path
desc: Resetting the canvas state resets the current path
testing:
- initial.reset
code: |
canvas.width = 100;
ctx.rect(0, 0, 100, 50);
canvas.width = 100;
ctx.fillStyle = '#f00';
ctx.fill();
@assert pixel 20,20 == 0,0,0,0;
expected: size 100 50 # transparent
- name: initial.reset.clip
desc: Resetting the canvas state resets the current clip region
testing:
- initial.reset
code: |
canvas.width = 100;
ctx.rect(0, 0, 1, 1);
ctx.clip();
canvas.width = 100;
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
@assert pixel 20,20 == 0,255,0,255;
expected: green
- name: initial.reset.transform
desc: Resetting the canvas state resets the current transformation matrix
testing:
- initial.reset
code: |
canvas.width = 100;
ctx.scale(0.1, 0.1);
canvas.width = 100;
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
@assert pixel 20,20 == 0,255,0,255;
expected: green
- name: initial.reset.gradient
desc: Resetting the canvas state does not invalidate any existing gradients
testing:
- initial.reset
code: |
canvas.width = 50;
var g = ctx.createLinearGradient(0, 0, 100, 0);
g.addColorStop(0, '#0f0');
g.addColorStop(1, '#0f0');
canvas.width = 100;
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.fillStyle = g;
ctx.fillRect(0, 0, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
expected: green
- name: initial.reset.pattern
desc: Resetting the canvas state does not invalidate any existing patterns
testing:
- initial.reset
code: |
canvas.width = 30;
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 30, 50);
var p = ctx.createPattern(canvas, 'repeat-x');
canvas.width = 100;
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.fillStyle = p;
ctx.fillRect(0, 0, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
expected: green
# See tests2d.yaml for initial.reset.2dstate
- name: context.emptystring
desc: getContext with empty string returns null
testing:
- context.unrecognised
code: |
@assert canvas.getContext("") === null;
- name: context.unrecognised.badname
desc: getContext with unrecognised context name returns null
testing:
- context.unrecognised
code: |
@assert canvas.getContext('This is not an implemented context in any real browser') === null;
- name: context.unrecognised.badsuffix
desc: Context name "2d" plus a suffix is unrecognised
testing:
- context.unrecognised
code: |
@assert canvas.getContext("2d#") === null;
- name: context.unrecognised.nullsuffix
desc: Context name "2d" plus a "\0" suffix is unrecognised
testing:
- context.unrecognised
code: |
@assert canvas.getContext("2d\0") === null;
- name: context.unrecognised.unicode
desc: Context name which kind of looks like "2d" is unrecognised
testing:
- context.unrecognised
code: |
@assert canvas.getContext("2\uFF44") === null; // Fullwidth Latin Small Letter D
- name: context.casesensitive
desc: Context name "2D" is unrecognised; matching is case sensitive
testing:
- context.unrecognised
code: |
@assert canvas.getContext('2D') === null;
- name: context.arguments.missing
notes: *bindings
testing:
- canvas.getContext
code: |
@assert throws TypeError canvas.getContext(); @moz-todo
- name: toBlob.png
desc: toBlob with image/png returns a PNG Blob
testing:
- toBlob.png
code: |
canvas.toBlob(function(data){
@assert data.type === "image/png";
}, 'image/png');
- name: toBlob.jpeg
desc: toBlob with image/jpeg returns a JPEG Blob
testing:
- toBlob.jpeg
code: |
canvas.toBlob(function(data){
@assert data.type === "image/jpeg";
}, 'image/jpeg');
- name: toDataURL.default
desc: toDataURL with no arguments returns a PNG
testing:
- toDataURL.noarguments
code: |
var data = canvas.toDataURL();
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.png
desc: toDataURL with image/png returns a PNG
testing:
- toDataURL.png
- toDataURL.witharguments
code: |
var data = canvas.toDataURL('image/png');
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.jpg
desc: toDataURL with image/jpg is invalid type hence returns a PNG
testing:
- toDataURL.jpg
- toDataURL.witharguments
code: |
var data = canvas.toDataURL('image/jpg');
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.bogustype
desc: toDataURL with a syntactically invalid type returns a PNG
testing:
- toDataURL.unrecognised
code: |
var data = canvas.toDataURL('bogus');
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.unrecognised
desc: toDataURL with an unhandled type returns a PNG
testing:
- toDataURL.unrecognised
code: |
var data = canvas.toDataURL('image/example');
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.lowercase.ascii
desc: toDataURL type is case-insensitive
testing:
- toDataURL.lowercase
code: |
var data = canvas.toDataURL('ImAgE/PnG');
@assert data =~ /^data:image\/png[;,]/;
// If JPEG is supported at all, it must be supported case-insensitively
data = canvas.toDataURL('image/jpeg');
if (data.match(/^data:image\/jpeg[;,]/)) {
data = canvas.toDataURL('ImAgE/JpEg');
@assert data =~ /^data:image\/jpeg[;,]/;
}
- name: toDataURL.lowercase.unicode
desc: toDataURL type is ASCII-case-insensitive
testing:
- toDataURL.lowercase
code: |
// Use LATIN CAPITAL LETTER I WITH DOT ABOVE (Unicode lowercase is "i")
var data = canvas.toDataURL('\u0130mage/png');
@assert data =~ /^data:image\/png[;,]/;
var data = canvas.toDataURL('\u0130mage/jpeg');
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.arguments.1
desc: toDataURL ignores extra arguments
testing:
- toDataURL.arguments
code: |
var data = canvas.toDataURL('image/png', 'another argument that should not raise an exception');
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.arguments.2
desc: toDataURL ignores extra arguments
testing:
- toDataURL.arguments
code: |
var data = canvas.toDataURL('image/png', 'another argument that should not raise an exception', 'and another');
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.arguments.3
desc: toDataURL ignores extra arguments
testing:
- toDataURL.arguments
code: |
// More arguments that should not raise exceptions
var data = canvas.toDataURL('image/png', null, null, null);
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.nocontext
desc: toDataURL works before any context has been got
testing:
- toDataURL.noarguments
code: |
var canvas2 = document.createElement('canvas');
var data = canvas2.toDataURL();
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.zerosize
desc: toDataURL on zero-size canvas returns 'data:,'
testing:
- toDataURL.zerosize
canvas: width="0" height="0"
code: |
var data = canvas.toDataURL();
@assert data === 'data:,';
- name: toDataURL.zerowidth
desc: toDataURL on zero-size canvas returns 'data:,'
testing:
- toDataURL.zerosize
canvas: width="0"
code: |
var data = canvas.toDataURL();
@assert data === 'data:,';
- name: toDataURL.zeroheight
desc: toDataURL on zero-size canvas returns 'data:,'
testing:
- toDataURL.zerosize
canvas: height="0"
code: |
var data = canvas.toDataURL();
@assert data === 'data:,';
- name: toDataURL.large1
DISABLED: just testing implementation limits, and tends to crash
canvas: width="30000" height="1"
code: |
var data = canvas.toDataURL();
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.large2
DISABLED: just testing implementation limits, and tends to crash
canvas: width="32767" height="1"
code: |
var data = canvas.toDataURL();
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.large3
DISABLED: just testing implementation limits, and tends to crash
canvas: width="32768" height="1"
code: |
var data = canvas.toDataURL();
@assert data =~ /^data:image\/png[;,]/;
- name: toDataURL.png.primarycolours
desc: toDataURL with PNG handles simple colours correctly
testing:
- toDataURL.png
code: |
ctx.fillStyle = '#ff0';
ctx.fillRect(0, 0, 25, 40);
ctx.fillStyle = '#0ff';
ctx.fillRect(25, 0, 50, 40);
ctx.fillStyle = '#00f';
ctx.fillRect(75, 0, 25, 40);
ctx.fillStyle = '#fff';
ctx.fillRect(0, 40, 100, 10);
var data = canvas.toDataURL();
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
var img = new Image();
deferTest();
img.onload = t.step_func_done(function ()
{
ctx.drawImage(img, 0, 0);
@assert pixel 12,20 == 255,255,0,255;
@assert pixel 50,20 == 0,255,255,255;
@assert pixel 87,20 == 0,0,255,255;
@assert pixel 50,45 == 255,255,255,255;
});
img.src = data;
expected: |
size 100 50
cr.set_source_rgb(1, 1, 0)
cr.rectangle(0, 0, 25, 40)
cr.fill()
cr.set_source_rgb(0, 1, 1)
cr.rectangle(25, 0, 50, 40)
cr.fill()
cr.set_source_rgb(0, 0, 1)
cr.rectangle(75, 0, 25, 40)
cr.fill()
cr.set_source_rgb(1, 1, 1)
cr.rectangle(0, 40, 100, 10)
cr.fill()
- name: toDataURL.png.complexcolours
desc: toDataURL with PNG handles non-primary and non-solid colours correctly
testing:
- toDataURL.png
code: |
// (These values are chosen to survive relatively alright through being premultiplied)
ctx.fillStyle = 'rgba(1, 3, 254, 1)';
ctx.fillRect(0, 0, 25, 25);
ctx.fillStyle = 'rgba(8, 252, 248, 0.75)';
ctx.fillRect(25, 0, 25, 25);
ctx.fillStyle = 'rgba(6, 10, 250, 0.502)';
ctx.fillRect(50, 0, 25, 25);
ctx.fillStyle = 'rgba(12, 16, 244, 0.25)';
ctx.fillRect(75, 0, 25, 25);
var img = new Image();
deferTest();
img.onload = t.step_func_done(function ()
{
ctx.drawImage(img, 0, 25);
// (The alpha values do not really survive float->int conversion, so just
// do approximate comparisons)
@assert pixel 12,40 == 1,3,254,255;
@assert pixel 37,40 ==~ 8,252,248,191 +/- 2;
@assert pixel 62,40 ==~ 6,10,250,127 +/- 4;
@assert pixel 87,40 ==~ 12,16,244,63 +/- 8;
});
img.src = canvas.toDataURL();
expected: |
size 100 50
cr.set_source_rgba(1/255., 3/255., 254/255., 1)
cr.rectangle(0, 0, 25, 50)
cr.fill()
cr.set_source_rgba(8/255., 252/255., 248/255., 191/255.)
cr.rectangle(25, 0, 25, 50)
cr.fill()
cr.set_source_rgba(6/255., 10/255., 250/255., 127/255.)
cr.rectangle(50, 0, 25, 50)
cr.fill()
cr.set_source_rgba(12/255., 16/255., 244/255., 63/255.)
cr.rectangle(75, 0, 25, 50)
cr.fill()
- name: toDataURL.jpeg.primarycolours
desc: toDataURL with JPEG handles simple colours correctly
testing:
- toDataURL.jpeg
code: |
ctx.fillStyle = '#ff0';
ctx.fillRect(0, 0, 25, 40);
ctx.fillStyle = '#0ff';
ctx.fillRect(25, 0, 50, 40);
ctx.fillStyle = '#00f';
ctx.fillRect(75, 0, 25, 40);
ctx.fillStyle = '#fff';
ctx.fillRect(0, 40, 100, 10);
var data = canvas.toDataURL('image/jpeg'); // it is okay if this returns a PNG instead
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
var img = new Image();
deferTest();
img.onload = t.step_func_done(function ()
{
ctx.drawImage(img, 0, 0);
@assert pixel 12,20 ==~ 255,255,0,255 +/- 8;
@assert pixel 50,20 ==~ 0,255,255,255 +/- 8;
@assert pixel 87,20 ==~ 0,0,255,255 +/- 8;
@assert pixel 50,45 ==~ 255,255,255,255 +/- 8;
});
img.src = data;
expected: |
size 100 50
cr.set_source_rgb(1, 1, 0)
cr.rectangle(0, 0, 25, 40)
cr.fill()
cr.set_source_rgb(0, 1, 1)
cr.rectangle(25, 0, 50, 40)
cr.fill()
cr.set_source_rgb(0, 0, 1)
cr.rectangle(75, 0, 25, 40)
cr.fill()
cr.set_source_rgb(1, 1, 1)
cr.rectangle(0, 40, 100, 10)
cr.fill()
- name: toDataURL.jpeg.alpha
desc: toDataURL with JPEG composites onto black
testing:
- toDataURL.jpeg
- toDataURL.noalpha
code: |
ctx.fillStyle = 'rgba(128, 255, 128, 0.5)';
ctx.fillRect(0, 0, 100, 50);
ctx.globalCompositeOperation = 'destination-over'; // should be ignored by toDataURL
var data = canvas.toDataURL('image/jpeg');
ctx.globalCompositeOperation = 'source-over';
if (!data.match(/^data:image\/jpeg[;,]/)) {
@assert true;
} else {
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
var img = new Image();
deferTest();
img.onload = t.step_func_done(function ()
{
ctx.drawImage(img, 0, 0);
@assert pixel 50,25 ==~ 63,127,63,255 +/- 8;
});
img.src = data;
}
expected: |
size 100 50
cr.set_source_rgb(0.25, 0.5, 0.25)
cr.rectangle(0, 0, 100, 50)
cr.fill()
- name: toDataURL.jpeg.quality.basic
desc: toDataURL with JPEG uses the quality parameter
testing:
- toDataURL.jpeg.quality
mozilla: { throws }
code: |
ctx.fillStyle = '#00f';
ctx.fillRect(0, 0, 100, 50);
ctx.fillStyle = '#0ff';
ctx.fillRect(0, 3, 100, 1);
// Check for JPEG support first
var data = canvas.toDataURL('image/jpeg');
if (!data.match(/^data:image\/jpeg[;,]/)) {
@assert true;
} else {
var data_hi = canvas.toDataURL('image/jpeg', 0.99);
var data_lo = canvas.toDataURL('image/jpeg', 0.01);
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
deferTest();
var img_hi = new Image();
img_hi.onload = function ()
{
var img_lo = new Image();
img_lo.onload = t.step_func_done(function ()
{
ctx.drawImage(img_hi, 0, 0, 50, 50, 0, 0, 50, 50);
ctx.drawImage(img_lo, 0, 0, 50, 50, 50, 0, 50, 50);
@assert data_hi.length > data_lo.length;
@assert pixel 25,25 ==~ 0,0,255,255 +/- 8;
@assert pixel 75,25 ==~ 0,0,255,255 +/- 32;
});
img_lo.src = data_lo;
};
img_hi.src = data_hi;
}
expected: |
size 100 50
cr.set_source_rgb(0, 0, 1)
cr.rectangle(0, 0, 100, 50)
cr.fill()
cr.set_source_rgb(0, 1, 1)
cr.rectangle(0, 3, 100, 1)
cr.fill()
- name: toDataURL.jpeg.quality.notnumber
desc: toDataURL with JPEG handles non-numeric quality parameters
testing:
- toDataURL.jpeg.nan
code: |
ctx.fillStyle = '#00f';
ctx.fillRect(0, 0, 100, 50);
ctx.fillStyle = '#0ff';
ctx.fillRect(0, 3, 100, 1);
// Check for JPEG support first
var data = canvas.toDataURL('image/jpeg');
if (!data.match(/^data:image\/jpeg[;,]/)) {
@assert true;
} else {
@assert canvas.toDataURL('image/jpeg', 'bogus') === data;
@assert canvas.toDataURL('image/jpeg', {}) === data;
@assert canvas.toDataURL('image/jpeg', null) === data;
@assert canvas.toDataURL('image/jpeg', undefined) === data;
@assert canvas.toDataURL('image/jpeg', true) === data;
@assert canvas.toDataURL('image/jpeg', '0.01') === data;
}
- name: toDataURL.jpeg.quality.outsiderange
desc: toDataURL with JPEG handles out-of-range quality parameters
testing:
- toDataURL.jpeg.range
code: |
ctx.fillStyle = '#00f';
ctx.fillRect(0, 0, 100, 50);
ctx.fillStyle = '#0ff';
ctx.fillRect(0, 3, 100, 1);
// Check for JPEG support first
var data = canvas.toDataURL('image/jpeg');
if (!data.match(/^data:image\/jpeg[;,]/)) {
@assert true;
} else {
@assert canvas.toDataURL('image/jpeg', 10) === data;
@assert canvas.toDataURL('image/jpeg', -10) === data;
@assert canvas.toDataURL('image/jpeg', 1.01) === data;
@assert canvas.toDataURL('image/jpeg', -0.01) === data;
@assert canvas.toDataURL('image/jpeg', 1).length >= canvas.toDataURL('image/jpeg', 0.9).length;
@assert canvas.toDataURL('image/jpeg', 0).length <= canvas.toDataURL('image/jpeg', 0.1).length;
}
# TODO: work out what security exception should be thrown
# TODO: test same-origin vs same-host
- name: security.drawImage.image
desc: drawImage of different-origin image makes the canvas origin-unclean
mozilla: { disabled } # relies on external resources
testing:
- security.drawImage.image
- security.toDataURL
- security.getImageData
scripts:
- /common/get-host-info.sub.js
- data:text/javascript,addCrossOriginYellowImage()
code: |
ctx.drawImage(document.getElementById('yellow.png'), 0, 0);
@assert throws SECURITY_ERR canvas.toDataURL();
@assert throws SECURITY_ERR ctx.getImageData(0, 0, 1, 1);
- name: security.drawImage.canvas
desc: drawImage of unclean canvas makes the canvas origin-unclean
mozilla: { disabled } # relies on external resources
testing:
- security.drawImage.canvas
scripts:
- /common/get-host-info.sub.js
- data:text/javascript,addCrossOriginYellowImage()
code: |
var canvas2 = document.createElement('canvas');
canvas2.width = 100;
canvas2.height = 50;
var ctx2 = canvas2.getContext('2d');
ctx2.drawImage(document.getElementById('yellow.png'), 0, 0);
ctx.drawImage(canvas2, 0, 0);
@assert throws SECURITY_ERR canvas.toDataURL();
@assert throws SECURITY_ERR ctx.getImageData(0, 0, 1, 1);
- name: security.pattern.create
desc: Creating an unclean pattern does not make the canvas origin-unclean
mozilla: { disabled } # relies on external resources
testing:
- security.start
scripts:
- /common/get-host-info.sub.js
- data:text/javascript,addCrossOriginYellowImage()
code: |
var p = ctx.createPattern(document.getElementById('yellow.png'), 'repeat');
canvas.toDataURL();
ctx.getImageData(0, 0, 1, 1);
@assert true; // okay if there was no exception
- name: security.pattern.cross
desc: Using an unclean pattern makes the target canvas origin-unclean, not the pattern canvas
mozilla: { disabled } # relies on external resources
testing:
- security.start
scripts:
- /common/get-host-info.sub.js
- data:text/javascript,addCrossOriginYellowImage()
code: |
var canvas2 = document.createElement('canvas');
canvas2.width = 100;
canvas2.height = 50;
var ctx2 = canvas2.getContext('2d');
var p = ctx2.createPattern(document.getElementById('yellow.png'), 'repeat');
ctx.fillStyle = p;
ctx.fillRect(0, 0, 100, 50);
@assert throws SECURITY_ERR canvas.toDataURL();
@assert throws SECURITY_ERR ctx.getImageData(0, 0, 1, 1);
canvas2.toDataURL();
ctx2.getImageData(0, 0, 1, 1);
- name: security.pattern.canvas.timing
desc: Pattern safety depends on whether the source was origin-clean, not on whether it still is clean
notes: Disagrees with spec on "is" vs "was"
mozilla: { disabled } # relies on external resources
testing:
- security.start
- security.fillStyle.canvas
scripts:
- /common/get-host-info.sub.js
- data:text/javascript,addCrossOriginYellowImage()
code: |
var canvas2 = document.createElement('canvas');
canvas2.width = 100;
canvas2.height = 50;
var ctx2 = canvas2.getContext('2d');
ctx2.fillStyle = '#0f0';
ctx2.fillRect(0, 0, 100, 50);
var p = ctx.createPattern(canvas2, 'repeat');
ctx2.drawImage(document.getElementById('yellow.png'), 0, 0); // make canvas2 origin-unclean
ctx.fillStyle = p;
ctx.fillRect(0, 0, 100, 50);
canvas.toDataURL();
ctx.getImageData(0, 0, 1, 1);
@assert true; // okay if there was no exception
- name: security.pattern.image.fillStyle
desc: Setting fillStyle to a pattern of a different-origin image makes the canvas origin-unclean
mozilla: { disabled } # relies on external resources
testing:
- security.fillStyle.image
scripts:
- /common/get-host-info.sub.js
- data:text/javascript,addCrossOriginYellowImage()
code: |
var p = ctx.createPattern(document.getElementById('yellow.png'), 'repeat');
ctx.fillStyle = p;
ctx.fillStyle = 'red';
@assert throws SECURITY_ERR canvas.toDataURL();
@assert throws SECURITY_ERR ctx.getImageData(0, 0, 1, 1);
- name: security.pattern.canvas.fillStyle
desc: Setting fillStyle to a pattern of an unclean canvas makes the canvas origin-unclean
mozilla: { bug: 354127, disabled } # relies on external resources
testing:
- security.fillStyle.canvas
scripts:
- /common/get-host-info.sub.js
- data:text/javascript,addCrossOriginYellowImage()
code: |
var canvas2 = document.createElement('canvas');
canvas2.width = 100;
canvas2.height = 50;
var ctx2 = canvas2.getContext('2d');
ctx2.drawImage(document.getElementById('yellow.png'), 0, 0);
var p = ctx.createPattern(canvas2, 'repeat');
ctx.fillStyle = p;
ctx.fillStyle = 'red';
@assert throws SECURITY_ERR canvas.toDataURL();
@assert throws SECURITY_ERR ctx.getImageData(0, 0, 1, 1);
- name: security.pattern.image.strokeStyle
desc: Setting strokeStyle to a pattern of a different-origin image makes the canvas origin-unclean
mozilla: { disabled } # relies on external resources
testing:
- security.strokeStyle.image
scripts:
- /common/get-host-info.sub.js
- data:text/javascript,addCrossOriginYellowImage()
code: |
var p = ctx.createPattern(document.getElementById('yellow.png'), 'repeat');
ctx.strokeStyle = p;
ctx.strokeStyle = 'red';
@assert throws SECURITY_ERR canvas.toDataURL();
@assert throws SECURITY_ERR ctx.getImageData(0, 0, 1, 1);
- name: security.pattern.canvas.strokeStyle
desc: Setting strokeStyle to a pattern of an unclean canvas makes the canvas origin-unclean
mozilla: { bug: 354127, disabled } # relies on external resources
testing:
- security.strokeStyle.canvas
scripts:
- /common/get-host-info.sub.js
- data:text/javascript,addCrossOriginYellowImage()
code: |
var canvas2 = document.createElement('canvas');
canvas2.width = 100;
canvas2.height = 50;
var ctx2 = canvas2.getContext('2d');
ctx2.drawImage(document.getElementById('yellow.png'), 0, 0);
var p = ctx.createPattern(canvas2, 'repeat');
ctx.strokeStyle = p;
ctx.strokeStyle = 'red';
@assert throws SECURITY_ERR canvas.toDataURL();
@assert throws SECURITY_ERR ctx.getImageData(0, 0, 1, 1);
- name: security.dataURI
desc: 'data: URIs do not count as different-origin, and do not taint the canvas'
mozilla: { disabled, bug: 417836 } # can't do "todo" so just disable it
code: |
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
var data = canvas.toDataURL();
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
var img = new Image();
deferTest();
img.onload = t.step_func_done(function ()
{
ctx.drawImage(img, 0, 0);
canvas.toDataURL(); // should be permitted
@assert pixel 50,25 == 0,255,0,255;
});
img.src = data;
expected: green
- name: security.reset
desc: Resetting the canvas state does not reset the origin-clean flag
mozilla: { disabled } # relies on external resources
testing:
- initial.reset
scripts:
- /common/get-host-info.sub.js
- data:text/javascript,addCrossOriginYellowImage()
code: |
canvas.width = 50;
ctx.drawImage(document.getElementById('yellow.png'), 0, 0);
@assert throws SECURITY_ERR canvas.toDataURL();
canvas.width = 100;
@assert throws SECURITY_ERR canvas.toDataURL();
|