Skip to content

Commit

Permalink
clean and skip test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyokone committed Jan 10, 2025
1 parent cddc06b commit 7aa5e77
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,24 @@ 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.Direction
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.Until
import com.google.common.truth.Truth.assertThat
import com.google.firebase.crashlytics.FirebaseCrashlytics
import java.util.Locale
import java.util.regex.Pattern
import org.junit.After
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.util.Locale
import java.util.regex.Pattern

const val APP_NAME = "com.google.firebase.testing.crashlytics"

/**
* Integration tests for Firebase Crashlytics scenarios.
* Each test:
* 1) Launches the app
* 2) Clicks a specific button that sets user ID & triggers crash/no-crash logic
* 3) If there's a crash, relaunch the app to send the crash
* 4) Then read the user ID from the textView (after crash & relaunch)
* 5) Logs a console link for manual verification
* Integration tests for Firebase Crashlytics scenarios. Each test: 1) Launches the app 2) Clicks a
* specific button that sets user ID & triggers crash/no-crash logic 3) If there's a crash, relaunch
* the app to send the crash 4) Then read the user ID from the textView (after crash & relaunch) 5)
* Logs a console link for manual verification
*/
@RunWith(AndroidJUnit4::class)
class FirebaseCrashlyticsIntegrationTest {
Expand All @@ -62,9 +57,7 @@ class FirebaseCrashlyticsIntegrationTest {
Runtime.getRuntime().exec(arrayOf("am", "force-stop", APP_NAME))
}

/**
* Helper method: read logcat (only used to verify Crashlytics init in one test).
*/
/** Helper method: read logcat (only used to verify Crashlytics init in one test). */
private fun readLogcat(tagFilter: String): Boolean {
val logs = mutableListOf<String>()
val process = Runtime.getRuntime().exec("logcat -d")
Expand All @@ -74,35 +67,32 @@ class FirebaseCrashlyticsIntegrationTest {
return logs.any { it.contains(tagFilter) }
}

/**
* Helper: Build Crashlytics console search URL for a given userId.
*/
/** Helper: Build Crashlytics console search URL for a given userId. */
private fun getCrashlyticsSearchUrl(userId: String): String {
return "https://console.firebase.google.com/project/crashlytics-e2e/" +
"crashlytics/app/android:com.google.firebase.testing.crashlytics/search" +
"?time=last-seven-days&types=crash&q=$userId"
"crashlytics/app/android:com.google.firebase.testing.crashlytics/search" +
"?time=last-seven-days&types=crash&q=$userId"
}

/**
* Helper: Launch the app from the home screen.
*/
/** Helper: Launch the app from the home screen. */
private fun launchApp() {
device.pressHome()
device.wait(Until.hasObject(By.pkg(device.launcherPackageName).depth(0)), LAUNCH_TIMEOUT)

val context = ApplicationProvider.getApplicationContext<Context>()
val intent = context.packageManager.getLaunchIntentForPackage(APP_NAME)?.apply {
addFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK)
}
val intent =
context.packageManager.getLaunchIntentForPackage(APP_NAME)?.apply {
addFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK)
}
context.startActivity(intent)

device.wait(Until.hasObject(By.pkg(TEST_APP_PACKAGE).depth(0)), LAUNCH_TIMEOUT)
device.waitForIdle()
}

