summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/canvas/canvas-ImageBitmap-close.html
blob: 7f6eacfa025735bc3319de0e502df5fc13aa0767 (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
<!DOCTYPE html>
<script src="../../resources/js-test.js"></script>
<script>
jsTestIsAsync = true;
var worker = new Worker('./resources/worker-onmessage-noop.js');

description("Tests that the close method of ImageBitmap does dispose the image data");

var imgHeight = 10;
var imgWidth = 10;
var imageData = new ImageData(10, 10);
var bitmap;
createImageBitmap(imageData).then(imageBitmap => {
    bitmap = imageBitmap;
    shouldBe("bitmap.width", "imgWidth");
    shouldBe("bitmap.height", "imgHeight");

    // Apply structured clone to the bitmap, nothing should be changed
    worker.postMessage({data: bitmap});
    shouldBe("bitmap.width", "imgWidth");
    shouldBe("bitmap.height", "imgHeight");

    // After calling close, the image data associated with the bitmap should no longer exist
    bitmap.close();
    shouldBe("bitmap.width", "0");
    shouldBe("bitmap.height", "0");

    // Try to apply structured clone to an already closed bitmap
    try {
        worker.postMessage({data: bitmap});
        testFailed("Apply structured clone to an already closed bitmap passed unexpectedly");
    } catch(ex) {
        testPassed("Apply structured clone to an already closed bitmap is rejected as expected: " + ex);
    }

    // Try to apply transfering to an already closed bitmap
    try {
        worker.postMessage({data: bitmap}, [bitmap]);
        testFailed("Apply transfering to an already closed bitmap passed unexpectedly");
    } catch(ex) {
        testPassed("Apply transfering to an already closed bitmap is rejected as expected: " + ex);
    }

    // Calling createImageBitmap from a closed bitmap should be rejected
    return createImageBitmap(bitmap).then(function() {
        testFailed("Promise accepted, expected to be rejected");
        finishJSTest();
    }, ex => {
        testPassed("createImageBitmap from a closed ImageBitmap was rejected. " + ex);
    });
}).then(() => {
    // Call close to a already closed bitmap should be noop.
    bitmap.close();
    shouldBe("bitmap.width", "0");
    shouldBe("bitmap.height", "0");

    return createImageBitmap(imageData).then(imageBitmap => {
        bitmap = imageBitmap;
        shouldBe("bitmap.width", "imgWidth");
        shouldBe("bitmap.height", "imgHeight");

        // Transfer the bitmap to a worker
        worker.postMessage({data: bitmap}, [bitmap]);

        // After transfering, the bitmap is neutered.
        shouldBe("bitmap.width", "0");
        shouldBe("bitmap.height", "0");

        // Calling close to a neutered bitmap should be noop.
        bitmap.close();
        shouldBe("bitmap.width", "0");
        shouldBe("bitmap.height", "0");
    });
}).catch(function(ex) {
    testFailed("Unexpected failure: " + ex);
}).then(() => { finishJSTest(); });
</script>