<!DOCTYPE html>
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

var xWidth = xHeight = 10;

function checkImageData(expectedColorSettings, imageData) {
  var imageDataColorSettings = imageData.getSettings();
  assert_equals(expectedColorSettings.colorSpace, imageDataColorSettings.colorSpace);
  assert_equals(expectedColorSettings.storageFormat, imageDataColorSettings.storageFormat);
  assert_equals(imageData.data.length, 4 * xWidth * xHeight);
}

// Test createImageData when the canvas is color managed
var helperImageData = new ImageData(xWidth, xHeight);
function runTestCreateImageData(canvasColorSettings, imageSetting) {
  var aCanvas = document.createElement("canvas");
  aCanvas.width = xWidth;
  aCanvas.height = xHeight;
  var ctx = aCanvas.getContext('2d', canvasColorSettings);
  var imageData = ctx.createImageData(xWidth, xHeight, imageSetting);
  checkImageData(imageSetting, imageData);

  imageData = ctx.createImageData(helperImageData);
  checkImageData(imageData.getSettings(), imageData);
}

var testScenariosCreateImageData = [
  ["Test color managed cretateImageData: {srgb, uint8} -> {srgb, uint8}",
      {colorSpace: "srgb", pixelFormat: "uint8"},
      {colorSpace: "srgb", storageFormat: "uint8"}],
  ["Test color managed cretateImageData: {srgb, float16} -> {srgb, float32}",
      {colorSpace: "srgb", pixelFormat: "float16"},
      {colorSpace: "srgb", storageFormat: "float32"}],
];

function runCreateImageDataTests() {
  for (var i = 0; i < testScenariosCreateImageData.length; i++){
    var t = test(function() {
      runTestCreateImageData(testScenariosCreateImageData[i][1],
          testScenariosCreateImageData[i][2]);
    }, testScenariosCreateImageData[i][0]);
  }
}
runCreateImageDataTests();

// Test getImageData when the canvas is color managed
function runTestGetImageData(canvasColorSettings, imageSetting) {
  var aCanvas = document.createElement("canvas");
  aCanvas.width = xWidth;
  aCanvas.height = xHeight;
  var ctx = aCanvas.getContext('2d', canvasColorSettings);
  var imageData = ctx.getImageData(0, 0, xWidth, xHeight, imageSetting);
  checkImageData(imageSetting, imageData);
}

var testScenariosGetImageData = [
  ["Test color managed getImageData: {srgb, uint8} -> {srgb, uint8}",
      {colorSpace: "srgb", pixelFormat: "uint8"},
      {colorSpace: "srgb", storageFormat: "uint8"}],
  ["Test color managed getImageData: {srgb, float16} -> {srgb, float32}",
      {colorSpace: "srgb", pixelFormat: "float16"},
      {colorSpace: "srgb", storageFormat: "float32"}],
];

function runGetImageDataTests() {
  for (var i = 0; i < testScenariosGetImageData.length; i++){
    var t = test(function() {
      runTestGetImageData(testScenariosGetImageData[i][1],
      testScenariosGetImageData[i][2]);
    }, testScenariosGetImageData[i][0]);
  }
}
runGetImageDataTests();

// Test putImageData when the canvas is color managed.

var testScenariosPutImageData = [];
function prepareTestScenariosPutImageData() {
  var colorSpaces = ["srgb"];
  var imageDataStorageFormats = ["uint8", "uint16", "float32"];
  var canvasPixelFormats = ["uint8", "float16"];
  // var canvasStorageFormats = ["uint8", "float32"];

  for (i = 0; i < colorSpaces.length; i++)
    for (j = 0; j < imageDataStorageFormats.length; j++)
      for (k = 0; k < colorSpaces.length; k++)
        for (l = 0; l < canvasPixelFormats.length; l++) {
          testTitle = "Test color managed putImageData: ".concat(
            "{", colorSpaces[i], ", ", imageDataStorageFormats[j], "} -> {", colorSpaces[k],
            ", ", canvasPixelFormats[l], "}");
          imageDataColorSettings =
            {colorSpace: colorSpaces[i], storageFormat: imageDataStorageFormats[j]};
          canvasColorSettings =
            {colorSpace: colorSpaces[k], pixelFormat: canvasPixelFormats[l]};
          testScenariosPutImageData.push([testTitle, imageDataColorSettings,
            canvasColorSettings]);
        }
}

function createAndPutImageData(imageDataColorSettings, canvasColorSettings) {
  // create color managed canvas
  var aCanvas = document.createElement("canvas");
  aCanvas.width = xWidth;
  aCanvas.height = xHeight;
  var ctx = aCanvas.getContext('2d', canvasColorSettings);
  // create color managed ImageData
  var imageData = ctx.createImageData(xWidth, xHeight, imageDataColorSettings);
  // put image data into canvas. test succeeds if this does not crash.
  ctx.putImageData(imageData, 0, 0);
}

function runTestPutImageData(imageDataColorSettings, canvasColorSettings) {
  createAndPutImageData(imageDataColorSettings, canvasColorSettings);
  createAndPutImageData(imageDataColorSettings, canvasColorSettings);
  createAndPutImageData(imageDataColorSettings, canvasColorSettings);
}

prepareTestScenariosPutImageData();

function runPutImageDataTests() {
  for (var i = 0; i < testScenariosPutImageData.length; i++){
    var t = test(function() {
      runTestPutImageData(testScenariosPutImageData[i][1]);
    }, testScenariosPutImageData[i][0]);
  }
}
runPutImageDataTests();

</script>
</body>
