From e3d2aade00b66cca0b89164c940985526d1e17a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Muller?= Date: Thu, 25 Jan 2024 10:36:07 +0100 Subject: [PATCH] Update unit tests in `pillarbox-analytics` (#411) --- build.gradle.kts | 7 + gradle/libs.versions.toml | 3 + pillarbox-analytics/build.gradle.kts | 8 +- .../pillarbox/analytics/ComScoreEventTest.kt | 44 ------ .../analytics/CommandersActEventTest.kt | 127 ---------------- .../pillarbox/analytics/SRGAnalyticsTest.kt | 135 ++++++++++++++++++ .../analytics/TestSRGAnalyticsPageViews.kt | 126 ---------------- .../commandersact/CommandersActEventTest.kt | 74 ++++++++++ .../CommandersActPageViewTest.kt | 129 +++++++++++++++++ .../commandersact/MediaEventTypeTest.kt | 22 +++ .../commandersact/TCEventExtensionsTest.kt | 48 +++++++ .../commandersact/TCMediaEventTest.kt | 82 +++++++++++ .../comscore/ComScorePageViewTest.kt | 52 +++++++ 13 files changed, 558 insertions(+), 299 deletions(-) delete mode 100644 pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/ComScoreEventTest.kt delete mode 100644 pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/CommandersActEventTest.kt create mode 100644 pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsTest.kt delete mode 100644 pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/TestSRGAnalyticsPageViews.kt create mode 100644 pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActEventTest.kt create mode 100644 pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActPageViewTest.kt create mode 100644 pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/MediaEventTypeTest.kt create mode 100644 pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/TCEventExtensionsTest.kt create mode 100644 pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/TCMediaEventTest.kt create mode 100644 pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/comscore/ComScorePageViewTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 15070ef51..f3d620cd0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -84,6 +84,13 @@ dependencyAnalysis { } } + // Required because of https://github.com/autonomousapps/dependency-analysis-gradle-plugin/issues/892 + structure { + bundle("kotlin-test") { + includeDependency(libs.kotlin.test) + } + } + project(":pillarbox-core-business") { onUnusedDependencies { // This dependency is not used directly, but required to be able to compile `CommandersActStreaming` diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d8e726c02..40ac3e88a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -21,6 +21,7 @@ comscore = "6.10.0" dependency-analysis-gradle-plugin = "1.29.0" detekt = "1.23.4" guava = "31.1-android" +json = "20231013" junit = "4.13.2" kotlin = "1.9.22" kotlinx-coroutines = "1.7.3" @@ -57,7 +58,9 @@ androidx-test-runner = { module = "androidx.test:runner", version.ref = "android androidx-tv-foundation = { module = "androidx.tv:tv-foundation", version.ref = "androidx-tv" } androidx-tv-material = { module = "androidx.tv:tv-material", version.ref = "androidx-tv" } coil = { group = "io.coil-kt", name = "coil-compose", version.ref = "coil" } +json = { module = "org.json:json", version.ref = "json" } kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" } +kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" } ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" } diff --git a/pillarbox-analytics/build.gradle.kts b/pillarbox-analytics/build.gradle.kts index 28b9185ae..c8fbf63bf 100644 --- a/pillarbox-analytics/build.gradle.kts +++ b/pillarbox-analytics/build.gradle.kts @@ -56,9 +56,13 @@ dependencies { implementation(libs.tagcommander.core) api(libs.tagcommander.serverside) + 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.kotlinx.coroutines.core) - testImplementation(libs.kotlinx.coroutines.test) + testImplementation(libs.kotlin.test) + testImplementation(libs.mockk) + testImplementation(libs.mockk.dsl) androidTestImplementation(libs.androidx.test.monitor) androidTestRuntimeOnly(libs.androidx.test.runner) diff --git a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/ComScoreEventTest.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/ComScoreEventTest.kt deleted file mode 100644 index a6eb17255..000000000 --- a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/ComScoreEventTest.kt +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) SRG SSR. All rights reserved. - * License information is available from the LICENSE file. - */ -package ch.srgssr.pillarbox.analytics - -import ch.srgssr.pillarbox.analytics.comscore.ComScoreLabel -import ch.srgssr.pillarbox.analytics.comscore.ComScorePageView -import org.junit.Assert -import org.junit.Test - -class ComScoreEventTest { - - @Test - fun testPageView() { - val title = "title 1" - val pageView = ComScorePageView(title) - val actual = pageView.toLabels() - val expected = HashMap().apply { - this[ComScoreLabel.C8.label] = "title 1" - } - Assert.assertEquals(actual, expected) - } - - - @Test(expected = IllegalArgumentException::class) - fun testPageViewEmptyTitle() { - ComScorePageView("") - Assert.assertTrue(false) - } - - @Test - fun testCustomLabels() { - val title = "title 1" - val customLabels = mapOf(Pair("key1", "value1"), Pair("key2", "")) - val pageView = ComScorePageView(title, customLabels) - val actual = pageView.toLabels() - val expected = HashMap().apply { - this[ComScoreLabel.C8.label] = "title 1" - this["key1"] = "value1" - } - Assert.assertEquals(actual, expected) - } -} diff --git a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/CommandersActEventTest.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/CommandersActEventTest.kt deleted file mode 100644 index 8b5558811..000000000 --- a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/CommandersActEventTest.kt +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (c) SRG SSR. All rights reserved. - * License information is available from the LICENSE file. - */ -package ch.srgssr.pillarbox.analytics - -import ch.srgssr.pillarbox.analytics.commandersact.CommandersActEvent -import ch.srgssr.pillarbox.analytics.commandersact.CommandersActPageView -import org.junit.Assert -import org.junit.Test - -class CommandersActEventTest { - - @Test - fun testPageEvent() { - val pageView = CommandersActPageView( - name = "title", - type = "type", - levels = listOf("level1", "level2") - ) - val tcEvent = pageView.toTCPageViewEvent(AnalyticsConfig.Vendor.RTS) - val expected = hashMapOf( - Pair("navigation_level_1", "level1"), - Pair("navigation_level_2", "level2"), - Pair("navigation_bu_distributer", "RTS") - ) - val actual = tcEvent.additionalProperties - Assert.assertEquals(expected, actual) - Assert.assertEquals("title", tcEvent.pageName) - Assert.assertEquals("type", tcEvent.pageType) - } - - @Test - fun testPageEventEmptyLevels() { - val pageView = CommandersActPageView( - name = "title", type = "type" - ) - val tcEvent = pageView.toTCPageViewEvent(AnalyticsConfig.Vendor.RTS) - val expected = hashMapOf( - Pair("navigation_bu_distributer", "RTS") - ) - val actual = tcEvent.additionalProperties - Assert.assertEquals(expected, actual) - Assert.assertEquals("title", tcEvent.pageName) - Assert.assertEquals("type", tcEvent.pageType) - } - - @Test - fun testPageEventCustomLabels() { - val pageView = CommandersActPageView( - name = "title", - type = "type", - labels = mapOf(Pair("Key1", "value1"), Pair("Key2", " ")) - ) - val tcEvent = pageView.toTCPageViewEvent(AnalyticsConfig.Vendor.RTS) - val expected = hashMapOf( - Pair("navigation_bu_distributer", "RTS"), - Pair("Key1", "value1") - ) - val actual = tcEvent.additionalProperties - Assert.assertEquals(expected, actual) - Assert.assertEquals("title", tcEvent.pageName) - Assert.assertEquals("type", tcEvent.pageType) - } - - @Test - fun testEvent() { - val event = CommandersActEvent("name") - val tcEvent = event.toTCCustomEvent() - Assert.assertEquals(tcEvent.name, "name") - Assert.assertNull(tcEvent.pageType) - Assert.assertNull(tcEvent.pageName) - } - - @Test - fun testEventWithLabels() { - val expectedLabels = hashMapOf( - Pair("event_value_3", "extra3"), - Pair("event_source", "source"), - Pair("event_value", "value"), - Pair("event_type", "type"), - ) - val event = CommandersActEvent("name", labels = expectedLabels) - val tcEvent = event.toTCCustomEvent() - val actualLabels = tcEvent.additionalProperties - Assert.assertEquals(expectedLabels, actualLabels) - Assert.assertEquals(tcEvent.name, "name") - Assert.assertNull(tcEvent.pageType) - Assert.assertNull(tcEvent.pageName) - } - - @Test - fun testEventWithLabelsBlank() { - val labels = hashMapOf( - Pair("event_value_3", ""), - Pair("event_source", " "), - Pair("event_value", "value"), - Pair("event_type", "type"), - ) - val event = CommandersActEvent("name", labels = labels) - val expectedLabels = hashMapOf( - Pair("event_value", "value"), - Pair("event_type", "type"), - ) - val tcEvent = event.toTCCustomEvent() - val actualLabels = tcEvent.additionalProperties - Assert.assertEquals(expectedLabels, actualLabels) - Assert.assertEquals(tcEvent.name, "name") - Assert.assertNull(tcEvent.pageType) - Assert.assertNull(tcEvent.pageName) - } - - @Test(expected = IllegalArgumentException::class) - fun testBlankEventName() { - CommandersActEvent(name = " ") - } - - @Test(expected = IllegalArgumentException::class) - fun testBlankPageViewTitle() { - CommandersActPageView(name = " ", type = "type") - } - - @Test(expected = IllegalArgumentException::class) - fun testBlankPageViewType() { - CommandersActPageView(name = "Title", type = " ") - } -} 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 new file mode 100644 index 000000000..494c45c5b --- /dev/null +++ b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/SRGAnalyticsTest.kt @@ -0,0 +1,135 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ +package ch.srgssr.pillarbox.analytics + +import ch.srgssr.pillarbox.analytics.commandersact.CommandersAct +import ch.srgssr.pillarbox.analytics.commandersact.CommandersActEvent +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.mockk +import io.mockk.verify +import io.mockk.verifySequence +import kotlin.test.BeforeTest +import kotlin.test.Test + +class SRGAnalyticsTest { + private lateinit var comScore: ComScore + private lateinit var commandersAct: CommandersAct + private lateinit var analytics: SRGAnalytics.Analytics + + @BeforeTest + fun setup() { + comScore = mockk(relaxed = true) + commandersAct = mockk(relaxed = true) + analytics = SRGAnalytics.Analytics( + comScore = comScore, + commandersAct = commandersAct, + ) + } + + @Test + fun `sendPageView invokes comScore and commandersAct`() { + val commandersActPageView = CommandersActPageView( + name = "Title", + type = "Type", + levels = listOf("level1", "level2"), + ) + val comScorePageView = ComScorePageView("Title") + + analytics.sendPageView( + commandersAct = commandersActPageView, + comScore = comScorePageView, + ) + + verifySequence { + commandersAct.sendPageView(commandersActPageView) + comScore.sendPageView(comScorePageView) + } + } + + @Test + fun `sendEvent invokes only commandersAct`() { + val commandersActEvent = CommandersActEvent(name = "name") + + analytics.sendEvent(commandersActEvent) + + verifySequence { + commandersAct.sendEvent(commandersActEvent) + } + verify(exactly = 0) { + comScore wasNot Called + } + } + + @Test + fun `putPersistentLabels invokes comScore and commandersAct`() { + val commandersActLabels = mapOf("key1" to "value1") + val comScoreLabels = mapOf("key2" to "value2") + + analytics.putPersistentLabels( + commandersActLabels = commandersActLabels, + comScoreLabels = comScoreLabels, + ) + + verifySequence { + comScore.putPersistentLabels(comScoreLabels) + commandersAct.putPermanentData(commandersActLabels) + } + } + + @Test + fun `removePersistentLabel invokes comScore and commandersAct`() { + val label = "label" + + analytics.removePersistentLabel(label = label) + + verifySequence { + comScore.removePersistentLabel(label) + commandersAct.removePermanentData(label) + } + } + + @Test + fun `getComScorePersistentLabel invokes comScore`() { + val label = "label" + + analytics.getComScorePersistentLabel(label = label) + + verifySequence { + comScore.getPersistentLabel(label) + } + verify(exactly = 0) { + commandersAct wasNot Called + } + } + + @Test + fun `getCommandersActPermanentData invokes commandersAct`() { + val label = "label" + + analytics.getCommandersActPermanentData(label = label) + + verifySequence { + commandersAct.getPermanentDataLabel(label) + } + verify(exactly = 0) { + comScore wasNot Called + } + } + + @Test + fun `setUserConsent invokes comScore and commandersAct`() { + val userConsent = UserConsent() + + analytics.setUserConsent(userConsent) + + verifySequence { + comScore.setUserConsent(userConsent.comScore) + commandersAct.setConsentServices(userConsent.commandersActConsentServices) + } + } +} diff --git a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/TestSRGAnalyticsPageViews.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/TestSRGAnalyticsPageViews.kt deleted file mode 100644 index d4f7c4e2c..000000000 --- a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/TestSRGAnalyticsPageViews.kt +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (c) SRG SSR. All rights reserved. - * License information is available from the LICENSE file. - */ -package ch.srgssr.pillarbox.analytics - -import ch.srgssr.pillarbox.analytics.commandersact.CommandersAct -import ch.srgssr.pillarbox.analytics.commandersact.CommandersActEvent -import ch.srgssr.pillarbox.analytics.commandersact.CommandersActPageView -import ch.srgssr.pillarbox.analytics.commandersact.TCMediaEvent -import ch.srgssr.pillarbox.analytics.comscore.ComScore -import ch.srgssr.pillarbox.analytics.comscore.ComScorePageView -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 TestSRGAnalyticsPageViews { - private lateinit var comScore: DummyComscore - private lateinit var commandersAct: DummyCommandersAct - private lateinit var analytics: SRGAnalytics.Analytics - private lateinit var dispatcher: CoroutineDispatcher - - @Before - fun setup() { - comScore = DummyComscore() - commandersAct = DummyCommandersAct() - analytics = SRGAnalytics.Analytics(comScore = comScore, commandersAct = commandersAct) - dispatcher = UnconfinedTestDispatcher() - } - - @Test - fun testComScorePageView() = runTest { - val commandersActPageView = CommandersActPageView(name = "Title", type = "Type", levels = listOf("level1", "level2")) - val comscorePageView = ComScorePageView("Title") - val actualTitle = ArrayList() - val job = launch(dispatcher) { - comScore.pageViewFlow.take(1).toList(actualTitle) - } - analytics.sendPageView(commandersAct = commandersActPageView, comScore = comscorePageView) - Assert.assertEquals(comscorePageView, actualTitle.first()) - job.cancel() - } - - @Test - fun testCommandersActPageView() = runTest { - val commandersActPageView = CommandersActPageView(name = "Title", type = "Type", levels = listOf("level1", "level2")) - val comscorePageView = ComScorePageView("Title") - val actualPageView = ArrayList() - val job = launch(dispatcher) { - commandersAct.pageViewFlow.take(1).toList(actualPageView) - } - analytics.sendPageView(commandersAct = commandersActPageView, comScore = comscorePageView) - Assert.assertEquals(commandersActPageView, actualPageView.first()) - job.cancel() - } - - - private class DummyComscore : ComScore { - val pageViewFlow = MutableSharedFlow(extraBufferCapacity = 1, replay = 1) - - override fun sendPageView(pageView: ComScorePageView) { - Assert.assertTrue(pageViewFlow.tryEmit(pageView)) - } - - override fun putPersistentLabels(labels: Map) { - // Nothing - } - - override fun removePersistentLabel(label: String) { - // Nothing - } - - override fun getPersistentLabel(label: String): String? { - // Nothing - return null - } - - override fun setUserConsent(userConsent: ComScoreUserConsent) { - // Nothing - } - } - - private class DummyCommandersAct : CommandersAct { - val pageViewFlow = MutableSharedFlow(extraBufferCapacity = 1, replay = 1) - - override fun sendPageView(pageView: CommandersActPageView) { - Assert.assertTrue(pageViewFlow.tryEmit(pageView)) - } - - override fun sendEvent(event: CommandersActEvent) { - - } - - override fun sendTcMediaEvent(event: TCMediaEvent) { - - } - - override fun putPermanentData(labels: Map) { - // Nothing - } - - override fun removePermanentData(label: String) { - // Nothing - } - - override fun getPermanentDataLabel(label: String): String? { - // Nothing - return null - } - - override fun setConsentServices(consentServices: List) { - // Nothing - } - - } -} diff --git a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActEventTest.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActEventTest.kt new file mode 100644 index 000000000..f42d32ad7 --- /dev/null +++ b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActEventTest.kt @@ -0,0 +1,74 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ +package ch.srgssr.pillarbox.analytics.commandersact + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class CommandersActEventTest { + @Test(expected = IllegalArgumentException::class) + fun `empty event name is invalid`() { + CommandersActEvent("") + } + + @Test(expected = IllegalArgumentException::class) + fun `blank event name is invalid`() { + CommandersActEvent(" ") + } + + @Test + fun `convert event with no labels to TCCustomEvent`() { + val event = CommandersActEvent("name") + val tcEvent = event.toTCCustomEvent() + + assertEquals(emptyMap(), tcEvent.additionalProperties) + assertEquals(event.name, tcEvent.name) + assertNull(tcEvent.pageName) + assertNull(tcEvent.pageType) + } + + @Test + fun `convert event with labels to TCCustomEvent`() { + val event = CommandersActEvent( + name = "name", + labels = mapOf( + "event_value_3" to "extra3", + "event_source" to "source", + "event_value" to "value", + "event_type" to "type", + ) + ) + val tcEvent = event.toTCCustomEvent() + + assertEquals>(event.labels, tcEvent.additionalProperties) + assertEquals(event.name, tcEvent.name) + assertNull(tcEvent.pageName) + assertNull(tcEvent.pageType) + } + + @Test + fun `convert event with some blank labels to TCCustomEvent`() { + val event = CommandersActEvent( + name = "name", + labels = mapOf( + "event_value_3" to "", + "event_source" to " ", + "event_value" to "value", + "event_type" to "type", + ) + ) + val tcEvent = event.toTCCustomEvent() + val expectedProperties = mapOf( + "event_value" to "value", + "event_type" to "type", + ) + + assertEquals>(expectedProperties, tcEvent.additionalProperties) + assertEquals(event.name, tcEvent.name) + assertNull(tcEvent.pageName) + assertNull(tcEvent.pageType) + } +} diff --git a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActPageViewTest.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActPageViewTest.kt new file mode 100644 index 000000000..a1b439539 --- /dev/null +++ b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/CommandersActPageViewTest.kt @@ -0,0 +1,129 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ +package ch.srgssr.pillarbox.analytics.commandersact + +import ch.srgssr.pillarbox.analytics.AnalyticsConfig.Vendor +import kotlin.test.Test +import kotlin.test.assertEquals + +class CommandersActPageViewTest { + @Test(expected = IllegalArgumentException::class) + fun `empty name is invalid`() { + CommandersActPageView( + name = "", + type = "type", + ) + } + + @Test(expected = IllegalArgumentException::class) + fun `blank name is invalid`() { + CommandersActPageView( + name = " ", + type = "type", + ) + } + + @Test(expected = IllegalArgumentException::class) + fun `empty type is invalid`() { + CommandersActPageView( + name = "name", + type = "", + ) + } + + @Test(expected = IllegalArgumentException::class) + fun `blank type is invalid`() { + CommandersActPageView( + name = "name", + type = " ", + ) + } + + @Test + fun `convert page view to TCPageViewEvent`() { + val pageView = CommandersActPageView( + name = "name", + type = "type", + ) + val tcPageView = pageView.toTCPageViewEvent(vendor = Vendor.RTS) + val expectedProperties = mapOf( + "navigation_bu_distributer" to "RTS", + ) + + assertEquals(expectedProperties, tcPageView.additionalProperties) + assertEquals(pageView.name, tcPageView.pageName) + assertEquals(pageView.type, tcPageView.pageType) + } + + @Test + fun `convert page view with levels to TCPageViewEvent`() { + val pageView = CommandersActPageView( + name = "name", + type = "type", + levels = listOf("level1", "level2"), + ) + val tcPageView = pageView.toTCPageViewEvent(vendor = Vendor.RSI) + val expectedProperties = mapOf( + "navigation_level_1" to "level1", + "navigation_level_2" to "level2", + "navigation_bu_distributer" to "RSI", + ) + + assertEquals(expectedProperties, tcPageView.additionalProperties) + assertEquals(pageView.name, tcPageView.pageName) + assertEquals(pageView.type, tcPageView.pageType) + } + + @Test + fun `convert page view with some blank labels to TCPageViewEvent`() { + val pageView = CommandersActPageView( + name = "name", + type = "type", + labels = mapOf( + "key1" to "value1", + "key2" to "", + "key3" to "value3", + "key4" to " ", + ), + ) + val tcPageView = pageView.toTCPageViewEvent(vendor = Vendor.RTR) + val expectedProperties = mapOf( + "key1" to "value1", + "key3" to "value3", + "navigation_bu_distributer" to "RTR", + ) + + assertEquals(expectedProperties, tcPageView.additionalProperties) + assertEquals(pageView.name, tcPageView.pageName) + assertEquals(pageView.type, tcPageView.pageType) + } + + @Test + fun `convert page view with levels and labels to TCPageViewEvent`() { + val pageView = CommandersActPageView( + name = "name", + type = "type", + levels = listOf("level1", "level2"), + labels = mapOf( + "key1" to "value1", + "key2" to "", + "key3" to "value3", + "key4" to " ", + ), + ) + val tcPageView = pageView.toTCPageViewEvent(vendor = Vendor.SRF) + val expectedProperties = mapOf( + "key1" to "value1", + "key3" to "value3", + "navigation_level_1" to "level1", + "navigation_level_2" to "level2", + "navigation_bu_distributer" to "SRF", + ) + + assertEquals(expectedProperties, tcPageView.additionalProperties) + assertEquals(pageView.name, tcPageView.pageName) + assertEquals(pageView.type, tcPageView.pageType) + } +} diff --git a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/MediaEventTypeTest.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/MediaEventTypeTest.kt new file mode 100644 index 000000000..f868f43e2 --- /dev/null +++ b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/MediaEventTypeTest.kt @@ -0,0 +1,22 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ +package ch.srgssr.pillarbox.analytics.commandersact + +import kotlin.test.Test +import kotlin.test.assertEquals + +class MediaEventTypeTest { + @Test + fun `to string`() { + assertEquals("play", MediaEventType.Play.toString()) + assertEquals("pause", MediaEventType.Pause.toString()) + assertEquals("eof", MediaEventType.Eof.toString()) + assertEquals("stop", MediaEventType.Stop.toString()) + assertEquals("seek", MediaEventType.Seek.toString()) + assertEquals("pos", MediaEventType.Pos.toString()) + assertEquals("uptime", MediaEventType.Uptime.toString()) + } + +} diff --git a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/TCEventExtensionsTest.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/TCEventExtensionsTest.kt new file mode 100644 index 000000000..85688b1e7 --- /dev/null +++ b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/TCEventExtensionsTest.kt @@ -0,0 +1,48 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ +package ch.srgssr.pillarbox.analytics.commandersact + +import ch.srgssr.pillarbox.analytics.commandersact.TCEventExtensions.addAdditionalParameterIfNotBlank +import com.tagcommander.lib.serverside.events.TCCustomEvent +import kotlin.test.Test +import kotlin.test.assertEquals + +class TCEventExtensionsTest { + @Test + fun `add additional parameter`() { + val tcEvent = TCCustomEvent("name") + tcEvent.addAdditionalParameterIfNotBlank("key", "value") + + val expectedProperties = mapOf( + "key" to "value" + ) + + assertEquals(expectedProperties, tcEvent.additionalProperties) + } + + @Test + fun `add additional parameter with null data`() { + val tcEvent = TCCustomEvent("name") + tcEvent.addAdditionalParameterIfNotBlank("key", null) + + assertEquals(emptyMap(), tcEvent.additionalProperties) + } + + @Test + fun `add additional parameter with empty data`() { + val tcEvent = TCCustomEvent("name") + tcEvent.addAdditionalParameterIfNotBlank("key", "") + + assertEquals(emptyMap(), tcEvent.additionalProperties) + } + + @Test + fun `add additional parameter with blank data`() { + val tcEvent = TCCustomEvent("name") + tcEvent.addAdditionalParameterIfNotBlank("key", " ") + + assertEquals(emptyMap(), tcEvent.additionalProperties) + } +} diff --git a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/TCMediaEventTest.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/TCMediaEventTest.kt new file mode 100644 index 000000000..3b39efe7d --- /dev/null +++ b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/commandersact/TCMediaEventTest.kt @@ -0,0 +1,82 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ +package ch.srgssr.pillarbox.analytics.commandersact + +import ch.srgssr.pillarbox.analytics.BuildConfig +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.time.Duration.Companion.minutes + +class TCMediaEventTest { + @Test + fun `convert event to JSONObject`() { + val tcEvent = TCMediaEvent( + eventType = MediaEventType.Play, + assets = emptyMap(), + ) + val json = tcEvent.jsonObject + + assertEquals(6, json.length()) + + // Properties set by TCEvent + assertEquals("play", json.getString("event_name")) + assertNotNull(json.optJSONObject("context")) + assertNotNull(json.getJSONObject("context").getString("event_id")) + assertFalse(json.has("page_type")) + assertFalse(json.has("page_name")) + + // Properties set by TCMediaEvent + assertEquals("0", json.getString("media_position")) + assertEquals(BuildConfig.VERSION_NAME, json.getString("media_player_version")) + assertEquals("Pillarbox", json.getString("media_player_display")) + assertEquals("false", json.getString("media_subtitles_on")) + } + + @Test + fun `convert event with properties set to JSONObject`() { + val tcEvent = TCMediaEvent( + eventType = MediaEventType.Play, + assets = mapOf( + "key1" to "value1", + "key2" to "", + "key3" to "value3", + "key4" to " ", + ), + sourceId = "sourceId", + ).apply { + mediaPosition = 2.5.minutes + timeShift = 1.minutes + deviceVolume = 0.5f + isSubtitlesOn = true + subtitleSelectionLanguage = "German" + audioTrackLanguage = "French" + } + val json = tcEvent.jsonObject + + assertEquals(13, json.length()) + + // Properties set by TCEvent + assertEquals("play", json.getString("event_name")) + assertNotNull(json.optJSONObject("context")) + assertNotNull(json.getJSONObject("context").getString("event_id")) + assertFalse(json.has("page_type")) + assertFalse(json.has("page_name")) + + // Properties set by TCMediaEvent + assertEquals("value1", json.getString("key1")) + assertEquals("value3", json.getString("key3")) + assertEquals("sourceId", json.getString("source_id")) + assertEquals("150", json.getString("media_position")) + assertEquals("60", json.getString("media_timeshift")) + assertEquals("0.5", json.getString("media_volume")) + assertEquals(BuildConfig.VERSION_NAME, json.getString("media_player_version")) + assertEquals("Pillarbox", json.getString("media_player_display")) + assertEquals("true", json.getString("media_subtitles_on")) + assertEquals("GERMAN", json.getString("media_subtitle_selection")) + assertEquals("FRENCH", json.getString("media_audio_track")) + } +} diff --git a/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/comscore/ComScorePageViewTest.kt b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/comscore/ComScorePageViewTest.kt new file mode 100644 index 000000000..48e48d388 --- /dev/null +++ b/pillarbox-analytics/src/test/java/ch/srgssr/pillarbox/analytics/comscore/ComScorePageViewTest.kt @@ -0,0 +1,52 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ +package ch.srgssr.pillarbox.analytics.comscore + +import kotlin.test.Test +import kotlin.test.assertEquals + +class ComScorePageViewTest { + @Test(expected = IllegalArgumentException::class) + fun `empty page name is invalid`() { + ComScorePageView("") + } + + @Test(expected = IllegalArgumentException::class) + fun `blank page name is invalid`() { + ComScorePageView(" ") + } + + @Test + fun `get labels from page view`() { + val pageView = ComScorePageView("name") + val labels = pageView.toLabels() + val expectedLabels = mapOf( + "c8" to pageView.name, + ) + + assertEquals(expectedLabels, labels) + } + + @Test + fun `get labels from page view with some blank labels`() { + val pageView = ComScorePageView( + name = "name", + labels = mapOf( + "key1" to "value1", + "key2" to "", + "key3" to "value3", + "key4" to " ", + ), + ) + val labels = pageView.toLabels() + val expectedLabels = mapOf( + "key1" to "value1", + "key3" to "value3", + "c8" to pageView.name, + ) + + assertEquals(expectedLabels, labels) + } +}