2022-06-17 19:03:29 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-06-17 19:03:29 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-06-17 19:03:29 +08:00
|
|
|
*/
|
|
|
|
|
2024-08-01 20:01:05 +08:00
|
|
|
import React from "react";
|
2023-07-17 20:07:58 +08:00
|
|
|
import { EventType, getHttpUriForMxc, IContent, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2022-11-18 17:16:11 +08:00
|
|
|
import { render, RenderResult } from "@testing-library/react";
|
2023-07-17 20:07:58 +08:00
|
|
|
import fetchMock from "fetch-mock-jest";
|
2022-06-17 19:03:29 +08:00
|
|
|
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
|
|
|
import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper";
|
2023-07-17 20:07:58 +08:00
|
|
|
import {
|
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
mockClientMethodsCrypto,
|
|
|
|
mockClientMethodsDevice,
|
|
|
|
mockClientMethodsServer,
|
|
|
|
mockClientMethodsUser,
|
|
|
|
} from "../../../test-utils";
|
2022-06-17 19:03:29 +08:00
|
|
|
import MVideoBody from "../../../../src/components/views/messages/MVideoBody";
|
|
|
|
|
|
|
|
describe("MVideoBody", () => {
|
|
|
|
it("does not crash when given a portrait image", () => {
|
|
|
|
// Check for an unreliable crash caused by a fractional-sized
|
|
|
|
// image dimension being used for a CanvasImageData.
|
2022-11-18 17:16:11 +08:00
|
|
|
const { asFragment } = makeMVideoBody(720, 1280);
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
2022-06-17 19:03:29 +08:00
|
|
|
// If we get here, we did not crash.
|
|
|
|
});
|
2023-07-17 20:07:58 +08:00
|
|
|
|
|
|
|
it("should show poster for encrypted media before downloading it", async () => {
|
|
|
|
const userId = "@user:server";
|
|
|
|
const deviceId = "DEADB33F";
|
|
|
|
const cli = getMockClientWithEventEmitter({
|
|
|
|
...mockClientMethodsUser(userId),
|
|
|
|
...mockClientMethodsServer(),
|
|
|
|
...mockClientMethodsDevice(deviceId),
|
|
|
|
...mockClientMethodsCrypto(),
|
|
|
|
getRooms: jest.fn().mockReturnValue([]),
|
|
|
|
getIgnoredUsers: jest.fn(),
|
|
|
|
getVersions: jest.fn().mockResolvedValue({
|
|
|
|
unstable_features: {
|
|
|
|
"org.matrix.msc3882": true,
|
|
|
|
"org.matrix.msc3886": true,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
});
|
2023-08-14 16:25:13 +08:00
|
|
|
const thumbUrl = "https://server/_matrix/media/v3/download/server/encrypted-poster";
|
2023-07-17 20:07:58 +08:00
|
|
|
fetchMock.getOnce(thumbUrl, { status: 200 });
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-restricted-properties
|
|
|
|
cli.mxcUrlToHttp.mockImplementation(
|
|
|
|
(mxcUrl: string, width?: number, height?: number, resizeMethod?: string, allowDirectLinks?: boolean) => {
|
|
|
|
return getHttpUriForMxc("https://server", mxcUrl, width, height, resizeMethod, allowDirectLinks);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const encryptedMediaEvent = new MatrixEvent({
|
|
|
|
room_id: "!room:server",
|
|
|
|
sender: userId,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
content: {
|
|
|
|
body: "alt for a test video",
|
|
|
|
info: {
|
|
|
|
duration: 420,
|
|
|
|
w: 40,
|
|
|
|
h: 50,
|
|
|
|
thumbnail_file: {
|
|
|
|
url: "mxc://server/encrypted-poster",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
file: {
|
|
|
|
url: "mxc://server/encrypted-image",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { asFragment } = render(
|
|
|
|
<MVideoBody mxEvent={encryptedMediaEvent} mediaEventHelper={new MediaEventHelper(encryptedMediaEvent)} />,
|
|
|
|
);
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
});
|
2022-06-17 19:03:29 +08:00
|
|
|
});
|
|
|
|
|
2022-11-18 17:16:11 +08:00
|
|
|
function makeMVideoBody(w: number, h: number): RenderResult {
|
2023-02-13 19:39:16 +08:00
|
|
|
const content: IContent = {
|
2022-06-17 19:03:29 +08:00
|
|
|
info: {
|
|
|
|
"w": w,
|
|
|
|
"h": h,
|
|
|
|
"mimetype": "video/mp4",
|
|
|
|
"size": 2495675,
|
|
|
|
"thumbnail_file": {
|
|
|
|
url: "",
|
|
|
|
key: { alg: "", key_ops: [], kty: "", k: "", ext: true },
|
|
|
|
iv: "",
|
|
|
|
hashes: {},
|
|
|
|
v: "",
|
|
|
|
},
|
|
|
|
"thumbnail_info": { mimetype: "" },
|
|
|
|
"xyz.amorgan.blurhash": "TrGl6bofof~paxWC?bj[oL%2fPj]",
|
|
|
|
},
|
|
|
|
url: "http://example.com",
|
|
|
|
};
|
|
|
|
|
|
|
|
const event = new MatrixEvent({
|
|
|
|
content,
|
|
|
|
});
|
|
|
|
|
2024-08-01 20:01:05 +08:00
|
|
|
const defaultProps: MVideoBody["props"] = {
|
2022-06-17 19:03:29 +08:00
|
|
|
mxEvent: event,
|
|
|
|
highlights: [],
|
|
|
|
highlightLink: "",
|
|
|
|
onHeightChanged: jest.fn(),
|
|
|
|
onMessageAllowed: jest.fn(),
|
|
|
|
permalinkCreator: {} as RoomPermalinkCreator,
|
|
|
|
mediaEventHelper: { media: { isEncrypted: false } } as MediaEventHelper,
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
mxcUrlToHttp: jest.fn(),
|
|
|
|
});
|
|
|
|
|
2022-11-18 17:16:11 +08:00
|
|
|
return render(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<MVideoBody {...defaultProps} />
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
2022-06-17 19:03:29 +08:00
|
|
|
}
|