|
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 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 var canvas = document.getElementById('c'); |
|
13 var 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 var pngData = canvas.toDataURL('image/png'); |
|
24 var pngQuality = canvas.toDataURL('image/png', 0.1); |
|
25 is(pngQuality, pngData, "Quality is not supported for PNG images"); |
|
26 |
|
27 var data = canvas.toDataURL('image/jpeg'); |
|
28 if (data.match(/^data:image\/jpeg[;,]/)) { |
|
29 // Test the JPEG quality parameter |
|
30 |
|
31 var fullQuality = canvas.toDataURL('image/jpeg', 1.0); |
|
32 var lowQuality = canvas.toDataURL('image/jpeg', 0.1); |
|
33 isnot(lowQuality, fullQuality, "A low quality (0.1) should differ from high quality (1.0)"); |
|
34 |
|
35 var medQuality = canvas.toDataURL('image/jpeg', 0.5); |
|
36 isnot(medQuality, fullQuality, "A medium quality (0.5) should differ from high (1.0)"); |
|
37 isnot(medQuality, lowQuality, "A medium quality (0.5) should differ from low (0.5)"); |
|
38 |
|
39 var tooHigh = canvas.toDataURL('image/jpeg', 2.0); |
|
40 is(tooHigh, data, "Quality above 1.0 is treated as unspecified"); |
|
41 |
|
42 var tooLow = canvas.toDataURL('image/jpeg', -1.0); |
|
43 is(tooLow, data, "Quality below 0.0 is treated as unspecified"); |
|
44 |
|
45 var lowQualityExtra = canvas.toDataURL('image/jpeg', 0.1, 'foo', 'bar', null); |
|
46 is(lowQualityExtra, lowQuality, "Quality applies even if extra arguments are present"); |
|
47 |
|
48 var lowQualityUppercase = canvas.toDataURL('IMAGE/JPEG', 0.1); |
|
49 is(lowQualityUppercase, lowQuality, "Quality applies to image/jpeg regardless of case"); |
|
50 |
|
51 var lowQualityString = canvas.toDataURL('image/jpeg', '0.1'); |
|
52 isnot(lowQualityString, lowQuality, "Quality must be a number (should not be a string)"); |
|
53 } |
|
54 </script> |