chromium/third_party/blink/web_tests/fast/webgl/offscreenCanvas-context-lost-worker.html

<!DOCTYPE html>
<html>
<head>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="./resources/webgl-test-utils-full.js"></script>
</head>

<script id="myWorker" type="text/worker">
var canvas;
var gl;
var extensionName;
var extension;

var buffer;
var framebuffer;
var renderbuffer;
var program;
var shader;
var texture;
var uniformLocation;
var arrayBuffer;
var arrayBufferView;
var imageData;
var float32array;
var int32array;
var OES_vertex_array_object;
var vertexArrayObject;

var validContextResults;
var lostContextResults;

function compareGLError(glError, evalStr)
{
    var exception;
    try {
        eval(evalStr);
    } catch (e) {
        exception = e;
    }
    if (exception) {
        return false;
    } else {
        if (gl.getError() == glError)
            return true;
        return false;
    }
}

var simpleTextureVertexShader = [
  'attribute vec4 vPosition;',
  'attribute vec2 texCoord0;',
  'varying vec2 texCoord;',
  'void main() {',
  '    gl_Position = vPosition;',
  '    texCoord = texCoord0;',
  '}'].join('\n');

var simpleTextureFragmentShader = [
  'precision mediump float;',
  'uniform sampler2D tex;',
  'varying vec2 texCoord;',
  'void main() {',
  '    gl_FragData[0] = texture2D(tex, texCoord);',
  '}'].join('\n');

function getShader(gl, shaderStr, type)
{
  var shader = gl.createShader(type);
  gl.shaderSource(shader, shaderStr);
  gl.compileShader(shader);

  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
    return null;
  return shader;
}

function setupProgram(gl, shaders, opt_attribs, opt_locations)
{
  var vertexShader = getShader(gl, simpleTextureVertexShader, gl.VERTEX_SHADER);
  var fragmentShader = getShader(gl, simpleTextureFragmentShader, gl.FRAGMENT_SHADER);
  var program = gl.createProgram();
  gl.attachShader(program, vertexShader);
  gl.attachShader(program, fragmentShader);

  if (opt_attribs) {
    for (var ii = 0; ii < opt_attribs.length; ++ii) {
      gl.bindAttribLocation(
          program,
          opt_locations ? opt_locations[ii] : ii,
          opt_attribs[ii]);
    }
  }
  gl.linkProgram(program);

  // Check the link status
  var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
  if (!linked) {
      // something went wrong with the link
      gl.deleteProgram(program);
      return null;
  }
  gl.useProgram(program);
  return program;
}

function setupSimpleTextureProgram(gl, opt_positionLocation, opt_texcoordLocation)
{
  opt_positionLocation = opt_positionLocation || 0;
  opt_texcoordLocation = opt_texcoordLocation || 1;
  return setupProgram(gl,
                      [simpleTextureVertexShader, simpleTextureFragmentShader],
                      ['vPosition', 'texCoord0'],
                      [opt_positionLocation, opt_texcoordLocation]);
}

function testValidContext()
{
    // Purposely make all results true
    var re1 = !(gl.isContextLost());

    arrayBuffer = new ArrayBuffer(4);
    arrayBufferView = new Int8Array(arrayBuffer);

    // Generate resources for testing.
    buffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
    framebuffer = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
    program = setupSimpleTextureProgram(gl);
    renderbuffer = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer);
    shader = gl.createShader(gl.VERTEX_SHADER);
    texture = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, texture);
    var re2 = (gl.getError() == gl.NO_ERROR);

    // Test is queries that will later be false
    var re3 = compareGLError(gl.NO_ERROR, "gl.enable(gl.BLEND)");
    var re4 = gl.isBuffer(buffer);
    var re5 = gl.isEnabled(gl.BLEND);
    var re6 = gl.isFramebuffer(framebuffer);
    var re7 = gl.isProgram(program);
    var re8 = gl.isRenderbuffer(renderbuffer);
    var re9 = gl.isShader(shader);
    var re10 = gl.isTexture(texture);

    validContextResults = [re1, re2, re3, re4, re5, re6, re7, re8, re9, re10];
}

