signal-desktop/ts/test-electron/util/scaleImageToLevel_test.ts

65 lines
2 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
2021-09-02 15:59:42 +00:00
import loadImage from 'blueimp-load-image';
import { IMAGE_JPEG, IMAGE_PNG } from '../../types/MIME';
import { scaleImageToLevel } from '../../util/scaleImageToLevel';
describe('scaleImageToLevel', () => {
// NOTE: These tests are incomplete.
2021-09-02 15:59:42 +00:00
async function getBlob(path: string): Promise<Blob> {
const response = await fetch(path);
return response.blob();
}
it("doesn't scale images that are already small enough", async () => {
const testCases = [
{
path: '../fixtures/kitten-1-64-64.jpg',
contentType: IMAGE_JPEG,
expectedWidth: 64,
expectedHeight: 64,
},
{
path: '../fixtures/20x200-yellow.png',
contentType: IMAGE_PNG,
expectedWidth: 20,
expectedHeight: 200,
},
];
await Promise.all(
testCases.map(
async ({ path, contentType, expectedWidth, expectedHeight }) => {
2021-09-02 15:59:42 +00:00
const blob = await getBlob(path);
const scaled = await scaleImageToLevel(blob, contentType, true);
2021-09-02 15:59:42 +00:00
const data = await loadImage(scaled.blob, { orientation: true });
const { originalWidth: width, originalHeight: height } = data;
assert.strictEqual(width, expectedWidth);
assert.strictEqual(height, expectedHeight);
assert.strictEqual(scaled.contentType, contentType);
assert.strictEqual(scaled.blob.type, contentType);
}
)
);
});
2021-09-02 15:59:42 +00:00
it('removes EXIF data from small images', async () => {
const original = await getBlob('../fixtures/kitten-2-64-64.jpg');
assert.isDefined(
(await loadImage(original, { meta: true, orientation: true })).exif,
'Test setup failure: expected fixture to have EXIF data'
);
const scaled = await scaleImageToLevel(original, IMAGE_JPEG, true);
assert.isUndefined(
(await loadImage(scaled.blob, { meta: true, orientation: true })).exif
);
});
});