-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
348 lines (266 loc) · 12.5 KB
/
index.test.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import { describe, before, it } from 'node:test';
import { strict as assert } from 'node:assert';
import qrcodePng from 'qrcode-png';
import jsQR from 'jsqr';
import { fromPng } from '@rgba-image/png';
describe('qrcode-png', () => {
const content = 'The content, with numbers 123, 日本語, etc. etc.';
let pngTypedArray;
let data;
let width;
let height;
describe('black and white', () => {
before(() => {
pngTypedArray = qrcodePng(content);
({ data, width, height } = fromPng(pngTypedArray));
});
it('creates a QR code which can be decoded', () => {
const parsed = jsQR(data, width, height);
assert.equal(parsed.data.trim(), content);
});
it('adds a padding of 4 pixels to the top by default', () => {
const expectedEmptyRowsTopBytes = width * 4 * 4; // 4 pixels * 4 channels
// Every channel should be 255 for opaque white pixels.
assert.ok(data.subarray(0, expectedEmptyRowsTopBytes).every(n => n === 255));
});
it('adds a padding of 4 pixels to the bottom by default', () => {
const expectedEmptyRowsBottomBytes = width * 4 * 4; // 4 pixels * 4 channels
// Every channel should be 255 for opaque white pixels.
assert.ok(data.subarray(-expectedEmptyRowsBottomBytes).every(n => n === 255));
});
it('adds a padding of 4 pixels to the start of each line by default', () => {
const rowLength = width * 4; // 4 channels
for (let i = 0, len = data.length; i < len; i += rowLength) {
assert.ok(data.subarray(i, i + 4 * 4).every(n => n === 255));
}
});
it('adds a padding of 4 pixels to the end of each line by default', () => {
const rowLength = width * 4; // 4 channels
for (let i = 0, len = data.length; i < len; i += rowLength) {
assert.ok(data.subarray(i + rowLength - 4 * 4, i + rowLength).every(n => n === 255));
}
});
it('sets non-background pixels to black', () => {
// The top left pixel (not counting offsets) must be non-background.
const topLeftOffset = width * 4 * 4 + 4 * 4; // 4 empty rows, plus row padding.
const pixel = Array.from(data.subarray(topLeftOffset, topLeftOffset + 4));
assert.deepEqual(pixel, [0, 0, 0, 255]); // rgba
});
it('uses only black and white for pixel colors', () => {
for (let i = 0, len = data.length; i < len; i += 4) {
const [r, g, b, a] = data.subarray(i, i + 4);
assert.equal(a, 255); // alpha
if (r === 255) {
assert.equal(g, 255);
assert.equal(b, 255);
} else if (r === 0) {
assert.equal(g, 0);
assert.equal(b, 0);
} else {
throw new Error(`Unexpected red pixel value at index ${i}: [${r}, ${g}, ${b}]`);
}
}
});
it('allows padding to be set', () => {
const pngTypedArray = qrcodePng(content, { padding: 1 });
const { data, width, height } = fromPng(pngTypedArray);
const parsed = jsQR(data, width, height);
assert.equal(parsed.data.trim(), content); // Still the expected QR code.
assert.ok(data.subarray(0, width * 4).every(n => n === 255)); // First row is padding.
assert.ok(data.subarray(-width * 4).every(n => n === 255)); // Last row is padding.
for (let i = 0, len = data.length; i < len; i += width * 4) {
const row = data.subarray(i, i + width * 4);
// First pixel in each row is padding.
assert.deepEqual(Array.from(row.subarray(0, 4)), [255, 255, 255, 255]);
// Last pixel in each row is padding.
assert.deepEqual(Array.from(row.subarray(-4)), [255, 255, 255, 255]);
}
// First and last non-padding pixel in the first row are black.
const firstRow = data.subarray(width * 4, width * 8);
const firstPixel = firstRow.subarray(4, 8);
const lastPixel = firstRow.subarray(-8, -4);
assert.deepEqual(Array.from(firstPixel), [0, 0, 0, 255]);
// Last non-padding pixel in first non-padding row is black.
assert.deepEqual(Array.from(lastPixel), [0, 0, 0, 255]);
});
});
describe('custom colors', () => {
const color = [64, 128, 8];
const background = [32, 16, 64];
before(() => {
pngTypedArray = qrcodePng(content, { color, background });
({ data, width, height } = fromPng(pngTypedArray));
});
it('creates a QR code which can be decoded', () => {
const parsed = jsQR(data, width, height);
assert.equal(parsed.data.trim(), content);
});
it('adds a padding of 4 pixels to the top by default', () => {
const expectedEmptyRowsTopBytes = width * 4 * 4; // 4 pixels * 4 channels
// Every channel should be 255 for opaque pixels.
for (let i = 0; i < expectedEmptyRowsTopBytes; i += 4) {
const [r, g, b, a] = data.subarray(i, i + 4);
assert.deepEqual([r, g, b], background);
assert.equal(a, 255);
}
});
it('adds a padding of 4 pixels to the bottom by default', () => {
const expectedEmptyRowsBottomBytes = width * 4 * 4; // 4 pixels * 4 channels
// Every channel should be 255 for opaque pixels.
for (let i = data.length - expectedEmptyRowsBottomBytes, len = data.length; i < len; i += 4) {
const [r, g, b, a] = data.subarray(i, i + 4);
assert.deepEqual([r, g, b], background);
assert.equal(a, 255);
}
});
it('adds a padding of 4 pixels to the start of each line by default', () => {
const rowLength = width * 4; // 4 channels
for (let i = 0, len = data.length; i < len; i += rowLength) {
const [r, g, b, a] = data.subarray(i, i + 4);
assert.deepEqual([r, g, b], background);
assert.equal(a, 255);
}
});
it('adds a padding of 4 pixels to the end of each line by default', () => {
const rowLength = width * 4; // 4 channels
for (let i = 0, len = data.length; i < len; i += rowLength) {
const offset = rowLength - 4 * 4;
const [r, g, b, a] = data.subarray(offset, offset + 4);
assert.deepEqual([r, g, b], background);
assert.equal(a, 255);
}
});
it('sets non-background pixels to color', () => {
// The top left pixel (not counting offsets) must be non-background.
const topLeftOffset = width * 4 * 4 + 4 * 4; // 4 empty rows, plus row padding.
const [r, g, b, a] = data.subarray(topLeftOffset, topLeftOffset + 4);
assert.deepEqual([r, g, b], color);
assert.deepEqual(a, 255);
});
it('uses only color and background for pixel colors', () => {
for (let i = 0, len = data.length; i < len; i += 4) {
assert.equal(data[i + 3], 255); // alpha
if (data[i] === background[0]) {
assert.equal(data[i + 1], background[1]);
assert.equal(data[i + 2], background[2]);
} else if (data[i] === color[0]) {
assert.equal(data[i + 1], color[1]);
assert.equal(data[i + 2], color[2]);
} else {
throw new Error(`Unexpected red pixel value at index ${i}: ${data[i]}`);
}
}
});
it('allows padding to be set', () => {
const pngTypedArray = qrcodePng(content, { padding: 1, color, background });
const { data, width, height } = fromPng(pngTypedArray);
const parsed = jsQR(data, width, height);
assert.equal(parsed.data.trim(), content); // Still the expected QR code.
// First and last row are padding.
for (let i = 0, len = width * 4; i < len; i += 4) {
const firstRowPixel = Array.from(data.subarray(i, i + 4));
const lastRowPixel = Array.from(data.subarray(data.length - i - 4, data.length - i));
assert.deepEqual(firstRowPixel, [...background, 255]);
assert.deepEqual(lastRowPixel, [...background, 255]);
}
for (let i = 0, len = data.length; i < len; i += width * 4) {
const row = data.subarray(i, i + width * 4);
// First pixel in each row is padding.
assert.deepEqual(Array.from(row.subarray(0, 4)), [...background, 255]);
// Last pixel in each row is padding.
assert.deepEqual(Array.from(row.subarray(-4)), [...background, 255]);
}
// First and last non-padding pixel in the first row are color.
const firstRow = data.subarray(width * 4, width * 8);
const firstPixel = Array.from(firstRow.subarray(4, 8));
const lastPixel = Array.from(firstRow.subarray(-8, -4));
assert.deepEqual(firstPixel, [...color, 255]);
// Last non-padding pixel in first non-padding row is color.
assert.deepEqual(lastPixel, [...color, 255]);
});
});
describe('custom colors with alpha', () => {
const color = [64, 128, 8, 128];
const background = [32, 16, 64, 192];
before(() => {
pngTypedArray = qrcodePng(content, { color, background });
({ data, width, height } = fromPng(pngTypedArray));
});
it('creates a QR code which can be decoded', () => {
const parsed = jsQR(data, width, height);
assert.equal(parsed.data.trim(), content);
});
it('adds a padding of 4 pixels to the top by default', () => {
const expectedEmptyRowsTopBytes = width * 4 * 4; // 4 pixels * 4 channels
// Every channel should be 255 for opaque pixels.
for (let i = 0; i < expectedEmptyRowsTopBytes; i += 4) {
assert.deepEqual(Array.from(data.subarray(i, i + 4)), background);
}
});
it('adds a padding of 4 pixels to the bottom by default', () => {
const expectedEmptyRowsBottomBytes = width * 4 * 4; // 4 pixels * 4 channels
// Every channel should be 255 for opaque pixels.
for (let i = data.length - expectedEmptyRowsBottomBytes, len = data.length; i < len; i += 4) {
assert.deepEqual(Array.from(data.subarray(i, i + 4)), background);
}
});
it('adds a padding of 4 pixels to the start of each line by default', () => {
const rowLength = width * 4; // 4 channels
for (let i = 0, len = data.length; i < len; i += rowLength) {
assert.deepEqual(Array.from(data.subarray(i, i + 4)), background);
}
});
it('adds a padding of 4 pixels to the end of each line by default', () => {
const rowLength = width * 4; // 4 channels
for (let i = 0, len = data.length; i < len; i += rowLength) {
const offset = i + rowLength - 4 * 4;
assert.deepEqual(Array.from(data.subarray(offset, offset + 4)), background);
}
});
it('sets non-background pixels to color', () => {
// The top left pixel (not counting offsets) must be non-background.
const topLeftOffset = width * 4 * 4 + 4 * 4; // 4 empty rows, plus row padding.
const pixel = Array.from(data.subarray(topLeftOffset, topLeftOffset + 4));
assert.deepEqual(pixel, color);
});
it('uses only color and background for pixel colors', () => {
for (let i = 0, len = data.length; i < len; i += 4) {
const pixel = Array.from(data.subarray(i, i + 4));
if (pixel[0] === background[0]) {
assert.deepEqual(pixel, background);
} else if (pixel[0] === color[0]) {
assert.deepEqual(pixel, color);
} else {
throw new Error(`Unexpected red pixel value at index ${i}: ${data[i]}`);
}
}
});
it('allows padding to be set', () => {
const pngTypedArray = qrcodePng(content, { padding: 1, color, background });
const { data, width, height } = fromPng(pngTypedArray);
const parsed = jsQR(data, width, height);
assert.equal(parsed.data.trim(), content); // Still the expected QR code.
// First and last row are padding.
for (let i = 0, len = width * 4; i < len; i += 4) {
const firstRowPixel = Array.from(data.subarray(i, i + 4));
const lastRowPixel = Array.from(data.subarray(data.length - i - 4, data.length - i));
assert.deepEqual(firstRowPixel, background);
assert.deepEqual(lastRowPixel, background);
}
for (let i = 0, len = data.length; i < len; i += width * 4) {
const row = data.subarray(i, i + width * 4);
// First pixel in each row is padding.
assert.deepEqual(Array.from(row.subarray(0, 4)), background);
// Last pixel in each row is padding.
assert.deepEqual(Array.from(row.subarray(-4)), background);
}
// First and last non-padding pixel in the first row are color.
const firstRow = data.subarray(width * 4, width * 8);
const firstPixel = Array.from(firstRow.subarray(4, 8));
const lastPixel = Array.from(firstRow.subarray(-8, -4));
assert.deepEqual(firstPixel, color);
// Last non-padding pixel in first non-padding row is color.
assert.deepEqual(lastPixel, color);
});
});
});