summaryrefslogtreecommitdiff
path: root/dom/canvas/test/test_offscreencanvas_toimagebitmap.html
blob: e07f4f3352a58ce7c7d30b3fcfaa3d5769a745e4 (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
<!DOCTYPE HTML>
<html>
<head>
<title>WebGL in OffscreenCanvas</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<canvas id="c" width="64" height="64"></canvas>
<canvas id="c2" width="64" height="64"></canvas>
<canvas id="c-ref" width="64" height="64"></canvas>
<script>

SimpleTest.waitForExplicitFinish();

function runTest() {

  var worker = new Worker("offscreencanvas.js");

  ok(worker, "Web worker successfully created");

  worker.onmessage = function(evt) {
    var msg = evt.data || {};
    if (msg.type == "test") {
      ok(msg.result, msg.name);
    }
    if (msg.type == "imagebitmap") {
      // testing toBlob
      // Fill c-ref with green color.
      var c = document.getElementById("c-ref");
      var ctx = c.getContext("2d");
      ctx.rect(0, 0, 64, 64);
      ctx.fillStyle = "#00FF00";
      ctx.fill();

      var htmlCanvas = document.getElementById("c");
      var bitmapRenderer = htmlCanvas.getContext("bitmaprenderer");
      bitmapRenderer.transferFromImageBitmap(msg.bitmap);

      ok(c.toDataURL() == htmlCanvas.toDataURL(),
         "imagebitmap should return a 64x64 green square");

      // The ownership of msg.bitmap should be transferred to canvas "c" when
      // we called transferFromImageBitmap. So we test if the ownership is actually
      // transferred here.
      var htmlCanvas = document.getElementById("c2");
      var bitmapRenderer = htmlCanvas.getContext("bitmaprenderer");
      bitmapRenderer.transferFromImageBitmap(msg.bitmap);

      SimpleTest.doesThrow(
        function() { c2.toDataURL(); },
        "ImageBitmap has been transferred, toDataURL will throw.");

      worker.terminate();
      SimpleTest.finish();
    }
  }

  worker.postMessage({test: 'webgl_imagebitmap'});
}

SpecialPowers.pushPrefEnv({'set': [
  ['gfx.offscreencanvas.enabled', true],
  ['webgl.force-enabled', true],
]}, runTest);

</script>
</body>
</html>