Skip to content

Commit

Permalink
Merge branch 'master' into refactor-api-code
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench authored Aug 19, 2024
2 parents 2692b79 + 5ce858a commit e56063c
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ internal fun IntegrationTestRule.Harness.getLastSavedSession(): Envelope<Session
return overriddenDeliveryModule.deliveryService.getLastSavedSession()
}

/**
* Returns the last background activity that was saved by the SDK.
*/
internal fun IntegrationTestRule.Harness.getLastSavedBackgroundActivity(): Envelope<SessionPayload>? {
return overriddenDeliveryModule.deliveryService.getLastSavedBackgroundActivity()
}

/**
* Returns the last session that was sent by the SDK.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package io.embrace.android.embracesdk.features
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.embrace.android.embracesdk.IntegrationTestRule
import io.embrace.android.embracesdk.findSessionSpan
import io.embrace.android.embracesdk.getLastSavedBackgroundActivity
import io.embrace.android.embracesdk.getLastSentBackgroundActivity
import io.embrace.android.embracesdk.getSentBackgroundActivities
import io.embrace.android.embracesdk.internal.payload.Span
import io.embrace.android.embracesdk.internal.payload.getSessionSpan
import io.embrace.android.embracesdk.internal.spans.getSessionProperty
import io.embrace.android.embracesdk.recordSession
import org.junit.Assert.assertEquals
Expand Down Expand Up @@ -120,6 +122,23 @@ internal class SessionPropertiesTest {
}
}

@Test
fun `adding properties in bg activity modifications change the cached payload`() {
with(testRule) {
startSdk()
harness.recordSession()
embrace.addSessionProperty("temp", "value", false)
val bgSnapshot = checkNotNull(harness.getLastSavedBackgroundActivity())
checkNotNull(bgSnapshot.getSessionSpan()).assertPropertyExistence(exist = listOf("temp"))

val session = checkNotNull(harness.recordSession())
checkNotNull(session.getSessionSpan()).assertPropertyExistence(missing = listOf("temp"))

val bg = checkNotNull(harness.getLastSentBackgroundActivity())
checkNotNull(bg.getSessionSpan()).assertPropertyExistence(exist = listOf("temp"))
}
}

private fun Span.assertPropertyExistence(exist: List<String> = emptyList(), missing: List<String> = emptyList()) {
exist.forEach {
assertNotNull(getSessionProperty(it), "Missing session property with key: $it")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal class BackgroundActivityDisabledTest {
// Check what should and shouldn't be logged when there is no background activity and the app is in the background
assertTrue(embrace.isStarted)
assertTrue(embrace.currentSessionId.isNullOrBlank())
assertTrue(embrace.getDeviceId().isNotBlank())
assertTrue(embrace.deviceId.isNotBlank())
assertNull(embrace.startSpan("test"))
embrace.logError("error")
runLoggingThread()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import io.embrace.android.embracesdk.IntegrationTestRule
import io.embrace.android.embracesdk.findSessionSpan
import io.embrace.android.embracesdk.getSentBackgroundActivities
import io.embrace.android.embracesdk.internal.spans.findAttributeValue
import io.embrace.android.embracesdk.getSessionId
import io.embrace.android.embracesdk.internal.opentelemetry.embSessionNumber
import io.embrace.android.embracesdk.internal.spans.findAttributeValue
import io.embrace.android.embracesdk.recordSession
import io.embrace.android.embracesdk.getSessionId
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface SdkStateApi {
*/
public fun setAppId(appId: String): Boolean

public fun getDeviceId(): String
public val deviceId: String

public val currentSessionId: String?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ internal class SdkStateApiDelegate(
return true
}

override fun getDeviceId(): String = when {
sdkCallChecker.check("get_device_id") ->
preferencesService?.deviceIdentifier
?: ""

else -> ""
}
override val deviceId: String
get() {
return if (sdkCallChecker.check("get_device_id")) {
preferencesService?.deviceIdentifier ?: ""
} else {
""
}
}

