Skip to content

Commit dfd11ed

Browse files
committed
spec: add MediaHandler unit tests
Signed-off-by: László Várady <[email protected]>
1 parent 4693446 commit dfd11ed

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

spec/test-utils/webrtc.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ export class MockRTCRtpSender {
8888
}
8989

9090
export class MockMediaStreamTrack {
91-
constructor(public readonly id: string, public readonly kind: "audio" | "video", public enabled = true) { }
91+
constructor(
92+
public readonly id: string,
93+
public readonly kind: "audio" | "video",
94+
public enabled = true,
95+
public settings = {}) { }
96+
97+
getSettings() { return { deviceId: this.id, ...this.settings }; }
9298

9399
stop() { }
94100
}
@@ -126,8 +132,6 @@ export class MockMediaStream extends MockEventTarget {
126132
super();
127133
}
128134

129-
130-
131135
getTracks() { return this.tracks; }
132136
getAudioTracks() { return this.tracks.filter((track) => track.kind === "audio"); }
133137
getVideoTracks() { return this.tracks.filter((track) => track.kind === "video"); }
@@ -138,6 +142,28 @@ export class MockMediaStream extends MockEventTarget {
138142
removeTrack(track: MockMediaStreamTrack) { this.tracks.splice(this.tracks.indexOf(track), 1); }
139143
}
140144

145+
export class MockMediaDevices extends MockEventTarget {
146+
public async enumerateDevices(): Promise<Array<MockMediaDeviceInfo>> {
147+
return [
148+
new MockMediaDeviceInfo("audioinput"),
149+
new MockMediaDeviceInfo("audiooutput"),
150+
new MockMediaDeviceInfo("videoinput"),
151+
];
152+
}
153+
154+
public async getDisplayMedia(constraints): Promise<MockMediaStream> {
155+
return new MockMediaStream("mock_stream", [new MockMediaStreamTrack("display_track", "video")]);
156+
}
157+
158+
public async getUserMedia(constraints) {
159+
const tracks = [];
160+
if (constraints.audio) tracks.push(new MockMediaStreamTrack("audio_track", "audio", true, constraints.audio));
161+
if (constraints.video) tracks.push(new MockMediaStreamTrack("video_track", "video", true, constraints.video));
162+
163+
return new MockMediaStream("mock_stream", tracks);
164+
}
165+
}
166+
141167
export class MockMediaDeviceInfo {
142168
constructor(
143169
public kind: "audioinput" | "audiooutput" | "videoinput",

spec/unit/webrtc/mediaHandler.spec.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 { MediaHandler } from "../../../src/webrtc/mediaHandler";
18+
import { TestClient } from "../../TestClient";
19+
import { MockMediaDevices } from "../../test-utils/webrtc";
20+
21+
describe("MediaHandler", () => {
22+
let client: TestClient;
23+
let mediaHandler: MediaHandler;
24+
25+
beforeEach(() => {
26+
client = new TestClient("@alice:foo", "somedevice", "token", undefined, {});
27+
mediaHandler = new MediaHandler(client.client);
28+
client.client["mediaHandler"] = mediaHandler;
29+
30+
// @ts-ignore Mock
31+
global.navigator = { mediaDevices: new MockMediaDevices() };
32+
});
33+
34+
afterEach(() => {
35+
mediaHandler.stopAllStreams();
36+
client.stop();
37+
});
38+
39+
describe("getUserMediaStream", () => {
40+
it("should provide audio/video streams with appropriate defaults", async () => {
41+
const stream = await mediaHandler.getUserMediaStream(true, true);
42+
expect(stream.getAudioTracks().length).toBeGreaterThan(0);
43+
expect(stream.getVideoTracks().length).toBeGreaterThan(0);
44+
45+
expect(() => { mediaHandler.stopUserMediaStream(stream); }).not.toThrow();
46+
});
47+
48+
it("should provide media stream with advanced audio settings when requested", async () => {
49+
await mediaHandler.setAudioSettings({
50+
autoGainControl: false,
51+
echoCancellation: true,
52+
noiseSuppression: false,
53+
});
54+
55+
const stream = await mediaHandler.getUserMediaStream(true, true);
56+
expect(stream.getAudioTracks().length).toBeGreaterThan(0);
57+
58+
const actualAudioSettings = stream.getAudioTracks()[0].getSettings();
59+
expect(actualAudioSettings).toHaveProperty("autoGainControl.ideal", false);
60+
expect(actualAudioSettings).toHaveProperty("echoCancellation.ideal", true);
61+
expect(actualAudioSettings).toHaveProperty("noiseSuppression.ideal", false);
62+
});
63+
});
64+
});

0 commit comments

Comments
 (0)