From 98c47cf223f0c2d512855cbbecd063530c76f273 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Fri, 8 Nov 2024 18:37:55 +0200 Subject: [PATCH 1/3] Refactor fetchNativeDeviceContexts for testability --- .../java/io/sentry/react/RNSentryModuleImpl.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/core/android/src/main/java/io/sentry/react/RNSentryModuleImpl.java b/packages/core/android/src/main/java/io/sentry/react/RNSentryModuleImpl.java index 388b45dc7a..5ae47a0ae6 100644 --- a/packages/core/android/src/main/java/io/sentry/react/RNSentryModuleImpl.java +++ b/packages/core/android/src/main/java/io/sentry/react/RNSentryModuleImpl.java @@ -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 breadcrumbsIterator = currentScope.getBreadcrumbs().iterator(); From b7ad111d2107bce3906c7902a36934a9e6472194 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Fri, 8 Nov 2024 18:38:08 +0200 Subject: [PATCH 2/3] Test fetchNativeDeviceContexts --- .../io/sentry/react/RNSentryModuleImplTest.kt | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 packages/core/RNSentryAndroidTester/app/src/androidTest/java/io/sentry/react/RNSentryModuleImplTest.kt diff --git a/packages/core/RNSentryAndroidTester/app/src/androidTest/java/io/sentry/react/RNSentryModuleImplTest.kt b/packages/core/RNSentryAndroidTester/app/src/androidTest/java/io/sentry/react/RNSentryModuleImplTest.kt new file mode 100644 index 0000000000..946435d47f --- /dev/null +++ b/packages/core/RNSentryAndroidTester/app/src/androidTest/java/io/sentry/react/RNSentryModuleImplTest.kt @@ -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) + } +} \ No newline at end of file From ada50fa86d939e30462f766e10016b60da932aa2 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Fri, 8 Nov 2024 18:41:24 +0200 Subject: [PATCH 3/3] Adds new line at the end --- .../androidTest/java/io/sentry/react/RNSentryModuleImplTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/RNSentryAndroidTester/app/src/androidTest/java/io/sentry/react/RNSentryModuleImplTest.kt b/packages/core/RNSentryAndroidTester/app/src/androidTest/java/io/sentry/react/RNSentryModuleImplTest.kt index 946435d47f..d314420758 100644 --- a/packages/core/RNSentryAndroidTester/app/src/androidTest/java/io/sentry/react/RNSentryModuleImplTest.kt +++ b/packages/core/RNSentryAndroidTester/app/src/androidTest/java/io/sentry/react/RNSentryModuleImplTest.kt @@ -83,4 +83,4 @@ class RNSentryModuleImplTest { module.fetchNativeDeviceContexts(promise, options, context, scope) } -} \ No newline at end of file +}