DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
<!DOCTYPE HTML>
<title>Test ImageBitmap</title>
<meta charset="utf-8">
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
<body>

<img src="image_anim-gr.gif" id="image" class="resource">
<video width="320" height="240" src="http://example.com/tests/dom/canvas/test/crossorigin/video.sjs?name=tests/dom/canvas/test/320x240.ogv&type=video/ogg&cors=anonymous" id="video" crossOrigin="anonymous" autoplay></video>

<canvas id="c1" class="output" width="128" height="128"></canvas>
<canvas id="c2" width="128" height="128"></canvas>

<script src="imagebitmap_bug1239300.js"></script>
<script src="imagebitmap_bug1239752.js"></script>
<script type="text/javascript">

SimpleTest.waitForExplicitFinish();

/**
 * [isPixel description]
 * @param  {[type]}  ctx : canvas context
 * @param  {[type]}  x   : pixel x coordinate
 * @param  {[type]}  y   : pixel y coordinate
 * @param  {[type]}  c   : a rgba color code
 * @param  {[type]}  d   : error duration
 * @return {Promise}
 */
function isPixel(ctx, x, y, c, d) {
  var pos = x + "," + y;
  var color = c[0] + "," + c[1] + "," + c[2] + "," + c[3];
  var pixel = ctx.getImageData(x, y, 1, 1);
  var pr = pixel.data[0],
      pg = pixel.data[1],
      pb = pixel.data[2],
      pa = pixel.data[3];
  ok(c[0]-d <= pr && pr <= c[0]+d &&
     c[1]-d <= pg && pg <= c[1]+d &&
     c[2]-d <= pb && pb <= c[2]+d &&
     c[3]-d <= pa && pa <= c[3]+d,
     "pixel "+pos+" of "+ctx.canvas.id+" is "+pr+","+pg+","+pb+","+pa+"; expected "+color+" +/- "+d);
}

var TEST_BITMAPS = [
    {'rect': [0, 0, 128, 128],        'draw': [0, 0, 64, 64, 0, 0, 64, 64],       'test': [[0,    0,    [255, 0, 0, 255], 5]]},
    {'rect': [128, 0, 128, 128],      'draw': [0, 0, 64, 64, 0, 0, 64, 64],       'test': [[0,    0,    [0, 255, 0, 255], 5]]},
    {'rect': [230, 230, 128, 128],    'draw': [0, 0, 128, 128, 0, 0, 128, 128],   'test': [[0,    0,    [255, 0, 0, 255], 5],
                                                                                           [100,  100,  [0, 0, 0, 0],     5]]},
    {'rect': [-64, -64, 512, 512],    'draw': [0, 0, 128, 128, 0, 0, 128, 128],   'test': [[0,    0,    [0, 0, 0, 0],     5],
                                                                                           [100,  100,  [255, 0, 0, 255], 5]]},
    {'rect': [128, 128, -128, -128],  'draw': [0, 0, 128, 128, 0, 0, 128, 128],   'test': [[0,    0,    [255, 0, 0, 255], 5]]},
    {'rect': [0, 0, 256, 256],        'draw': [0, 128, 128, 128, 0, 0, 128, 128], 'test': [[0,    0,    [0, 255, 0, 255], 5]]},
];

var canvas, ctx, ctx2, completedImage;

function failed(ex) {
  ok(false, "Promise failure: " + ex);
}

function testDraw() {
  var resolver, bitmaps = [], image = new Image();

  image.src = 'image_rgrg-256x256.png';
  var promise = new Promise(function (arg) { resolver = arg; });

  function createBitmap(def) {
    return createImageBitmap(image, def.rect[0], def.rect[1], def.rect[2], def.rect[3])
      .then(function (bitmap) { def.bitmap = bitmap; }, failed);
  };

  image.onload = function() {
    completedImage = image;
    resolver(Promise.all(TEST_BITMAPS.map(createBitmap)));
  };

  function testPixel(test) {
    isPixel(ctx, test[0], test[1], test[2], test[3]);
  };

  return promise.then(function() {
    TEST_BITMAPS.forEach(function (test) {
      if (!test.bitmap) { return; }
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      ctx.drawImage(test.bitmap, test.draw[0], test.draw[1], test.draw[2], test.draw[3], test.draw[4], test.draw[5], test.draw[6], test.draw[7]);
      test.test.forEach(testPixel);
      is(test.bitmap.width, Math.abs(test.rect[2]), "Bitmap has correct width " + test.bitmap.width);
      is(test.bitmap.height, Math.abs(test.rect[3]), "Bitmap has correct height " + test.bitmap.height);
    });
  });
}

