Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
referenceColors: [],
capturedColors: [],
selectedCellIndex: null,
imageLoadRevision: { colorcard: 0, test: 0 },
colorCardImage: null,
testImage: null,
processedTestImage: null,
Expand Down Expand Up @@ -856,7 +857,8 @@
}

// Load an image file
function loadImage(file, type) {
function loadImage(file, type, revision = ++state.imageLoadRevision[type]) {
if (revision !== state.imageLoadRevision[type]) return;
if (!file || !file.type.startsWith('image/')) {
logMessage('Please choose a supported image file', 'error');
return;
Expand All @@ -870,6 +872,7 @@
const img = new Image();
img.onload = function() {
URL.revokeObjectURL(imageUrl);
if (revision !== state.imageLoadRevision[type]) return;
if (!img.width || !img.height || img.width * img.height > 50_000_000) {
logMessage('The selected image dimensions are invalid or exceed the 50-megapixel limit', 'error');
return;
Expand All @@ -885,6 +888,7 @@
};
img.onerror = function() {
URL.revokeObjectURL(imageUrl);
if (revision !== state.imageLoadRevision[type]) return;
logMessage('The selected image could not be decoded', 'error');
};
img.src = imageUrl;
Expand Down Expand Up @@ -1626,6 +1630,8 @@

// Load a demo image
function loadDemoImage(type) {
// Count the selection before fetching, not when its download finishes.
const revision = ++state.imageLoadRevision[type];
const demoUrl = type === 'colorcard'
? 'logcrop.jpg'
: 'log.jpg';
Expand All @@ -1641,9 +1647,10 @@
})
.then(blob => {
const file = new File([blob], `demo-${type}.jpg`, { type: 'image/jpeg' });
loadImage(file, type); // Pass the correct type parameter
loadImage(file, type, revision);
})
.catch(error => {
if (revision !== state.imageLoadRevision[type]) return;
logMessage(`Error loading demo image: ${error.message}`, 'error');
});
}
Expand Down
117 changes: 117 additions & 0 deletions tests/image-loading.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
'use strict';

const assert = require('node:assert/strict');
const { readFileSync } = require('node:fs');
const path = require('node:path');
const { test } = require('node:test');
const vm = require('node:vm');

function createApp() {
const images = [];
const displayed = [];
const revoked = [];
const messages = [];
const requests = [];
const context = vm.createContext({
document: { addEventListener() {} },
Image: class {
constructor() {
this.width = 24;
this.height = 24;
images.push(this);
}
},
File: class {
constructor(parts, name, options) {
this.name = name;
this.type = options.type;
this.size = 1;
}
},
URL: {
createObjectURL: file => `blob:${file.name}`,
revokeObjectURL: url => revoked.push(url)
},
fetch: url => new Promise((resolve, reject) => requests.push({ url, resolve, reject })),
setTimeout,
clearTimeout,
displayed,
messages
});
vm.runInContext(readFileSync(path.join(__dirname, '../app.js'), 'utf8'), context);
vm.runInContext(`
displayColorCardImage = image => displayed.push(['colorcard', image]);
displayTestImage = image => displayed.push(['test', image]);
logMessage = (message, type) => messages.push([message, type]);
`, context);
return { context, images, displayed, revoked, messages, requests };
}

const imageFile = name => ({ name, type: 'image/png', size: 1 });
const settle = () => new Promise(resolve => setImmediate(resolve));

for (const type of ['colorcard', 'test']) {
test(`the latest ${type} selection wins when decoding finishes out of order`, () => {
const app = createApp();
app.context.loadImage(imageFile('old.png'), type);
app.context.loadImage(imageFile('new.png'), type);
app.images[1].onload();
app.images[0].onload();
assert.equal(app.displayed.length, 1);
assert.equal(app.displayed[0][1], app.images[1]);
assert.deepEqual(app.revoked, ['blob:new.png', 'blob:old.png']);
});
}

test('loading a test image does not cancel a color card image', () => {
const app = createApp();
app.context.loadImage(imageFile('card.png'), 'colorcard');
app.context.loadImage(imageFile('test.png'), 'test');
app.images[1].onload();
app.images[0].onload();
assert.equal(app.displayed.length, 2);
});

test('superseded image failures release their URL without reporting an error', () => {
const app = createApp();
app.context.loadImage(imageFile('old.png'), 'test');
app.context.loadImage(imageFile('new.png'), 'test');
app.images[1].onload();
app.images[0].onerror();
assert.equal(app.messages.filter(([, type]) => type === 'error').length, 0);
assert.ok(app.revoked.includes('blob:old.png'));
});

test('a delayed demo download cannot replace a newer uploaded image', async () => {
const app = createApp();
app.context.loadDemoImage('colorcard');
app.context.loadImage(imageFile('new.png'), 'colorcard');
app.images[0].onload();
app.requests[0].resolve({ ok: true, blob: async () => ({}) });
await settle();
assert.equal(app.images.length, 1);
assert.equal(app.displayed.length, 1);
});

test('a newer demo request invalidates an image still being decoded', async () => {
const app = createApp();
app.context.loadImage(imageFile('old.png'), 'test');
app.context.loadDemoImage('test');
app.images[0].onload();
assert.equal(app.displayed.length, 0);
app.requests[0].resolve({ ok: true, blob: async () => ({}) });
await settle();
app.images[1].onload();
assert.equal(app.displayed.length, 1);
assert.equal(app.displayed[0][1], app.images[1]);
});

test('superseded demo failures do not report an error over the current image', async () => {
const app = createApp();
app.context.loadDemoImage('test');
app.context.loadImage(imageFile('new.png'), 'test');
app.images[0].onload();
app.requests[0].reject(new Error('synthetic old request failure'));
await settle();
assert.equal(app.messages.filter(([, type]) => type === 'error').length, 0);
});
Loading