content/canvas/test/webgl-conformance/conformance/context/context-lost-restored.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/canvas/test/webgl-conformance/conformance/context/context-lost-restored.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,213 @@
     1.4 +<!DOCTYPE html>
     1.5 +<html>
     1.6 +<head>
     1.7 +<link rel="stylesheet" href="../../resources/js-test-style.css"/>
     1.8 +<script src="../../resources/js-test-pre.js"></script>
     1.9 +<script src="../resources/webgl-test.js"></script>
    1.10 +<script src="../resources/webgl-test-utils.js"></script>
    1.11 +<script>
    1.12 +var wtu = WebGLTestUtils;
    1.13 +var canvas;
    1.14 +var gl;
    1.15 +var shouldGenerateGLError;
    1.16 +var extensionNames = [
    1.17 +    "WEBKIT_WEBGL_lose_context",
    1.18 +    "MOZ_WEBGL_lose_context",
    1.19 +];
    1.20 +var extension;
    1.21 +var bufferObjects;
    1.22 +var program;
    1.23 +var texture;
    1.24 +var texColor = [255, 10, 20, 255];
    1.25 +var allowRestore;
    1.26 +var contextLostEventFired;
    1.27 +var contextRestoredEventFired;
    1.28 +
    1.29 +function init()
    1.30 +{
    1.31 +    if (window.initNonKhronosFramework) {
    1.32 +        window.initNonKhronosFramework(true);
    1.33 +    }
    1.34 +
    1.35 +    description("Tests behavior under a restored context.");
    1.36 +
    1.37 +    shouldGenerateGLError = wtu.shouldGenerateGLError;
    1.38 +    testLosingContext();
    1.39 +}
    1.40 +
    1.41 +function setupTest()
    1.42 +{
    1.43 +    canvas = document.createElement("canvas");
    1.44 +    canvas.width = 1;
    1.45 +    canvas.height = 1;
    1.46 +    gl = wtu.create3DContext(canvas);
    1.47 +    for (var ii = 0; ii < extensionNames.length; ++ii) {
    1.48 +        extension = gl.getExtension(extensionNames[ii]);
    1.49 +        if (extension)
    1.50 +            break;
    1.51 +    }
    1.52 +    if (!extension) {
    1.53 +        debug("Could not find lose_context extension under the following names: " + extensionNames.join(" "));
    1.54 +        return false;
    1.55 +    }
    1.56 +    return true;
    1.57 +}
    1.58 +
    1.59 +function testLosingContext()
    1.60 +{
    1.61 +    if (!setupTest())
    1.62 +        finishTest();
    1.63 +
    1.64 +    debug("Test losing a context and inability to restore it.");
    1.65 +
    1.66 +    canvas.addEventListener("webglcontextlost", function(e) {
    1.67 +       testLostContext(e);
    1.68 +       // restore the context after this event has exited.
    1.69 +       setTimeout(function() {
    1.70 +         // we didn't call prevent default so we should not be able to restore the context
    1.71 +         shouldGenerateGLError(gl, gl.INVALID_OPERATION, "extension.restoreContext()");
    1.72 +         testLosingAndRestoringContext();
    1.73 +       }, 0);
    1.74 +    });
    1.75 +    canvas.addEventListener("webglcontextrestored", testShouldNotRestoreContext);
    1.76 +    allowRestore = false;
    1.77 +    contextLostEventFired = false;
    1.78 +    contextRestoredEventFired = false;
    1.79 +
    1.80 +    testOriginalContext();
    1.81 +    extension.loseContext();
    1.82 +    // The context should be lost immediately.
    1.83 +    shouldBeTrue("gl.isContextLost()");
    1.84 +    shouldBe("gl.getError()", "gl.CONTEXT_LOST_WEBGL");
    1.85 +    shouldBe("gl.getError()", "gl.NO_ERROR");
    1.86 +    // gl methods should be no-ops
    1.87 +    shouldGenerateGLError(gl, gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP)");
    1.88 +    // but the event should not have been fired.
    1.89 +    shouldBeFalse("contextLostEventFired");
    1.90 +}
    1.91 +
    1.92 +function testLosingAndRestoringContext()
    1.93 +{
    1.94 +    if (!setupTest())
    1.95 +        finishTest();
    1.96 +
    1.97 +    debug("");
    1.98 +    debug("Test losing and restoring a context.");
    1.99 +
   1.100 +    canvas.addEventListener("webglcontextlost", function(e) {
   1.101 +      testLostContext(e);
   1.102 +      // restore the context after this event has exited.
   1.103 +      setTimeout(function() {
   1.104 +        shouldGenerateGLError(gl, gl.NO_ERROR, "extension.restoreContext()");
   1.105 +        // The context should still be lost. It will not get restored until the 
   1.106 +        // webglrestorecontext event is fired.
   1.107 +        shouldBeTrue("gl.isContextLost()");
   1.108 +        shouldBe("gl.getError()", "gl.NO_ERROR");
   1.109 +        // gl methods should still be no-ops
   1.110 +        shouldGenerateGLError(gl, gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP)");
   1.111 +      }, 0);
   1.112 +    });
   1.113 +    canvas.addEventListener("webglcontextrestored", function() {
   1.114 +      testRestoredContext();
   1.115 +      finishTest();
   1.116 +    });
   1.117 +    allowRestore = true;
   1.118 +    contextLostEventFired = false;
   1.119 +    contextRestoredEventFired = false;
   1.120 +
   1.121 +    testOriginalContext();
   1.122 +    extension.loseContext();
   1.123 +    // The context should be lost immediately.
   1.124 +    shouldBeTrue("gl.isContextLost()");
   1.125 +    shouldBe("gl.getError()", "gl.CONTEXT_LOST_WEBGL");
   1.126 +    shouldBe("gl.getError()", "gl.NO_ERROR");
   1.127 +    // gl methods should be no-ops
   1.128 +    shouldGenerateGLError(gl, gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP)");
   1.129 +    // but the event should not have been fired.
   1.130 +    shouldBeFalse("contextLostEventFired");
   1.131 +}
   1.132 +
   1.133 +function testRendering()
   1.134 +{
   1.135 +    gl.clearColor(0, 0, 0, 255);
   1.136 +    gl.colorMask(1, 1, 1, 0);
   1.137 +    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   1.138 +
   1.139 +    program = wtu.setupSimpleTextureProgram(gl);
   1.140 +    bufferObjects = wtu.setupUnitQuad(gl);
   1.141 +    texture = wtu.createColoredTexture(gl, canvas.width, canvas.height, texColor);
   1.142 +
   1.143 +    gl.uniform1i(gl.getUniformLocation(program, "tex"), 0);
   1.144 +    wtu.drawQuad(gl, [0, 0, 0, 255]);
   1.145 +
   1.146 +    var compare = texColor.slice(0, 3);
   1.147 +    wtu.checkCanvasRect(gl, 0, 0, canvas.width, canvas.height, compare, "shouldBe " + compare);
   1.148 +
   1.149 +    shouldBe("gl.getError()", "gl.NO_ERROR");
   1.150 +}
   1.151 +
   1.152 +function testOriginalContext()
   1.153 +{
   1.154 +    debug("Test valid context");
   1.155 +    shouldBeFalse("gl.isContextLost()");
   1.156 +    shouldBe("gl.getError()", "gl.NO_ERROR");
   1.157 +    testRendering();
   1.158 +    debug("");
   1.159 +}
   1.160 +
   1.161 +function testLostContext(e)
   1.162 +{
   1.163 +    debug("Test lost context");
   1.164 +    shouldBeFalse("contextLostEventFired");
   1.165 +    contextLostEventFired = true;
   1.166 +    shouldBeTrue("gl.isContextLost()");
   1.167 +    shouldBe("gl.getError()", "gl.NO_ERROR");
   1.168 +    debug("");
   1.169 +    if (allowRestore)
   1.170 +      e.preventDefault();
   1.171 +}
   1.172 +
   1.173 +function testShouldNotRestoreContext(e)
   1.174 +{
   1.175 +    testFailed("Should not restore the context unless preventDefault is called on the context lost event");
   1.176 +    debug("");
   1.177 +}
   1.178 +
   1.179 +function testResources(expected)
   1.180 +{
   1.181 +    var tests = [
   1.182 +        "gl.bindTexture(gl.TEXTURE_2D, texture)",
   1.183 +        "gl.useProgram(program)",
   1.184 +        "gl.bindBuffer(gl.ARRAY_BUFFER, bufferObjects[0])",
   1.185 +    ];
   1.186 +
   1.187 +    for (var i = 0; i < tests.length; ++i)
   1.188 +        shouldGenerateGLError(gl, expected, tests[i]);
   1.189 +}
   1.190 +
   1.191 +function testRestoredContext()
   1.192 +{
   1.193 +    debug("Test restored context");
   1.194 +    shouldBeFalse("contextRestoredEventFired");
   1.195 +    contextRestoredEventFired = true;
   1.196 +    shouldBeFalse("gl.isContextLost()");
   1.197 +    shouldBe("gl.getError()", "gl.NO_ERROR");
   1.198 +
   1.199 +    // Validate that using old resources fails.
   1.200 +    testResources(gl.INVALID_OPERATION);
   1.201 +
   1.202 +    testRendering();
   1.203 +
   1.204 +    // Validate new resources created in testRendering().
   1.205 +    testResources(gl.NO_ERROR);
   1.206 +    debug("");
   1.207 +}
   1.208 +
   1.209 +
   1.210 +</script>
   1.211 +</head>
   1.212 +<body onload="init()">
   1.213 +<div id="description"></div>
   1.214 +<div id="console"></div>
   1.215 +</body>
   1.216 +</html>

mercurial