chromium/third_party/blink/web_tests/fast/canvas/offscreencanvas-lost-context-layers.html

<!doctype html>
<script src="../../resources/js-test.js"></script>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>

async_test(t => {
  assert_true(!!window.internals, 'This test requires window.internals.');

  const canvas = new OffscreenCanvas(500, 500);
  const ctx = canvas.getContext('2d');

  canvas.oncontextlost = t.step_func(function() {
    // Add calls are no-ops: `restore` and `endLayer` shouldn't throw despite
    // not having matching `save` and `beginLayer` calls.
    ctx.restore();
    ctx.endLayer();
    ctx.endLayer();
    ctx.beginLayer();
  });

  canvas.oncontextrestored = t.step_func_done(function() {
    // Canvas should be back to it's default state, with no layer opened.
    ctx.beginLayer();
    ctx.endLayer();
    assert_throws_dom('INVALID_STATE_ERR', () => {
      ctx.endLayer();
    });
  });

  ctx.beginLayer();
  internals.forceLoseCanvasContext(canvas, '2d');
}, 'Test losing context while layers are opened.');

</script>