content/canvas/test/test_toDataURL_parameters.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/canvas/test/test_toDataURL_parameters.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,54 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<title>Canvas test: toDataURL parameters (Bug 564388)</title>
     1.6 +<script src="/tests/SimpleTest/SimpleTest.js"></script>
     1.7 +<link rel="stylesheet" href="/tests/SimpleTest/test.css">
     1.8 +<body>
     1.9 +<p>
    1.10 +This test covers the JPEG quality parameter. If (when) the HTML5 spec changes the
    1.11 +allowed parameters for ToDataURL, new tests should go here.
    1.12 +</p>
    1.13 +<canvas id="c" width="100" height="100"><p class="fallback">FAIL (fallback content)</p></canvas>
    1.14 +<script>
    1.15 +var canvas = document.getElementById('c');
    1.16 +var ctx = canvas.getContext("2d");
    1.17 +
    1.18 +ctx.strokeStyle = '#FF0000';
    1.19 +ctx.fillStyle = '#00FF00';
    1.20 +ctx.fillRect(0, 0, 100, 100);
    1.21 +ctx.beginPath();
    1.22 +ctx.moveTo(10, 10);
    1.23 +ctx.lineTo(90, 90);
    1.24 +ctx.stroke();
    1.25 +
    1.26 +var pngData = canvas.toDataURL('image/png');
    1.27 +var pngQuality = canvas.toDataURL('image/png', 0.1);
    1.28 +is(pngQuality, pngData, "Quality is not supported for PNG images");
    1.29 +
    1.30 +var data = canvas.toDataURL('image/jpeg');
    1.31 +if (data.match(/^data:image\/jpeg[;,]/)) {
    1.32 +    // Test the JPEG quality parameter
    1.33 +    
    1.34 +    var fullQuality = canvas.toDataURL('image/jpeg', 1.0);
    1.35 +    var lowQuality = canvas.toDataURL('image/jpeg', 0.1);
    1.36 +    isnot(lowQuality, fullQuality, "A low quality (0.1) should differ from high quality (1.0)");
    1.37 +    
    1.38 +    var medQuality = canvas.toDataURL('image/jpeg', 0.5);
    1.39 +    isnot(medQuality, fullQuality, "A medium quality (0.5) should differ from high (1.0)");
    1.40 +    isnot(medQuality, lowQuality, "A medium quality (0.5) should differ from low (0.5)");
    1.41 +    
    1.42 +    var tooHigh = canvas.toDataURL('image/jpeg', 2.0);
    1.43 +    is(tooHigh, data, "Quality above 1.0 is treated as unspecified");
    1.44 +    
    1.45 +    var tooLow = canvas.toDataURL('image/jpeg', -1.0);
    1.46 +    is(tooLow, data, "Quality below 0.0 is treated as unspecified");
    1.47 +    
    1.48 +    var lowQualityExtra = canvas.toDataURL('image/jpeg', 0.1, 'foo', 'bar', null);
    1.49 +    is(lowQualityExtra, lowQuality, "Quality applies even if extra arguments are present");
    1.50 +    
    1.51 +    var lowQualityUppercase = canvas.toDataURL('IMAGE/JPEG', 0.1);
    1.52 +    is(lowQualityUppercase, lowQuality, "Quality applies to image/jpeg regardless of case");
    1.53 +    
    1.54 +    var lowQualityString = canvas.toDataURL('image/jpeg', '0.1');
    1.55 +    isnot(lowQualityString, lowQuality, "Quality must be a number (should not be a string)");
    1.56 +}
    1.57 +</script>

mercurial