Skip to content

Crashlytics smoke test stubs #2737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.content.pm.PackageManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
Expand Down Expand Up @@ -84,6 +85,7 @@ public class FirebaseCrashlytics {

final ExecutorService crashHandlerExecutor =
ExecutorUtils.buildSingleThreadExecutorService("Crashlytics Exception Handler");

final CrashlyticsCore core =
new CrashlyticsCore(
app,
Expand Down Expand Up @@ -156,7 +158,8 @@ public Void call() throws Exception {
return new FirebaseCrashlytics(core);
}

private final CrashlyticsCore core;
@VisibleForTesting // accessible for smoke tests
final CrashlyticsCore core;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you still add @VisibleForTesting?


private FirebaseCrashlytics(@NonNull CrashlyticsCore core) {
this.core = core;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
Expand Down Expand Up @@ -76,10 +77,11 @@ public class CrashlyticsCore {
private boolean didCrashOnPreviousExecution;

private CrashlyticsController controller;

private final IdManager idManager;
private final BreadcrumbSource breadcrumbSource;

@VisibleForTesting public final BreadcrumbSource breadcrumbSource;
private final AnalyticsEventLogger analyticsEventLogger;

private final ExecutorService crashHandlerExecutor;
private final CrashlyticsBackgroundWorker backgroundWorker;

Expand Down
3 changes: 3 additions & 0 deletions smoke-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ buildscript {
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"
classpath "com.google.gms:google-services:4.3.8"
classpath "com.google.firebase:firebase-crashlytics-gradle:2.7.0"
}
}

Expand Down Expand Up @@ -69,6 +70,7 @@ dependencies {
implementation "com.google.firebase:firebase-auth"
implementation "com.google.firebase:firebase-common"
implementation "com.google.firebase:firebase-config"
implementation "com.google.firebase:firebase-crashlytics"
implementation "com.google.firebase:firebase-database"
implementation "com.google.firebase:firebase-dynamic-links"
implementation "com.google.firebase:firebase-firestore"
Expand Down Expand Up @@ -101,3 +103,4 @@ clean.doLast {
}

apply plugin: "com.google.gms.google-services"
apply plugin: "com.google.firebase.crashlytics"
8 changes: 8 additions & 0 deletions smoke-tests/smoke-test-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@
public <methods>;
}

-keepclassmembers class com.google.firebase.crashlytics.FirebaseCrashlytics {
private com.google.firebase.crashlytics.internal.common.CrashlyticsCore core;
}

-keepclassmembers class com.google.firebase.crashlytics.internal.common.CrashlyticsCore {
private com.google.firebase.crashlytics.internal.breadcrumbs.BreadcrumbSource breadcrumbSource;
}

-dontwarn android.**
-dontwarn okio.**
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.crashlytics;

import static com.google.common.truth.Truth.assertThat;

import android.os.Bundle;
import androidx.test.core.app.ApplicationProvider;
import com.google.firebase.analytics.FirebaseAnalytics;

import androidx.test.runner.AndroidJUnit4;
import com.google.firebase.crashlytics.internal.breadcrumbs.BreadcrumbSource;
import com.google.firebase.crashlytics.internal.common.CrashlyticsCore;
import java.lang.reflect.Field;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* This class is in the com.google.firebase.crashlytics package to access FirebaseCrashlytics's
* package-private fields.
**/
@RunWith(AndroidJUnit4.class)
public final class CrashlyticsTest {

@Test
public void analyticsIntegration() {
// Validates that Firebase Analytics and Crashlytics interoperability is working, by confirming
// that events sent to Firebase Analytics are received by the Crashlytics breadcrumb handler.
try {
BreadcrumbSource breadcrumbSource = FirebaseCrashlytics.getInstance().core.breadcrumbSource;
final CountDownLatch eventReceivedLatch = new CountDownLatch(1);
breadcrumbSource.registerBreadcrumbHandler(breadcrumbHandler -> {
eventReceivedLatch.countDown();
});

Bundle eventBundle = new Bundle();
eventBundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "testName");
FirebaseAnalytics.getInstance(ApplicationProvider.getApplicationContext()).logEvent(
FirebaseAnalytics.Event.APP_OPEN, eventBundle);

// Wait up to 2 seconds, which is plenty of time for the event
eventReceivedLatch.await(2000, TimeUnit.MILLISECONDS);
assertThat(eventReceivedLatch.getCount()).isEqualTo(0);

} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Test
public void setCustomKeys() {
// For now, simply validate that the API does not throw any exception. A more robust functional
// test could be implemented via reflection or monitoring logcat.
FirebaseCrashlytics.getInstance().setCustomKey("TestKey", "TestValue");
}

@Test
public void log() {
// For now, simply validate that the API does not throw any exception. A more robust functional
// test could be implemented via reflection or monitoring logcat.
FirebaseCrashlytics.getInstance().log("This is a log message");
}

@Test
public void didCrashOnPreviousExecution() {
assertThat(FirebaseCrashlytics.getInstance().didCrashOnPreviousExecution()).isFalse();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.firebase.testing;

import com.google.firebase.crashlytics.CrashlyticsTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

Expand All @@ -25,6 +26,7 @@
@RunWith(Suite.class)
@Suite.SuiteClasses({
BuildOnlyTest.class,
CrashlyticsTest.class,
DatabaseTest.class,
DynamicLinksTest.class,
FirestoreTest.class,
Expand Down