You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-07 23:02:56 +03:00
Test placing a call in a group call (#2593)
* Test placing a call in a group call Refactors a bit of the call testing stuff Fixes https://github.com/vector-im/element-call/issues/521 * Unused imports * Use expect.toHaveBeenCalledWith() * Types * More types * Add comment on mock typing * Use toHaveBeenCalledWith() * Initialise groupcall & room in beforeEach * Initialise mockMediahandler sensibly * Add type params to mock * Rename mute tests * Move comment * Join / leave in parallel * Remove leftover expect
This commit is contained in:
@@ -70,20 +70,41 @@ export class MockAudioContext {
|
||||
}
|
||||
|
||||
export class MockRTCPeerConnection {
|
||||
private static instances: MockRTCPeerConnection[] = [];
|
||||
|
||||
private negotiationNeededListener: () => void;
|
||||
private needsNegotiation = false;
|
||||
localDescription: RTCSessionDescription;
|
||||
|
||||
public static triggerAllNegotiations() {
|
||||
for (const inst of this.instances) {
|
||||
inst.doNegotiation();
|
||||
}
|
||||
}
|
||||
|
||||
public static resetInstances() {
|
||||
this.instances = [];
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.localDescription = {
|
||||
sdp: DUMMY_SDP,
|
||||
type: 'offer',
|
||||
toJSON: function() { },
|
||||
};
|
||||
|
||||
MockRTCPeerConnection.instances.push(this);
|
||||
}
|
||||
|
||||
addEventListener() { }
|
||||
addEventListener(type: string, listener: () => void) {
|
||||
if (type === 'negotiationneeded') this.negotiationNeededListener = listener;
|
||||
}
|
||||
createDataChannel(label: string, opts: RTCDataChannelInit) { return { label, ...opts }; }
|
||||
createOffer() {
|
||||
return Promise.resolve({});
|
||||
return Promise.resolve({
|
||||
type: 'offer',
|
||||
sdp: DUMMY_SDP,
|
||||
});
|
||||
}
|
||||
setRemoteDescription() {
|
||||
return Promise.resolve();
|
||||
@@ -93,7 +114,17 @@ export class MockRTCPeerConnection {
|
||||
}
|
||||
close() { }
|
||||
getStats() { return []; }
|
||||
addTrack(track: MockMediaStreamTrack) { return new MockRTCRtpSender(track); }
|
||||
addTrack(track: MockMediaStreamTrack) {
|
||||
this.needsNegotiation = true;
|
||||
return new MockRTCRtpSender(track);
|
||||
}
|
||||
|
||||
doNegotiation() {
|
||||
if (this.needsNegotiation && this.negotiationNeededListener) {
|
||||
this.needsNegotiation = false;
|
||||
this.negotiationNeededListener();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class MockRTCRtpSender {
|
||||
@@ -140,6 +171,10 @@ export class MockMediaStream {
|
||||
this.dispatchEvent("addtrack");
|
||||
}
|
||||
removeTrack(track: MockMediaStreamTrack) { this.tracks.splice(this.tracks.indexOf(track), 1); }
|
||||
|
||||
clone() {
|
||||
return new MockMediaStream(this.id, this.tracks);
|
||||
}
|
||||
}
|
||||
|
||||
export class MockMediaDeviceInfo {
|
||||
@@ -149,6 +184,9 @@ export class MockMediaDeviceInfo {
|
||||
}
|
||||
|
||||
export class MockMediaHandler {
|
||||
public userMediaStreams: MediaStream[] = [];
|
||||
public screensharingStreams: MediaStream[] = [];
|
||||
|
||||
getUserMediaStream(audio: boolean, video: boolean) {
|
||||
const tracks = [];
|
||||
if (audio) tracks.push(new MockMediaStreamTrack("audio_track", "audio"));
|
||||
@@ -160,3 +198,32 @@ export class MockMediaHandler {
|
||||
hasAudioDevice() { return true; }
|
||||
stopAllStreams() {}
|
||||
}
|
||||
|
||||
export function installWebRTCMocks() {
|
||||
global.navigator = {
|
||||
mediaDevices: {
|
||||
// @ts-ignore Mock
|
||||
getUserMedia: () => new MockMediaStream("local_stream"),
|
||||
// @ts-ignore Mock
|
||||
enumerateDevices: async () => [new MockMediaDeviceInfo("audio"), new MockMediaDeviceInfo("video")],
|
||||
},
|
||||
};
|
||||
|
||||
global.window = {
|
||||
// @ts-ignore Mock
|
||||
RTCPeerConnection: MockRTCPeerConnection,
|
||||
// @ts-ignore Mock
|
||||
RTCSessionDescription: {},
|
||||
// @ts-ignore Mock
|
||||
RTCIceCandidate: {},
|
||||
getUserMedia: () => new MockMediaStream("local_stream"),
|
||||
};
|
||||
// @ts-ignore Mock
|
||||
global.document = {};
|
||||
|
||||
// @ts-ignore Mock
|
||||
global.AudioContext = MockAudioContext;
|
||||
|
||||
// @ts-ignore Mock
|
||||
global.RTCRtpReceiver = {};
|
||||
}
|
||||
|
Reference in New Issue
Block a user