Skip to content

Commit 050cc89

Browse files
authored
Add isCrashlyticsCollectionEnabled() API (#6080)
FR: #5919 Tested Manually
1 parent 3dc208c commit 050cc89

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed

firebase-crashlytics/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
# 19.0.3
55
* [changed] Update the internal file system to handle long file names.
6+
* [feature] Added the `isCrashlyticsCollectionEnabled` API to check if Crashlytics collection is enabled.
7+
([Github #5919](//github.com/firebase/firebase-android-sdk/issues/5919))
68

79

810
## Kotlin

firebase-crashlytics/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package com.google.firebase.crashlytics {
2323
method public void log(@NonNull String);
2424
method public void recordException(@NonNull Throwable);
2525
method public void sendUnsentReports();
26+
method public boolean isCrashlyticsCollectionEnabled();
2627
method public void setCrashlyticsCollectionEnabled(boolean);
2728
method public void setCrashlyticsCollectionEnabled(@Nullable Boolean);
2829
method public void setCustomKey(@NonNull String, boolean);

firebase-crashlytics/firebase-crashlytics.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ dependencies {
9797
testImplementation(libs.junit)
9898
testImplementation(libs.mockito.core)
9999
testImplementation(libs.robolectric)
100+
testImplementation(libs.truth)
100101

101102
androidTestImplementation(libs.androidx.test.core)
102103
androidTestImplementation(libs.androidx.test.runner)

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/FirebaseCrashlytics.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,20 @@ public boolean didCrashOnPreviousExecution() {
457457
return core.didCrashOnPreviousExecution();
458458
}
459459

460+
/**
461+
* Indicates whether or not automatic data collection is enabled
462+
*
463+
* @return In order of priority:
464+
* <p>If {@link #setCrashlyticsCollectionEnabled(boolean)} is called with a value, use it</p>
465+
*
466+
* <p>If the <b>firebase_crashlytics_collection_enabled</b> key is in your app’s AndroidManifest.xml, use it</p>
467+
*
468+
* <p>Otherwise, use the default {@link FirebaseApp#isDataCollectionDefaultEnabled()} in FirebaseApp</p>
469+
*/
470+
public boolean isCrashlyticsCollectionEnabled() {
471+
return core.isCrashlyticsCollectionEnabled();
472+
}
473+
460474
/**
461475
* Enables or disables the automatic data collection configuration for Crashlytics.
462476
*

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/CrashlyticsCore.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ private Task<Void> doBackgroundInitialization(SettingsProvider settingsProvider)
281281

282282
// endregion
283283

284+
public boolean isCrashlyticsCollectionEnabled() {
285+
return dataCollectionArbiter.isAutomaticDataCollectionEnabled();
286+
}
287+
284288
public void setCrashlyticsCollectionEnabled(@Nullable Boolean enabled) {
285289
dataCollectionArbiter.setCrashlyticsDataCollectionEnabled(enabled);
286290
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.crashlytics.internal.common;
16+
17+
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
18+
import static org.mockito.Mockito.mock;
19+
import static org.mockito.Mockito.when;
20+
import static com.google.common.truth.Truth.assertThat;
21+
import static org.robolectric.Shadows.shadowOf;
22+
23+
import android.content.Context;
24+
import android.os.Bundle;
25+
26+
import com.google.firebase.FirebaseApp;
27+
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.robolectric.RobolectricTestRunner;
32+
33+
@RunWith(RobolectricTestRunner.class)
34+
public class DataCollectionArbiterRobolectricTest {
35+
36+
private Context testContext;
37+
private FirebaseApp firebaseApp;
38+
39+
private static final String FIREBASE_CRASHLYTICS_COLLECTION_ENABLED =
40+
"firebase_crashlytics_collection_enabled";
41+
42+
@Before
43+
public void setUp() {
44+
testContext = getApplicationContext();
45+
firebaseApp = mock(FirebaseApp.class);
46+
when(firebaseApp.getApplicationContext()).thenReturn(testContext);
47+
}
48+
49+
private DataCollectionArbiter getDataCollectionArbiter(FirebaseApp app) {
50+
return new DataCollectionArbiter(app);
51+
}
52+
53+
@Test
54+
public void testSetCrashlyticsDataCollectionEnabled_overridesOtherSettings() {
55+
// Ensure that Manifest metadata is set to false.
56+
editManifestApplicationMetadata(testContext)
57+
.putBoolean(FIREBASE_CRASHLYTICS_COLLECTION_ENABLED, false);
58+
59+
// Mock FirebaseApp to return default data collection as false.
60+
when(firebaseApp.isDataCollectionDefaultEnabled()).thenReturn(false);
61+
62+
DataCollectionArbiter arbiter = getDataCollectionArbiter(firebaseApp);
63+
64+
// Setting explicitly to true should override both manifest and default settings.
65+
arbiter.setCrashlyticsDataCollectionEnabled(true);
66+
assertThat(arbiter.isAutomaticDataCollectionEnabled()).isTrue();
67+
68+
// Setting explicitly to false should also override the previous value
69+
arbiter.setCrashlyticsDataCollectionEnabled(false);
70+
assertThat(arbiter.isAutomaticDataCollectionEnabled()).isFalse();
71+
72+
arbiter.setCrashlyticsDataCollectionEnabled(null);
73+
//Expecting `false` result since manifest metadata value is `false`
74+
assertThat(arbiter.isAutomaticDataCollectionEnabled()).isFalse();
75+
}
76+
77+
@Test
78+
public void testManifestMetadata_respectedWhenNoOverride() {
79+
editManifestApplicationMetadata(testContext)
80+
.putBoolean(FIREBASE_CRASHLYTICS_COLLECTION_ENABLED, true);
81+
82+
DataCollectionArbiter arbiter = getDataCollectionArbiter(firebaseApp);
83+
84+
assertThat(arbiter.isAutomaticDataCollectionEnabled()).isTrue();
85+
86+
editManifestApplicationMetadata(testContext)
87+
.putBoolean(FIREBASE_CRASHLYTICS_COLLECTION_ENABLED, false);
88+
89+
arbiter = getDataCollectionArbiter(firebaseApp);
90+
91+
assertThat(arbiter.isAutomaticDataCollectionEnabled()).isFalse();
92+
}
93+
94+
@Test
95+
public void testDefaultDataCollection_usedWhenNoOverrideOrManifestSetting() {
96+
editManifestApplicationMetadata(testContext)
97+
.remove(FIREBASE_CRASHLYTICS_COLLECTION_ENABLED);
98+
99+
DataCollectionArbiter arbiter = getDataCollectionArbiter(firebaseApp);
100+
101+
when(firebaseApp.isDataCollectionDefaultEnabled()).thenReturn(true);
102+
assertThat(arbiter.isAutomaticDataCollectionEnabled()).isTrue();
103+
104+
when(firebaseApp.isDataCollectionDefaultEnabled()).thenReturn(false);
105+
assertThat(arbiter.isAutomaticDataCollectionEnabled()).isFalse();
106+
107+
//No Test of `null` return for firebaseApp.isDataCollectionDefaultEnabled(), since it will never return `null` value
108+
}
109+
110+
private Bundle editManifestApplicationMetadata(Context context) {
111+
return shadowOf(context.getPackageManager())
112+
.getInternalMutablePackageInfo(context.getPackageName())
113+
.applicationInfo
114+
.metaData;
115+
}
116+
}

0 commit comments

Comments
 (0)