test.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. var PNG = require('pngjs').PNG,
  3. fs = require('fs'),
  4. test = require('tap').test,
  5. path = require('path'),
  6. match = require('../.');
  7. diffTest('1a', '1b', '1diff', 0.05, false, 143);
  8. diffTest('2a', '2b', '2diff', 0.05, false, 12439);
  9. diffTest('3a', '3b', '3diff', 0.05, false, 212);
  10. diffTest('4a', '4b', '4diff', 0.05, false, 36089);
  11. function diffTest(imgPath1, imgPath2, diffPath, threshold, includeAA, expectedMismatch) {
  12. var name = 'comparing ' + imgPath1 + ' to ' + imgPath2 +
  13. ', threshold: ' + threshold + ', includeAA: ' + includeAA;
  14. test(name, function (t) {
  15. var img1 = readImage(imgPath1, function () {
  16. var img2 = readImage(imgPath2, function () {
  17. var expectedDiff = readImage(diffPath, function () {
  18. var diff = new PNG({width: img1.width, height: img1.height});
  19. var mismatch = match(img1.data, img2.data, diff.data, diff.width, diff.height, {
  20. threshold: threshold,
  21. includeAA: includeAA
  22. });
  23. var mismatch2 = match(img1.data, img2.data, null, diff.width, diff.height, {
  24. threshold: threshold,
  25. includeAA: includeAA
  26. });
  27. t.same(diff.data, expectedDiff.data, 'diff image');
  28. t.same(mismatch, expectedMismatch, 'number of mismatched pixels');
  29. t.same(mismatch, mismatch2, 'number of mismatched pixels');
  30. t.end();
  31. });
  32. });
  33. });
  34. });
  35. }
  36. function readImage(name, done) {
  37. return fs.createReadStream(path.join(__dirname, '/fixtures/' + name + '.png')).pipe(new PNG()).on('parsed', done);
  38. }