Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/crashlytics/usage/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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> firebaseCrashlytics =
mockStatic(FirebaseCrashlytics.class);
MockedStatic<ReactNativeFirebasePreferences> preferences =
mockStatic(ReactNativeFirebasePreferences.class);
MockedStatic<ReactNativeFirebaseCrashlyticsInitProvider> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

+ (void)deleteUnsentReports;

+ (void)setCrashlyticsCollectionEnabled:(BOOL)enabled;

+ (BOOL)didCrashDuringPreviousExecution;

+ (void)log:(NSString *)message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ + (void)deleteUnsentReports {
[[FIRCrashlytics crashlytics] deleteUnsentReports];
}

+ (void)setCrashlyticsCollectionEnabled:(BOOL)enabled {
[[FIRCrashlytics crashlytics] setCrashlyticsCollectionEnabled:enabled];
}

+ (BOOL)didCrashDuringPreviousExecution {
return [[FIRCrashlytics crashlytics] didCrashDuringPreviousExecution];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down
3 changes: 3 additions & 0 deletions packages/crashlytics/lib/types/crashlytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<null>;
Expand Down