|
| 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.testing; |
| 16 | + |
| 17 | +import static com.google.common.truth.Truth.assertThat; |
| 18 | + |
| 19 | +import android.os.Bundle; |
| 20 | +import androidx.test.core.app.ApplicationProvider; |
| 21 | +import com.google.firebase.analytics.FirebaseAnalytics; |
| 22 | +import com.google.firebase.crashlytics.FirebaseCrashlytics; |
| 23 | + |
| 24 | +import androidx.test.runner.AndroidJUnit4; |
| 25 | +import com.google.firebase.crashlytics.internal.breadcrumbs.BreadcrumbSource; |
| 26 | +import java.lang.reflect.Field; |
| 27 | +import java.util.concurrent.CountDownLatch; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | + |
| 32 | +@RunWith(AndroidJUnit4.class) |
| 33 | +public final class CrashlyticsTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void analyticsIntegration() { |
| 37 | + // Validates that Firebase Analytics and Crashlytics interoperability is working, by confirming |
| 38 | + // that events sent to Firebase Analytics are received by the Crashlytics breadcrumb handler. |
| 39 | + try { |
| 40 | + // need to use reflection to get the breadcrumb handler. |
| 41 | + FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); |
| 42 | + Field coreField = crashlytics.getClass().getDeclaredField("core"); |
| 43 | + coreField.setAccessible(true); |
| 44 | + Object core = coreField.get(crashlytics); |
| 45 | + Field breadcrumbSourceField = core.getClass().getDeclaredField("breadcrumbSource"); |
| 46 | + breadcrumbSourceField.setAccessible(true); |
| 47 | + BreadcrumbSource breadcrumbSource = (BreadcrumbSource)breadcrumbSourceField.get(core); |
| 48 | + |
| 49 | + final CountDownLatch eventReceivedLatch = new CountDownLatch(1); |
| 50 | + breadcrumbSource.registerBreadcrumbHandler(breadcrumbHandler -> { |
| 51 | + eventReceivedLatch.countDown(); |
| 52 | + }); |
| 53 | + |
| 54 | + Bundle eventBundle = new Bundle(); |
| 55 | + eventBundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "testName"); |
| 56 | + FirebaseAnalytics.getInstance(ApplicationProvider.getApplicationContext()).logEvent( |
| 57 | + FirebaseAnalytics.Event.APP_OPEN, eventBundle); |
| 58 | + |
| 59 | + // Wait up to 2 seconds, which is plenty of time for the event |
| 60 | + eventReceivedLatch.await(2000, TimeUnit.MILLISECONDS); |
| 61 | + assertThat(eventReceivedLatch.getCount()).isEqualTo(0); |
| 62 | + |
| 63 | + } catch (Exception e) { |
| 64 | + throw new RuntimeException(e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void setCustomKeys() { |
| 70 | + // For now, simply validate that the API does not throw any exception. A more robust functional |
| 71 | + // test could be implemented via reflection or monitoring logcat. |
| 72 | + FirebaseCrashlytics.getInstance().setCustomKey("TestKey", "TestValue"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void log() { |
| 77 | + // For now, simply validate that the API does not throw any exception. A more robust functional |
| 78 | + // test could be implemented via reflection or monitoring logcat. |
| 79 | + FirebaseCrashlytics.getInstance().log("This is a log message"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void didCrashOnPreviousExecution() { |
| 84 | + assertThat(FirebaseCrashlytics.getInstance().didCrashOnPreviousExecution()).isFalse(); |
| 85 | + } |
| 86 | +} |
| 87 | + |
0 commit comments