Skip to content

Commit

Permalink
Merge pull request #688 from OpenGeoscience/mock-vgl-as-util
Browse files Browse the repository at this point in the history
Move mock VGL functions to util
  • Loading branch information
manthey authored Apr 27, 2017
2 parents c3c4eaf + 3f52df8 commit 1103e98
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 193 deletions.
1 change: 1 addition & 0 deletions src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
189 changes: 189 additions & 0 deletions src/util/mockVGL.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
2 changes: 1 addition & 1 deletion testing/test-data/base-images.tgz.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f2fa25ac074a409ccc072c3444edfb6f
c9987ae6444d8124142e040ae06b7468
2 changes: 1 addition & 1 deletion testing/test-data/base-images.tgz.url
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://data.kitware.com/api/v1/file/58c934128d777f0aef5d79a7/download
https://data.kitware.com/api/v1/file/590103778d777f16d01e04fb/download
4 changes: 2 additions & 2 deletions tests/cases/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/annotationLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions tests/cases/choroplethFeature.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
});
});
Expand Down
6 changes: 3 additions & 3 deletions tests/cases/contourWrap.js
Original file line number Diff line number Diff line change
@@ -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 () {
$('<div id="map-contour-wrap"/>')
Expand Down
6 changes: 3 additions & 3 deletions tests/cases/lineFeature.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
8 changes: 4 additions & 4 deletions tests/cases/osmLayer.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions tests/cases/pointFeature.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
6 changes: 3 additions & 3 deletions tests/cases/polygonFeature.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
6 changes: 3 additions & 3 deletions tests/cases/quadFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/renderers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 1103e98

Please sign in to comment.