Skip to content

Commit

Permalink
test without crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyokone committed Jan 9, 2025
1 parent 951ea1a commit 1b03de7
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
11 changes: 9 additions & 2 deletions firebase-crashlytics/test-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
android:exported="true"
android:label="@string/app_name"
android:name="com.google.firebase.testing.crashlytics.MainActivity"
android:process=":main"
android:theme="@style/Theme.Widget_test_app.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -52,6 +51,15 @@
android:name="firebase_crashlytics_crashlytics_restart_timeout"
android:value="5" />

<provider
android:authorities="${applicationId}.preFirebase"
android:directBootAware="true"
android:exported="false"
android:initOrder="101"
android:name="com.google.firebase.testing.crashlytics.PreFirebaseProvider" />

<receiver android:name=".CrashBroadcastReceiver" />

<receiver
android:exported="false"
android:name="com.google.firebase.testing.crashlytics.CrashWidgetProvider"
Expand All @@ -64,7 +72,6 @@
android:resource="@xml/homescreen_widget" />
</receiver>

<receiver android:name="com.google.firebase.testing.crashlytics.CrashBroadcastReceiver" />

<service
android:enabled="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ class FirstFragment : Fragment() {
}

binding.buttonSharedGenerateCrash.setOnClickListener {
PreFirebaseProvider.expectedMessage = "Shared_Generate_Crash"
val userId = "SharedGenerate_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

throw RuntimeException("Test: Shared_Generate_Crash")
}

binding.buttonSharedVerifyCrash.setOnClickListener {
PreFirebaseProvider.expectedMessage = "Shared_Verify_Crash"
val userId = "SharedVerifyCrash_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

Expand All @@ -88,13 +90,15 @@ class FirstFragment : Fragment() {
}

binding.buttonFirebasecoreFatalError.setOnClickListener {
PreFirebaseProvider.expectedMessage = "FirebaseCore_Fatal_Error"
val userId = "FirebaseCoreFatal_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

throw RuntimeException("FirebaseCore_Fatal_Error has occurred.")
}

binding.buttonPublicApiLog.setOnClickListener {
PreFirebaseProvider.expectedMessage = "Public_API_Log"
val userId = "PublicApiLog_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

Expand All @@ -105,6 +109,7 @@ class FirstFragment : Fragment() {
}

binding.buttonPublicApiSetcustomvalue.setOnClickListener {
PreFirebaseProvider.expectedMessage = "Public_API_SetCustomValue"
val userId = "PublicApiSetCustomValue_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

Expand All @@ -114,13 +119,15 @@ class FirstFragment : Fragment() {
}

binding.buttonPublicApiSetuserid.setOnClickListener {
PreFirebaseProvider.expectedMessage = "Public_API_SetUserID"
val userId = "PublicApiSetUserID_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

throw RuntimeException("Public_API_SetUserID crash")
}

binding.buttonPublicApiDidcrashpreviously.setOnClickListener {
PreFirebaseProvider.expectedMessage = "Public_API_DidCrashPreviously"
val userId = "DidCrashPreviously_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

Expand All @@ -138,48 +145,55 @@ class FirstFragment : Fragment() {
}

binding.buttonDatacollectionDefault.setOnClickListener {
PreFirebaseProvider.expectedMessage = "DataCollection_Default"
val userId = "DataCollectionDefault_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

throw RuntimeException("DataCollection_Default crash")
}

binding.buttonDatacollectionFirebaseOff.setOnClickListener {
PreFirebaseProvider.expectedMessage = "DataCollection_Firebase_Off"
val userId = "DataCollectionFirebaseOff_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

throw RuntimeException("DataCollection_Firebase_Off crash")
}

binding.buttonDatacollectionCrashlyticsOff.setOnClickListener {
PreFirebaseProvider.expectedMessage = "DataCollection_Crashlytics_Off"
val userId = "DataCollectionCrashlyticsOff_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

throw RuntimeException("DataCollection_Crashlytics_Off crash")
}

binding.buttonDatacollectionOffThenOn.setOnClickListener {
PreFirebaseProvider.expectedMessage = "DataCollection_Crashlytics_Off_Then_On"
val userId = "DataCollectionOffThenOn_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

throw RuntimeException("DataCollection_Crashlytics_Off_Then_On crash")
}

binding.buttonDatacollectionOffThenSend.setOnClickListener {
PreFirebaseProvider.expectedMessage = "DataCollection_Crashlytics_Off_Then_Send"
val userId = "DataCollectionOffThenSend_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

throw RuntimeException("DataCollection_Crashlytics_Off_Then_Send crash")
}

binding.buttonDatacollectionOffThenDelete.setOnClickListener {
PreFirebaseProvider.expectedMessage = "DataCollection_Crashlytics_Off_Then_Delete"
val userId = "DataCollectionOffThenDelete_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

throw RuntimeException("DataCollection_Crashlytics_Off_Then_Delete crash")
}

binding.buttonInteroperabilityIid.setOnClickListener {
PreFirebaseProvider.expectedMessage = "Interoperability_IID"
val userId = "InteroperabilityIID_${System.currentTimeMillis()}"
saveAndApplyUserId(userId)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2024 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.ContentProvider
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import java.lang.Thread.UncaughtExceptionHandler
import kotlin.system.exitProcess

class PreFirebaseProvider : ContentProvider(), UncaughtExceptionHandler {
private var defaultUncaughtExceptionHandler: UncaughtExceptionHandler? = null

companion object {
/**
* Set an expected exception message. If the uncaught exception contains the given string, then
* the app will exit cleanly. Otherwise, it will propagate up to the default exception handler.
*/
var expectedMessage: String? = null
}

override fun onCreate(): Boolean {
defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler(this)

return false
}

/* Returns if the given exception contains the expectedMessage in its message. */
private fun isExpected(throwable: Throwable): Boolean =
expectedMessage?.run { throwable.message?.contains(this) } ?: false

override fun uncaughtException(thread: Thread, throwable: Throwable) {
if (isExpected(throwable)) {
// Exit cleanly
exitProcess(0)
} else {
// Propagate up to the default exception handler
defaultUncaughtExceptionHandler?.uncaughtException(thread, throwable)
}
}

override fun query(
uri: Uri,
projection: Array<out String>?,
selection: String?,
selectionArgs: Array<out String>?,
sortOrder: String?
): Cursor? = null

override fun getType(uri: Uri): String? = null

override fun insert(uri: Uri, values: ContentValues?): Uri? = null

override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0

override fun update(
uri: Uri,
values: ContentValues?,
selection: String?,
selectionArgs: Array<out String>?
): Int = 0
}

0 comments on commit 1b03de7

Please sign in to comment.