<!DOCTYPE html>
<html>
<title>createImageBitmap test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/canvas-tests.js"></script>
<link rel="stylesheet" href="/common/canvas-tests.css">
<body>
<script>
(function() {
    let loadImage = new Promise(function(resolve, reject) {
        let img = new Image();
        img.onload = function() { resolve(img); };
        img.onerror = function() { reject(); };
        img.src = "/images/pattern.png";
    });

    promise_test(function(t) {
        return promise_rejects_js(t, RangeError, createImageBitmap(new Image(), 0, 0, 0, 10));
    }, "createImageBitmap rejects with RangeError if width is zero");

    promise_test(function(t) {
        return promise_rejects_js(t, RangeError, createImageBitmap(new Image(), 0, 0, 10, 0));
    }, "createImageBitmap rejects with RangeError if height is zero");

    promise_test(function(t) {
        return loadImage.then(function(img) {
            assert_equals(img.width, 20);
            assert_equals(img.height, 20);
            return createImageBitmap(img, 10, 10, -10, -10);
        }).then(function(imageBitmap) {
            assert_equals(imageBitmap.width, 10);
            assert_equals(imageBitmap.height, 10);
        });
    }, "createImageBitmap if width is negative");

    promise_test(function(t) {
        return loadImage.then(function(img) {
            assert_equals(img.width, 20);
            assert_equals(img.height, 20);
            return createImageBitmap(img, 10, 10, 10, -10);
        }).then(function(imageBitmap) {
            assert_equals(imageBitmap.width, 10);
            assert_equals(imageBitmap.height, 10);
        });
    }, "createImageBitmap if height is negative");

    promise_test(function(t) {
        return promise_rejects_dom(t, "InvalidStateError", createImageBitmap(new Image()));
    }, "createImageBitmap rejects with InvalidStateError on an HTMLImageElement with no image data");

    promise_test(function() {
        return loadImage.then(function(img) {
            return createImageBitmap(img);
        }).then(function(imageBitmap) {
            assert_equals(imageBitmap.width, 20, "ImageBitmap width should be 20");
            assert_equals(imageBitmap.height, 20, "ImageBitmap height should be 20");
        });
    }, "createImageBitmap from an HTMLImageElement with image data");

    promise_test(function(t) {
        let canvas = document.createElement("canvas");
        canvas.width = 0;
        canvas.height = 0;
        return promise_rejects_dom(t, "InvalidStateError", createImageBitmap(canvas));
    }, "createImageBitmap rejects with InvalidStateError from an canvas with zero dimensions");

    promise_test(function() {
        return new Promise(function(resolve, reject) {
            let canvas = document.createElement("canvas");
            canvas.width = 20;
            canvas.height = 20;
            canvas.getContext("2d").fillRect(0, 0, 20, 20);
            resolve(createImageBitmap(canvas));
        }).then(function(imageBitmap) {
            assert_equals(imageBitmap.width, 20, "ImageBitmap width should be 20");
            assert_equals(imageBitmap.height, 20, "ImageBitmap height should be 20");
        });
    }, "createImageBitmap from an canvas");

    promise_test(function() {
        return new Promise(function(resolve, reject) {
            let img = new Image();
            img.onload = function() { resolve(img); };
            img.src = "/images/pattern.png";
        }).then(function(img) {
            return createImageBitmap(img);
        }).then(function(imageBitmap) {
            imageBitmap.close();
            assert_equals(imageBitmap.width, 0, "A closed ImageBitmap width should be 0");
            assert_equals(imageBitmap.height, 0, "A closed ImageBitmap height should be 0");
        });
    }, "A closed ImageBitmap has zero width and height");

})();
</script>
</body>
</html>
