From b9924205bf83dcc556b92568bfd0a76ec4e1fa37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaquim=20St=C3=A4hli?= Date: Tue, 30 Jan 2024 16:54:34 +0100 Subject: [PATCH] Add robolectric analytics tests (#431) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gaƫtan Muller --- build.gradle.kts | 5 +- gradle.properties | 3 + gradle/libs.versions.toml | 5 + pillarbox-analytics/build.gradle.kts | 14 +- .../pillarbox/analytics/TestComScoreSrg.kt | 103 ------------- .../pillarbox/analytics/TestCommandersAct.kt | 61 -------- .../pillarbox/analytics/SRGAnalytics.kt | 2 +- .../commandersact/CommandersActSrg.kt | 39 +++-- .../analytics/comscore/ComScoreSrg.kt | 14 -- .../analytics/SRGAnalyticsSingletonTest.kt} | 28 +++- .../pillarbox/analytics/SRGAnalyticsTest.kt | 31 ++-- .../commandersact/CommandersActSrgTest.kt | 137 +++++++++++++++++ .../analytics/commandersact}/TestUtils.kt | 4 +- .../analytics/comscore/ComScoreSrgTest.kt | 138 ++++++++++++++++++ pillarbox-core-business/build.gradle.kts | 3 + .../core/business/ImageScalingServiceTest.kt | 4 +- .../MediaCompositionMediaItemSourceTest.kt | 25 ++-- .../core/business/ResourceSelectorTest.kt | 54 +++---- .../core/business/TestJsonSerialization.kt | 17 ++- .../akamai}/AkamaiTokenProviderTest.kt | 60 ++++---- .../TestIsPlaybackSpeedPossibleAtPosition.kt | 1 - 21 files changed, 445 insertions(+), 303 deletions(-) delete mode 100644 pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/TestComScoreSrg.kt delete mode 100644 pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/TestCommandersAct.kt rename pillarbox-analytics/src/{androidTest/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsTest.kt => test/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsSingletonTest.kt} (50%) create mode 100644 pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActSrgTest.kt rename pillarbox-analytics/src/{androidTest/java/ch/srgssr/pillarbox/analytics => test/java/ch/srgssr/pillarbox/analytics/commandersact}/TestUtils.kt (75%) create mode 100644 pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/comscore/ComScoreSrgTest.kt rename pillarbox-core-business/src/{androidTest/java/ch/srgssr/pillarbox/core/business => test/java/ch/srgssr/pillarbox/core/business/akamai}/AkamaiTokenProviderTest.kt (57%) rename pillarbox-player/src/{androidTest => test}/java/ch/srgssr/pillarbox/player/TestIsPlaybackSpeedPossibleAtPosition.kt (99%) diff --git a/build.gradle.kts b/build.gradle.kts index a7b48b9ac..a4e1981ed 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -84,8 +84,11 @@ dependencyAnalysis { } } - // Required because of https://github.com/autonomousapps/dependency-analysis-gradle-plugin/issues/892 structure { + // https://github.com/autonomousapps/dependency-analysis-gradle-plugin/wiki/Customizing-plugin-behavior + ignoreKtx(true) // default is false + + // Required because of https://github.com/autonomousapps/dependency-analysis-gradle-plugin/issues/892 bundle("kotlin-test") { includeDependency(libs.kotlin.test) } diff --git a/gradle.properties b/gradle.properties index 05f904976..81879c2e1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,6 +19,9 @@ org.gradle.parallel=true # https://developer.android.com/topic/libraries/support-library/androidx-rn android.useAndroidX=true +# https://github.com/autonomousapps/dependency-analysis-gradle-plugin/issues/1079#issuecomment-1862266603 +dependency.analysis.test.analysis=false + # Kotlin code style for this project: "official" or "obsolete": kotlin.code.style=official diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 22cacfa06..9e197ebf2 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -12,6 +12,7 @@ androidx-media = "1.7.0" androidx-media3 = "1.2.1" androidx-navigation = "2.7.6" androidx-paging = "3.2.1" +androidx-test-core = "1.5.0" androidx-test-ext-junit = "1.1.5" androidx-test-monitor = "1.6.1" androidx-test-runner = "1.5.2" @@ -31,6 +32,7 @@ kotlinx-serialization = "1.6.2" ktor = "2.3.7" mockk = "1.13.9" okhttp = "4.12.0" +robolectric = "4.11" srg-data-provider = "0.8.0" tag-commander-core = "5.4.2" tag-commander-server-side = "5.5.2" @@ -56,6 +58,7 @@ androidx-navigation-runtime = { module = "androidx.navigation:navigation-runtime androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "androidx-navigation" } androidx-paging-common = { module = "androidx.paging:paging-common", version.ref = "androidx-paging" } androidx-paging-compose = { module = "androidx.paging:paging-compose", version.ref = "androidx-paging" } +androidx-test-core = { module = "androidx.test:core-ktx", version.ref = "androidx-test-core" } androidx-test-monitor = { module = "androidx.test:monitor", version.ref = "androidx-test-monitor" } androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test-runner" } androidx-tv-foundation = { module = "androidx.tv:tv-foundation", version.ref = "androidx-tv" } @@ -74,6 +77,7 @@ ktor-http = { module = "io.ktor:ktor-http", version.ref = "ktor" } ktor-serialization = { module = "io.ktor:ktor-serialization", version.ref = "ktor" } ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" } ktor-utils = { module = "io.ktor:ktor-utils", version.ref = "ktor" } +robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" } srg-data = { module = "ch.srg.data.provider:data", version.ref = "srg-data-provider" } srg-dataprovider-paging = { module = "ch.srg.data.provider:dataprovider-paging", version.ref = "srg-data-provider" } srg-dataprovider-retrofit = { module = "ch.srg.data.provider:dataprovider-retrofit", version.ref = "srg-data-provider" } @@ -121,6 +125,7 @@ androidx-compose-runtime = { module = "androidx.compose.runtime:runtime" } androidx-compose-runtime-saveable = { module = "androidx.compose.runtime:runtime-saveable" } leanback = { group = "androidx.leanback", name = "leanback", version.ref = "androidx-leanback" } androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" } +androidx-test-ext-junit-ktx = { group = "androidx.test.ext", name = "junit-ktx", version.ref = "androidx-test-ext-junit" } guava = { module = "com.google.guava:guava", version.ref = "guava" } turbine = { module = "app.cash.turbine:turbine", version.ref = "turbine" } diff --git a/pillarbox-analytics/build.gradle.kts b/pillarbox-analytics/build.gradle.kts index eb5ef7d5a..b6e328946 100644 --- a/pillarbox-analytics/build.gradle.kts +++ b/pillarbox-analytics/build.gradle.kts @@ -50,6 +50,11 @@ android { withJavadocJar() } } + testOptions { + unitTests { + isIncludeAndroidResources = true + } + } } dependencies { @@ -57,6 +62,8 @@ dependencies { implementation(libs.tagcommander.core) api(libs.tagcommander.serverside) + testImplementation(libs.androidx.test.core) + testImplementation(libs.androidx.test.ext.junit.ktx) testImplementation(libs.json) { because("The 'org.json' package is included in the Android SDK. Adding this dependency allows us to not mock the Android SDK in unit tests.") } @@ -64,12 +71,7 @@ dependencies { testImplementation(libs.kotlin.test) testImplementation(libs.mockk) testImplementation(libs.mockk.dsl) - - androidTestImplementation(libs.androidx.test.monitor) - androidTestRuntimeOnly(libs.androidx.test.runner) - androidTestImplementation(libs.junit) - androidTestImplementation(libs.kotlinx.coroutines.core) - androidTestImplementation(libs.kotlinx.coroutines.test) + testImplementation(libs.robolectric) } kover { diff --git a/pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/TestComScoreSrg.kt b/pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/TestComScoreSrg.kt deleted file mode 100644 index 2742221f3..000000000 --- a/pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/TestComScoreSrg.kt +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (c) SRG SSR. All rights reserved. - * License information is available from the LICENSE file. - */ -package ch.srgssr.pillarbox.analytics - -import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation -import ch.srgssr.pillarbox.analytics.comscore.ComScoreLabel -import ch.srgssr.pillarbox.analytics.comscore.ComScorePageView -import ch.srgssr.pillarbox.analytics.comscore.ComScoreSrg -import ch.srgssr.pillarbox.analytics.comscore.ComScoreUserConsent -import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.flow.MutableSharedFlow -import kotlinx.coroutines.flow.take -import kotlinx.coroutines.flow.toList -import kotlinx.coroutines.launch -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.runTest -import org.junit.Assert -import org.junit.Before -import org.junit.Test - -@OptIn(ExperimentalCoroutinesApi::class) -class TestComScoreSrg { - - private lateinit var comScore: ComScoreSrg - private lateinit var dispatcher: CoroutineDispatcher - - @Before - fun setup() { - dispatcher = UnconfinedTestDispatcher() - val appContext = getInstrumentation().targetContext.applicationContext - comScore = ComScoreSrg.init(config = TestUtils.analyticsConfig, context = appContext) - comScore.start(appContext) // Simulate Activity start - } - - @Test - fun testSendPageView() = runTest { - val tracker = PageViewTracking() - comScore.debugListener = tracker - val pageTitle = "Title" - val pageView = ComScorePageView(name = pageTitle) - val actualLabels = ArrayList>() - val expectedLabels = listOf(mapOf(Pair(ComScoreLabel.C8.label, pageTitle))) - val job = launch(dispatcher) { - tracker.pageViewFlow.take(1).toList(actualLabels) - } - comScore.sendPageView(pageView) - Assert.assertEquals(expectedLabels, actualLabels) - job.cancel() - } - - @Test - fun testSendPageViewWithLabels() = runTest { - val tracker = PageViewTracking() - comScore.debugListener = tracker - val pageTitle = "Title" - val labels = mapOf(Pair("key1", "value01"), Pair("key2", " ")) - val pageView = ComScorePageView(name = pageTitle, labels = labels) - val actualLabels = ArrayList>() - val expectedLabels = listOf(mapOf(Pair(ComScoreLabel.C8.label, pageTitle), Pair("key1", "value01"))) - val job = launch(dispatcher) { - tracker.pageViewFlow.take(1).toList(actualLabels) - } - comScore.sendPageView(pageView) - Assert.assertEquals(expectedLabels, actualLabels) - job.cancel() - } - - @Test - fun testUserConsentGiven() { - val userConsent = ComScoreUserConsent.ACCEPTED - val expectedLabel = "1" - comScore.setUserConsent(userConsent) - Assert.assertEquals(expectedLabel, comScore.getPersistentLabel(ComScoreLabel.CS_UC_FR.label)) - } - - @Test - fun testUserConsentNotGiven() { - val userConsent = ComScoreUserConsent.DECLINED - val expectedLabel = "0" - comScore.setUserConsent(userConsent) - Assert.assertEquals(expectedLabel, comScore.getPersistentLabel(ComScoreLabel.CS_UC_FR.label)) - } - - @Test - fun testUserConsentUnknown() { - val userConsent = ComScoreUserConsent.UNKNOWN - val expectedLabel = "" - comScore.setUserConsent(userConsent) - Assert.assertEquals(expectedLabel, comScore.getPersistentLabel(ComScoreLabel.CS_UC_FR.label)) - } - - private class PageViewTracking : ComScoreSrg.DebugListener { - val pageViewFlow = MutableSharedFlow>(extraBufferCapacity = 1, replay = 1) - - override fun onPageViewSend(labels: Map) { - Assert.assertTrue(pageViewFlow.tryEmit(labels)) - } - - } -} diff --git a/pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/TestCommandersAct.kt b/pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/TestCommandersAct.kt deleted file mode 100644 index dfd835d2f..000000000 --- a/pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/TestCommandersAct.kt +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) SRG SSR. All rights reserved. - * License information is available from the LICENSE file. - */ -package ch.srgssr.pillarbox.analytics - -import androidx.test.platform.app.InstrumentationRegistry -import ch.srgssr.pillarbox.analytics.commandersact.CommandersActEvent -import ch.srgssr.pillarbox.analytics.commandersact.CommandersActLabels -import ch.srgssr.pillarbox.analytics.commandersact.CommandersActPageView -import ch.srgssr.pillarbox.analytics.commandersact.CommandersActSrg -import com.tagcommander.lib.core.TCUser -import com.tagcommander.lib.serverside.TCPredefinedVariables -import com.tagcommander.lib.serverside.schemas.TCDevice -import org.junit.Assert -import org.junit.Before -import org.junit.Test - -class TestCommandersAct { - - private lateinit var commandersAct: CommandersActSrg - - @Before - fun setup() { - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - commandersAct = CommandersActSrg(config = TestUtils.analyticsConfig, appContext = appContext) - } - - @Test - fun testSendEvent() { - commandersAct.sendEvent(CommandersActEvent(name = "Event1")) - Assert.assertTrue(true) - } - - @Test - fun testSendPageViewEvent() { - commandersAct.sendPageView( - CommandersActPageView( - name = "PageTitle1", - type = "UnitTest", - levels = listOf("pillarbox", "unit-test") - ) - ) - Assert.assertTrue(true) - } - - @Test - fun testConsentServices() { - val services = listOf("service1", "service2") - val expected = "service1,service2" - commandersAct.setConsentServices(services) - Assert.assertEquals(expected, commandersAct.getPermanentDataLabel(CommandersActLabels.CONSENT_SERVICES.label)) - } - - @Test - fun testLegacyUniqueId() { - val legacyUniqueId: String = TCPredefinedVariables.getInstance().uniqueIdentifier - Assert.assertEquals(legacyUniqueId, TCDevice.getInstance().sdkID) - Assert.assertEquals(legacyUniqueId, TCUser.getInstance().anonymous_id) - } -} diff --git a/pillarbox-analytics/src/main/java/ch/srgssr/pillarbox/analytics/SRGAnalytics.kt b/pillarbox-analytics/src/main/java/ch/srgssr/pillarbox/analytics/SRGAnalytics.kt index 8e0265642..2b80bcfdd 100644 --- a/pillarbox-analytics/src/main/java/ch/srgssr/pillarbox/analytics/SRGAnalytics.kt +++ b/pillarbox-analytics/src/main/java/ch/srgssr/pillarbox/analytics/SRGAnalytics.kt @@ -190,8 +190,8 @@ object SRGAnalytics { } fun removePersistentLabel(label: String) { - comScore.removePersistentLabel(label) commandersAct.removePermanentData(label) + comScore.removePersistentLabel(label) } fun getComScorePersistentLabel(label: String): String? { diff --git a/pillarbox-analytics/src/main/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActSrg.kt b/pillarbox-analytics/src/main/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActSrg.kt index 04dcba52a..6aad567f3 100644 --- a/pillarbox-analytics/src/main/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActSrg.kt +++ b/pillarbox-analytics/src/main/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActSrg.kt @@ -19,35 +19,19 @@ import com.tagcommander.lib.serverside.schemas.TCDevice /** * CommandersAct for SRG SSR * + * @param tcServerSide TCServiceSide to use. * @param config analytics config. - * @param appContext application context. + * @param navigationDevice The navigation device. * * @constructor */ internal class CommandersActSrg( + private val tcServerSide: TCServerSide, private val config: AnalyticsConfig, - appContext: Context -) : - CommandersAct { - - /** - * Debug listener for testing purpose. - */ - interface DebugListener { - /** - * On event sent - * - * @param event sent to [tcServerSide] - */ - fun onEventSent(event: TCEvent) - } - - private val tcServerSide: TCServerSide + navigationDevice: String +) : CommandersAct { init { - tcServerSide = TCServerSide(SITE_SRG, config.sourceKey, appContext).also { - workaroundUniqueIdV4Tov5() - } TCDebug.setDebugLevel(if (BuildConfig.DEBUG) Log.DEBUG else Log.INFO) config.commandersActPersistentLabels?.let { @@ -57,7 +41,18 @@ internal class CommandersActSrg( // Data send with all events that never change tcServerSide.addPermanentData(APP_LIBRARY_VERSION, "${BuildConfig.VERSION_NAME} ${BuildConfig.BUILD_DATE}") tcServerSide.addPermanentData(NAVIGATION_APP_SITE_NAME, config.appSiteName) - tcServerSide.addPermanentData(NAVIGATION_DEVICE, appContext.getString(R.string.tc_analytics_device)) + tcServerSide.addPermanentData(NAVIGATION_DEVICE, navigationDevice) + } + + constructor( + config: AnalyticsConfig, + appContext: Context + ) : this( + tcServerSide = TCServerSide(SITE_SRG, config.sourceKey, appContext), + config = config, + navigationDevice = appContext.getString(R.string.tc_analytics_device) + ) { + workaroundUniqueIdV4Tov5() } override fun sendPageView(pageView: CommandersActPageView) { diff --git a/pillarbox-analytics/src/main/java/ch/srgssr/pillarbox/analytics/comscore/ComScoreSrg.kt b/pillarbox-analytics/src/main/java/ch/srgssr/pillarbox/analytics/comscore/ComScoreSrg.kt index 4cf2d6c5c..313b557cd 100644 --- a/pillarbox-analytics/src/main/java/ch/srgssr/pillarbox/analytics/comscore/ComScoreSrg.kt +++ b/pillarbox-analytics/src/main/java/ch/srgssr/pillarbox/analytics/comscore/ComScoreSrg.kt @@ -24,22 +24,9 @@ import java.util.concurrent.atomic.AtomicBoolean * SRGSSR doc : https://confluence.srg.beecollaboration.com/pages/viewpage.action?pageId=13188965 */ internal object ComScoreSrg : ComScore { - /** - * Debug listener for testing purpose. - */ - interface DebugListener { - /** - * On page view send - * - * @param labels send to [Analytics.notifyViewEvent] - */ - fun onPageViewSend(labels: Map) - } - private var config: AnalyticsConfig? = null private const val publisherId = "6036016" private val started = AtomicBoolean(false) - internal var debugListener: DebugListener? = null /** * Init ComScore if [context] is an [Activity] we init ComScpre directly otherwise we start it when an [Activity] as been created. @@ -105,7 +92,6 @@ internal object ComScoreSrg : ComScore { checkInitialized() if (!started.get()) return with(pageView.toLabels()) { - debugListener?.onPageViewSend(this) Analytics.notifyViewEvent(this) } } diff --git a/pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsTest.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsSingletonTest.kt similarity index 50% rename from pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsTest.kt rename to pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsSingletonTest.kt index 9be5cc532..517cef651 100644 --- a/pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsTest.kt +++ b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsSingletonTest.kt @@ -5,10 +5,19 @@ package ch.srgssr.pillarbox.analytics import android.app.Application -import androidx.test.platform.app.InstrumentationRegistry -import org.junit.Test +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.comscore.Analytics +import io.mockk.mockkStatic +import io.mockk.unmockkAll +import org.junit.runner.RunWith +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test -class SRGAnalyticsTest { +@RunWith(AndroidJUnit4::class) +class SRGAnalyticsSingletonTest { private val config = AnalyticsConfig( vendor = AnalyticsConfig.Vendor.SRG, @@ -16,11 +25,20 @@ class SRGAnalyticsTest { sourceKey = AnalyticsConfig.SOURCE_KEY_SRG_DEBUG ) + @BeforeTest + fun setup() { + mockkStatic(Analytics::class) + } + + @AfterTest + fun tearDown() { + unmockkAll() + } + @Test(expected = IllegalArgumentException::class) fun testInitTwice() { - val appContext = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext + val appContext: Context = ApplicationProvider.getApplicationContext() SRGAnalytics.init(appContext as Application, config) SRGAnalytics.init(appContext, config.copy(vendor = AnalyticsConfig.Vendor.RSI)) } - } diff --git a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsTest.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsTest.kt index 676a6361f..a5bba2226 100644 --- a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsTest.kt +++ b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsTest.kt @@ -10,9 +10,9 @@ import ch.srgssr.pillarbox.analytics.commandersact.CommandersActPageView import ch.srgssr.pillarbox.analytics.comscore.ComScore import ch.srgssr.pillarbox.analytics.comscore.ComScorePageView import io.mockk.Called +import io.mockk.confirmVerified import io.mockk.mockk import io.mockk.verify -import io.mockk.verifySequence import kotlin.test.BeforeTest import kotlin.test.Test @@ -45,10 +45,11 @@ class SRGAnalyticsTest { comScore = comScorePageView, ) - verifySequence { + verify(exactly = 1) { commandersAct.sendPageView(commandersActPageView) comScore.sendPageView(comScorePageView) } + confirmVerified(comScore, commandersAct) } @Test @@ -57,12 +58,12 @@ class SRGAnalyticsTest { analytics.sendEvent(commandersActEvent) - verifySequence { + verify(exactly = 1) { commandersAct.sendEvent(commandersActEvent) - } - verify { comScore wasNot Called } + + confirmVerified(comScore, commandersAct) } @Test @@ -75,10 +76,11 @@ class SRGAnalyticsTest { comScoreLabels = comScoreLabels, ) - verifySequence { + verify(exactly = 1) { comScore.putPersistentLabels(comScoreLabels) commandersAct.putPermanentData(commandersActLabels) } + confirmVerified(comScore, commandersAct) } @Test @@ -87,10 +89,12 @@ class SRGAnalyticsTest { analytics.removePersistentLabel(label = label) - verifySequence { + verify(exactly = 1) { comScore.removePersistentLabel(label) commandersAct.removePermanentData(label) } + + confirmVerified(comScore, commandersAct) } @Test @@ -99,12 +103,11 @@ class SRGAnalyticsTest { analytics.getComScorePersistentLabel(label = label) - verifySequence { + verify(exactly = 1) { comScore.getPersistentLabel(label) - } - verify { commandersAct wasNot Called } + confirmVerified(comScore, commandersAct) } @Test @@ -113,12 +116,11 @@ class SRGAnalyticsTest { analytics.getCommandersActPermanentData(label = label) - verifySequence { + verify(exactly = 1) { commandersAct.getPermanentDataLabel(label) - } - verify { comScore wasNot Called } + confirmVerified(comScore, commandersAct) } @Test @@ -127,9 +129,10 @@ class SRGAnalyticsTest { analytics.setUserConsent(userConsent) - verifySequence { + verify(exactly = 1) { comScore.setUserConsent(userConsent.comScore) commandersAct.setConsentServices(userConsent.commandersActConsentServices) } + confirmVerified(comScore, commandersAct) } } diff --git a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActSrgTest.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActSrgTest.kt new file mode 100644 index 000000000..6a9db81ca --- /dev/null +++ b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActSrgTest.kt @@ -0,0 +1,137 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ +package ch.srgssr.pillarbox.analytics.commandersact + +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.tagcommander.lib.core.TCUser +import com.tagcommander.lib.serverside.TCPredefinedVariables +import com.tagcommander.lib.serverside.TCServerSide +import com.tagcommander.lib.serverside.events.base.TCEvent +import com.tagcommander.lib.serverside.schemas.TCDevice +import io.mockk.mockk +import io.mockk.slot +import io.mockk.verify +import org.junit.runner.RunWith +import org.robolectric.annotation.Config +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull +import kotlin.test.assertTrue + +@RunWith(AndroidJUnit4::class) +class CommandersActSrgTest { + + private val analyticsConfig = TestUtils.analyticsConfig + + private val commandersAct: CommandersActSrg by lazy { + CommandersActSrg(config = analyticsConfig, appContext = ApplicationProvider.getApplicationContext()) + } + + @Test + @Config(qualifiers = "television") + fun `navigation device is tvbox`() { + val actual = commandersAct.getPermanentDataLabel("navigation_device") + assertEquals("tvbox", actual) + } + + @Test + @Config + fun `navigation device is phone`() { + val actual = commandersAct.getPermanentDataLabel("navigation_device") + assertEquals("phone", actual) + } + + @Test + @Config(qualifiers = "sw600dp") + fun `test navigation device is tablet`() { + val actual = commandersAct.getPermanentDataLabel("navigation_device") + assertEquals("tablet", actual) + } + + @Test + @Config(qualifiers = "car") + fun `navigation device is auto`() { + val actual = commandersAct.getPermanentDataLabel("navigation_device") + assertEquals("auto", actual) + } + + @Test + fun `sendEvent() with CommandersActEvent`() { + val serverSide = mockk(relaxed = true) + val commandersAct = CommandersActSrg(tcServerSide = serverSide, config = analyticsConfig, "tests") + val eventSlot = slot() + + commandersAct.sendEvent(CommandersActEvent(name = "dummy")) + verify(exactly = 1) { + serverSide.execute(capture(eventSlot)) + } + + assertTrue(eventSlot.isCaptured) + assertEquals(eventSlot.captured.name, "dummy") + } + + @Test + fun `sendPageView() with CommandersActPageView`() { + val serverSide = mockk(relaxed = true) + val commandersAct = CommandersActSrg(tcServerSide = serverSide, config = analyticsConfig, "tests") + val eventSlot = slot() + + commandersAct.sendPageView( + CommandersActPageView( + name = "PageTitle1", + type = "UnitTest", + levels = listOf("pillarbox", "unit-test") + ) + ) + + verify(exactly = 1) { + serverSide.execute(capture(eventSlot)) + } + assertTrue(eventSlot.isCaptured) + val capturedEvent = eventSlot.captured + assertEquals("page_view", capturedEvent.name) + assertEquals("PageTitle1", capturedEvent.pageName) + assertEquals("UnitTest", capturedEvent.pageType) + assertEquals("pillarbox", capturedEvent.additionalProperties["navigation_level_1"]) + assertEquals("unit-test", capturedEvent.additionalProperties["navigation_level_2"]) + assertNull(capturedEvent.additionalProperties["navigation_level_3"]) + } + + @Test + fun `sendTcMediaEvent() with TCMediaEvent`() { + val serverSide = mockk(relaxed = true) + val commandersAct = CommandersActSrg(tcServerSide = serverSide, config = analyticsConfig, "tests") + val eventSlot = slot() + + commandersAct.sendTcMediaEvent(TCMediaEvent(eventType = MediaEventType.Eof, assets = emptyMap())) + verify(exactly = 1) { + serverSide.execute(capture(eventSlot)) + } + + assertTrue(eventSlot.isCaptured) + assertEquals("eof", eventSlot.captured.name) + } + + @Test + fun `initial consent services`() { + assertNull(commandersAct.getPermanentDataLabel(CommandersActLabels.CONSENT_SERVICES.label)) + } + + @Test + fun `set consent services`() { + val services = listOf("service1", "service2") + val expected = "service1,service2" + commandersAct.setConsentServices(services) + assertEquals(expected, commandersAct.getPermanentDataLabel(CommandersActLabels.CONSENT_SERVICES.label)) + } + + @Test + fun `legacyUniqueID is used for sdkID and anonymous_id`() { + val legacyUniqueId: String = TCPredefinedVariables.getInstance().uniqueIdentifier + assertEquals(legacyUniqueId, TCDevice.getInstance().sdkID) + assertEquals(legacyUniqueId, TCUser.getInstance().anonymous_id) + } +} diff --git a/pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/TestUtils.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/TestUtils.kt similarity index 75% rename from pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/TestUtils.kt rename to pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/TestUtils.kt index 94bb52279..c90277507 100644 --- a/pillarbox-analytics/src/androidTest/java/ch/srgssr/pillarbox/analytics/TestUtils.kt +++ b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/TestUtils.kt @@ -2,7 +2,9 @@ * Copyright (c) SRG SSR. All rights reserved. * License information is available from the LICENSE file. */ -package ch.srgssr.pillarbox.analytics +package ch.srgssr.pillarbox.analytics.commandersact + +import ch.srgssr.pillarbox.analytics.AnalyticsConfig object TestUtils { val analyticsConfig = AnalyticsConfig( diff --git a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/comscore/ComScoreSrgTest.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/comscore/ComScoreSrgTest.kt new file mode 100644 index 000000000..d5386024f --- /dev/null +++ b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/comscore/ComScoreSrgTest.kt @@ -0,0 +1,138 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ +package ch.srgssr.pillarbox.analytics.comscore + +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import ch.srgssr.pillarbox.analytics.AnalyticsConfig +import com.comscore.Analytics +import com.comscore.PublisherConfiguration +import io.mockk.confirmVerified +import io.mockk.every +import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.unmockkAll +import io.mockk.verify +import org.junit.runner.RunWith +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test + +@RunWith(AndroidJUnit4::class) +class ComScoreSrgTest { + + private val config = AnalyticsConfig( + vendor = AnalyticsConfig.Vendor.SRG, + appSiteName = "pillarbox-test-android", + sourceKey = AnalyticsConfig.SOURCE_KEY_SRG_DEBUG + ) + + @BeforeTest + fun setup() { + mockkStatic(Analytics::class) + ComScoreSrg.init(config = config, context = ApplicationProvider.getApplicationContext()) + } + + @AfterTest + fun tearDown() { + unmockkAll() + } + + @Test(expected = IllegalArgumentException::class) + fun `init a second time with other config should throw exception`() { + ComScoreSrg.init(config = config.copy(vendor = AnalyticsConfig.Vendor.RTS), context = ApplicationProvider.getApplicationContext()) + } + + @Test + fun `put persistent labels`() { + val config = mockk(relaxed = true) + every { Analytics.getConfiguration().getPublisherConfiguration(any()) } returns config + val labels = mapOf("a" to "b", "c" to "d") + ComScoreSrg.putPersistentLabels(labels) + verify(exactly = 1) { + config.addPersistentLabels(labels) + } + confirmVerified(config) + } + + @Test + fun `set user consent`() { + val config = mockk(relaxed = true) + every { Analytics.getConfiguration().getPublisherConfiguration(any()) } returns config + + ComScoreSrg.setUserConsent(ComScoreUserConsent.ACCEPTED) + ComScoreSrg.setUserConsent(ComScoreUserConsent.DECLINED) + ComScoreSrg.setUserConsent(ComScoreUserConsent.UNKNOWN) + + verify(exactly = 1) { + config.addPersistentLabels(mapOf(ComScoreLabel.CS_UC_FR.label to "1")) + config.addPersistentLabels(mapOf(ComScoreLabel.CS_UC_FR.label to "0")) + config.addPersistentLabels(mapOf(ComScoreLabel.CS_UC_FR.label to "")) + } + confirmVerified(config) + } + + @Test + fun `put persistent label`() { + val config = mockk(relaxed = true) + every { Analytics.getConfiguration().getPublisherConfiguration(any()) } returns config + + ComScoreSrg.putPersistentLabels(mapOf("label1" to "value1")) + + verify(exactly = 1) { + config.addPersistentLabels(mapOf("label1" to "value1")) + } + confirmVerified(config) + } + + @Test + fun `get persistent label`() { + val config = mockk(relaxed = true) + every { Analytics.getConfiguration().getPublisherConfiguration(any()) } returns config + + ComScoreSrg.getPersistentLabel("label1") + ComScoreSrg.getPersistentLabel(ComScoreLabel.CS_UC_FR.label) + + verify(exactly = 1) { + config.getPersistentLabel("label1") + config.getPersistentLabel(ComScoreLabel.CS_UC_FR.label) + } + confirmVerified(config) + } + + @Test + fun `remove persistent label`() { + val config = mockk(relaxed = true) + every { Analytics.getConfiguration().getPublisherConfiguration(any()) } returns config + + ComScoreSrg.removePersistentLabel("label1") + ComScoreSrg.removePersistentLabel(ComScoreLabel.CS_UC_FR.label) + + verify(exactly = 1) { + config.removePersistentLabel("label1") + config.removePersistentLabel(ComScoreLabel.CS_UC_FR.label) + } + confirmVerified(config) + } + + @Test + fun `send page view with no labels`() { + ComScoreSrg.sendPageView(ComScorePageView("page")) + val expectedLabels = mapOf(ComScoreLabel.C8.label to "page") + verify(exactly = 1) { + Analytics.notifyViewEvent(expectedLabels) + } + } + + @Test + fun `send page view with labels`() { + val labels = mapOf("key01" to "value01", "key02" to "value02") + ComScoreSrg.sendPageView(ComScorePageView("page", labels = labels)) + val expectedLabels = mapOf(ComScoreLabel.C8.label to "page", "key01" to "value01", "key02" to "value02") + verify(exactly = 1) { + Analytics.notifyViewEvent(expectedLabels) + } + } +} diff --git a/pillarbox-core-business/build.gradle.kts b/pillarbox-core-business/build.gradle.kts index 8b9ed0c34..017041609 100644 --- a/pillarbox-core-business/build.gradle.kts +++ b/pillarbox-core-business/build.gradle.kts @@ -76,12 +76,15 @@ dependencies { implementation(libs.okhttp.logging.interceptor) api(libs.tagcommander.core) + testImplementation(libs.androidx.test.core) + testImplementation(libs.androidx.test.ext.junit.ktx) testImplementation(libs.junit) testImplementation(libs.kotlin.test) testImplementation(libs.kotlinx.coroutines.test) testImplementation(libs.ktor.client.mock) testImplementation(libs.mockk) testImplementation(libs.mockk.dsl) + testImplementation(libs.robolectric) androidTestImplementation(project(":pillarbox-player-testutils")) diff --git a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/ImageScalingServiceTest.kt b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/ImageScalingServiceTest.kt index f1689a405..2dab8f92f 100644 --- a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/ImageScalingServiceTest.kt +++ b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/ImageScalingServiceTest.kt @@ -6,9 +6,9 @@ package ch.srgssr.pillarbox.core.business import ch.srgssr.pillarbox.core.business.MediaCompositionMediaItemSource.ImageScalingService import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlHost -import org.junit.Assert.assertEquals -import org.junit.Test import java.net.URLEncoder +import kotlin.test.Test +import kotlin.test.assertEquals class ImageScalingServiceTest { @Test diff --git a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/MediaCompositionMediaItemSourceTest.kt b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/MediaCompositionMediaItemSourceTest.kt index c25c77e02..f8e45c465 100644 --- a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/MediaCompositionMediaItemSourceTest.kt +++ b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/MediaCompositionMediaItemSourceTest.kt @@ -15,8 +15,10 @@ import ch.srgssr.pillarbox.core.business.integrationlayer.data.Resource import ch.srgssr.pillarbox.core.business.integrationlayer.data.Segment import ch.srgssr.pillarbox.core.business.integrationlayer.service.MediaCompositionDataSource import kotlinx.coroutines.runBlocking -import org.junit.Assert -import org.junit.Test +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertTrue class MediaCompositionMediaItemSourceTest { @@ -51,20 +53,21 @@ class MediaCompositionMediaItemSourceTest { @Test fun testCompatibleResource() = runBlocking { val mediaItem = mediaItemSource.loadMediaItem(createMediaItem(DummyMediaCompositionProvider.URN_HLS_RESOURCE)) - Assert.assertNotNull(mediaItem) + assertNotNull(mediaItem) + Unit } @Test fun testMetadata() = runBlocking { val mediaItem = mediaItemSource.loadMediaItem(createMediaItem(DummyMediaCompositionProvider.URN_METADATA)) - Assert.assertNotNull(mediaItem) + assertNotNull(mediaItem) val metadata = mediaItem.mediaMetadata val expected = MediaMetadata.Builder() .setTitle("Title") .setSubtitle("Lead") .setDescription("Description") .build() - Assert.assertEquals(expected, metadata) + assertEquals(expected, metadata) } @Test @@ -75,10 +78,10 @@ class MediaCompositionMediaItemSourceTest { .setDescription("CustomDescription") .build() val mediaItem = mediaItemSource.loadMediaItem(createMediaItem(DummyMediaCompositionProvider.URN_METADATA, input)) - Assert.assertNotNull(mediaItem) + assertNotNull(mediaItem) val metadata = mediaItem.mediaMetadata val expected = input.buildUpon().build() - Assert.assertEquals(expected, metadata) + assertEquals(expected, metadata) } @Test @@ -87,28 +90,28 @@ class MediaCompositionMediaItemSourceTest { .setTitle("CustomTitle") .build() val mediaItem = mediaItemSource.loadMediaItem(createMediaItem(DummyMediaCompositionProvider.URN_METADATA, input)) - Assert.assertNotNull(mediaItem) + assertNotNull(mediaItem) val metadata = mediaItem.mediaMetadata val expected = MediaMetadata.Builder() .setTitle("CustomTitle") .setSubtitle("Lead") .setDescription("Description") .build() - Assert.assertEquals(expected, metadata) + assertEquals(expected, metadata) } @Test(expected = BlockReasonException::class) fun testBlockReason() = runBlocking { val input = MediaMetadata.Builder().build() mediaItemSource.loadMediaItem(createMediaItem(DummyMediaCompositionProvider.URN_BLOCK_REASON, input)) - Assert.assertTrue(false) + assertTrue(false) } @Test(expected = BlockReasonException::class) fun testBlockedSegment() = runBlocking { val input = MediaMetadata.Builder().build() mediaItemSource.loadMediaItem(createMediaItem(DummyMediaCompositionProvider.URN_SEGMENT_BLOCK_REASON, input)) - Assert.assertTrue(false) + assertTrue(false) } internal class DummyMediaCompositionProvider : MediaCompositionDataSource { diff --git a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/ResourceSelectorTest.kt b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/ResourceSelectorTest.kt index 901204eaa..1a9c54624 100644 --- a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/ResourceSelectorTest.kt +++ b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/ResourceSelectorTest.kt @@ -7,8 +7,10 @@ package ch.srgssr.pillarbox.core.business import ch.srgssr.pillarbox.core.business.integrationlayer.data.Chapter import ch.srgssr.pillarbox.core.business.integrationlayer.data.Drm import ch.srgssr.pillarbox.core.business.integrationlayer.data.Resource -import org.junit.Assert -import org.junit.Test +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull class ResourceSelectorTest { @@ -18,14 +20,14 @@ class ResourceSelectorTest { fun testNull() { val chapter = createChapter(null) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNull(result) + assertNull(result) } @Test fun testEmptyList() { val chapter = createChapter(emptyList()) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNull(result) + assertNull(result) } @Test @@ -39,7 +41,7 @@ class ResourceSelectorTest { ) ) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNull(result) + assertNull(result) } @Test @@ -55,8 +57,8 @@ class ResourceSelectorTest { ) ) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNotNull(result) - Assert.assertEquals(createResource(type), result) + assertNotNull(result) + assertEquals(createResource(type), result) } @Test @@ -72,8 +74,8 @@ class ResourceSelectorTest { ) ) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNotNull(result) - Assert.assertEquals(createResource(type), result) + assertNotNull(result) + assertEquals(createResource(type), result) } @Test @@ -89,8 +91,8 @@ class ResourceSelectorTest { ) ) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNotNull(result) - Assert.assertEquals(createResource(type), result) + assertNotNull(result) + assertEquals(createResource(type), result) } @Test @@ -108,8 +110,8 @@ class ResourceSelectorTest { ) ) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNotNull(result) - Assert.assertEquals(createResource(type), result) + assertNotNull(result) + assertEquals(createResource(type), result) } @Test @@ -127,8 +129,8 @@ class ResourceSelectorTest { ) ) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNotNull(result) - Assert.assertEquals(createResource(type), result) + assertNotNull(result) + assertEquals(createResource(type), result) } @Test @@ -146,15 +148,15 @@ class ResourceSelectorTest { ) ) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNotNull(result) - Assert.assertEquals(createResource(type), result) + assertNotNull(result) + assertEquals(createResource(type), result) } @Test fun testUnsupportedDrmOnly() { val chapter = createChapter(listOf(createUnsupportedDrmResource())) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNull(result) + assertNull(result) } @Test @@ -167,8 +169,8 @@ class ResourceSelectorTest { ) ) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNotNull(result) - Assert.assertEquals(createResource(type), result) + assertNotNull(result) + assertEquals(createResource(type), result) } @Test @@ -179,8 +181,8 @@ class ResourceSelectorTest { ) ) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNotNull(result) - Assert.assertEquals(createSupportedDrmResource(), result) + assertNotNull(result) + assertEquals(createSupportedDrmResource(), result) } @Test @@ -193,8 +195,8 @@ class ResourceSelectorTest { ) ) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNotNull(result) - Assert.assertEquals(createResource(Resource.Type.HLS), result) + assertNotNull(result) + assertEquals(createResource(Resource.Type.HLS), result) } @Test @@ -209,8 +211,8 @@ class ResourceSelectorTest { ) ) val result = resourceSelector.selectResourceFromChapter(chapter) - Assert.assertNotNull(result) - Assert.assertEquals(createSupportedDrmResource(), result) + assertNotNull(result) + assertEquals(createSupportedDrmResource(), result) } companion object { diff --git a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/TestJsonSerialization.kt b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/TestJsonSerialization.kt index 5a316b3d0..8e61f3158 100644 --- a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/TestJsonSerialization.kt +++ b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/TestJsonSerialization.kt @@ -9,24 +9,25 @@ import ch.srgssr.pillarbox.core.business.integrationlayer.data.Chapter import ch.srgssr.pillarbox.core.business.integrationlayer.data.MediaComposition import ch.srgssr.pillarbox.core.business.integrationlayer.service.DefaultHttpClient.jsonSerializer import kotlinx.serialization.SerializationException -import org.junit.Assert -import org.junit.Test +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull class TestJsonSerialization { @Test fun testChapterValidJson() { val json = "{\"urn\":\"urn:srf:video:12343\",\"title\":\"Chapter title\",\"imageUrl\":\"https://image.png\",\"blockReason\": \"UNKNOWN\"}" val chapter = jsonSerializer.decodeFromString(json) - Assert.assertNotNull(chapter) - Assert.assertEquals(BlockReason.UNKNOWN, chapter.blockReason) + assertNotNull(chapter) + assertEquals(BlockReason.UNKNOWN, chapter.blockReason) } @Test(expected = SerializationException::class) fun testChapterValidJsonUnknownBlockreason() { val json = "{\"urn\":\"urn:srf:video:12343\",\"title\":\"Chapter title\",\"imageUrl\":\"https://image.png\",\"blockReason\": \"TOTO\"}" val chapter = jsonSerializer.decodeFromString(json) - Assert.assertNotNull(chapter) - Assert.assertNotNull(chapter.blockReason) + assertNotNull(chapter) + assertNotNull(chapter.blockReason) } @Test(expected = SerializationException::class) @@ -45,7 +46,7 @@ class TestJsonSerialization { fun testMediaCompositionWithNullJsonFields() { val json = "{\"chapterList\": [{\"title\":\"Chapter title\",\"imageUrl\":\"https://image.png\"}]}" val mediaComposition = jsonSerializer.decodeFromString(json) - Assert.assertNotNull(mediaComposition) + assertNotNull(mediaComposition) } @Test @@ -63,6 +64,6 @@ class TestJsonSerialization { } """ val mediaComposition = jsonSerializer.decodeFromString(json) - Assert.assertNotNull(mediaComposition) + assertNotNull(mediaComposition) } } diff --git a/pillarbox-core-business/src/androidTest/java/ch/srgssr/pillarbox/core/business/AkamaiTokenProviderTest.kt b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/akamai/AkamaiTokenProviderTest.kt similarity index 57% rename from pillarbox-core-business/src/androidTest/java/ch/srgssr/pillarbox/core/business/AkamaiTokenProviderTest.kt rename to pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/akamai/AkamaiTokenProviderTest.kt index c212c246d..8400136e7 100644 --- a/pillarbox-core-business/src/androidTest/java/ch/srgssr/pillarbox/core/business/AkamaiTokenProviderTest.kt +++ b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/akamai/AkamaiTokenProviderTest.kt @@ -2,82 +2,88 @@ * Copyright (c) SRG SSR. All rights reserved. * License information is available from the LICENSE file. */ -package ch.srgssr.pillarbox.core.business +package ch.srgssr.pillarbox.core.business.akamai import android.net.Uri -import ch.srgssr.pillarbox.core.business.akamai.AkamaiTokenProvider -import org.junit.Assert -import org.junit.Test +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.runner.RunWith +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue +@RunWith(AndroidJUnit4::class) class AkamaiTokenProviderTest { @Test - fun testInvalidUriForAcl() { + fun `getAcl() from a simple uri return null`() { val uri = Uri.parse("https://www.fake.url") val acl = AkamaiTokenProvider.getAcl(uri) - Assert.assertNull(acl) + assertNull(acl) } @Test - fun testAclVeryLongPath() { + fun `getAcl() from a long uri without extension at the end`() { val uri = Uri.parse("https://fake.url/content/not/playingLive/with/a/very/long/pat") val expectedAcl = "/content/not/playingLive/with/a/very/long/*" val acl = AkamaiTokenProvider.getAcl(uri) - Assert.assertNotNull(acl) - Assert.assertEquals(expectedAcl, acl) + assertNotNull(acl) + assertEquals(expectedAcl, acl) } @Test - fun testAclVeryLongPathWithExtension() { + fun `getAcl() from an uri with very long path and finishing with an extension`() { val uri = Uri.parse("https://fake.url/content/not/playingLive/with/a/very/long/pat/playlist.mp3") val expectedAcl = "/content/not/playingLive/with/a/very/long/pat/*" val acl = AkamaiTokenProvider.getAcl(uri) - Assert.assertNotNull(acl) - Assert.assertEquals(expectedAcl, acl) + assertNotNull(acl) + assertEquals(expectedAcl, acl) } @Test - fun testAclShortPath() { + fun `getAcl() from an uri with short path and finishing with an extension`() { val uri = Uri.parse("https://fake.url/content/playlist.m3u8") val expectedAcl = "/content/*" val acl = AkamaiTokenProvider.getAcl(uri) - Assert.assertNotNull(acl) - Assert.assertEquals(expectedAcl, acl) + assertNotNull(acl) + assertEquals(expectedAcl, acl) } @Test - fun testAclNoPath() { + fun `getAcl() from a uri with no path but end with an extension`() { val uri = Uri.parse("https://fake.url/playlist.m3u8") val expectedAcl = "/*" val acl = AkamaiTokenProvider.getAcl(uri) - Assert.assertNotNull(acl) - Assert.assertEquals(expectedAcl, acl) + assertNotNull(acl) + assertEquals(expectedAcl, acl) } @Test - fun testAclLive() { + fun `getAcl() from a live uri`() { val uri = Uri.parse("https://fake.url/content/hls/playingLive/additional/path") val expectedAcl = "/content/hls/playingLive/additional/*" val acl = AkamaiTokenProvider.getAcl(uri) - Assert.assertNotNull(acl) - Assert.assertEquals(expectedAcl, acl) + assertNotNull(acl) + assertEquals(expectedAcl, acl) } @Test - fun testAclWithQueryParameters() { + fun `getAcl() from an uri with query parameters`() { val uri = Uri.parse("https://srgssrch.akamaized.net/hls/live/2022027/srgssr-hls-stream15-ch-dvr/master.m3u8?start=1697860830&end=1697867100") val expectedAcl = "/hls/live/2022027/srgssr-hls-stream15-ch-dvr/*" val acl = AkamaiTokenProvider.getAcl(uri) - Assert.assertNotNull(acl) - Assert.assertEquals(expectedAcl, acl) + assertNotNull(acl) + assertEquals(expectedAcl, acl) } @Test - fun testAppendTokenToUri() { + fun `AppendTokenToUri add token at the end of the uri`() { val uri = Uri.parse("https://srgssrch.akamaized.net/hls/live/2022027/srgssr-hls-stream15-ch-dvr/master.m3u8?start=1697860830&end=1697867100") val fakeToken = AkamaiTokenProvider.Token(authParams = "Token") val actual = AkamaiTokenProvider.appendTokenToUri(uri, fakeToken) - Assert.assertNotEquals(uri, actual) - Assert.assertTrue("Contains base url", actual.toString().contains(uri.toString())) + assertNotEquals(uri, actual) + assertTrue(actual.toString().contains(uri.toString()), "Contains base url") } } diff --git a/pillarbox-player/src/androidTest/java/ch/srgssr/pillarbox/player/TestIsPlaybackSpeedPossibleAtPosition.kt b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/TestIsPlaybackSpeedPossibleAtPosition.kt similarity index 99% rename from pillarbox-player/src/androidTest/java/ch/srgssr/pillarbox/player/TestIsPlaybackSpeedPossibleAtPosition.kt rename to pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/TestIsPlaybackSpeedPossibleAtPosition.kt index 2ad0e85bc..4273cc7e0 100644 --- a/pillarbox-player/src/androidTest/java/ch/srgssr/pillarbox/player/TestIsPlaybackSpeedPossibleAtPosition.kt +++ b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/TestIsPlaybackSpeedPossibleAtPosition.kt @@ -20,7 +20,6 @@ import kotlin.time.Duration.Companion.hours import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds - class TestIsPlaybackSpeedPossibleAtPosition { @After