1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/test_fileapi_slice.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,142 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<head> 1.7 +<!-- 1.8 +https://bugzilla.mozilla.org/show_bug.cgi?id=575946 1.9 +--> 1.10 + <title>Test for Bug 575946</title> 1.11 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 + <script type="text/javascript" src="fileutils.js"></script> 1.13 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.14 +</head> 1.15 + 1.16 +<body> 1.17 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=575946">Mozilla Bug 575946</a> 1.18 +<p id="display"> 1.19 + <canvas id=canvas width=1100 height=1100 hidden moz-opaque></canvas> 1.20 + <canvas id=testcanvas hidden moz-opaque></canvas> 1.21 + <input id="fileList" type="file"></input> 1.22 +</p> 1.23 +<div id="content" style="display: none"> 1.24 +</div> 1.25 + 1.26 +<pre id="test"> 1.27 +<script class="testbody" type="text/javascript"> 1.28 +const isOSXMtnLion = navigator.userAgent.indexOf("Mac OS X 10.8") != -1; 1.29 + 1.30 +if (isOSXMtnLion) { 1.31 + todo(false, "Mountain Lion doesn't like this test (bug 788999)"); 1.32 +} else { 1.33 +var fileNum = 1; 1.34 +SimpleTest.waitForExplicitFinish(); 1.35 + 1.36 +// Create files containing data we'll test with. We'll want long 1.37 +// strings to ensure they span multiple buffers while loading 1.38 + 1.39 +// Create a decent-sized image 1.40 +cx = $("canvas").getContext('2d'); 1.41 +var s = cx.canvas.width; 1.42 +var grad = cx.createLinearGradient(0, 0, s-1, s-1); 1.43 +for (i = 0; i < 0.95; i += .1) { 1.44 + grad.addColorStop(i, "white"); 1.45 + grad.addColorStop(i + .05, "black"); 1.46 +} 1.47 +grad.addColorStop(1, "white"); 1.48 +cx.fillStyle = grad; 1.49 +cx.fillRect(0, 0, s-1, s-1); 1.50 +cx.fillStyle = "rgba(200, 0, 0, 0.9)"; 1.51 +cx.fillRect(.1 * s, .1 * s, .7 * s, .7 * s); 1.52 +cx.strokeStyle = "rgba(0, 0, 130, 0.5)"; 1.53 +cx.lineWidth = .14 * s; 1.54 +cx.beginPath(); 1.55 +cx.arc(.6 * s, .6 * s, .3 * s, 0, Math.PI*2, true); 1.56 +cx.stroke(); 1.57 +cx.closePath(); 1.58 +cx.fillStyle = "rgb(0, 255, 0)"; 1.59 +cx.beginPath(); 1.60 +cx.arc(.1 * s, .8 * s, .1 * s, 0, Math.PI*2, true); 1.61 +cx.fill(); 1.62 +cx.closePath(); 1.63 + 1.64 + 1.65 +var fileData = 1.66 + atob(cx.canvas.toDataURL("image/png").substring("data:text/png;base64,".length + 1)); 1.67 +var memFile = cx.canvas.mozGetAsFile("image/png"); 1.68 +var fileFile = createFileWithData(fileData); 1.69 +var size = fileData.length; 1.70 + 1.71 +// This might fail if we dramatically improve the png encoder. If that happens 1.72 +// please increase the complexity or size of the image generated above to ensure 1.73 +// that we're testing with files that are large enough. 1.74 +ok(size > 65536, "test data sufficiently large"); 1.75 + 1.76 + 1.77 +// Test that basic properties work 1.78 +testSlice(memFile, size, "image/png", fileData, "memFile"); 1.79 +testSlice(fileFile, size, "", fileData, "fileFile"); 1.80 + 1.81 + 1.82 +// Try loading directly from slice into an image 1.83 +var testBinaryData = ""; 1.84 +for (var i = 0; i < 256; i++) { 1.85 + testBinaryData += String.fromCharCode(i); 1.86 +} 1.87 +while (testBinaryData.length < 20000) { 1.88 + testBinaryData += testBinaryData; 1.89 +} 1.90 +function imageLoadHandler(event) { 1.91 + var origcanvas = $("canvas"); 1.92 + var testcanvas = $("testcanvas"); 1.93 + var image = event.target; 1.94 + is(image.naturalWidth, origcanvas.width, "width correct"); 1.95 + is(image.naturalHeight, origcanvas.height, "height correct"); 1.96 + 1.97 + testcanvas.width = origcanvas.width; 1.98 + testcanvas.height = origcanvas.height; 1.99 + testcanvas.getContext("2d").drawImage(image, 0, 0); 1.100 + // Do not use |is(testcanvas.toDataURL("image/png"), origcanvas.toDataURL("image/png"), "...");| that results in a _very_ long line. 1.101 + var origDataURL = origcanvas.toDataURL("image/png"); 1.102 + var testDataURL = testcanvas.toDataURL("image/png"); 1.103 + is(testDataURL.length, origDataURL.length, 1.104 + "Length of correct image data"); 1.105 + ok(testDataURL == origDataURL, 1.106 + "Content of correct image data"); 1.107 + 1.108 + testHasRun(); 1.109 +} 1.110 + 1.111 +// image in the middle 1.112 +var imgfile = createFileWithData(testBinaryData + fileData + testBinaryData); 1.113 +is(imgfile.size, size + testBinaryData.length * 2, "correct file size (middle)"); 1.114 +var img = new Image; 1.115 +img.src = URL.createObjectURL(imgfile.slice(testBinaryData.length, testBinaryData.length + size)); 1.116 +img.onload = imageLoadHandler; 1.117 +expectedTestCount++; 1.118 + 1.119 +// image at start 1.120 +var imgfile = createFileWithData(fileData + testBinaryData); 1.121 +is(imgfile.size, size + testBinaryData.length, "correct file size (start)"); 1.122 +var img = new Image; 1.123 +img.src = URL.createObjectURL(imgfile.slice(0, size)); 1.124 +img.onload = imageLoadHandler; 1.125 +expectedTestCount++; 1.126 + 1.127 +// image at end 1.128 +var imgfile = createFileWithData(testBinaryData + fileData); 1.129 +is(imgfile.size, size + testBinaryData.length, "correct file size (end)"); 1.130 +var img = new Image; 1.131 +img.src = URL.createObjectURL(imgfile.slice(testBinaryData.length, testBinaryData.length + size)); 1.132 +img.onload = imageLoadHandler; 1.133 +expectedTestCount++; 1.134 + 1.135 +// image past end 1.136 +var imgfile = createFileWithData(testBinaryData + fileData); 1.137 +is(imgfile.size, size + testBinaryData.length, "correct file size (past end)"); 1.138 +var img = new Image; 1.139 +img.src = URL.createObjectURL(imgfile.slice(testBinaryData.length, testBinaryData.length + size + 1000)); 1.140 +img.onload = imageLoadHandler; 1.141 +expectedTestCount++; 1.142 +} 1.143 +</script> 1.144 +</pre> 1.145 +</body> </html>