-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmethods.spec.js
178 lines (127 loc) · 3.95 KB
/
methods.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
var ImageMethods = require("../src/methods.js"),
resemble = require("resemblejs").resemble,
async = require("async"),
_ = require("lodash");
/**
* Lets have one array with all the parsed test data in it
* i.e. files.IMG_0086.canvas or files.IMG_0086.blob
**/
var imagesToDownload = [ "IMG_0032", "IMG_0086" ],
files = {},
basePath;
if(window.__karma__) {
// Karma has the annoying root path of /base
basePath = "base/test/";
} else {
// Otherwise with something like Testling
basePath = "test/";
}
jasmine.getEnv().defaultTimeoutInterval = 60 * 1000;
beforeEach(function(done) {
// Lets just do this once
if(_.size(files)) {
done();
return;
}
/**
* Helper method
**/
var parseCanvas = function(name, callback) {
ImageMethods.getCanvasFromUrl(basePath + name + ".jpg", function(canvas, blob) {
callback(null, {
blob: blob,
canvas: canvas,
name: name
})
});
};
async.map(imagesToDownload, parseCanvas, function(err, _files) {
_files.forEach(function(file) {
files[file.name] = file;
});
done();
});
});
/**
* Unit Tests
**/
describe("methods", function() {
describe("rotate", function() {
it("should resemble original image 1x1 when rotate 360 degrees", function(done) {
resemble(files.IMG_0086.blob)
.compareTo(
(new ImageMethods(files.IMG_0086.canvas)).rotate(360).toBlob()
)
.onComplete(function(diff){
expect(diff.misMatchPercentage).toBe('0.00');
expect(diff.dimensionDifference.width).toBe(0);
expect(diff.dimensionDifference.height).toBe(0);
done();
});
});
});
describe("crop", function() {
it("should resemble original image 1x1 except cropped out areas", function(done) {
resemble(files.IMG_0086.blob)
.compareTo(
ImageMethods.toBlob(
ImageMethods.crop(files.IMG_0086.canvas, 0, 0, files.IMG_0086.canvas.width, files.IMG_0086.canvas.height/2)
)
)
.onComplete(function(diff){
expect(diff.misMatchPercentage).toBe('50.00');
expect(diff.dimensionDifference.width).toBe(0);
expect(diff.dimensionDifference.height).toBe(files.IMG_0086.canvas.height/2);
done();
});
});
});
describe("resize", function() {
it("should still resemble file after resized and blew up", function(done) {
var smallVersion = ImageMethods.resize(files.IMG_0086.canvas, files.IMG_0086.canvas.width/2, files.IMG_0086.canvas.height/2),
blewUpVersion = ImageMethods.resize(smallVersion, smallVersion.width*2, smallVersion.height*2);
resemble(files.IMG_0086.blob)
.compareTo(
ImageMethods.toBlob(blewUpVersion)
)
.onComplete(function(diff){
// We expect the size of the resized images to be correct
expect(smallVersion.width).toBe(files.IMG_0086.canvas.width/2);
expect(smallVersion.height).toBe(files.IMG_0086.canvas.height/2);
expect(blewUpVersion.width).toBe(files.IMG_0086.canvas.width);
expect(blewUpVersion.height).toBe(files.IMG_0086.canvas.height);
// Expect images to be similar, but not the same
expect(Number(diff.misMatchPercentage) > 0).toBe(true);
expect(Number(diff.misMatchPercentage) < 10).toBe(true);
done();
});
});
});
describe("toBlob", function() {
it("should not change how the file resembles", function(done) {
resemble(files.IMG_0086.blob)
.compareTo(
ImageMethods.toBlob(files.IMG_0086.canvas)
)
.onComplete(function(diff) {
expect(diff.misMatchPercentage).toBe('0.00');
done();
});
});
});
describe("getOrientationFromFile", function() {
it("identifies photos rotated via EXIF orientation", function(done) {
ImageMethods.getOrientationFromFile(files.IMG_0032.blob, function(rotate) {
expect(rotate).toBe(90);
done();
});
});
it("identifies photos not rotated via EXIF orientation", function(done) {
ImageMethods.getOrientationFromFile(files.IMG_0086.blob, function(rotate) {
expect(rotate).toBe(0);
done();
});
});
});
});
// });