Skip to content

Commit

Permalink
Add robolectric analytics tests (#431)
Browse files Browse the repository at this point in the history
Co-authored-by: Gaëtan Muller <[email protected]>
  • Loading branch information
StaehliJ and MGaetan89 authored Jan 30, 2024
1 parent 9ac894e commit b992420
Show file tree
Hide file tree
Showing 21 changed files with 445 additions and 303 deletions.
5 changes: 4 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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" }
Expand All @@ -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" }
Expand Down Expand Up @@ -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" }

Expand Down
14 changes: 8 additions & 6 deletions pillarbox-analytics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,28 @@ android {
withJavadocJar()
}
}
testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}

dependencies {
implementation(libs.comscore)
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.")
}
testImplementation(libs.junit)
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 {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ object SRGAnalytics {
}

fun removePersistentLabel(label: String) {
comScore.removePersistentLabel(label)
commandersAct.removePermanentData(label)
comScore.removePersistentLabel(label)
}

fun getComScorePersistentLabel(label: String): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>)
}

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.
Expand Down Expand Up @@ -105,7 +92,6 @@ internal object ComScoreSrg : ComScore {
checkInitialized()
if (!started.get()) return
with(pageView.toLabels()) {
debugListener?.onPageViewSend(this)
Analytics.notifyViewEvent(this)
}
}
Expand Down
Loading

0 comments on commit b992420

Please sign in to comment.