Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcus92 committed Jun 15, 2024
1 parent a821642 commit 890fb9f
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/pgsRendererInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class PgsRendererInternal {
// Builds the subtitle.
const pixelData = this.getPixelDataFromComposition(compositionObject, palette, ctxObjects);
if (pixelData) {
this.context?.drawImage(pixelData, window.horizontalPosition, window.verticalPosition);
this.context.drawImage(pixelData, window.horizontalPosition, window.verticalPosition);
}
}
}
Expand Down
Binary file removed tests/files/test-0.png
Binary file not shown.
Binary file added tests/files/test-0.rgba
Binary file not shown.
Binary file removed tests/files/test-1.png
Binary file not shown.
Binary file added tests/files/test-1.rgba
Binary file not shown.
Binary file removed tests/files/test-2.png
Binary file not shown.
Binary file added tests/files/test-2.rgba
Binary file not shown.
Binary file modified tests/files/test.sup
Binary file not shown.
19 changes: 13 additions & 6 deletions tests/pgsRenderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,29 @@ test('load and render pgs', () => {
const dataSup = fs.readFileSync(`${__dirname}/files/test.sup`);

const canvas = document.createElement("canvas");
const context = canvas.getContext("2d")!;
const renderer = new PgsRendererInternal();
renderer.setCanvas(canvas);
renderer.loadFromBuffer(dataSup);

// Helper function to render and compare the image in the test directory.
// Since we only set pixel data and don't use font rendering this should be deterministic on every machine.
const expectImageAtTimestamp = (filename: string, timestamp: number) => {
const dataPng = fs.readFileSync(`${__dirname}/files/${filename}`, 'base64');
const dataExpected = fs.readFileSync(`${__dirname}/files/${filename}`);

renderer.renderAtTimestamp(timestamp);
const dataResult = canvas.toDataURL('image/png').replace('data:image/png;base64,', '');
const dataResult = context.getImageData(0, 0, canvas.width, canvas.height).data;

expect(dataResult).toEqual(dataPng);
expect(dataResult.length).toBe(dataExpected.length);

for (let i = 0; i < dataResult.length; i++) {
if (dataResult[i] != dataExpected[i]) {
fail(`Unexpected pixel data ${filename}!`);
}
}
}

expectImageAtTimestamp('test-0.png', 1.5);
expectImageAtTimestamp('test-1.png', 2.5);
expectImageAtTimestamp('test-2.png', 3.5);
expectImageAtTimestamp('test-0.rgba', 1.5);
expectImageAtTimestamp('test-1.rgba', 2.5);
expectImageAtTimestamp('test-2.rgba', 3.5);
});

0 comments on commit 890fb9f

Please sign in to comment.