summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/test_offscreencanvas_dynamic_fallback.html
blob: 8e4f0f8deafe5b2f1493c3c8239bbf0658a2862c (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
<!DOCTYPE HTML>
<html>
<head>
<title>WebGL in OffscreenCanvas</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/WindowSnapshot.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<script>

SimpleTest.waitForExplicitFinish();

function createCanvas(initWithMask) {
  var canvas = document.createElement("canvas");
  canvas.width = 64;
  canvas.height = 64;
  document.body.appendChild(canvas);
  if (initWithMask) {
    canvas.style.mask = "url('offscreencanvas_mask.svg#fade_mask_both')";
  }

  return canvas;
}

function getRefSnapshot(initWithMask) {
  var refCanvas = createCanvas(!initWithMask);
  var ctx = refCanvas.getContext("2d");
  ctx.rect(0, 0, 64, 64);
  ctx.fillStyle = "#00FF00";
  ctx.fill();
  var result = snapshotWindow(window);
  document.body.removeChild(refCanvas);
  return result;
}

function runTest(initWithMask) {
  var htmlCanvas = createCanvas(initWithMask);
  var worker = new Worker("offscreencanvas.js");

  worker.onmessage = function(evt) {
    var msg = evt.data || {};
    if (msg.type == "draw") {
      if (msg.count === 10) {
        // Change the fallback state dynamically when drawing count reaches 10.
        if (initWithMask) {
          htmlCanvas.style.mask = "";
        } else {
          htmlCanvas.style.mask = "url('offscreencanvas_mask.svg#fade_mask_both')";
        }
      } else if (msg.count === 20) {
        var snapshotFallback = snapshotWindow(window);
        worker.terminate();
        document.body.removeChild(htmlCanvas);

        var results = compareSnapshots(snapshotFallback, getRefSnapshot(initWithMask), true);
        ok(results[0], "after dynamic fallback, screenshots should be the same");

        if (initWithMask) {
          SimpleTest.finish();
        } else {
          runTest(true);
        }
      }
    }
  }

  var offscreenCanvas = htmlCanvas.transferControlToOffscreen();

  worker.postMessage({test: 'webgl_fallback', canvas: offscreenCanvas}, [offscreenCanvas]);
}

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

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