diff --git a/src/util/index.js b/src/util/index.js index ca95383203..f7206750fc 100644 --- a/src/util/index.js +++ b/src/util/index.js @@ -5,6 +5,7 @@ var $ = require('jquery'); */ var util = require('./init'); $.extend(util, require('./throttle')); +$.extend(util, require('./mockVGL')); util.DistanceGrid = require('./distanceGrid.js'); util.ClusterGroup = require('./clustering.js'); diff --git a/src/util/mockVGL.js b/src/util/mockVGL.js new file mode 100644 index 0000000000..354ef8029e --- /dev/null +++ b/src/util/mockVGL.js @@ -0,0 +1,189 @@ +/* eslint-disable camelcase */ +/* eslint-disable underscore/prefer-constant */ + +var $ = require('jquery'); +var vgl = require('vgl'); +var vglRenderer = require('../gl/vglRenderer'); + +var _renderWindow, _supported; + +module.exports = {}; + +/** + * Replace vgl.renderer with a mocked version for testing in a non-webGL state. + * Use restoreVGLRenderer to unmock. Call vgl.mockCounts() to get the number + * of times different webGL functions have been called. + * + * @param {boolean} [supported=true] If false, then the vgl renderer will + * indicate that this is an unsupported browser environment. + */ +module.exports.mockVGLRenderer = function mockVGLRenderer(supported) { + 'use strict'; + var vgl = require('vgl'); + + if (supported === undefined) { + supported = true; + } + + if (vgl._mocked) { + throw new Error('VGL renderer already mocked'); + } + + var mockCounts = {}; + var count = function (name) { + mockCounts[name] = (mockCounts[name] || 0) + 1; + }; + var noop = function (name) { + return function () { + count(name); + }; + }; + var _id = 0, + incID = function (name) { + return function () { + count(name); + _id += 1; + return _id; + }; + }; + /* The context largely does nothing. */ + var default_context = { + activeTexture: noop('activeTexture'), + attachShader: noop('attachShader'), + bindAttribLocation: noop('bindAttribLocation'), + bindBuffer: noop('bindBuffer'), + bindFramebuffer: noop('bindFramebuffer'), + bindTexture: noop('bindTexture'), + blendFuncSeparate: noop('blendFuncSeparate'), + bufferData: noop('bufferData'), + bufferSubData: noop('bufferSubData'), + checkFramebufferStatus: function (key) { + count('checkFramebufferStatus'); + if (key === vgl.GL.FRAMEBUFFER) { + return vgl.GL.FRAMEBUFFER_COMPLETE; + } + }, + clear: noop('clear'), + clearColor: noop('clearColor'), + clearDepth: noop('clearDepth'), + compileShader: noop('compileShader'), + createBuffer: incID('createBuffer'), + createFramebuffer: noop('createFramebuffer'), + createProgram: incID('createProgram'), + createShader: incID('createShader'), + createTexture: incID('createTexture'), + deleteBuffer: noop('deleteBuffer'), + deleteProgram: noop('deleteProgram'), + deleteShader: noop('deleteShader'), + deleteTexture: noop('deleteTexture'), + depthFunc: noop('depthFunc'), + disable: noop('disable'), + disableVertexAttribArray: noop('disableVertexAttribArray'), + drawArrays: noop('drawArrays'), + enable: noop('enable'), + enableVertexAttribArray: noop('enableVertexAttribArray'), + finish: noop('finish'), + getExtension: incID('getExtension'), + getParameter: function (key) { + count('getParameter'); + if (key === vgl.GL.DEPTH_BITS) { + return 16; + } + }, + getProgramParameter: function (id, key) { + count('getProgramParameter'); + if (key === vgl.GL.LINK_STATUS) { + return true; + } + }, + getShaderInfoLog: function () { + count('getShaderInfoLog'); + return 'log'; + }, + getShaderParameter: function (id, key) { + count('getShaderParameter'); + if (key === vgl.GL.COMPILE_STATUS) { + return true; + } + }, + getUniformLocation: incID('getUniformLocation'), + isEnabled: function (key) { + count('isEnabled'); + if (key === vgl.GL.BLEND) { + return true; + } + }, + linkProgram: noop('linkProgram'), + pixelStorei: noop('pixelStorei'), + shaderSource: noop('shaderSource'), + texImage2D: noop('texImage2D'), + texParameteri: noop('texParameteri'), + uniform1iv: noop('uniform1iv'), + uniform1fv: noop('uniform1fv'), + uniform2fv: noop('uniform2fv'), + uniform3fv: noop('uniform3fv'), + uniform4fv: noop('uniform4fv'), + uniformMatrix3fv: noop('uniformMatrix3fv'), + uniformMatrix4fv: noop('uniformMatrix4fv'), + useProgram: noop('useProgram'), + vertexAttribPointer: noop('vertexAttribPointer'), + vertexAttrib3fv: noop('vertexAttrib3fv'), + viewport: noop('viewport') + }; + + _renderWindow = vgl.renderWindow; + var mockedRenderWindow = function () { + /* Temporarily put back the original definition of renderWindow so that the + * class instance will be instantiated correctly. */ + vgl.renderWindow = _renderWindow; + var m_this = new vgl.renderWindow(), + m_context; + vgl.renderWindow = mockedRenderWindow; + + m_this._setup = function () { + var i, renderers = m_this.renderers(), + wsize = m_this.windowSize(), + wpos = m_this.windowPosition(); + + m_context = $.extend({}, default_context); + + for (i = 0; i < renderers.length; i += 1) { + if ((renderers[i].width() > wsize[0]) || + renderers[i].width() === 0 || + (renderers[i].height() > wsize[1]) || + renderers[i].height() === 0) { + renderers[i].resize(wpos[0], wpos[1], wsize[0], wsize[1]); + } + } + return true; + }; + m_this.context = function () { + return m_context; + }; + return m_this; + }; + vgl.renderWindow = mockedRenderWindow; + + _supported = vglRenderer.supported; + vglRenderer.supported = function () { + return !!supported; + }; + + vgl._mocked = true; + vgl.mockCounts = function () { + return mockCounts; + }; +}; + +/** + * Unmock the vgl renderer. + */ +module.exports.restoreVGLRenderer = function () { + if (vgl._mocked) { + vgl.renderWindow = _renderWindow; + vglRenderer.supported = _supported; + delete vgl._mocked; + // delete vgl._mockedRenderWindow; + delete vgl.mockCounts; + } +}; diff --git a/testing/test-data/base-images.tgz.md5 b/testing/test-data/base-images.tgz.md5 index f2458e316b..c462aac6a5 100644 --- a/testing/test-data/base-images.tgz.md5 +++ b/testing/test-data/base-images.tgz.md5 @@ -1 +1 @@ -f2fa25ac074a409ccc072c3444edfb6f \ No newline at end of file +c9987ae6444d8124142e040ae06b7468 \ No newline at end of file diff --git a/testing/test-data/base-images.tgz.url b/testing/test-data/base-images.tgz.url index 7bc3557343..4418bdb639 100644 --- a/testing/test-data/base-images.tgz.url +++ b/testing/test-data/base-images.tgz.url @@ -1 +1 @@ -https://data.kitware.com/api/v1/file/58c934128d777f0aef5d79a7/download \ No newline at end of file +https://data.kitware.com/api/v1/file/590103778d777f16d01e04fb/download \ No newline at end of file diff --git a/tests/cases/annotation.js b/tests/cases/annotation.js index 6e6b9065f9..557178ad76 100644 --- a/tests/cases/annotation.js +++ b/tests/cases/annotation.js @@ -5,8 +5,8 @@ describe('geo.annotation', function () { var $ = require('jquery'); var geo = require('../test-utils').geo; - var mockVGLRenderer = require('../test-utils').mockVGLRenderer; - var restoreVGLRenderer = require('../test-utils').restoreVGLRenderer; + var mockVGLRenderer = geo.util.mockVGLRenderer; + var restoreVGLRenderer = geo.util.restoreVGLRenderer; beforeEach(function () { mockVGLRenderer(); diff --git a/tests/cases/annotationLayer.js b/tests/cases/annotationLayer.js index 6ba1cadc57..e1be239bd1 100644 --- a/tests/cases/annotationLayer.js +++ b/tests/cases/annotationLayer.js @@ -5,8 +5,8 @@ describe('geo.annotationLayer', function () { var $ = require('jquery'); var geo = require('../test-utils').geo; - var mockVGLRenderer = require('../test-utils').mockVGLRenderer; - var restoreVGLRenderer = require('../test-utils').restoreVGLRenderer; + var mockVGLRenderer = geo.util.mockVGLRenderer; + var restoreVGLRenderer = geo.util.restoreVGLRenderer; beforeEach(function () { mockVGLRenderer(); diff --git a/tests/cases/choroplethFeature.js b/tests/cases/choroplethFeature.js index 1c4de3376d..3130f004cf 100644 --- a/tests/cases/choroplethFeature.js +++ b/tests/cases/choroplethFeature.js @@ -1,9 +1,9 @@ // Test geo.choroplethFeature and geo.gl.choroplethFeature -var geo = require('../test-utils').geo; var $ = require('jquery'); -var mockVGLRenderer = require('../test-utils').mockVGLRenderer; -var restoreVGLRenderer = require('../test-utils').restoreVGLRenderer; +var geo = require('../test-utils').geo; +var mockVGLRenderer = geo.util.mockVGLRenderer; +var restoreVGLRenderer = geo.util.restoreVGLRenderer; describe('geo.choroplethFeature', function () { 'use strict'; @@ -130,9 +130,9 @@ describe('geo.choroplethFeature', function () { .scalarValue(scalarData[0])).toBe(10); expect(choropleth.choropleth.get('accessors')() .geoId(mpdata[0])).toBe(0); - expect(Object.keys(choropleth.choropleth.get())).toEqual( - ['colorRange', 'scale', 'accessors', 'scalar', - 'scalarAggregator', 'name']); + expect(Object.keys(choropleth.choropleth.get())).toEqual([ + 'colorRange', 'scale', 'accessors', 'scalar', + 'scalarAggregator', 'name']); restoreVGLRenderer(); }); }); diff --git a/tests/cases/contourWrap.js b/tests/cases/contourWrap.js index 0c80d310a8..92bc9d950b 100644 --- a/tests/cases/contourWrap.js +++ b/tests/cases/contourWrap.js @@ -1,12 +1,12 @@ -var geo = require('../test-utils').geo; var $ = require('jquery'); describe('Contour Feature', function () { 'use strict'; var map, layer; - var mockVGLRenderer = require('../test-utils').mockVGLRenderer; - var restoreVGLRenderer = require('../test-utils').restoreVGLRenderer; + var geo = require('../test-utils').geo; + var mockVGLRenderer = geo.util.mockVGLRenderer; + var restoreVGLRenderer = geo.util.restoreVGLRenderer; beforeEach(function () { $('
') diff --git a/tests/cases/lineFeature.js b/tests/cases/lineFeature.js index 6c9eaaa30a..431a9189e4 100644 --- a/tests/cases/lineFeature.js +++ b/tests/cases/lineFeature.js @@ -1,14 +1,14 @@ // Test geo.lineFeature, geo.d3.lineFeature, geo.canvas.lineFeature, and // geo.gl.lineFeature -var geo = require('../test-utils').geo; var $ = require('jquery'); var mockAnimationFrame = require('../test-utils').mockAnimationFrame; var stepAnimationFrame = require('../test-utils').stepAnimationFrame; var unmockAnimationFrame = require('../test-utils').unmockAnimationFrame; +var geo = require('../test-utils').geo; +var mockVGLRenderer = geo.util.mockVGLRenderer; +var restoreVGLRenderer = geo.util.restoreVGLRenderer; var vgl = require('vgl'); -var mockVGLRenderer = require('../test-utils').mockVGLRenderer; -var restoreVGLRenderer = require('../test-utils').restoreVGLRenderer; var waitForIt = require('../test-utils').waitForIt; var logCanvas2D = require('../test-utils').logCanvas2D; diff --git a/tests/cases/osmLayer.js b/tests/cases/osmLayer.js index be29953b2b..c15ab6bd2e 100644 --- a/tests/cases/osmLayer.js +++ b/tests/cases/osmLayer.js @@ -1,7 +1,5 @@ // Test geo.core.osmLayer -var geo = require('../test-utils').geo; var $ = require('jquery'); -var vgl = require('vgl'); var mockAnimationFrame = require('../test-utils').mockAnimationFrame; var stepAnimationFrame = require('../test-utils').stepAnimationFrame; var unmockAnimationFrame = require('../test-utils').unmockAnimationFrame; @@ -13,8 +11,10 @@ describe('geo.core.osmLayer', function () { var waitForIt = require('../test-utils').waitForIt; var submitNote = require('../test-utils').submitNote; // var logCanvas2D = require('../test-utils').logCanvas2D; - var mockVGLRenderer = require('../test-utils').mockVGLRenderer; - var restoreVGLRenderer = require('../test-utils').restoreVGLRenderer; + var geo = require('../test-utils').geo; + var mockVGLRenderer = geo.util.mockVGLRenderer; + var restoreVGLRenderer = geo.util.restoreVGLRenderer; + var vgl = require('vgl'); var closeToEqual = require('../test-utils').closeToEqual; function create_map(opts) { diff --git a/tests/cases/pointFeature.js b/tests/cases/pointFeature.js index 52ea343559..16c3701124 100644 --- a/tests/cases/pointFeature.js +++ b/tests/cases/pointFeature.js @@ -1,13 +1,13 @@ // Test geo.pointFeature, geo.d3.pointFeature, and geo.gl.pointFeature -var geo = require('../test-utils').geo; var $ = require('jquery'); var mockAnimationFrame = require('../test-utils').mockAnimationFrame; var stepAnimationFrame = require('../test-utils').stepAnimationFrame; var unmockAnimationFrame = require('../test-utils').unmockAnimationFrame; +var geo = require('../test-utils').geo; +var mockVGLRenderer = geo.util.mockVGLRenderer; +var restoreVGLRenderer = geo.util.restoreVGLRenderer; var vgl = require('vgl'); -var mockVGLRenderer = require('../test-utils').mockVGLRenderer; -var restoreVGLRenderer = require('../test-utils').restoreVGLRenderer; var waitForIt = require('../test-utils').waitForIt; describe('geo.pointFeature', function () { diff --git a/tests/cases/polygonFeature.js b/tests/cases/polygonFeature.js index e7eca2662b..d50867b687 100644 --- a/tests/cases/polygonFeature.js +++ b/tests/cases/polygonFeature.js @@ -1,10 +1,10 @@ // Test geo.polygonFeature and geo.gl.polygonFeature -var geo = require('../test-utils').geo; var $ = require('jquery'); +var geo = require('../test-utils').geo; +var mockVGLRenderer = geo.util.mockVGLRenderer; +var restoreVGLRenderer = geo.util.restoreVGLRenderer; var vgl = require('vgl'); -var mockVGLRenderer = require('../test-utils').mockVGLRenderer; -var restoreVGLRenderer = require('../test-utils').restoreVGLRenderer; var waitForIt = require('../test-utils').waitForIt; // var closeToArray = require('../test-utils').closeToArray; diff --git a/tests/cases/quadFeature.js b/tests/cases/quadFeature.js index 2ba3d1a27f..d5d1e5d038 100644 --- a/tests/cases/quadFeature.js +++ b/tests/cases/quadFeature.js @@ -2,11 +2,11 @@ /* globals Image */ -var geo = require('../test-utils').geo; var $ = require('jquery'); +var geo = require('../test-utils').geo; +var mockVGLRenderer = geo.util.mockVGLRenderer; +var restoreVGLRenderer = geo.util.restoreVGLRenderer; var vgl = require('vgl'); -var mockVGLRenderer = require('../test-utils').mockVGLRenderer; -var restoreVGLRenderer = require('../test-utils').restoreVGLRenderer; var waitForIt = require('../test-utils').waitForIt; var closeToArray = require('../test-utils').closeToArray; var logCanvas2D = require('../test-utils').logCanvas2D; diff --git a/tests/cases/registry.js b/tests/cases/registry.js index 0f14ab54da..98a171075f 100644 --- a/tests/cases/registry.js +++ b/tests/cases/registry.js @@ -2,8 +2,8 @@ describe('geo.registry', function () { 'use strict'; var geo = require('../test-utils').geo; - var mockVGLRenderer = require('../test-utils').mockVGLRenderer; - var restoreVGLRenderer = require('../test-utils').restoreVGLRenderer; + var mockVGLRenderer = geo.util.mockVGLRenderer; + var restoreVGLRenderer = geo.util.restoreVGLRenderer; describe('Check rendererForFeatures', function () { it('specific features', function () { diff --git a/tests/cases/renderers.js b/tests/cases/renderers.js index 4755ba0330..a754cf7623 100644 --- a/tests/cases/renderers.js +++ b/tests/cases/renderers.js @@ -6,8 +6,8 @@ describe('renderers', function () { var supported = true; var fallback = 'd3'; var geo = require('../test-utils').geo; - var mockVGLRenderer = require('../test-utils').mockVGLRenderer; - var restoreVGLRenderer = require('../test-utils').restoreVGLRenderer; + var mockVGLRenderer = geo.util.mockVGLRenderer; + var restoreVGLRenderer = geo.util.restoreVGLRenderer; function create_simple_renderer() { var simpleRenderer = function (arg) { diff --git a/tests/test-utils.js b/tests/test-utils.js index dd42ba68a8..b33d4b87b3 100644 --- a/tests/test-utils.js +++ b/tests/test-utils.js @@ -1,14 +1,10 @@ /* These are functions we want available to jasmine tests. */ -/* exported waitForIt, mockVGLRenderer, closeToArray, closeToEqual, logCanvas2D, submitNote */ +/* exported waitForIt, closeToArray, closeToEqual, logCanvas2D, submitNote */ /* global CanvasRenderingContext2D */ -var vgl = require('vgl'); -var _renderWindow = vgl.renderWindow; - var $ = require('jquery'); var geo = require('../src'); -var _supported = geo.gl.vglRenderer.supported; var bowser = require('bowser'); module.exports = {}; @@ -96,159 +92,6 @@ module.exports.closeToEqual = function closeToEqual(o1, o2, precision) { return true; }; -/** - * Replace vgl.renderer with a mocked version for phantom tests. - * Returns a function that, when called, will restore vgl to its - * default state. - * - * @param {boolean} [supported=true] If false, then the vgl renderer - * will indicate that this is an unsupported browser environment. - */ -module.exports.mockVGLRenderer = function mockVGLRenderer(supported) { - 'use strict'; - var vgl = require('vgl'); - - if (supported === undefined) { - supported = true; - } - - if (vgl._mocked) { - throw new Error('VGL renderer already mocked'); - } - - var mockCounts = {}; - var count = function (name) { - mockCounts[name] = (mockCounts[name] || 0) + 1; - }; - var noop = function (name) { - return function () { - count(name); - }; - }; - var _id = 0, - incID = function (name) { - return function () { - count(name); - _id += 1; - return _id; - }; - }; - /* The context largely does nothing. */ - var m_context = { - activeTexture: noop('activeTexture'), - attachShader: noop('attachShader'), - bindAttribLocation: noop('bindAttribLocation'), - bindBuffer: noop('bindBuffer'), - bindFramebuffer: noop('bindFramebuffer'), - bindTexture: noop('bindTexture'), - blendFuncSeparate: noop('blendFuncSeparate'), - bufferData: noop('bufferData'), - bufferSubData: noop('bufferSubData'), - checkFramebufferStatus: function (key) { - count('checkFramebufferStatus'); - if (key === vgl.GL.FRAMEBUFFER) { - return vgl.GL.FRAMEBUFFER_COMPLETE; - } - }, - clear: noop('clear'), - clearColor: noop('clearColor'), - clearDepth: noop('clearDepth'), - compileShader: noop('compileShader'), - createBuffer: incID('createBuffer'), - createFramebuffer: noop('createFramebuffer'), - createProgram: incID('createProgram'), - createShader: incID('createShader'), - createTexture: incID('createTexture'), - deleteBuffer: noop('deleteBuffer'), - deleteProgram: noop('deleteProgram'), - deleteShader: noop('deleteShader'), - deleteTexture: noop('deleteTexture'), - depthFunc: noop('depthFunc'), - disable: noop('disable'), - disableVertexAttribArray: noop('disableVertexAttribArray'), - drawArrays: noop('drawArrays'), - enable: noop('enable'), - enableVertexAttribArray: noop('enableVertexAttribArray'), - finish: noop('finish'), - getExtension: incID('getExtension'), - getParameter: function (key) { - count('getParameter'); - if (key === vgl.GL.DEPTH_BITS) { - return 16; - } - }, - getProgramParameter: function (id, key) { - count('getProgramParameter'); - if (key === vgl.GL.LINK_STATUS) { - return true; - } - }, - getShaderInfoLog: function () { - count('getShaderInfoLog'); - return 'log'; - }, - getShaderParameter: function (id, key) { - count('getShaderParameter'); - if (key === vgl.GL.COMPILE_STATUS) { - return true; - } - }, - getUniformLocation: incID('getUniformLocation'), - isEnabled: function (key) { - count('isEnabled'); - if (key === vgl.GL.BLEND) { - return true; - } - }, - linkProgram: noop('linkProgram'), - pixelStorei: noop('pixelStorei'), - shaderSource: noop('shaderSource'), - texImage2D: noop('texImage2D'), - texParameteri: noop('texParameteri'), - uniform1iv: noop('uniform1iv'), - uniform1fv: noop('uniform1fv'), - uniform2fv: noop('uniform2fv'), - uniform3fv: noop('uniform3fv'), - uniform4fv: noop('uniform4fv'), - uniformMatrix3fv: noop('uniformMatrix3fv'), - uniformMatrix4fv: noop('uniformMatrix4fv'), - useProgram: noop('useProgram'), - vertexAttribPointer: noop('vertexAttribPointer'), - vertexAttrib3fv: noop('vertexAttrib3fv'), - viewport: noop('viewport') - }; - - /* Our mock has only a single renderWindow */ - var m_renderWindow = vgl.renderWindow(); - m_renderWindow._setup = function () { - return true; - }; - m_renderWindow.context = function () { - return m_context; - }; - vgl.renderWindow = function () { - return m_renderWindow; - }; - geo.gl.vglRenderer.supported = function () { - return !!supported; - }; - - vgl._mocked = true; - vgl.mockCounts = function () { - return mockCounts; - }; -}; - -/** - * Restore the vgl renderer to a pristine state. - */ -module.exports.restoreVGLRenderer = function () { - vgl.renderWindow = _renderWindow; - geo.gl.vglRenderer.supported = _supported; - delete vgl._mocked; - delete vgl.mockCounts; -}; - /** * Add counters for various canvas calls so we can tell if they have been used. */