<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<title>Canvas test: 2d.imageData.object.ctor.basics</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>
<link rel="stylesheet" href="/html/canvas/resources/canvas-tests.css">
<body class="show_output">

<h1>2d.imageData.object.ctor.basics</h1>
<p class="desc">Testing different type of ImageData constructor</p>


<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>

<ul id="d"></ul>
<script>
var t = async_test("Testing different type of ImageData constructor");
_addTest(function(canvas, ctx) {

  function setRGBA(imageData, i, rgba)
  {
      var s = i * 4;
      imageData[s] = rgba[0];
      imageData[s + 1] = rgba[1];
      imageData[s + 2] = rgba[2];
      imageData[s + 3] = rgba[3];
  }

  function getRGBA(imageData, i)
  {
      var result = [];
      var s = i * 4;
      for (var j = 0; j < 4; j++) {
          result[j] = imageData[s + j];
      }
      return result;
  }

  function assertArrayEquals(actual, expected)
  {
      _assertSame(typeof actual, "object", "typeof actual", "\"object\"");
      _assertDifferent(actual, null, "actual", "null");
      _assertSame("length" in actual, true, "\"length\" in actual", "true");
      _assertSame(actual.length, expected.length, "actual.length", "expected.length");
      for (var i = 0; i < actual.length; i++) {
          _assertSame(actual.hasOwnProperty(i), expected.hasOwnProperty(i), "actual.hasOwnProperty(i)", "expected.hasOwnProperty(i)");
          _assertSame(actual[i], expected[i], "actual[\""+(i)+"\"]", "expected[\""+(i)+"\"]");
      }
  }

  _assertDifferent(ImageData, undefined, "ImageData", "undefined");
  imageData = new ImageData(100, 50);

  _assertDifferent(imageData, null, "imageData", "null");
  _assertDifferent(imageData.data, null, "imageData.data", "null");
  _assertSame(imageData.width, 100, "imageData.width", "100");
  _assertSame(imageData.height, 50, "imageData.height", "50");
  assertArrayEquals(getRGBA(imageData.data, 4), [0, 0, 0, 0]);

  var testColor = [0, 255, 255, 128];
  setRGBA(imageData.data, 4, testColor);
  assertArrayEquals(getRGBA(imageData.data, 4), testColor);

  assert_throws_js(TypeError, function() { ImageData(1, 1); });
  assert_throws_js(TypeError, function() { new ImageData(10); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(0, 10); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(10, 0); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData('width', 'height'); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(1 << 31, 1 << 31); });
  assert_throws_js(TypeError, function() { new ImageData(new Uint8ClampedArray(0)); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(new Uint8Array(100), 25); });
  assert_throws_dom("INVALID_STATE_ERR", function() { new ImageData(new Uint8ClampedArray(27), 2); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(new Uint8ClampedArray(28), 7, 0); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(new Uint8ClampedArray(104), 14); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(new Uint8ClampedArray([12, 34, 168, 65328]), 1, 151); });
  assert_throws_js(TypeError, function() { new ImageData(self, 4, 4); });
  assert_throws_js(TypeError, function() { new ImageData(null, 4, 4); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(imageData.data, 0); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(imageData.data, 13); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(imageData.data, 1 << 31); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(imageData.data, 'biggish'); });
  assert_throws_dom("INDEX_SIZE_ERR", function() { new ImageData(imageData.data, 1 << 24, 1 << 31); });
  _assertSame(new ImageData(new Uint8ClampedArray(28), 7).height, 1, "new ImageData(new Uint8ClampedArray(28), 7).height", "1");

  imageDataFromData = new ImageData(imageData.data, 100);
  _assertSame(imageDataFromData.width, 100, "imageDataFromData.width", "100");
  _assertSame(imageDataFromData.height, 50, "imageDataFromData.height", "50");
  _assertSame(imageDataFromData.data, imageData.data, "imageDataFromData.data", "imageData.data");
  assertArrayEquals(getRGBA(imageDataFromData.data, 10), getRGBA(imageData.data, 10));
  setRGBA(imageData.data, 10, testColor);
  assertArrayEquals(getRGBA(imageDataFromData.data, 10), getRGBA(imageData.data, 10));

  var data = new Uint8ClampedArray(400);
  data[22] = 129;
  imageDataFromData = new ImageData(data, 20, 5);
  _assertSame(imageDataFromData.width, 20, "imageDataFromData.width", "20");
  _assertSame(imageDataFromData.height, 5, "imageDataFromData.height", "5");
  _assertSame(imageDataFromData.data, data, "imageDataFromData.data", "data");
  assertArrayEquals(getRGBA(imageDataFromData.data, 2), getRGBA(data, 2));
  setRGBA(imageDataFromData.data, 2, testColor);
  assertArrayEquals(getRGBA(imageDataFromData.data, 2), getRGBA(data, 2));

  if (window.SharedArrayBuffer) {
      assert_throws_js(TypeError, function() { new ImageData(new Uint16Array(new SharedArrayBuffer(32)), 4, 2); });
  }

});
</script>

