tor-browser

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

test_imagebitmap_on_worker.html (5063B)


      1 <!DOCTYPE HTML>
      2 <title>Test ImageBitmap on Worker</title>
      3 <meta charset="utf-8">
      4 <script src="/tests/SimpleTest/SimpleTest.js"></script>
      5 <link rel="stylesheet" href="/tests/SimpleTest/test.css">
      6 <body>
      7 <script type="text/javascript">
      8 
      9 // The following tests is not enabled in Worker now:
     10 // create from a HTMLImageElement
     11 // create from a HTMLVideoElement
     12 // create from a HTMLCanvasElement
     13 // create from a CanvasRenderingContext2D
     14 // call CanvasRenderingContext2D.drawImage()
     15 // call CanvasRenderingContext2D.createPaattern()
     16 // test security error from an unclean HTHMLImageElemnt
     17 // test security error from an unclean HTHMLVideoElemnt
     18 // test security error from an tainted HTHMLCanvasElemnt
     19 // test security error from an tainted CanvasRenderingContext2D
     20 
     21 // Task constructor function
     22 function Task(aType, aWidth, aHeight, aMsg, aSource) {
     23  this.type = aType;
     24  this.width = aWidth;
     25  this.height = aHeight;
     26  this.msg = aMsg;
     27  this.source = aSource;
     28 }
     29 
     30 function TaskWithCrop(aType, aWidth, aHeight, aMsg, aSource, aSx, aSy, aSw, aSh) {
     31  Task.call(this, aType, aWidth, aHeight, aMsg, aSource);
     32  this.sx = aSx;
     33  this.sy = aSy;
     34  this.sw = aSw;
     35  this.sh = aSh;
     36 }
     37 TaskWithCrop.prototype = Object.create(Task.prototype);
     38 TaskWithCrop.prototype.constructor = TaskWithCrop;
     39 
     40 var WORKER_TASKS = {
     41  tasks: [], // an arrayf of Task objects
     42  dispatch() {
     43    if (this.tasks.length) {
     44      worker.postMessage(this.tasks.pop());
     45    } else {
     46      worker.terminate();
     47      SimpleTest.finish();
     48    }
     49  },
     50 };
     51 
     52 var worker = new Worker("imagebitmap_on_worker.js");
     53 worker.onmessage = function(event) {
     54  if (event.data.type == "status") {
     55    ok(event.data.status, event.data.msg);
     56  } else if (event.data.type == "doneTask") {
     57    WORKER_TASKS.dispatch();
     58  }
     59 }
     60 
     61 function runTests() {
     62  ok(worker, "Worker created successfully.");
     63 
     64  // prepare an ImageData object
     65  var image = document.createElement('img');
     66  var canvas = document.createElement('canvas');
     67  var ctx = canvas.getContext('2d');
     68  var imageData;
     69  image.src = "image_rgrg-256x256.png";
     70  image.onload = function() {
     71    var width = image.naturalWidth;
     72    var height = image.naturalHeight;
     73 
     74    canvas.width = image.naturalWidth;
     75    canvas.height = image.naturalHeight;
     76    ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight);
     77 
     78    imageData = ctx.getImageData(0, 0, image.naturalWidth, image.naturalHeight);
     79 
     80    // task: test soruce: an ImageData
     81    WORKER_TASKS.tasks.push(new Task("testImageData", width, height, "", imageData));
     82 
     83    // task: test soruce: an ImageBitmap
     84    WORKER_TASKS.tasks.push(new Task("testImageBitmap", width, height, "", imageData));
     85 
     86    // task: test soruce: a Blob
     87    canvas.toBlob(function(aBlob) {
     88    	WORKER_TASKS.tasks.push(new Task("testBlob", width, height, "", aBlob));
     89    });
     90  };
     91 
     92  // task: throw exception: general: sw == 0 || sh == 0
     93  WORKER_TASKS.tasks.push(new TaskWithCrop("testException", 0, 0, "createImageBitmap should throw with 0 width/height", imageData, 0, 0, 0, 0));
     94 
     95  // task: throw exception: general: source is a null
     96  WORKER_TASKS.tasks.push(new TaskWithCrop("testException", 0, 0, "createImageBitmap should throw with null source", null, 0, 0, 0, 0));
     97 
     98  // task: throw exception: ImageData: an ImageData object whose data attribute is backed by a detached buffer
     99  var neuturedImageData = function getNeuturedImageData(iData) {
    100    worker.postMessage(iData.data.buffer, [iData.data.buffer]);
    101    return iData;
    102  }(ctx.getImageData(0, 0, 50, 50));
    103  WORKER_TASKS.tasks.push(new TaskWithCrop("testException", neuturedImageData.width, neuturedImageData.height,
    104                                           "createImageBitmap should throw with neutured ImageData",
    105                                           neuturedImageData, 0, 0, neuturedImageData.width, neuturedImageData.height));
    106 
    107  // task: throw exception: Blob: a corrupted blob
    108  function getCorruptedBlob(fileName) {
    109    var xhr = new XMLHttpRequest();
    110    xhr.open("GET", fileName);
    111    xhr.responseType = "blob";//force the HTTP response, response-type header to be blob
    112    xhr.onload = function() {
    113        WORKER_TASKS.tasks.push(new Task("testException", 0, 0, "createImageBitmap should reject promise with corrupted blob", xhr.response));
    114    }
    115    xhr.send();
    116  }
    117  getCorruptedBlob("image_error-early.png");
    118 
    119  // task: throw exception: Blob: non-image file
    120  function getNonImageFile(fileName) {
    121    var xhr = new XMLHttpRequest();
    122    xhr.open("GET", fileName);
    123    xhr.responseType = "blob";//force the HTTP response, response-type header to be blob
    124    xhr.onload = function() {
    125      WORKER_TASKS.tasks.push(new Task("testException", 0, 0, "createImageBitmap should reject promise with non-image blob", xhr.response));
    126 
    127      // start to dispatch tasks to worker
    128      WORKER_TASKS.dispatch();
    129    }
    130    xhr.send();
    131  }
    132  getNonImageFile("imagebitmap_on_worker.js");
    133 
    134  // task: test bug : bug 1239300
    135  WORKER_TASKS.tasks.push(new Task("testBug1239300"));
    136 }
    137 
    138 SimpleTest.waitForExplicitFinish();
    139 addLoadEvent(runTests);
    140 
    141 </script>
    142 </body>