diff --git a/docs/crashlytics/usage/index.mdx b/docs/crashlytics/usage/index.mdx index 76f6100fa4..a4ca2ac16c 100644 --- a/docs/crashlytics/usage/index.mdx +++ b/docs/crashlytics/usage/index.mdx @@ -204,6 +204,7 @@ This can be done throughout the app with a simple method call to `setCrashlytics - `setCrashlyticsCollectionEnabled(crashlytics, enabled)` throws if `enabled` is not a boolean and returns a `Promise`. - The module stores the value on the device, so it is used on later launches and takes priority over the `crashlytics_auto_collection_enabled` value in `firebase.json`. +- The value is also passed to the native Crashlytics SDK straight away. Firebase guarantees the change from the next app launch, so don't rely on it for crashes later in the same run. - `crashlytics.isCrashlyticsCollectionEnabled` is a read-only property that reflects the last value you set, or the startup value if you have not set one. - In a debug build without `crashlytics_debug_enabled`, native collection stays disabled even after you call `setCrashlyticsCollectionEnabled(crashlytics, true)`, although the property then reads `true`. diff --git a/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java b/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java index 1068f9799f..94472b4f5e 100644 --- a/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java +++ b/packages/crashlytics/android/src/main/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlytics.java @@ -182,9 +182,21 @@ public void setUserId(String userId, Promise promise) { @Override public void setCrashlyticsCollectionEnabled(boolean enabled, Promise promise) { + saveAndApplyCollectionEnabled(enabled); + promise.resolve(null); + } + + /** + * Saves the value for later launches and passes the native SDK the effective value straight away. + * The effective value still honours crashlytics_debug_enabled in debug builds. Firebase + * guarantees the change from the next app launch. + */ + static void saveAndApplyCollectionEnabled(boolean enabled) { ReactNativeFirebasePreferences.getSharedInstance() .setBooleanValue(Constants.KEY_CRASHLYTICS_AUTO_COLLECTION_ENABLED, enabled); - promise.resolve(null); + FirebaseCrashlytics.getInstance() + .setCrashlyticsCollectionEnabled( + ReactNativeFirebaseCrashlyticsInitProvider.isCrashlyticsCollectionEnabled()); } @Override diff --git a/packages/crashlytics/android/src/test/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlyticsCollectionEnabledTest.java b/packages/crashlytics/android/src/test/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlyticsCollectionEnabledTest.java new file mode 100644 index 0000000000..351dccab8d --- /dev/null +++ b/packages/crashlytics/android/src/test/java/io/invertase/firebase/crashlytics/NativeRNFBTurboCrashlyticsCollectionEnabledTest.java @@ -0,0 +1,78 @@ +package io.invertase.firebase.crashlytics; + +/* + * Copyright (c) 2016-present Invertase Limited & Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this library except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.verify; + +import com.google.firebase.crashlytics.FirebaseCrashlytics; +import io.invertase.firebase.common.ReactNativeFirebasePreferences; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockedStatic; +import org.robolectric.RobolectricTestRunner; + +/** + * JVM coverage for {@link NativeRNFBTurboCrashlytics#saveAndApplyCollectionEnabled(boolean)}. The + * SDK must receive the init provider's effective value, not the raw argument, so a debug build + * without crashlytics_debug_enabled stays disabled after setCrashlyticsCollectionEnabled(true). + */ +@RunWith(RobolectricTestRunner.class) +public class NativeRNFBTurboCrashlyticsCollectionEnabledTest { + + /** Runs the helper with the SDK, preferences and init provider stubbed; returns the SDK mock. */ + private static FirebaseCrashlytics saveAndApply(boolean enabled, boolean effective) { + FirebaseCrashlytics crashlytics = mock(FirebaseCrashlytics.class); + ReactNativeFirebasePreferences prefs = mock(ReactNativeFirebasePreferences.class); + + try (MockedStatic firebaseCrashlytics = + mockStatic(FirebaseCrashlytics.class); + MockedStatic preferences = + mockStatic(ReactNativeFirebasePreferences.class); + MockedStatic initProvider = + mockStatic(ReactNativeFirebaseCrashlyticsInitProvider.class)) { + firebaseCrashlytics.when(FirebaseCrashlytics::getInstance).thenReturn(crashlytics); + preferences.when(ReactNativeFirebasePreferences::getSharedInstance).thenReturn(prefs); + initProvider + .when(ReactNativeFirebaseCrashlyticsInitProvider::isCrashlyticsCollectionEnabled) + .thenReturn(effective); + + NativeRNFBTurboCrashlytics.saveAndApplyCollectionEnabled(enabled); + + verify(prefs).setBooleanValue(Constants.KEY_CRASHLYTICS_AUTO_COLLECTION_ENABLED, enabled); + } + return crashlytics; + } + + @Test + public void passesTheEffectiveValueNotTheArgument() { + // A debug build without crashlytics_debug_enabled: the provider computes false. + verify(saveAndApply(true, false)).setCrashlyticsCollectionEnabled(false); + } + + @Test + public void passesTrueWhenCollectionIsEffectivelyEnabled() { + verify(saveAndApply(true, true)).setCrashlyticsCollectionEnabled(true); + } + + @Test + public void passesFalseOnWithdrawal() { + verify(saveAndApply(false, false)).setCrashlyticsCollectionEnabled(false); + } +} diff --git a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.h b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.h index 134a851fb7..677b2e6efb 100644 --- a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.h +++ b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.h @@ -34,6 +34,8 @@ + (void)deleteUnsentReports; ++ (void)setCrashlyticsCollectionEnabled:(BOOL)enabled; + + (BOOL)didCrashDuringPreviousExecution; + (void)log:(NSString *)message; diff --git a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.m b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.m index d813facbff..381352c979 100644 --- a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.m +++ b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsHelper.m @@ -57,6 +57,10 @@ + (void)deleteUnsentReports { [[FIRCrashlytics crashlytics] deleteUnsentReports]; } ++ (void)setCrashlyticsCollectionEnabled:(BOOL)enabled { + [[FIRCrashlytics crashlytics] setCrashlyticsCollectionEnabled:enabled]; +} + + (BOOL)didCrashDuringPreviousExecution { return [[FIRCrashlytics crashlytics] didCrashDuringPreviousExecution]; } diff --git a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm index 8e81bb9f2c..76849b146a 100644 --- a/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm +++ b/packages/crashlytics/ios/RNFBCrashlytics/RNFBCrashlyticsModule.mm @@ -194,6 +194,10 @@ - (void)setCrashlyticsCollectionEnabled:(BOOL)enabled (void)reject; [[RNFBPreferences shared] setBooleanValue:@"crashlytics_auto_collection_enabled" boolValue:enabled]; + // Also pass the SDK the value straight away. Firebase guarantees it from the next app launch. + // The effective value still honours crashlytics_debug_enabled in debug builds. + [RNFBCrashlyticsHelper + setCrashlyticsCollectionEnabled:[RNFBCrashlyticsInitProvider isCrashlyticsCollectionEnabled]]; resolve([NSNull null]); } diff --git a/packages/crashlytics/lib/types/crashlytics.ts b/packages/crashlytics/lib/types/crashlytics.ts index e7c18ff0c8..5a40d12cf2 100644 --- a/packages/crashlytics/lib/types/crashlytics.ts +++ b/packages/crashlytics/lib/types/crashlytics.ts @@ -108,6 +108,9 @@ export interface Crashlytics extends ReactNativeFirebase.FirebaseModule { * * Use this for opt-in first user data collection flows combined with `firebase.json` settings to disable auto collection. * + * The value is also passed to the native Crashlytics SDK straight away. Firebase guarantees the change from the + * next app launch, so don't rely on it for crashes later in the same run. + * * @param enabled A boolean value representing whether to enable Crashlytics error collection. */ setCrashlyticsCollectionEnabled(enabled: boolean): Promise;