function loseContext()
{
    // Note: this will cause the context to be lost, but the
    // webglcontextlost event listener to be queued.
    extension.loseContext();
}

function testLostContext()
{
    // Purposely make all results true
    // Functions with special return values.
    var re1 = gl.isContextLost();
    var re2 = (gl.getError() == gl.CONTEXT_LOST_WEBGL);
    var re3 = (gl.getError() == gl.NO_ERROR);
    var re4 = (gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_UNSUPPORTED);
    var re5 = (gl.getAttribLocation(program, 'u_modelViewProjMatrix') == -1);
    var re6 = (gl.getVertexAttribOffset(0, gl.VERTEX_ATTRIB_ARRAY_POINTER) == 0);

    // Test the extension itself.
    var re7 = compareGLError(gl.INVALID_OPERATION, "extension.loseContext()");

    imageData = new ImageData(1, 1);
    float32array = new Float32Array(1);
    int32array = new Int32Array(1);

    // Test a set of functions that should not generate any GL error
    var re8 = compareGLError(gl.NO_ERROR, "gl.activeTexture(gl.TEXTURE0)");
    var re9 = compareGLError(gl.NO_ERROR, "gl.attachShader(program, shader)");
    var re10 = compareGLError(gl.NO_ERROR, "gl.bindBuffer(gl.ARRAY_BUFFER, buffer)");
    var re11 = compareGLError(gl.NO_ERROR, "gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer)");
    var re12 = compareGLError(gl.NO_ERROR, "gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer)");
    var re13 = compareGLError(gl.NO_ERROR, "gl.bindTexture(gl.TEXTURE_2D, texture)");
    var re14 = compareGLError(gl.NO_ERROR, "gl.blendColor(1.0, 1.0, 1.0, 1.0)");
    var re15 = compareGLError(gl.NO_ERROR, "gl.blendEquation(gl.FUNC_ADD)");
    var re16 = compareGLError(gl.NO_ERROR, "gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD)");
    var re17 = compareGLError(gl.NO_ERROR, "gl.blendFunc(gl.ONE, gl.ONE)");
    var re18 = compareGLError(gl.NO_ERROR, "gl.blendFuncSeparate(gl.ONE, gl.ONE, gl.ONE, gl.ONE)");
    var re19 = compareGLError(gl.NO_ERROR, "gl.bufferData(gl.ARRAY_BUFFER, 0, gl.STATIC_DRAW)");
    var re20 = compareGLError(gl.NO_ERROR, "gl.bufferData(gl.ARRAY_BUFFER, arrayBufferView, gl.STATIC_DRAW)");
    var re21 = compareGLError(gl.NO_ERROR, "gl.bufferData(gl.ARRAY_BUFFER, arrayBuffer, gl.STATIC_DRAW)");
    var re22 = compareGLError(gl.NO_ERROR, "gl.bufferSubData(gl.ARRAY_BUFFRE, 0, arrayBufferView)");
    var re23 = compareGLError(gl.NO_ERROR, "gl.bufferSubData(gl.ARRAY_BUFFRE, 0, arrayBuffer)");
    var re24 = compareGLError(gl.NO_ERROR, "gl.clear(gl.COLOR_BUFFER_BIT)");
    var re25 = compareGLError(gl.NO_ERROR, "gl.clearColor(1, 1, 1, 1)");
    var re26 = compareGLError(gl.NO_ERROR, "gl.clearDepth(1)");
    var re27 = compareGLError(gl.NO_ERROR, "gl.clearStencil(0)");
    var re28 = compareGLError(gl.NO_ERROR, "gl.colorMask(1, 1, 1, 1)");
    var re29 = compareGLError(gl.NO_ERROR, "gl.compileShader(shader)");
    var re30 = compareGLError(gl.NO_ERROR, "gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, 0, 0, 0)");
    var re31 = compareGLError(gl.NO_ERROR, "gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, 0, 0, 0)");
    var re32 = compareGLError(gl.NO_ERROR, "gl.cullFace(gl.FRONT)");
    var re33 = compareGLError(gl.NO_ERROR, "gl.deleteBuffer(buffer)");
    var re34 = compareGLError(gl.NO_ERROR, "gl.deleteFramebuffer(framebuffer)");
    var re35 = compareGLError(gl.NO_ERROR, "gl.deleteProgram(program)");
    var re36 = compareGLError(gl.NO_ERROR, "gl.deleteRenderbuffer(renderbuffer)");
    var re37 = compareGLError(gl.NO_ERROR, "gl.deleteShader(shader)");
    var re38 = compareGLError(gl.NO_ERROR, "gl.deleteTexture(texture)");
    var re39 = compareGLError(gl.NO_ERROR, "gl.depthFunc(gl.NEVER)");
    var re40 = compareGLError(gl.NO_ERROR, "gl.depthMask(0)");
    var re41 = compareGLError(gl.NO_ERROR, "gl.depthRange(0, 1)");
    var re42 = compareGLError(gl.NO_ERROR, "gl.detachShader(program, shader)");
    var re43 = compareGLError(gl.NO_ERROR, "gl.disable(gl.BLEND)");
    var re44 = compareGLError(gl.NO_ERROR, "gl.disableVertexAttribArray(0)");
    var re45 = compareGLError(gl.NO_ERROR, "gl.drawArrays(gl.POINTS, 0, 0)");
    var re46 = compareGLError(gl.NO_ERROR, "gl.drawElements(gl.POINTS, 0, gl.UNSIGNED_SHORT, 0)");
    var re47 = compareGLError(gl.NO_ERROR, "gl.enable(gl.BLEND)");
    var re48 = compareGLError(gl.NO_ERROR, "gl.enableVertexAttribArray(0)");
    var re49 = compareGLError(gl.NO_ERROR, "gl.finish()");
    var re50 = compareGLError(gl.NO_ERROR, "gl.flush()");
    var re51 = compareGLError(gl.NO_ERROR, "gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, renderbuffer)");
    var re52 = compareGLError(gl.NO_ERROR, "gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0)");
    var re53 = compareGLError(gl.NO_ERROR, "gl.frontFace(gl.CW)");
    var re54 = compareGLError(gl.NO_ERROR, "gl.generateMipmap(gl.TEXTURE_2D)");
    var re55 = compareGLError(gl.NO_ERROR, "gl.hint(gl.GENERATE_MIPMAP_HINT, gl.FASTEST)");
    var re56 = compareGLError(gl.NO_ERROR, "gl.lineWidth(0)");
    var re57 = compareGLError(gl.NO_ERROR, "gl.linkProgram(program)");
    var re58 = compareGLError(gl.NO_ERROR, "gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0)");
    var re59 = compareGLError(gl.NO_ERROR, "gl.polygonOffset(0, 0)");
    var re60 = compareGLError(gl.NO_ERROR, "gl.readPixels(0, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, arrayBufferView)");
    var re61 = compareGLError(gl.NO_ERROR, "gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, 0, 0)");
    var re62 = compareGLError(gl.NO_ERROR, "gl.sampleCoverage(0, 0)");
    var re63 = compareGLError(gl.NO_ERROR, "gl.scissor(0, 0, 0, 0)");
    var re64 = compareGLError(gl.NO_ERROR, "gl.shaderSource(shader, '')");
    var re65 = compareGLError(gl.NO_ERROR, "gl.stencilFunc(gl.NEVER, 0, 0)");
    var re66 = compareGLError(gl.NO_ERROR, "gl.stencilFuncSeparate(gl.FRONT, gl.NEVER, 0, 0)");
    var re67 = compareGLError(gl.NO_ERROR, "gl.stencilMask(0)");
    var re68 = compareGLError(gl.NO_ERROR, "gl.stencilMaskSeparate(gl.FRONT, 0)");
    var re69 = compareGLError(gl.NO_ERROR, "gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP)");
    var re70 = compareGLError(gl.NO_ERROR, "gl.stencilOpSeparate(gl.FRONT, gl.KEEP, gl.KEEP, gl.KEEP)");
    var re71 = compareGLError(gl.NO_ERROR, "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, arrayBufferView)");
    var re72 = compareGLError(gl.NO_ERROR, "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, imageData)");
    var re73 = compareGLError(gl.NO_ERROR, "gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)");
    var re74 = compareGLError(gl.NO_ERROR, "gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)");
    var re75 = compareGLError(gl.NO_ERROR, "gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, arrayBufferView)");
    var re76 = compareGLError(gl.NO_ERROR, "gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, imageData)");
    var re77 = compareGLError(gl.NO_ERROR, "gl.uniform1f(uniformLocation, 0)");
    var re78 = compareGLError(gl.NO_ERROR, "gl.uniform1fv(uniformLocation, float32array)");
    var re79 = compareGLError(gl.NO_ERROR, "gl.uniform1fv(uniformLocation, [0])");
    var re80 = compareGLError(gl.NO_ERROR, "gl.uniform1i(uniformLocation, 0)");
    var re81 = compareGLError(gl.NO_ERROR, "gl.uniform1iv(uniformLocation, int32array)");
    var re82 = compareGLError(gl.NO_ERROR, "gl.uniform1iv(uniformLocation, [0])");
    var re83 = compareGLError(gl.NO_ERROR, "gl.uniform2f(uniformLocation, 0, 0)");
    var re84 = compareGLError(gl.NO_ERROR, "gl.uniform2fv(uniformLocation, float32array)");
    var re85 = compareGLError(gl.NO_ERROR, "gl.uniform2fv(uniformLocation, [0, 0])");
    var re86 = compareGLError(gl.NO_ERROR, "gl.uniform2i(uniformLocation, 0, 0)");
    var re87 = compareGLError(gl.NO_ERROR, "gl.uniform2iv(uniformLocation, int32array)");
    var re88 = compareGLError(gl.NO_ERROR, "gl.uniform2iv(uniformLocation, [0, 0])");
    var re89 = compareGLError(gl.NO_ERROR, "gl.uniform3f(uniformLocation, 0, 0, 0)");
    var re90 = compareGLError(gl.NO_ERROR, "gl.uniform3fv(uniformLocation, float32array)");
    var re91 = compareGLError(gl.NO_ERROR, "gl.uniform3fv(uniformLocation, [0, 0, 0])");
    var re92 = compareGLError(gl.NO_ERROR, "gl.uniform3i(uniformLocation, 0, 0, 0)");
    var re93 = compareGLError(gl.NO_ERROR, "gl.uniform3iv(uniformLocation, int32array)");
    var re94 = compareGLError(gl.NO_ERROR, "gl.uniform3iv(uniformLocation, [0, 0, 0])");
    var re95 = compareGLError(gl.NO_ERROR, "gl.uniform4f(uniformLocation, 0, 0, 0, 0)");
    var re96 = compareGLError(gl.NO_ERROR, "gl.uniform4fv(uniformLocation, float32array)");
    var re97 = compareGLError(gl.NO_ERROR, "gl.uniform4fv(uniformLocation, [0, 0, 0, 0])");
    var re98 = compareGLError(gl.NO_ERROR, "gl.uniform4i(uniformLocation, 0, 0, 0, 0)");
    var re99 = compareGLError(gl.NO_ERROR, "gl.uniform4iv(uniformLocation, int32array)");
    var re100 = compareGLError(gl.NO_ERROR, "gl.uniform4iv(uniformLocation, [0, 0, 0, 0])");
    var re101 = compareGLError(gl.NO_ERROR, "gl.uniformMatrix2fv(uniformLocation, false, float32array)");
    var re102 = compareGLError(gl.NO_ERROR, "gl.uniformMatrix2fv(uniformLocation, false, [0, 0, 0, 0])");
    var re103 = compareGLError(gl.NO_ERROR, "gl.uniformMatrix3fv(uniformLocation, false, float32array)");
    var re104 = compareGLError(gl.NO_ERROR, "gl.uniformMatrix3fv(uniformLocation, false, [0, 0, 0, 0, 0, 0, 0, 0, 0])");
    var re105 = compareGLError(gl.NO_ERROR, "gl.uniformMatrix4fv(uniformLocation, false, float32array)");
    var re106 = compareGLError(gl.NO_ERROR, "gl.uniformMatrix4fv(uniformLocation, false, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])");
    var re107 = compareGLError(gl.NO_ERROR, "gl.useProgram(program)");
    var re108 = compareGLError(gl.NO_ERROR, "gl.validateProgram(program)");
    var re109 = compareGLError(gl.NO_ERROR, "gl.vertexAttrib1f(0, 0)");
    var re110 = compareGLError(gl.NO_ERROR, "gl.vertexAttrib1fv(0, float32array)");
    var re111 = compareGLError(gl.NO_ERROR, "gl.vertexAttrib1fv(0, [0])");
    var re112 = compareGLError(gl.NO_ERROR, "gl.vertexAttrib2f(0, 0, 0)");
    var re113 = compareGLError(gl.NO_ERROR, "gl.vertexAttrib2fv(0, float32array)");
    var re114 = compareGLError(gl.NO_ERROR, "gl.vertexAttrib2fv(0, [0, 0])");
    var re115 = compareGLError(gl.NO_ERROR, "gl.vertexAttrib3f(0, 0, 0, 0)");
    var re116 = compareGLError(gl.NO_ERROR, "gl.vertexAttrib3fv(0, float32array)");
    var re117 = compareGLError(gl.NO_ERROR, "gl.vertexAttrib3fv(0, [0, 0, 0])");
    var re118 = compareGLError(gl.NO_ERROR, "gl.vertexAttrib4f(0, 0, 0, 0, 0)");
    var re119 = compareGLError(gl.NO_ERROR, "gl.vertexAttrib4fv(0, float32array)");
    var re120 = compareGLError(gl.NO_ERROR, "gl.vertexAttrib4fv(0, [0, 0, 0, 0])");
    var re121 = compareGLError(gl.NO_ERROR, "gl.vertexAttribPointer(0, 0, gl.FLOAT, false, 0, 0)");
    var re122 = compareGLError(gl.NO_ERROR, "gl.viewport(0, 0, 0, 0)");

    // Test a set of functions that should return null
    var re123 = (gl.createBuffer() == null);
    var re124 = (gl.createFramebuffer() == null);
    var re125 = (gl.createProgram() == null);
    var re126 = (gl.createRenderbuffer() == null);
    var re127 = (gl.createShader(gl.GL_VERTEX_SHADER) == null);
    var re128 = (gl.createTexture() == null);
    var re129 = (gl.getActiveAttrib(program, 0) == null);
    var re130 = (gl.getActiveUniform(program, 0) == null);
    var re131 = (gl.getAttachedShaders(program) == null);
    var re132 = (gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE) == null);
    var re133 = (gl.getContextAttributes() == null);
    var re134 = (gl.getFramebufferAttachmentParameter(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.FRAMEBUFFER_ATTACHMENT_OBJECT_NAME) == null);
    var re135 = (gl.getParameter(gl.CURRENT_PROGRAM) == null);
    var re136 = (gl.getProgramInfoLog(program) == null);
    var re137 = (gl.getProgramParameter(program, gl.LINK_STATUS) == null);
    var re138 = (gl.getRenderbufferParameter(gl.RENDERBUFFER, gl.RENDERBUFFER_WIDTH) == null);
    var re139 = (gl.getShaderInfoLog(shader) == null);
    var re140 = (gl.getShaderParameter(shader, gl.SHADER_TYPE) == null);
    var re141 = (gl.getShaderSource(shader) == null);
    var re142 = (gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S) == null);
    var re143 = (gl.getUniform(program, uniformLocation) == null);
    var re144 = (gl.getUniformLocation(program, 'vPosition') == null);
    var re145 = (gl.getVertexAttrib(0, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING) == null);
    var re146 = (gl.getSupportedExtensions() == null);
    var re147 = (eval("gl.getExtension('" + extensionName + "')") == null);

    // "Is" queries should all return false.
    var re148 = !(gl.isBuffer(buffer));
    var re149 = !(gl.isEnabled(gl.BLEND));
    var re150 = !(gl.isFramebuffer(framebuffer));
    var re151 = !(gl.isProgram(program));
    var re152 = !(gl.isRenderbuffer(renderbuffer));
    var re153 = !(gl.isShader(shader));
    var re154 = !(gl.isTexture(texture));

    var re155 = (gl.getError() == gl.NO_ERROR);

    // test extensions
    var re156 = compareGLError(gl.NO_ERROR, "OES_vertex_array_object.bindVertexArrayOES(vertexArrayObject)");
    var re157 = compareGLError(gl.NO_ERROR, "OES_vertex_array_object.isVertexArrayOES(vertexArrayObject)");
    var re158 = compareGLError(gl.NO_ERROR, "OES_vertex_array_object.deleteVertexArrayOES(vertexArrayObject)");
    var re159 = (OES_vertex_array_object.createVertexArrayOES() == null);

    lostContextResults = [re1, re2, re3, re4, re5, re6, re7, re8, re9, re10,
                          re11, re12, re13, re14, re15, re16, re17, re18, re19, re20,
                          re21, re22, re23, re24, re25, re26, re27, re28, re29, re30,
                          re31, re32, re33, re34, re35, re36, re37, re38, re39, re40,
                          re41, re42, re43, re44, re45, re46, re47, re48, re49, re50,
                          re51, re52, re53, re54, re55, re56, re57, re58, re59, re60,
                          re61, re62, re63, re64, re65, re66, re67, re68, re69, re70,
                          re71, re72, re73, re74, re75, re76, re77, re78, re79, re80,
                          re81, re82, re83, re84, re85, re86, re87, re88, re89, re90,
                          re91, re92, re93, re94, re95, re96, re97, re98, re99, re100,
                          re101, re102, re103, re104, re105, re106, re107, re108, re109, re110,
                          re111, re112, re113, re114, re115, re116, re117, re118, re119, re120,
                          re121, re122, re123, re124, re125, re126, re127, re128, re129, re130,
                          re131, re132, re133, re134, re135, re136, re137, re138, re139, re140,
                          re141, re142, re143, re144, re145, re146, re147, re148, re149, re150,
                          re151, re152, re153, re154, re155, re156, re157, re158, re159];
}