function testSources() {
  ctx.fillStyle="#00FF00";
  ctx.fillRect(0, 0, 128, 128);

  function check(bitmap) {
    ctx2.clearRect(0, 0, 128, 128);
    ctx2.drawImage(bitmap, 0, 0);
    isPixel(ctx2, 0, 0, [0, 255, 0, 255], 5);
  };

  function getPNGBlobBitmapPromise() {
    return new Promise(function(resolve, reject) {
      canvas.toBlob(function(blob) {
        resolve(createImageBitmap(blob));
      })
    });
  }

  function getJPGBlobBitmapPromise() {
    return new Promise(function(resolve, reject) {
      canvas.toBlob(function(blob) {
        resolve(createImageBitmap(blob));
      }, "image/jpeg", 0.95)
    });
  }

  return Promise.all([
    createImageBitmap(document.getElementById('image')).then(check, failed),                                         // HTMLImageElement
    createImageBitmap(ctx).then(check, failed),                                                                      // CanvasRenderingContext2D
    createImageBitmap(canvas).then(check, failed),                                                                   // HTMLCanvasElement
    createImageBitmap(ctx).then(function (bitmap) { return createImageBitmap(bitmap).then(check, failed); }, failed), // ImageBitmap
    createImageBitmap(document.getElementById('video'), 140, 0, 20, 20).then(check, failed),                         // HTMLVideoElement
    createImageBitmap(ctx.getImageData(0, 0, 128, 128)).then(check, failed),                                          // ImageData
    getPNGBlobBitmapPromise().then(check, failed),                                                                   // PNG blob
    getJPGBlobBitmapPromise().then(check, failed),                                                                   // JPEG blob
  ]);
}

function promiseThrows(p, name) {
  var didThrow;
  return p.then(function() { didThrow = false; },
                function() { didThrow = true; })
          .then(function() { ok(didThrow, name); });
}

function testExceptions() {

  function createImageBitmapWithNeuturedImageData() {
    return new Promise(function(resolve, reject) {
      var tempImage = document.createElement('img');
      tempImage.src = 'image_rgrg-256x256.png';
      tempImage.onload = function() {
        var tempCanvas = document.createElement('canvas');
        var tempCtx = tempCanvas.getContext('2d');
        tempCanvas.with = tempImage.naturalWidth;
        tempCanvas.height = tempImage.naturalHeight;
        tempCtx.drawImage(tempImage, 0, 0);
        var tempWorker = new Worker("test_imagebitmap_on_worker.js");
        var imageData = tempCtx.getImageData(0, 0, tempImage.naturalWidth, tempImage.naturalHeight);
        tempWorker.postMessage(imageData.data.buffer, [imageData.data.buffer]);
        tempWorker.terminate();

        ok(imageData.data.length == 0, "Get a neutured ImageData.");
        resolve(createImageBitmap(imageData));
      }
    });
  }

  function createImageBitmapWithCorruptedBlob() {
    return new Promise(function(resolve, reject) {
      var xhr = new XMLHttpRequest();
      xhr.open("GET", "image_error-early.png");
      xhr.responseType = "blob";//force the HTTP response, response-type header to be blob
      xhr.onload = function()
      {
        ok(xhr.response, "Get a corrupted blob");
        resolve(createImageBitmap(xhr.response));
      }
      xhr.send();
    });
  }

  function createImageBitmapWithNonImageFile() {
    return new Promise(function(resolve, reject) {
      var xhr = new XMLHttpRequest();
      xhr.open("GET", "test_imagebitmap_on_worker.js");
      xhr.responseType = "blob";//force the HTTP response, response-type header to be blob
      xhr.onload = function()
      {
        ok(xhr.response, "Get a non-image blob");
        resolve(createImageBitmap(xhr.response));
      }
      xhr.send();
    });
  }

  return Promise.all([
    promiseThrows(createImageBitmap(new Image()), 'createImageBitmap should throw with unloaded image'),
    promiseThrows(createImageBitmap(completedImage, 0, 0, 0, 0), 'createImageBitmap should throw with 0 width/height'),
    promiseThrows(createImageBitmap(null), 'createImageBitmap should throw with null source'),
    promiseThrows(createImageBitmapWithNeuturedImageData(), "createImageBitmap should throw with neutured ImageData"),
    promiseThrows(createImageBitmapWithCorruptedBlob(), "createImageBitmap should throw with corrupted blob"),
    promiseThrows(createImageBitmapWithNonImageFile(), "createImageBitmap should throw with non-image blob"),
  ]);
}

