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>
9 window
.internals
.settings
.setWebGLErrorsToConsoleEnabled(false);
14 var shouldGenerateGLError
;
38 canvas
= document
.getElementById("canvas");
39 gl
= wtu
.create3DContext(canvas
);
40 shouldGenerateGLError
= wtu
.shouldGenerateGLError
;
42 description("Tests behavior under a lost context");
44 if (window
.initNonKhronosFramework
) {
45 window
.initNonKhronosFramework(true);
48 // call testValidContext() before checking for the extension, because this is where we check
49 // for the isContextLost() method, which we want to do regardless of the extension's presence.
52 extension
= gl
.getExtension("WEBGL_lose_context");
54 debug("Could not find lose_context extension under the following names: WEBGL_lose_context");
59 canvas
.addEventListener("webglcontextlost", testLostContext
, false);
64 function loseContext()
67 debug("Lose context");
69 // Note: this will cause the context to be lost, but the
70 // webglcontextlost event listener to be queued.
71 extension
.loseContext();
75 function testValidContext()
77 debug("Test valid context");
79 shouldBeFalse("gl.isContextLost()");
81 arrayBuffer
= new ArrayBuffer(4);
82 arrayBufferView
= new Int8Array(arrayBuffer
);
84 // Generate resources for testing.
85 buffer
= gl
.createBuffer();
86 gl
.bindBuffer(gl
.ARRAY_BUFFER
, buffer
);
87 framebuffer
= gl
.createFramebuffer();
88 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, framebuffer
);
89 program
= wtu
.setupSimpleTextureProgram(gl
);
90 renderbuffer
= gl
.createRenderbuffer();
91 gl
.bindRenderbuffer(gl
.RENDERBUFFER
, renderbuffer
);
92 shader
= gl
.createShader(gl
.VERTEX_SHADER
);
93 texture
= gl
.createTexture();
94 gl
.bindTexture(gl
.TEXTURE_2D
, texture
);
95 shouldBe("gl.getError()", "gl.NO_ERROR");
97 // Test is queries that will later be false
98 shouldGenerateGLError(gl
, gl
.NO_ERROR
, "gl.enable(gl.BLEND)");
99 shouldBeTrue("gl.isBuffer(buffer)");
100 shouldBeTrue("gl.isEnabled(gl.BLEND)");
101 shouldBeTrue("gl.isFramebuffer(framebuffer)");
102 shouldBeTrue("gl.isProgram(program)");
103 shouldBeTrue("gl.isRenderbuffer(renderbuffer)");
104 shouldBeTrue("gl.isShader(shader)");
105 shouldBeTrue("gl.isTexture(texture)");
108 function testLostContext()
110 debug("Test lost context");
112 // Functions with special return values.
113 shouldBeTrue("gl.isContextLost()");
114 shouldBe("gl.getError()", "gl.CONTEXT_LOST_WEBGL");
115 shouldBe("gl.getError()", "gl.NO_ERROR");
116 shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_UNSUPPORTED");
117 shouldBe("gl.getAttribLocation(program, 'u_modelViewProjMatrix')", "-1");
118 shouldBe("gl.getVertexAttribOffset(0, gl.VERTEX_ATTRIB_ARRAY_POINTER)", "0");
120 // Test the extension itself.
121 shouldGenerateGLError(gl
, gl
.INVALID_OPERATION
, "extension.loseContext()");
123 image
= document
.createElement("img");
124 video
= document
.createElement("video");
125 canvas2d
= document
.createElement("canvas");
126 ctx2d
= canvas2d
.getContext("2d");
127 imageData
= ctx2d
.createImageData(1, 1);
128 float32array
= new Float32Array(1);
129 int32array
= new Int32Array(1);
131 // Functions returning void should return immediately.
132 // This is untestable, but we can at least be sure they cause no errors
133 // and the codepaths are exercised.
135 "gl.activeTexture(gl.TEXTURE0)",
136 "gl.attachShader(program, shader)",
137 "gl.bindBuffer(gl.ARRAY_BUFFER, buffer)",
138 "gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer)",
139 "gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer)",
140 "gl.bindTexture(gl.TEXTURE_2D, texture)",
141 "gl.blendColor(1.0, 1.0, 1.0, 1.0)",
142 "gl.blendEquation(gl.FUNC_ADD)",
143 "gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD)",
144 "gl.blendFunc(gl.ONE, gl.ONE)",
145 "gl.blendFuncSeparate(gl.ONE, gl.ONE, gl.ONE, gl.ONE)",
146 "gl.bufferData(gl.ARRAY_BUFFER, 0, gl.STATIC_DRAW)",
147 "gl.bufferData(gl.ARRAY_BUFFER, arrayBufferView, gl.STATIC_DRAW)",
148 "gl.bufferData(gl.ARRAY_BUFFER, arrayBuffer, gl.STATIC_DRAW)",
149 "gl.bufferSubData(gl.ARRAY_BUFFRE, 0, arrayBufferView)",
150 "gl.bufferSubData(gl.ARRAY_BUFFRE, 0, arrayBuffer)",
151 "gl.clear(gl.COLOR_BUFFER_BIT)",
152 "gl.clearColor(1, 1, 1, 1)",
154 "gl.clearStencil(0)",
155 "gl.colorMask(1, 1, 1, 1)",
156 "gl.compileShader(shader)",
157 "gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, 0, 0, 0)",
158 "gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, 0, 0, 0)",
159 "gl.cullFace(gl.FRONT)",
160 "gl.deleteBuffer(buffer)",
161 "gl.deleteFramebuffer(framebuffer)",
162 "gl.deleteProgram(program)",
163 "gl.deleteRenderbuffer(renderbuffer)",
164 "gl.deleteShader(shader)",
165 "gl.deleteTexture(texture)",
166 "gl.depthFunc(gl.NEVER)",
168 "gl.depthRange(0, 1)",
169 "gl.detachShader(program, shader)",
170 "gl.disable(gl.BLEND)",
171 "gl.disableVertexAttribArray(0)",
172 "gl.drawArrays(gl.POINTS, 0, 0)",
173 "gl.drawElements(gl.POINTS, 0, gl.UNSIGNED_SHORT, 0)",
174 "gl.enable(gl.BLEND)",
175 "gl.enableVertexAttribArray(0)",
178 "gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, renderbuffer)",
179 "gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0)",
180 "gl.frontFace(gl.CW)",
181 "gl.generateMipmap(gl.TEXTURE_2D)",
182 "gl.hint(gl.GENERATE_MIPMAP_HINT, gl.FASTEST)",
184 "gl.linkProgram(program)",
185 "gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0)",
186 "gl.polygonOffset(0, 0)",
187 "gl.readPixels(0, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, arrayBufferView)",
188 "gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, 0, 0)",
189 "gl.sampleCoverage(0, 0)",
190 "gl.scissor(0, 0, 0, 0)",
191 "gl.shaderSource(shader, '')",
192 "gl.stencilFunc(gl.NEVER, 0, 0)",
193 "gl.stencilFuncSeparate(gl.FRONT, gl.NEVER, 0, 0)",
195 "gl.stencilMaskSeparate(gl.FRONT, 0)",
196 "gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP)",
197 "gl.stencilOpSeparate(gl.FRONT, gl.KEEP, gl.KEEP, gl.KEEP)",
198 "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, arrayBufferView)",
199 "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, imageData)",
200 "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image)",
201 "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas)",
202 "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, video)",
203 "gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)",
204 "gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)",
205 "gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, arrayBufferView)",
206 "gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, imageData)",
207 "gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, image)",
208 "gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, canvas)",
209 "gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, video)",
210 "gl.uniform1f(uniformLocation, 0)",
211 "gl.uniform1fv(uniformLocation, float32array)",
212 "gl.uniform1fv(uniformLocation, [0])",
213 "gl.uniform1i(uniformLocation, 0)",
214 "gl.uniform1iv(uniformLocation, int32array)",
215 "gl.uniform1iv(uniformLocation, [0])",
216 "gl.uniform2f(uniformLocation, 0, 0)",
217 "gl.uniform2fv(uniformLocation, float32array)",
218 "gl.uniform2fv(uniformLocation, [0, 0])",
219 "gl.uniform2i(uniformLocation, 0, 0)",
220 "gl.uniform2iv(uniformLocation, int32array)",
221 "gl.uniform2iv(uniformLocation, [0, 0])",
222 "gl.uniform3f(uniformLocation, 0, 0, 0)",
223 "gl.uniform3fv(uniformLocation, float32array)",
224 "gl.uniform3fv(uniformLocation, [0, 0, 0])",
225 "gl.uniform3i(uniformLocation, 0, 0, 0)",
226 "gl.uniform3iv(uniformLocation, int32array)",
227 "gl.uniform3iv(uniformLocation, [0, 0, 0])",
228 "gl.uniform4f(uniformLocation, 0, 0, 0, 0)",
229 "gl.uniform4fv(uniformLocation, float32array)",
230 "gl.uniform4fv(uniformLocation, [0, 0, 0, 0])",
231 "gl.uniform4i(uniformLocation, 0, 0, 0, 0)",
232 "gl.uniform4iv(uniformLocation, int32array)",
233 "gl.uniform4iv(uniformLocation, [0, 0, 0, 0])",
234 "gl.uniformMatrix2fv(uniformLocation, false, float32array)",
235 "gl.uniformMatrix2fv(uniformLocation, false, [0, 0, 0, 0])",
236 "gl.uniformMatrix3fv(uniformLocation, false, float32array)",
237 "gl.uniformMatrix3fv(uniformLocation, false, [0, 0, 0, 0, 0, 0, 0, 0, 0])",
238 "gl.uniformMatrix4fv(uniformLocation, false, float32array)",
239 "gl.uniformMatrix4fv(uniformLocation, false, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])",
240 "gl.useProgram(program)",
241 "gl.validateProgram(program)",
242 "gl.vertexAttrib1f(0, 0)",
243 "gl.vertexAttrib1fv(0, float32array)",
244 "gl.vertexAttrib1fv(0, [0])",
245 "gl.vertexAttrib2f(0, 0, 0)",
246 "gl.vertexAttrib2fv(0, float32array)",
247 "gl.vertexAttrib2fv(0, [0, 0])",
248 "gl.vertexAttrib3f(0, 0, 0, 0)",
249 "gl.vertexAttrib3fv(0, float32array)",
250 "gl.vertexAttrib3fv(0, [0, 0, 0])",
251 "gl.vertexAttrib4f(0, 0, 0, 0, 0)",
252 "gl.vertexAttrib4fv(0, float32array)",
253 "gl.vertexAttrib4fv(0, [0, 0, 0, 0])",
254 "gl.vertexAttribPointer(0, 0, gl.FLOAT, false, 0, 0)",
255 "gl.viewport(0, 0, 0, 0)",
257 for (var i
= 0; i
< voidTests
.length
; ++i
) {
258 shouldGenerateGLError(gl
, gl
.NO_ERROR
, voidTests
[i
]);
261 // Functions return nullable values should all return null.
264 "gl.createFramebuffer()",
265 "gl.createProgram()",
266 "gl.createRenderbuffer()",
267 "gl.createShader(gl.GL_VERTEX_SHADER)",
268 "gl.createTexture()",
269 "gl.getActiveAttrib(program, 0)",
270 "gl.getActiveUniform(program, 0)",
271 "gl.getAttachedShaders(program)",
272 "gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE)",
273 "gl.getContextAttributes()",
274 "gl.getFramebufferAttachmentParameter(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.FRAMEBUFFER_ATTACHMENT_OBJECT_NAME)",
275 "gl.getParameter(gl.CURRENT_PROGRAM)",
276 "gl.getProgramInfoLog(program)",
277 "gl.getProgramParameter(program, gl.LINK_STATUS)",
278 "gl.getRenderbufferParameter(gl.RENDERBUFFER, gl.RENDERBUFFER_WIDTH)",
279 "gl.getShaderInfoLog(shader)",
280 "gl.getShaderParameter(shader, gl.SHADER_TYPE)",
281 "gl.getShaderSource(shader)",
282 "gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S)",
283 "gl.getUniform(program, uniformLocation)",
284 "gl.getUniformLocation(program, 'vPosition')",
285 "gl.getVertexAttrib(0, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING)",
286 "gl.getSupportedExtensions()",
287 "gl.getExtension('WEBGL_lose_context')",
289 for (var i
= 0; i
< nullTests
.length
; ++i
) {
290 shouldBeNull(nullTests
[i
]);
293 // "Is" queries should all return false.
294 shouldBeFalse("gl.isBuffer(buffer)");
295 shouldBeFalse("gl.isEnabled(gl.BLEND)");
296 shouldBeFalse("gl.isFramebuffer(framebuffer)");
297 shouldBeFalse("gl.isProgram(program)");
298 shouldBeFalse("gl.isRenderbuffer(renderbuffer)");
299 shouldBeFalse("gl.isShader(shader)");
300 shouldBeFalse("gl.isTexture(texture)");
302 shouldBe("gl.getError()", "gl.NO_ERROR");
311 <body onload=
"init()">
312 <div id=
"description"></div>
313 <div id=
"console"></div>