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);
11 var wtu
= WebGLTestUtils
;
14 var shouldGenerateGLError
;
19 var texColor
= [255, 10, 20, 255];
21 var contextLostEventFired
;
22 var contextRestoredEventFired
;
26 if (window
.initNonKhronosFramework
) {
27 window
.initNonKhronosFramework(true);
30 description("Tests behavior under a restored context.");
32 shouldGenerateGLError
= wtu
.shouldGenerateGLError
;
38 canvas
= document
.createElement("canvas");
41 gl
= wtu
.create3DContext(canvas
);
42 extension
= gl
.getExtension("WEBGL_lose_context");
44 debug("Could not find lose_context extension under the following names: WEBGL_lose_context");
50 function testLosingContext()
55 debug("Test losing a context and inability to restore it.");
57 canvas
.addEventListener("webglcontextlost", function(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 the context
62 shouldGenerateGLError(gl
, gl
.INVALID_OPERATION
, "extension.restoreContext()");
63 testLosingAndRestoringContext();
66 canvas
.addEventListener("webglcontextrestored", testShouldNotRestoreContext
);
68 contextLostEventFired
= false;
69 contextRestoredEventFired
= false;
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.TEXTURE_CUBE_MAP)");
79 // but the event should not have been fired.
80 shouldBeFalse("contextLostEventFired");
83 function testLosingAndRestoringContext()
89 debug("Test losing and restoring a context.");
91 canvas
.addEventListener("webglcontextlost", function(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.TEXTURE_CUBE_MAP)");
104 canvas
.addEventListener("webglcontextrestored", function() {
105 testRestoredContext();
109 contextLostEventFired
= false;
110 contextRestoredEventFired
= false;
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.TEXTURE_CUBE_MAP)");
120 // but the event should not have been fired.
121 shouldBeFalse("contextLostEventFired");
124 function testRendering()
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
);
130 program
= wtu
.setupSimpleTextureProgram(gl
);
131 bufferObjects
= wtu
.setupUnitQuad(gl
);
132 texture
= wtu
.createColoredTexture(gl
, canvas
.width
, canvas
.height
, texColor
);
134 gl
.uniform1i(gl
.getUniformLocation(program
, "tex"), 0);
135 wtu
.drawQuad(gl
, [0, 0, 0, 255]);
137 var compare
= texColor
.slice(0, 3);
138 wtu
.checkCanvasRect(gl
, 0, 0, canvas
.width
, canvas
.height
, compare
, "shouldBe " + compare
);
140 shouldBe("gl.getError()", "gl.NO_ERROR");
143 function testOriginalContext()
145 debug("Test valid context");
146 shouldBeFalse("gl.isContextLost()");
147 shouldBe("gl.getError()", "gl.NO_ERROR");
152 function testLostContext(e
)
154 debug("Test lost context");
155 shouldBeFalse("contextLostEventFired");
156 contextLostEventFired
= true;
157 shouldBeTrue("gl.isContextLost()");
158 shouldBe("gl.getError()", "gl.NO_ERROR");
164 function testShouldNotRestoreContext(e
)
166 testFailed("Should not restore the context unless preventDefault is called on the context lost event");
170 function testResources(expected
)
173 "gl.bindTexture(gl.TEXTURE_2D, texture)",
174 "gl.useProgram(program)",
175 "gl.bindBuffer(gl.ARRAY_BUFFER, bufferObjects[0])",
178 for (var i
= 0; i
< tests
.length
; ++i
)
179 shouldGenerateGLError(gl
, expected
, tests
[i
]);
182 function testRestoredContext()
184 debug("Test restored context");
185 shouldBeFalse("contextRestoredEventFired");
186 contextRestoredEventFired
= true;
187 shouldBeFalse("gl.isContextLost()");
188 shouldBe("gl.getError()", "gl.NO_ERROR");
190 // Validate that using old resources fails.
191 testResources(gl
.INVALID_OPERATION
);
195 // Validate new resources created in testRendering().
196 testResources(gl
.NO_ERROR
);
203 <body onload=
"init()">
204 <div id=
"description"></div>
205 <div id=
"console"></div>