OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head> |
| 4 <meta charset="utf-8"> |
| 5 <!-- |
| 6 |
| 7 /* |
| 8 ** Copyright (c) 2012 The Khronos Group Inc. |
| 9 ** |
| 10 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 11 ** copy of this software and/or associated documentation files (the |
| 12 ** "Materials"), to deal in the Materials without restriction, including |
| 13 ** without limitation the rights to use, copy, modify, merge, publish, |
| 14 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 15 ** permit persons to whom the Materials are furnished to do so, subject to |
| 16 ** the following conditions: |
| 17 ** |
| 18 ** The above copyright notice and this permission notice shall be included |
| 19 ** in all copies or substantial portions of the Materials. |
| 20 ** |
| 21 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 23 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 24 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 25 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 26 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 27 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 28 */ |
| 29 |
| 30 --> |
| 31 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 32 <script src="../../resources/js-test-pre.js"></script> |
| 33 <script src="../resources/webgl-test.js"></script> |
| 34 <script src="../resources/webgl-test-utils.js"></script> |
| 35 <script> |
| 36 var wtu = WebGLTestUtils; |
| 37 var canvas; |
| 38 var gl; |
| 39 var shouldGenerateGLError; |
| 40 var extensionNames = [ |
| 41 "WEBKIT_WEBGL_lose_context", |
| 42 "MOZ_WEBGL_lose_context", |
| 43 ]; |
| 44 var extension; |
| 45 var bufferObjects; |
| 46 var program; |
| 47 var texture; |
| 48 var texColor = [255, 10, 20, 255]; |
| 49 var allowRestore; |
| 50 var contextLostEventFired; |
| 51 var contextRestoredEventFired; |
| 52 |
| 53 function init() |
| 54 { |
| 55 if (window.initNonKhronosFramework) { |
| 56 window.initNonKhronosFramework(true); |
| 57 } |
| 58 |
| 59 description("Tests behavior under a restored context."); |
| 60 |
| 61 shouldGenerateGLError = wtu.shouldGenerateGLError; |
| 62 testLosingContext(); |
| 63 } |
| 64 |
| 65 function setupTest() |
| 66 { |
| 67 canvas = document.createElement("canvas"); |
| 68 canvas.width = 1; |
| 69 canvas.height = 1; |
| 70 gl = wtu.create3DContext(canvas); |
| 71 for (var ii = 0; ii < extensionNames.length; ++ii) { |
| 72 extension = gl.getExtension(extensionNames[ii]); |
| 73 if (extension) |
| 74 break; |
| 75 } |
| 76 if (!extension) { |
| 77 debug("Could not find lose_context extension under the following names:
" + extensionNames.join(" ")); |
| 78 return false; |
| 79 } |
| 80 return true; |
| 81 } |
| 82 |
| 83 function testLosingContext() |
| 84 { |
| 85 if (!setupTest()) |
| 86 finishTest(); |
| 87 |
| 88 debug("Test losing a context and inability to restore it."); |
| 89 |
| 90 canvas.addEventListener("webglcontextlost", function(e) { |
| 91 testLostContext(e); |
| 92 // restore the context after this event has exited. |
| 93 setTimeout(function() { |
| 94 // we didn't call prevent default so we should not be able to restore t
he context |
| 95 shouldGenerateGLError(gl, gl.INVALID_OPERATION, "extension.restoreConte
xt()"); |
| 96 testLosingAndRestoringContext(); |
| 97 }, 0); |
| 98 }); |
| 99 canvas.addEventListener("webglcontextrestored", testShouldNotRestoreContext)
; |
| 100 allowRestore = false; |
| 101 contextLostEventFired = false; |
| 102 contextRestoredEventFired = false; |
| 103 |
| 104 testOriginalContext(); |
| 105 extension.loseContext(); |
| 106 // The context should be lost immediately. |
| 107 shouldBeTrue("gl.isContextLost()"); |
| 108 shouldBe("gl.getError()", "gl.CONTEXT_LOST_WEBGL"); |
| 109 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 110 // gl methods should be no-ops |
| 111 shouldGenerateGLError(gl, gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTU
RE_CUBE_MAP)"); |
| 112 // but the event should not have been fired. |
| 113 shouldBeFalse("contextLostEventFired"); |
| 114 } |
| 115 |
| 116 function testLosingAndRestoringContext() |
| 117 { |
| 118 if (!setupTest()) |
| 119 finishTest(); |
| 120 |
| 121 debug(""); |
| 122 debug("Test losing and restoring a context."); |
| 123 |
| 124 canvas.addEventListener("webglcontextlost", function(e) { |
| 125 testLostContext(e); |
| 126 // restore the context after this event has exited. |
| 127 setTimeout(function() { |
| 128 shouldGenerateGLError(gl, gl.NO_ERROR, "extension.restoreContext()"); |
| 129 // The context should still be lost. It will not get restored until the |
| 130 // webglrestorecontext event is fired. |
| 131 shouldBeTrue("gl.isContextLost()"); |
| 132 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 133 // gl methods should still be no-ops |
| 134 shouldGenerateGLError(gl, gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.T
EXTURE_CUBE_MAP)"); |
| 135 }, 0); |
| 136 }); |
| 137 canvas.addEventListener("webglcontextrestored", function() { |
| 138 testRestoredContext(); |
| 139 finishTest(); |
| 140 }); |
| 141 allowRestore = true; |
| 142 contextLostEventFired = false; |
| 143 contextRestoredEventFired = false; |
| 144 |
| 145 testOriginalContext(); |
| 146 extension.loseContext(); |
| 147 // The context should be lost immediately. |
| 148 shouldBeTrue("gl.isContextLost()"); |
| 149 shouldBe("gl.getError()", "gl.CONTEXT_LOST_WEBGL"); |
| 150 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 151 // gl methods should be no-ops |
| 152 shouldGenerateGLError(gl, gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTU
RE_CUBE_MAP)"); |
| 153 // but the event should not have been fired. |
| 154 shouldBeFalse("contextLostEventFired"); |
| 155 } |
| 156 |
| 157 function testRendering() |
| 158 { |
| 159 gl.clearColor(0, 0, 0, 255); |
| 160 gl.colorMask(1, 1, 1, 0); |
| 161 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 162 |
| 163 program = wtu.setupSimpleTextureProgram(gl); |
| 164 bufferObjects = wtu.setupUnitQuad(gl); |
| 165 texture = wtu.createColoredTexture(gl, canvas.width, canvas.height, texColor
); |
| 166 |
| 167 gl.uniform1i(gl.getUniformLocation(program, "tex"), 0); |
| 168 wtu.drawQuad(gl, [0, 0, 0, 255]); |
| 169 |
| 170 var compare = texColor.slice(0, 3); |
| 171 wtu.checkCanvasRect(gl, 0, 0, canvas.width, canvas.height, compare, "shouldB
e " + compare); |
| 172 |
| 173 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 174 } |
| 175 |
| 176 function testOriginalContext() |
| 177 { |
| 178 debug("Test valid context"); |
| 179 shouldBeFalse("gl.isContextLost()"); |
| 180 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 181 testRendering(); |
| 182 debug(""); |
| 183 } |
| 184 |
| 185 function testLostContext(e) |
| 186 { |
| 187 debug("Test lost context"); |
| 188 shouldBeFalse("contextLostEventFired"); |
| 189 contextLostEventFired = true; |
| 190 shouldBeTrue("gl.isContextLost()"); |
| 191 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 192 debug(""); |
| 193 if (allowRestore) |
| 194 e.preventDefault(); |
| 195 } |
| 196 |
| 197 function testShouldNotRestoreContext(e) |
| 198 { |
| 199 testFailed("Should not restore the context unless preventDefault is called o
n the context lost event"); |
| 200 debug(""); |
| 201 } |
| 202 |
| 203 function testResources(expected) |
| 204 { |
| 205 var tests = [ |
| 206 "gl.bindTexture(gl.TEXTURE_2D, texture)", |
| 207 "gl.useProgram(program)", |
| 208 "gl.bindBuffer(gl.ARRAY_BUFFER, bufferObjects[0])", |
| 209 ]; |
| 210 |
| 211 for (var i = 0; i < tests.length; ++i) |
| 212 shouldGenerateGLError(gl, expected, tests[i]); |
| 213 } |
| 214 |
| 215 function testRestoredContext() |
| 216 { |
| 217 debug("Test restored context"); |
| 218 shouldBeFalse("contextRestoredEventFired"); |
| 219 contextRestoredEventFired = true; |
| 220 shouldBeFalse("gl.isContextLost()"); |
| 221 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 222 |
| 223 // Validate that using old resources fails. |
| 224 testResources(gl.INVALID_OPERATION); |
| 225 |
| 226 testRendering(); |
| 227 |
| 228 // Validate new resources created in testRendering(). |
| 229 testResources(gl.NO_ERROR); |
| 230 debug(""); |
| 231 } |
| 232 |
| 233 |
| 234 </script> |
| 235 </head> |
| 236 <body onload="init()"> |
| 237 <div id="description"></div> |
| 238 <div id="console"></div> |
| 239 </body> |
| 240 </html> |
OLD | NEW |