Automated testing using phantomjs

This commit is contained in:
Luke Page 2015-08-03 13:25:48 +01:00
parent 8933a9048d
commit 233925190c
10 changed files with 246 additions and 76 deletions

View file

@ -18,7 +18,12 @@ Known lack of support for:
Tests
=====
Tested using [PNG Suite](http://www.schaik.com/pngsuite/). To see tests, run `node test/test` and `view test/list.html`.
Tested using [PNG Suite](http://www.schaik.com/pngsuite/).
To run the tests in phantomjs, run `node test`.
TODO: How to run the gamma tests
TODO: How to run the tests in chrome
Installation
===============

View file

@ -100,12 +100,17 @@ ParserAsync.prototype._finished = function(data) {
ParserAsync.prototype._complete = function(data, width, height) {
data = bitmapper.dataToBitMap(data, width, height,
this._bpp,
this._depth,
this._interlace);
try {
data = bitmapper.dataToBitMap(data, width, height,
this._bpp,
this._depth,
this._interlace);
data = this._parser.reverseFiltered(data, this._depth, width, height);
data = this._parser.reverseFiltered(data, this._depth, width, height);
}
catch(e) {
this.emit('error', e);
}
this.emit('parsed', data);
};

View file

@ -2,7 +2,15 @@
"name": "pngjs2",
"version": "0.0.2",
"description": "Pure JS PNG encoder/decoder",
"contributors": ["Kuba Niegowski", "Luke Page", "Pietajan De Potter", "Steven Sojka", "Alexandre Paré", "Gaurav Mali", "liangzeng"],
"contributors": [
"Alexandre Paré",
"Gaurav Mali",
"Kuba Niegowski",
"Luke Page",
"Pietajan De Potter",
"Steven Sojka",
"liangzeng"
],
"homepage": "https://github.com/lukeapage/pngjs2",
"keywords": [
"png"
@ -24,5 +32,10 @@
"license": "MIT",
"bugs": {
"url": "https://github.com/lukeapage/pngjs2/issues"
},
"devDependencies": {
"connect": "^3.4.0",
"phantomjs": "^1.9.17",
"serve-static": "^1.10.0"
}
}

85
test/convert-images.js Normal file
View file

@ -0,0 +1,85 @@
var fs = require('fs'),
PNG = require('../lib/png').PNG;
module.exports = function(done) {
fs.readdir(__dirname + '/in/', function (err, files) {
if (err) throw err;
files = files.filter(function(file) {
return Boolean(file.match(/\.png$/i));
});
console.log("Converting images");
var asyncSaved = 0;
files.forEach(function (file) {
var expectedError = false;
if (file.match(/^x/)) {
expectedError = true;
}
var syncError = true;
var data = fs.readFileSync(__dirname + '/in/' + file);
try {
var png = PNG.sync.read(data);
} catch (e) {
if (!expectedError) {
console.log("Unexpected error parsing.." + file);
console.log(e);
console.log(e.stack);
syncError = true;
}
}
if (!syncError) {
if (expectedError) {
console.log("Error expected, parsed fine ..", file);
}
var outpng = new PNG();
//PNG.adjustGamma(png);
outpng.data = png.data;
outpng.width = png.width;
outpng.height = png.height;
outpng.pack()
.pipe(fs.createWriteStream(__dirname + '/outsync/' + file));
}
fs.createReadStream(__dirname + '/in/' + file)
.pipe(new PNG())
.on('error', function (err) {
if (!expectedError) {
console.log("Error reading " + file, err);
}
asyncSaved++;
if (asyncSaved === files.length) {
done();
}
})
.on('parsed', function () {
if (expectedError) {
console.log("Error expected, parsed fine", file);
}
//this.adjustGamma();
this.pack()
.on("end", function() {
asyncSaved++;
if (asyncSaved === files.length) {
done();
}
})
.pipe(fs.createWriteStream(__dirname + '/out/' + file))
});
});
});
}

14
test/http-server.js Normal file
View file

@ -0,0 +1,14 @@
var serveStatic = require('serve-static');
//var serveIndex = require('serve-index');
var http = require('http');
var connect = require('connect');
var app = connect();
server = http.createServer(app);
app.use(serveStatic('test'));
//app.use(serveIndex('test'));
server.listen(8000);
console.log("Tests available at http://localhost:8000/");

View file

@ -198,7 +198,32 @@
<p class="testline"><img class="inimage" src="in/g03n3p04.png"> <img class="asyncimage" src="out/g03n3p04.png"> <img class="syncimage" src="outsync/g03n3p04.png"> </p>
<p class="testline"><img class="inimage" src="in/g03n2c08.png"> <img class="asyncimage" src="out/g03n2c08.png"> <img class="syncimage" src="outsync/g03n2c08.png"> </p>
<p class="testline"><img class="inimage" src="in/g03n0g16.png"> <img class="asyncimage" src="out/g03n0g16.png"> <img class="syncimage" src="outsync/g03n0g16.png"> </p>
<script>
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
</script>
<script src="imagediff.js"></script>
<script>
@ -214,7 +239,14 @@
}, 10);
}
window.isFinished = function() {
return testCount && testsDone === testCount;
};
window.results = [];
var testLines = document.getElementsByClassName("testline");
var testCount = testLines.length,
testsDone = 0;
for(var i = 0; i < testLines.length; i++) {
var testLine = testLines[i];
var inImage = testLine.getElementsByClassName("inimage")[0];
@ -223,15 +255,18 @@
waitForImages([inImage, asyncImage, syncImage], function(inImage, asyncImage, syncImage, testLine) {
var success = true;
console.log("testing " + inImage.src);
var tolerance;
if (inImage.src.match(/16\.png/)) {
tolerance = 3; // phantomjs uses a different scaling method.
// In chrome with gamma correction this can be 2.
} else {
tolerance = 0;
}
if (asyncImage.height === 0 || syncImage.height === 0) {
success = false;
} else {
var tolerance = 0;
if (inImage.src.match(/16\.png/)) {
tolerance = 16;
} else if (inImage.src.match(/g04n2c08/)) {
tolerance = 1;
}
var equal = imagediff.equal(inImage, asyncImage, tolerance);
if (!equal) {
success = false;
@ -246,6 +281,8 @@
} else {
testLine.className = testLine.className + " success";
}
results.push({name: inImage.src.replace(/\.png$/, "").replace(/^.*\//, ""), success: success});
testsDone++;
}.bind(null, inImage, asyncImage, syncImage, testLine));
}
</script>

3
test/index.js Normal file
View file

@ -0,0 +1,3 @@
require("./convert-images")(function() {
require("./run-compare");
});

48
test/phantom-compare.js Normal file
View file

@ -0,0 +1,48 @@
/*global phantom:true*/
'use strict';
var page = require('webpage').create();
var last = new Date();
var timeout = 10000;
setInterval(function() {
var results = page.evaluate(function(){
if (window.isFinished && window.isFinished()) {
return window.results;
}
});
if (results) {
var success = true;
for(var i = 0; i < results.length; i++) {
var result = results[i];
console.log(result.name, result.success);
success = success && result.success;
}
phantom.exit(success ? 0 : 1);
return;
}
if (new Date() - last > timeout) {
phantom.exit();
}
}, 100);
page.onConsoleMessage = function(msg, lineNum, sourceId) {
//console.log('CONSOLE: ' + msg);
};
page.onError = function(msg, trace) {
console.log('error.onError', msg, trace);
phantom.exit();
};
phantom.onError = function(msg, trace) {
console.log('error.onError', msg, trace);
phantom.exit();
};
page.open("http://localhost:8000");

23
test/run-compare.js Normal file
View file

@ -0,0 +1,23 @@
require("./http-server");
var path = require('path');
var childProcess = require('child_process');
var phantomjs = require('phantomjs');
var binPath = phantomjs.path;
var childArgs = [
path.join(__dirname, 'phantom-compare.js')
];
try {
console.log("Comparing in PhantomJS");
childProcess.execFile(binPath, childArgs, function (err, stdout, stderr) {
// handle results
console.log("Comparison Test Results:");
console.log(stdout);
process.exit(err ? 1 : 0);
});
} catch(e) {
console.log("Error starting phantomjs");
}

View file

@ -1,63 +0,0 @@
var fs = require('fs'),
PNG = require('../lib/png').PNG;
fs.readdir(__dirname + '/in/', function(err, files) {
if (err) throw err;
files.forEach(function(file) {
if (!file.match(/\.png$/i))
return;
var expectedError = false;
if (file.match(/^x/)) {
expectedError = true;
}
var data = fs.readFileSync(__dirname + '/in/' + file);
try {
var png = PNG.sync.read(data);
} catch (e) {
if (!expectedError) {
console.log("Unexpected error parsing.." + file);
console.log(e);
console.log(e.stack);
}
return;
}
if (expectedError) {
console.log("Error expected, parsed fine ..", file);
}
var outpng = new PNG();
PNG.adjustGamma(png);
outpng.data = png.data;
outpng.width = png.width;
outpng.height = png.height;
outpng.pack()
.pipe(fs.createWriteStream(__dirname + '/outsync/' + file));
fs.createReadStream(__dirname + '/in/' + file)
.pipe(new PNG())
.on('error', function(err) {
if (!expectedError) {
console.log("Error reading " + file, err);
}
})
.on('parsed', function() {
if (expectedError) {
console.log("Error expected, parsed fine", file);
}
this.adjustGamma();
this.pack()
.pipe(fs.createWriteStream(__dirname + '/out/' + file));
});
});
});