Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

- Fallback to Current Activity Holder when React Context Activity is not present ([#4779](https://github.com/getsentry/sentry-react-native/pull/4779))

### Fixes

- Initialize Sentry Android with ApplicationContext if available ([#4780](https://github.com/getsentry/sentry-react-native/pull/4780))

### Dependencies

- Bump Cocoa SDK from v8.49.1 to v8.49.2 ([#4792](https://github.com/getsentry/sentry-react-native/pull/4792))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.sentry.react

import android.app.Application
import org.junit.Assert.assertSame
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.mock
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class RNSentryModuleInitWithApplicationTest {
@Test
fun `when application context is null fallback to react context`() {
val mockedReactContext = Utils.makeReactContextMock()
whenever(mockedReactContext.applicationContext).thenReturn(null)

assertSame(RNSentryModuleImpl(mockedReactContext).applicationContext, mockedReactContext)
}

@Test
fun `use application context if available`() {
val mockedApplicationContext = mock(Application::class.java)
val mockedReactContext = Utils.makeReactContextMock()
whenever(mockedReactContext.applicationContext).thenReturn(mockedApplicationContext)

assertSame(RNSentryModuleImpl(mockedReactContext).applicationContext, mockedApplicationContext)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ import org.mockito.kotlin.whenever

class Utils {
companion object {
fun createRNSentryModuleWithMockedContext(): RNSentryModuleImpl {
fun makeReactContextMock(): ReactApplicationContext {
val packageManager = mock(PackageManager::class.java)
val packageInfo = mock(PackageInfo::class.java)

val reactContext = mock(ReactApplicationContext::class.java)
whenever(reactContext.packageManager).thenReturn(packageManager)
whenever(packageManager.getPackageInfo(anyString(), anyInt())).thenReturn(packageInfo)

return reactContext
}

fun createRNSentryModuleWithMockedContext(): RNSentryModuleImpl {
RNSentryModuleImpl.lastStartTimestampMs = -1

return RNSentryModuleImpl(reactContext)
return RNSentryModuleImpl(makeReactContextMock())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import java.util.concurrent.CountDownLatch;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

public class RNSentryModuleImpl {

Expand Down Expand Up @@ -177,12 +178,22 @@ public void initNativeReactNavigationNewFrameTracking(Promise promise) {

public void initNativeSdk(final ReadableMap rnOptions, Promise promise) {
SentryAndroid.init(
this.getReactApplicationContext(),
options -> getSentryAndroidOptions(options, rnOptions, logger));
getApplicationContext(), options -> getSentryAndroidOptions(options, rnOptions, logger));

promise.resolve(true);
}

@TestOnly
protected Context getApplicationContext() {
final Context context = this.getReactApplicationContext().getApplicationContext();
if (context == null) {
logger.log(
SentryLevel.ERROR, "ApplicationContext is null, using ReactApplicationContext fallback.");
return this.getReactApplicationContext();
}
return context;
}

protected void getSentryAndroidOptions(
@NotNull SentryAndroidOptions options, @NotNull ReadableMap rnOptions, ILogger logger) {
@Nullable SdkVersion sdkVersion = options.getSdkVersion();
Expand Down
Loading