test_toDataURL_parameters.html (2763B)
1 <!DOCTYPE HTML> 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"> 5 <body> 6 <p> 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. 9 </p> 10 <canvas id="c" width="100" height="100"><p class="fallback">FAIL (fallback content)</p></canvas> 11 <script> 12 let canvas = document.getElementById('c'); 13 let ctx = canvas.getContext("2d"); 14 15 ctx.strokeStyle = '#FF0000'; 16 ctx.fillStyle = '#00FF00'; 17 ctx.fillRect(0, 0, 100, 100); 18 ctx.beginPath(); 19 ctx.moveTo(10, 10); 20 ctx.lineTo(90, 90); 21 ctx.stroke(); 22 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"); 26 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 33 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); 37 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); 41 42 let tooHigh = canvas.toDataURL(imageTypeString, 2.0); 43 is(tooHigh, data, "Quality above 1.0 is treated as unspecified " + imageTypeString); 44 45 let tooLow = canvas.toDataURL(imageTypeString, -1.0); 46 is(tooLow, data, "Quality below 0.0 is treated as unspecified " + imageTypeString); 47 48 let lowQualityExtra = canvas.toDataURL(imageTypeString, 0.1, 'foo', 'bar', null); 49 is(lowQualityExtra, lowQuality, "Quality applies even if extra arguments are present " + imageTypeString); 50 51 let lowQualityUppercase = canvas.toDataURL(imageTypeUpperString, 0.1); 52 is(lowQualityUppercase, lowQuality, "Quality applies to " + imageTypeString + " regardless of case"); 53 54 let lowQualityString = canvas.toDataURL(imageTypeString, '0.1'); 55 isnot(lowQualityString, lowQuality, "Quality must be a number (should not be a string) " + imageTypeString); 56 } else { 57 ok(false, "should get a data url " + imageTypeString); 58 } 59 } 60 61 checkType('jpeg'); 62 checkType('webp'); 63 </script>