override val currentSessionId: String?
get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ internal class SessionApiDelegate(
*/
override fun addSessionProperty(key: String, value: String, permanent: Boolean): Boolean {
if (sdkCallChecker.check("add_session_property")) {
return sessionPropertiesService?.addProperty(key, value, permanent) ?: false
return sessionPropertiesService?.addProperty(key, value, permanent)
.apply {
sessionOrchestrator?.reportBackgroundActivityStateChange()
} ?: false
}
return false
}
Expand All @@ -31,7 +34,9 @@ internal class SessionApiDelegate(
*/
override fun removeSessionProperty(key: String): Boolean {
if (sdkCallChecker.check("remove_session_property")) {
return sessionPropertiesService?.removeProperty(key) ?: false
return sessionPropertiesService?.removeProperty(key).apply {
sessionOrchestrator?.reportBackgroundActivityStateChange()
} ?: false
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ internal class SdkStateApiDelegateTest {
@Test
fun getDeviceId() {
preferencesService.deviceIdentifier = "foo"
assertEquals("foo", delegate.getDeviceId())
assertEquals("foo", delegate.deviceId)
}

@Test
fun `device ID not returned SDK is not enabled`() {
preferencesService.deviceIdentifier = "foo"
sdkCallChecker.started.set(false)
assertEquals("", delegate.deviceId)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.embrace.android.embracesdk.fakes.FakeTelemetryService
import io.embrace.android.embracesdk.fakes.fakeModuleInitBootstrapper
import io.embrace.android.embracesdk.internal.payload.AppFramework
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
Expand All @@ -19,6 +20,7 @@ internal class SessionApiDelegateTest {

private lateinit var delegate: SessionApiDelegate
private lateinit var orchestrator: FakeSessionOrchestrator
private lateinit var sdkCallChecker: SdkCallChecker
private lateinit var sessionPropertiesService: FakeSessionPropertiesService

@Before
Expand All @@ -27,29 +29,42 @@ internal class SessionApiDelegateTest {
moduleInitBootstrapper.init(ApplicationProvider.getApplicationContext(), AppFramework.NATIVE, 0)
orchestrator = moduleInitBootstrapper.sessionOrchestrationModule.sessionOrchestrator as FakeSessionOrchestrator
sessionPropertiesService = moduleInitBootstrapper.essentialServiceModule.sessionPropertiesService as FakeSessionPropertiesService

val sdkCallChecker = SdkCallChecker(FakeEmbLogger(), FakeTelemetryService())
sdkCallChecker = SdkCallChecker(FakeEmbLogger(), FakeTelemetryService())
sdkCallChecker.started.set(true)
delegate = SessionApiDelegate(moduleInitBootstrapper, sdkCallChecker)
}

@Test
fun `cannot modify session properties when SDK is not enabled`() {
sdkCallChecker.started.set(false)
assertFalse(delegate.addSessionProperty("test", "value", false))
assertFalse(delegate.removeSessionProperty("test"))
assertEquals(0, orchestrator.stateChangeCount)
delegate.endSession()
assertEquals(0, orchestrator.manualEndCount)
}

@Test
fun `add session property`() {
delegate.addSessionProperty("test", "value", false)
assertEquals("value", sessionPropertiesService.props["test"])
assertEquals(1, orchestrator.stateChangeCount)
}

@Test
fun `remove session property`() {
delegate.addSessionProperty("test", "value", false)
delegate.removeSessionProperty("test")
assertNull(sessionPropertiesService.props["test"])
assertEquals(2, orchestrator.stateChangeCount)
}

@Suppress("DEPRECATION")
@Test
fun `get session properties`() {
sessionPropertiesService.props["key"] = "value"
assertEquals(mapOf("key" to "value"), delegate.getSessionProperties())
assertEquals(0, orchestrator.stateChangeCount)
}

@Test
Expand Down

0 comments on commit e56063c

Please sign in to comment.