chromium/third_party/blink/web_tests/fast/canvas/canvas-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>
<body></body>
<script>
async_test(t => {
  assert_true(!!window.internals, 'This test requires window.internals.');

  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');
  document.body.appendChild(ctx.canvas);

  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>