Add voice broadcast pre-recoding PiP (#9548)

This commit is contained in:
Michael Weimann 2022-11-10 09:38:48 +01:00 committed by GitHub
parent afdf289a78
commit abec724387
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 977 additions and 111 deletions

View file

@ -24,6 +24,7 @@ import { SpaceStoreClass } from "../src/stores/spaces/SpaceStore";
import { WidgetLayoutStore } from "../src/stores/widgets/WidgetLayoutStore";
import { WidgetPermissionStore } from "../src/stores/widgets/WidgetPermissionStore";
import WidgetStore from "../src/stores/WidgetStore";
import { VoiceBroadcastPreRecordingStore, VoiceBroadcastRecordingsStore } from "../src/voice-broadcast";
/**
* A class which provides the same API as SdkContextClass but adds additional unsafe setters which can
@ -39,6 +40,8 @@ export class TestSdkContext extends SdkContextClass {
public _PosthogAnalytics?: PosthogAnalytics;
public _SlidingSyncManager?: SlidingSyncManager;
public _SpaceStore?: SpaceStoreClass;
public _VoiceBroadcastRecordingsStore?: VoiceBroadcastRecordingsStore;
public _VoiceBroadcastPreRecordingStore?: VoiceBroadcastPreRecordingStore;
constructor() {
super();

View file

@ -31,6 +31,8 @@ import {
setupAsyncStoreWithClient,
resetAsyncStoreWithClient,
wrapInMatrixClientContext,
wrapInSdkContext,
mkRoomCreateEvent,
} from "../../../test-utils";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import { CallStore } from "../../../../src/stores/CallStore";
@ -41,17 +43,27 @@ import DMRoomMap from "../../../../src/utils/DMRoomMap";
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
import { Action } from "../../../../src/dispatcher/actions";
import { ViewRoomPayload } from "../../../../src/dispatcher/payloads/ViewRoomPayload";
const PipView = wrapInMatrixClientContext(UnwrappedPipView);
import { TestSdkContext } from "../../../TestSdkContext";
import {
VoiceBroadcastInfoState,
VoiceBroadcastPreRecording,
VoiceBroadcastPreRecordingStore,
VoiceBroadcastRecording,
VoiceBroadcastRecordingsStore,
} from "../../../../src/voice-broadcast";
import { mkVoiceBroadcastInfoStateEvent } from "../../../voice-broadcast/utils/test-utils";
describe("PipView", () => {
useMockedCalls();
Object.defineProperty(navigator, "mediaDevices", { value: { enumerateDevices: () => [] } });
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});
let sdkContext: TestSdkContext;
let client: Mocked<MatrixClient>;
let room: Room;
let alice: RoomMember;
let voiceBroadcastRecordingsStore: VoiceBroadcastRecordingsStore;
let voiceBroadcastPreRecordingStore: VoiceBroadcastPreRecordingStore;
beforeEach(async () => {
stubClient();
@ -64,6 +76,9 @@ describe("PipView", () => {
client.getRoom.mockImplementation(roomId => roomId === room.roomId ? room : null);
client.getRooms.mockReturnValue([room]);
alice = mkRoomMember(room.roomId, "@alice:example.org");
room.currentState.setStateEvents([
mkRoomCreateEvent(alice.userId, room.roomId),
]);
jest.spyOn(room, "getMember").mockImplementation(userId => userId === alice.userId ? alice : null);
client.getRoom.mockImplementation(roomId => roomId === room.roomId ? room : null);
@ -73,6 +88,13 @@ describe("PipView", () => {
await Promise.all([CallStore.instance, WidgetMessagingStore.instance].map(
store => setupAsyncStoreWithClient(store, client),
));
sdkContext = new TestSdkContext();
voiceBroadcastRecordingsStore = new VoiceBroadcastRecordingsStore();
voiceBroadcastPreRecordingStore = new VoiceBroadcastPreRecordingStore();
sdkContext.client = client;
sdkContext._VoiceBroadcastRecordingsStore = voiceBroadcastRecordingsStore;
sdkContext._VoiceBroadcastPreRecordingStore = voiceBroadcastPreRecordingStore;
});
afterEach(async () => {
@ -82,7 +104,12 @@ describe("PipView", () => {
jest.restoreAllMocks();
});
const renderPip = () => { render(<PipView />); };
const renderPip = () => {
const PipView = wrapInMatrixClientContext(
wrapInSdkContext(UnwrappedPipView, sdkContext),
);
render(<PipView />);
};
const viewRoom = (roomId: string) =>
defaultDispatcher.dispatch<ViewRoomPayload>({
@ -172,4 +199,44 @@ describe("PipView", () => {
screen.getByRole("button", { name: /return/i });
});
});
describe("when there is a voice broadcast recording", () => {
beforeEach(() => {
const voiceBroadcastInfoEvent = mkVoiceBroadcastInfoStateEvent(
room.roomId,
VoiceBroadcastInfoState.Started,
alice.userId,
client.getDeviceId() || "",
);
const voiceBroadcastRecording = new VoiceBroadcastRecording(voiceBroadcastInfoEvent, client);
voiceBroadcastRecordingsStore.setCurrent(voiceBroadcastRecording);
renderPip();
});
it("should render the voice broadcast recording PiP", () => {
// check for the „Live“ badge
screen.getByText("Live");
});
});
describe("when there is a voice broadcast pre-recording", () => {
beforeEach(() => {
const voiceBroadcastPreRecording = new VoiceBroadcastPreRecording(
room,
alice,
client,
voiceBroadcastRecordingsStore,
);
voiceBroadcastPreRecordingStore.setCurrent(voiceBroadcastPreRecording);
renderPip();
});
it("should render the voice broadcast pre-recording PiP", () => {
// check for the „Go live“ button
screen.getByText("Go live");
});
});
});

View file

@ -0,0 +1,34 @@
/*
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.
*/
import { SdkContextClass } from "../../src/contexts/SDKContext";
import { VoiceBroadcastPreRecordingStore } from "../../src/voice-broadcast";
jest.mock("../../src/voice-broadcast/stores/VoiceBroadcastPreRecordingStore");
describe("SdkContextClass", () => {
const sdkContext = SdkContextClass.instance;
it("instance should always return the same instance", () => {
expect(SdkContextClass.instance).toBe(sdkContext);
});
it("voiceBroadcastPreRecordingStore should always return the same VoiceBroadcastPreRecordingStore", () => {
const first = sdkContext.voiceBroadcastPreRecordingStore;
expect(first).toBeInstanceOf(VoiceBroadcastPreRecordingStore);
expect(sdkContext.voiceBroadcastPreRecordingStore).toBe(first);
});
});

View file

@ -45,6 +45,7 @@ import { getMockClientWithEventEmitter } from "../test-utils/client";
// modern fake timers and lodash.debounce are a faff
// short circuit it
jest.mock("lodash", () => ({
...jest.requireActual("lodash") as object,
debounce: jest.fn().mockImplementation(callback => callback),
}));

View file

@ -32,6 +32,7 @@ import {
IUnsigned,
IPusher,
RoomType,
KNOWN_SAFE_ROOM_VERSION,
} from 'matrix-js-sdk/src/matrix';
import { normalize } from "matrix-js-sdk/src/utils";
import { ReEmitter } from "matrix-js-sdk/src/ReEmitter";
@ -223,6 +224,20 @@ type MakeEventProps = MakeEventPassThruProps & {
unsigned?: IUnsigned;
};
export const mkRoomCreateEvent = (userId: string, roomId: string): MatrixEvent => {
return mkEvent({
event: true,
type: EventType.RoomCreate,
content: {
creator: userId,
room_version: KNOWN_SAFE_ROOM_VERSION,
},
skey: "",
user: userId,
room: roomId,
});
};
/**
* Create an Event.
* @param {Object} opts Values for the event.
@ -567,6 +582,19 @@ export const mkSpace = (
return space;
};
export const mkRoomMemberJoinEvent = (user: string, room: string): MatrixEvent => {
return mkEvent({
event: true,
type: EventType.RoomMember,
content: {
membership: "join",
},
skey: user,
user,
room,
});
};
export const mkPusher = (extra: Partial<IPusher> = {}): IPusher => ({
app_display_name: "app",
app_id: "123",

View file

@ -19,6 +19,7 @@ import { MatrixClient } from "matrix-js-sdk/src/matrix";
import { MatrixClientPeg as peg } from '../../src/MatrixClientPeg';
import MatrixClientContext from "../../src/contexts/MatrixClientContext";
import { SDKContext, SdkContextClass } from "../../src/contexts/SDKContext";
type WrapperProps<T> = { wrappedRef?: RefCallback<ComponentType<T>> } & T;
@ -39,3 +40,16 @@ export function wrapInMatrixClientContext<T>(WrappedComponent: ComponentType<T>)
}
return Wrapper;
}
export function wrapInSdkContext<T>(
WrappedComponent: ComponentType<T>,
sdkContext: SdkContextClass,
): ComponentType<WrapperProps<T>> {
return class extends React.Component<WrapperProps<T>> {
render() {
return <SDKContext.Provider value={sdkContext}>
<WrappedComponent {...this.props} />
</SDKContext.Provider>;
}
};
}

View file

@ -41,6 +41,7 @@ jest.mock('../../src/Modal', () => ({
jest.mock('../../src/settings/SettingsStore', () => ({
getValue: jest.fn(),
monitorSetting: jest.fn(),
}));
const mockPromptBeforeInviteUnknownUsers = (value: boolean) => {

View file

@ -0,0 +1,77 @@
/*
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.
*/
import { MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import {
startNewVoiceBroadcastRecording,
VoiceBroadcastPreRecording,
VoiceBroadcastRecordingsStore,
} from "../../../src/voice-broadcast";
import { stubClient } from "../../test-utils";
jest.mock("../../../src/voice-broadcast/utils/startNewVoiceBroadcastRecording");
describe("VoiceBroadcastPreRecording", () => {
const roomId = "!room:example.com";
let client: MatrixClient;
let room: Room;
let sender: RoomMember;
let recordingsStore: VoiceBroadcastRecordingsStore;
let preRecording: VoiceBroadcastPreRecording;
let onDismiss: (voiceBroadcastPreRecording: VoiceBroadcastPreRecording) => void;
beforeAll(() => {
client = stubClient();
room = new Room(roomId, client, client.getUserId() || "");
sender = new RoomMember(roomId, client.getUserId() || "");
recordingsStore = new VoiceBroadcastRecordingsStore();
});
beforeEach(() => {
onDismiss = jest.fn();
preRecording = new VoiceBroadcastPreRecording(room, sender, client, recordingsStore);
preRecording.on("dismiss", onDismiss);
});
describe("start", () => {
beforeEach(() => {
preRecording.start();
});
it("should start a new voice broadcast recording", () => {
expect(startNewVoiceBroadcastRecording).toHaveBeenCalledWith(
room,
client,
recordingsStore,
);
});
it("should emit a dismiss event", () => {
expect(onDismiss).toHaveBeenCalledWith(preRecording);
});
});
describe("cancel", () => {
beforeEach(() => {
preRecording.cancel();
});
it("should emit a dismiss event", () => {
expect(onDismiss).toHaveBeenCalledWith(preRecording);
});
});
});

View file

@ -0,0 +1,137 @@
/*
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.
*/
import { mocked } from "jest-mock";
import { MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import {
VoiceBroadcastPreRecording,
VoiceBroadcastPreRecordingStore,
VoiceBroadcastRecordingsStore,
} from "../../../src/voice-broadcast";
import { stubClient } from "../../test-utils";
jest.mock("../../../src/voice-broadcast/stores/VoiceBroadcastRecordingsStore");
describe("VoiceBroadcastPreRecordingStore", () => {
const roomId = "!room:example.com";
let client: MatrixClient;
let room: Room;
let sender: RoomMember;
let recordingsStore: VoiceBroadcastRecordingsStore;
let store: VoiceBroadcastPreRecordingStore;
let preRecording1: VoiceBroadcastPreRecording;
beforeAll(() => {
client = stubClient();
room = new Room(roomId, client, client.getUserId() || "");
sender = new RoomMember(roomId, client.getUserId() || "");
recordingsStore = new VoiceBroadcastRecordingsStore();
});
beforeEach(() => {
store = new VoiceBroadcastPreRecordingStore();
jest.spyOn(store, "emit");
jest.spyOn(store, "removeAllListeners");
preRecording1 = new VoiceBroadcastPreRecording(room, sender, client, recordingsStore);
jest.spyOn(preRecording1, "off");
});
it("getCurrent() should return null", () => {
expect(store.getCurrent()).toBeNull();
});
it("clearCurrent() should work", () => {
store.clearCurrent();
expect(store.getCurrent()).toBeNull();
});
describe("when setting a current recording", () => {
beforeEach(() => {
store.setCurrent(preRecording1);
});
it("getCurrent() should return the recording", () => {
expect(store.getCurrent()).toBe(preRecording1);
});
it("should emit a changed event with the recording", () => {
expect(store.emit).toHaveBeenCalledWith("changed", preRecording1);
});
describe("and calling destroy()", () => {
beforeEach(() => {
store.destroy();
});
it("should remove all listeners", () => {
expect(store.removeAllListeners).toHaveBeenCalled();
});
it("should deregister from the pre-recordings", () => {
expect(preRecording1.off).toHaveBeenCalledWith("dismiss", expect.any(Function));
});
});
describe("and cancelling the pre-recording", () => {
beforeEach(() => {
preRecording1.cancel();
});
it("should clear the current recording", () => {
expect(store.getCurrent()).toBeNull();
});
it("should emit a changed event with null", () => {
expect(store.emit).toHaveBeenCalledWith("changed", null);
});
});
describe("and setting the same pre-recording again", () => {
beforeEach(() => {
mocked(store.emit).mockClear();
store.setCurrent(preRecording1);
});
it("should not emit a changed event", () => {
expect(store.emit).not.toHaveBeenCalled();
});
});
describe("and setting another pre-recording", () => {
let preRecording2: VoiceBroadcastPreRecording;
beforeEach(() => {
mocked(store.emit).mockClear();
mocked(preRecording1.off).mockClear();
preRecording2 = new VoiceBroadcastPreRecording(room, sender, client, recordingsStore);
store.setCurrent(preRecording2);
});
it("should deregister from the current pre-recording", () => {
expect(preRecording1.off).toHaveBeenCalledWith("dismiss", expect.any(Function));
});
it("getCurrent() should return the new recording", () => {
expect(store.getCurrent()).toBe(preRecording2);
});
it("should emit a changed event with the new recording", () => {
expect(store.emit).toHaveBeenCalledWith("changed", preRecording2);
});
});
});
});

View file

@ -0,0 +1,102 @@
/*
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.
*/
import { mocked } from "jest-mock";
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import {
checkVoiceBroadcastPreConditions,
VoiceBroadcastPreRecording,
VoiceBroadcastPreRecordingStore,
VoiceBroadcastRecordingsStore,
} from "../../../src/voice-broadcast";
import { setUpVoiceBroadcastPreRecording } from "../../../src/voice-broadcast/utils/setUpVoiceBroadcastPreRecording";
import { mkRoomMemberJoinEvent, stubClient } from "../../test-utils";
jest.mock("../../../src/voice-broadcast/utils/checkVoiceBroadcastPreConditions");
describe("setUpVoiceBroadcastPreRecording", () => {
const roomId = "!room:example.com";
let client: MatrixClient;
let userId: string;
let room: Room;
let preRecordingStore: VoiceBroadcastPreRecordingStore;
let recordingsStore: VoiceBroadcastRecordingsStore;
const itShouldReturnNull = () => {
it("should return null", () => {
expect(setUpVoiceBroadcastPreRecording(room, client, recordingsStore, preRecordingStore)).toBeNull();
expect(checkVoiceBroadcastPreConditions).toHaveBeenCalledWith(room, client, recordingsStore);
});
};
beforeEach(() => {
client = stubClient();
const clientUserId = client.getUserId();
if (!clientUserId) fail("empty userId");
userId = clientUserId;
room = new Room(roomId, client, userId);
preRecordingStore = new VoiceBroadcastPreRecordingStore();
recordingsStore = new VoiceBroadcastRecordingsStore();
});
describe("when the preconditions fail", () => {
beforeEach(() => {
mocked(checkVoiceBroadcastPreConditions).mockReturnValue(false);
});
itShouldReturnNull();
});
describe("when the preconditions pass", () => {
beforeEach(() => {
mocked(checkVoiceBroadcastPreConditions).mockReturnValue(true);
});
describe("and there is no user id", () => {
beforeEach(() => {
mocked(client.getUserId).mockReturnValue(null);
});
itShouldReturnNull();
});
describe("and there is no room member", () => {
beforeEach(() => {
// check test precondition
expect(room.getMember(userId)).toBeNull();
});
itShouldReturnNull();
});
describe("and there is a room member", () => {
beforeEach(() => {
room.currentState.setStateEvents([
mkRoomMemberJoinEvent(userId, roomId),
]);
});
it("should create a voice broadcast pre-recording", () => {
const result = setUpVoiceBroadcastPreRecording(room, client, recordingsStore, preRecordingStore);
expect(checkVoiceBroadcastPreConditions).toHaveBeenCalledWith(room, client, recordingsStore);
expect(result).toBeInstanceOf(VoiceBroadcastPreRecording);
});
});
});
});