function testSecurityErrors() {

  function getUncleanImagePromise() {
    return new Promise(function(resolve, reject) {
      var uncleanImage = document.createElement('img');

      uncleanImage.onload = function() {
        resolve(createImageBitmap(uncleanImage));
      }

      uncleanImage.onerror = function() {
        reject();
      }

      uncleanImage.src = "http://example.com/tests/dom/canvas/test/crossorigin/image.png";
    });
  }

  function getUncleanVideoPromise() {
    return new Promise(function(resolve, reject) {
      var uncleanVideo = document.createElement('video');

      uncleanVideo.onloadeddata = function() {
        resolve(createImageBitmap(uncleanVideo));
      }

      uncleanVideo.onerror = function() {
        reject();
      }

      uncleanVideo.src = "http://example.com/tests/dom/canvas/test/crossorigin/video.sjs?name=tests/dom/canvas/test/320x240.ogv&type=video/ogg";
      uncleanVideo.play();
    });
  }

  function getTaintedCanvasPromise() {
    return new Promise(function(resolve, reject) {
      var uncleanImage = document.createElement('img');

      uncleanImage.onload = function() {
        var taintedCanvas = document.createElement('canvas');
        var taintedCtx = taintedCanvas.getContext('2d');
        taintedCtx.drawImage(uncleanImage, 0, 0);
        resolve(createImageBitmap(taintedCanvas));
      }

      uncleanImage.onerror = function() {
        reject();
      }

      uncleanImage.src = "http://example.com/tests/dom/canvas/test/crossorigin/image.png";
    });
  }

  function getTaintedCanvasRenderingContex2dPromise() {
    return new Promise(function(resolve, reject) {
      var uncleanImage = document.createElement('img');

      uncleanImage.onload = function() {
        var taintedCanvas = document.createElement('canvas');
        var taintedCtx = taintedCanvas.getContext('2d');
        taintedCtx.drawImage(uncleanImage, 0, 0);
        resolve(createImageBitmap(taintedCtx));
      }

      uncleanImage.onerror = function() {
        reject();
      }

      uncleanImage.src = "http://example.com/tests/dom/canvas/test/crossorigin/image.png";
    });
  }

  function checkPromiseFailedWithSecurityError(p) {
    return p.then(imageBitmap => {
      ok(!!imageBitmap, "ImageBitmaps are always created");
      const context = document.createElement("canvas").getContext("2d");
      context.drawImage(imageBitmap, 0, 0);
      try {
        context.getImageData(0, 0, 1, 1);
        ok(false, "Did not get SecurityError with unclean source. ImageBitmap was created successfully.");
      } catch (ex) {
        if (ex == "SecurityError: The operation is insecure.") {
          ok(true, ex);
        }
        else {
          ok(false, "Did not get SecurityError with unclean source. Error Message: " + ex);
        }
      }
    });
  }

  return Promise.all([
    checkPromiseFailedWithSecurityError(getUncleanImagePromise()),
    checkPromiseFailedWithSecurityError(getUncleanVideoPromise()),
    checkPromiseFailedWithSecurityError(getTaintedCanvasPromise()),
    checkPromiseFailedWithSecurityError(getTaintedCanvasRenderingContex2dPromise()),
  ]);
}

function testCreatePattern() {
  var resolve;
  var promise = new Promise(function (arg) { resolve = arg; });

  var TEST_PATTERN = [
    [0,   0,   [255, 0, 0, 255], 1],
    [128, 128, [255, 0, 0, 255], 1],
    [256, 256, [255, 0, 0, 255], 1],
    [384, 0,   [0, 255, 0, 255], 1],
    [0,   384, [0, 255, 0, 255], 1],
  ];

  var patternCanvas = document.createElement('canvas');
  patternCanvas.width = "512";
  patternCanvas.height = "512";
  var patternCtx = patternCanvas.getContext('2d');

  var image = new Image();
  image.src = 'image_rgrg-256x256.png';
  image.onload = function() {
    var p = createImageBitmap(image);
    p.then(function(bitmap) {
      patternCtx.rect(0, 0, 512, 512);
      patternCtx.fillStyle = patternCtx.createPattern(bitmap, "repeat");
      patternCtx.fill();
      document.body.appendChild(patternCanvas);
    });
    resolve(p);
  }

  return promise.then(function() {
    TEST_PATTERN.forEach(function(test) {
      isPixel(patternCtx, test[0], test[1], test[2], test[3]);
    });
  });
}


function runTests() {
  canvas = document.getElementById('c1');
  ctx = canvas.getContext('2d');
  ctx2 = document.getElementById('c2').getContext('2d');

  testDraw()
    .then(testCreatePattern)
    .then(testSources)
    .then(testExceptions)
    .then(testSecurityErrors)
    .then(testBug1239300)
    .then(testBug1239752)
    .then(SimpleTest.finish, function(ev) { failed(ev); SimpleTest.finish(); });
}

addLoadEvent(runTests);

</script>
</body>