-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathimage-helper.ts
138 lines (118 loc) · 4.58 KB
/
image-helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as BlinkDiff from "blink-diff";
import * as PngJsImage from "pngjs-image";
import { ImageOptions } from "./image-options";
import { INsCapabilities } from "./interfaces/ns-capabilities";
import { IRectangle } from "./interfaces/rectangle";
import { LogImageType } from "./enums/log-image-type";
export class ImageHelper {
private _imageCropRect: IRectangle;
private _blockOutAreas: IRectangle[];
constructor(private _args: INsCapabilities) {
}
get imageCropRect(): IRectangle {
return this._imageCropRect;
}
set imageCropRect(rect: IRectangle) {
this._imageCropRect = rect;
}
get blockOutAreas() {
return this._blockOutAreas;
}
set blockOutAreas(rectangles: IRectangle[]) {
this._blockOutAreas = rectangles;
}
public imageOutputLimit() {
return ImageOptions.outputAll;
}
public thresholdType() {
return ImageOptions.percent;
}
public threshold(thresholdType) {
if (thresholdType == ImageOptions.percent) {
return 0.01; // 0.01 = 1 percent; 500 percent
} else {
return 10; // 10 pixels
}
}
public delta() {
return 20;
}
public static cropImageDefault(_args: INsCapabilities) {
return { x: 0, y: ImageHelper.getOffsetPixels(_args), width: undefined, height: undefined };
}
private static getOffsetPixels(args: INsCapabilities) {
return args.device.config ? args.device.config.offsetPixels : 0
}
private runDiff(diffOptions: BlinkDiff, diffImage: string) {
var that = this;
return new Promise<boolean>((resolve, reject) => {
diffOptions.run(function (error, result) {
if (error) {
throw error;
} else {
let message: string;
let resultCode = diffOptions.hasPassed(result.code);
if (resultCode) {
message = "Screen compare passed!";
console.log(message);
console.log('Found ' + result.differences + ' differences.');
return resolve(true);
} else {
message = `Screen compare failed! Found ${result.differences} differences.\n`;
console.log(message);
if (Object.getOwnPropertyNames(that._args.testReporter).length > 0 && that._args.testReporter.logImageTypes && that._args.testReporter.logImageTypes.indexOf(LogImageType.everyImage) > -1) {
that._args.testReporterLog(message);
that._args.testReporterLog(diffImage);
}
return resolve(false);
}
}
});
});
}
public compareImages(actual: string, expected: string, output: string, valueThreshold: number = this.threshold(this.thresholdType()), typeThreshold: any = this.thresholdType()) {
const diff = new BlinkDiff({
imageAPath: actual,
imageBPath: expected,
imageOutputPath: output,
imageOutputLimit: this.imageOutputLimit(),
thresholdType: typeThreshold,
threshold: valueThreshold,
delta: this.delta(),
cropImageA: this._imageCropRect,
cropImageB: this._imageCropRect,
blockOut: this._blockOutAreas,
verbose: this._args.verbose,
});
if (typeThreshold == ImageOptions.percent) {
valueThreshold = Math.floor(valueThreshold * 100);
}
console.log(`Using ${valueThreshold}\ ${typeThreshold} tolerance`);
const result = this.runDiff(diff, output);
this._blockOutAreas = undefined;
return result;
}
public async clipRectangleImage(rect: IRectangle, path: string) {
let imageToClip: PngJsImage;
imageToClip = await this.readImage(path);
imageToClip.clip(rect.x, rect.y, rect.width, rect.height);
return new Promise((resolve, reject) => {
imageToClip.writeImage(path, (err) => {
if (err) {
return reject(err);
}
return resolve();
});
})
}
public readImage(path: string): Promise<any> {
return new Promise((resolve, reject) => {
PngJsImage.readImage(path, (err, image) => {
if (err) {
return reject(err);
}
return resolve(image);
});
})
}
}