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 313b557cd..3c215cc0e 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 @@ -6,7 +6,6 @@ package ch.srgssr.pillarbox.analytics.comscore import android.app.Activity import android.content.Context -import android.content.pm.PackageManager import android.util.Log import ch.srgssr.pillarbox.analytics.AnalyticsConfig import ch.srgssr.pillarbox.analytics.BuildConfig @@ -49,14 +48,8 @@ internal object ComScoreSrg : ComScore { val userConsentLabel = getUserConsentPair(config.userConsent.comScore) persistentLabels[userConsentLabel.first] = userConsentLabel.second - val versionName: String = try { - // When unit testing from library packageInfo.versionName is null! - context.applicationContext.packageManager.getPackageInfo(context.applicationContext.packageName, 0).versionName - ?: BuildConfig.VERSION_NAME - } catch (e: PackageManager.NameNotFoundException) { - Log.e("COMSCORE", "Cannot find package", e) - BuildConfig.VERSION_NAME - } + val versionName: String = context.applicationContext.packageManager.getPackageInfo(context.applicationContext.packageName, 0).versionName + ?: BuildConfig.VERSION_NAME persistentLabels[ComScoreLabel.MP_V.label] = versionName persistentLabels[ComScoreLabel.MP_BRAND.label] = config.vendor.toString() val publisher = PublisherConfiguration.Builder() diff --git a/pillarbox-core-business/build.gradle.kts b/pillarbox-core-business/build.gradle.kts index 9e95fc0a1..d2ec25a36 100644 --- a/pillarbox-core-business/build.gradle.kts +++ b/pillarbox-core-business/build.gradle.kts @@ -45,6 +45,11 @@ android { withJavadocJar() } } + testOptions { + unitTests { + isIncludeAndroidResources = true + } + } } dependencies { @@ -73,7 +78,7 @@ dependencies { implementation(libs.okhttp.logging.interceptor) api(libs.tagcommander.core) - testRuntimeOnly(libs.androidx.test.core) + testImplementation(libs.androidx.test.core) testImplementation(libs.androidx.test.ext.junit) testImplementation(libs.junit) testImplementation(libs.kotlin.test) diff --git a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/SRGErrorMessageProviderTest.kt b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/SRGErrorMessageProviderTest.kt new file mode 100644 index 000000000..ccdcb260b --- /dev/null +++ b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/SRGErrorMessageProviderTest.kt @@ -0,0 +1,99 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ +package ch.srgssr.pillarbox.core.business + +import android.content.Context +import androidx.core.util.component1 +import androidx.core.util.component2 +import androidx.media3.common.PlaybackException +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import ch.srgssr.pillarbox.core.business.exception.BlockReasonException +import ch.srgssr.pillarbox.core.business.exception.DataParsingException +import ch.srgssr.pillarbox.core.business.exception.ResourceNotFoundException +import ch.srgssr.pillarbox.core.business.integrationlayer.data.BlockReason +import org.junit.runner.RunWith +import java.io.IOException +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals + +@RunWith(AndroidJUnit4::class) +class SRGErrorMessageProviderTest { + private lateinit var context: Context + private lateinit var errorMessageProvider: SRGErrorMessageProvider + + @BeforeTest + fun setup() { + context = ApplicationProvider.getApplicationContext() + errorMessageProvider = SRGErrorMessageProvider(context) + } + + @Test + fun `getErrorMessage BlockReasonException`() { + val exception = BlockReasonException(BlockReason.AGERATING12) + val (errorCode, errorMessage) = errorMessageProvider.getErrorMessage(playbackException(exception)) + + assertEquals(0, errorCode) + assertEquals(context.getString(R.string.blockReason_ageRating12), errorMessage) + } + + @Test + fun `getErrorMessage ResourceNotFoundException`() { + val exception = ResourceNotFoundException() + val (errorCode, errorMessage) = errorMessageProvider.getErrorMessage(playbackException(exception)) + + assertEquals(0, errorCode) + assertEquals(context.getString(R.string.noPlayableResourceFound), errorMessage) + } + + @Test + fun `getErrorMessage DataParsingException`() { + val exception = DataParsingException() + val (errorCode, errorMessage) = errorMessageProvider.getErrorMessage(playbackException(exception)) + + assertEquals(0, errorCode) + assertEquals(context.getString(R.string.invalidDataError), errorMessage) + } + + @Test + fun `getErrorMessage HttpResultException`() { + val exception = HttpResultException("HTTP request failed") + val (errorCode, errorMessage) = errorMessageProvider.getErrorMessage(playbackException(exception)) + + assertEquals(0, errorCode) + assertEquals(exception.message, errorMessage) + } + + @Test + fun `getErrorMessage IOException`() { + val exception = IOException() + val (errorCode, errorMessage) = errorMessageProvider.getErrorMessage(playbackException(exception)) + + assertEquals(0, errorCode) + assertEquals(context.getString(R.string.NoInternet), errorMessage) + } + + @Test + fun `getErrorMessage unknown cause cause`() { + val exception = IllegalStateException() + val (errorCode, errorMessage) = errorMessageProvider.getErrorMessage(playbackException(exception)) + + assertEquals(PlaybackException.ERROR_CODE_UNSPECIFIED, errorCode) + assertEquals(context.getString(R.string.unknownError), errorMessage) + } + + @Test + fun `getErrorMessage null cause`() { + val (errorCode, errorMessage) = errorMessageProvider.getErrorMessage(playbackException(null)) + + assertEquals(PlaybackException.ERROR_CODE_UNSPECIFIED, errorCode) + assertEquals(context.getString(R.string.unknownError), errorMessage) + } + + private fun playbackException(cause: Exception?): PlaybackException { + return PlaybackException(null, cause, PlaybackException.ERROR_CODE_UNSPECIFIED) + } +} diff --git a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/extension/BlockReasonTest.kt b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/extension/BlockReasonTest.kt index c9b814c61..fa5f031ca 100644 --- a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/extension/BlockReasonTest.kt +++ b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/extension/BlockReasonTest.kt @@ -4,12 +4,31 @@ */ package ch.srgssr.pillarbox.core.business.extension +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 import ch.srgssr.pillarbox.core.business.R import ch.srgssr.pillarbox.core.business.integrationlayer.data.BlockReason +import org.junit.runner.RunWith import kotlin.test.Test import kotlin.test.assertEquals +@RunWith(AndroidJUnit4::class) class BlockReasonTest { + @Test + fun `getString() for BlockReason via Context`() { + val context = ApplicationProvider.getApplicationContext() + + assertEquals("To protect children this content is only available between 8PM and 6AM.", context.getString(BlockReason.AGERATING12)) + assertEquals("To protect children this content is only available between 10PM and 5AM.", context.getString(BlockReason.AGERATING18)) + assertEquals("This commercial content is not available.", context.getString(BlockReason.COMMERCIAL)) + assertEquals("This content is not available anymore.", context.getString(BlockReason.ENDDATE)) + assertEquals("This content is not available outside Switzerland.", context.getString(BlockReason.GEOBLOCK)) + assertEquals("This content is not available due to legal restrictions.", context.getString(BlockReason.LEGAL)) + assertEquals("This content is not available yet.", context.getString(BlockReason.STARTDATE)) + assertEquals("This content is not available.", context.getString(BlockReason.UNKNOWN)) + } + @Test fun `get string resId for BlockReason`() { assertEquals(R.string.blockReason_ageRating12, BlockReason.AGERATING12.getStringResId())