Skip to content

Commit

Permalink
feature(exitInfo) addPreOnSend callBack test (#1902)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmartbearYing authored Sep 18, 2023
1 parent 1afd928 commit 461bb50
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal data class CallbackState(
val onErrorTasks: MutableCollection<OnErrorCallback> = CopyOnWriteArrayList(),
val onBreadcrumbTasks: MutableCollection<OnBreadcrumbCallback> = CopyOnWriteArrayList(),
val onSessionTasks: MutableCollection<OnSessionCallback> = CopyOnWriteArrayList(),
val onSendTasks: MutableCollection<OnSendCallback> = CopyOnWriteArrayList()
val onSendTasks: MutableList<OnSendCallback> = CopyOnWriteArrayList()
) : CallbackAware {

private var internalMetrics: InternalMetrics = InternalMetricsNoop()
Expand Down Expand Up @@ -67,6 +67,11 @@ internal data class CallbackState(
}
}

fun addPreOnSend(onSend: OnSendCallback) {
onSendTasks.add(0, onSend)
internalMetrics.notifyAddCallback(onSendName)
}

fun removeOnSend(onSend: OnSendCallback) {
if (onSendTasks.remove(onSend)) {
internalMetrics.notifyRemoveCallback(onSendName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1187,4 +1187,8 @@ void setAutoNotify(boolean autoNotify) {
void setAutoDetectAnrs(boolean autoDetectAnrs) {
pluginClient.setAutoDetectAnrs(this, autoDetectAnrs);
}

void addOnSend(OnSendCallback callback) {
callbackState.addPreOnSend(callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,16 @@ class CallbackStateTest {
assertFalse(state.runOnSendTasks({ event }, NoopLogger))
assertEquals(0, count)
}

@Test
fun testOnPreSend() {
val state = CallbackState()
state.addOnSend(OnSendCallback { true })
val onSendCallBack = state.onSendTasks.first()
state.addPreOnSend(OnSendCallback { true })
val expectedOnSendCallBackPosition = state.onSendTasks.indexOf(onSendCallBack)

assertEquals(2, state.onSendTasks.size)
assertEquals(1, expectedOnSendCallBackPosition)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
public final class com/bugsnag/android/BugsnagExitInfoPlugin : com/bugsnag/android/OnSendCallback, com/bugsnag/android/Plugin {
public final class com/bugsnag/android/BugsnagExitInfoPlugin : com/bugsnag/android/Plugin {
public fun <init> ()V
public fun <init> (Z)V
public fun <init> (ZZ)V
public fun <init> (ZZZ)V
public synthetic fun <init> (ZZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun load (Lcom/bugsnag/android/Client;)V
public fun onSend (Lcom/bugsnag/android/Event;)Z
public fun unload ()V
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ class BugsnagExitInfoPlugin @JvmOverloads constructor(
*/
private val disableProcessStateSummaryOverride: Boolean = false

) : Plugin, OnSendCallback {
) : Plugin {

private var exitInfoCallback: ExitInfoCallback? = null

override fun onSend(event: Event): Boolean {
return exitInfoCallback?.onSend(event) ?: true
}

override fun load(client: Client) {
if (!disableProcessStateSummaryOverride) {
client.addOnSession(
OnSessionCallback { session: Session ->
val am = client.appContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val am =
client.appContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
am.setProcessStateSummary(session.id.toByteArray())
return@OnSessionCallback true
}
Expand All @@ -47,6 +44,7 @@ class BugsnagExitInfoPlugin @JvmOverloads constructor(
TombstoneEventEnhancer(client.logger),
TraceEventEnhancer(client.logger, client.immutableConfig.projectPackages)
)
client.addOnSend(exitInfoCallback)
}

override fun unload() {
Expand Down

0 comments on commit 461bb50

Please sign in to comment.