tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_imagebitmap_close.html (2778B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4 <title>WebGL in OffscreenCanvas</title>
      5 <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6 <script src="/tests/SimpleTest/WindowSnapshot.js"></script>
      7 <link rel="stylesheet" href="/tests/SimpleTest/test.css">
      8 </head>
      9 <body>
     10 <script type="text/js-worker">
     11 function ok(expect, msg) {
     12  postMessage({"type": "status", status: !!expect, msg: msg});
     13 }
     14 
     15 onmessage = function(event) {
     16  var bitmap = event.data.bitmap;
     17  ok(!!bitmap, "Get the ImageBitmap from the main script.");
     18  bitmap.close();
     19  ok(bitmap.width == 0 && bitmap.height == 0, "After close(), width and height should return 0");
     20  postMessage({"type": "finish"});
     21 }
     22 </script>
     23 <script>
     24 
     25 SimpleTest.waitForExplicitFinish();
     26 
     27 function createCanvas() {
     28  var htmlCanvas = document.createElement('canvas');
     29  htmlCanvas.width = 64;
     30  htmlCanvas.height = 64;
     31  document.body.appendChild(htmlCanvas);
     32  return htmlCanvas;
     33 }
     34 
     35 function runTest() {
     36  var canvas1 = createCanvas();
     37  var ctx = canvas1.getContext("2d");
     38  ctx.fillStyle = "#00FF00";
     39  ctx.fillRect(0, 0, 64, 64);
     40 
     41  var canvasRef = createCanvas();
     42  var ctx = canvasRef.getContext("2d");
     43  ctx.fillStyle = "#00FF00";
     44  ctx.fillRect(0, 0, 64, 64);
     45 
     46  createImageBitmap(canvas1).then(function(bmp) {
     47    var canvas2 = createCanvas();
     48    var ctx2 = canvas2.getContext("2d");
     49    ctx2.drawImage(bmp, 0, 0);
     50 
     51    ok(canvasRef.toDataURL() == canvas2.toDataURL(), "toDataURL should return same result.");
     52    document.body.removeChild(canvas2);
     53 
     54    bmp.close();
     55    ok(bmp.width == 0 && bmp.height == 0, "After close(), width and height should return 0");
     56    var canvas2 = createCanvas();
     57    var ctx2 = canvas2.getContext("2d");
     58    var beforeDrawImageDataURL = canvas2.toDataURL();
     59    var _thrown = undefined; try {
     60      ctx2.drawImage(bmp, 0, 0);
     61    } catch (e) { _thrown = e };
     62    ok(_thrown && _thrown.name == "InvalidStateError" && _thrown.code == DOMException.INVALID_STATE_ERR, "should throw InvalidStateError");
     63    runTestOnWorker();
     64  });
     65 }
     66 
     67 function runTestOnWorker() {
     68  var canvas1 = createCanvas();
     69  var ctx = canvas1.getContext("2d");
     70  ctx.fillStyle = "#00FF00";
     71  ctx.fillRect(0, 0, 64, 64);
     72 
     73  var blob = new Blob(Array.prototype.map.call(document.querySelectorAll("script[type=\"text\/js-worker\"]"), function (oScript) { return oScript.textContent; }),{type: "text/javascript"});
     74 
     75  var worker = new Worker(window.URL.createObjectURL(blob));
     76 
     77  createImageBitmap(canvas1).then(function(bmp) {
     78    worker.postMessage({bitmap: bmp}, [bmp]);
     79    worker.onmessage = function(event) {
     80      if (event.data.type == "status") {
     81        ok(event.data.status, event.data.msg);
     82      } else if (event.data.type == "finish") {
     83        SimpleTest.finish();
     84      }
     85    }
     86  });
     87 }
     88 
     89 runTest();
     90 
     91 </script>
     92 </body>
     93 </html>