2021-03-09 19:16:56 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
2021-08-06 00:17:05 +00:00
|
|
|
import { IMAGE_JPEG, IMAGE_PNG } from '../../types/MIME';
|
|
|
|
import { sniffImageMimeType } from '../../util/sniffImageMimeType';
|
2021-03-09 19:16:56 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
import { canvasToBytes } from '../../util/canvasToBytes';
|
2021-03-09 19:16:56 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
describe('canvasToBytes', () => {
|
2021-08-06 00:17:05 +00:00
|
|
|
let canvas: HTMLCanvasElement;
|
|
|
|
beforeEach(() => {
|
|
|
|
canvas = document.createElement('canvas');
|
2021-03-09 19:16:56 +00:00
|
|
|
canvas.width = 100;
|
|
|
|
canvas.height = 200;
|
|
|
|
|
|
|
|
const context = canvas.getContext('2d');
|
|
|
|
if (!context) {
|
|
|
|
throw new Error('Test setup error: cannot get canvas rendering context');
|
|
|
|
}
|
|
|
|
context.fillStyle = '#ff9900';
|
|
|
|
context.fillRect(10, 10, 20, 20);
|
2021-08-06 00:17:05 +00:00
|
|
|
});
|
2021-03-09 19:16:56 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('converts a canvas to an Uint8Array, JPEG by default', async () => {
|
|
|
|
const result = await canvasToBytes(canvas);
|
2021-03-09 19:16:56 +00:00
|
|
|
|
2021-08-06 00:17:05 +00:00
|
|
|
assert.strictEqual(sniffImageMimeType(result), IMAGE_JPEG);
|
|
|
|
|
2021-03-09 19:16:56 +00:00
|
|
|
// These are just smoke tests.
|
2021-09-24 00:49:05 +00:00
|
|
|
assert.instanceOf(result, Uint8Array);
|
2021-03-09 19:16:56 +00:00
|
|
|
assert.isAtLeast(result.byteLength, 50);
|
|
|
|
});
|
2021-08-06 00:17:05 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
it('can convert a canvas to a PNG Uint8Array', async () => {
|
|
|
|
const result = await canvasToBytes(canvas, IMAGE_PNG);
|
2021-08-06 00:17:05 +00:00
|
|
|
|
|
|
|
assert.strictEqual(sniffImageMimeType(result), IMAGE_PNG);
|
|
|
|
});
|
2021-03-09 19:16:56 +00:00
|
|
|
});
|