|
1 <!DOCTYPE HTML> |
|
2 <title>Canvas test: mozGetAsFile</title> |
|
3 <script src="/tests/SimpleTest/SimpleTest.js"></script> |
|
4 <link rel="stylesheet" href="/tests/SimpleTest/test.css"> |
|
5 <body> |
|
6 <canvas id="c" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas> |
|
7 <script> |
|
8 |
|
9 function compareAsync(file, canvas, type, callback) |
|
10 { |
|
11 var reader = new FileReader(); |
|
12 reader.onload = |
|
13 function(e) { |
|
14 is(e.target.result, canvas.toDataURL(type), |
|
15 "<canvas>.mozGetAsFile().getAsDataURL() should equal <canvas>.toDataURL()"); |
|
16 callback(canvas); |
|
17 }; |
|
18 reader.readAsDataURL(file); |
|
19 } |
|
20 |
|
21 function test1(canvas) |
|
22 { |
|
23 var pngfile = canvas.mozGetAsFile("foo.png"); |
|
24 is(pngfile.type, "image/png", "Default type for mozGetAsFile should be PNG"); |
|
25 compareAsync(pngfile, canvas, "image/png", test2); |
|
26 is(pngfile.name, "foo.png", "File name should be what we passed in"); |
|
27 } |
|
28 |
|
29 function test2(canvas) |
|
30 { |
|
31 var jpegfile = canvas.mozGetAsFile("bar.jpg", "image/jpeg"); |
|
32 is(jpegfile.type, "image/jpeg", |
|
33 "When a valid type is specified that should be returned"); |
|
34 compareAsync(jpegfile, canvas, "image/jpeg", SimpleTest.finish); |
|
35 is(jpegfile.name, "bar.jpg", "File name should be what we passed in"); |
|
36 } |
|
37 |
|
38 SimpleTest.waitForExplicitFinish(); |
|
39 addLoadEvent(function () { |
|
40 |
|
41 var canvas = document.getElementById('c'); |
|
42 var ctx = canvas.getContext('2d'); |
|
43 ctx.drawImage(document.getElementById('yellow75.png'), 0, 0); |
|
44 |
|
45 test1(canvas); |
|
46 |
|
47 }); |
|
48 </script> |
|
49 <img src="image_yellow75.png" id="yellow75.png" class="resource"> |
|
50 |