<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Resizing Test for OffscreenCanvas commit()</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
<script src="../../js/tests/canvas-tests-utils.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description("This test ensures that the OffscreenCanvas context returns the correct image size after resizing and calling commit().");

function testResizeOnNewOffscreenCanvas() {
  var canvas = new OffscreenCanvas(10, 20);
  canvas.getContext("webgl");
  canvas.width = 30;
  canvas.height = 40;
  assertWidthAndHeight(canvas, "canvas", 30, 40);
  var imagebitmap = canvas.transferToImageBitmap();
  assertWidthAndHeight(imagebitmap, "imagebitmap", 30, 40);
}

function testResizeOnTransferredOffscreenCanvas() {
  var placeholder = document.createElement("canvas");
  var offscreencanvas = transferredOffscreenCanvasCreation(placeholder, 10, 20);
  var ctx = offscreencanvas.getContext("webgl");

  // Verify that setting the size of an OffscreenCanvas that has a placeholder works.
  offscreencanvas.width = 30;
  offscreencanvas.height = 40;
  assertWidthAndHeight(offscreencanvas, "resized offscreencanvas", 30, 40);
  var imagebitmap = offscreencanvas.transferToImageBitmap();
  assertWidthAndHeight(imagebitmap, "imagebitmap transferred from resized offscreencanvas" , 30, 40);

  // Verify that setting the size of an OffscreenCanvas does not directly update the size of its placeholder canvas.
  assertWidthAndHeight(placeholder, "placeholder canvas", 10, 20);

  var asyncStepsCompleted = 0;
  ctx.commit();
  createImageBitmap(placeholder).then(image => {
    // Verify that the placeholder was not updated synchronously.
    assertWidthAndHeight(image, "imagebitmap from placeholder canvas", 10, 20);
    asyncStepsCompleted++;
    if (asyncStepsCompleted == 2) {
      finishTest();
    }
  });

  // Set timeout acts as a sync barrier to allow commit to propagate
  setTimeout(function() {
    // Verify that commit() asynchronously updates the size of its placeholder canvas.
    assertWidthAndHeight(placeholder, "placeholder canvas", 30, 40);

    // Verify that width/height attributes are not settable
    shouldThrow("placeholder.width = 50");
    shouldThrow("placeholder.height = 60");

    assertWidthAndHeight(placeholder, "placeholder canvas after size reset", 30, 40);

    createImageBitmap(placeholder).then(image => {
      // Verify that an image grabbed from the placeholder has the correct dimensions
      assertWidthAndHeight(image, "imagebitmap from placeholder canvas", 30, 40);
      asyncStepsCompleted++;
      if (asyncStepsCompleted == 2) {
        finishTest();
      }
    });
  }, 0);
}

if (!window.OffscreenCanvas) {
  testPassed("No OffscreenCanvas support");
  finishTest();
} else if (!new OffscreenCanvas(10, 20).getContext("webgl").commit) {
  testPassed("commit() not supported");
  finishTest();
} else {
  testResizeOnNewOffscreenCanvas();
  testResizeOnTransferredOffscreenCanvas();
}
</script>
</body>
</html>
