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