Skip to content

Commit d0fea0e

Browse files
authored
Add tests for internal apis used by development platform plugins (#6140)
Add test cases for internal apis used by development platform plugins so we don't break them by accident during refactoring. We should consider making the internal api more explicit.
1 parent 10fe142 commit d0fea0e

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.crashlytics;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import androidx.test.core.app.ApplicationProvider;
22+
import com.google.firebase.FirebaseApp;
23+
import com.google.firebase.FirebaseOptions;
24+
import com.google.firebase.crashlytics.internal.common.CrashlyticsCore;
25+
import com.google.firebase.crashlytics.internal.common.DataCollectionArbiter;
26+
import java.lang.reflect.Field;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
31+
/** Tests for the internal apis that development platform plugins call. */
32+
public class CrashlyticsPluginsTest {
33+
private static final String APP_ID = "1:1:android:1a";
34+
private static final String API_KEY = "API-KEY-API-KEY-API-KEY-API-KEY-API-KEY";
35+
private static final String PROJECT_ID = "PROJECT-ID";
36+
37+
@Before
38+
public void setUp() {
39+
FirebaseApp.initializeApp(
40+
ApplicationProvider.getApplicationContext(),
41+
new FirebaseOptions.Builder()
42+
.setApplicationId(APP_ID)
43+
.setApiKey(API_KEY)
44+
.setProjectId(PROJECT_ID)
45+
.build());
46+
}
47+
48+
@After
49+
public void tearDown() {
50+
FirebaseApp.clearInstancesForTest();
51+
}
52+
53+
@Test
54+
public void accessCrashlyticsCore() {
55+
// Both Flutter and Unity plugins access CrashlyticsCore from FirebaseCrashlytics.core field.
56+
CrashlyticsCore core = FirebaseCrashlytics.getInstance().core;
57+
assertThat(core).isNotNull();
58+
59+
// Verify the internal method logFatalException exists without reflection.
60+
Runnable logFatalException = () -> core.logFatalException(new Throwable());
61+
assertThat(logFatalException).isNotNull();
62+
63+
// Verify the internal method setInternalKey exists without reflection.
64+
Runnable setInternalKey = () -> core.setInternalKey("", "");
65+
assertThat(setInternalKey).isNotNull();
66+
}
67+
68+
@Test
69+
public void accessDataCollection() throws Exception {
70+
// The Unity plugin accesses CrashlyticsCore.dataCollectionArbiter this via reflection.
71+
CrashlyticsCore core = FirebaseCrashlytics.getInstance().core;
72+
Field field = core.getClass().getDeclaredField("dataCollectionArbiter");
73+
field.setAccessible(true); // The dataCollectionArbiter field is private in CrashlyticsCore.
74+
DataCollectionArbiter dataCollectionArbiter = (DataCollectionArbiter) field.get(core);
75+
76+
assertThat(dataCollectionArbiter).isNotNull();
77+
}
78+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ public class FirebaseCrashlytics {
172172
return new FirebaseCrashlytics(core);
173173
}
174174

175-
@VisibleForTesting // accessible for smoke tests
176-
final CrashlyticsCore core;
175+
/** Accessible for smoke tests, Unity, and Flutter plugins. */
176+
@VisibleForTesting final CrashlyticsCore core;
177177

178178
private FirebaseCrashlytics(@NonNull CrashlyticsCore core) {
179179
this.core = core;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ public class CrashlyticsCore {
7474

7575
private final Context context;
7676
private final FirebaseApp app;
77+
78+
/** This field is accessed by the Unity plugin via reflection. */
7779
private final DataCollectionArbiter dataCollectionArbiter;
80+
7881
private final OnDemandCounter onDemandCounter;
7982

8083
private final long startTime;

0 commit comments

Comments
 (0)