Use pixelmatch instead of resemble.js

This commit is contained in:
Andreas Hocevar
2017-08-16 22:37:47 -04:00
parent 50e15dc3e7
commit b710c1f76e
6 changed files with 49 additions and 33 deletions

View File

@@ -75,8 +75,8 @@
"mocha-phantomjs-core": "^2.1.0",
"mustache": "2.3.0",
"phantomjs-prebuilt": "2.1.15",
"pixelmatch": "^4.0.2",
"proj4": "2.4.4",
"resemblejs": "2.2.4",
"serve-files": "1.0.1",
"sinon": "3.2.0",
"slimerjs": "0.10.3"

View File

@@ -14,7 +14,7 @@
"expect": false,
"expectResemble": false,
"proj4": false,
"resemble": false,
"pixelmatch": false,
"resembleCanvas": false,
"sinon": false,
"where": false

View File

@@ -29,7 +29,7 @@ in Chrome by default).
# Rendering tests
The `test/rendering` directory contains rendering tests which compare a rendered map with a
reference image using [resemble.js](http://huddle.github.io/Resemble.js/).
reference image using [pixelmatch](https://github.com/mapbox/pixelmatch).
To run the tests in the browser, make sure the development server is running
(`make serve`) and open the URL

View File

@@ -19,6 +19,9 @@ module.exports = function(karma) {
},
files: [
{
pattern: 'module-global.js',
watched: false
}, {
pattern: path.resolve(__dirname, require.resolve('jquery/dist/jquery.js')),
watched: false
}, {
@@ -31,7 +34,7 @@ module.exports = function(karma) {
pattern: path.resolve(__dirname, require.resolve('proj4/dist/proj4.js')),
watched: false
}, {
pattern: path.resolve(__dirname, require.resolve('resemblejs/resemble.js')),
pattern: path.resolve(__dirname, require.resolve('pixelmatch/index.js')),
watched: false
}, {
pattern: path.resolve(__dirname, './test-extensions.js')

1
test/module-global.js Normal file
View File

@@ -0,0 +1 @@
window.module = {};

View File

@@ -386,35 +386,47 @@ goog.require('ol.renderer.webgl.Map');
};
function resembleCanvas(canvas, referenceImage, tolerance, done) {
if (showMap) {
var wrapper = document.createElement('div');
wrapper.style.width = canvas.width + 'px';
wrapper.style.height = canvas.height + 'px';
wrapper.appendChild(canvas);
document.body.appendChild(wrapper);
document.body.appendChild(document.createTextNode(referenceImage));
}
resemble(referenceImage)
.compareTo(canvas.getContext('2d').getImageData(
0, 0, canvas.width, canvas.height))
.onComplete(function(data) {
if (!data.isSameDimensions) {
expect().fail(
'The dimensions of the reference image and ' +
'the test canvas are not the same.');
}
if (data.misMatchPercentage > tolerance) {
if (showDiff) {
var diffImage = new Image();
diffImage.src = data.getImageDataUrl();
document.body.appendChild(diffImage);
}
expect(data.misMatchPercentage).to.be.below(tolerance);
}
done();
});
var width = canvas.width;
var height = canvas.height;
var image = new Image();
image.addEventListener('load', function() {
expect(image.width).to.be(width);
expect(image.height).to.be(height);
var referenceCanvas = document.createElement('CANVAS');
referenceCanvas.width = image.width;
referenceCanvas.height = image.height;
var referenceContext = referenceCanvas.getContext('2d');
referenceContext.drawImage(image, 0, 0, image.width, image.height);
if (showMap) {
var wrapper = document.createElement('div');
wrapper.style.width = canvas.width + 'px';
wrapper.style.height = canvas.height + 'px';
wrapper.appendChild(canvas);
document.body.appendChild(wrapper);
document.body.appendChild(document.createTextNode(referenceImage));
}
var context = canvas.getContext('2d');
var output = context.createImageData(canvas.width, canvas.height);
var mismatchPx = pixelmatch(
context.getImageData(0, 0, width, height).data,
referenceContext.getImageData(0, 0, width, height).data,
output.data, width, height);
var mismatchPct = mismatchPx / (width * height) * 100;
if (showDiff && mismatchPct > tolerance) {
var diffCanvas = document.createElement('canvas');
diffCanvas.width = width;
diffCanvas.height = height;
diffCanvas.getContext('2d').putImageData(output, 0, 0);
document.body.appendChild(diffCanvas);
}
expect(mismatchPct).to.be.below(tolerance);
done();
});
image.addEventListener('error', function() {
expect().fail('Reference image could not be loaded');
done();
});
image.src = referenceImage;
}
global.resembleCanvas = resembleCanvas;