2022-10-06 16:27:12 +02:00
|
|
|
/*
|
|
|
|
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 React from "react";
|
|
|
|
import { render, screen, cleanup, fireEvent, waitFor } from "@testing-library/react";
|
|
|
|
import { mocked, Mocked } from "jest-mock";
|
2023-08-09 09:18:41 +02:00
|
|
|
import { Room, RoomStateEvent, MatrixEvent, MatrixEventEvent, MatrixClient } from "matrix-js-sdk/src/matrix";
|
2022-10-06 16:27:12 +02:00
|
|
|
import { ClientWidgetApi, Widget } from "matrix-widget-api";
|
2023-11-21 18:12:08 +01:00
|
|
|
// eslint-disable-next-line no-restricted-imports
|
|
|
|
import { MatrixRTCSessionManagerEvents } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSessionManager";
|
|
|
|
// eslint-disable-next-line no-restricted-imports
|
|
|
|
import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession";
|
|
|
|
// eslint-disable-next-line no-restricted-imports
|
|
|
|
import { ICallNotifyContent } from "matrix-js-sdk/src/matrixrtc/types";
|
2022-10-06 16:27:12 +02:00
|
|
|
|
2023-08-04 13:22:08 +02:00
|
|
|
import type { RoomMember } from "matrix-js-sdk/src/matrix";
|
2022-10-06 16:27:12 +02:00
|
|
|
import {
|
|
|
|
useMockedCalls,
|
|
|
|
MockedCall,
|
|
|
|
stubClient,
|
|
|
|
mkRoomMember,
|
|
|
|
setupAsyncStoreWithClient,
|
|
|
|
resetAsyncStoreWithClient,
|
|
|
|
} from "../test-utils";
|
|
|
|
import defaultDispatcher from "../../src/dispatcher/dispatcher";
|
|
|
|
import { Action } from "../../src/dispatcher/actions";
|
|
|
|
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
|
|
|
|
import { CallStore } from "../../src/stores/CallStore";
|
|
|
|
import { WidgetMessagingStore } from "../../src/stores/widgets/WidgetMessagingStore";
|
|
|
|
import DMRoomMap from "../../src/utils/DMRoomMap";
|
|
|
|
import ToastStore from "../../src/stores/ToastStore";
|
|
|
|
import { getIncomingCallToastKey, IncomingCallToast } from "../../src/toasts/IncomingCallToast";
|
2023-11-21 18:12:08 +01:00
|
|
|
import { AudioID } from "../../src/LegacyCallHandler";
|
2022-10-06 16:27:12 +02:00
|
|
|
|
|
|
|
describe("IncomingCallEvent", () => {
|
|
|
|
useMockedCalls();
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});
|
2022-10-06 16:27:12 +02:00
|
|
|
|
|
|
|
let client: Mocked<MatrixClient>;
|
|
|
|
let room: Room;
|
2023-11-21 18:12:08 +01:00
|
|
|
let notifyContent: ICallNotifyContent;
|
2022-10-06 16:27:12 +02:00
|
|
|
let alice: RoomMember;
|
|
|
|
let bob: RoomMember;
|
|
|
|
let call: MockedCall;
|
|
|
|
let widget: Widget;
|
|
|
|
const dmRoomMap = {
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap;
|
|
|
|
const toastStore = {
|
|
|
|
dismissToast: jest.fn(),
|
|
|
|
} as unknown as ToastStore;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
stubClient();
|
2023-06-05 19:12:23 +02:00
|
|
|
client = mocked(MatrixClientPeg.safeGet());
|
2022-10-06 16:27:12 +02:00
|
|
|
|
2023-11-21 18:12:08 +01:00
|
|
|
const audio = document.createElement("audio");
|
|
|
|
audio.id = AudioID.Ring;
|
|
|
|
document.body.appendChild(audio);
|
2022-10-06 16:27:12 +02:00
|
|
|
|
2023-11-21 18:12:08 +01:00
|
|
|
room = new Room("!1:example.org", client, "@alice:example.org");
|
|
|
|
notifyContent = {
|
|
|
|
call_id: "",
|
|
|
|
getRoomId: () => room.roomId,
|
|
|
|
} as unknown as ICallNotifyContent;
|
2022-10-06 16:27:12 +02:00
|
|
|
alice = mkRoomMember(room.roomId, "@alice:example.org");
|
|
|
|
bob = mkRoomMember(room.roomId, "@bob:example.org");
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
client.getRoom.mockImplementation((roomId) => (roomId === room.roomId ? room : null));
|
2022-10-06 16:27:12 +02:00
|
|
|
client.getRooms.mockReturnValue([room]);
|
|
|
|
client.reEmitter.reEmit(room, [RoomStateEvent.Events]);
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
await Promise.all(
|
|
|
|
[CallStore.instance, WidgetMessagingStore.instance].map((store) =>
|
|
|
|
setupAsyncStoreWithClient(store, client),
|
|
|
|
),
|
|
|
|
);
|
2022-10-06 16:27:12 +02:00
|
|
|
|
|
|
|
MockedCall.create(room, "1");
|
2022-10-07 04:27:28 +02:00
|
|
|
const maybeCall = CallStore.instance.getCall(room.roomId);
|
2022-10-06 16:27:12 +02:00
|
|
|
if (!(maybeCall instanceof MockedCall)) throw new Error("Failed to create call");
|
|
|
|
call = maybeCall;
|
|
|
|
|
|
|
|
widget = new Widget(call.widget);
|
|
|
|
WidgetMessagingStore.instance.storeMessaging(widget, room.roomId, {
|
2022-12-12 12:24:14 +01:00
|
|
|
stop: () => {},
|
2022-10-06 16:27:12 +02:00
|
|
|
} as unknown as ClientWidgetApi);
|
|
|
|
|
|
|
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
|
|
|
|
jest.spyOn(ToastStore, "sharedInstance").mockReturnValue(toastStore);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
cleanup(); // Unmount before we do any cleanup that might update the component
|
|
|
|
call.destroy();
|
|
|
|
WidgetMessagingStore.instance.stopMessaging(widget, room.roomId);
|
|
|
|
await Promise.all([CallStore.instance, WidgetMessagingStore.instance].map(resetAsyncStoreWithClient));
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const renderToast = () => {
|
2023-11-21 18:12:08 +01:00
|
|
|
call.event.getContent = () => notifyContent as any;
|
|
|
|
render(<IncomingCallToast notifyEvent={call.event} />);
|
2022-12-12 12:24:14 +01:00
|
|
|
};
|
2022-10-06 16:27:12 +02:00
|
|
|
|
|
|
|
it("correctly shows all the information", () => {
|
2022-11-28 22:37:32 +01:00
|
|
|
call.participants = new Map([
|
|
|
|
[alice, new Set("a")],
|
|
|
|
[bob, new Set(["b1", "b2"])],
|
|
|
|
]);
|
2022-10-06 16:27:12 +02:00
|
|
|
renderToast();
|
|
|
|
|
|
|
|
screen.getByText("Video call started");
|
|
|
|
screen.getByText("Video");
|
2024-01-29 17:06:12 +01:00
|
|
|
screen.getByLabelText("3 people joined");
|
2022-10-06 16:27:12 +02:00
|
|
|
|
|
|
|
screen.getByRole("button", { name: "Join" });
|
|
|
|
screen.getByRole("button", { name: "Close" });
|
|
|
|
});
|
|
|
|
|
2023-11-21 18:12:08 +01:00
|
|
|
it("start ringing on ring notify event", () => {
|
|
|
|
call.event.getContent = () =>
|
|
|
|
({
|
|
|
|
...notifyContent,
|
|
|
|
notify_type: "ring",
|
2024-01-02 19:56:39 +01:00
|
|
|
}) as any;
|
2023-11-21 18:12:08 +01:00
|
|
|
const playMock = jest.fn();
|
|
|
|
const audio = { play: playMock, paused: true };
|
|
|
|
|
|
|
|
jest.spyOn(document, "getElementById").mockReturnValue(audio as any);
|
|
|
|
render(<IncomingCallToast notifyEvent={call.event} />);
|
|
|
|
expect(playMock).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-10-06 16:27:12 +02:00
|
|
|
it("correctly renders toast without a call", () => {
|
|
|
|
call.destroy();
|
|
|
|
renderToast();
|
|
|
|
|
|
|
|
screen.getByText("Video call started");
|
|
|
|
screen.getByText("Video");
|
|
|
|
|
|
|
|
screen.getByRole("button", { name: "Join" });
|
|
|
|
screen.getByRole("button", { name: "Close" });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("joins the call and closes the toast", async () => {
|
|
|
|
renderToast();
|
|
|
|
|
|
|
|
const dispatcherSpy = jest.fn();
|
|
|
|
const dispatcherRef = defaultDispatcher.register(dispatcherSpy);
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Join" }));
|
2022-12-12 12:24:14 +01:00
|
|
|
await waitFor(() =>
|
|
|
|
expect(dispatcherSpy).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room.roomId,
|
2024-01-29 17:06:12 +01:00
|
|
|
skipLobby: false,
|
|
|
|
view_call: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(toastStore.dismissToast).toHaveBeenCalledWith(
|
|
|
|
getIncomingCallToastKey(notifyContent.call_id, room.roomId),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
defaultDispatcher.unregister(dispatcherRef);
|
|
|
|
});
|
|
|
|
it("skips lobby when using shift key click", async () => {
|
|
|
|
renderToast();
|
|
|
|
|
|
|
|
const dispatcherSpy = jest.fn();
|
|
|
|
const dispatcherRef = defaultDispatcher.register(dispatcherSpy);
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Join" }), { shiftKey: true });
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(dispatcherSpy).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room.roomId,
|
|
|
|
skipLobby: true,
|
2022-12-12 12:24:14 +01:00
|
|
|
view_call: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
await waitFor(() =>
|
2023-11-21 18:12:08 +01:00
|
|
|
expect(toastStore.dismissToast).toHaveBeenCalledWith(
|
|
|
|
getIncomingCallToastKey(notifyContent.call_id, room.roomId),
|
|
|
|
),
|
2022-12-12 12:24:14 +01:00
|
|
|
);
|
2022-10-06 16:27:12 +02:00
|
|
|
|
|
|
|
defaultDispatcher.unregister(dispatcherRef);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("closes the toast", async () => {
|
|
|
|
renderToast();
|
|
|
|
|
|
|
|
const dispatcherSpy = jest.fn();
|
|
|
|
const dispatcherRef = defaultDispatcher.register(dispatcherSpy);
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Close" }));
|
2022-12-12 12:24:14 +01:00
|
|
|
await waitFor(() =>
|
2023-11-21 18:12:08 +01:00
|
|
|
expect(toastStore.dismissToast).toHaveBeenCalledWith(
|
|
|
|
getIncomingCallToastKey(notifyContent.call_id, room.roomId),
|
|
|
|
),
|
2022-12-12 12:24:14 +01:00
|
|
|
);
|
2022-10-06 16:27:12 +02:00
|
|
|
|
|
|
|
defaultDispatcher.unregister(dispatcherRef);
|
|
|
|
});
|
2022-10-07 19:31:22 +02:00
|
|
|
|
|
|
|
it("closes toast when the call lobby is viewed", async () => {
|
|
|
|
renderToast();
|
|
|
|
|
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room.roomId,
|
|
|
|
view_call: true,
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
await waitFor(() =>
|
2023-11-21 18:12:08 +01:00
|
|
|
expect(toastStore.dismissToast).toHaveBeenCalledWith(
|
|
|
|
getIncomingCallToastKey(notifyContent.call_id, room.roomId),
|
|
|
|
),
|
2022-12-12 12:24:14 +01:00
|
|
|
);
|
2022-10-07 19:31:22 +02:00
|
|
|
});
|
2023-03-28 15:16:30 +02:00
|
|
|
|
|
|
|
it("closes toast when the call event is redacted", async () => {
|
|
|
|
renderToast();
|
|
|
|
|
|
|
|
const event = room.currentState.getStateEvents(MockedCall.EVENT_TYPE, "1")!;
|
|
|
|
event.emit(MatrixEventEvent.BeforeRedaction, event, {} as unknown as MatrixEvent);
|
|
|
|
|
|
|
|
await waitFor(() =>
|
2023-11-21 18:12:08 +01:00
|
|
|
expect(toastStore.dismissToast).toHaveBeenCalledWith(
|
|
|
|
getIncomingCallToastKey(notifyContent.call_id, room.roomId),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("closes toast when the matrixRTC session has ended", async () => {
|
|
|
|
renderToast();
|
|
|
|
|
|
|
|
client.matrixRTC.emit(MatrixRTCSessionManagerEvents.SessionEnded, room.roomId, {
|
|
|
|
callId: notifyContent.call_id,
|
|
|
|
room: room,
|
|
|
|
} as unknown as MatrixRTCSession);
|
|
|
|
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(toastStore.dismissToast).toHaveBeenCalledWith(
|
|
|
|
getIncomingCallToastKey(notifyContent.call_id, room.roomId),
|
|
|
|
),
|
2023-03-28 15:16:30 +02:00
|
|
|
);
|
|
|
|
});
|
2022-10-06 16:27:12 +02:00
|
|
|
});
|