2 <title>Canvas test: toDataURL parameters (Bug
564388)
</title>
3 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
4 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css">
7 This test covers the JPEG and webp quality parameter. If (when) the HTML5 spec changes the
8 allowed parameters for ToDataURL, new tests should go here.
10 <canvas id=
"c" width=
"100" height=
"100"><p class=
"fallback">FAIL (fallback content)
</p></canvas>
12 let canvas
= document
.getElementById('c');
13 let ctx
= canvas
.getContext("2d");
15 ctx
.strokeStyle
= '#FF0000';
16 ctx
.fillStyle
= '#00FF00';
17 ctx
.fillRect(0, 0, 100, 100);
23 let pngData
= canvas
.toDataURL('image/png');
24 let pngQuality
= canvas
.toDataURL('image/png', 0.1);
25 is(pngQuality
, pngData
, "Quality is not supported for PNG images");
27 function checkType(imagetype
) {
28 let imageTypeString
= 'image/' + imagetype
;
29 let imageTypeUpperString
= 'IMAGE/' + imagetype
.toUpperCase();
30 let data
= canvas
.toDataURL(imageTypeString
);
31 if (data
.match(new RegExp('^data:image\/' + imagetype
+ '[;,]'))) {
32 // Test the JPEG/wepb quality parameter
34 let fullQuality
= canvas
.toDataURL(imageTypeString
, 1.0);
35 let lowQuality
= canvas
.toDataURL(imageTypeString
, 0.1);
36 isnot(lowQuality
, fullQuality
, "A low quality (0.1) should differ from high quality (1.0) " + imageTypeString
);
38 let medQuality
= canvas
.toDataURL(imageTypeString
, 0.5);
39 isnot(medQuality
, fullQuality
, "A medium quality (0.5) should differ from high (1.0) " + imageTypeString
);
40 isnot(medQuality
, lowQuality
, "A medium quality (0.5) should differ from low (0.5) " + imageTypeString
);
42 let tooHigh
= canvas
.toDataURL(imageTypeString
, 2.0);
43 is(tooHigh
, data
, "Quality above 1.0 is treated as unspecified " + imageTypeString
);
45 let tooLow
= canvas
.toDataURL(imageTypeString
, -1.0);
46 is(tooLow
, data
, "Quality below 0.0 is treated as unspecified " + imageTypeString
);
48 let lowQualityExtra
= canvas
.toDataURL(imageTypeString
, 0.1, 'foo', 'bar', null);
49 is(lowQualityExtra
, lowQuality
, "Quality applies even if extra arguments are present " + imageTypeString
);
51 let lowQualityUppercase
= canvas
.toDataURL(imageTypeUpperString
, 0.1);
52 is(lowQualityUppercase
, lowQuality
, "Quality applies to " + imageTypeString
+ " regardless of case");
54 let lowQualityString
= canvas
.toDataURL(imageTypeString
, '0.1');
55 isnot(lowQualityString
, lowQuality
, "Quality must be a number (should not be a string) " + imageTypeString
);
57 ok(false, "should get a data url " + imageTypeString
);