Move mock VGL functions to util.
This will make in convenient for other projects to test geojs in non-webgl browsers.
MODIFY
src/util/index.js
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');

ADD
src/util/mockVGL.js
+/* eslint-disable camelcase */
+/* eslint-disable underscore/prefer-constant */
+
+var vgl = require('vgl');
+var vglRenderer = require('../gl/vglRenderer');
+
+var _renderWindow = vgl.renderWindow;
+var _supported = vglRenderer.supported;
+
+module.exports = {};
+
+/**
+ * Replace vgl.renderer with a mocked version for testing in a non-webGL state.
+ * Use resotreVGLRenderer to unmock.
+ *
+ * @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;
+ };
+ 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.mockCounts;
+ }
+};

MODIFY
tests/cases/annotation.js
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();

MODIFY
tests/cases/annotationLayer.js
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();

MODIFY
tests/cases/choroplethFeature.js
tests/cases/choroplethFeature.js
// 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,8 +130,8 @@ 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',
+ expect(Object.keys(choropleth.choropleth.get())).toEqual([
+ 'colorRange', 'scale', 'accessors', 'scalar',
'scalarAggregator', 'name']);
restoreVGLRenderer();
});

MODIFY
tests/cases/contourWrap.js
tests/cases/contourWrap.js
-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 () {
$('<div id="map-contour-wrap"/>')

MODIFY
tests/cases/lineFeature.js
tests/cases/lineFeature.js
// 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;

MODIFY
tests/cases/osmLayer.js
tests/cases/osmLayer.js
// 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) {

MODIFY
tests/cases/pointFeature.js
tests/cases/pointFeature.js
// 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 () {

MODIFY
tests/cases/polygonFeature.js
tests/cases/polygonFeature.js
// 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;

MODIFY
tests/cases/quadFeature.js
tests/cases/quadFeature.js
/* 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;

MODIFY
tests/cases/registry.js
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 () {

MODIFY
tests/cases/renderers.js
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) {

MODIFY
tests/test-utils.js
tests/test-utils.js
/* 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.
*/

Move mock VGL functions to util.

This will make in convenient for other projects to test geojs in non-webgl browsers.