content/base/test/test_fileapi_slice.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <!--
michael@0 5 https://bugzilla.mozilla.org/show_bug.cgi?id=575946
michael@0 6 -->
michael@0 7 <title>Test for Bug 575946</title>
michael@0 8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <script type="text/javascript" src="fileutils.js"></script>
michael@0 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 11 </head>
michael@0 12
michael@0 13 <body>
michael@0 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=575946">Mozilla Bug 575946</a>
michael@0 15 <p id="display">
michael@0 16 <canvas id=canvas width=1100 height=1100 hidden moz-opaque></canvas>
michael@0 17 <canvas id=testcanvas hidden moz-opaque></canvas>
michael@0 18 <input id="fileList" type="file"></input>
michael@0 19 </p>
michael@0 20 <div id="content" style="display: none">
michael@0 21 </div>
michael@0 22
michael@0 23 <pre id="test">
michael@0 24 <script class="testbody" type="text/javascript">
michael@0 25 const isOSXMtnLion = navigator.userAgent.indexOf("Mac OS X 10.8") != -1;
michael@0 26
michael@0 27 if (isOSXMtnLion) {
michael@0 28 todo(false, "Mountain Lion doesn't like this test (bug 788999)");
michael@0 29 } else {
michael@0 30 var fileNum = 1;
michael@0 31 SimpleTest.waitForExplicitFinish();
michael@0 32
michael@0 33 // Create files containing data we'll test with. We'll want long
michael@0 34 // strings to ensure they span multiple buffers while loading
michael@0 35
michael@0 36 // Create a decent-sized image
michael@0 37 cx = $("canvas").getContext('2d');
michael@0 38 var s = cx.canvas.width;
michael@0 39 var grad = cx.createLinearGradient(0, 0, s-1, s-1);
michael@0 40 for (i = 0; i < 0.95; i += .1) {
michael@0 41 grad.addColorStop(i, "white");
michael@0 42 grad.addColorStop(i + .05, "black");
michael@0 43 }
michael@0 44 grad.addColorStop(1, "white");
michael@0 45 cx.fillStyle = grad;
michael@0 46 cx.fillRect(0, 0, s-1, s-1);
michael@0 47 cx.fillStyle = "rgba(200, 0, 0, 0.9)";
michael@0 48 cx.fillRect(.1 * s, .1 * s, .7 * s, .7 * s);
michael@0 49 cx.strokeStyle = "rgba(0, 0, 130, 0.5)";
michael@0 50 cx.lineWidth = .14 * s;
michael@0 51 cx.beginPath();
michael@0 52 cx.arc(.6 * s, .6 * s, .3 * s, 0, Math.PI*2, true);
michael@0 53 cx.stroke();
michael@0 54 cx.closePath();
michael@0 55 cx.fillStyle = "rgb(0, 255, 0)";
michael@0 56 cx.beginPath();
michael@0 57 cx.arc(.1 * s, .8 * s, .1 * s, 0, Math.PI*2, true);
michael@0 58 cx.fill();
michael@0 59 cx.closePath();
michael@0 60
michael@0 61
michael@0 62 var fileData =
michael@0 63 atob(cx.canvas.toDataURL("image/png").substring("data:text/png;base64,".length + 1));
michael@0 64 var memFile = cx.canvas.mozGetAsFile("image/png");
michael@0 65 var fileFile = createFileWithData(fileData);
michael@0 66 var size = fileData.length;
michael@0 67
michael@0 68 // This might fail if we dramatically improve the png encoder. If that happens
michael@0 69 // please increase the complexity or size of the image generated above to ensure
michael@0 70 // that we're testing with files that are large enough.
michael@0 71 ok(size > 65536, "test data sufficiently large");
michael@0 72
michael@0 73
michael@0 74 // Test that basic properties work
michael@0 75 testSlice(memFile, size, "image/png", fileData, "memFile");
michael@0 76 testSlice(fileFile, size, "", fileData, "fileFile");
michael@0 77
michael@0 78
michael@0 79 // Try loading directly from slice into an image
michael@0 80 var testBinaryData = "";
michael@0 81 for (var i = 0; i < 256; i++) {
michael@0 82 testBinaryData += String.fromCharCode(i);
michael@0 83 }
michael@0 84 while (testBinaryData.length < 20000) {
michael@0 85 testBinaryData += testBinaryData;
michael@0 86 }
michael@0 87 function imageLoadHandler(event) {
michael@0 88 var origcanvas = $("canvas");
michael@0 89 var testcanvas = $("testcanvas");
michael@0 90 var image = event.target;
michael@0 91 is(image.naturalWidth, origcanvas.width, "width correct");
michael@0 92 is(image.naturalHeight, origcanvas.height, "height correct");
michael@0 93
michael@0 94 testcanvas.width = origcanvas.width;
michael@0 95 testcanvas.height = origcanvas.height;
michael@0 96 testcanvas.getContext("2d").drawImage(image, 0, 0);
michael@0 97 // Do not use |is(testcanvas.toDataURL("image/png"), origcanvas.toDataURL("image/png"), "...");| that results in a _very_ long line.
michael@0 98 var origDataURL = origcanvas.toDataURL("image/png");
michael@0 99 var testDataURL = testcanvas.toDataURL("image/png");
michael@0 100 is(testDataURL.length, origDataURL.length,
michael@0 101 "Length of correct image data");
michael@0 102 ok(testDataURL == origDataURL,
michael@0 103 "Content of correct image data");
michael@0 104
michael@0 105 testHasRun();
michael@0 106 }
michael@0 107
michael@0 108 // image in the middle
michael@0 109 var imgfile = createFileWithData(testBinaryData + fileData + testBinaryData);
michael@0 110 is(imgfile.size, size + testBinaryData.length * 2, "correct file size (middle)");
michael@0 111 var img = new Image;
michael@0 112 img.src = URL.createObjectURL(imgfile.slice(testBinaryData.length, testBinaryData.length + size));
michael@0 113 img.onload = imageLoadHandler;
michael@0 114 expectedTestCount++;
michael@0 115
michael@0 116 // image at start
michael@0 117 var imgfile = createFileWithData(fileData + testBinaryData);
michael@0 118 is(imgfile.size, size + testBinaryData.length, "correct file size (start)");
michael@0 119 var img = new Image;
michael@0 120 img.src = URL.createObjectURL(imgfile.slice(0, size));
michael@0 121 img.onload = imageLoadHandler;
michael@0 122 expectedTestCount++;
michael@0 123
michael@0 124 // image at end
michael@0 125 var imgfile = createFileWithData(testBinaryData + fileData);
michael@0 126 is(imgfile.size, size + testBinaryData.length, "correct file size (end)");
michael@0 127 var img = new Image;
michael@0 128 img.src = URL.createObjectURL(imgfile.slice(testBinaryData.length, testBinaryData.length + size));
michael@0 129 img.onload = imageLoadHandler;
michael@0 130 expectedTestCount++;
michael@0 131
michael@0 132 // image past end
michael@0 133 var imgfile = createFileWithData(testBinaryData + fileData);
michael@0 134 is(imgfile.size, size + testBinaryData.length, "correct file size (past end)");
michael@0 135 var img = new Image;
michael@0 136 img.src = URL.createObjectURL(imgfile.slice(testBinaryData.length, testBinaryData.length + size + 1000));
michael@0 137 img.onload = imageLoadHandler;
michael@0 138 expectedTestCount++;
michael@0 139 }
michael@0 140 </script>
michael@0 141 </pre>
michael@0 142 </body> </html>

mercurial