From a0f1aea1cf349a87960eb145ec9c52ae1cd9df0b Mon Sep 17 00:00:00 2001 From: Lukas Knoch-Girstmair Date: Tue, 2 Jul 2024 06:44:21 +0200 Subject: [PATCH] Extract common test code --- .../conviva/testapp/AdsTrackingTests.kt | 5 +- .../conviva/testapp/AdvertisingTests.kt | 58 +++---------------- .../analytics/conviva/testapp/TestBase.kt | 9 +-- .../conviva/testapp/framework/Sources.kt | 18 ++++++ .../testapp/framework/TestingFramework.kt | 43 ++++++++++++++ 5 files changed, 75 insertions(+), 58 deletions(-) create mode 100644 ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/framework/Sources.kt create mode 100644 ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/framework/TestingFramework.kt diff --git a/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/AdsTrackingTests.kt b/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/AdsTrackingTests.kt index 1fad1ef..81696f3 100644 --- a/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/AdsTrackingTests.kt +++ b/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/AdsTrackingTests.kt @@ -1,6 +1,7 @@ package com.bitmovin.analytics.conviva.testapp import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.bitmovin.analytics.conviva.testapp.framework.Sources import com.conviva.api.* import com.conviva.sdk.ConvivaSdkConstants import io.mockk.* @@ -9,13 +10,11 @@ import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class AdsTrackingTests : TestBase() { - val VMAP_PREROLL_SINGLE_TAG = "https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/ad_rule_samples&ciu_szs=300x250&ad_rule=1&impl=s&gdfp_req=1&env=vp&output=vmap&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ar%3Dpreonly&cmsid=496&vid=short_onecue&correlator=" - @Test fun test_adEventsVmapPrerollSingleAd() { // launch player with autoPlay enabled val metadata = defaultMetadataOverrides() - activityScenario = setupPlayerActivityForTest(autoPlay = true, metadata, VMAP_PREROLL_SINGLE_TAG) + activityScenario = setupPlayerActivityForTest(autoPlay = true, metadata, Sources.Ads.VMAP_PREROLL_SINGLE_TAG) // initialize session and verify initializeSession(activityScenario) diff --git a/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/AdvertisingTests.kt b/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/AdvertisingTests.kt index 1d92632..fb136f3 100644 --- a/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/AdvertisingTests.kt +++ b/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/AdvertisingTests.kt @@ -6,6 +6,12 @@ import androidx.test.platform.app.InstrumentationRegistry import com.bitmovin.analytics.conviva.ConvivaAnalyticsIntegration import com.bitmovin.analytics.conviva.ConvivaConfig import com.bitmovin.analytics.conviva.MetadataOverrides +import com.bitmovin.analytics.conviva.testapp.framework.BITMOVIN_PLAYER_LICENSE_KEY +import com.bitmovin.analytics.conviva.testapp.framework.CONVIVA_CUSTOMER_KEY +import com.bitmovin.analytics.conviva.testapp.framework.CONVIVA_GATEWAY_URL +import com.bitmovin.analytics.conviva.testapp.framework.Sources +import com.bitmovin.analytics.conviva.testapp.framework.expectEvent +import com.bitmovin.analytics.conviva.testapp.framework.postWaiting import com.bitmovin.player.api.PlaybackConfig import com.bitmovin.player.api.Player import com.bitmovin.player.api.PlayerConfig @@ -14,29 +20,11 @@ import com.bitmovin.player.api.advertising.AdSource import com.bitmovin.player.api.advertising.AdSourceType import com.bitmovin.player.api.advertising.AdvertisingConfig import com.bitmovin.player.api.analytics.AnalyticsPlayerConfig -import com.bitmovin.player.api.event.Event -import com.bitmovin.player.api.event.EventEmitter import com.bitmovin.player.api.event.PlayerEvent -import com.bitmovin.player.api.event.on -import com.bitmovin.player.api.source.SourceConfig -import com.bitmovin.player.api.source.SourceType import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import org.junit.Test import org.junit.runner.RunWith -import kotlin.coroutines.resume -import kotlin.coroutines.suspendCoroutine - -private const val BITMOVIN_PLAYER_LICENSE_KEY = "YOUR_LICENSE_KEY" -private const val CONVIVA_CUSTOMER_KEY = "YOUR-CUSTOMER-KEY" -private const val CONVIVA_GATEWAY_URL = "YOUR-GATEWAY-URL" - -private const val VMAP_PREROLL_SINGLE_TAG = "https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/ad_rule_samples&ciu_szs=300x250&ad_rule=1&impl=s&gdfp_req=1&env=vp&output=vmap&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ar%3Dpreonly&cmsid=496&vid=short_onecue&correlator=" -private val ART_OF_MOTION_DASH = SourceConfig( - url = "https://bitmovin-a.akamaihd.net/content/MI201109210084_1/mpds/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.mpd", - type = SourceType.Dash, - title = "Art of Motion Test Stream", -) /** * This test class does not verify any specific behavior, but rather can be used to validate the @@ -59,7 +47,7 @@ class AdvertisingTests { PlayerConfig( key = BITMOVIN_PLAYER_LICENSE_KEY, advertisingConfig = AdvertisingConfig( - AdItem(AdSource(AdSourceType.Ima, VMAP_PREROLL_SINGLE_TAG)), + AdItem(AdSource(AdSourceType.Ima, Sources.Ads.VMAP_PREROLL_SINGLE_TAG)), ), playbackConfig = PlaybackConfig( isAutoplayEnabled = true, @@ -87,7 +75,7 @@ class AdvertisingTests { player } - mainHandler.postWaiting { player.load(ART_OF_MOTION_DASH) } + mainHandler.postWaiting { player.load(Sources.Dash.basic) } player.expectEvent { it.time > 5.0 } // pause player for a second and resume playback @@ -102,32 +90,4 @@ class AdvertisingTests { mainHandler.postWaiting { player.destroy() } runBlocking { delay(1000) } } -} - -/** - * Subscribes to an [Event] on the [Player] and suspends until the event is emitted. - * Optionally a [condition] can be provided to filter the emitted events. - */ -private inline fun EventEmitter.expectEvent( - crossinline condition: (T) -> Boolean = { true } -) = runBlocking { - suspendCoroutine { continuation -> - lateinit var action: ((T) -> Unit) - action = { - if (condition(it)) { - off(action) - continuation.resume(Unit) - } - } - on(action) - } -} - -/** - * Posts a [block] of code to the main thread and suspends until it is executed. - */ -private inline fun Handler.postWaiting(crossinline block: () -> T) = runBlocking { - suspendCoroutine { continuation -> - post { continuation.resume(block()) } - } -} +} \ No newline at end of file diff --git a/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/TestBase.kt b/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/TestBase.kt index 3ff04c8..89bdb58 100644 --- a/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/TestBase.kt +++ b/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/TestBase.kt @@ -8,7 +8,9 @@ import androidx.test.core.app.ApplicationProvider import com.bitmovin.analytics.conviva.ConvivaAnalyticsIntegration import com.bitmovin.analytics.conviva.ConvivaConfig import com.bitmovin.analytics.conviva.MetadataOverrides +import com.bitmovin.analytics.conviva.testapp.framework.Sources import com.bitmovin.player.api.source.Source +import com.bitmovin.player.api.source.SourceBuilder import com.bitmovin.player.api.source.SourceConfig import com.bitmovin.player.api.source.SourceType import com.conviva.sdk.ConvivaAnalytics @@ -32,12 +34,7 @@ open class TestBase { ) as HashMap val CUSTOM_ERROR_MESSAGE = "CUSTOM_ERROR_MESSAGE" - val DEFAULT_DASH_VOD_SOURCE = Source.create( - SourceConfig( - "https://bitmovin-a.akamaihd.net/content/MI201109210084_1/mpds/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.mpd", - SourceType.Dash - ) - ) + val DEFAULT_DASH_VOD_SOURCE = SourceBuilder(Sources.Dash.basic).build() val DEFAULT_DASH_VOD_SOURCE_DURATION = 210 val DEFAULT_DASH_LIVE_SOURCE = Source.create( SourceConfig( diff --git a/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/framework/Sources.kt b/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/framework/Sources.kt new file mode 100644 index 0000000..2db7452 --- /dev/null +++ b/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/framework/Sources.kt @@ -0,0 +1,18 @@ +package com.bitmovin.analytics.conviva.testapp.framework + +import com.bitmovin.player.api.source.SourceConfig +import com.bitmovin.player.api.source.SourceType + +object Sources { + object Ads { + const val VMAP_PREROLL_SINGLE_TAG = "https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/ad_rule_samples&ciu_szs=300x250&ad_rule=1&impl=s&gdfp_req=1&env=vp&output=vmap&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ar%3Dpreonly&cmsid=496&vid=short_onecue&correlator=" + } + + object Dash { + val basic = SourceConfig( + url = "https://bitmovin-a.akamaihd.net/content/MI201109210084_1/mpds/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.mpd", + type = SourceType.Dash, + title = "Art of Motion Test Stream", + ) + } +} \ No newline at end of file diff --git a/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/framework/TestingFramework.kt b/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/framework/TestingFramework.kt new file mode 100644 index 0000000..c27a51f --- /dev/null +++ b/ConvivaTestApp/src/androidTest/java/com/bitmovin/analytics/conviva/testapp/framework/TestingFramework.kt @@ -0,0 +1,43 @@ +package com.bitmovin.analytics.conviva.testapp.framework + +import android.os.Handler +import com.bitmovin.player.api.Player +import com.bitmovin.player.api.event.Event +import com.bitmovin.player.api.event.EventEmitter +import com.bitmovin.player.api.event.on +import kotlinx.coroutines.runBlocking +import kotlin.coroutines.resume +import kotlin.coroutines.suspendCoroutine + +const val BITMOVIN_PLAYER_LICENSE_KEY = "YOUR_LICENSE_KEY" +const val CONVIVA_CUSTOMER_KEY = "YOUR-CUSTOMER-KEY" +const val CONVIVA_GATEWAY_URL = "YOUR-GATEWAY-URL" + + +/** + * Subscribes to an [Event] on the [Player] and suspends until the event is emitted. + * Optionally a [condition] can be provided to filter the emitted events. + */ +inline fun EventEmitter.expectEvent( + crossinline condition: (T) -> Boolean = { true } +) = runBlocking { + suspendCoroutine { continuation -> + lateinit var action: ((T) -> Unit) + action = { + if (condition(it)) { + off(action) + continuation.resume(Unit) + } + } + on(action) + } +} + +/** + * Posts a [block] of code to the main thread and suspends until it is executed. + */ +inline fun Handler.postWaiting(crossinline block: () -> T) = runBlocking { + suspendCoroutine { continuation -> + post { continuation.resume(block()) } + } +}