From 432861a1a2a2ac0857cfa628679137ad71c838be Mon Sep 17 00:00:00 2001 From: Cecilia de Siqueira Saraiva Date: Tue, 30 Jul 2024 10:31:07 +0200 Subject: [PATCH] - Add missing tests for playlog: load, play, reset the player engine, set up a new player engine, load and play the same track and reset; load, play, reset the player engine, set up a new player engine, load and play another track and reset --- Tests/PlayerTests/Playlog/PlayLogTests.swift | 169 +++++++++++++++++++ 1 file changed, 169 insertions(+) diff --git a/Tests/PlayerTests/Playlog/PlayLogTests.swift b/Tests/PlayerTests/Playlog/PlayLogTests.swift index 5569bf85..86b6d7a6 100644 --- a/Tests/PlayerTests/Playlog/PlayLogTests.swift +++ b/Tests/PlayerTests/Playlog/PlayLogTests.swift @@ -1200,6 +1200,175 @@ extension PlayLogTests { ) assertPlayLogEvent(actualPlayLogEvent: playLogEvent2, expectedPlayLogEvent: expectedPlayLogEvent2) } + + func test_load_and_play_and_resetPlayerEngine_and_setUpANewPlayerEngine_and_load_and_play_another_track_and_reset() { + // GIVEN + // First we load the media product and then proceed to play it. + uuid = "uuid1" + let shortAudioFile = shortAudioFile + setAudioFileResponseToURLProtocol(audioFile: shortAudioFile) + let mediaProduct1 = shortAudioFile.mediaProduct + playerEngine.load(mediaProduct1, timestamp: timestamp) + + optimizedWait { + playerEngine.currentItem != nil + } + guard let currentItem = playerEngine.currentItem else { + XCTFail("Expected for the currentItem to be set up!") + return + } + + playerEngine.play(timestamp: timestamp) + + // Wait for the track to reach 2 seconds + let loadSecondMediaProductAssetPosition: Double = 2 + wait(for: currentItem, toReach: loadSecondMediaProductAssetPosition) + + // Simulate Player.load() is called. + // This is in order to simulate the following scenario: load and play a track, load and play another track. + playerEngine.notificationsHandler = nil + playerEngine.resetOrUnload() + setUpPlayerEngine() + + // Now we load the second media product and then proceed to play it. + uuid = "uuid2" + let longAudioFile = longAudioFile + setAudioFileResponseToURLProtocol(audioFile: longAudioFile) + let mediaProduct2 = longAudioFile.mediaProduct + playerEngine.load(mediaProduct2, timestamp: timestamp) + + optimizedWait { + playerEngine.currentItem != nil + } + guard let nextCurrentItem = playerEngine.currentItem else { + XCTFail("Expected for the currentItem to be set up!") + return + } + + playerEngine.play(timestamp: timestamp) + + // Wait for the track to reach 1 second + let resetAssetPosition: Double = 1 + wait(for: nextCurrentItem, toReach: resetAssetPosition) + + playerEngine.reset() + + // THEN + optimizedWait(timeout: shortAudioFile.duration) { + playerEventSender.playLogEvents.count == 2 + } + + let playLogEvent1 = playerEventSender.playLogEvents[0] + let expectedPlayLogEvent1 = PlayLogEvent.mock( + startAssetPosition: 0, + requestedProductId: mediaProduct1.productId, + actualProductId: mediaProduct1.productId, + actualQuality: AudioQuality.LOSSLESS.rawValue, + sourceType: Constants.PlayLogSource.short.sourceType, + sourceId: Constants.PlayLogSource.short.sourceId, + actions: [], + endTimestamp: timestamp, + endAssetPosition: loadSecondMediaProductAssetPosition + ) + assertPlayLogEvent(actualPlayLogEvent: playLogEvent1, expectedPlayLogEvent: expectedPlayLogEvent1) + + let playLogEvent2 = playerEventSender.playLogEvents[1] + let expectedPlayLogEvent2 = PlayLogEvent.mock( + startAssetPosition: 0, + requestedProductId: mediaProduct2.productId, + actualProductId: mediaProduct2.productId, + actualQuality: AudioQuality.LOSSLESS.rawValue, + sourceType: Constants.PlayLogSource.long.sourceType, + sourceId: Constants.PlayLogSource.long.sourceId, + actions: [], + endTimestamp: timestamp, + endAssetPosition: resetAssetPosition + ) + assertPlayLogEvent(actualPlayLogEvent: playLogEvent2, expectedPlayLogEvent: expectedPlayLogEvent2) + } + + func test_load_and_play_and_resetPlayerEngine_and_setUpANewPlayerEngine_and_load_and_play_same_track_and_reset() { + // GIVEN + // First we load the media product and then proceed to play it. + uuid = "uuid1" + let shortAudioFile = shortAudioFile + setAudioFileResponseToURLProtocol(audioFile: shortAudioFile) + let mediaProduct1 = shortAudioFile.mediaProduct + playerEngine.load(mediaProduct1, timestamp: timestamp) + + optimizedWait { + playerEngine.currentItem != nil + } + guard let currentItem = playerEngine.currentItem else { + XCTFail("Expected for the currentItem to be set up!") + return + } + + playerEngine.play(timestamp: timestamp) + + // Wait for the track to reach 2 seconds + let loadSecondTimeAssetPosition: Double = 2 + wait(for: currentItem, toReach: loadSecondTimeAssetPosition) + + // Simulate Player.load() is called. + // This is in order to simulate the following scenario: load and play a track, load and play another track. + playerEngine.notificationsHandler = nil + playerEngine.resetOrUnload() + setUpPlayerEngine() + + // Now we load the same media product and then proceed to play it. + let mediaProduct2 = shortAudioFile.mediaProduct + playerEngine.load(mediaProduct2, timestamp: timestamp) + + optimizedWait { + playerEngine.currentItem != nil + } + guard let nextCurrentItem = playerEngine.currentItem else { + XCTFail("Expected for the currentItem to be set up!") + return + } + + playerEngine.play(timestamp: timestamp) + + // Wait for the track to reach 1 second + let resetAssetPosition: Double = 1 + wait(for: nextCurrentItem, toReach: resetAssetPosition) + + playerEngine.reset() + + // THEN + optimizedWait(timeout: shortAudioFile.duration) { + playerEventSender.playLogEvents.count == 2 + } + + let playLogEvent1 = playerEventSender.playLogEvents[0] + let expectedPlayLogEvent1 = PlayLogEvent.mock( + startAssetPosition: 0, + requestedProductId: mediaProduct1.productId, + actualProductId: mediaProduct1.productId, + actualQuality: AudioQuality.LOSSLESS.rawValue, + sourceType: Constants.PlayLogSource.short.sourceType, + sourceId: Constants.PlayLogSource.short.sourceId, + actions: [], + endTimestamp: timestamp, + endAssetPosition: loadSecondTimeAssetPosition + ) + assertPlayLogEvent(actualPlayLogEvent: playLogEvent1, expectedPlayLogEvent: expectedPlayLogEvent1) + + let playLogEvent2 = playerEventSender.playLogEvents[1] + let expectedPlayLogEvent2 = PlayLogEvent.mock( + startAssetPosition: 0, + requestedProductId: mediaProduct2.productId, + actualProductId: mediaProduct2.productId, + actualQuality: AudioQuality.LOSSLESS.rawValue, + sourceType: Constants.PlayLogSource.short.sourceType, + sourceId: Constants.PlayLogSource.short.sourceId, + actions: [], + endTimestamp: timestamp, + endAssetPosition: resetAssetPosition + ) + assertPlayLogEvent(actualPlayLogEvent: playLogEvent2, expectedPlayLogEvent: expectedPlayLogEvent2) + } } // MARK: - Assertion Helpers