Skip to content

Commit

Permalink
Refactor and test RNSentryModuleImpl.fetchNativeDeviceContexts (#4253)
Browse files Browse the repository at this point in the history
* Refactor fetchNativeDeviceContexts for testability

* Test fetchNativeDeviceContexts

* Adds new line at the end
  • Loading branch information
antonis authored Nov 11, 2024
1 parent fef2173 commit 76285fa
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.sentry.react

import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import com.facebook.react.bridge.PromiseImpl
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.WritableMap
import com.facebook.soloader.SoLoader
import io.sentry.Breadcrumb
import io.sentry.Scope
import io.sentry.SentryOptions
import io.sentry.android.core.SentryAndroidOptions
import org.junit.Assert.assertEquals
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class RNSentryModuleImplTest {

private lateinit var module: RNSentryModuleImpl
private lateinit var context: Context

@Before
fun setUp() {
context = InstrumentationRegistry.getInstrumentation().targetContext
SoLoader.init(context, false)
val reactContext = ReactApplicationContext(context)
module = RNSentryModuleImpl(reactContext)
}

@Test
fun fetchNativeDeviceContextsWithNullContext() {
val options = SentryAndroidOptions()
val scope = Scope(options)
val promise = PromiseImpl({
assertEquals(1, it.size)
assertEquals(null, it[0])
}, {
fail("Promise was rejected unexpectedly")
})
module.fetchNativeDeviceContexts(promise, options, null, scope)
}

@Test
fun fetchNativeDeviceContextsWithInvalidSentryOptions() {
class NotAndroidSentryOptions : SentryOptions()

val options = NotAndroidSentryOptions()
val scope = Scope(options)
val promise = PromiseImpl({
assertEquals(1, it.size)
assertEquals(null, it[0])
}, {
fail("Promise was rejected unexpectedly")
})
module.fetchNativeDeviceContexts(promise, options, context, scope)
}

@Test
fun fetchNativeDeviceContextsFiltersBreadcrumbs() {
val options = SentryAndroidOptions().apply { maxBreadcrumbs = 5 }
val scope = Scope(options)
scope.addBreadcrumb(Breadcrumb("Breadcrumb1-RN").apply { origin = "react-native" })
scope.addBreadcrumb(Breadcrumb("Breadcrumb2-Native"))
scope.addBreadcrumb(Breadcrumb("Breadcrumb3-Native").apply { origin = "java" })
scope.addBreadcrumb(Breadcrumb("Breadcrumb2-RN").apply { origin = "react-native" })
scope.addBreadcrumb(Breadcrumb("Breadcrumb2-RN").apply { origin = "react-native" })

val promise = PromiseImpl({
assertEquals(1, it.size)
assertEquals(true, it[0] is WritableMap)
val actual = it[0] as WritableMap
val breadcrumbs = actual.getArray("breadcrumbs")
assertEquals(2, breadcrumbs?.size())
assertEquals("Breadcrumb2-Native", breadcrumbs?.getMap(0)?.getString("message"))
assertEquals("Breadcrumb3-Native", breadcrumbs?.getMap(1)?.getString("message"))
}, {
fail("Promise was rejected unexpectedly")
})

module.fetchNativeDeviceContexts(promise, options, context, scope)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -888,18 +888,24 @@ private String readStringFromFile(File path) throws IOException {

public void fetchNativeDeviceContexts(Promise promise) {
final @NotNull SentryOptions options = HubAdapter.getInstance().getOptions();
final @Nullable Context context = this.getReactApplicationContext().getApplicationContext();
final @Nullable IScope currentScope = InternalSentrySdk.getCurrentScope();
fetchNativeDeviceContexts(promise, options, context, currentScope);
}

protected void fetchNativeDeviceContexts(
Promise promise,
final @NotNull SentryOptions options,
final @Nullable Context context,
final @Nullable IScope currentScope) {
if (!(options instanceof SentryAndroidOptions)) {
promise.resolve(null);
return;
}

final @Nullable Context context = this.getReactApplicationContext().getApplicationContext();
if (context == null) {
promise.resolve(null);
return;
}

final @Nullable IScope currentScope = InternalSentrySdk.getCurrentScope();
if (currentScope != null) {
// Remove react-native breadcrumbs
Iterator<Breadcrumb> breadcrumbsIterator = currentScope.getBreadcrumbs().iterator();
Expand Down

0 comments on commit 76285fa

Please sign in to comment.