Skip to content

Commit 0b8de25

Browse files
dbkrrobintown
andauthored
Add basic creation / entering tests for group calls (#2575)
* Add basic creation / entering tests for group calls * Missing space Co-authored-by: Robin <[email protected]> * 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 <[email protected]>
1 parent 88ce017 commit 0b8de25

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

spec/test-utils/webrtc.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ class MockMediaStreamAudioSourceNode {
5858
connect() {}
5959
}
6060

61+
class MockAnalyser {
62+
getFloatFrequencyData() { return 0.0; }
63+
}
64+
6165
export class MockAudioContext {
6266
constructor() {}
63-
createAnalyser() { return {}; }
67+
createAnalyser() { return new MockAnalyser(); }
6468
createMediaStreamSource() { return new MockMediaStreamAudioSourceNode(); }
6569
close() {}
6670
}
@@ -154,4 +158,5 @@ export class MockMediaHandler {
154158
}
155159
stopUserMediaStream() { }
156160
hasAudioDevice() { return true; }
161+
stopAllStreams() {}
157162
}

spec/unit/webrtc/groupCall.spec.ts

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { EventType, GroupCallIntent, GroupCallType, Room, RoomMember } from '../../../src';
18+
import { GroupCall } from "../../../src/webrtc/groupCall";
19+
import { MatrixClient } from "../../../src/client";
20+
import { MockAudioContext, MockMediaHandler } from '../../test-utils/webrtc';
21+
22+
const FAKE_SELF_USER_ID = "@me:test.dummy";
23+
const FAKE_SELF_DEVICE_ID = "AAAAAA";
24+
const FAKE_SELF_SESSION_ID = "1";
25+
const FAKE_ROOM_ID = "!fake:test.dummy";
26+
27+
describe('Group Call', function() {
28+
beforeEach(function() {
29+
// @ts-ignore Mock
30+
global.AudioContext = MockAudioContext;
31+
});
32+
33+
it("sends state event to room when creating", async () => {
34+
const mockSendState = jest.fn();
35+
36+
const mockClient = {
37+
sendStateEvent: mockSendState,
38+
groupCallEventHandler: {
39+
groupCalls: new Map(),
40+
},
41+
} as unknown as MatrixClient;
42+
43+
const room = new Room(FAKE_ROOM_ID, mockClient, FAKE_SELF_USER_ID);
44+
const groupCall = new GroupCall(mockClient, room, GroupCallType.Video, false, GroupCallIntent.Prompt);
45+
46+
await groupCall.create();
47+
48+
expect(mockSendState.mock.calls[0][0]).toEqual(FAKE_ROOM_ID);
49+
expect(mockSendState.mock.calls[0][1]).toEqual(EventType.GroupCallPrefix);
50+
expect(mockSendState.mock.calls[0][2]["m.type"]).toEqual(GroupCallType.Video);
51+
expect(mockSendState.mock.calls[0][2]["m.intent"]).toEqual(GroupCallIntent.Prompt);
52+
});
53+
54+
it("sends member state event to room on enter", async () => {
55+
const mockSendState = jest.fn();
56+
const mockMediaHandler = new MockMediaHandler();
57+
58+
const mockClient = {
59+
sendStateEvent: mockSendState,
60+
groupCallEventHandler: {
61+
groupCalls: new Map(),
62+
},
63+
callEventHandler: {
64+
calls: new Map(),
65+
},
66+
mediaHandler: mockMediaHandler,
67+
getMediaHandler: () => mockMediaHandler,
68+
getUserId: () => FAKE_SELF_USER_ID,
69+
getDeviceId: () => FAKE_SELF_DEVICE_ID,
70+
getSessionId: () => FAKE_SELF_SESSION_ID,
71+
emit: jest.fn(),
72+
on: jest.fn(),
73+
removeListener: jest.fn(),
74+
} as unknown as MatrixClient;
75+
76+
const room = new Room(FAKE_ROOM_ID, mockClient, FAKE_SELF_USER_ID);
77+
const groupCall = new GroupCall(mockClient, room, GroupCallType.Video, false, GroupCallIntent.Prompt);
78+
79+
room.currentState.members[FAKE_SELF_USER_ID] = {
80+
userId: FAKE_SELF_USER_ID,
81+
} as unknown as RoomMember;
82+
83+
await groupCall.create();
84+
85+
try {
86+
await groupCall.enter();
87+
88+
expect(mockSendState.mock.lastCall[0]).toEqual(FAKE_ROOM_ID);
89+
expect(mockSendState.mock.lastCall[1]).toEqual(EventType.GroupCallMemberPrefix);
90+
expect(mockSendState.mock.lastCall[2]['m.calls'].length).toEqual(1);
91+
expect(mockSendState.mock.lastCall[2]['m.calls'][0]["m.call_id"]).toEqual(groupCall.groupCallId);
92+
expect(mockSendState.mock.lastCall[2]['m.calls'][0]['m.devices'].length).toEqual(1);
93+
expect(mockSendState.mock.lastCall[2]['m.calls'][0]['m.devices'][0].device_id).toEqual(FAKE_SELF_DEVICE_ID);
94+
} finally {
95+
groupCall.leave();
96+
}
97+
});
98+
});

0 commit comments

Comments
 (0)