2022-10-22 19:07:39 +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.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
import { mocked } from "jest-mock";
|
2022-12-06 00:19:50 +08:00
|
|
|
// @ts-ignore
|
2022-12-12 19:24:14 +08:00
|
|
|
import Recorder from "opus-recorder/dist/recorder.min.js";
|
2022-12-06 00:19:50 +08:00
|
|
|
|
|
|
|
import { VoiceRecording, voiceRecorderOptions, highQualityRecorderOptions } from "../../src/audio/VoiceRecording";
|
2022-12-12 19:24:14 +08:00
|
|
|
import { createAudioContext } from "../..//src/audio/compat";
|
2022-12-06 00:19:50 +08:00
|
|
|
import MediaDeviceHandler from "../../src/MediaDeviceHandler";
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.mock("opus-recorder/dist/recorder.min.js");
|
2022-12-06 00:19:50 +08:00
|
|
|
const RecorderMock = mocked(Recorder);
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.mock("../../src/audio/compat", () => ({
|
2022-12-06 00:19:50 +08:00
|
|
|
createAudioContext: jest.fn(),
|
|
|
|
}));
|
|
|
|
const createAudioContextMock = mocked(createAudioContext);
|
|
|
|
|
|
|
|
jest.mock("../../src/MediaDeviceHandler");
|
|
|
|
const MediaDeviceHandlerMock = mocked(MediaDeviceHandler);
|
2022-10-22 19:07:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The tests here are heavily using access to private props.
|
|
|
|
* While this is not so great, we can at lest test some behaviour easily this way.
|
|
|
|
*/
|
|
|
|
describe("VoiceRecording", () => {
|
|
|
|
let recording: VoiceRecording;
|
|
|
|
let recorderSecondsSpy: jest.SpyInstance;
|
|
|
|
|
|
|
|
const itShouldNotCallStop = () => {
|
|
|
|
it("should not call stop", () => {
|
|
|
|
expect(recording.stop).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const simulateUpdate = (recorderSeconds: number) => {
|
|
|
|
beforeEach(() => {
|
|
|
|
recorderSecondsSpy.mockReturnValue(recorderSeconds);
|
|
|
|
// @ts-ignore
|
|
|
|
recording.processAudioUpdate(recorderSeconds);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
recording = new VoiceRecording();
|
|
|
|
// @ts-ignore
|
|
|
|
recording.observable = {
|
|
|
|
update: jest.fn(),
|
2022-12-06 00:19:50 +08:00
|
|
|
close: jest.fn(),
|
2022-10-22 19:07:39 +08:00
|
|
|
};
|
|
|
|
jest.spyOn(recording, "stop").mockImplementation();
|
|
|
|
recorderSecondsSpy = jest.spyOn(recording, "recorderSeconds", "get");
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
2022-12-06 00:19:50 +08:00
|
|
|
describe("when starting a recording", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
const mockAudioContext = {
|
|
|
|
createMediaStreamSource: jest.fn().mockReturnValue({
|
|
|
|
connect: jest.fn(),
|
|
|
|
disconnect: jest.fn(),
|
|
|
|
}),
|
|
|
|
createScriptProcessor: jest.fn().mockReturnValue({
|
|
|
|
connect: jest.fn(),
|
|
|
|
disconnect: jest.fn(),
|
|
|
|
addEventListener: jest.fn(),
|
|
|
|
removeEventListener: jest.fn(),
|
|
|
|
}),
|
|
|
|
destination: {},
|
|
|
|
close: jest.fn(),
|
|
|
|
};
|
|
|
|
createAudioContextMock.mockReturnValue(mockAudioContext as unknown as AudioContext);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await recording.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should record high-quality audio if voice processing is disabled", async () => {
|
|
|
|
MediaDeviceHandlerMock.getAudioNoiseSuppression.mockReturnValue(false);
|
|
|
|
await recording.start();
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
audio: expect.objectContaining({ noiseSuppression: { ideal: false } }),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(RecorderMock).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
encoderBitRate: highQualityRecorderOptions.bitrate,
|
|
|
|
encoderApplication: highQualityRecorderOptions.encoderApplication,
|
|
|
|
}),
|
|
|
|
);
|
2022-12-06 00:19:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should record normal-quality voice if voice processing is enabled", async () => {
|
|
|
|
MediaDeviceHandlerMock.getAudioNoiseSuppression.mockReturnValue(true);
|
|
|
|
await recording.start();
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
audio: expect.objectContaining({ noiseSuppression: { ideal: true } }),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(RecorderMock).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
encoderBitRate: voiceRecorderOptions.bitrate,
|
|
|
|
encoderApplication: voiceRecorderOptions.encoderApplication,
|
|
|
|
}),
|
|
|
|
);
|
2022-12-06 00:19:50 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-22 19:07:39 +08:00
|
|
|
describe("when recording", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
// @ts-ignore
|
|
|
|
recording.recording = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and there is an audio update and time left", () => {
|
|
|
|
simulateUpdate(42);
|
|
|
|
itShouldNotCallStop();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and there is an audio update and time is up", () => {
|
|
|
|
// one second above the limit
|
|
|
|
simulateUpdate(901);
|
|
|
|
|
|
|
|
it("should call stop", () => {
|
|
|
|
expect(recording.stop).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and the max length limit has been disabled", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
recording.disableMaxLength();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and there is an audio update and time left", () => {
|
|
|
|
simulateUpdate(42);
|
|
|
|
itShouldNotCallStop();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and there is an audio update and time is up", () => {
|
|
|
|
// one second above the limit
|
|
|
|
simulateUpdate(901);
|
|
|
|
itShouldNotCallStop();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when not recording", () => {
|
|
|
|
describe("and there is an audio update and time left", () => {
|
|
|
|
simulateUpdate(42);
|
|
|
|
itShouldNotCallStop();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and there is an audio update and time is up", () => {
|
|
|
|
// one second above the limit
|
|
|
|
simulateUpdate(901);
|
|
|
|
itShouldNotCallStop();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|