Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reenable Callcontroller tests #529

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Sources/StreamVideo/Controllers/CallController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import StreamWebRTC
/// Class that handles a particular call.
class CallController: @unchecked Sendable {

private lazy var webRTCCoordinator = WebRTCCoordinator(
private lazy var webRTCCoordinator = webRTCCoordinatorFactory.buildCoordinator(
user: user,
apiKey: apiKey,
callCid: callCid(from: callId, callType: callType),
Expand Down Expand Up @@ -40,6 +40,7 @@ class CallController: @unchecked Sendable {
private let defaultAPI: DefaultAPI
private let videoConfig: VideoConfig
private let sfuReconnectionTime: CGFloat
private let webRTCCoordinatorFactory: WebRTCCoordinatorProviding
private var reconnectionDate: Date?
private var cachedLocation: String?
private var currentSFU: String?
Expand All @@ -61,7 +62,8 @@ class CallController: @unchecked Sendable {
callType: String,
apiKey: String,
videoConfig: VideoConfig,
cachedLocation: String?
cachedLocation: String?,
webRTCCoordinatorFactory: WebRTCCoordinatorProviding = WebRTCCoordinatorFactory()
) {
self.user = user
self.callId = callId
Expand All @@ -71,6 +73,7 @@ class CallController: @unchecked Sendable {
sfuReconnectionTime = 30
self.defaultAPI = defaultAPI
self.cachedLocation = cachedLocation
self.webRTCCoordinatorFactory = webRTCCoordinatorFactory

_ = webRTCCoordinator

Expand Down Expand Up @@ -388,9 +391,7 @@ class CallController: @unchecked Sendable {
func cleanUp() {
guard call != nil else { return }
call = nil
Task {
await webRTCCoordinator.cleanUp()
}
Task { await webRTCCoordinator.cleanUp() }
}

/// Collects user feedback asynchronously.
Expand Down
63 changes: 63 additions & 0 deletions Sources/StreamVideo/WebRTC/v2/WebRTCCoordinatorProviding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Copyright © 2024 Stream.io Inc. All rights reserved.
//

import Foundation

/// A protocol that defines a factory for creating instances of `WebRTCCoordinator`.
/// The factory requires user details, API key, call identifier, video
/// configuration, and an authentication handler.
protocol WebRTCCoordinatorProviding {

/// Builds a `WebRTCCoordinator` instance.
///
/// - Parameters:
/// - user: The user participating in the WebRTC session.
/// - apiKey: The API key for authenticating WebRTC calls.
/// - callCid: The call identifier (callCid) for the session.
/// - videoConfig: The video configuration settings for the session.
/// - callAuthentication: A closure to handle authentication when
/// joining the call.
/// - Returns: An instance of `WebRTCCoordinator`.
func buildCoordinator(
user: User,
apiKey: String,
callCid: String,
videoConfig: VideoConfig,
callAuthentication: @escaping WebRTCCoordinator.AuthenticationHandler
) -> WebRTCCoordinator
}

/// A concrete implementation of the `WebRTCCoordinatorProviding` protocol.
/// The `WebRTCCoordinatorFactory` provides an implementation of the
/// `buildCoordinator` method that creates and returns a `WebRTCCoordinator`.
struct WebRTCCoordinatorFactory: WebRTCCoordinatorProviding {

/// Builds and returns a `WebRTCCoordinator` using the provided parameters.
///
/// - Parameters:
/// - user: The user participating in the WebRTC session.
/// - apiKey: The API key for authenticating WebRTC calls.
/// - callCid: The call identifier (callCid) for the session.
/// - videoConfig: The video configuration settings for the session.
/// - callAuthentication: A closure to handle authentication when
/// joining the call.
/// - Returns: A newly initialized `WebRTCCoordinator` instance.
/// - Note: Uses the ``StreamRTCPeerConnectionCoordinatorFactory`` for the provided
/// `RTCPeerConnectionCoordinatorProviding`.
func buildCoordinator(
user: User,
apiKey: String,
callCid: String,
videoConfig: VideoConfig,
callAuthentication: @escaping WebRTCCoordinator.AuthenticationHandler
) -> WebRTCCoordinator {
.init(
user: user,
apiKey: apiKey,
callCid: callCid,
videoConfig: videoConfig,
callAuthentication: callAuthentication
)
}
}
8 changes: 8 additions & 0 deletions StreamVideo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@
4046DEE92A9E381F00CA6D2F /* AppIntentVocabulary.plist in Resources */ = {isa = PBXBuildFile; fileRef = 4046DEE82A9E381F00CA6D2F /* AppIntentVocabulary.plist */; };
4046DEF02A9F469100CA6D2F /* GDPerformanceView-Swift in Frameworks */ = {isa = PBXBuildFile; productRef = 4046DEEF2A9F469100CA6D2F /* GDPerformanceView-Swift */; settings = {ATTRIBUTES = (Required, ); }; };
4046DEF22A9F510C00CA6D2F /* DebugMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4046DEF12A9F510C00CA6D2F /* DebugMenu.swift */; };
40483CB82C9B1DEE00B4FCA8 /* WebRTCCoordinatorProviding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40483CB72C9B1DEE00B4FCA8 /* WebRTCCoordinatorProviding.swift */; };
40483CBA2C9B1E6600B4FCA8 /* MockWebRTCCoordinatorFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40483CB92C9B1E6000B4FCA8 /* MockWebRTCCoordinatorFactory.swift */; };
4049CE822BBBF74C003D07D2 /* LegacyAsyncImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4049CE812BBBF74C003D07D2 /* LegacyAsyncImage.swift */; };
4049CE842BBBF8EF003D07D2 /* StreamAsyncImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4049CE832BBBF8EF003D07D2 /* StreamAsyncImage.swift */; };
404A5CFB2AD5648100EF1C62 /* DemoChatModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 404A5CFA2AD5648100EF1C62 /* DemoChatModifier.swift */; };
Expand Down Expand Up @@ -1472,6 +1474,8 @@
4046DEE82A9E381F00CA6D2F /* AppIntentVocabulary.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = AppIntentVocabulary.plist; sourceTree = "<group>"; };
4046DEEA2A9E38DC00CA6D2F /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4046DEF12A9F510C00CA6D2F /* DebugMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugMenu.swift; sourceTree = "<group>"; };
40483CB72C9B1DEE00B4FCA8 /* WebRTCCoordinatorProviding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebRTCCoordinatorProviding.swift; sourceTree = "<group>"; };
40483CB92C9B1E6000B4FCA8 /* MockWebRTCCoordinatorFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockWebRTCCoordinatorFactory.swift; sourceTree = "<group>"; };
4049CE812BBBF74C003D07D2 /* LegacyAsyncImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LegacyAsyncImage.swift; sourceTree = "<group>"; };
4049CE832BBBF8EF003D07D2 /* StreamAsyncImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StreamAsyncImage.swift; sourceTree = "<group>"; };
404A5CFA2AD5648100EF1C62 /* DemoChatModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoChatModifier.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3527,6 +3531,7 @@
40BBC4C12C637377002AEF92 /* v2 */ = {
isa = PBXGroup;
children = (
40483CB72C9B1DEE00B4FCA8 /* WebRTCCoordinatorProviding.swift */,
40BBC4C52C638915002AEF92 /* WebRTCCoordinator.swift */,
40BBC4C22C6373C4002AEF92 /* WebRTCStateAdapter.swift */,
406B3C422C91E41000FC93A1 /* WebRTCAuthenticator.swift */,
Expand Down Expand Up @@ -4545,6 +4550,7 @@
8492B87629081CE700006649 /* Mock */ = {
isa = PBXGroup;
children = (
40483CB92C9B1E6000B4FCA8 /* MockWebRTCCoordinatorFactory.swift */,
40AF6A3A2C93469000BA2935 /* MockWebSocketClientFactory.swift */,
406B3C5C2C92E37500FC93A1 /* MockInternetConnection.swift */,
406B3C502C91F8C000FC93A1 /* MockWebRTCAuthenticator.swift */,
Expand Down Expand Up @@ -6253,6 +6259,7 @@
84DC38CB29ADFCFD00946713 /* SortParamRequest.swift in Sources */,
8490032529D308A000AD9BB4 /* GetCallResponse.swift in Sources */,
841947982886D9CD0007B36E /* BundleExtensions.swift in Sources */,
40483CB82C9B1DEE00B4FCA8 /* WebRTCCoordinatorProviding.swift in Sources */,
842E70D32B91BE1700D2D68B /* CallRecordingReadyEvent.swift in Sources */,
408CE0F32BD905920052EC3A /* Models+Sendable.swift in Sources */,
842E70D42B91BE1700D2D68B /* CallTranscription.swift in Sources */,
Expand Down Expand Up @@ -6496,6 +6503,7 @@
40F017572BBEF07B00E89FD1 /* GeofenceSettings+Dummy.swift in Sources */,
40C9E4532C9888C100802B28 /* WebRTCMigrationStatusObserver_Tests.swift in Sources */,
40DE867D2BBEAA8600E88D8A /* CallKitPushNotificationAdapterTests.swift in Sources */,
40483CBA2C9B1E6600B4FCA8 /* MockWebRTCCoordinatorFactory.swift in Sources */,
845E31062A7121D6004DC470 /* BroadcastObserver_Tests.swift in Sources */,
40F017392BBEAF6400E89FD1 /* MockCallKitService.swift in Sources */,
403FB1602BFE22840047A696 /* StreamCallStateMachineStageRejectingStage_Tests.swift in Sources */,
Expand Down
35 changes: 35 additions & 0 deletions StreamVideoTests/Call/Call_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,41 @@ final class Call_Tests: StreamVideoTestCase {
XCTAssertTrue(Int(duration) >= 1)
}

// MARK: - Update State from Coordinator events

func test_coordinatorEventReceived_startedRecording_updatesStateCorrectly() async throws {
try await assertCoordinatorEventReceived(
.typeCallRecordingStartedEvent(
CallRecordingStartedEvent(callCid: callCid, createdAt: Date())
)
) { call in await fulfillment { call.state.recordingState == .recording } }
}

func test_coordinatorEventReceived_startedRecordingForAnotherCall_doesNotUpdateState() async throws {
try await assertCoordinatorEventReceived(
.typeCallRecordingStartedEvent(
CallRecordingStartedEvent(callCid: .unique, createdAt: Date())
)
) { @MainActor call in
await wait(for: 1)
XCTAssertEqual(call.state.recordingState, .noRecording)
}
}

private func assertCoordinatorEventReceived(
_ event: VideoEvent,
fulfillmentHandler: @MainActor(Call) async throws -> Void
) async throws {
let streamVideo = try XCTUnwrap(streamVideo)
let call = streamVideo.call(callType: callType, callId: callId)

streamVideo
.eventNotificationCenter
.process(.coordinatorEvent(event))

try await fulfillmentHandler(call)
}

// MARK: - join

func test_join_callControllerWasCalledOnlyOnce() async throws {
Expand Down
Loading
Loading