Skip to content

Commit

Permalink
Extract common test code
Browse files Browse the repository at this point in the history
  • Loading branch information
strangesource committed Jul 2, 2024
1 parent f0b8619 commit a0f1aea
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -87,7 +75,7 @@ class AdvertisingTests {
player
}

mainHandler.postWaiting { player.load(ART_OF_MOTION_DASH) }
mainHandler.postWaiting { player.load(Sources.Dash.basic) }
player.expectEvent<PlayerEvent.TimeChanged> { it.time > 5.0 }

// pause player for a second and resume playback
Expand All @@ -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 <reified T : Event> EventEmitter<Event>.expectEvent(
crossinline condition: (T) -> Boolean = { true }
) = runBlocking {
suspendCoroutine { continuation ->
lateinit var action: ((T) -> Unit)
action = {
if (condition(it)) {
off(action)
continuation.resume(Unit)
}
}
on<T>(action)
}
}

/**
* Posts a [block] of code to the main thread and suspends until it is executed.
*/
private inline fun <T> Handler.postWaiting(crossinline block: () -> T) = runBlocking {
suspendCoroutine { continuation ->
post { continuation.resume(block()) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,12 +34,7 @@ open class TestBase {
) as HashMap<String, String>
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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
)
}
}
Original file line number Diff line number Diff line change
@@ -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 <reified T : Event> EventEmitter<Event>.expectEvent(
crossinline condition: (T) -> Boolean = { true }
) = runBlocking {
suspendCoroutine { continuation ->
lateinit var action: ((T) -> Unit)
action = {
if (condition(it)) {
off(action)
continuation.resume(Unit)
}
}
on<T>(action)
}
}

/**
* Posts a [block] of code to the main thread and suspends until it is executed.
*/
inline fun <T> Handler.postWaiting(crossinline block: () -> T) = runBlocking {
suspendCoroutine { continuation ->
post { continuation.resume(block()) }
}
}

0 comments on commit a0f1aea

Please sign in to comment.