summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/imagebitmap-renderingcontext/bitmaprenderer-as-imagesource.html
blob: 21c5ea48ad4cadff96c401972594083958f447f3 (plain)
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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Canvas's ImageBitmapRenderingContext test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context">
<script>
var width = 10;
var height = 10;

function testCanvas(ctx, x, y, r, g, b, a)
{
    var color = ctx.getImageData(x, y, 1, 1).data;
    assert_array_equals(color, [r, g, b, a]);
}

function consumeImageBitmap(image, t)
{
    var myCanvas = document.createElement('canvas');
    myCanvas.width = myCanvas.height = 20;
    var myCtx = myCanvas.getContext('bitmaprenderer');
    myCtx.transferFromImageBitmap(image);

    createImageBitmap(myCanvas).then(t.step_func_done(function(imageBitmap) {
        // Per spec, when transferFromImageBitmap happens, the transferred
        // ImageBitmap (|image| here) must determine the intrinsic size of
        // myCanvas, and hence myCanvas.width/height is ignored. Therefore,
        // |imageBitmap| should have the same size as the |image|.
        assert_equals(imageBitmap.width, width);
        assert_equals(imageBitmap.height, height);

        var dstCanvas = document.createElement('canvas');
        dstCanvas.width = dstCanvas.height = 20;
        var dstCtx = dstCanvas.getContext('2d');
        dstCtx.drawImage(myCanvas, 0, 0);
        testCanvas(dstCtx, 5, 5, 0, 255, 0, 255);
        testCanvas(dstCtx, 15, 15, 0, 0, 0, 0);
    }));
}

async_test(function(t) {
    var canvas = document.createElement("canvas");
    canvas.width = width;
    canvas.height = height;
    var ctx = canvas.getContext('2d');
    ctx.fillStyle = '#0f0';
    ctx.fillRect(0, 0, width, height);
    createImageBitmap(canvas).then(t.step_func(function(image) {
        consumeImageBitmap(image, t);
    }));
}, 'Test that createImageBitmap from a bitmaprenderer canvas produces correct result');

async_test(function(t) {
    var canvas = document.createElement("canvas");
    canvas.width = width;
    canvas.height = height;
    var ctx = canvas.getContext('bitmaprenderer');
    createImageBitmap(canvas).then(t.step_func_done(function(image) {
        assert_equals(image.width, width);
        assert_equals(image.height, height);

        var dstCanvas = document.createElement('canvas');
        dstCanvas.width = width;
        dstCanvas.height = height;
        var dstCtx = dstCanvas.getContext('2d');
        dstCtx.drawImage(canvas, 0, 0);
        testCanvas(dstCtx, 5, 5, 0, 0, 0, 0);
    }));
}, 'Test that createImageBitmap on a bitmaprenderer canvas that never consumes any source produces correct result');


async_test(function(t) {
    var canvas = document.createElement("canvas");
    canvas.width = width;
    canvas.height = height;
    var ctx = canvas.getContext('bitmaprenderer');
    ctx.transferFromImageBitmap(null);
    createImageBitmap(canvas).then(t.step_func_done(function(image) {
        assert_equals(image.width, width);
        assert_equals(image.height, height);

        var dstCanvas = document.createElement('canvas');
        dstCanvas.width = width;
        dstCanvas.height = height;
        var dstCtx = dstCanvas.getContext('2d');
        dstCtx.drawImage(canvas, 0, 0);
        testCanvas(dstCtx, 5, 5, 0, 0, 0, 0);
    }));
}, 'Test that createImageBitmap on a bitmaprenderer canvas that consumes null produces correct result');
</script>