Skip to content

Commit a78f6bd

Browse files
authored
Crashlytics smoke test stubs (#2737)
One test validates the Analytics integration; the rest are simple stubs for now.
1 parent 1b0fdcf commit a78f6bd

File tree

6 files changed

+104
-3
lines changed

6 files changed

+104
-3
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import android.content.pm.PackageManager;
1919
import androidx.annotation.NonNull;
2020
import androidx.annotation.Nullable;
21+
import androidx.annotation.VisibleForTesting;
2122
import com.google.android.gms.tasks.Continuation;
2223
import com.google.android.gms.tasks.Task;
2324
import com.google.android.gms.tasks.Tasks;
@@ -84,6 +85,7 @@ public class FirebaseCrashlytics {
8485

8586
final ExecutorService crashHandlerExecutor =
8687
ExecutorUtils.buildSingleThreadExecutorService("Crashlytics Exception Handler");
88+
8789
final CrashlyticsCore core =
8890
new CrashlyticsCore(
8991
app,
@@ -156,7 +158,8 @@ public Void call() throws Exception {
156158
return new FirebaseCrashlytics(core);
157159
}
158160

159-
private final CrashlyticsCore core;
161+
@VisibleForTesting // accessible for smoke tests
162+
final CrashlyticsCore core;
160163

161164
private FirebaseCrashlytics(@NonNull CrashlyticsCore core) {
162165
this.core = core;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.util.Log;
2020
import androidx.annotation.NonNull;
2121
import androidx.annotation.Nullable;
22+
import androidx.annotation.VisibleForTesting;
2223
import com.google.android.gms.tasks.Task;
2324
import com.google.android.gms.tasks.Tasks;
2425
import com.google.firebase.FirebaseApp;
@@ -76,10 +77,11 @@ public class CrashlyticsCore {
7677
private boolean didCrashOnPreviousExecution;
7778

7879
private CrashlyticsController controller;
79-
8080
private final IdManager idManager;
81-
private final BreadcrumbSource breadcrumbSource;
81+
82+
@VisibleForTesting public final BreadcrumbSource breadcrumbSource;
8283
private final AnalyticsEventLogger analyticsEventLogger;
84+
8385
private final ExecutorService crashHandlerExecutor;
8486
private final CrashlyticsBackgroundWorker backgroundWorker;
8587

smoke-tests/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ buildscript {
2424
dependencies {
2525
classpath "com.android.tools.build:gradle:4.2.2"
2626
classpath "com.google.gms:google-services:4.3.8"
27+
classpath "com.google.firebase:firebase-crashlytics-gradle:2.7.0"
2728
}
2829
}
2930

@@ -69,6 +70,7 @@ dependencies {
6970
implementation "com.google.firebase:firebase-auth"
7071
implementation "com.google.firebase:firebase-common"
7172
implementation "com.google.firebase:firebase-config"
73+
implementation "com.google.firebase:firebase-crashlytics"
7274
implementation "com.google.firebase:firebase-database"
7375
implementation "com.google.firebase:firebase-dynamic-links"
7476
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.crashlytics.internal.breadcrumbs.BreadcrumbSource breadcrumbSource;
28+
}
29+
2230
-dontwarn android.**
2331
-dontwarn okio.**
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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;
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+
23+
import androidx.test.runner.AndroidJUnit4;
24+
import com.google.firebase.crashlytics.internal.breadcrumbs.BreadcrumbSource;
25+
import com.google.firebase.crashlytics.internal.common.CrashlyticsCore;
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+
/**
33+
* This class is in the com.google.firebase.crashlytics package to access FirebaseCrashlytics's
34+
* package-private fields.
35+
**/
36+
@RunWith(AndroidJUnit4.class)
37+
public final class CrashlyticsTest {
38+
39+
@Test
40+
public void analyticsIntegration() {
41+
// Validates that Firebase Analytics and Crashlytics interoperability is working, by confirming
42+
// that events sent to Firebase Analytics are received by the Crashlytics breadcrumb handler.
43+
try {
44+
BreadcrumbSource breadcrumbSource = FirebaseCrashlytics.getInstance().core.breadcrumbSource;
45+
final CountDownLatch eventReceivedLatch = new CountDownLatch(1);
46+
breadcrumbSource.registerBreadcrumbHandler(breadcrumbHandler -> {
47+
eventReceivedLatch.countDown();
48+
});
49+
50+
Bundle eventBundle = new Bundle();
51+
eventBundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "testName");
52+
FirebaseAnalytics.getInstance(ApplicationProvider.getApplicationContext()).logEvent(
53+
FirebaseAnalytics.Event.APP_OPEN, eventBundle);
54+
55+
// Wait up to 2 seconds, which is plenty of time for the event
56+
eventReceivedLatch.await(2000, TimeUnit.MILLISECONDS);
57+
assertThat(eventReceivedLatch.getCount()).isEqualTo(0);
58+
59+
} catch (Exception e) {
60+
throw new RuntimeException(e);
61+
}
62+
}
63+
64+
@Test
65+
public void setCustomKeys() {
66+
// For now, simply validate that the API does not throw any exception. A more robust functional
67+
// test could be implemented via reflection or monitoring logcat.
68+
FirebaseCrashlytics.getInstance().setCustomKey("TestKey", "TestValue");
69+
}
70+
71+
@Test
72+
public void log() {
73+
// For now, simply validate that the API does not throw any exception. A more robust functional
74+
// test could be implemented via reflection or monitoring logcat.
75+
FirebaseCrashlytics.getInstance().log("This is a log message");
76+
}
77+
78+
@Test
79+
public void didCrashOnPreviousExecution() {
80+
assertThat(FirebaseCrashlytics.getInstance().didCrashOnPreviousExecution()).isFalse();
81+
}
82+
}
83+

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.firebase.testing;
1616

17+
import com.google.firebase.crashlytics.CrashlyticsTest;
1718
import org.junit.runner.RunWith;
1819
import org.junit.runners.Suite;
1920

@@ -25,6 +26,7 @@
2526
@RunWith(Suite.class)
2627
@Suite.SuiteClasses({
2728
BuildOnlyTest.class,
29+
CrashlyticsTest.class,
2830
DatabaseTest.class,
2931
DynamicLinksTest.class,
3032
FirestoreTest.class,

0 commit comments

Comments
 (0)