Skip to content

Commit 03afcd7

Browse files
committed
Crashlytics smoke tests
One test validates the Analytics integration; the rest are simple stubbs for now.
1 parent c816404 commit 03afcd7

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

smoke-tests/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ buildscript {
2525
dependencies {
2626
classpath "com.android.tools.build:gradle:3.4.3"
2727
classpath "com.google.gms:google-services:4.3.0"
28+
classpath "com.google.firebase:firebase-crashlytics-gradle:2.7.0"
2829
}
2930
}
3031

@@ -70,6 +71,7 @@ dependencies {
7071
implementation "com.google.firebase:firebase-auth"
7172
implementation "com.google.firebase:firebase-common"
7273
implementation "com.google.firebase:firebase-config"
74+
implementation "com.google.firebase:firebase-crashlytics"
7375
implementation "com.google.firebase:firebase-database"
7476
implementation "com.google.firebase:firebase-dynamic-links"
7577
implementation "com.google.firebase:firebase-firestore"
@@ -101,3 +103,4 @@ clean.doLast {
101103
}
102104

103105
apply plugin: "com.google.gms.google-services"
106+
apply plugin: "com.google.firebase.crashlytics"

smoke-tests/smoke-test-rules.pro

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,13 @@
1919
public <methods>;
2020
}
2121

22+
-keepclassmembers class com.google.firebase.crashlytics.FirebaseCrashlytics {
23+
private com.google.firebase.crashlytics.internal.common.CrashlyticsCore core;
24+
}
25+
26+
-keepclassmembers class com.google.firebase.crashlytics.internal.common.CrashlyticsCore {
27+
private com.google.firebase.internal.breadcrumbs.BreadcrumbSource breadcrumbSource;
28+
}
29+
2230
-dontwarn android.**
2331
-dontwarn okio.**
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+

smoke-tests/src/main/java/com/google/firebase/testing/TestSuite.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
@RunWith(Suite.class)
2626
@Suite.SuiteClasses({
2727
BuildOnlyTest.class,
28+
CrashlyticsTest.class,
2829
DatabaseTest.class,
2930
DynamicLinksTest.class,
3031
FirestoreTest.class,

0 commit comments

Comments
 (0)