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

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

mercurial