OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head> |
| 4 <meta charset="utf-8"> |
| 5 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 6 <script src="../../resources/js-test-pre.js"></script> |
| 7 <script src="../resources/webgl-test.js"></script> |
| 8 <script src="../resources/webgl-test-utils.js"></script> |
| 9 <script> |
| 10 var wtu = WebGLTestUtils; |
| 11 var canvas; |
| 12 var gl; |
| 13 var shouldGenerateGLError; |
| 14 var extension_name = "WEBKIT_lose_context"; |
| 15 var extension; |
| 16 var bufferObjects; |
| 17 var program; |
| 18 var texture; |
| 19 var texColor = [255, 10, 20, 255]; |
| 20 var allowRestore; |
| 21 |
| 22 function init() |
| 23 { |
| 24 if (window.initNonKhronosFramework) { |
| 25 window.initNonKhronosFramework(true); |
| 26 } |
| 27 |
| 28 description("Tests behavior under a restored context."); |
| 29 |
| 30 shouldGenerateGLError = wtu.shouldGenerateGLError; |
| 31 runTests(); |
| 32 } |
| 33 |
| 34 function runTests() |
| 35 { |
| 36 testLosingContext(); |
| 37 testLosingAndRestoringContext(); |
| 38 |
| 39 finish(); |
| 40 } |
| 41 |
| 42 function setupTest() |
| 43 { |
| 44 canvas = document.createElement("canvas"); |
| 45 canvas.width = 1; |
| 46 canvas.height = 1; |
| 47 gl = wtu.create3DContext(canvas); |
| 48 extension = gl.getExtension(extension_name); |
| 49 if (!extension) { |
| 50 debug(extension_name + " extension not found."); |
| 51 return false; |
| 52 } |
| 53 return true; |
| 54 } |
| 55 |
| 56 function testLosingContext() |
| 57 { |
| 58 if (!setupTest()) |
| 59 return; |
| 60 |
| 61 debug("Test losing a context and inability to restore it."); |
| 62 |
| 63 canvas.addEventListener("webglcontextlost", testLostContext); |
| 64 canvas.addEventListener("webglcontextrestored", testShouldNotRestoreContext)
; |
| 65 allowRestore = false; |
| 66 |
| 67 testOriginalContext(); |
| 68 extension.loseContext(); |
| 69 shouldGenerateGLError(gl, gl.INVALID_OPERATION, "extension.restoreContext()"
); |
| 70 debug(""); |
| 71 } |
| 72 |
| 73 function testLosingAndRestoringContext() |
| 74 { |
| 75 if (!setupTest()) |
| 76 return; |
| 77 |
| 78 debug("Test losing and restoring a context."); |
| 79 |
| 80 canvas.addEventListener("webglcontextlost", testLostContext); |
| 81 canvas.addEventListener("webglcontextrestored", testRestoredContext); |
| 82 allowRestore = true; |
| 83 |
| 84 testOriginalContext(); |
| 85 extension.loseContext(); |
| 86 shouldGenerateGLError(gl, gl.NO_ERROR, "extension.restoreContext()"); |
| 87 debug(""); |
| 88 } |
| 89 |
| 90 function testRendering() |
| 91 { |
| 92 gl.clearColor(0, 0, 0, 255); |
| 93 gl.colorMask(1, 1, 1, 0); |
| 94 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 95 |
| 96 program = wtu.setupSimpleTextureProgram(gl); |
| 97 bufferObjects = wtu.setupUnitQuad(gl); |
| 98 texture = wtu.createColoredTexture(gl, canvas.width, canvas.height, texColor
); |
| 99 |
| 100 gl.uniform1i(gl.getUniformLocation(program, "tex"), 0); |
| 101 wtu.drawQuad(gl, [0, 0, 0, 255]); |
| 102 |
| 103 var compare = texColor.slice(0, 3); |
| 104 wtu.checkCanvasRect(gl, 0, 0, canvas.width, canvas.height, compare, "shouldB
e " + compare); |
| 105 |
| 106 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 107 } |
| 108 |
| 109 function testOriginalContext() |
| 110 { |
| 111 debug("Test valid context"); |
| 112 shouldBeFalse("gl.isContextLost()"); |
| 113 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 114 testRendering(); |
| 115 debug(""); |
| 116 } |
| 117 |
| 118 function testLostContext(e) |
| 119 { |
| 120 debug("Test lost context"); |
| 121 shouldBeTrue("gl.isContextLost()"); |
| 122 shouldBe("gl.getError()", "gl.CONTEXT_LOST_WEBGL"); |
| 123 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 124 debug(""); |
| 125 if (allowRestore) |
| 126 e.preventDefault(); |
| 127 } |
| 128 |
| 129 function testShouldNotRestoreContext(e) |
| 130 { |
| 131 testFailed("Should not restore the context unless preventDefault is called o
n the context lost event"); |
| 132 debug(""); |
| 133 } |
| 134 |
| 135 function testResources(expected) |
| 136 { |
| 137 var tests = [ |
| 138 "gl.bindTexture(gl.TEXTURE_2D, texture)", |
| 139 "gl.useProgram(program)", |
| 140 "gl.bindBuffer(gl.ARRAY_BUFFER, bufferObjects[0])", |
| 141 ]; |
| 142 |
| 143 for (var i = 0; i < tests.length; ++i) |
| 144 shouldGenerateGLError(gl, expected, tests[i]); |
| 145 } |
| 146 |
| 147 function testRestoredContext() |
| 148 { |
| 149 debug("Test restored context"); |
| 150 shouldBeFalse("gl.isContextLost()"); |
| 151 shouldBe("gl.getError()", "gl.NO_ERROR"); |
| 152 |
| 153 // Validate that using old resources fails. |
| 154 testResources(gl.INVALID_OPERATION); |
| 155 |
| 156 testRendering(); |
| 157 |
| 158 // Validate new resources created in testRendering(). |
| 159 testResources(gl.NO_ERROR); |
| 160 debug(""); |
| 161 } |
| 162 |
| 163 function finish() { |
| 164 successfullyParsed = true; |
| 165 var epilogue = document.createElement("script"); |
| 166 epilogue.onload = function() { |
| 167 if (window.nonKhronosFrameworkNotifyDone) |
| 168 window.nonKhronosFrameworkNotifyDone(); |
| 169 }; |
| 170 epilogue.src = "../../resources/js-test-post.js"; |
| 171 document.body.appendChild(epilogue); |
| 172 } |
| 173 |
| 174 </script> |
| 175 </head> |
| 176 <body onload="init()"> |
| 177 <div id="description"></div> |
| 178 <div id="console"></div> |
| 179 </body> |
| 180 </html> |
OLD | NEW |