From f8d61985ab21efb267a0f56b7d387414d641c512 Mon Sep 17 00:00:00 2001 From: Ceyhun Ozgoc Date: Wed, 18 Nov 2026 09:47:00 +0000 Subject: [PATCH 01/11] Conviva: allow injecting CISAnalytics into ConvivaEndpoints for tests Co-Authored-By: Ceyhun Ozgoc Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- Code/Conviva/Source/Base/ConvivaEndpoints.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Code/Conviva/Source/Base/ConvivaEndpoints.swift b/Code/Conviva/Source/Base/ConvivaEndpoints.swift index f8e9cc80..b2c22de0 100644 --- a/Code/Conviva/Source/Base/ConvivaEndpoints.swift +++ b/Code/Conviva/Source/Base/ConvivaEndpoints.swift @@ -10,12 +10,16 @@ class ConvivaEndpoints { let videoAnalytics: CISVideoAnalytics let adAnalytics: CISAdAnalytics - init?(configuration: ConvivaConfiguration) { + convenience init?(configuration: ConvivaConfiguration) { guard let analytics = CISAnalyticsCreator.create(withCustomerKey: configuration.customerKey, settings: configuration.convivaSettingsDictionary) else { return nil } - + self.init(analytics: analytics) + } + + /// Lets tests inject a recording `CISAnalytics` instead of the Conviva SDK one. + init(analytics: CISAnalytics) { self.analytics = analytics self.videoAnalytics = self.analytics.createVideoAnalytics() self.adAnalytics = self.analytics.createAdAnalytics(withVideoAnalytics: videoAnalytics) From f2161a88b1ea78fb4fc0870dbc15a5e0b56f2ec3 Mon Sep 17 00:00:00 2001 From: Ceyhun Ozgoc Date: Wed, 18 Nov 2026 09:51:10 +0000 Subject: [PATCH 02/11] Conviva: add THEOplayerConnectorConvivaTests target with recording analytics and run it on CI Co-Authored-By: Ceyhun Ozgoc Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .github/workflows/build-and-test.yml | 1 + .../ConvivaTestSupport.swift | 103 ++++++++++++++++++ .../RecordingAnalytics.swift | 95 ++++++++++++++++ Package.swift | 9 ++ 4 files changed, 208 insertions(+) create mode 100644 Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift create mode 100644 Code/Conviva/Tests/THEOplayerConnectorConvivaTests/RecordingAnalytics.swift diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 3f445ae0..826ede1f 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -26,6 +26,7 @@ jobs: THEOplayerConnectorUplynk # Test schemes to run TEST_SCHEMES: >- + THEOplayerConnectorConvivaTests THEOplayerConnectorSideloadedSubtitleTests THEOplayerConnectorUplynkTests diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift new file mode 100644 index 00000000..883691c4 --- /dev/null +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift @@ -0,0 +1,103 @@ +// +// ConvivaTestSupport.swift +// + +import XCTest +import THEOplayerSDK +@testable import THEOplayerConnectorConviva + +enum TestSources { + // The default (unlicensed) player only plays sources hosted on theoplayer.com. + static let bigBuckBunny = "https://cdn.theoplayer.com/video/big_buck_bunny/big_buck_bunny.m3u8" + static let elephantsDream = "https://cdn.theoplayer.com/video/elephants-dream/playlist.m3u8" + + static func hls(_ url: String = bigBuckBunny) -> SourceDescription { + SourceDescription(source: TypedSource(src: url, type: "application/x-mpegurl")) + } +} + +/// A real player with a `ConvivaConnector` that reports to a `RecordingAnalytics`. +final class ConvivaHarness { + let player = THEOplayer(with: nil) + let analytics = RecordingAnalytics() + let connector: ConvivaConnector + var video: RecordingVideoAnalytics { analytics.video } + var ad: RecordingAdAnalytics { analytics.ad } + + init(convivaMetadata: [String: Any] = [:]) { + connector = ConvivaConnector(conviva: ConvivaEndpoints(analytics: analytics), player: player, convivaMetadata: convivaMetadata) + } + + /// Registers a custom server-side ad integration so tests can drive real ad break and ad events. + func registerCustomAdIntegration() -> ServerSideAdIntegrationController { + var controller: ServerSideAdIntegrationController! + player.ads.registerServerSideIntegration(integrationId: "conviva-tests") { + controller = $0 + return NoopAdIntegrationHandler() + } + return controller + } +} + +struct NoopAdIntegrationHandler: ServerSideAdIntegrationHandler {} + +struct MockAdBreak: AdBreak { + var id: String? + var ads: [Ad] = [] + var maxDuration: Int = 0 + var maxRemainingDuration: Double = 0 + var timeOffset: Int = 0 + var integration: AdIntegrationKind = .custom + var customIntegration: String? +} + +extension XCTestCase { + func makeHarness(convivaMetadata: [String: Any] = [:]) -> ConvivaHarness { + let harness = ConvivaHarness(convivaMetadata: convivaMetadata) + addTeardownBlock { + harness.connector.destroy() + harness.player.destroy() + } + return harness + } + + /// Waits for the next `type` event, triggered by `trigger`, and lets the main queue drain afterwards. + @discardableResult + func waitForEvent( + _ type: EventType, + on dispatcher: EventDispatcherProtocol, + timeout: TimeInterval = 30, + file: StaticString = #filePath, + line: UInt = #line, + after trigger: () -> Void = {} + ) -> E? { + let eventReceived = expectation(description: "\(type.name) event") + var received: E? + let listener = dispatcher.addEventListener(type: type) { event in + guard received == nil else { return } + received = event + eventReceived.fulfill() + } + trigger() + wait(for: [eventReceived], timeout: timeout) + dispatcher.removeEventListener(type: type, listener: listener) + drainMainQueue() + if received == nil { + XCTFail("No \(type.name) event within \(timeout)s", file: file, line: line) + } + return received + } + + func waitUntil(_ description: String, timeout: TimeInterval = 30, _ condition: @escaping () -> Bool) { + let conditionMet = XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in condition() }, object: nil) + conditionMet.expectationDescription = description + wait(for: [conditionMet], timeout: timeout) + } + + /// The connector forwards ad events through `DispatchQueue.main.async`; this lets those run. + func drainMainQueue() { + let drained = expectation(description: "main queue drained") + DispatchQueue.main.async { DispatchQueue.main.async { drained.fulfill() } } + wait(for: [drained], timeout: 5) + } +} diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/RecordingAnalytics.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/RecordingAnalytics.swift new file mode 100644 index 00000000..967577cc --- /dev/null +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/RecordingAnalytics.swift @@ -0,0 +1,95 @@ +// +// RecordingAnalytics.swift +// + +import ConvivaSDK + +/// Records the calls the connector makes on the Conviva SDK so tests can assert on what is reported. +class RecordingStreamAnalytics: NSObject, CISStreamAnalyticsProtocol { + private(set) var metrics: [(key: String, value: Any?)] = [] + private(set) var cleanupCount = 0 + + func metricValues(_ key: String) -> [Any?] { + metrics.filter { $0.key == key }.map(\.value) + } + + var playerStates: [PlayerState] { + metricValues(CIS_SSDK_PLAYBACK_METRIC_PLAYER_STATE).compactMap { ($0 as? UInt).flatMap(PlayerState.init(rawValue:)) } + } + + func reportPlaybackMetric(_ key: String, value: Any?) { metrics.append((key, value)) } + func cleanup() { cleanupCount += 1 } + func reportPlayerState(_ playerState: PlayerState) {} + func getSessionId() -> Int32 { 42 } + func getSessionKey() -> Int32 { 42 } + func getMetadataInfo() -> [AnyHashable: Any] { [:] } + func setUpdateHandler(_ updateHandler: @escaping UpdateHandler) {} + func isAdAnalytics() -> Bool { false } + func isVideoAnalytics() -> Bool { false } + func isAirPlaying() -> Bool { false } +} + +final class RecordingVideoAnalytics: RecordingStreamAnalytics, CISVideoAnalyticsProtocol { + private(set) var contentInfos: [[AnyHashable: Any]] = [] + private(set) var playbackRequested: [[AnyHashable: Any]?] = [] + private(set) var playbackEndedCount = 0 + private(set) var playbackFailed: [String] = [] + private(set) var playbackErrors: [(message: String, severity: ErrorSeverity)] = [] + private(set) var adBreaksStarted: [(adType: AdTechnology, info: [AnyHashable: Any])] = [] + private(set) var adBreakEndedCount = 0 + + /// Content info values for `key`, in the order they were sent. + func contentInfoValues(_ key: String) -> [Any] { + contentInfos.compactMap { $0[key] } + } + + func setContentInfo(_ contentInfo: [AnyHashable: Any]) { contentInfos.append(contentInfo) } + func reportPlaybackRequested(_ contentInfo: [AnyHashable: Any]?) { playbackRequested.append(contentInfo) } + func reportPlaybackEnded() { playbackEndedCount += 1 } + func reportPlaybackFailed(_ errorMessage: String, contentInfo: [AnyHashable: Any]?) { playbackFailed.append(errorMessage) } + func reportPlaybackError(_ errorMessage: String, errorSeverity severity: ErrorSeverity) { playbackErrors.append((errorMessage, severity)) } + func reportAdBreakStarted(_ adPlayer: AdPlayer, adType: AdTechnology, adBreakInfo: [AnyHashable: Any]) { adBreaksStarted.append((adType, adBreakInfo)) } + func reportAdBreakEnded() { adBreakEndedCount += 1 } + func setPlayerInfo(_ playerInfo: [AnyHashable: Any]) {} + func reportPlaybackEvent(_ eventName: String, withAttributes attributes: [AnyHashable: Any]?) {} + func setPlayer(_ player: Any?) {} + func setAdAnalytics(_ adAnalytics: CISAdAnalytics) {} +} + +final class RecordingAdAnalytics: RecordingStreamAnalytics, CISAdAnalyticsProtocol { + private(set) var adInfos: [[AnyHashable: Any]] = [] + private(set) var adFailed: [(message: String, adInfo: [AnyHashable: Any]?)] = [] + + func setAdInfo(_ adInfo: [AnyHashable: Any]) { adInfos.append(adInfo) } + func reportAdFailed(_ errorMessage: String, adInfo: [AnyHashable: Any]?) { adFailed.append((errorMessage, adInfo)) } + func reportAdLoaded(_ adInfo: [AnyHashable: Any]?) {} + func reportAdStarted(_ adInfo: [AnyHashable: Any]?) {} + func reportAdEnded() {} + func setAdPlayerInfo(_ adPlayerInfo: [AnyHashable: Any]) {} + func reportAdError(_ errorMessage: String, severity: ErrorSeverity) {} + func reportAdSkipped() {} + func reportAdPlayerEvent(_ eventType: String, details: [AnyHashable: Any]?) {} + func reportAdMetric(_ key: String, value: Any) {} + func setContentSessionID(_ sessionID: Int32) {} + func setAdListener(_ adProxy: Any?, andInfo info: [AnyHashable: Any]) {} +} + +final class RecordingAnalytics: NSObject, CISAnalyticsProtocol { + let video = RecordingVideoAnalytics() + let ad = RecordingAdAnalytics() + + func createVideoAnalytics() -> CISVideoAnalytics { video } + func createVideoAnalytics(_ options: [AnyHashable: Any]) -> CISVideoAnalytics { video } + func createAdAnalytics() -> CISAdAnalytics { ad } + func createAdAnalytics(withVideoAnalytics videoAnalytics: CISVideoAnalytics) -> CISAdAnalytics { ad } + func getClientId() -> String { "test-client" } + func reportAppEvent(_ event: String, details: [AnyHashable: Any]) {} + func reportAppBackgrounded() {} + func reportAppForegrounded() {} + func setUserPrefsForDataCollection(_ userPrefs: [AnyHashable: Any]) {} + func setUserPrefsForDataDeletion(_ userPrefs: [AnyHashable: Any]) {} + func cleanup() {} + func getGlobalSessionId() -> Int32 { 0 } + func getipv4SessionId() -> Int32 { 0 } + func getipv6SessionId() -> Int32 { 0 } +} diff --git a/Package.swift b/Package.swift index 9500e289..3d04cd89 100644 --- a/Package.swift +++ b/Package.swift @@ -40,6 +40,15 @@ let package = Package( ], path: "Code/Conviva/Source" ), + .testTarget( + name: "THEOplayerConnectorConvivaTests", + dependencies: [ + "THEOplayerConnectorConviva", + "THEOplayerSDK", + "ConvivaSDK", + ], + path: "Code/Conviva/Tests/THEOplayerConnectorConvivaTests" + ), .target( name: "THEOplayerConnectorNielsen", From e564e4f3b380db9c4281e531c631881ae197eff3 Mon Sep 17 00:00:00 2001 From: Ceyhun Ozgoc Date: Wed, 18 Nov 2026 09:51:10 +0000 Subject: [PATCH 03/11] Conviva: test session lifecycle, source change, persistent metadata, errors and player state Co-Authored-By: Ceyhun Ozgoc Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../ErrorTests.swift | 41 +++++++ .../SessionLifecycleTests.swift | 106 ++++++++++++++++++ .../SourceChangeTests.swift | 54 +++++++++ 3 files changed, 201 insertions(+) create mode 100644 Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ErrorTests.swift create mode 100644 Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SessionLifecycleTests.swift create mode 100644 Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SourceChangeTests.swift diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ErrorTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ErrorTests.swift new file mode 100644 index 00000000..45897dd8 --- /dev/null +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ErrorTests.swift @@ -0,0 +1,41 @@ +// +// ErrorTests.swift +// + +import XCTest +import THEOplayerSDK +@testable import THEOplayerConnectorConviva + +final class ErrorTests: XCTestCase { + private let analytics = RecordingAnalytics() + private lazy var endpoints = ConvivaEndpoints(analytics: analytics) + private let storage = ConvivaStorage() + private lazy var handler = PlayerHandler(endpoints: endpoints, storage: storage, convivaMetadata: [:]) + + // C4 + func testFatalErrorFailsSessionOnce() { + handler.maybeReportPlaybackRequested() + + handler.error(event: ErrorEvent(error: "first", errorObject: nil)) + handler.error(event: ErrorEvent(error: "second", errorObject: nil)) + + XCTAssertEqual(analytics.video.playbackFailed, ["first"]) + } + + // C4 + func testFatalErrorResetsSession() { + handler.maybeReportPlaybackRequested() + handler.error(event: ErrorEvent(error: "fatal", errorObject: nil)) + + handler.maybeReportPlaybackRequested() + + XCTAssertEqual(analytics.video.playbackRequested.count, 2) + } + + // C4 + func testFatalErrorWithoutSessionIsNotReported() { + handler.error(event: ErrorEvent(error: "fatal", errorObject: nil)) + + XCTAssertTrue(analytics.video.playbackFailed.isEmpty) + } +} diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SessionLifecycleTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SessionLifecycleTests.swift new file mode 100644 index 00000000..fab80f73 --- /dev/null +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SessionLifecycleTests.swift @@ -0,0 +1,106 @@ +// +// SessionLifecycleTests.swift +// + +import XCTest +import THEOplayerSDK +import ConvivaSDK +@testable import THEOplayerConnectorConviva + +@MainActor +final class SessionLifecycleTests: XCTestCase { + // C1 + func testOnlyFirstPlayRequestsPlayback() { + let harness = makeHarness() + harness.player.source = TestSources.hls() + + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + waitForEvent(PlayerEventTypes.PAUSE, on: harness.player) { harness.player.pause() } + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + + XCTAssertEqual(harness.video.playbackRequested.count, 1) + XCTAssertEqual(harness.video.playbackEndedCount, 0) + } + + // C1 — needs real playback to reach the end of the stream. + func testEndedReportsStoppedAndEndsSession() { + let harness = makeHarness() + let player = harness.player + waitForEvent(PlayerEventTypes.DURATION_CHANGE, on: player) { player.source = TestSources.hls() } + guard let duration = player.duration, duration.isFinite else { return XCTFail("VOD source without finite duration") } + + player.currentTime = duration - 2 + waitForEvent(PlayerEventTypes.ENDED, on: player, timeout: 60) { player.play() } + + XCTAssertEqual(harness.video.playbackRequested.count, 1) + XCTAssertEqual(harness.video.playerStates.last, .CONVIVA_STOPPED) + XCTAssertEqual(harness.video.playbackEndedCount, 1) + } + + // C3 + func testInitialMetadataIsSentWithEverySession() { + let initialMetadata = ["customerTag": "TV Nova", CIS_SSDK_METADATA_VIEWER_ID: "viewer-1"] + let harness = makeHarness(convivaMetadata: initialMetadata) + harness.player.source = TestSources.hls() + + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + harness.connector.stopAndStartNewSession(contentInfo: [:]) + harness.connector.stopAndStartNewSession(contentInfo: [:]) + + XCTAssertEqual(harness.video.playbackEndedCount, 2) + XCTAssertEqual(harness.video.playbackRequested.count, 3) + for requested in harness.video.playbackRequested { + XCTAssertEqual(requested as? [String: String], initialMetadata) + } + } + + // C7 + func testPauseReportsPausedState() { + let harness = makeHarness() + harness.player.source = TestSources.hls() + + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + waitForEvent(PlayerEventTypes.PAUSE, on: harness.player) { harness.player.pause() } + + XCTAssertEqual(harness.video.playerStates.last, .CONVIVA_PAUSED) + } + + // C7 — needs real playback. + func testPlayingReportsPlayingState() { + let harness = makeHarness() + harness.player.source = TestSources.hls() + + waitForEvent(PlayerEventTypes.PLAYING, on: harness.player) { harness.player.play() } + + XCTAssertEqual(harness.video.playerStates.last, .CONVIVA_PLAYING) + } + + // C7 — needs real playback; seeking to an unbuffered position makes the player wait. + func testWaitingReportsBufferingState() { + let harness = makeHarness() + let player = harness.player + player.source = TestSources.hls() + waitForEvent(PlayerEventTypes.PLAYING, on: player) { player.play() } + + waitForEvent(PlayerEventTypes.WAITING, on: player) { player.currentTime = 300 } + + XCTAssertTrue(harness.video.playerStates.contains(.CONVIVA_BUFFERING)) + } + + // C7 + func testNewSessionWhilePausedStartsOnPlay() { + let harness = makeHarness() + harness.player.source = TestSources.hls() + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + waitForEvent(PlayerEventTypes.PAUSE, on: harness.player) { harness.player.pause() } + + harness.connector.stopAndStartNewSession(contentInfo: [:]) + + XCTAssertEqual(harness.video.playbackEndedCount, 1) + XCTAssertEqual(harness.video.playbackRequested.count, 1, "A new session must not start while paused") + + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + + XCTAssertEqual(harness.video.playbackRequested.count, 2) + } +} diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SourceChangeTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SourceChangeTests.swift new file mode 100644 index 00000000..de98040a --- /dev/null +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SourceChangeTests.swift @@ -0,0 +1,54 @@ +// +// SourceChangeTests.swift +// + +import XCTest +import THEOplayerSDK +@testable import THEOplayerConnectorConviva + +@MainActor +final class SourceChangeTests: XCTestCase { + // C2 + func testNewSourceEndsPreviousSession() { + let harness = makeHarness() + harness.player.source = TestSources.hls() + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + + waitForEvent(PlayerEventTypes.SOURCE_CHANGE, on: harness.player) { + harness.player.source = TestSources.hls(TestSources.elephantsDream) + } + + XCTAssertEqual(harness.video.playbackEndedCount, 1) + XCTAssertEqual(harness.video.cleanupCount, 1) + } + + // C2 + func testNewSourceClearsMetadataOfPreviousSession() { + let harness = makeHarness() + harness.player.source = TestSources.hls() + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + harness.connector.setContentInfo(["previousSourceOnly": "yes"]) + XCTAssertEqual(harness.video.contentInfoValues("previousSourceOnly").count, 1) + + harness.player.source = TestSources.hls(TestSources.elephantsDream) + let contentInfosBefore = harness.video.contentInfos.count + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + + let newSessionInfos = harness.video.contentInfos.dropFirst(contentInfosBefore) + XCTAssertFalse(newSessionInfos.isEmpty) + XCTAssertTrue(newSessionInfos.allSatisfy { $0["previousSourceOnly"] == nil }) + XCTAssertEqual(newSessionInfos.last?[CIS_SSDK_METADATA_STREAM_URL] as? String, TestSources.elephantsDream) + } + + // C2 + func testSettingSameSourceKeepsSession() { + let harness = makeHarness() + let source = TestSources.hls() + harness.player.source = source + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + + waitForEvent(PlayerEventTypes.SOURCE_CHANGE, on: harness.player) { harness.player.source = source } + + XCTAssertEqual(harness.video.playbackEndedCount, 0) + } +} From 245d3b296330246617fdd3974574249449e0c363 Mon Sep 17 00:00:00 2001 From: Ceyhun Ozgoc Date: Wed, 18 Nov 2026 09:51:10 +0000 Subject: [PATCH 04/11] Conviva: test live detection and encoding_type reporting Co-Authored-By: Ceyhun Ozgoc Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../EncodingTypeTests.swift | 32 ++++++++++++++++ .../LiveDetectionTests.swift | 38 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 Code/Conviva/Tests/THEOplayerConnectorConvivaTests/EncodingTypeTests.swift create mode 100644 Code/Conviva/Tests/THEOplayerConnectorConvivaTests/LiveDetectionTests.swift diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/EncodingTypeTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/EncodingTypeTests.swift new file mode 100644 index 00000000..4ed2693e --- /dev/null +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/EncodingTypeTests.swift @@ -0,0 +1,32 @@ +// +// EncodingTypeTests.swift +// + +import XCTest +import THEOplayerSDK +@testable import THEOplayerConnectorConviva + +@MainActor +final class EncodingTypeTests: XCTestCase { + // C6 + func testHlsEncodingTypeIsReportedAtSessionStart() { + let harness = makeHarness() + harness.player.source = TestSources.hls() + XCTAssertTrue(harness.video.contentInfoValues(ENCODING_TYPE).isEmpty) + + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + + XCTAssertEqual(harness.video.contentInfoValues(ENCODING_TYPE).first as? String, "HLS") + } + + // C6 + func testCustomerEncodingTypeWins() { + let harness = makeHarness() + waitForEvent(PlayerEventTypes.SOURCE_CHANGE, on: harness.player) { harness.player.source = TestSources.hls() } + harness.connector.setContentInfo([ENCODING_TYPE: "CUSTOM"]) + + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + + XCTAssertEqual(Set(harness.video.contentInfoValues(ENCODING_TYPE).compactMap { $0 as? String }), ["CUSTOM"]) + } +} diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/LiveDetectionTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/LiveDetectionTests.swift new file mode 100644 index 00000000..281578fc --- /dev/null +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/LiveDetectionTests.swift @@ -0,0 +1,38 @@ +// +// LiveDetectionTests.swift +// + +import XCTest +import THEOplayerSDK +import ConvivaSDK +@testable import THEOplayerConnectorConviva + +@MainActor +final class LiveDetectionTests: XCTestCase { + // C5 + func testFiniteDurationReportsVodWithDuration() { + let harness = makeHarness() + + let event = waitForEvent(PlayerEventTypes.DURATION_CHANGE, on: harness.player) { + harness.player.source = TestSources.hls() + } + + XCTAssertEqual(harness.video.contentInfoValues(CIS_SSDK_METADATA_IS_LIVE).last as? NSNumber, false) + XCTAssertEqual(harness.video.contentInfoValues(CIS_SSDK_METADATA_DURATION).last as? NSNumber, event?.duration.map { NSNumber(value: $0) }) + } + + // C5 + func testCustomerIsLiveIsNotOverwritten() { + let harness = makeHarness() + + waitForEvent(PlayerEventTypes.SOURCE_CHANGE, on: harness.player) { harness.player.source = TestSources.hls() } + waitForEvent(PlayerEventTypes.DURATION_CHANGE, on: harness.player) { + harness.connector.setContentInfo([CIS_SSDK_METADATA_IS_LIVE: true]) + } + + let reportedIsLive = harness.video.contentInfoValues(CIS_SSDK_METADATA_IS_LIVE).map { $0 as? Bool } + XCTAssertGreaterThan(reportedIsLive.count, 1, "The duration change must have pushed content info") + XCTAssertTrue(reportedIsLive.allSatisfy { $0 == true }) + XCTAssertTrue(harness.video.contentInfoValues(CIS_SSDK_METADATA_DURATION).isEmpty) + } +} From b9a89d8bf7041322a42aab3d760646accae44fc5 Mon Sep 17 00:00:00 2001 From: Ceyhun Ozgoc Date: Wed, 18 Nov 2026 09:51:10 +0000 Subject: [PATCH 05/11] Conviva: test bitrate routing to content and ad endpoints Co-Authored-By: Ceyhun Ozgoc Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../BitrateTests.swift | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Code/Conviva/Tests/THEOplayerConnectorConvivaTests/BitrateTests.swift diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/BitrateTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/BitrateTests.swift new file mode 100644 index 00000000..e3b01e9d --- /dev/null +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/BitrateTests.swift @@ -0,0 +1,71 @@ +// +// BitrateTests.swift +// + +import XCTest +import THEOplayerSDK +import ConvivaSDK +@testable import THEOplayerConnectorConviva + +/// These tests need real playback: the player only reports an active quality once it renders media. +@MainActor +final class BitrateTests: XCTestCase { + // C8 + func testActiveQualityIsReportedInKbpsToContentEndpoint() { + let harness = makeHarness() + let bandwidths = recordActiveQualityBandwidths(of: harness.player) + harness.player.source = TestSources.hls() + harness.player.play() + + waitUntil("content bitrate reported") { !harness.video.metricValues(CIS_SSDK_PLAYBACK_METRIC_BITRATE).isEmpty } + + let reported = harness.video.metricValues(CIS_SSDK_PLAYBACK_METRIC_BITRATE).compactMap { ($0 as? NSNumber)?.intValue } + XCTAssertEqual(reported.first, bandwidths.values.first.map { $0 / 1000 }) + XCTAssertTrue(harness.ad.metricValues(CIS_SSDK_PLAYBACK_METRIC_BITRATE).isEmpty) + } + + // C8 + func testActiveQualityDuringAdIsReportedToAdEndpoint() { + let harness = makeHarness() + let controller = harness.registerCustomAdIntegration() + harness.player.source = TestSources.hls() + let adBreak = controller.createAdBreak(params: AdBreakInit(timeOffset: 0, maxDuration: 30)) + controller.beginAd(ad: controller.createAd(params: AdInit(type: "linear", duration: 30), adBreak: adBreak)) + XCTAssertTrue(harness.player.ads.playing) + harness.player.play() + + waitUntil("ad bitrate reported") { !harness.ad.metricValues(CIS_SSDK_PLAYBACK_METRIC_BITRATE).isEmpty } + + XCTAssertTrue(harness.video.metricValues(CIS_SSDK_PLAYBACK_METRIC_BITRATE).isEmpty) + } + + // C8 + func testBitrateIsReportedAgainForNewSession() { + let harness = makeHarness() + harness.player.source = TestSources.hls() + harness.player.play() + waitUntil("content bitrate reported") { !harness.video.metricValues(CIS_SSDK_PLAYBACK_METRIC_BITRATE).isEmpty } + let lastBitrate = harness.video.metricValues(CIS_SSDK_PLAYBACK_METRIC_BITRATE).last as? NSNumber + harness.player.pause() + drainMainQueue() + let reportedBefore = harness.video.metricValues(CIS_SSDK_PLAYBACK_METRIC_BITRATE).count + + harness.connector.stopAndStartNewSession(contentInfo: [:]) + + let reported = harness.video.metricValues(CIS_SSDK_PLAYBACK_METRIC_BITRATE) + XCTAssertEqual(reported.count, reportedBefore + 1) + XCTAssertEqual(reported.last as? NSNumber, lastBitrate) + } + + private final class Bandwidths { var values: [Int] = [] } + + private func recordActiveQualityBandwidths(of player: THEOplayer) -> Bandwidths { + let bandwidths = Bandwidths() + _ = player.videoTracks.addEventListener(type: VideoTrackListEventTypes.ADD_TRACK) { event in + _ = (event.track as? VideoTrack)?.addEventListener(type: MediaTrackEventTypes.ACTIVE_QUALITY_CHANGED) { + bandwidths.values.append($0.quality.bandwidth) + } + } + return bandwidths + } +} From e3224a95f7e4e7c04377a862ae4156134336bce3 Mon Sep 17 00:00:00 2001 From: Ceyhun Ozgoc Date: Wed, 18 Nov 2026 09:51:10 +0000 Subject: [PATCH 06/11] Conviva: test ad break reporting and failed ad break reporting Co-Authored-By: Ceyhun Ozgoc Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../AdBreakTests.swift | 73 ++++++++++++++ .../FailedAdBreakTests.swift | 96 +++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 Code/Conviva/Tests/THEOplayerConnectorConvivaTests/AdBreakTests.swift create mode 100644 Code/Conviva/Tests/THEOplayerConnectorConvivaTests/FailedAdBreakTests.swift diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/AdBreakTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/AdBreakTests.swift new file mode 100644 index 00000000..b9903b98 --- /dev/null +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/AdBreakTests.swift @@ -0,0 +1,73 @@ +// +// AdBreakTests.swift +// + +import XCTest +import THEOplayerSDK +import ConvivaSDK +@testable import THEOplayerConnectorConviva + +@MainActor +final class AdBreakTests: XCTestCase { + // C9 + func testPodPositionFollowsTimeOffset() { + XCTAssertEqual(MockAdBreak(timeOffset: 0).calculateCurrentAdBreakPosition(), "Pre-roll") + XCTAssertEqual(MockAdBreak(timeOffset: 30).calculateCurrentAdBreakPosition(), "Mid-roll") + XCTAssertEqual(MockAdBreak(timeOffset: -1).calculateCurrentAdBreakPosition(), "Post-roll") + } + + // C9 — pod values must not depend on the device locale (e.g. "," as decimal separator). + func testSerializationUsesDotWithoutGrouping() { + let formatter = AdHandler.createSerializationFormatter() + XCTAssertEqual(formatter.decimalSeparator, ".") + XCTAssertFalse(formatter.usesGroupingSeparator) + XCTAssertEqual(AdHandler.serialize(number: 12.5), "12.5") + XCTAssertEqual(AdHandler.serialize(number: 1234567.25), "1234567.25") + XCTAssertEqual(AdHandler.serialize(number: 30), "30") + } + + // C9 + func testAdBreaksReportIncrementingPodIndexAndPosition() { + let harness = makeHarness() + let controller = harness.registerCustomAdIntegration() + harness.player.source = TestSources.hls() + + playAdBreak(timeOffset: 0, maxDuration: 15, using: controller) + playAdBreak(timeOffset: 60, maxDuration: 10, using: controller) + drainMainQueue() + + let started = harness.video.adBreaksStarted + XCTAssertEqual(started.map { $0.info[CIS_SSDK_AD_BREAK_POD_INDEX] as? String }, ["1", "2"]) + XCTAssertEqual(started.map { $0.info[CIS_SSDK_AD_BREAK_POD_POSITION] as? String }, ["Pre-roll", "Mid-roll"]) + XCTAssertEqual(started.map { $0.info[CIS_SSDK_AD_BREAK_POD_DURATION] as? String }, ["15", "10"]) + XCTAssertEqual(started.map { $0.info["podTechnology"] as? String }, ["Server Side", "Server Side"]) + XCTAssertEqual(started.map(\.adType), [.SERVER_SIDE, .SERVER_SIDE]) + XCTAssertEqual(harness.video.adBreakEndedCount, 2) + } + + // C9 + func testAdPlaybackEventsAreOnlyForwardedDuringAdBreak() { + let harness = makeHarness() + let controller = harness.registerCustomAdIntegration() + harness.player.source = TestSources.hls() + + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + waitForEvent(PlayerEventTypes.PAUSE, on: harness.player) { harness.player.pause() } + XCTAssertTrue(harness.ad.playerStates.isEmpty) + + let adBreak = controller.createAdBreak(params: AdBreakInit(timeOffset: 0, maxDuration: 30)) + controller.beginAd(ad: controller.createAd(params: AdInit(type: "linear", duration: 30), adBreak: adBreak)) + drainMainQueue() + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + waitForEvent(PlayerEventTypes.PAUSE, on: harness.player) { harness.player.pause() } + + XCTAssertEqual(harness.ad.playerStates.last, .CONVIVA_PAUSED) + } + + private func playAdBreak(timeOffset: Int, maxDuration: Int, using controller: ServerSideAdIntegrationController) { + let adBreak = controller.createAdBreak(params: AdBreakInit(timeOffset: timeOffset, maxDuration: maxDuration)) + let ad = controller.createAd(params: AdInit(type: "linear", duration: maxDuration), adBreak: adBreak) + controller.beginAd(ad: ad) + controller.endAd(ad: ad) + } +} diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/FailedAdBreakTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/FailedAdBreakTests.swift new file mode 100644 index 00000000..f0d20630 --- /dev/null +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/FailedAdBreakTests.swift @@ -0,0 +1,96 @@ +// +// FailedAdBreakTests.swift +// + +import XCTest +import THEOplayerSDK +import ConvivaSDK +@testable import THEOplayerConnectorConviva +#if canImport(THEOplayerTHEOadsIntegration) +import THEOplayerTHEOadsIntegration +#endif + +@MainActor +final class FailedAdBreakTests: XCTestCase { + // C10 + func testFailedAdBreakIsReportedAsServerGuidedPod() { + let analytics = RecordingAnalytics() + let endpoints = ConvivaEndpoints(analytics: analytics) + let storage = ConvivaStorage() + let handler = AdHandler(endpoints: endpoints, storage: storage) + + handler.reportFailedAdBreak(message: "No ad available", podDuration: 12.5, podPosition: "Mid-roll") + + let started = analytics.video.adBreaksStarted + XCTAssertEqual(started.count, 1) + XCTAssertEqual(started.first?.adType, .SERVER_SIDE) + XCTAssertEqual(started.first?.info[CIS_SSDK_AD_BREAK_POD_INDEX] as? String, "1") + XCTAssertEqual(started.first?.info[CIS_SSDK_AD_BREAK_POD_POSITION] as? String, "Mid-roll") + XCTAssertEqual(started.first?.info[CIS_SSDK_AD_BREAK_POD_DURATION] as? String, "12.5") + XCTAssertEqual(started.first?.info["podTechnology"] as? String, "Server Guided") + XCTAssertEqual(analytics.ad.adFailed.map(\.message), ["No ad available"]) + XCTAssertEqual(analytics.ad.adFailed.first?.adInfo?["c3.ad.technology"] as? String, "Server Guided") + XCTAssertEqual(analytics.ad.adFailed.first?.adInfo?["c3.csid"] as? Int32, 42) + XCTAssertEqual(analytics.video.adBreakEndedCount, 1) + withExtendedLifetime((endpoints, storage)) {} + } + + // C10 + func testFailedAdBreakIsIgnoredWhileAdBreakIsActive() { + let harness = makeHarness() + let controller = harness.registerCustomAdIntegration() + let analytics = RecordingAnalytics() + let endpoints = ConvivaEndpoints(analytics: analytics) + let storage = ConvivaStorage() + let handler = AdHandler(endpoints: endpoints, storage: storage) + let forwarder = AdEventForwarder(player: harness.player, handler: handler) + harness.player.source = TestSources.hls() + let adBreak = controller.createAdBreak(params: AdBreakInit(timeOffset: 0, maxDuration: 30)) + let ad = controller.createAd(params: AdInit(type: "linear", duration: 30), adBreak: adBreak) + controller.beginAd(ad: ad) + drainMainQueue() + + handler.reportFailedAdBreak(message: "No ad available", podDuration: 30, podPosition: "Mid-roll") + XCTAssertEqual(analytics.video.adBreaksStarted.count, 1, "Only the active ad break must be reported") + XCTAssertTrue(analytics.ad.adFailed.isEmpty) + + controller.endAd(ad: ad) + drainMainQueue() + handler.reportFailedAdBreak(message: "No ad available", podDuration: 30, podPosition: "Mid-roll") + + XCTAssertEqual(analytics.video.adBreaksStarted.map { $0.info[CIS_SSDK_AD_BREAK_POD_INDEX] as? String }, ["1", "2"]) + XCTAssertEqual(analytics.ad.adFailed.count, 1) + withExtendedLifetime((endpoints, storage, forwarder)) {} + } + +#if canImport(THEOplayerTHEOadsIntegration) + private struct FakeInterstitial: Interstitial { + var id = "interstitial" + var type: InterstitialType = .adbreak + var startTime: Double + var duration: Double? + var adTagParameters: [String: String] = [:] + } + + // C10 + func testOnlyInterstitialsThatEndedBeforeCurrentTimeArePast() { + XCTAssertTrue(AdHandler.isPast(interstitial: FakeInterstitial(startTime: 10, duration: 5), currentTime: 20)) + XCTAssertFalse(AdHandler.isPast(interstitial: FakeInterstitial(startTime: 10, duration: 5), currentTime: 12)) + XCTAssertFalse(AdHandler.isPast(interstitial: FakeInterstitial(startTime: 30, duration: 5), currentTime: 20)) + } + + // C10 + func testPostRollInterstitialIsNeverPast() { + XCTAssertFalse(AdHandler.isPast(interstitial: FakeInterstitial(startTime: -1, duration: 5), currentTime: 1_000)) + XCTAssertFalse(AdHandler.isPast(interstitial: FakeInterstitial(startTime: .infinity, duration: 5), currentTime: 1_000)) + } + + // C10 + func testInterstitialPodPositionFollowsStartTime() { + XCTAssertEqual(AdHandler.calculateInterstitialAdBreakPosition(startTime: 0), "Pre-roll") + XCTAssertEqual(AdHandler.calculateInterstitialAdBreakPosition(startTime: 30), "Mid-roll") + XCTAssertEqual(AdHandler.calculateInterstitialAdBreakPosition(startTime: -1), "Post-roll") + XCTAssertEqual(AdHandler.calculateInterstitialAdBreakPosition(startTime: .infinity), "Post-roll") + } +#endif +} From 4e01917be249db70de4a25ef6348479ef18ab390 Mon Sep 17 00:00:00 2001 From: Ceyhun Ozgoc Date: Wed, 18 Nov 2026 09:59:41 +0000 Subject: [PATCH 07/11] Conviva: test live HLS and HESP encoding_type on the THEOlive demo channel Co-Authored-By: Ceyhun Ozgoc Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../ConvivaTestSupport.swift | 20 +++++++++++++++++++ .../EncodingTypeTests.swift | 15 ++++++++++++++ .../LiveDetectionTests.swift | 14 +++++++++++++ Package.swift | 1 + 4 files changed, 50 insertions(+) diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift index 883691c4..a0468ecb 100644 --- a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift @@ -16,6 +16,12 @@ enum TestSources { } } +/// The public THEOlive demo channel. The default license implicitly allows theo.live sources. +struct THEOliveDemoEndpoint: Decodable { + let hlsSrc: String + let hespSrc: String +} + /// A real player with a `ConvivaConnector` that reports to a `RecordingAnalytics`. final class ConvivaHarness { let player = THEOplayer(with: nil) @@ -88,6 +94,20 @@ extension XCTestCase { return received } + /// Asks THEOlive discovery for the demo channel's current endpoint, so tests don't depend on its engine path. + func fetchTHEOliveDemoEndpoint(file: StaticString = #filePath, line: UInt = #line) throws -> THEOliveDemoEndpoint { + struct Distribution: Decodable { let endpoints: [THEOliveDemoEndpoint] } + let fetched = expectation(description: "THEOlive discovery response") + var result: Result = .failure(URLError(.timedOut)) + URLSession.shared.dataTask(with: URL(string: "https://discovery.theo.live/v2/distributions/demo")!) { data, _, error in + result = data.map { .success($0) } ?? .failure(error ?? URLError(.badServerResponse)) + fetched.fulfill() + }.resume() + wait(for: [fetched], timeout: 30) + let distribution = try JSONDecoder().decode(Distribution.self, from: result.get()) + return try XCTUnwrap(distribution.endpoints.first, "No THEOlive demo endpoint", file: file, line: line) + } + func waitUntil(_ description: String, timeout: TimeInterval = 30, _ condition: @escaping () -> Bool) { let conditionMet = XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in condition() }, object: nil) conditionMet.expectationDescription = description diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/EncodingTypeTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/EncodingTypeTests.swift index 4ed2693e..67c0686b 100644 --- a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/EncodingTypeTests.swift +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/EncodingTypeTests.swift @@ -4,6 +4,7 @@ import XCTest import THEOplayerSDK +import THEOplayerTHEOliveIntegration @testable import THEOplayerConnectorConviva @MainActor @@ -19,6 +20,20 @@ final class EncodingTypeTests: XCTestCase { XCTAssertEqual(harness.video.contentInfoValues(ENCODING_TYPE).first as? String, "HLS") } + // C6 — HESP needs the THEOlive integration; the connector only learns the type from CURRENT_SOURCE_CHANGE. + func testHespEncodingTypeIsReportedOnceSourceIsSelected() throws { + let endpoint = try fetchTHEOliveDemoEndpoint() + let harness = makeHarness() + harness.player.addIntegration(THEOliveIntegrationFactory.createIntegration()) + harness.player.source = SourceDescription(source: TypedSource(src: endpoint.hespSrc, type: "application/vnd.theo.hesp+json")) + + waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + XCTAssertEqual(harness.video.playbackRequested.count, 1) + + waitUntil("HESP encoding_type reported") { !harness.video.contentInfoValues(ENCODING_TYPE).isEmpty } + XCTAssertEqual(Set(harness.video.contentInfoValues(ENCODING_TYPE).compactMap { $0 as? String }), ["HESP"]) + } + // C6 func testCustomerEncodingTypeWins() { let harness = makeHarness() diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/LiveDetectionTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/LiveDetectionTests.swift index 281578fc..c3251c63 100644 --- a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/LiveDetectionTests.swift +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/LiveDetectionTests.swift @@ -21,6 +21,20 @@ final class LiveDetectionTests: XCTestCase { XCTAssertEqual(harness.video.contentInfoValues(CIS_SSDK_METADATA_DURATION).last as? NSNumber, event?.duration.map { NSNumber(value: $0) }) } + // C5 + func testInfiniteDurationReportsLive() throws { + let endpoint = try fetchTHEOliveDemoEndpoint() + let harness = makeHarness() + + let event = waitForEvent(PlayerEventTypes.DURATION_CHANGE, on: harness.player) { + harness.player.source = TestSources.hls(endpoint.hlsSrc) + } + + XCTAssertEqual(event?.duration, .infinity) + XCTAssertEqual(harness.video.contentInfoValues(CIS_SSDK_METADATA_IS_LIVE).last as? NSNumber, true) + XCTAssertTrue(harness.video.contentInfoValues(CIS_SSDK_METADATA_DURATION).isEmpty) + } + // C5 func testCustomerIsLiveIsNotOverwritten() { let harness = makeHarness() diff --git a/Package.swift b/Package.swift index 3d04cd89..ff8d2ac4 100644 --- a/Package.swift +++ b/Package.swift @@ -45,6 +45,7 @@ let package = Package( dependencies: [ "THEOplayerConnectorConviva", "THEOplayerSDK", + .product(name: "THEOplayerTHEOliveIntegration", package: "THEOplayerSDK"), "ConvivaSDK", ], path: "Code/Conviva/Tests/THEOplayerConnectorConvivaTests" From 33f5d2072b7c2b3f5f34545967cf5ac61303831f Mon Sep 17 00:00:00 2001 From: Ceyhun Ozgoc Date: Wed, 18 Nov 2026 10:04:56 +0000 Subject: [PATCH 08/11] Conviva: test that a network error is reported as a warning and keeps the session Co-Authored-By: Ceyhun Ozgoc Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../ConvivaTestSupport.swift | 2 ++ .../ErrorTests.swift | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift index a0468ecb..2ff2ec1a 100644 --- a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift @@ -10,6 +10,8 @@ enum TestSources { // The default (unlicensed) player only plays sources hosted on theoplayer.com. static let bigBuckBunny = "https://cdn.theoplayer.com/video/big_buck_bunny/big_buck_bunny.m3u8" static let elephantsDream = "https://cdn.theoplayer.com/video/elephants-dream/playlist.m3u8" + // Segment 4 (18-24 s) of every video variant returns HTTP 403. + static let missingSegment = "https://cdn.theoplayer.com/video/elephants-dream/404/index.m3u8" static func hls(_ url: String = bigBuckBunny) -> SourceDescription { SourceDescription(source: TypedSource(src: url, type: "application/x-mpegurl")) diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ErrorTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ErrorTests.swift index 45897dd8..e77dd213 100644 --- a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ErrorTests.swift +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ErrorTests.swift @@ -38,4 +38,32 @@ final class ErrorTests: XCTestCase { XCTAssertTrue(analytics.video.playbackFailed.isEmpty) } + + // C4 + func testNetworkErrorIsReportedAsWarningAndKeepsSession() { + let harness = makeHarness() + harness.player.source = TestSources.hls(TestSources.missingSegment) + harness.player.currentTime = 15 + + // Snapshot inside the listener: AVPlayer may give up on the stream later and raise a fatal ERROR. + let networkErrorReceived = expectation(description: "network ERROR event") + var errorsAtNetworkError: [(message: String, severity: ErrorSeverity)] = [] + var failedAtNetworkError: [String] = [] + var endedAtNetworkError = -1 + let listener = harness.player.network.addEventListener(type: NetworkEventTypes.ERROR) { _ in + guard endedAtNetworkError < 0 else { return } + errorsAtNetworkError = harness.video.playbackErrors + failedAtNetworkError = harness.video.playbackFailed + endedAtNetworkError = harness.video.playbackEndedCount + networkErrorReceived.fulfill() + } + harness.player.play() + wait(for: [networkErrorReceived], timeout: 60) + harness.player.network.removeEventListener(type: NetworkEventTypes.ERROR, listener: listener) + + XCTAssertEqual(harness.video.playbackRequested.count, 1) + XCTAssertEqual(errorsAtNetworkError.map(\.severity), [.ERROR_WARNING]) + XCTAssertEqual(failedAtNetworkError, []) + XCTAssertEqual(endedAtNetworkError, 0) + } } From f86a7b63b3f7f00aa75b17acef7ed7e089745c6e Mon Sep 17 00:00:00 2001 From: Ceyhun Ozgoc Date: Wed, 18 Nov 2026 03:55:31 -0800 Subject: [PATCH 09/11] Conviva: fix Xcode test setup and playback fixtures Co-Authored-By: Ceyhun Ozgoc Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../THEOplayerConnectorConvivaTests.xcscheme | 53 +++++++++++++++++++ .../Source/Base/ConvivaEndpoints.swift | 2 +- .../ConvivaTestSupport.swift | 21 +++++--- .../EncodingTypeTests.swift | 10 ++-- .../ErrorTests.swift | 1 + .../LiveDetectionTests.swift | 17 ++++-- .../RecordingAnalytics.swift | 2 +- .../SessionLifecycleTests.swift | 8 +-- .../SourceChangeTests.swift | 1 + 9 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 .swiftpm/xcode/xcshareddata/xcschemes/THEOplayerConnectorConvivaTests.xcscheme diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/THEOplayerConnectorConvivaTests.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/THEOplayerConnectorConvivaTests.xcscheme new file mode 100644 index 00000000..55c7611e --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/THEOplayerConnectorConvivaTests.xcscheme @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/Code/Conviva/Source/Base/ConvivaEndpoints.swift b/Code/Conviva/Source/Base/ConvivaEndpoints.swift index b2c22de0..05ea5458 100644 --- a/Code/Conviva/Source/Base/ConvivaEndpoints.swift +++ b/Code/Conviva/Source/Base/ConvivaEndpoints.swift @@ -17,7 +17,7 @@ class ConvivaEndpoints { } self.init(analytics: analytics) } - + /// Lets tests inject a recording `CISAnalytics` instead of the Conviva SDK one. init(analytics: CISAnalytics) { self.analytics = analytics diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift index 2ff2ec1a..6316cef2 100644 --- a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ConvivaTestSupport.swift @@ -12,6 +12,7 @@ enum TestSources { static let elephantsDream = "https://cdn.theoplayer.com/video/elephants-dream/playlist.m3u8" // Segment 4 (18-24 s) of every video variant returns HTTP 403. static let missingSegment = "https://cdn.theoplayer.com/video/elephants-dream/404/index.m3u8" + static let theoliveDemoChannel = "demo" static func hls(_ url: String = bigBuckBunny) -> SourceDescription { SourceDescription(source: TypedSource(src: url, type: "application/x-mpegurl")) @@ -21,7 +22,6 @@ enum TestSources { /// The public THEOlive demo channel. The default license implicitly allows theo.live sources. struct THEOliveDemoEndpoint: Decodable { let hlsSrc: String - let hespSrc: String } /// A real player with a `ConvivaConnector` that reports to a `RecordingAnalytics`. @@ -64,7 +64,7 @@ extension XCTestCase { let harness = ConvivaHarness(convivaMetadata: convivaMetadata) addTeardownBlock { harness.connector.destroy() - harness.player.destroy() + harness.player.stop() } return harness } @@ -100,11 +100,20 @@ extension XCTestCase { func fetchTHEOliveDemoEndpoint(file: StaticString = #filePath, line: UInt = #line) throws -> THEOliveDemoEndpoint { struct Distribution: Decodable { let endpoints: [THEOliveDemoEndpoint] } let fetched = expectation(description: "THEOlive discovery response") + let url = URL(string: "https://discovery.theo.live/v2/distributions/\(TestSources.theoliveDemoChannel)")! var result: Result = .failure(URLError(.timedOut)) - URLSession.shared.dataTask(with: URL(string: "https://discovery.theo.live/v2/distributions/demo")!) { data, _, error in - result = data.map { .success($0) } ?? .failure(error ?? URLError(.badServerResponse)) - fetched.fulfill() - }.resume() + let task = URLSession.shared.dataTask(with: url) { data, response, error in + DispatchQueue.main.async { + if let data, (response as? HTTPURLResponse)?.statusCode == 200 { + result = .success(data) + } else { + result = .failure(error ?? URLError(.badServerResponse)) + } + fetched.fulfill() + } + } + task.resume() + defer { task.cancel() } wait(for: [fetched], timeout: 30) let distribution = try JSONDecoder().decode(Distribution.self, from: result.get()) return try XCTUnwrap(distribution.endpoints.first, "No THEOlive demo endpoint", file: file, line: line) diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/EncodingTypeTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/EncodingTypeTests.swift index 67c0686b..da0e0fbc 100644 --- a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/EncodingTypeTests.swift +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/EncodingTypeTests.swift @@ -21,13 +21,13 @@ final class EncodingTypeTests: XCTestCase { } // C6 — HESP needs the THEOlive integration; the connector only learns the type from CURRENT_SOURCE_CHANGE. - func testHespEncodingTypeIsReportedOnceSourceIsSelected() throws { - let endpoint = try fetchTHEOliveDemoEndpoint() + func testHespEncodingTypeIsReportedOnceSourceIsSelected() { let harness = makeHarness() - harness.player.addIntegration(THEOliveIntegrationFactory.createIntegration()) - harness.player.source = SourceDescription(source: TypedSource(src: endpoint.hespSrc, type: "application/vnd.theo.hesp+json")) + harness.player.source = SourceDescription(source: TheoLiveSource(channelId: TestSources.theoliveDemoChannel)) - waitForEvent(PlayerEventTypes.PLAY, on: harness.player) { harness.player.play() } + guard waitForEvent(PlayerEventTypes.PLAY, on: harness.player, after: { harness.player.play() }) != nil else { + return + } XCTAssertEqual(harness.video.playbackRequested.count, 1) waitUntil("HESP encoding_type reported") { !harness.video.contentInfoValues(ENCODING_TYPE).isEmpty } diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ErrorTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ErrorTests.swift index e77dd213..7d39d0c1 100644 --- a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ErrorTests.swift +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/ErrorTests.swift @@ -2,6 +2,7 @@ // ErrorTests.swift // +import ConvivaSDK import XCTest import THEOplayerSDK @testable import THEOplayerConnectorConviva diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/LiveDetectionTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/LiveDetectionTests.swift index c3251c63..d07015b6 100644 --- a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/LiveDetectionTests.swift +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/LiveDetectionTests.swift @@ -10,15 +10,18 @@ import ConvivaSDK @MainActor final class LiveDetectionTests: XCTestCase { // C5 - func testFiniteDurationReportsVodWithDuration() { + func testFiniteDurationReportsVodWithDuration() throws { let harness = makeHarness() let event = waitForEvent(PlayerEventTypes.DURATION_CHANGE, on: harness.player) { harness.player.source = TestSources.hls() + harness.player.play() } + let duration = try XCTUnwrap(event?.duration) + XCTAssertTrue(duration.isFinite) XCTAssertEqual(harness.video.contentInfoValues(CIS_SSDK_METADATA_IS_LIVE).last as? NSNumber, false) - XCTAssertEqual(harness.video.contentInfoValues(CIS_SSDK_METADATA_DURATION).last as? NSNumber, event?.duration.map { NSNumber(value: $0) }) + XCTAssertEqual(harness.video.contentInfoValues(CIS_SSDK_METADATA_DURATION).last as? NSNumber, NSNumber(value: duration)) } // C5 @@ -28,24 +31,30 @@ final class LiveDetectionTests: XCTestCase { let event = waitForEvent(PlayerEventTypes.DURATION_CHANGE, on: harness.player) { harness.player.source = TestSources.hls(endpoint.hlsSrc) + harness.player.play() } XCTAssertEqual(event?.duration, .infinity) XCTAssertEqual(harness.video.contentInfoValues(CIS_SSDK_METADATA_IS_LIVE).last as? NSNumber, true) XCTAssertTrue(harness.video.contentInfoValues(CIS_SSDK_METADATA_DURATION).isEmpty) + XCTAssertEqual(harness.video.contentInfoValues(ENCODING_TYPE).first as? String, "HLS") } // C5 func testCustomerIsLiveIsNotOverwritten() { let harness = makeHarness() + var contentInfoCountAtPlay = 0 - waitForEvent(PlayerEventTypes.SOURCE_CHANGE, on: harness.player) { harness.player.source = TestSources.hls() } waitForEvent(PlayerEventTypes.DURATION_CHANGE, on: harness.player) { + harness.player.source = TestSources.hls() harness.connector.setContentInfo([CIS_SSDK_METADATA_IS_LIVE: true]) + harness.player.play() + contentInfoCountAtPlay = harness.video.contentInfos.count } let reportedIsLive = harness.video.contentInfoValues(CIS_SSDK_METADATA_IS_LIVE).map { $0 as? Bool } - XCTAssertGreaterThan(reportedIsLive.count, 1, "The duration change must have pushed content info") + XCTAssertGreaterThan(harness.video.contentInfos.count, contentInfoCountAtPlay, "The duration change must have pushed content info") + XCTAssertFalse(reportedIsLive.isEmpty) XCTAssertTrue(reportedIsLive.allSatisfy { $0 == true }) XCTAssertTrue(harness.video.contentInfoValues(CIS_SSDK_METADATA_DURATION).isEmpty) } diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/RecordingAnalytics.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/RecordingAnalytics.swift index 967577cc..fd0f8023 100644 --- a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/RecordingAnalytics.swift +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/RecordingAnalytics.swift @@ -19,7 +19,7 @@ class RecordingStreamAnalytics: NSObject, CISStreamAnalyticsProtocol { func reportPlaybackMetric(_ key: String, value: Any?) { metrics.append((key, value)) } func cleanup() { cleanupCount += 1 } - func reportPlayerState(_ playerState: PlayerState) {} + func report(_ playerState: PlayerState) {} func getSessionId() -> Int32 { 42 } func getSessionKey() -> Int32 { 42 } func getMetadataInfo() -> [AnyHashable: Any] { [:] } diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SessionLifecycleTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SessionLifecycleTests.swift index fab80f73..9380c0f1 100644 --- a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SessionLifecycleTests.swift +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SessionLifecycleTests.swift @@ -26,11 +26,13 @@ final class SessionLifecycleTests: XCTestCase { func testEndedReportsStoppedAndEndsSession() { let harness = makeHarness() let player = harness.player - waitForEvent(PlayerEventTypes.DURATION_CHANGE, on: player) { player.source = TestSources.hls() } + waitForEvent(PlayerEventTypes.DURATION_CHANGE, on: player) { + player.source = TestSources.hls() + player.play() + } guard let duration = player.duration, duration.isFinite else { return XCTFail("VOD source without finite duration") } - player.currentTime = duration - 2 - waitForEvent(PlayerEventTypes.ENDED, on: player, timeout: 60) { player.play() } + waitForEvent(PlayerEventTypes.ENDED, on: player, timeout: 60) { player.currentTime = duration - 2 } XCTAssertEqual(harness.video.playbackRequested.count, 1) XCTAssertEqual(harness.video.playerStates.last, .CONVIVA_STOPPED) diff --git a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SourceChangeTests.swift b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SourceChangeTests.swift index de98040a..11b3105e 100644 --- a/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SourceChangeTests.swift +++ b/Code/Conviva/Tests/THEOplayerConnectorConvivaTests/SourceChangeTests.swift @@ -2,6 +2,7 @@ // SourceChangeTests.swift // +import ConvivaSDK import XCTest import THEOplayerSDK @testable import THEOplayerConnectorConviva From a13f75ef6b56257ca225a7483a5de31b860c6412 Mon Sep 17 00:00:00 2001 From: Ceyhun Ozgoc Date: Thu, 19 Nov 2026 11:26:51 +0000 Subject: [PATCH 10/11] Conviva: compile the OptiView Ads interstitial tests Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-Authored-By: Ceyhun Ozgoc --- Package.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Package.swift b/Package.swift index ff8d2ac4..e06dcaf6 100644 --- a/Package.swift +++ b/Package.swift @@ -46,6 +46,7 @@ let package = Package( "THEOplayerConnectorConviva", "THEOplayerSDK", .product(name: "THEOplayerTHEOliveIntegration", package: "THEOplayerSDK"), + .product(name: "THEOplayerTHEOadsIntegration", package: "THEOplayerSDK"), "ConvivaSDK", ], path: "Code/Conviva/Tests/THEOplayerConnectorConvivaTests" From f289f91fae5928aac4dab86c646a7acddd10947c Mon Sep 17 00:00:00 2001 From: Ceyhun Ozgoc Date: Thu, 19 Nov 2026 11:30:11 +0000 Subject: [PATCH 11/11] Conviva: add Google IMA to the test target for the OptiView Ads integration Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-Authored-By: Ceyhun Ozgoc --- Package.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Package.swift b/Package.swift index e06dcaf6..4c27612c 100644 --- a/Package.swift +++ b/Package.swift @@ -29,6 +29,7 @@ let package = Package( .package(name: "Swifter", url: "https://github.com/httpswift/swifter.git", .exactItem("1.5.0")), .package(name: "SwiftSubtitles", url: "https://github.com/dagronf/SwiftSubtitles.git", .exactItem("0.9.1")), .package(name: "ComScore", url: "https://github.com/comScore/Comscore-Swift-Package-Manager", "6.10.0"..<"6.11.0"), + .package(name: "GoogleInteractiveMediaAds", url: "https://github.com/googleads/swift-package-manager-google-interactive-media-ads-ios", "3.18.0"..<"4.0.0"), ], targets: [ .target( @@ -47,6 +48,7 @@ let package = Package( "THEOplayerSDK", .product(name: "THEOplayerTHEOliveIntegration", package: "THEOplayerSDK"), .product(name: "THEOplayerTHEOadsIntegration", package: "THEOplayerSDK"), + .product(name: "GoogleInteractiveMediaAds", package: "GoogleInteractiveMediaAds", condition: .when(platforms: [.iOS])), "ConvivaSDK", ], path: "Code/Conviva/Tests/THEOplayerConnectorConvivaTests"