Skip to content

Commit

Permalink
Add a few more tests to pillarbox-core-business
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Jan 31, 2024
1 parent 61c57da commit f8ab054
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion pillarbox-core-business/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ android {
withJavadocJar()
}
}
testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}

dependencies {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Context>()

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())
Expand Down

0 comments on commit f8ab054

Please sign in to comment.