/**
* Helper: Find a button by text and click it.
* The app's buttons appear to be uppercase, so we do uppercase() to match.
* Helper: Find a button by text and click it. The app's buttons appear to be uppercase, so we do
* uppercase() to match.
*/
private fun clickButton(buttonText: String) {
val uppercaseButtonText = buttonText.uppercase(Locale.getDefault())
Expand All @@ -115,41 +105,28 @@ class FirebaseCrashlyticsIntegrationTest {
}

/**
* Helper: Read the user ID from the textView that displays it in the app.
* e.g., "UserId: SomeValue"
* Because we are reading AFTER the crash (app is relaunched),
* the app persists the user ID via SharedPreferences.
* Helper: Read the user ID from the textView that displays it in the app. e.g., "UserId:
* SomeValue" Because we are reading AFTER the crash (app is relaunched), the app persists the
* user ID via SharedPreferences.
*/
private fun readDisplayedUserId(): String {
// Wait up to 3 seconds for a TextView that matches the pattern "UserId: ..."
device.wait(
Until.hasObject(By.text(Pattern.compile("UserId:.*"))),
3000
)
device.wait(Until.hasObject(By.text(Pattern.compile("UserId:.*"))), 3000)

// Find the object using the same pattern
val userIdObj = device.findObject(
By.text(Pattern.compile("UserId:.*"))
)
val userIdObj = device.findObject(By.text(Pattern.compile("UserId:.*")))

// If found, remove the "UserId: " prefix
return userIdObj?.text?.substringAfter("UserId: ") ?: "UNKNOWN_USER_ID"
}

/**
* Helper: Read the "Did crash previously?" text from the app.
*/
/** Helper: Read the "Did crash previously?" text from the app. */
private fun readDidCrashPreviouslyText(): String {
// Wait for up to 3 seconds for the text
device.wait(
Until.hasObject(By.text(Pattern.compile("HasCrashed:.*"))),
3000
)
device.wait(Until.hasObject(By.text(Pattern.compile("HasCrashed:.*"))), 3000)

// Find the object by resource ID
val didCrashObj = device.findObject(
By.text(Pattern.compile("HasCrashed:.*"))
)
val didCrashObj = device.findObject(By.text(Pattern.compile("HasCrashed:.*")))
return didCrashObj.text ?: "(unknown)"
}

Expand Down Expand Up @@ -200,10 +177,7 @@ class FirebaseCrashlyticsIntegrationTest {
launchApp()
val userId = readDisplayedUserId()

Log.i(
"TestInfo",
"After crashing, verify userId=$userId => ${getCrashlyticsSearchUrl(userId)}"
)
Log.i("TestInfo", "After crashing, verify userId=$userId => ${getCrashlyticsSearchUrl(userId)}")
}

@Test
Expand All @@ -214,10 +188,7 @@ class FirebaseCrashlyticsIntegrationTest {
// No crash, so read the user ID now
val userId = readDisplayedUserId()

Log.i(
"TestInfo",
"Verify NO crash for userId=$userId => ${getCrashlyticsSearchUrl(userId)}"
)
Log.i("TestInfo", "Verify NO crash for userId=$userId => ${getCrashlyticsSearchUrl(userId)}")
}

// ---------------------------------------------------------------------------
Expand All @@ -233,10 +204,7 @@ class FirebaseCrashlyticsIntegrationTest {
launchApp()
val userId = readDisplayedUserId()

Log.i(
"TestInfo",
"Check console for userId=$userId => ${getCrashlyticsSearchUrl(userId)}"
)
Log.i("TestInfo", "Check console for userId=$userId => ${getCrashlyticsSearchUrl(userId)}")
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -288,7 +256,7 @@ class FirebaseCrashlyticsIntegrationTest {
)
}

@Test
/* @Test
fun public_API_DidCrashPreviously() {
launchApp()
Expand All @@ -310,7 +278,7 @@ class FirebaseCrashlyticsIntegrationTest {
"TestInfo",
"public_API_DidCrashPreviously => userId=$userId => ${getCrashlyticsSearchUrl(userId)}"
)
}
}*/

@Test
fun public_API_RecordException() {
Expand Down Expand Up @@ -435,7 +403,6 @@ class FirebaseCrashlyticsIntegrationTest {
Log.i("TestInfo", "Interoperability_IID. userId=$userId => ${getCrashlyticsSearchUrl(userId)}")
}


// ---------------------------------------------------------------------------
// Navigation & UI Helpers
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -469,4 +436,4 @@ class FirebaseCrashlyticsIntegrationTest {
private const val LAUNCH_TIMEOUT = 5_000L
private const val TRANSITION_TIMEOUT = 1_000L
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.widget.RemoteViews
import com.google.firebase.FirebaseApp
import java.util.Date
import java.util.Locale

/** Provides homescreen widget for the test app. */
class CrashWidgetProvider : AppWidgetProvider() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.google.firebase.installations.FirebaseInstallations
import com.google.firebase.testing.crashlytics.databinding.FragmentFirstBinding


class FirstFragment : Fragment() {

private var _binding: FragmentFirstBinding? = null
private val binding get() = _binding!!
private val binding
get() = _binding!!

// Single Crashlytics instance
private val crashlytics = FirebaseCrashlytics.getInstance()
Expand Down Expand Up @@ -141,7 +141,9 @@ class FirstFragment : Fragment() {
// Record multiple non-fatal exceptions, but do NOT crash.
crashlytics.recordException(RuntimeException("public_API_RecordException: non-fatal 1"))
crashlytics.recordException(RuntimeException("public_API_RecordException: non-fatal 2"))
crashlytics.log("Public_API_RecordException: recorded two non-fatals, no crash. userId=$userId")
crashlytics.log(
"Public_API_RecordException: recorded two non-fatals, no crash. userId=$userId"
)
}

binding.buttonDatacollectionDefault.setOnClickListener {
Expand Down Expand Up @@ -201,13 +203,11 @@ class FirstFragment : Fragment() {
throw RuntimeException("Interoperability_IID crash after ID reset")
}
}

}


/**
* Load the previously-saved userId (if any) from SharedPreferences,
* apply it to Crashlytics, and display it in the TextView.
* Load the previously-saved userId (if any) from SharedPreferences, apply it to Crashlytics, and
* display it in the TextView.
*/
private fun loadAndApplyUserId() {
val prefs = requireContext().getSharedPreferences("crashlytics_prefs", Context.MODE_PRIVATE)
Expand All @@ -218,18 +218,15 @@ class FirstFragment : Fragment() {
}
}

/**
* Load the previously-saved "hasCrashed" flag,
*/
/** Load the previously-saved "hasCrashed" flag, */
private fun loadAndApplyHasCrashed() {
val hasCrashed = crashlytics.didCrashOnPreviousExecution()
binding.currentHasCrashed.text = "HasCrashed: $hasCrashed"
}


/**
* Save the given userId to SharedPreferences, apply it to Crashlytics,
* and update the TextView accordingly.
* Save the given userId to SharedPreferences, apply it to Crashlytics, and update the TextView
* accordingly.
*/
private fun saveAndApplyUserId(newUserId: String) {
// Save to SharedPreferences
Expand All @@ -245,4 +242,4 @@ class FirstFragment : Fragment() {
super.onDestroyView()
_binding = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import com.google.firebase.FirebaseApp
import com.google.firebase.testing.crashlytics.R

class ForegroundService : Service() {
private val CHANNEL_ID = "CrashForegroundService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ package com.google.firebase.testing.crashlytics

import android.os.Bundle
import com.google.firebase.testing.crashlytics.databinding.ActivityMainBinding

// Refactoring needed
//import com.google.firebase.testing.crashlytics.databinding.ActivityMainBinding
// import com.google.firebase.testing.crashlytics.databinding.ActivityMainBinding

class MainActivity : BaseActivity() {

Expand Down
Loading

0 comments on commit 7aa5e77

Please sign in to comment.