|
| 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 | +} |
0 commit comments