@tremho/jove-test
Version:
Test API Module for Jove Framework
123 lines (122 loc) • 5.67 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareToComp = exports.compareImages = void 0;
const fs = require('fs');
const path = require('path');
const pixelmatch = require('pixelmatch');
const es_1 = __importDefault(require("jimp/es"));
function compareImages(imgPath1, imgPath2, passingPct) {
// console.log('-->CompareImages ', imgPath1, imgPath2)
return new Promise(resolve => {
let data = {};
let message;
let img1 = null;
let img2 = null;
let pa = [];
if (fs.existsSync(imgPath1)) {
pa.push(es_1.default.read(imgPath1).then(image => {
img1 = image;
}));
}
else {
data.error = 'image not found!';
return resolve(data);
}
if (fs.existsSync(imgPath2)) {
pa.push(es_1.default.read(imgPath2).then(image => {
img2 = image;
}));
}
else {
data.error = 'comp image not found!';
return resolve(data);
}
Promise.all(pa).then(() => {
var _a, _b;
let width = (_a = img1 === null || img1 === void 0 ? void 0 : img1.bitmap) === null || _a === void 0 ? void 0 : _a.width;
let height = (_b = img1 === null || img1 === void 0 ? void 0 : img1.bitmap) === null || _b === void 0 ? void 0 : _b.height;
let tpix, delta;
function part2() {
return __awaiter(this, void 0, void 0, function* () {
let diff;
try {
tpix = width * height;
diff = img2.clone();
// console.log(`images 0=${width}x${height} 1=${img1?.bitmap?.width}x${img1?.bitmap?.height} 2=${img2?.bitmap?.width}x${img2?.bitmap?.height} 3=${diff?.bitmap?.width}x${diff?.bitmap?.height}`)
let data1 = img1.bitmap.data; // await img1.getBufferAsync(Jimp.AUTO)
let data2 = img2.bitmap.data; // await img2.getBufferAsync(Jimp.AUTO)
let data3 = diff.bitmap.data; //await diff.getBufferAsync(Jimp.AUTO)
delta = pixelmatch(data1, data2, data3, width, height, { threshold: 0.1 });
}
catch (e) {
data.error = 'err(2): ' + e.toString();
if (data.error.toLowerCase().indexOf('sizes do not match') !== -1) {
data.error += ` (${img1.bitmap.width}x${img1.bitmap.height} vs ${img2.bitmap.width}x${img2.bitmap.height})`;
const diffPath = imgPath1.substring(0, imgPath1.lastIndexOf('.')) + '-diff.png';
diff.write(diffPath, () => {
// console.log('comp file written as diff', diffPath)
});
}
return resolve(data);
}
try {
const diffPath = imgPath1.substring(0, imgPath1.lastIndexOf('.')) + '-diff.png';
diff.write(diffPath, () => {
// console.log('diff file written', diffPath)
});
}
catch (e) {
message = 'err(2.5): ' + e.toString();
}
});
}
Promise.resolve(part2()).then(() => {
let ok, pct;
try {
pct = 100 * delta / tpix;
ok = pct <= passingPct;
pct = '' + pct;
pct = pct.substring(0, 8);
}
catch (e) {
message = 'err(3): ' + e.toString();
}
// console.log(`${delta} out of ${tpix} pixels differ (${pct}%) in ${width}x${height} image. okay=${ok}`)
data = {
ok,
width,
height,
countDiff: delta,
percentDiff: pct,
error: message
};
resolve(data);
});
});
});
}
exports.compareImages = compareImages;
function compareToComp(imgName, passingPct) {
let plat = 'electron';
// console.log('> compareToComp', imgName, passingPct)
let imgPath1 = path.join('report', 'latest', 'images', imgName);
let rp = fs.realpathSync(imgPath1);
if (rp.indexOf('/mobile/') !== -1)
plat = 'mobile';
let imgPath2 = path.join('report', 'comp', plat, imgName);
// console.log(imgPath1, imgPath2, passingPct)
return compareImages(imgPath1, imgPath2, passingPct);
}
exports.compareToComp = compareToComp;