forked from rsmbl/Resemble.js
-
Notifications
You must be signed in to change notification settings - Fork 37
/
resemble.spec.js
197 lines (170 loc) · 7.67 KB
/
resemble.spec.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
describe('node-resemble.js', function() {
var EXAMPLE_LARGE_IMAGE = 'example/LargeImage.png';
var EXAMPLE_SMALL_IMAGE = 'example/SmallImage.png';
var EXAMPLE_PEOPLE_IMAGES = [
'example/People.png',
'example/People2.png'
];
var OPTIMISATION_SKIP_STEP = 6;
var DEFAULT_LARGE_IMAGE_THRESHOLD = 1200;
var resemble = require('./resemble.js');
describe('largeImageThreshold', function() {
describe('when unset', function() {
describe('when ignoreAntialiasing is enabled', function() {
it('skips pixels when a dimension is larger than the default threshold (1200)', function(done) {
getLargeImageComparison().ignoreAntialiasing().onComplete(function(data) {
expectPixelsToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
it('does not skip pixels when both dimensions are smaller than the default threshold (1200)', function(done) {
getSmallImageComparison().ignoreAntialiasing().onComplete(function(data) {
expectPixelsNotToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
});
describe('when ignoreAntialiasing is disabled', function() {
it('does not skip pixels when a dimension is larger than the default threshold (1200)', function(done) {
getLargeImageComparison().onComplete(function(data) {
expectPixelsNotToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
it('does not skip pixels when both dimensions are smaller than the default threshold (1200)', function(done) {
getSmallImageComparison().onComplete(function(data) {
expectPixelsNotToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
});
});
describe('when explicitly set', function() {
afterAll(function() {
resemble.outputSettings({largeImageThreshold: DEFAULT_LARGE_IMAGE_THRESHOLD});
});
describe('when ignoreAntialiasing is enabled', function() {
it('skips pixels on images with a dimension larger than the given threshold', function(done) {
resemble.outputSettings({largeImageThreshold: 999});
getSmallImageComparison().ignoreAntialiasing().onComplete(function(data) {
expectPixelsToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
it('does not skip pixels on images with a dimension equal to the given threshold', function(done) {
resemble.outputSettings({largeImageThreshold: 1000});
getSmallImageComparison().ignoreAntialiasing().onComplete(function(data) {
expectPixelsNotToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
it('does not skip pixels on images with both dimensions smaller than the given threshold', function(done) {
resemble.outputSettings({largeImageThreshold: 1001});
getSmallImageComparison().ignoreAntialiasing().onComplete(function(data) {
expectPixelsNotToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
});
describe('when ignoreAntialiasing is disabled', function() {
it('does not skip pixels on images with a dimension larger than the given threshold', function(done) {
resemble.outputSettings({largeImageThreshold: 999});
getSmallImageComparison().onComplete(function(data) {
expectPixelsNotToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
it('does not skip pixels on images with a dimension equal to the given threshold', function(done) {
resemble.outputSettings({largeImageThreshold: 1000});
getSmallImageComparison().onComplete(function(data) {
expectPixelsNotToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
it('does not skip pixels on images with both dimensions smaller than the given threshold', function(done) {
resemble.outputSettings({largeImageThreshold: 1001});
getSmallImageComparison().onComplete(function(data) {
expectPixelsNotToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
});
});
describe('when set to a falsy value', function() {
beforeEach(function() {
resemble.outputSettings({largeImageThreshold: 0});
});
afterAll(function() {
resemble.outputSettings({largeImageThreshold: DEFAULT_LARGE_IMAGE_THRESHOLD});
});
describe('when ignoreAntialiasing is enabled', function() {
it('does not skip pixels on images with a dimension larger than the default threshold (1200)', function(done) {
getLargeImageComparison().ignoreAntialiasing().onComplete(function(data) {
expectPixelsNotToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
it('does not skip pixels on images with a dimension smaller than the default threshold (1200)', function(done) {
getSmallImageComparison().ignoreAntialiasing().onComplete(function(data) {
expectPixelsNotToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
});
describe('when ignoreAntialiasing is disabled', function() {
it('does not skip pixels on images with a dimension larger than the default threshold (1200)', function(done) {
getLargeImageComparison().onComplete(function(data) {
expectPixelsNotToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
it('does not skip pixels on images with a dimension smaller than the default threshold (1200)', function(done) {
getSmallImageComparison().onComplete(function(data) {
expectPixelsNotToBeSkipped(data.getDiffImage(), OPTIMISATION_SKIP_STEP);
done();
});
});
});
});
function expectPixelsToBeSkipped(image, step) {
expect(getPixelForLocation(image, 1, step - 1).alpha).not.toBe(0);
expect(getPixelForLocation(image, 1, step).alpha).toBe(0);
expect(getPixelForLocation(image, 1, step + 1).alpha).not.toBe(0);
expect(getPixelForLocation(image, step - 1, 1).alpha).not.toBe(0);
expect(getPixelForLocation(image, step, 1).alpha).toBe(0);
expect(getPixelForLocation(image, step + 1, 1).alpha).not.toBe(0);
expect(getPixelForLocation(image, step, step).alpha).toBe(0);
}
function expectPixelsNotToBeSkipped(image, step) {
expect(getPixelForLocation(image, 1, step).alpha).not.toBe(0);
expect(getPixelForLocation(image, step, 1).alpha).not.toBe(0);
expect(getPixelForLocation(image, step, step).alpha).not.toBe(0);
}
});
it('rawMisMatchPercentage contains raw result', function(done) {
resemble(
EXAMPLE_PEOPLE_IMAGES[0]
).compareTo(
EXAMPLE_PEOPLE_IMAGES[1]
).onComplete(function(data) {
expect(data.rawMisMatchPercentage).toBe(8.6612);
done();
});
});
function getLargeImageComparison() {
return resemble(EXAMPLE_LARGE_IMAGE).compareTo(EXAMPLE_LARGE_IMAGE);
}
function getSmallImageComparison() {
return resemble(EXAMPLE_SMALL_IMAGE).compareTo(EXAMPLE_SMALL_IMAGE);
}
function getPixelForLocation(image, x, y) {
var index = (image.width * y + x) << 2;
return {
red: image.data[index],
green: image.data[index + 1],
blue: image.data[index + 2],
alpha: image.data[index + 3]
};
}
});