2022-03-01 09:43:32 +01: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 12:24:14 +01:00
|
|
|
import React from "react";
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2023-02-28 09:58:23 +01:00
|
|
|
import { fireEvent, render, RenderResult } from "@testing-library/react";
|
2022-12-12 12:24:14 +01:00
|
|
|
|
|
|
|
import RecordingPlayback, { PlaybackLayout } from "../../../../src/components/views/audio_messages/RecordingPlayback";
|
|
|
|
import { Playback } from "../../../../src/audio/Playback";
|
|
|
|
import RoomContext, { TimelineRenderingType } from "../../../../src/contexts/RoomContext";
|
|
|
|
import { createAudioContext } from "../../../../src/audio/compat";
|
2023-02-28 09:58:23 +01:00
|
|
|
import { flushPromises } from "../../../test-utils";
|
|
|
|
import { IRoomState } from "../../../../src/components/structures/RoomView";
|
2022-03-01 09:43:32 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.mock("../../../../src/audio/compat", () => ({
|
2022-03-01 09:43:32 +01:00
|
|
|
createAudioContext: jest.fn(),
|
|
|
|
decodeOgg: jest.fn().mockResolvedValue({}),
|
|
|
|
}));
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("<RecordingPlayback />", () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const mockAudioBufferSourceNode = {
|
|
|
|
addEventListener: jest.fn(),
|
|
|
|
connect: jest.fn(),
|
|
|
|
start: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockAudioContext = {
|
|
|
|
decodeAudioData: jest.fn(),
|
|
|
|
suspend: jest.fn(),
|
|
|
|
resume: jest.fn(),
|
|
|
|
currentTime: 0,
|
|
|
|
createBufferSource: jest.fn().mockReturnValue(mockAudioBufferSourceNode),
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockAudioBuffer = {
|
|
|
|
duration: 99,
|
|
|
|
getChannelData: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockChannelData = new Float32Array();
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
const defaultRoom = { roomId: "!room:server.org", timelineRenderingType: TimelineRenderingType.File } as IRoomState;
|
2022-03-30 10:12:43 +02:00
|
|
|
const getComponent = (props: React.ComponentProps<typeof RecordingPlayback>, room = defaultRoom) =>
|
2023-02-28 09:58:23 +01:00
|
|
|
render(
|
|
|
|
<RoomContext.Provider value={room}>
|
|
|
|
<RecordingPlayback {...props} />
|
|
|
|
</RoomContext.Provider>,
|
|
|
|
);
|
2022-03-01 09:43:32 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(logger, "error").mockRestore();
|
2022-03-01 09:43:32 +01:00
|
|
|
mockAudioBuffer.getChannelData.mockClear().mockReturnValue(mockChannelData);
|
2022-12-12 12:24:14 +01:00
|
|
|
mockAudioContext.decodeAudioData.mockReset().mockImplementation((_b, callback) => callback(mockAudioBuffer));
|
2022-03-01 09:43:32 +01:00
|
|
|
mocked(createAudioContext).mockReturnValue(mockAudioContext as unknown as AudioContext);
|
|
|
|
});
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
const getPlayButton = (component: RenderResult) => component.getByTestId("play-pause-button");
|
2022-03-01 09:43:32 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("renders recording playback", () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const playback = new Playback(new ArrayBuffer(8));
|
|
|
|
const component = getComponent({ playback });
|
|
|
|
expect(component).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("disables play button while playback is decoding", async () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const playback = new Playback(new ArrayBuffer(8));
|
|
|
|
const component = getComponent({ playback });
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(getPlayButton(component)).toHaveAttribute("disabled");
|
|
|
|
expect(getPlayButton(component)).toHaveAttribute("aria-disabled", "true");
|
2022-03-01 09:43:32 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("enables play button when playback is finished decoding", async () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const playback = new Playback(new ArrayBuffer(8));
|
|
|
|
const component = getComponent({ playback });
|
|
|
|
await flushPromises();
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(getPlayButton(component)).not.toHaveAttribute("disabled");
|
|
|
|
expect(getPlayButton(component)).not.toHaveAttribute("aria-disabled", "true");
|
2022-03-01 09:43:32 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("displays error when playback decoding fails", async () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
// stub logger to keep console clean from expected error
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(logger, "error").mockReturnValue(undefined);
|
|
|
|
jest.spyOn(logger, "warn").mockReturnValue(undefined);
|
|
|
|
mockAudioContext.decodeAudioData.mockImplementation((_b, _cb, error) => error(new Error("oh no")));
|
2022-03-01 09:43:32 +01:00
|
|
|
const playback = new Playback(new ArrayBuffer(8));
|
|
|
|
const component = getComponent({ playback });
|
|
|
|
await flushPromises();
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(component.container.querySelector(".text-warning")).toBeDefined();
|
2022-03-01 09:43:32 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("displays pre-prepared playback with correct playback phase", async () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const playback = new Playback(new ArrayBuffer(8));
|
|
|
|
await playback.prepare();
|
|
|
|
const component = getComponent({ playback });
|
|
|
|
// playback already decoded, button is not disabled
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(getPlayButton(component)).not.toHaveAttribute("disabled");
|
|
|
|
expect(getPlayButton(component)).not.toHaveAttribute("aria-disabled", "true");
|
|
|
|
expect(component.container.querySelector(".text-warning")).toBeFalsy();
|
2022-03-01 09:43:32 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("toggles playback on play pause button click", async () => {
|
2022-03-01 09:43:32 +01:00
|
|
|
const playback = new Playback(new ArrayBuffer(8));
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(playback, "toggle").mockResolvedValue(undefined);
|
2022-03-01 09:43:32 +01:00
|
|
|
await playback.prepare();
|
|
|
|
const component = getComponent({ playback });
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
fireEvent.click(getPlayButton(component));
|
2022-03-01 09:43:32 +01:00
|
|
|
|
|
|
|
expect(playback.toggle).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("Composer Layout", () => {
|
|
|
|
it("should have a waveform, no seek bar, and clock", () => {
|
2022-06-15 02:13:13 +02:00
|
|
|
const playback = new Playback(new ArrayBuffer(8));
|
|
|
|
const component = getComponent({ playback, layout: PlaybackLayout.Composer });
|
2022-03-01 09:43:32 +01:00
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(component.container.querySelector(".mx_Clock")).toBeDefined();
|
|
|
|
expect(component.container.querySelector(".mx_Waveform")).toBeDefined();
|
|
|
|
expect(component.container.querySelector(".mx_SeekBar")).toBeFalsy();
|
2022-06-15 02:13:13 +02:00
|
|
|
});
|
2022-03-01 09:43:32 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("Timeline Layout", () => {
|
|
|
|
it("should have a waveform, a seek bar, and clock", () => {
|
2022-06-15 02:13:13 +02:00
|
|
|
const playback = new Playback(new ArrayBuffer(8));
|
|
|
|
const component = getComponent({ playback, layout: PlaybackLayout.Timeline });
|
2022-03-01 09:43:32 +01:00
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(component.container.querySelector(".mx_Clock")).toBeDefined();
|
|
|
|
expect(component.container.querySelector(".mx_Waveform")).toBeDefined();
|
|
|
|
expect(component.container.querySelector(".mx_SeekBar")).toBeDefined();
|
2022-06-15 02:13:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should be the default", () => {
|
2022-06-15 02:13:13 +02:00
|
|
|
const playback = new Playback(new ArrayBuffer(8));
|
|
|
|
const component = getComponent({ playback }); // no layout set for test
|
|
|
|
|
2023-02-28 09:58:23 +01:00
|
|
|
expect(component.container.querySelector(".mx_Clock")).toBeDefined();
|
|
|
|
expect(component.container.querySelector(".mx_Waveform")).toBeDefined();
|
|
|
|
expect(component.container.querySelector(".mx_SeekBar")).toBeDefined();
|
2022-06-15 02:13:13 +02:00
|
|
|
});
|
2022-03-01 09:43:32 +01:00
|
|
|
});
|
|
|
|
});
|