Skip to content

Commit

Permalink
Update method names
Browse files Browse the repository at this point in the history
  • Loading branch information
bidetofevil committed Nov 28, 2024
1 parent 6495572 commit 54431ea
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import io.embrace.android.embracesdk.internal.utils.VersionChecker
import io.opentelemetry.sdk.common.Clock

/**
* Maps [ActivityLifecycleCallbacks] events to [OpenEvents] depending on the current state of the app and capabilities of the OS.
* Maps [ActivityLifecycleCallbacks] events to [UiLoadEvents] depending on the current state of the app and capabilities of the OS.
*
* The purpose of this is to leverage Activity lifecycle events to provide data for the underlying workflow to bring a new Activity on
* screen. Due to the varying capabilities of the APIs available on the different versions of Android, the precise triggering events for
* the start and intermediate steps may differ.
*
* See [OpenTraceEmitter] for details.
* See [UiLoadTraceEmitter] for details.
*/
class OpenEventEmitter(
private val openEvents: OpenEvents,
class UiLoadEventEmitter(
private val uiLoadEvents: UiLoadEvents,
private val clock: Clock,
private val versionChecker: VersionChecker,
) : ActivityLifecycleListener {
Expand Down Expand Up @@ -69,75 +69,73 @@ class OpenEventEmitter(
}

override fun onActivityPrePaused(activity: Activity) {
resetTrace(activity)
abandonTrace(activity)
}

override fun onActivityPaused(activity: Activity) {
if (!versionChecker.firePrePostEvents()) {
resetTrace(activity)
abandonTrace(activity)
}
}

override fun onActivityStopped(activity: Activity) {
hibernate(activity)
reset(activity)
}

private fun resetTrace(activity: Activity) {
openEvents.resetTrace(
private fun abandonTrace(activity: Activity) {
uiLoadEvents.abandon(
instanceId = traceInstanceId(activity),
activityName = activity.localClassName,
timestampMs = nowMs()
)
}

private fun hibernate(activity: Activity) {
openEvents.hibernate(
private fun reset(activity: Activity) {
uiLoadEvents.reset(
instanceId = traceInstanceId(activity),
activityName = activity.localClassName,
timestampMs = nowMs()
)
}

private fun create(activity: Activity) {
openEvents.create(
uiLoadEvents.create(
instanceId = traceInstanceId(activity),
activityName = activity.localClassName,
timestampMs = nowMs()
)
}

private fun createEnd(activity: Activity) {
openEvents.createEnd(
uiLoadEvents.createEnd(
instanceId = traceInstanceId(activity),
timestampMs = nowMs()
)
}

private fun start(activity: Activity) {
openEvents.start(
uiLoadEvents.start(
instanceId = traceInstanceId(activity),
activityName = activity.localClassName,
timestampMs = nowMs()
)
}

private fun startEnd(activity: Activity) {
openEvents.startEnd(
uiLoadEvents.startEnd(
instanceId = traceInstanceId(activity),
timestampMs = nowMs()
)
}

private fun resume(activity: Activity) {
openEvents.resume(
uiLoadEvents.resume(
instanceId = traceInstanceId(activity),
activityName = activity.localClassName,
timestampMs = nowMs()
)
}

private fun resumeEnd(activity: Activity) {
openEvents.resumeEnd(
uiLoadEvents.resumeEnd(
instanceId = traceInstanceId(activity),
timestampMs = nowMs()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.embrace.android.embracesdk.internal.ClockTickingActivityLifecycleCallb
import io.embrace.android.embracesdk.internal.ClockTickingActivityLifecycleCallbacks.Companion.POST_DURATION
import io.embrace.android.embracesdk.internal.ClockTickingActivityLifecycleCallbacks.Companion.PRE_DURATION
import io.embrace.android.embracesdk.internal.ClockTickingActivityLifecycleCallbacks.Companion.STATE_DURATION
import io.embrace.android.embracesdk.internal.capture.activity.OpenEventEmitterTest.FakeOpenEvents.EventData
import io.embrace.android.embracesdk.internal.capture.activity.UiLoadEventEmitterTest.FakeOpenEvents.EventData
import io.embrace.android.embracesdk.internal.utils.BuildVersionChecker
import org.junit.Assert.assertEquals
import org.junit.Before
Expand All @@ -21,10 +21,10 @@ import org.robolectric.android.controller.ActivityController
import org.robolectric.annotation.Config

@RunWith(AndroidJUnit4::class)
internal class OpenEventEmitterTest {
internal class UiLoadEventEmitterTest {
private lateinit var clock: FakeClock
private lateinit var openEvents: FakeOpenEvents
private lateinit var eventEmitter: OpenEventEmitter
private lateinit var eventEmitter: UiLoadEventEmitter
private lateinit var activityController: ActivityController<*>
private var startTimeMs: Long = 0L
private var instanceId = 0
Expand All @@ -36,8 +36,8 @@ internal class OpenEventEmitterTest {
val initModule = FakeInitModule(clock = clock)
clock.tick(100L)
openEvents = FakeOpenEvents()
eventEmitter = OpenEventEmitter(
openEvents = openEvents,
eventEmitter = UiLoadEventEmitter(
uiLoadEvents = openEvents,
clock = initModule.openTelemetryModule.openTelemetryClock,
versionChecker = BuildVersionChecker,
)
Expand Down Expand Up @@ -82,12 +82,11 @@ internal class OpenEventEmitterTest {
timestampMs = startTimeMs + (POST_DURATION + STATE_DURATION + PRE_DURATION) * 3
),
createEvent(
stage = "resetTrace",
stage = "abandon",
timestampMs = startTimeMs + (POST_DURATION + STATE_DURATION + PRE_DURATION) * 3 + PRE_DURATION
),
createEvent(
stage = "hibernate",
timestampMs = startTimeMs + (POST_DURATION + STATE_DURATION + PRE_DURATION) * 4 + STATE_DURATION
stage = "reset",
),
)
)
Expand Down Expand Up @@ -116,12 +115,11 @@ internal class OpenEventEmitterTest {
timestampMs = startTimeMs + (POST_DURATION + STATE_DURATION + PRE_DURATION) * 2
),
createEvent(
stage = "resetTrace",
stage = "abandon",
timestampMs = startTimeMs + (POST_DURATION + STATE_DURATION + PRE_DURATION) * 2 + PRE_DURATION
),
createEvent(
stage = "hibernate",
timestampMs = startTimeMs + (POST_DURATION + STATE_DURATION + PRE_DURATION) * 3 + STATE_DURATION
stage = "reset",
),
)
)
Expand Down Expand Up @@ -154,12 +152,11 @@ internal class OpenEventEmitterTest {
timestampMs = startTimeMs + STATE_DURATION * 3
),
createEvent(
stage = "resetTrace",
stage = "abandon",
timestampMs = startTimeMs + STATE_DURATION * 4
),
createEvent(
stage = "hibernate",
timestampMs = startTimeMs + STATE_DURATION * 5
stage = "reset",
),
)
)
Expand Down Expand Up @@ -188,12 +185,11 @@ internal class OpenEventEmitterTest {
timestampMs = startTimeMs + STATE_DURATION * 2
),
createEvent(
stage = "resetTrace",
stage = "abandon",
timestampMs = startTimeMs + STATE_DURATION * 3
),
createEvent(
stage = "hibernate",
timestampMs = startTimeMs + STATE_DURATION * 4
stage = "reset",
),
)
)
Expand All @@ -217,35 +213,33 @@ internal class OpenEventEmitterTest {
assertEquals(expectedEvents.map { it.stage to it.timestampMs }, map { it.stage to it.timestampMs })
}

private fun createEvent(stage: String, timestampMs: Long) =
private fun createEvent(stage: String, timestampMs: Long? = null) =
EventData(
stage = stage,
instanceId = instanceId,
activityName = activityName,
timestampMs = timestampMs
)

class FakeOpenEvents : OpenEvents {
class FakeOpenEvents : UiLoadEvents {
val events = mutableListOf<EventData>()

override fun resetTrace(instanceId: Int, activityName: String, timestampMs: Long) {
override fun abandon(instanceId: Int, activityName: String, timestampMs: Long) {
events.add(
EventData(
stage = "resetTrace",
stage = "abandon",
instanceId = instanceId,
activityName = activityName,
timestampMs = timestampMs
)
)
}

override fun hibernate(instanceId: Int, activityName: String, timestampMs: Long) {
override fun reset(instanceId: Int) {
events.add(
EventData(
stage = "hibernate",
stage = "reset",
instanceId = instanceId,
activityName = activityName,
timestampMs = timestampMs
)
)
}
Expand Down Expand Up @@ -339,7 +333,7 @@ internal class OpenEventEmitterTest {
val stage: String,
val instanceId: Int,
val activityName: String? = null,
val timestampMs: Long,
val timestampMs: Long? = null,
)
}
}

0 comments on commit 54431ea

Please sign in to comment.