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

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 <!DOCTYPE html>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
michael@0 5 <script src="../../resources/js-test-pre.js"></script>
michael@0 6 <script src="../resources/webgl-test.js"></script>
michael@0 7 <script src="../resources/webgl-test-utils.js"></script>
michael@0 8 <script>
michael@0 9 var wtu = WebGLTestUtils;
michael@0 10 var canvas;
michael@0 11 var gl;
michael@0 12 var shouldGenerateGLError;
michael@0 13 var extensionNames = [
michael@0 14 "WEBKIT_WEBGL_lose_context",
michael@0 15 "MOZ_WEBGL_lose_context",
michael@0 16 ];
michael@0 17 var extension;
michael@0 18 var bufferObjects;
michael@0 19 var program;
michael@0 20 var texture;
michael@0 21 var texColor = [255, 10, 20, 255];
michael@0 22 var allowRestore;
michael@0 23 var contextLostEventFired;
michael@0 24 var contextRestoredEventFired;
michael@0 25
michael@0 26 function init()
michael@0 27 {
michael@0 28 if (window.initNonKhronosFramework) {
michael@0 29 window.initNonKhronosFramework(true);
michael@0 30 }
michael@0 31
michael@0 32 description("Tests behavior under a restored context.");
michael@0 33
michael@0 34 shouldGenerateGLError = wtu.shouldGenerateGLError;
michael@0 35 testLosingContext();
michael@0 36 }
michael@0 37
michael@0 38 function setupTest()
michael@0 39 {
michael@0 40 canvas = document.createElement("canvas");
michael@0 41 canvas.width = 1;
michael@0 42 canvas.height = 1;
michael@0 43 gl = wtu.create3DContext(canvas);
michael@0 44 for (var ii = 0; ii < extensionNames.length; ++ii) {
michael@0 45 extension = gl.getExtension(extensionNames[ii]);
michael@0 46 if (extension)
michael@0 47 break;
michael@0 48 }
michael@0 49 if (!extension) {
michael@0 50 debug("Could not find lose_context extension under the following names: " + extensionNames.join(" "));
michael@0 51 return false;
michael@0 52 }
michael@0 53 return true;
michael@0 54 }
michael@0 55
michael@0 56 function testLosingContext()
michael@0 57 {
michael@0 58 if (!setupTest())
michael@0 59 finishTest();
michael@0 60
michael@0 61 debug("Test losing a context and inability to restore it.");
michael@0 62
michael@0 63 canvas.addEventListener("webglcontextlost", function(e) {
michael@0 64 testLostContext(e);
michael@0 65 // restore the context after this event has exited.
michael@0 66 setTimeout(function() {
michael@0 67 // we didn't call prevent default so we should not be able to restore the context
michael@0 68 shouldGenerateGLError(gl, gl.INVALID_OPERATION, "extension.restoreContext()");
michael@0 69 testLosingAndRestoringContext();
michael@0 70 }, 0);
michael@0 71 });
michael@0 72 canvas.addEventListener("webglcontextrestored", testShouldNotRestoreContext);
michael@0 73 allowRestore = false;
michael@0 74 contextLostEventFired = false;
michael@0 75 contextRestoredEventFired = false;
michael@0 76
michael@0 77 testOriginalContext();
michael@0 78 extension.loseContext();
michael@0 79 // The context should be lost immediately.
michael@0 80 shouldBeTrue("gl.isContextLost()");
michael@0 81 shouldBe("gl.getError()", "gl.CONTEXT_LOST_WEBGL");
michael@0 82 shouldBe("gl.getError()", "gl.NO_ERROR");
michael@0 83 // gl methods should be no-ops
michael@0 84 shouldGenerateGLError(gl, gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP)");
michael@0 85 // but the event should not have been fired.
michael@0 86 shouldBeFalse("contextLostEventFired");
michael@0 87 }
michael@0 88
michael@0 89 function testLosingAndRestoringContext()
michael@0 90 {
michael@0 91 if (!setupTest())
michael@0 92 finishTest();
michael@0 93
michael@0 94 debug("");
michael@0 95 debug("Test losing and restoring a context.");
michael@0 96
michael@0 97 canvas.addEventListener("webglcontextlost", function(e) {
michael@0 98 testLostContext(e);
michael@0 99 // restore the context after this event has exited.
michael@0 100 setTimeout(function() {
michael@0 101 shouldGenerateGLError(gl, gl.NO_ERROR, "extension.restoreContext()");
michael@0 102 // The context should still be lost. It will not get restored until the
michael@0 103 // webglrestorecontext event is fired.
michael@0 104 shouldBeTrue("gl.isContextLost()");
michael@0 105 shouldBe("gl.getError()", "gl.NO_ERROR");
michael@0 106 // gl methods should still be no-ops
michael@0 107 shouldGenerateGLError(gl, gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP)");
michael@0 108 }, 0);
michael@0 109 });
michael@0 110 canvas.addEventListener("webglcontextrestored", function() {
michael@0 111 testRestoredContext();
michael@0 112 finishTest();
michael@0 113 });
michael@0 114 allowRestore = true;
michael@0 115 contextLostEventFired = false;
michael@0 116 contextRestoredEventFired = false;
michael@0 117
michael@0 118 testOriginalContext();
michael@0 119 extension.loseContext();
michael@0 120 // The context should be lost immediately.
michael@0 121 shouldBeTrue("gl.isContextLost()");
michael@0 122 shouldBe("gl.getError()", "gl.CONTEXT_LOST_WEBGL");
michael@0 123 shouldBe("gl.getError()", "gl.NO_ERROR");
michael@0 124 // gl methods should be no-ops
michael@0 125 shouldGenerateGLError(gl, gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP)");
michael@0 126 // but the event should not have been fired.
michael@0 127 shouldBeFalse("contextLostEventFired");
michael@0 128 }
michael@0 129
michael@0 130 function testRendering()
michael@0 131 {
michael@0 132 gl.clearColor(0, 0, 0, 255);
michael@0 133 gl.colorMask(1, 1, 1, 0);
michael@0 134 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 135
michael@0 136 program = wtu.setupSimpleTextureProgram(gl);
michael@0 137 bufferObjects = wtu.setupUnitQuad(gl);
michael@0 138 texture = wtu.createColoredTexture(gl, canvas.width, canvas.height, texColor);
michael@0 139
michael@0 140 gl.uniform1i(gl.getUniformLocation(program, "tex"), 0);
michael@0 141 wtu.drawQuad(gl, [0, 0, 0, 255]);
michael@0 142
michael@0 143 var compare = texColor.slice(0, 3);
michael@0 144 wtu.checkCanvasRect(gl, 0, 0, canvas.width, canvas.height, compare, "shouldBe " + compare);
michael@0 145
michael@0 146 shouldBe("gl.getError()", "gl.NO_ERROR");
michael@0 147 }
michael@0 148
michael@0 149 function testOriginalContext()
michael@0 150 {
michael@0 151 debug("Test valid context");
michael@0 152 shouldBeFalse("gl.isContextLost()");
michael@0 153 shouldBe("gl.getError()", "gl.NO_ERROR");
michael@0 154 testRendering();
michael@0 155 debug("");
michael@0 156 }
michael@0 157
michael@0 158 function testLostContext(e)
michael@0 159 {
michael@0 160 debug("Test lost context");
michael@0 161 shouldBeFalse("contextLostEventFired");
michael@0 162 contextLostEventFired = true;
michael@0 163 shouldBeTrue("gl.isContextLost()");
michael@0 164 shouldBe("gl.getError()", "gl.NO_ERROR");
michael@0 165 debug("");
michael@0 166 if (allowRestore)
michael@0 167 e.preventDefault();
michael@0 168 }
michael@0 169
michael@0 170 function testShouldNotRestoreContext(e)
michael@0 171 {
michael@0 172 testFailed("Should not restore the context unless preventDefault is called on the context lost event");
michael@0 173 debug("");
michael@0 174 }
michael@0 175
michael@0 176 function testResources(expected)
michael@0 177 {
michael@0 178 var tests = [
michael@0 179 "gl.bindTexture(gl.TEXTURE_2D, texture)",
michael@0 180 "gl.useProgram(program)",
michael@0 181 "gl.bindBuffer(gl.ARRAY_BUFFER, bufferObjects[0])",
michael@0 182 ];
michael@0 183
michael@0 184 for (var i = 0; i < tests.length; ++i)
michael@0 185 shouldGenerateGLError(gl, expected, tests[i]);
michael@0 186 }
michael@0 187
michael@0 188 function testRestoredContext()
michael@0 189 {
michael@0 190 debug("Test restored context");
michael@0 191 shouldBeFalse("contextRestoredEventFired");
michael@0 192 contextRestoredEventFired = true;
michael@0 193 shouldBeFalse("gl.isContextLost()");
michael@0 194 shouldBe("gl.getError()", "gl.NO_ERROR");
michael@0 195
michael@0 196 // Validate that using old resources fails.
michael@0 197 testResources(gl.INVALID_OPERATION);
michael@0 198
michael@0 199 testRendering();
michael@0 200
michael@0 201 // Validate new resources created in testRendering().
michael@0 202 testResources(gl.NO_ERROR);
michael@0 203 debug("");
michael@0 204 }
michael@0 205
michael@0 206
michael@0 207 </script>
michael@0 208 </head>
michael@0 209 <body onload="init()">
michael@0 210 <div id="description"></div>
michael@0 211 <div id="console"></div>
michael@0 212 </body>
michael@0 213 </html>

mercurial