diff --git a/Tests/MuxPlayerSwift/MonitorTests.swift b/Tests/MuxPlayerSwift/MonitorTests.swift index 715bda6e..9dcb640e 100644 --- a/Tests/MuxPlayerSwift/MonitorTests.swift +++ b/Tests/MuxPlayerSwift/MonitorTests.swift @@ -228,4 +228,558 @@ class MonitorTests: XCTestCase { XCTAssertTrue(registration.1) testMonitor.monitoringRegistrations.removeAll() } + + func testInitializedPlayerViewControllerKVORegistrationNonDRM() throws { + PlayerSDK.shared.monitor = Monitor() + + let playerViewController = AVPlayerViewController( + playbackID: "abc123" + ) + + XCTAssertEqual( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations + .count, + 1 + ) + + let player = try XCTUnwrap( + playerViewController.player + ) + + let observations = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + observations.count, + 1 + ) + + let playerViewControllerObjectIdentifier = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .playerObjectIdentifiersToBindingReferenceObjectIdentifier[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + playerViewControllerObjectIdentifier, + ObjectIdentifier(playerViewController) + ) + + PlayerSDK.shared.monitor.tearDownMonitoring( + playerViewController: playerViewController + ) + + XCTAssertNil( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + } + + func testInitializedPlayerLayerKVORegistrationNonDRM() throws { + PlayerSDK.shared.monitor = Monitor() + + let playerLayer = AVPlayerLayer( + playbackID: "abc123" + ) + + XCTAssertEqual( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations + .count, + 1 + ) + + let player = try XCTUnwrap( + playerLayer.player + ) + + let observations = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + observations.count, + 1 + ) + + let playerLayerObjectIdentifier = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .playerObjectIdentifiersToBindingReferenceObjectIdentifier[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + playerLayerObjectIdentifier, + ObjectIdentifier(playerLayer) + ) + + PlayerSDK.shared.monitor.tearDownMonitoring( + playerLayer: playerLayer + ) + + XCTAssertNil( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + } + + func testPreexistingPlayerViewControllerKVORegistrationNonDRM() throws { + PlayerSDK.shared.monitor = Monitor() + + let playerViewController = AVPlayerViewController() + playerViewController.prepare( + playbackID: "abc123" + ) + + XCTAssertEqual( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations + .count, + 1 + ) + + let player = try XCTUnwrap( + playerViewController.player + ) + + let observations = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + observations.count, + 1 + ) + + let playerViewControllerObjectIdentifier = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .playerObjectIdentifiersToBindingReferenceObjectIdentifier[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + playerViewControllerObjectIdentifier, + ObjectIdentifier(playerViewController) + ) + + PlayerSDK.shared.monitor.tearDownMonitoring( + playerViewController: playerViewController + ) + + XCTAssertNil( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + } + + func testPreexistingPlayerLayerKVORegistrationNonDRM() throws { + PlayerSDK.shared.monitor = Monitor() + + let playerLayer = AVPlayerLayer() + playerLayer.prepare( + playbackID: "abc123" + ) + + XCTAssertEqual( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations + .count, + 1 + ) + + let player = try XCTUnwrap( + playerLayer.player + ) + + let observations = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + observations.count, + 1 + ) + + let playerLayerObjectIdentifier = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .playerObjectIdentifiersToBindingReferenceObjectIdentifier[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + playerLayerObjectIdentifier, + ObjectIdentifier(playerLayer) + ) + + PlayerSDK.shared.monitor.tearDownMonitoring( + playerLayer: playerLayer + ) + + XCTAssertNil( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + } + + func testInitializedPlayerViewControllerKVORegistrationDRM() throws { + PlayerSDK.shared.monitor = Monitor() + + let playerViewController = AVPlayerViewController( + playbackID: "abc123", + playbackOptions: PlaybackOptions( + playbackToken: "def456", + drmToken: "ghi789" + ) + ) + + XCTAssertEqual( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations + .count, + 1 + ) + + let player = try XCTUnwrap( + playerViewController.player + ) + + let observations = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + observations.count, + 2 + ) + + let playerViewControllerObjectIdentifier = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .playerObjectIdentifiersToBindingReferenceObjectIdentifier[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + playerViewControllerObjectIdentifier, + ObjectIdentifier(playerViewController) + ) + + PlayerSDK.shared.monitor.tearDownMonitoring( + playerViewController: playerViewController + ) + + XCTAssertNil( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + } + + func testInitializedPlayerLayerKVORegistrationDRM() throws { + PlayerSDK.shared.monitor = Monitor() + + let playerLayer = AVPlayerLayer( + playbackID: "abc123", + playbackOptions: PlaybackOptions( + playbackToken: "def456", + drmToken: "ghi789" + ) + ) + + XCTAssertEqual( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations + .count, + 1 + ) + + let player = try XCTUnwrap( + playerLayer.player + ) + + let observations = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + observations.count, + 2 + ) + + let playerLayerObjectIdentifier = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .playerObjectIdentifiersToBindingReferenceObjectIdentifier[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + playerLayerObjectIdentifier, + ObjectIdentifier(playerLayer) + ) + + PlayerSDK.shared.monitor.tearDownMonitoring( + playerLayer: playerLayer + ) + + XCTAssertNil( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + } + + func testPreexistingPlayerViewControllerKVORegistrationDRM() throws { + PlayerSDK.shared.monitor = Monitor() + + let playerViewController = AVPlayerViewController() + playerViewController.prepare( + playbackID: "abc123", + playbackOptions: PlaybackOptions( + playbackToken: "def456", + drmToken: "ghi789" + ) + ) + + XCTAssertEqual( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations + .count, + 1 + ) + + let player = try XCTUnwrap( + playerViewController.player + ) + + let observations = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + observations.count, + 2 + ) + + let playerViewControllerObjectIdentifier = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .playerObjectIdentifiersToBindingReferenceObjectIdentifier[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + playerViewControllerObjectIdentifier, + ObjectIdentifier(playerViewController) + ) + + PlayerSDK.shared.monitor.tearDownMonitoring( + playerViewController: playerViewController + ) + + XCTAssertNil( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + } + + func testPreexistingPlayerLayerKVORegistrationDRM() throws { + PlayerSDK.shared.monitor = Monitor() + + let playerLayer = AVPlayerLayer() + playerLayer.prepare( + playbackID: "abc123", + playbackOptions: PlaybackOptions( + playbackToken: "def456", + drmToken: "ghi789" + ) + ) + + XCTAssertEqual( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations + .count, + 1 + ) + + let player = try XCTUnwrap( + playerLayer.player + ) + + let observations = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + observations.count, + 2 + ) + + let playerLayerObjectIdentifier = try XCTUnwrap( + PlayerSDK + .shared + .monitor + .playerObjectIdentifiersToBindingReferenceObjectIdentifier[ + ObjectIdentifier(player) + ] + ) + + XCTAssertEqual( + playerLayerObjectIdentifier, + ObjectIdentifier(playerLayer) + ) + + PlayerSDK.shared.monitor.tearDownMonitoring( + playerLayer: playerLayer + ) + + XCTAssertNil( + PlayerSDK + .shared + .monitor + .keyValueObservation + .observations[ + ObjectIdentifier(player) + ] + ) + } + + func testPlayerItemUpdates() throws { + PlayerSDK.shared.monitor = Monitor() + + let playerLayer = AVPlayerLayer( + playbackID: "abc123", + playbackOptions: PlaybackOptions( + playbackToken: "def456", + drmToken: "ghi789" + ) + ) + + + } }