2022-10-07 05:27:28 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-10-07 05:27:28 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 16:57:16 +03: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-10-07 05:27:28 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
import { mocked, Mocked } from "jest-mock";
|
2024-10-14 19:11:58 +03:00
|
|
|
import { screen, render, act, cleanup } from "jest-matrix-react";
|
2023-01-04 07:44:38 +03:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2024-12-02 13:53:27 +03:00
|
|
|
import { MatrixClient, PendingEventOrdering, Room, RoomStateEvent } from "matrix-js-sdk/src/matrix";
|
2022-10-07 05:27:28 +03:00
|
|
|
import { Widget, ClientWidgetApi } from "matrix-widget-api";
|
2023-01-04 07:44:38 +03:00
|
|
|
import { UserEvent } from "@testing-library/user-event/dist/types/setup/setup";
|
2022-10-07 05:27:28 +03:00
|
|
|
|
2023-08-04 14:22:08 +03:00
|
|
|
import type { RoomMember } from "matrix-js-sdk/src/matrix";
|
2022-10-07 05:27:28 +03:00
|
|
|
import {
|
|
|
|
useMockedCalls,
|
|
|
|
MockedCall,
|
|
|
|
mkRoomMember,
|
|
|
|
stubClient,
|
|
|
|
setupAsyncStoreWithClient,
|
|
|
|
resetAsyncStoreWithClient,
|
|
|
|
wrapInMatrixClientContext,
|
2022-11-10 11:38:48 +03:00
|
|
|
wrapInSdkContext,
|
|
|
|
mkRoomCreateEvent,
|
2023-01-04 07:44:38 +03:00
|
|
|
mockPlatformPeg,
|
2023-08-16 11:29:34 +03:00
|
|
|
useMockMediaDevices,
|
2024-10-15 16:57:26 +03:00
|
|
|
} from "../../../test-utils";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import { CallStore } from "../../../../src/stores/CallStore";
|
|
|
|
import { WidgetMessagingStore } from "../../../../src/stores/widgets/WidgetMessagingStore";
|
|
|
|
import { PipContainer as UnwrappedPipContainer } from "../../../../src/components/structures/PipContainer";
|
|
|
|
import ActiveWidgetStore from "../../../../src/stores/ActiveWidgetStore";
|
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
|
|
|
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
|
|
|
|
import { Action } from "../../../../src/dispatcher/actions";
|
|
|
|
import { ViewRoomPayload } from "../../../../src/dispatcher/payloads/ViewRoomPayload";
|
2023-01-04 07:44:38 +03:00
|
|
|
import { TestSdkContext } from "../../TestSdkContext";
|
2024-10-15 16:57:26 +03:00
|
|
|
import { RoomViewStore } from "../../../../src/stores/RoomViewStore";
|
|
|
|
import { Container, WidgetLayoutStore } from "../../../../src/stores/widgets/WidgetLayoutStore";
|
|
|
|
import WidgetStore from "../../../../src/stores/WidgetStore";
|
|
|
|
import { WidgetType } from "../../../../src/widgets/WidgetType";
|
|
|
|
import { SdkContextClass } from "../../../../src/contexts/SDKContext";
|
|
|
|
import { ElementWidgetActions } from "../../../../src/stores/widgets/ElementWidgetActions";
|
2023-01-04 07:44:38 +03:00
|
|
|
|
2024-10-15 16:57:26 +03:00
|
|
|
jest.mock("../../../../src/stores/OwnProfileStore", () => ({
|
2024-01-08 15:11:44 +03:00
|
|
|
OwnProfileStore: {
|
|
|
|
instance: {
|
|
|
|
isProfileInfoFetched: true,
|
|
|
|
removeListener: jest.fn(),
|
|
|
|
getHttpAvatarUrl: jest.fn().mockReturnValue("http://avatar_url"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
describe("PipContainer", () => {
|
2022-10-07 05:27:28 +03:00
|
|
|
useMockedCalls();
|
|
|
|
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});
|
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
let user: UserEvent;
|
2022-11-10 11:38:48 +03:00
|
|
|
let sdkContext: TestSdkContext;
|
2022-10-07 05:27:28 +03:00
|
|
|
let client: Mocked<MatrixClient>;
|
|
|
|
let room: Room;
|
2022-11-24 11:08:41 +03:00
|
|
|
let room2: Room;
|
2022-10-07 05:27:28 +03:00
|
|
|
let alice: RoomMember;
|
2022-12-22 13:37:07 +03:00
|
|
|
|
2022-10-07 05:27:28 +03:00
|
|
|
beforeEach(async () => {
|
2023-08-16 11:29:34 +03:00
|
|
|
useMockMediaDevices();
|
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
user = userEvent.setup();
|
|
|
|
|
2022-10-07 05:27:28 +03:00
|
|
|
stubClient();
|
2023-06-05 20:12:23 +03:00
|
|
|
client = mocked(MatrixClientPeg.safeGet());
|
2024-01-08 15:11:44 +03:00
|
|
|
client.getUserId.mockReturnValue("@alice:example.org");
|
|
|
|
client.getSafeUserId.mockReturnValue("@alice:example.org");
|
2023-05-23 18:24:12 +03:00
|
|
|
DMRoomMap.makeShared(client);
|
2022-10-07 05:27:28 +03:00
|
|
|
|
|
|
|
room = new Room("!1:example.org", client, "@alice:example.org", {
|
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
|
|
|
alice = mkRoomMember(room.roomId, "@alice:example.org");
|
2022-11-24 11:08:41 +03:00
|
|
|
|
|
|
|
room2 = new Room("!2:example.com", client, "@alice:example.org", {
|
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
|
|
|
client.getRoom.mockImplementation((roomId: string) => {
|
|
|
|
if (roomId === room.roomId) return room;
|
|
|
|
if (roomId === room2.roomId) return room2;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
client.getRooms.mockReturnValue([room, room2]);
|
|
|
|
client.reEmitter.reEmit(room, [RoomStateEvent.Events]);
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
room.currentState.setStateEvents([mkRoomCreateEvent(alice.userId, room.roomId)]);
|
|
|
|
jest.spyOn(room, "getMember").mockImplementation((userId) => (userId === alice.userId ? alice : null));
|
2022-10-07 05:27:28 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
room2.currentState.setStateEvents([mkRoomCreateEvent(alice.userId, room2.roomId)]);
|
2022-10-07 05:27:28 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
await Promise.all(
|
|
|
|
[CallStore.instance, WidgetMessagingStore.instance].map((store) =>
|
|
|
|
setupAsyncStoreWithClient(store, client),
|
|
|
|
),
|
|
|
|
);
|
2022-11-10 11:38:48 +03:00
|
|
|
|
|
|
|
sdkContext = new TestSdkContext();
|
2023-01-04 07:44:38 +03:00
|
|
|
// @ts-ignore PipContainer uses SDKContext in the constructor
|
|
|
|
SdkContextClass.instance = sdkContext;
|
2022-11-10 11:38:48 +03:00
|
|
|
sdkContext.client = client;
|
2022-10-07 05:27:28 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
cleanup();
|
|
|
|
await Promise.all([CallStore.instance, WidgetMessagingStore.instance].map(resetAsyncStoreWithClient));
|
|
|
|
client.reEmitter.stopReEmitting(room, [RoomStateEvent.Events]);
|
2023-08-16 13:20:48 +03:00
|
|
|
jest.clearAllMocks();
|
2022-10-07 05:27:28 +03:00
|
|
|
});
|
|
|
|
|
2022-11-10 11:38:48 +03:00
|
|
|
const renderPip = () => {
|
2023-01-04 07:44:38 +03:00
|
|
|
const PipContainer = wrapInMatrixClientContext(wrapInSdkContext(UnwrappedPipContainer, sdkContext));
|
|
|
|
render(<PipContainer />);
|
2022-11-10 11:38:48 +03:00
|
|
|
};
|
2022-10-07 05:27:28 +03:00
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
const viewRoom = (roomId: string) => {
|
2022-12-12 14:24:14 +03:00
|
|
|
defaultDispatcher.dispatch<ViewRoomPayload>(
|
|
|
|
{
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: roomId,
|
|
|
|
metricsTrigger: undefined,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
);
|
2023-01-04 07:44:38 +03:00
|
|
|
};
|
2022-10-07 05:27:28 +03:00
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
const withCall = async (fn: (call: MockedCall) => Promise<void>): Promise<void> => {
|
2022-10-07 05:27:28 +03:00
|
|
|
MockedCall.create(room, "1");
|
|
|
|
const call = CallStore.instance.getCall(room.roomId);
|
|
|
|
if (!(call instanceof MockedCall)) throw new Error("Failed to create call");
|
|
|
|
|
|
|
|
const widget = new Widget(call.widget);
|
|
|
|
await act(async () => {
|
2024-11-20 16:29:23 +03:00
|
|
|
WidgetStore.instance.addVirtualWidget(call.widget, room.roomId);
|
|
|
|
WidgetMessagingStore.instance.storeMessaging(widget, room.roomId, {
|
|
|
|
stop: () => {},
|
|
|
|
} as unknown as ClientWidgetApi);
|
|
|
|
|
2024-01-29 19:06:12 +03:00
|
|
|
await call.start();
|
2022-10-07 05:27:28 +03:00
|
|
|
ActiveWidgetStore.instance.setWidgetPersistence(widget.id, room.roomId, true);
|
|
|
|
});
|
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
await fn(call);
|
2022-10-07 05:27:28 +03:00
|
|
|
|
|
|
|
cleanup();
|
2024-11-20 16:29:23 +03:00
|
|
|
act(() => {
|
|
|
|
call.destroy();
|
|
|
|
ActiveWidgetStore.instance.destroyPersistentWidget(widget.id, room.roomId);
|
|
|
|
WidgetStore.instance.removeVirtualWidget(widget.id, room.roomId);
|
|
|
|
});
|
2022-10-07 05:27:28 +03:00
|
|
|
};
|
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
const withWidget = async (fn: () => Promise<void>): Promise<void> => {
|
2022-10-07 05:27:28 +03:00
|
|
|
act(() => ActiveWidgetStore.instance.setWidgetPersistence("1", room.roomId, true));
|
2023-01-04 07:44:38 +03:00
|
|
|
await fn();
|
2022-10-07 05:27:28 +03:00
|
|
|
cleanup();
|
|
|
|
ActiveWidgetStore.instance.destroyPersistentWidget("1", room.roomId);
|
|
|
|
};
|
|
|
|
|
2022-11-24 11:08:41 +03:00
|
|
|
const setUpRoomViewStore = () => {
|
2023-01-04 07:44:38 +03:00
|
|
|
sdkContext._RoomViewStore = new RoomViewStore(defaultDispatcher, sdkContext);
|
2022-11-24 11:08:41 +03:00
|
|
|
};
|
|
|
|
|
2022-10-07 05:27:28 +03:00
|
|
|
it("hides if there's no content", () => {
|
|
|
|
renderPip();
|
|
|
|
expect(screen.queryByRole("complementary")).toBeNull();
|
|
|
|
});
|
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
it("shows an active call with back and leave buttons", async () => {
|
2022-10-07 05:27:28 +03:00
|
|
|
renderPip();
|
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
await withCall(async (call) => {
|
2022-10-07 05:27:28 +03:00
|
|
|
screen.getByRole("complementary");
|
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
// The return button should jump to the call
|
2022-10-07 05:27:28 +03:00
|
|
|
const dispatcherSpy = jest.fn();
|
|
|
|
const dispatcherRef = defaultDispatcher.register(dispatcherSpy);
|
2023-01-04 07:44:38 +03:00
|
|
|
await user.click(screen.getByRole("button", { name: "Back" }));
|
|
|
|
expect(dispatcherSpy).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room.roomId,
|
|
|
|
view_call: true,
|
|
|
|
metricsTrigger: expect.any(String),
|
|
|
|
});
|
2022-10-07 05:27:28 +03:00
|
|
|
defaultDispatcher.unregister(dispatcherRef);
|
2023-01-04 07:44:38 +03:00
|
|
|
|
|
|
|
// The leave button should disconnect from the call
|
|
|
|
const disconnectSpy = jest.spyOn(call, "disconnect");
|
|
|
|
await user.click(screen.getByRole("button", { name: "Leave" }));
|
|
|
|
expect(disconnectSpy).toHaveBeenCalled();
|
2022-10-07 05:27:28 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
it("shows a persistent widget with back button when viewing the room", async () => {
|
|
|
|
setUpRoomViewStore();
|
2022-10-07 05:27:28 +03:00
|
|
|
viewRoom(room.roomId);
|
2023-01-04 07:44:38 +03:00
|
|
|
const widget = WidgetStore.instance.addVirtualWidget(
|
|
|
|
{
|
|
|
|
id: "1",
|
2024-01-08 15:11:44 +03:00
|
|
|
creatorUserId: "@alice:example.org",
|
2023-01-04 07:44:38 +03:00
|
|
|
type: WidgetType.CUSTOM.preferred,
|
|
|
|
url: "https://example.org",
|
|
|
|
name: "Example widget",
|
|
|
|
},
|
|
|
|
room.roomId,
|
|
|
|
);
|
2022-10-07 05:27:28 +03:00
|
|
|
renderPip();
|
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
await withWidget(async () => {
|
2022-10-07 05:27:28 +03:00
|
|
|
screen.getByRole("complementary");
|
2023-01-04 07:44:38 +03:00
|
|
|
|
|
|
|
// The return button should maximize the widget
|
|
|
|
const moveSpy = jest.spyOn(WidgetLayoutStore.instance, "moveToContainer");
|
2024-01-08 15:11:44 +03:00
|
|
|
await user.click(await screen.findByRole("button", { name: "Back" }));
|
2023-01-04 07:44:38 +03:00
|
|
|
expect(moveSpy).toHaveBeenCalledWith(room, widget, Container.Center);
|
|
|
|
|
|
|
|
expect(screen.queryByRole("button", { name: "Leave" })).toBeNull();
|
2022-10-07 05:27:28 +03:00
|
|
|
});
|
2023-01-04 07:44:38 +03:00
|
|
|
|
|
|
|
WidgetStore.instance.removeVirtualWidget("1", room.roomId);
|
2022-10-07 05:27:28 +03:00
|
|
|
});
|
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
it("shows a persistent Jitsi widget with back and leave buttons when not viewing the room", async () => {
|
|
|
|
mockPlatformPeg({ supportsJitsiScreensharing: () => true });
|
|
|
|
setUpRoomViewStore();
|
2022-11-24 11:08:41 +03:00
|
|
|
viewRoom(room2.roomId);
|
2023-01-04 07:44:38 +03:00
|
|
|
const widget = WidgetStore.instance.addVirtualWidget(
|
|
|
|
{
|
|
|
|
id: "1",
|
2024-01-08 15:11:44 +03:00
|
|
|
creatorUserId: "@alice:example.org",
|
2023-01-04 07:44:38 +03:00
|
|
|
type: WidgetType.JITSI.preferred,
|
|
|
|
url: "https://meet.example.org",
|
|
|
|
name: "Jitsi example",
|
|
|
|
},
|
|
|
|
room.roomId,
|
|
|
|
);
|
2022-10-07 05:27:28 +03:00
|
|
|
renderPip();
|
|
|
|
|
2023-01-04 07:44:38 +03:00
|
|
|
await withWidget(async () => {
|
2022-10-07 05:27:28 +03:00
|
|
|
screen.getByRole("complementary");
|
2023-01-04 07:44:38 +03:00
|
|
|
|
|
|
|
// The return button should view the room
|
|
|
|
const dispatcherSpy = jest.fn();
|
|
|
|
const dispatcherRef = defaultDispatcher.register(dispatcherSpy);
|
2024-01-08 15:11:44 +03:00
|
|
|
await user.click(await screen.findByRole("button", { name: "Back" }));
|
2023-01-04 07:44:38 +03:00
|
|
|
expect(dispatcherSpy).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room.roomId,
|
|
|
|
metricsTrigger: expect.any(String),
|
|
|
|
});
|
|
|
|
defaultDispatcher.unregister(dispatcherRef);
|
|
|
|
|
|
|
|
// The leave button should hangup the call
|
|
|
|
const sendSpy = jest
|
|
|
|
.fn<
|
|
|
|
ReturnType<ClientWidgetApi["transport"]["send"]>,
|
|
|
|
Parameters<ClientWidgetApi["transport"]["send"]>
|
|
|
|
>()
|
|
|
|
.mockResolvedValue({});
|
|
|
|
const mockMessaging = { transport: { send: sendSpy }, stop: () => {} } as unknown as ClientWidgetApi;
|
|
|
|
WidgetMessagingStore.instance.storeMessaging(new Widget(widget), room.roomId, mockMessaging);
|
|
|
|
await user.click(screen.getByRole("button", { name: "Leave" }));
|
|
|
|
expect(sendSpy).toHaveBeenCalledWith(ElementWidgetActions.HangupCall, {});
|
2022-10-07 05:27:28 +03:00
|
|
|
});
|
2023-01-04 07:44:38 +03:00
|
|
|
|
|
|
|
WidgetStore.instance.removeVirtualWidget("1", room.roomId);
|
2022-10-07 05:27:28 +03:00
|
|
|
});
|
|
|
|
});
|