Skip to content

Commit

Permalink
Add an option to override platform context properties
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Jan 28, 2024
1 parent 334ebbe commit 9ae3a3f
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.snowplowanalytics.core.constants.Parameters
import com.snowplowanalytics.core.constants.TrackerConstants
import com.snowplowanalytics.core.tracker.PlatformContext
import com.snowplowanalytics.snowplow.configuration.PlatformContextProperty
import com.snowplowanalytics.snowplow.tracker.PlatformContextRetriever
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -29,7 +30,7 @@ class PlatformContextTest {
// --- TESTS
@Test
fun addsNotMockedMobileContext() {
val platformContext = PlatformContext(null, context)
val platformContext = PlatformContext(context = context)
val sdj = platformContext.getMobileContext(false)
Assert.assertNotNull(sdj)
val sdjMap = sdj!!.map
Expand All @@ -47,7 +48,7 @@ class PlatformContextTest {
@Test
fun addsAllMockedInfo() {
val deviceInfoMonitor = MockDeviceInfoMonitor()
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, null, context)
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, context = context)
Thread.sleep(100) // sleep in order to fetch the app set properties
val sdj = platformContext.getMobileContext(false)
val sdjMap = sdj!!.map
Expand Down Expand Up @@ -77,7 +78,7 @@ class PlatformContextTest {
@Test
fun updatesMobileInfo() {
val deviceInfoMonitor = MockDeviceInfoMonitor()
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, null, context)
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, context = context)
Assert.assertEquals(
1,
deviceInfoMonitor.getMethodAccessCount("getSystemAvailableMemory").toLong()
Expand All @@ -100,7 +101,7 @@ class PlatformContextTest {
@Test
fun doesntUpdateMobileInfoWithinUpdateWindow() {
val deviceInfoMonitor = MockDeviceInfoMonitor()
val platformContext = PlatformContext(1000, 0, deviceInfoMonitor, null, context)
val platformContext = PlatformContext(1000, 0, deviceInfoMonitor, context = context)
Assert.assertEquals(
1,
deviceInfoMonitor.getMethodAccessCount("getSystemAvailableMemory").toLong()
Expand All @@ -123,7 +124,7 @@ class PlatformContextTest {
@Test
fun updatesNetworkInfo() {
val deviceInfoMonitor = MockDeviceInfoMonitor()
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, null, context)
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, context = context)
Assert.assertEquals(1, deviceInfoMonitor.getMethodAccessCount("getNetworkType").toLong())
Assert.assertEquals(
1,
Expand All @@ -140,7 +141,7 @@ class PlatformContextTest {
@Test
fun doesntUpdateNetworkInfoWithinUpdateWindow() {
val deviceInfoMonitor = MockDeviceInfoMonitor()
val platformContext = PlatformContext(0, 1000, deviceInfoMonitor, null, context)
val platformContext = PlatformContext(0, 1000, deviceInfoMonitor, context = context)
Assert.assertEquals(1, deviceInfoMonitor.getMethodAccessCount("getNetworkType").toLong())
Assert.assertEquals(
1,
Expand All @@ -157,7 +158,7 @@ class PlatformContextTest {
@Test
fun doesntUpdateNonEphemeralInfo() {
val deviceInfoMonitor = MockDeviceInfoMonitor()
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, null, context)
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, context = context)
Assert.assertEquals(1, deviceInfoMonitor.getMethodAccessCount("getOsType").toLong())
Assert.assertEquals(1, deviceInfoMonitor.getMethodAccessCount("getTotalStorage").toLong())
platformContext.getMobileContext(false)
Expand All @@ -168,7 +169,7 @@ class PlatformContextTest {
@Test
fun doesntUpdateIdfaIfNotNull() {
val deviceInfoMonitor = MockDeviceInfoMonitor()
val platformContext = PlatformContext(0, 1, deviceInfoMonitor, null, context)
val platformContext = PlatformContext(0, 1, deviceInfoMonitor, context = context)
Assert.assertEquals(1, deviceInfoMonitor.getMethodAccessCount("getAndroidIdfa").toLong())
platformContext.getMobileContext(false)
Assert.assertEquals(1, deviceInfoMonitor.getMethodAccessCount("getAndroidIdfa").toLong())
Expand All @@ -178,7 +179,7 @@ class PlatformContextTest {
fun updatesIdfaIfEmptyOrNull() {
val deviceInfoMonitor = MockDeviceInfoMonitor()
deviceInfoMonitor.customIdfa = ""
val platformContext = PlatformContext(0, 1, deviceInfoMonitor, null, context)
val platformContext = PlatformContext(0, 1, deviceInfoMonitor, context = context)
Assert.assertEquals(1, deviceInfoMonitor.getMethodAccessCount("getAndroidIdfa").toLong())
deviceInfoMonitor.customIdfa = null
platformContext.getMobileContext(false)
Expand All @@ -190,7 +191,7 @@ class PlatformContextTest {
@Test
fun anonymisesUserIdentifiers() {
val deviceInfoMonitor = MockDeviceInfoMonitor()
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, null, context)
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, context = context)
val sdj = platformContext.getMobileContext(true)
val sdjMap = sdj!!.map
val sdjData = sdjMap["data"] as Map<*, *>?
Expand All @@ -201,16 +202,16 @@ class PlatformContextTest {
@Test
fun readsAppSetInfoSynchronouslyFromGeneralPrefsSecondTime() {
val deviceInfoMonitor = MockDeviceInfoMonitor()
PlatformContext(0, 0, deviceInfoMonitor, null, context)
PlatformContext(0, 0, deviceInfoMonitor, context = context)

Thread.sleep(100)

val secondPlatformContext = PlatformContext(0, 0, deviceInfoMonitor, null, context)
val secondPlatformContext = PlatformContext(0, 0, deviceInfoMonitor, context = context)
val sdj = secondPlatformContext.getMobileContext(true)
val sdjData = sdj!!.map["data"] as Map<*, *>?

Assert.assertEquals("XXX", sdjData!![Parameters.APP_SET_ID])
Assert.assertEquals("app", sdjData!![Parameters.APP_SET_ID_SCOPE])
Assert.assertEquals("app", sdjData[Parameters.APP_SET_ID_SCOPE])
}

@Test
Expand All @@ -219,7 +220,7 @@ class PlatformContextTest {
val platformContext = PlatformContext(
0, 0, deviceInfoMonitor,
listOf(PlatformContextProperty.ANDROID_IDFA, PlatformContextProperty.BATTERY_LEVEL),
context
context = context
)

val sdj = platformContext.getMobileContext(false)
Expand All @@ -238,7 +239,7 @@ class PlatformContextTest {
fun truncatesLanguageToMax8Chars() {
val deviceInfoMonitor = MockDeviceInfoMonitor()
deviceInfoMonitor.language = "1234567890"
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, null, context)
val platformContext = PlatformContext(0, 0, deviceInfoMonitor, context = context)

val sdj = platformContext.getMobileContext(false)
Assert.assertNotNull(sdj)
Expand All @@ -254,7 +255,7 @@ class PlatformContextTest {
// set locale to an ISO-639 invalid 2-letter code
Locale.setDefault(Locale("dk", "example"))

val platformContext = PlatformContext(null, context)
val platformContext = PlatformContext(context = context)
val sdj = platformContext.getMobileContext(false)
Assert.assertNotNull(sdj)
val sdjData = sdj!!.map["data"] as Map<*, *>
Expand All @@ -267,9 +268,9 @@ class PlatformContextTest {

@Test
fun doesntSetTheNetworkTechIfNotRequested() {
val platformContext = PlatformContext(listOf(
val platformContext = PlatformContext(properties = listOf(
PlatformContextProperty.NETWORK_TYPE
), context)
), context = context)
val sdj = platformContext.getMobileContext(false)
Assert.assertNotNull(sdj)
val sdjData = sdj!!.map["data"] as Map<*, *>
Expand All @@ -278,6 +279,62 @@ class PlatformContextTest {
Assert.assertFalse(sdjData.containsKey(Parameters.NETWORK_TECHNOLOGY))
}

@Test
fun PlatformContextRetrieverOverridesProperties() {
val retriever = PlatformContextRetriever(
osType = { "r1" },
osVersion = { "r2" },
deviceVendor = { "r3" },
deviceModel = { "r4" },
carrier = { "r5" },
networkType = { "r6" },
networkTechnology = { "r7" },
androidIdfa = { "r8" },
availableStorage = { 100 },
totalStorage = { 101 },
physicalMemory = { 102 },
systemAvailableMemory = { 103 },
batteryLevel = { 104 },
batteryState = { "r9" },
isPortrait = { false },
resolution = { "r10" },
scale = { 105f },
language = { "r11" },
appSetId = { "r12" },
appSetIdScope = { "r13" },
)
val platformContext = PlatformContext(
retriever = retriever,
context = context
)
Thread.sleep(100)

val sdj = platformContext.getMobileContext(false)
Assert.assertNotNull(sdj)
val sdjData = sdj!!.map["data"] as Map<*, *>

Assert.assertEquals("r1", sdjData[Parameters.OS_TYPE])
Assert.assertEquals("r2", sdjData[Parameters.OS_VERSION])
Assert.assertEquals("r3", sdjData[Parameters.DEVICE_MANUFACTURER])
Assert.assertEquals("r4", sdjData[Parameters.DEVICE_MODEL])
Assert.assertEquals("r5", sdjData[Parameters.CARRIER])
Assert.assertEquals("r6", sdjData[Parameters.NETWORK_TYPE])
Assert.assertEquals("r7", sdjData[Parameters.NETWORK_TECHNOLOGY])
Assert.assertEquals("r8", sdjData[Parameters.ANDROID_IDFA])
Assert.assertEquals(100L, sdjData[Parameters.AVAILABLE_STORAGE])
Assert.assertEquals(101L, sdjData[Parameters.TOTAL_STORAGE])
Assert.assertEquals(102L, sdjData[Parameters.PHYSICAL_MEMORY])
Assert.assertEquals(103L, sdjData[Parameters.SYSTEM_AVAILABLE_MEMORY])
Assert.assertEquals(104, sdjData[Parameters.BATTERY_LEVEL])
Assert.assertEquals("r9", sdjData[Parameters.BATTERY_STATE])
Assert.assertEquals(false, sdjData[Parameters.IS_PORTRAIT])
Assert.assertEquals("r10", sdjData[Parameters.MOBILE_RESOLUTION])
Assert.assertEquals(105f, sdjData[Parameters.MOBILE_SCALE])
Assert.assertEquals("r11", sdjData[Parameters.MOBILE_LANGUAGE])
Assert.assertEquals("r12", sdjData[Parameters.APP_SET_ID])
Assert.assertEquals("r13", sdjData[Parameters.APP_SET_ID_SCOPE])
}

// --- PRIVATE
private val context: Context
get() = InstrumentationRegistry.getInstrumentation().targetContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class StateManagerTest {
tracker.base64Encoded = false
tracker.logLevel = LogLevel.VERBOSE
}
val tracker = Tracker(emitter, "namespace", "appId", null, context, trackerBuilder)
val tracker = Tracker(emitter, "namespace", "appId", context = context, builder = trackerBuilder)

// Send events
tracker.track(Timing("category", "variable", 123))
Expand Down Expand Up @@ -221,7 +221,7 @@ class StateManagerTest {
tracker.base64Encoded = false
tracker.logLevel = LogLevel.VERBOSE
}
val tracker = Tracker(emitter, "namespace", "appId", null, context, trackerBuilder)
val tracker = Tracker(emitter, "namespace", "appId", context = context, builder = trackerBuilder)

// Send events
tracker.track(Timing("category", "variable", 123))
Expand Down Expand Up @@ -295,7 +295,7 @@ class StateManagerTest {
tracker.sessionContext = false
tracker.platformContextEnabled = false
}
val tracker = Tracker(emitter, "namespace", "appId", null, context, trackerBuilder)
val tracker = Tracker(emitter, "namespace", "appId", context = context, builder = trackerBuilder)

// Send events
tracker.track(Timing("category", "variable", 123))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class TrackerTest {
tracker.installAutotracking = installTracking
tracker.applicationContext = true
}
Companion.tracker = Tracker(emitter, "myNamespace", "myAppId", null, context, trackerBuilder)
Companion.tracker = Tracker(emitter, "myNamespace", "myAppId", context = context, builder = trackerBuilder)
return Companion.tracker
}

Expand Down Expand Up @@ -203,7 +203,7 @@ class TrackerTest {
tracker.screenViewAutotracking = false
}
Companion.tracker =
Tracker(emitter!!, namespace, "testTrackWithNoContext", null, context, trackerBuilder)
Tracker(emitter!!, namespace, "testTrackWithNoContext", context = context, builder = trackerBuilder)
val eventStore = emitter.eventStore
val isClean = eventStore.removeAllEvents()
Log.i("testTrackSelfDescribingEvent", "EventStore clean: $isClean")
Expand Down Expand Up @@ -265,7 +265,7 @@ class TrackerTest {
tracker.screenViewAutotracking = false
}
Companion.tracker =
Tracker(emitter!!, namespace, "testTrackWithNoContext", null, context, trackerBuilder)
Tracker(emitter!!, namespace, "testTrackWithNoContext", context = context, builder = trackerBuilder)

Log.i("testTrackWithNoContext", "Send ScreenView event")
Companion.tracker!!.track(ScreenView("name"))
Expand Down Expand Up @@ -317,7 +317,7 @@ class TrackerTest {
tracker.exceptionAutotracking = false
tracker.screenViewAutotracking = false
}
Companion.tracker = Tracker(emitter, namespace, "myAppId", null, context, trackerBuilder)
Companion.tracker = Tracker(emitter, namespace, "myAppId", context = context, builder = trackerBuilder)
Companion.tracker!!.pauseEventTracking()
val eventId = Companion.tracker!!.track(ScreenView("name"))
Assert.assertNull(eventId)
Expand Down Expand Up @@ -351,7 +351,7 @@ class TrackerTest {
tracker.foregroundTimeout = 5
tracker.backgroundTimeout = 5
}
Companion.tracker = Tracker(emitter, namespace, "myAppId", null, context, trackerBuilder)
Companion.tracker = Tracker(emitter, namespace, "myAppId", context = context, builder = trackerBuilder)
Assert.assertNotNull(Companion.tracker!!.session)
Companion.tracker!!.resumeSessionChecking()
Thread.sleep(2000)
Expand Down Expand Up @@ -380,7 +380,7 @@ class TrackerTest {
tracker.foregroundTimeout = 5
tracker.backgroundTimeout = 5
}
Companion.tracker = Tracker(emitter, namespace, "myAppId", null, context, trackerBuilder)
Companion.tracker = Tracker(emitter, namespace, "myAppId", context = context, builder = trackerBuilder)
val screenState = Companion.tracker!!.getScreenState()
Assert.assertNotNull(screenState)
var screenStateMapWrapper: Map<String, Any?> = screenState!!.getCurrentScreen(true).map
Expand Down Expand Up @@ -423,7 +423,7 @@ class TrackerTest {
tracker.exceptionAutotracking = true
tracker.screenViewAutotracking = false
}
Companion.tracker = Tracker(emitter, namespace, "myAppId", null, context, trackerBuilder)
Companion.tracker = Tracker(emitter, namespace, "myAppId", context = context, builder = trackerBuilder)
Assert.assertTrue(Companion.tracker!!.exceptionAutotracking)
Assert.assertEquals(
ExceptionHandler::class.java,
Expand Down Expand Up @@ -451,7 +451,7 @@ class TrackerTest {
tracker.exceptionAutotracking = false
tracker.screenViewAutotracking = false
}
Companion.tracker = Tracker(emitter, namespace, "myAppId", null, context, trackerBuilder)
Companion.tracker = Tracker(emitter, namespace, "myAppId", context = context, builder = trackerBuilder)
val handler1 = ExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler(handler1)
Assert.assertEquals(
Expand Down Expand Up @@ -481,7 +481,7 @@ class TrackerTest {
tracker.foregroundTimeout = 5
tracker.backgroundTimeout = 5
}
Companion.tracker = Tracker(emitter, "ns", "myAppId", null, context, trackerBuilder)
Companion.tracker = Tracker(emitter, "ns", "myAppId", context = context, builder = trackerBuilder)

Companion.tracker!!.track(Structured("c", "a"))
val sessionIdStart = Companion.tracker!!.session!!.state!!.sessionId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class LoggingTest {
"namespace",
"myAppId",
null,
ApplicationProvider.getApplicationContext(),
trackerBuilder
context = ApplicationProvider.getApplicationContext(),
builder = trackerBuilder
)
Assert.assertTrue(mockLoggerDelegate!!.capturedLogs.contains("Session checking has been resumed. (debug)"))
Assert.assertTrue(mockLoggerDelegate!!.capturedLogs.contains("Tracker created successfully. (verbose)"))
Expand Down Expand Up @@ -107,8 +107,8 @@ class LoggingTest {
"namespace",
"myAppId",
null,
ApplicationProvider.getApplicationContext(),
trackerBuilder
context = ApplicationProvider.getApplicationContext(),
builder = trackerBuilder
)
Assert.assertTrue(mockLoggerDelegate!!.capturedLogs.contains("Session checking has been resumed. (debug)"))
Assert.assertFalse(mockLoggerDelegate!!.capturedLogs.contains("Tracker created successfully. (verbose)"))
Expand Down Expand Up @@ -141,8 +141,8 @@ class LoggingTest {
"namespace",
"myAppId",
null,
ApplicationProvider.getApplicationContext(),
trackerBuilder
context = ApplicationProvider.getApplicationContext(),
builder = trackerBuilder
)
Assert.assertFalse(mockLoggerDelegate!!.capturedLogs.contains("Session checking has been resumed. (debug)"))
Assert.assertFalse(mockLoggerDelegate!!.capturedLogs.contains("Tracker created successfully. (verbose)"))
Expand Down
Loading

0 comments on commit 9ae3a3f

Please sign in to comment.