OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head> |
| 4 <script src="../../../resources/testharness.js"></script> |
| 5 <script src="../../../resources/testharnessreport.js"></script> |
| 6 <script src="./resources/webgl-test-utils-full.js"></script> |
| 7 </head> |
| 8 |
| 9 <script id="myWorker" type="text/worker"> |
| 10 var canvas; |
| 11 var gl; |
| 12 var WEBGL_lose_context; |
| 13 var allowRestore; |
| 14 var contextLostEventFired; |
| 15 var contextRestoredEventFired; |
| 16 // Make everything in results true for easy testing |
| 17 var results = []; |
| 18 |
| 19 function setupTest() |
| 20 { |
| 21 canvas = new OffscreenCanvas(10, 10); |
| 22 gl = canvas.getContext('webgl'); |
| 23 WEBGL_lose_context = gl.getExtension("WEBGL_lose_context"); |
| 24 } |
| 25 |
| 26 function testOriginalContext() |
| 27 { |
| 28 results.push(!gl.isContextLost()); |
| 29 results.push(gl.getError() == gl.NO_ERROR); |
| 30 } |
| 31 |
| 32 function testLostContext(e) |
| 33 { |
| 34 results.push(!contextLostEventFired); |
| 35 contextLostEventFired = true; |
| 36 results.push(gl.isContextLost()); |
| 37 results.push(gl.getError() == gl.NO_ERROR); |
| 38 if (allowRestore) |
| 39 e.preventDefault(); |
| 40 } |
| 41 |
| 42 function testShouldNotRestoreContext(e) |
| 43 { |
| 44 // Should not restore the context unless preventDefault is called on the con
text lost event |
| 45 results.push(false); |
| 46 } |
| 47 |
| 48 function compareGLError(glError, evalStr) { |
| 49 var exception; |
| 50 try { |
| 51 eval(evalStr); |
| 52 } catch(e) { |
| 53 exception = e; |
| 54 } |
| 55 if (exception) { |
| 56 return false; |
| 57 } else { |
| 58 if (gl.getError() == glError) |
| 59 return true; |
| 60 return false; |
| 61 } |
| 62 } |
| 63 |
| 64 function testRestoredContext() { |
| 65 results.push(!contextRestoredEventFired); |
| 66 contextRestoredEventFired = true; |
| 67 results.push(!gl.isContextLost()); |
| 68 results.push(gl.getError() == gl.NO_ERROR); |
| 69 } |
| 70 |
| 71 self.onmessage = function(e) { |
| 72 setupTest(); |
| 73 testOriginalContext(); |
| 74 |
| 75 canvas.addEventListener("webglcontextrestored", testShouldNotRestoreContext)
; |
| 76 canvas.addEventListener("webglcontextlost", function(e) { |
| 77 testLostContext(e); |
| 78 // restore the context after this event has exited. |
| 79 setTimeout(function() { |
| 80 // we didn't call prevent default so we should not be able to restore th
e context |
| 81 results.push(compareGLError(gl.INVALID_OPERATION, "WEBGL_lose_contex
t.restoreContext()")); |
| 82 results.push(gl.isContextLost()); |
| 83 results.push(gl.getError() == gl.NO_ERROR); |
| 84 // gl methods should still be no-ops |
| 85 results.push(compareGLError(gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D
, gl.TEXTURE_CUBE_MAP)")); |
| 86 results.push(false); |
| 87 setTimeout(function() { |
| 88 testRestoredContext(); |
| 89 self.postMessage(results); |
| 90 }, 0); |
| 91 }, 0); |
| 92 }); |
| 93 allowRestore = false; |
| 94 contextLostEventFired = false; |
| 95 contextRestoredEventFired = false; |
| 96 |
| 97 WEBGL_lose_context.loseContext(); |
| 98 // The context should be lost immediately. |
| 99 results.push(gl.isContextLost()); |
| 100 results.push(gl.getError() == gl.CONTEXT_LOST_WEBGL); |
| 101 results.push(gl.getError() == gl.NO_ERROR); |
| 102 // gl methods should be no-ops |
| 103 results.push(compareGLError(gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEX
TURE_CUBE_MAP)")); |
| 104 // but the event should not have been fired. |
| 105 results.push(!contextLostEventFired); |
| 106 }; |
| 107 |
| 108 </script> |
| 109 |
| 110 <script> |
| 111 function makeWorker(script) |
| 112 { |
| 113 var blob = new Blob([script]); |
| 114 return new Worker(URL.createObjectURL(blob)); |
| 115 } |
| 116 |
| 117 var t = async_test('Test that WebGL context restore event can be handled with Of
fscreenCanvas in a worker'); |
| 118 var worker = makeWorker(document.getElementById("myWorker").textContent); |
| 119 worker.addEventListener('message', t.step_func(function(msg) { |
| 120 assert_equals(msg.data.length, 18); |
| 121 for (var i = 0; i < msg.data.length; ++i) |
| 122 assert_true(msg.data[i]); |
| 123 t.done(); |
| 124 })); |
| 125 worker.postMessage('start'); |
| 126 </script> |
| 127 </html> |
OLD | NEW |