-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_browser_decoder.html
More file actions
92 lines (78 loc) · 3.56 KB
/
test_browser_decoder.html
File metadata and controls
92 lines (78 loc) · 3.56 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPQR Browser Decoder Test</title>
<script src="web/jsQR.js"></script>
<script src="web/qrcodegen.js"></script>
<script src="web/app.js"></script>
</head>
<body>
<h1>SPQR Browser Decoder Automated Test</h1>
<div id="results"></div>
<script>
async function runTests() {
const results = document.getElementById('results');
results.innerHTML = '<p>Running tests...</p>';
const tests = [
{
name: '2-layer BWRG - Simple text',
file: 'test_3layer.png',
expectedBase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ01234',
expectedRed: '56789abcdefghijklmnopqrstuvwxyz',
expectedCombined: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'
}
];
let passed = 0, failed = 0;
let output = '<h2>Test Results</h2>';
for (const test of tests) {
output += `<h3>${test.name}</h3>`;
try {
const img = new Image();
await new Promise((resolve, reject) => {
img.onload = resolve;
img.onerror = reject;
img.src = test.file;
});
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d', { willReadFrequently: true });
ctx.imageSmoothingEnabled = false;
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const result = detectSPQR(imageData);
if (!result || (!result.base && !result.red)) {
throw new Error('No SPQR data decoded');
}
const { base, red, combined } = result;
let errors = [];
if (base !== test.expectedBase) {
errors.push(`Base mismatch: expected "${test.expectedBase}", got "${base}"`);
}
if (red !== test.expectedRed) {
errors.push(`Red mismatch: expected "${test.expectedRed}", got "${red}"`);
}
if (combined !== test.expectedCombined) {
errors.push(`Combined mismatch: expected "${test.expectedCombined}", got "${combined}"`);
}
if (errors.length > 0) {
throw new Error(errors.join('; '));
}
output += '<p style="color: green;">✓ PASSED</p>';
passed++;
} catch (err) {
output += `<p style="color: red;">✗ FAILED: ${err.message}</p>`;
failed++;
}
}
output += `<h2>Summary: ${passed} passed, ${failed} failed</h2>`;
results.innerHTML = output;
// Exit code for automation
window.testsPassed = passed;
window.testsFailed = failed;
}
window.addEventListener('load', runTests);
</script>
</body>
</html>