|
| 1 | +describe('webglContextRestore', function () { |
| 2 | + var common = require('../test-common'); |
| 3 | + var imageTest = require('../image-test'); |
| 4 | + |
| 5 | + var myMap; |
| 6 | + |
| 7 | + beforeEach(function () { |
| 8 | + imageTest.prepareImageTest(); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(function () { |
| 12 | + if (myMap) { |
| 13 | + myMap.exit(); |
| 14 | + myMap = null; |
| 15 | + } |
| 16 | + }); |
| 17 | + |
| 18 | + it('recovers after WEBGL_lose_context lose/restore', function (done) { |
| 19 | + var mapOptions = {center: {x: -95, y: 39}, zoom: 4}; |
| 20 | + myMap = common.createOsmMap(mapOptions, {}, true); |
| 21 | + |
| 22 | + var layer = myMap.createLayer('feature', {renderer: 'webgl'}); |
| 23 | + layer.createFeature('point') |
| 24 | + .data([{x: -95, y: 39}, {x: -96, y: 38.5}, {x: -94, y: 39.5}]) |
| 25 | + .position(function (d) { |
| 26 | + return d; |
| 27 | + }) |
| 28 | + .style({ |
| 29 | + radius: 8, |
| 30 | + fillColor: {r: 0.9, g: 0.1, b: 0.1}, |
| 31 | + fillOpacity: 1 |
| 32 | + }); |
| 33 | + |
| 34 | + myMap.draw(); |
| 35 | + myMap.onIdle(function () { |
| 36 | + var renderer = layer.renderer(); |
| 37 | + var context = renderer && renderer._glContext && renderer._glContext(); |
| 38 | + var ext = context && context.getExtension && |
| 39 | + context.getExtension('WEBGL_lose_context'); |
| 40 | + var canvas = renderer && renderer.canvas && renderer.canvas().get(0); |
| 41 | + |
| 42 | + if (!ext || !ext.loseContext || !ext.restoreContext || !canvas) { |
| 43 | + pending('WEBGL_lose_context is not available in this environment.'); |
| 44 | + done(); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // Losing/restoring context can emit expected transient shader/program |
| 49 | + // compile/link errors/warnings/logs while the context is invalid; suppress them for |
| 50 | + // this test window so they don't clutter the output. |
| 51 | + var originalConsoleError = console.error; |
| 52 | + var originalConsoleWarn = console.warn; |
| 53 | + var originalConsoleLog = console.log; |
| 54 | + console.error = function () {}; |
| 55 | + console.warn = function () {}; |
| 56 | + console.log = function () {}; |
| 57 | + function restoreConsoleMethods() { |
| 58 | + if (console.error !== originalConsoleError) { |
| 59 | + console.error = originalConsoleError; |
| 60 | + } |
| 61 | + if (console.warn !== originalConsoleWarn) { |
| 62 | + console.warn = originalConsoleWarn; |
| 63 | + } |
| 64 | + if (console.log !== originalConsoleLog) { |
| 65 | + console.log = originalConsoleLog; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + var restoreTimeout = window.setTimeout(function () { |
| 70 | + restoreConsoleMethods(); |
| 71 | + fail('Timed out waiting for webglcontextrestored.'); |
| 72 | + done(); |
| 73 | + }, 5000); |
| 74 | + |
| 75 | + canvas.addEventListener('webglcontextrestored', function () { |
| 76 | + window.clearTimeout(restoreTimeout); |
| 77 | + myMap.draw(); |
| 78 | + myMap.onIdle(function () { |
| 79 | + restoreConsoleMethods(); |
| 80 | + expect(renderer._glContext()).toBeTruthy(); |
| 81 | + done(); |
| 82 | + }); |
| 83 | + }, {once: true}); |
| 84 | + |
| 85 | + canvas.addEventListener('webglcontextlost', function (evt) { |
| 86 | + // Required for restoration in many browsers/drivers. |
| 87 | + evt.preventDefault(); |
| 88 | + // Defer restore until after the lost event handler returns. |
| 89 | + window.setTimeout(function () { |
| 90 | + ext.restoreContext(); |
| 91 | + }, 0); |
| 92 | + }, {once: true}); |
| 93 | + |
| 94 | + ext.loseContext(); |
| 95 | + }); |
| 96 | + }, 30000); |
| 97 | +}); |
0 commit comments