From 233925190cd31a8e7636881c7e7c82b7f6543405 Mon Sep 17 00:00:00 2001 From: Luke Page Date: Mon, 3 Aug 2015 13:25:48 +0100 Subject: [PATCH] Automated testing using phantomjs --- README.md | 7 ++- lib/parser-async.js | 15 ++++-- package.json | 15 +++++- test/convert-images.js | 85 ++++++++++++++++++++++++++++++++++ test/http-server.js | 14 ++++++ test/{list.html => index.html} | 49 +++++++++++++++++--- test/index.js | 3 ++ test/phantom-compare.js | 48 +++++++++++++++++++ test/run-compare.js | 23 +++++++++ test/test.js | 63 ------------------------- 10 files changed, 246 insertions(+), 76 deletions(-) create mode 100644 test/convert-images.js create mode 100644 test/http-server.js rename test/{list.html => index.html} (95%) create mode 100644 test/index.js create mode 100644 test/phantom-compare.js create mode 100644 test/run-compare.js delete mode 100644 test/test.js diff --git a/README.md b/README.md index 89307ae..3684994 100644 --- a/README.md +++ b/README.md @@ -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 =============== diff --git a/lib/parser-async.js b/lib/parser-async.js index 93bc41c..7561747 100644 --- a/lib/parser-async.js +++ b/lib/parser-async.js @@ -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); }; diff --git a/package.json b/package.json index 6ed6b83..1a36f95 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/convert-images.js b/test/convert-images.js new file mode 100644 index 0000000..59d5a5b --- /dev/null +++ b/test/convert-images.js @@ -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)) + + }); + + }); + }); +} diff --git a/test/http-server.js b/test/http-server.js new file mode 100644 index 0000000..711c87b --- /dev/null +++ b/test/http-server.js @@ -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/"); diff --git a/test/list.html b/test/index.html similarity index 95% rename from test/list.html rename to test/index.html index 3b02def..fa20506 100644 --- a/test/list.html +++ b/test/index.html @@ -198,7 +198,32 @@

+ diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..01026c8 --- /dev/null +++ b/test/index.js @@ -0,0 +1,3 @@ +require("./convert-images")(function() { + require("./run-compare"); +}); \ No newline at end of file diff --git a/test/phantom-compare.js b/test/phantom-compare.js new file mode 100644 index 0000000..ca961b8 --- /dev/null +++ b/test/phantom-compare.js @@ -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"); diff --git a/test/run-compare.js b/test/run-compare.js new file mode 100644 index 0000000..4484fc3 --- /dev/null +++ b/test/run-compare.js @@ -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"); +} \ No newline at end of file diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 805ba76..0000000 --- a/test/test.js +++ /dev/null @@ -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)); - - }); - - }); -});