Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Add gif/webp utility tests
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy committed Mar 25, 2022
1 parent 32e9880 commit c045294
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.1",
"allchange": "^1.0.6",
"babel-jest": "^26.6.3",
"blob-polyfill": "^6.0.20211015",
"chokidar": "^3.5.1",
"concurrently": "^5.3.0",
"enzyme": "^3.11.0",
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function blobIsAnimated(mimeType: string, blob: Blob): Promise<bool
if (mimeType === "image/webp") {
// Only extended file format WEBP images support animation, so grab the expected data range and verify header.
// Based on https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
const arr = await blob.slice(0, 16).arrayBuffer();
const arr = await blob.slice(0, 17).arrayBuffer();
if (
arrayBufferReadStr(arr, 0, 4) === "RIFF" &&
arrayBufferReadStr(arr, 8, 4) === "WEBP" &&
Expand Down
61 changes: 61 additions & 0 deletions test/Image-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import fs from "fs";
import path from "path";

import './skinned-sdk';
import { blobIsAnimated, mayBeAnimated } from "../src/utils/Image";

describe("Image", () => {
describe("mayBeAnimated", () => {
it("image/gif", async () => {
expect(mayBeAnimated("image/gif")).toBeTruthy();
});
it("image/webp", async () => {
expect(mayBeAnimated("image/webp")).toBeTruthy();
});
it("image/png", async () => {
expect(mayBeAnimated("image/png")).toBeFalsy();
});
it("image/jpeg", async () => {
expect(mayBeAnimated("image/jpeg")).toBeFalsy();
});
});

describe("blobIsAnimated", () => {
const animatedGif = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.gif"))]);
const animatedWebp = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.webp"))]);
const staticGif = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo.gif"))]);
const staticWebp = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo.webp"))]);

it("Animated GIF", async () => {
expect(await blobIsAnimated("image/gif", animatedGif)).toBeTruthy();
});

it("Static GIF", async () => {
expect(await blobIsAnimated("image/gif", staticGif)).toBeFalsy();
});

it("Animated WEBP", async () => {
expect(await blobIsAnimated("image/webp", animatedWebp)).toBeTruthy();
});

it("Static WEBP", async () => {
expect(await blobIsAnimated("image/webp", staticWebp)).toBeFalsy();
});
});
});
Binary file added test/images/animated-logo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/images/animated-logo.webp
Binary file not shown.
Binary file added test/images/static-logo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/images/static-logo.webp
Binary file not shown.
1 change: 1 addition & 0 deletions test/setupTests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TextEncoder, TextDecoder } from 'util';
import Adapter from "@wojtekmaj/enzyme-adapter-react-17";
import { configure } from "enzyme";
import "blob-polyfill"; // https://github.com/jsdom/jsdom/issues/2555

import * as languageHandler from "../src/languageHandler";
import SdkConfig, { DEFAULTS } from '../src/SdkConfig';
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2627,6 +2627,11 @@ binary-extensions@^2.0.0:
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
integrity sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==

blob-polyfill@^6.0.20211015:
version "6.0.20211015"
resolved "https://registry.yarnpkg.com/blob-polyfill/-/blob-polyfill-6.0.20211015.tgz#7c47e62347e302e8d1d1ee5e140b881f74bdb23e"
integrity sha512-OGL4bm6ZNpdFAvQugRlQy5MNly8gk15aWi/ZhQHimQsrx9WKD05r+v+xNgHCChLER3MH+9KLAhzuFlwFKrH1Yw==

bluebird@^3.5.0:
version "3.7.2"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
Expand Down

0 comments on commit c045294

Please sign in to comment.