Skip to content

Commit

Permalink
Don't expose calls on GroupCall (#2941)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBrandner authored Dec 5, 2022
1 parent 4a4d493 commit 2c8eece
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 107 deletions.
51 changes: 50 additions & 1 deletion spec/test-utils/webrtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import {
MatrixClient,
MatrixEvent,
Room,
RoomMember,
RoomState,
RoomStateEvent,
RoomStateEventHandlerMap,
} from "../../src";
import { TypedEventEmitter } from "../../src/models/typed-event-emitter";
import { ReEmitter } from "../../src/ReEmitter";
import { SyncState } from "../../src/sync";
import { CallEvent, CallEventHandlerMap, MatrixCall } from "../../src/webrtc/call";
import { CallEvent, CallEventHandlerMap, CallState, MatrixCall } from "../../src/webrtc/call";
import { CallEventHandlerEvent, CallEventHandlerEventHandlerMap } from "../../src/webrtc/callEventHandler";
import { CallFeed } from "../../src/webrtc/callFeed";
import { GroupCallEventHandlerMap } from "../../src/webrtc/groupCall";
Expand Down Expand Up @@ -83,6 +84,17 @@ export const DUMMY_SDP = (
export const USERMEDIA_STREAM_ID = "mock_stream_from_media_handler";
export const SCREENSHARE_STREAM_ID = "mock_screen_stream_from_media_handler";

export const FAKE_ROOM_ID = "!fake:test.dummy";
export const FAKE_CONF_ID = "fakegroupcallid";

export const FAKE_USER_ID_1 = "@alice:test.dummy";
export const FAKE_DEVICE_ID_1 = "@AAAAAA";
export const FAKE_SESSION_ID_1 = "alice1";
export const FAKE_USER_ID_2 = "@bob:test.dummy";
export const FAKE_DEVICE_ID_2 = "@BBBBBB";
export const FAKE_SESSION_ID_2 = "bob1";
export const FAKE_USER_ID_3 = "@charlie:test.dummy";

class MockMediaStreamAudioSourceNode {
public connect() {}
}
Expand Down Expand Up @@ -431,6 +443,43 @@ export class MockCallMatrixClient extends TypedEventEmitter<EmittedEvents, Emitt
}
}

export class MockMatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap> {
constructor(public roomId: string, public groupCallId?: string) {
super();
}

public state = CallState.Ringing;
public opponentUserId = FAKE_USER_ID_1;
public opponentDeviceId = FAKE_DEVICE_ID_1;
public opponentMember = { userId: this.opponentUserId };
public callId = "1";
public localUsermediaFeed = {
setAudioVideoMuted: jest.fn<void, [boolean, boolean]>(),
stream: new MockMediaStream("stream"),
};
public remoteUsermediaFeed?: CallFeed;
public remoteScreensharingFeed?: CallFeed;

public reject = jest.fn<void, []>();
public answerWithCallFeeds = jest.fn<void, [CallFeed[]]>();
public hangup = jest.fn<void, []>();

public sendMetadataUpdate = jest.fn<void, []>();

public on = jest.fn();
public removeListener = jest.fn();

public getOpponentMember(): Partial<RoomMember> {
return this.opponentMember;
}

public getOpponentDeviceId(): string | undefined {
return this.opponentDeviceId;
}

public typed(): MatrixCall { return this as unknown as MatrixCall; }
}

export class MockCallFeed {
constructor(
public userId: string,
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/webrtc/call.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ describe('Call', function() {
it("ends call on onHangupReceived() if state is ringing", async () => {
expect(call.callHasEnded()).toBe(false);

call.state = CallState.Ringing;
(call as any).state = CallState.Ringing;
call.onHangupReceived({} as MCallHangupReject);

expect(call.callHasEnded()).toBe(true);
Expand Down Expand Up @@ -1424,7 +1424,7 @@ describe('Call', function() {
)("ends call on onRejectReceived() if in correct state (state=%s)", async (state: CallState) => {
expect(call.callHasEnded()).toBe(false);

call.state = state;
(call as any).state = state;
call.onRejectReceived({} as MCallHangupReject);

expect(call.callHasEnded()).toBe(
Expand Down
55 changes: 38 additions & 17 deletions spec/unit/webrtc/callFeed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,37 @@ limitations under the License.
import { SDPStreamMetadataPurpose } from "../../../src/webrtc/callEventTypes";
import { CallFeed } from "../../../src/webrtc/callFeed";
import { TestClient } from "../../TestClient";
import { MockMediaStream, MockMediaStreamTrack } from "../../test-utils/webrtc";
import { MockMatrixCall, MockMediaStream, MockMediaStreamTrack } from "../../test-utils/webrtc";
import { CallEvent, CallState } from "../../../src/webrtc/call";

describe("CallFeed", () => {
let client;
const roomId = "room1";
let client: TestClient;
let call: MockMatrixCall;
let feed: CallFeed;

beforeEach(() => {
client = new TestClient("@alice:foo", "somedevice", "token", undefined, {});
call = new MockMatrixCall(roomId);

feed = new CallFeed({
client: client.client,
call: call.typed(),
roomId,
userId: "user1",
// @ts-ignore Mock
stream: new MockMediaStream("stream1"),
purpose: SDPStreamMetadataPurpose.Usermedia,
audioMuted: false,
videoMuted: false,
});
});

afterEach(() => {
client.stop();
});

describe("muting", () => {
let feed: CallFeed;

beforeEach(() => {
feed = new CallFeed({
client,
roomId: "room1",
userId: "user1",
// @ts-ignore Mock
stream: new MockMediaStream("stream1"),
purpose: SDPStreamMetadataPurpose.Usermedia,
audioMuted: false,
videoMuted: false,
});
});

describe("muting by default", () => {
it("should mute audio by default", () => {
expect(feed.isAudioMuted()).toBeTruthy();
Expand Down Expand Up @@ -86,4 +88,23 @@ describe("CallFeed", () => {
});
});
});

describe("connected", () => {
it.each([true, false])("should always be connected, if isLocal()", (val: boolean) => {
// @ts-ignore
feed._connected = val;
jest.spyOn(feed, "isLocal").mockReturnValue(true);

expect(feed.connected).toBeTruthy();
});

it.each([
[CallState.Connected, true],
[CallState.Connecting, false],
])("should react to call state, when !isLocal()", (state: CallState, expected: Boolean) => {
call.emit(CallEvent.State, state);

expect(feed.connected).toBe(expected);
});
});
});
Loading

0 comments on commit 2c8eece

Please sign in to comment.