summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/canvas/offscreen/manual/the-offscreen-canvas/offscreencanvas.transfer.to.imagebitmap.html
blob: 6e1adf9591fa8c2fb67efca4b55f8d6a3c3acdf0 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/#dom-offscreencanvas-transfertoimagebitmap">

<script id="myWorker" type="text/worker">

self.onmessage = function(e) {
};

</script>

<script>
function makeWorker(script)
{
    var blob = new Blob([script]);
    return new Worker(URL.createObjectURL(blob));
}

test(function() {
    function testSize(contextType) {
        var offscreenCanvas = new OffscreenCanvas(100, 50);
        var ctx = offscreenCanvas.getContext(contextType);
        var image = offscreenCanvas.transferToImageBitmap();
        assert_equals(image.width, 100);
        assert_equals(image.height, 50);
    }

    testSize('2d');
    testSize('webgl');
}, "Test that transferToImageBitmap returns an ImageBitmap with correct width and height");

test(function() {
    function testImageBitmapClr(shouldCallTwice, alphaVal) {
        var offscreenCanvas = new OffscreenCanvas(100, 50);
        var ctx = offscreenCanvas.getContext('2d', {alpha: alphaVal});
        ctx.fillStyle = "#0f0";
        ctx.fillRect(0, 0, 100, 50);
        var image = offscreenCanvas.transferToImageBitmap();

        if (shouldCallTwice)
            image = offscreenCanvas.transferToImageBitmap();

        var drawCanvas = document.createElement('canvas');
        drawCanvas.width = 100;
        drawCanvas.height = 50;
        var dCtx = drawCanvas.getContext('2d');
        dCtx.drawImage(image, 0, 0);

        if (shouldCallTwice) {
            if (alphaVal)
                _assertPixel(drawCanvas, 50,25, 0,0,0,0);
            else
                _assertPixel(drawCanvas, 50,25, 0,0,0,255);
        } else {
            _assertPixel(drawCanvas, 50,25, 0,255,0,255);
        }
    }

    testImageBitmapClr(false, true);
    testImageBitmapClr(true, true);
    testImageBitmapClr(true, false);
}, "Test that transferToImageBitmap returns an ImageBitmap with correct color");

test(function() {
    var offscreenCanvas = new OffscreenCanvas(100, 50);
    var ctx = offscreenCanvas.getContext('2d');
    ctx.lineWidth = 10;
    var image = offscreenCanvas.transferToImageBitmap();
    assert_equals(ctx.lineWidth, 10);
}, "Test that transferToImageBitmap won't change context's property");

test(function() {
    var offscreenCanvas = new OffscreenCanvas(100, 50);
    var ctx = offscreenCanvas.getContext('2d');
    ctx.rect(0, 0, 25, 50);
    ctx.clip();
    ctx.translate(20, 20);

    ctx.fillStyle = '#0f0';
    var image1 = offscreenCanvas.transferToImageBitmap();
    // trasnform should be preserved
    ctx.fillRect(0, 0, 10, 10);
    var image2 = offscreenCanvas.transferToImageBitmap();

    var drawCanvas = document.createElement('canvas');
    drawCanvas.width = 100;
    drawCanvas.height = 50;
    var dCtx = drawCanvas.getContext('2d');
    dCtx.drawImage(image2, 0, 0);
    // Verify that transform was carried over.
    _assertPixel(drawCanvas, 23,25, 0,255,0,255);
    // Verify that clip was carried over.
    _assertPixel(drawCanvas, 27,25, 0,0,0,0);
}, "Test that transferToImageBitmap preserves transform");

async_test(function(t) {
    var worker = makeWorker(document.getElementById("myWorker").textContent);
    var offscreenCanvas = new OffscreenCanvas(10, 10);
    worker.postMessage(offscreenCanvas, [offscreenCanvas]);
    assert_throws_dom("InvalidStateError", function() { offscreenCanvas.transferToImageBitmap(); });
    t.done();
}, "Test that call transferToImageBitmap on a detached OffscreenCanvas throws an exception");

test(function() {
    var offscreenCanvas = new OffscreenCanvas(10, 10);
    assert_throws_dom("InvalidStateError", function() { offscreenCanvas.transferToImageBitmap(); });
}, "Test that transferToImageBitmap without a context throws an exception");

</script>