2022-04-20 23:03:33 +08: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 { mount } from "enzyme";
|
|
|
|
import { act } from "react-dom/test-utils";
|
2022-05-17 04:54:08 +08:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { MatrixClient, IMyDevice } from "matrix-js-sdk/src/client";
|
2022-05-05 05:02:06 +08:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2022-04-20 23:03:33 +08:00
|
|
|
import { MatrixWidgetType } from "matrix-widget-api";
|
|
|
|
|
2022-05-05 05:02:06 +08:00
|
|
|
import {
|
|
|
|
stubClient,
|
|
|
|
stubVideoChannelStore,
|
|
|
|
StubVideoChannelStore,
|
|
|
|
mkRoom,
|
|
|
|
wrapInMatrixClientContext,
|
2022-05-17 04:54:08 +08:00
|
|
|
mockStateEventImplementation,
|
|
|
|
mkVideoChannelMember,
|
2022-05-05 05:02:06 +08:00
|
|
|
} from "../../test-utils";
|
2022-04-20 23:03:33 +08:00
|
|
|
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
|
2022-06-02 21:10:22 +08:00
|
|
|
import { VIDEO_CHANNEL_MEMBER } from "../../../src/utils/VideoChannelUtils";
|
2022-04-20 23:03:33 +08:00
|
|
|
import WidgetStore from "../../../src/stores/WidgetStore";
|
|
|
|
import _VideoRoomView from "../../../src/components/structures/VideoRoomView";
|
|
|
|
import VideoLobby from "../../../src/components/views/voip/VideoLobby";
|
|
|
|
import AppTile from "../../../src/components/views/elements/AppTile";
|
|
|
|
|
|
|
|
const VideoRoomView = wrapInMatrixClientContext(_VideoRoomView);
|
|
|
|
|
|
|
|
describe("VideoRoomView", () => {
|
|
|
|
jest.spyOn(WidgetStore.instance, "getApps").mockReturnValue([{
|
2022-06-02 21:10:22 +08:00
|
|
|
id: "1",
|
2022-04-20 23:03:33 +08:00
|
|
|
eventId: "$1:example.org",
|
|
|
|
roomId: "!1:example.org",
|
|
|
|
type: MatrixWidgetType.JitsiMeet,
|
|
|
|
url: "https://example.org",
|
|
|
|
name: "Video channel",
|
|
|
|
creatorUserId: "@alice:example.org",
|
|
|
|
avatar_url: null,
|
2022-06-02 21:10:22 +08:00
|
|
|
data: { isVideoChannel: true },
|
2022-04-20 23:03:33 +08:00
|
|
|
}]);
|
|
|
|
Object.defineProperty(navigator, "mediaDevices", {
|
|
|
|
value: { enumerateDevices: () => [] },
|
|
|
|
});
|
|
|
|
|
2022-05-05 05:02:06 +08:00
|
|
|
let cli: MatrixClient;
|
|
|
|
let room: Room;
|
|
|
|
let store: StubVideoChannelStore;
|
2022-04-20 23:03:33 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-05-05 05:02:06 +08:00
|
|
|
stubClient();
|
|
|
|
cli = MatrixClientPeg.get();
|
|
|
|
jest.spyOn(WidgetStore.instance, "matrixClient", "get").mockReturnValue(cli);
|
2022-04-20 23:03:33 +08:00
|
|
|
store = stubVideoChannelStore();
|
2022-05-05 05:02:06 +08:00
|
|
|
room = mkRoom(cli, "!1:example.org");
|
2022-04-20 23:03:33 +08:00
|
|
|
});
|
|
|
|
|
2022-05-17 04:54:08 +08:00
|
|
|
it("removes stuck devices on mount", async () => {
|
|
|
|
// Simulate an unclean disconnect
|
|
|
|
store.roomId = "!1:example.org";
|
|
|
|
|
|
|
|
const devices: IMyDevice[] = [
|
|
|
|
{
|
|
|
|
device_id: cli.getDeviceId(),
|
|
|
|
last_seen_ts: new Date().valueOf(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
device_id: "went offline 2 hours ago",
|
|
|
|
last_seen_ts: new Date().valueOf() - 1000 * 60 * 60 * 2,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
mocked(cli).getDevices.mockResolvedValue({ devices });
|
|
|
|
|
|
|
|
// Make both devices be stuck
|
|
|
|
mocked(room.currentState).getStateEvents.mockImplementation(mockStateEventImplementation([
|
|
|
|
mkVideoChannelMember(cli.getUserId(), devices.map(d => d.device_id)),
|
|
|
|
]));
|
|
|
|
|
|
|
|
mount(<VideoRoomView room={room} resizing={false} />);
|
|
|
|
// Wait for state to settle
|
|
|
|
await act(() => Promise.resolve());
|
|
|
|
|
|
|
|
// All devices should have been removed
|
|
|
|
expect(cli.sendStateEvent).toHaveBeenLastCalledWith(
|
2022-06-09 21:19:52 +08:00
|
|
|
"!1:example.org",
|
|
|
|
VIDEO_CHANNEL_MEMBER,
|
|
|
|
{ devices: [], expires_ts: expect.any(Number) },
|
|
|
|
cli.getUserId(),
|
2022-05-17 04:54:08 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-04-20 23:03:33 +08:00
|
|
|
it("shows lobby and keeps widget loaded when disconnected", async () => {
|
|
|
|
const view = mount(<VideoRoomView room={room} resizing={false} />);
|
|
|
|
// Wait for state to settle
|
2022-05-05 05:02:06 +08:00
|
|
|
await act(() => Promise.resolve());
|
2022-04-20 23:03:33 +08:00
|
|
|
|
|
|
|
expect(view.find(VideoLobby).exists()).toEqual(true);
|
|
|
|
expect(view.find(AppTile).exists()).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("only shows widget when connected", async () => {
|
|
|
|
store.connect("!1:example.org");
|
|
|
|
const view = mount(<VideoRoomView room={room} resizing={false} />);
|
|
|
|
// Wait for state to settle
|
2022-05-05 05:02:06 +08:00
|
|
|
await act(() => Promise.resolve());
|
2022-04-20 23:03:33 +08:00
|
|
|
|
|
|
|
expect(view.find(VideoLobby).exists()).toEqual(false);
|
|
|
|
expect(view.find(AppTile).exists()).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|