Skip to content
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

test: add e2e tests for Crashlytics #6559

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions firebase-crashlytics/test-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/google-services.json
17 changes: 17 additions & 0 deletions firebase-crashlytics/test-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Firebase Crashlytics Test App

## Setup

Download the `google-services.json` file
from [Firebase Console](https://console.firebase.google.com/) (for whatever Firebase project you
have or want to integrate the `test-app`) and store it under the current directory.

Note: The [Package name](https://firebase.google.com/docs/android/setup#register-app) for your app
created on the Firebase Console (for which the `google-services.json` is downloaded) must match
the [applicationId](https://developer.android.com/studio/build/application-id.html) declared in
the `test-app/test-app.gradle.kts` for the app to link to Firebase.

## Running

Run the test app directly from Android Studio by selecting and running
the `firebase-crashlytics.test-app` run configuration.
60 changes: 60 additions & 0 deletions firebase-crashlytics/test-app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.google.gms.google-services")
id("com.google.firebase.crashlytics")
//kotlin("android") version "1.9.0" apply false
}

android {
namespace = "com.example.test_app"
compileSdk = 35

defaultConfig {
applicationId = "com.example.test_app"
minSdk = 24
targetSdk = 35
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
buildFeatures {
viewBinding = true
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
//implementation(libs.firebase.crashlytics.ktx)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation("androidx.test.uiautomator:uiautomator:2.2.0")
androidTestImplementation("com.google.truth:truth:1.1.3")
implementation("androidx.navigation:navigation-fragment-ktx:2.6.0")
implementation("androidx.navigation:navigation-ui-ktx:2.6.0")
implementation("com.google.firebase:firebase-common-ktx:21.0.0")
implementation("com.google.firebase:firebase-crashlytics-ktx:19.2.1")
implementation("androidx.multidex:multidex:2.0.1")
}
1 change: 1 addition & 0 deletions firebase-crashlytics/test-app/multidex-config.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-keep class com.google.firebase.** { *; }
21 changes: 21 additions & 0 deletions firebase-crashlytics/test-app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
28 changes: 28 additions & 0 deletions firebase-crashlytics/test-app/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2023 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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:versionCode="1"
android:versionName="1.0.0">

<application>

</application>

<uses-sdk tools:overrideLibrary="android_libs.ub_uiautomator" />

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* Copyright 2023 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.testing.crashlytics

import android.content.Context
import android.content.Intent
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiObject2
import androidx.test.uiautomator.Until
import com.google.common.truth.Truth.assertThat
import java.util.regex.Pattern
import org.junit.After
import org.junit.Assert
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class FirebaseCrashlyticsIntegrationTest {

private lateinit var device: UiDevice

@Before
fun setup() {
// Initialize UiDevice instance
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
}

@After
fun cleanup() {
// Make sure all processes are killed
Runtime.getRuntime().exec(arrayOf("am", "force-stop", TEST_APP_PACKAGE))
}

@Test
fun sharedInitializeCrashlytics() {
// Enable verbose logging for FirebaseCrashlytics
Runtime.getRuntime().exec(arrayOf("adb", "shell", "setprop", "log.tag.FirebaseCrashlytics", "VERBOSE")).waitFor()

// Launch the app
launchApp()

// Start monitoring logs
val logcatProcess = ProcessBuilder("adb", "logcat", "-s", "FirebaseCrashlytics")
.redirectErrorStream(true)
.start()

val logcatReader = logcatProcess.inputStream.bufferedReader()

// Flag to detect Crashlytics initialization
var crashlyticsInitialized = false

// Monitor log output
logcatReader.useLines { lines ->
for (line in lines) {
if (line.contains("FirebaseCrashlytics is initialized")) {
crashlyticsInitialized = true
break
}
}
}

// Clean up the logcat process
logcatProcess.destroy()

// Assert that Crashlytics was initialized
assertThat(crashlyticsInitialized).isTrue()
}


// Keeping here for now as an example
@Test
fun sameSessionIdBetweenActivitiesOnDifferentProcesses() {
launchApp()

val sessionId1 = getCurrentSessionId()
navigateToSecondActivity()
Thread.sleep(TIME_TO_PROPAGATE_SESSION)
val sessionId2 = getCurrentSessionId()

assertThat(sessionId1).isEqualTo(sessionId2)
}


private fun launchApp() {
// Start from the home screen
device.pressHome()

// Wait for launcher
device.wait(Until.hasObject(By.pkg(device.launcherPackageName).depth(0)), LAUNCH_TIMEOUT)

// Launch the app
val context = ApplicationProvider.getApplicationContext<Context>()
val intent =
context.packageManager.getLaunchIntentForPackage(TEST_APP_PACKAGE)?.apply {
// Clear out any previous instances
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
}
context.startActivity(intent)

// Wait for the app to appear
device.wait(Until.hasObject(By.pkg(TEST_APP_PACKAGE).depth(0)), LAUNCH_TIMEOUT)
device.waitForIdle()
}

private fun navigateToSecondActivity() {
device.wait(Until.hasObject(By.text("NEXT ACTIVITY").depth(0)), TRANSITION_TIMEOUT)
val nextActivityButton =
device.findObject(By.text("NEXT ACTIVITY").clazz("android.widget.Button"))
nextActivityButton?.click()
device.wait(Until.hasObject(By.pkg(TEST_APP_PACKAGE).depth(0)), TRANSITION_TIMEOUT)
}

private fun getButton(text: String): UiObject2 {
device.wait(Until.hasObject(By.text(text).depth(0)), TRANSITION_TIMEOUT)
val button = device.findObject(By.text(text).clazz("android.widget.Button"))
if (button == null) {
fail("Could not locate button with text $text")
}
return button
}

private fun dismissPossibleAnrDialog() {
device.wait(
Until.hasObject(By.clazz("com.android.server.am.AppNotRespondingDialog")),
TRANSITION_TIMEOUT
)
device.findObject(By.text("Close app").clazz("android.widget.Button"))?.click()
}

private fun dismissPossibleErrorDialog() {
device.wait(
Until.hasObject(By.clazz("com.android.server.am.AppErrorDialog")),
TRANSITION_TIMEOUT
)
device.findObject(By.text("Close app").clazz("android.widget.Button"))?.click()
}

private fun background() {
device.pressHome()
device.wait(Until.hasObject(By.pkg(device.launcherPackageName).depth(0)), TRANSITION_TIMEOUT)
}

private fun foreground() {
device.pressRecentApps()
Thread.sleep(1_000L)
device.click(device.displayWidth / 2, device.displayHeight / 2)
device.wait(Until.hasObject(By.pkg(TEST_APP_PACKAGE).depth(0)), TRANSITION_TIMEOUT)
device.waitForIdle()
}

private fun getCurrentSessionId(): String? {
device.wait(
Until.hasObject(By.res(Pattern.compile(".*session_id_(fragment|second)_text")).depth(0)),
TRANSITION_TIMEOUT
)
return device.findObject(By.res(Pattern.compile(".*session_id_(fragment|second)_text")))?.text
}

companion object {
private const val TEST_APP_PACKAGE = "com.google.firebase.testing.crashlytics"
private const val LAUNCH_TIMEOUT = 5_000L
private const val TRANSITION_TIMEOUT = 1_000L
private const val TIME_TO_PROPAGATE_SESSION = 5_000L
}
}
Loading
Loading