self.onmessage = function(e) {
    canvas = new OffscreenCanvas(10, 10);
    gl = canvas.getContext('webgl');

    // call testValidContext() before checking for the extension, because this is where we check
    // for the isContextLost() method, which we want to do regardless of the extension's presence.
    testValidContext();

    extensionName = "WEBGL_lose_context";
    extension = gl.getExtension(extensionName);
    // need an extension that exposes new API methods.
    OES_vertex_array_object = gl.getExtension("OES_vertex_array_object");

    canvas.addEventListener("webglcontextlost", function() {
        testLostContext();
        self.postMessage({validRe: validContextResults, lostRe: lostContextResults});
    });
    // We need to initialize |uniformLocation| before losing context.
    // Otherwise gl.getUniform() when context is lost will throw.
    uniformLocation = gl.getUniformLocation(program, "tex");
    loseContext();
};

</script>

<script>
function makeWorker(script)
{
   var blob = new Blob([script]);
   return new Worker(URL.createObjectURL(blob));
}

var t = async_test('Test that WebGL context loss events can be handled with OffscreenCanvas in a worker');
var worker = makeWorker(document.getElementById("myWorker").textContent);
worker.addEventListener('message', t.step_func(function(msg) {
    var validResults = msg.data.validRe;
    var lostResults = msg.data.lostRe;
    assert_equals(validResults.length, 10);
    assert_equals(lostResults.length, 159);
    for (var i = 0; i < validResults.length; i++)
        assert_true(validResults[i], 'Result from testValidContext() should all be true');
    for (var i = 0; i < lostResults.length; i++)
        assert_true(lostResults[i], 'Result from testLostContext() should all be true');
    t.done();
}));
worker.postMessage('start');
</script>
</html>