From 0b8de251bf52236f8fca00f124be77541099c0b4 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 9 Aug 2022 13:43:02 +0100 Subject: [PATCH] Add basic creation / entering tests for group calls (#2575) * Add basic creation / entering tests for group calls * Missing space Co-authored-by: Robin * Assert more of the group call member event and also move call leaving to a finally so it doesn't leaving a call hagning if it fails. Co-authored-by: Robin --- spec/test-utils/webrtc.ts | 7 ++- spec/unit/webrtc/groupCall.spec.ts | 98 ++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 spec/unit/webrtc/groupCall.spec.ts diff --git a/spec/test-utils/webrtc.ts b/spec/test-utils/webrtc.ts index 8b5946912..ed124ba7a 100644 --- a/spec/test-utils/webrtc.ts +++ b/spec/test-utils/webrtc.ts @@ -58,9 +58,13 @@ class MockMediaStreamAudioSourceNode { connect() {} } +class MockAnalyser { + getFloatFrequencyData() { return 0.0; } +} + export class MockAudioContext { constructor() {} - createAnalyser() { return {}; } + createAnalyser() { return new MockAnalyser(); } createMediaStreamSource() { return new MockMediaStreamAudioSourceNode(); } close() {} } @@ -154,4 +158,5 @@ export class MockMediaHandler { } stopUserMediaStream() { } hasAudioDevice() { return true; } + stopAllStreams() {} } diff --git a/spec/unit/webrtc/groupCall.spec.ts b/spec/unit/webrtc/groupCall.spec.ts new file mode 100644 index 000000000..36669ea15 --- /dev/null +++ b/spec/unit/webrtc/groupCall.spec.ts @@ -0,0 +1,98 @@ +/* +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 { EventType, GroupCallIntent, GroupCallType, Room, RoomMember } from '../../../src'; +import { GroupCall } from "../../../src/webrtc/groupCall"; +import { MatrixClient } from "../../../src/client"; +import { MockAudioContext, MockMediaHandler } from '../../test-utils/webrtc'; + +const FAKE_SELF_USER_ID = "@me:test.dummy"; +const FAKE_SELF_DEVICE_ID = "AAAAAA"; +const FAKE_SELF_SESSION_ID = "1"; +const FAKE_ROOM_ID = "!fake:test.dummy"; + +describe('Group Call', function() { + beforeEach(function() { + // @ts-ignore Mock + global.AudioContext = MockAudioContext; + }); + + it("sends state event to room when creating", async () => { + const mockSendState = jest.fn(); + + const mockClient = { + sendStateEvent: mockSendState, + groupCallEventHandler: { + groupCalls: new Map(), + }, + } as unknown as MatrixClient; + + const room = new Room(FAKE_ROOM_ID, mockClient, FAKE_SELF_USER_ID); + const groupCall = new GroupCall(mockClient, room, GroupCallType.Video, false, GroupCallIntent.Prompt); + + await groupCall.create(); + + expect(mockSendState.mock.calls[0][0]).toEqual(FAKE_ROOM_ID); + expect(mockSendState.mock.calls[0][1]).toEqual(EventType.GroupCallPrefix); + expect(mockSendState.mock.calls[0][2]["m.type"]).toEqual(GroupCallType.Video); + expect(mockSendState.mock.calls[0][2]["m.intent"]).toEqual(GroupCallIntent.Prompt); + }); + + it("sends member state event to room on enter", async () => { + const mockSendState = jest.fn(); + const mockMediaHandler = new MockMediaHandler(); + + const mockClient = { + sendStateEvent: mockSendState, + groupCallEventHandler: { + groupCalls: new Map(), + }, + callEventHandler: { + calls: new Map(), + }, + mediaHandler: mockMediaHandler, + getMediaHandler: () => mockMediaHandler, + getUserId: () => FAKE_SELF_USER_ID, + getDeviceId: () => FAKE_SELF_DEVICE_ID, + getSessionId: () => FAKE_SELF_SESSION_ID, + emit: jest.fn(), + on: jest.fn(), + removeListener: jest.fn(), + } as unknown as MatrixClient; + + const room = new Room(FAKE_ROOM_ID, mockClient, FAKE_SELF_USER_ID); + const groupCall = new GroupCall(mockClient, room, GroupCallType.Video, false, GroupCallIntent.Prompt); + + room.currentState.members[FAKE_SELF_USER_ID] = { + userId: FAKE_SELF_USER_ID, + } as unknown as RoomMember; + + await groupCall.create(); + + try { + await groupCall.enter(); + + expect(mockSendState.mock.lastCall[0]).toEqual(FAKE_ROOM_ID); + expect(mockSendState.mock.lastCall[1]).toEqual(EventType.GroupCallMemberPrefix); + expect(mockSendState.mock.lastCall[2]['m.calls'].length).toEqual(1); + expect(mockSendState.mock.lastCall[2]['m.calls'][0]["m.call_id"]).toEqual(groupCall.groupCallId); + expect(mockSendState.mock.lastCall[2]['m.calls'][0]['m.devices'].length).toEqual(1); + expect(mockSendState.mock.lastCall[2]['m.calls'][0]['m.devices'][0].device_id).toEqual(FAKE_SELF_DEVICE_ID); + } finally { + groupCall.leave(); + } + }); +});