Skip to content

Commit a57db01

Browse files
committed
Working on tests, some things need refactoring and some junk files need deleting
1 parent d66f02e commit a57db01

File tree

24 files changed

+194
-101
lines changed

24 files changed

+194
-101
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
plugins {
2+
id("com.android.application")
3+
id("org.jetbrains.kotlin.android")
4+
id("com.google.gms.google-services")
5+
id("com.google.firebase.crashlytics")
6+
//kotlin("android") version "1.9.0" apply false
7+
}
8+
9+
android {
10+
namespace = "com.example.test_app"
11+
compileSdk = 35
12+
13+
defaultConfig {
14+
applicationId = "com.example.test_app"
15+
minSdk = 24
16+
targetSdk = 35
17+
versionCode = 1
18+
versionName = "1.0"
19+
20+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
21+
}
22+
23+
buildTypes {
24+
release {
25+
isMinifyEnabled = false
26+
proguardFiles(
27+
getDefaultProguardFile("proguard-android-optimize.txt"),
28+
"proguard-rules.pro"
29+
)
30+
}
31+
}
32+
buildFeatures {
33+
viewBinding = true
34+
}
35+
compileOptions {
36+
sourceCompatibility = JavaVersion.VERSION_11
37+
targetCompatibility = JavaVersion.VERSION_11
38+
}
39+
kotlinOptions {
40+
jvmTarget = "11"
41+
}
42+
}
43+
44+
dependencies {
45+
46+
implementation(libs.androidx.core.ktx)
47+
implementation(libs.androidx.appcompat)
48+
implementation(libs.material)
49+
//implementation(libs.firebase.crashlytics.ktx)
50+
testImplementation(libs.junit)
51+
androidTestImplementation(libs.androidx.test.junit)
52+
androidTestImplementation(libs.androidx.espresso.core)
53+
androidTestImplementation("androidx.test.uiautomator:uiautomator:2.2.0")
54+
androidTestImplementation("com.google.truth:truth:1.1.3")
55+
implementation("androidx.navigation:navigation-fragment-ktx:2.6.0")
56+
implementation("androidx.navigation:navigation-ui-ktx:2.6.0")
57+
implementation("com.google.firebase:firebase-common-ktx:21.0.0")
58+
implementation("com.google.firebase:firebase-crashlytics-ktx:19.2.1")
59+
implementation("androidx.multidex:multidex:2.0.1")
60+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

firebase-crashlytics/test-app/src/androidTest/kotlin/com/google/firebase/testing/crashlytics/FirebaseCrashlyticsIntegrationTest.kt

Lines changed: 32 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import androidx.test.uiautomator.Until
2828
import com.google.common.truth.Truth.assertThat
2929
import java.util.regex.Pattern
3030
import org.junit.After
31+
import org.junit.Assert
3132
import org.junit.Assert.fail
3233
import org.junit.Before
3334
import org.junit.Test
@@ -51,108 +52,55 @@ class FirebaseCrashlyticsIntegrationTest {
5152
}
5253

5354
@Test
54-
fun sameSessionIdBetweenActivitiesOnDifferentProcesses() {
55-
launchApp()
56-
57-
val sessionId1 = getCurrentSessionId()
58-
navigateToSecondActivity()
59-
Thread.sleep(TIME_TO_PROPAGATE_SESSION)
60-
val sessionId2 = getCurrentSessionId()
55+
fun sharedInitializeCrashlytics() {
56+
// Enable verbose logging for FirebaseCrashlytics
57+
Runtime.getRuntime().exec(arrayOf("adb", "shell", "setprop", "log.tag.FirebaseCrashlytics", "VERBOSE")).waitFor()
6158

62-
assertThat(sessionId1).isEqualTo(sessionId2)
63-
}
64-
65-
@Test
66-
fun sameSessionIdAfterQuickForegroundBackground() {
67-
launchApp()
68-
69-
val sessionId1 = getCurrentSessionId()
70-
background()
71-
foreground()
72-
val sessionId2 = getCurrentSessionId()
73-
74-
assertThat(sessionId1).isEqualTo(sessionId2)
75-
}
76-
77-
@Test
78-
fun newSessionIdAfterLongBackground() {
59+
// Launch the app
7960
launchApp()
8061

81-
val sessionId1 = getCurrentSessionId()
82-
background()
83-
// Test app overrides the background time from 30m, to 5s.
84-
Thread.sleep(6_000)
85-
foreground()
86-
device.waitForIdle()
87-
Thread.sleep(TIME_TO_PROPAGATE_SESSION)
88-
val sessionId2 = getCurrentSessionId()
62+
// Start monitoring logs
63+
val logcatProcess = ProcessBuilder("adb", "logcat", "-s", "FirebaseCrashlytics")
64+
.redirectErrorStream(true)
65+
.start()
8966

90-
assertThat(sessionId1).isNotEqualTo(sessionId2)
91-
}
67+
val logcatReader = logcatProcess.inputStream.bufferedReader()
9268

93-
@Test
94-
fun newSessionFollowingCrash() {
95-
if (!BuildConfig.SHOULD_CRASH_APP) return
69+
// Flag to detect Crashlytics initialization
70+
var crashlyticsInitialized = false
9671

97-
launchApp()
98-
val origSession = getCurrentSessionId()
99-
getButton("CRASH!").click()
100-
dismissPossibleErrorDialog()
72+
// Monitor log output
73+
logcatReader.useLines { lines ->
74+
for (line in lines) {
75+
if (line.contains("FirebaseCrashlytics is initialized")) {
76+
crashlyticsInitialized = true
77+
break
78+
}
79+
}
80+
}
10181

102-
launchApp()
82+
// Clean up the logcat process
83+
logcatProcess.destroy()
10384

104-
Thread.sleep(TIME_TO_PROPAGATE_SESSION)
105-
val newSession = getCurrentSessionId()
106-
assertThat(newSession).isNotEqualTo(origSession)
85+
// Assert that Crashlytics was initialized
86+
assertThat(crashlyticsInitialized).isTrue()
10787
}
10888

109-
@Test
110-
fun nonFatalMainActivity() {
111-
launchApp()
112-
val origSession = getCurrentSessionId()
113-
114-
getButton("NON FATAL").click()
115-
device.waitForIdle()
116-
117-
Thread.sleep(TIME_TO_PROPAGATE_SESSION)
118-
val newSession = getCurrentSessionId()
119-
assertThat(origSession).isEqualTo(newSession)
120-
}
12189

90+
// Keeping here for now as an example
12291
@Test
123-
fun anrMainActivity() {
124-
if (!BuildConfig.SHOULD_CRASH_APP) return
125-
launchApp()
126-
val origSession = getCurrentSessionId()
127-
128-
getButton("ANR").click()
129-
device.waitForIdle()
130-
dismissPossibleAnrDialog()
131-
92+
fun sameSessionIdBetweenActivitiesOnDifferentProcesses() {
13293
launchApp()
13394

134-
Thread.sleep(TIME_TO_PROPAGATE_SESSION)
135-
val newSession = getCurrentSessionId()
136-
assertThat(origSession).isNotEqualTo(newSession)
137-
}
138-
139-
@Test
140-
fun crashSecondaryProcess() {
141-
if (!BuildConfig.SHOULD_CRASH_APP) return
142-
launchApp()
95+
val sessionId1 = getCurrentSessionId()
14396
navigateToSecondActivity()
144-
val origSession = getCurrentSessionId()
145-
146-
getButton("CRASH!").click()
147-
dismissPossibleErrorDialog()
148-
149-
launchApp()
150-
15197
Thread.sleep(TIME_TO_PROPAGATE_SESSION)
152-
val newSession = getCurrentSessionId()
153-
assertThat(newSession).isNotEqualTo(origSession)
98+
val sessionId2 = getCurrentSessionId()
99+
100+
assertThat(sessionId1).isEqualTo(sessionId2)
154101
}
155102

103+
156104
private fun launchApp() {
157105
// Start from the home screen
158106
device.pressHome()

firebase-crashlytics/test-app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1818
xmlns:tools="http://schemas.android.com/tools">
1919

20+
2021
<application
2122
android:icon="@mipmap/ic_launcher"
2223
android:label="@string/app_name"
23-
android:name=".TestApplication"
24+
android:name="com.google.firebase.testing.crashlytics.TestApplication"
2425
android:supportsRtl="true"
2526
android:theme="@style/Theme.Widget_test_app"
2627
tools:targetApi="31">
2728
<activity
2829
android:exported="true"
2930
android:label="@string/app_name"
30-
android:name=".MainActivity"
31+
android:name="com.google.firebase.testing.crashlytics.MainActivity"
3132
android:process=":main"
3233
android:theme="@style/Theme.Widget_test_app.NoActionBar">
3334
<intent-filter>
@@ -39,7 +40,7 @@
3940
<activity
4041
android:exported="true"
4142
android:label="@string/app_name"
42-
android:name=".SecondActivity"
43+
android:name="com.google.firebase.testing.crashlytics.SecondActivity"
4344
android:process=":second"
4445
android:theme="@style/Theme.Widget_test_app.NoActionBar" />
4546

@@ -53,7 +54,7 @@
5354

5455
<receiver
5556
android:exported="false"
56-
android:name="CrashWidgetProvider"
57+
android:name="com.google.firebase.testing.crashlytics.CrashWidgetProvider"
5758
android:process=":widgetProcess">
5859
<intent-filter>
5960
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
@@ -63,13 +64,13 @@
6364
android:resource="@xml/homescreen_widget" />
6465
</receiver>
6566

66-
<receiver android:name=".CrashBroadcastReceiver" />
67+
<receiver android:name="com.google.firebase.testing.crashlytics.CrashBroadcastReceiver" />
6768

6869
<service
6970
android:enabled="true"
7071
android:exported="false"
7172
android:foregroundServiceType="shortService"
72-
android:name=".ForegroundService"
73+
android:name="com.google.firebase.testing.crashlytics.ForegroundService"
7374
android:process=":foregroundServiceProcess" />
7475

7576
</application>

firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/CrashWidgetProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import android.content.Intent
2424
import android.icu.text.SimpleDateFormat
2525
import android.os.Build
2626
import android.widget.RemoteViews
27+
import com.example.test_app.R
2728
import com.google.firebase.FirebaseApp
2829
import java.util.Date
2930
import java.util.Locale
30-
3131
/** Provides homescreen widget for the test app. */
3232
class CrashWidgetProvider : AppWidgetProvider() {
3333

firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/FirstFragment.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import android.view.LayoutInflater
2626
import android.view.View
2727
import android.view.ViewGroup
2828
import androidx.fragment.app.Fragment
29+
import com.example.test_app.databinding.FragmentFirstBinding
2930
import com.google.firebase.crashlytics.FirebaseCrashlytics
30-
import com.google.firebase.testing.crashlytics.databinding.FragmentFirstBinding
31+
//import com.google.firebase.testing.crashlytics.databinding.FragmentFirstBinding
3132
import java.util.Date
3233
import java.util.Locale
3334

@@ -66,26 +67,26 @@ class FirstFragment : Fragment() {
6667
}
6768
binding.buttonForegroundProcess.setOnClickListener {
6869
if (binding.buttonForegroundProcess.getText().startsWith("Start")) {
69-
ForegroundService.startService(getContext()!!, "Starting service at ${getDateText()}")
70+
ForegroundService.startService(requireContext(), "Starting service at ${getDateText()}")
7071
binding.buttonForegroundProcess.setText("Stop foreground service")
7172
} else {
72-
ForegroundService.stopService(getContext()!!)
73+
ForegroundService.stopService(requireContext())
7374
binding.buttonForegroundProcess.setText("Start foreground service")
7475
}
7576
}
7677
binding.startSplitscreen.setOnClickListener {
77-
val intent = Intent(getContext()!!, SecondActivity::class.java)
78+
val intent = Intent(requireContext(), SecondActivity::class.java)
7879
intent.addFlags(FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_LAUNCH_ADJACENT)
7980
startActivity(intent)
8081
activity?.finish()
8182
}
8283
binding.startSplitscreenSame.setOnClickListener {
83-
val intent = Intent(getContext()!!, MainActivity::class.java)
84+
val intent = Intent(requireContext(), MainActivity::class.java)
8485
intent.addFlags(FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_LAUNCH_ADJACENT)
8586
startActivity(intent)
8687
}
8788
binding.nextActivityButton.setOnClickListener {
88-
val intent = Intent(getContext()!!, SecondActivity::class.java)
89+
val intent = Intent(requireContext(), SecondActivity::class.java)
8990
intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
9091
startActivity(intent)
9192
}

firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/ForegroundService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import android.util.Log
2929
import androidx.core.app.NotificationCompat
3030
import androidx.core.content.ContextCompat
3131
import com.google.firebase.FirebaseApp
32+
import com.example.test_app.R
3233

3334
class ForegroundService : Service() {
3435
private val CHANNEL_ID = "CrashForegroundService"

firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/MainActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package com.google.firebase.testing.crashlytics
1818

1919
import android.os.Bundle
20-
import com.google.firebase.testing.crashlytics.databinding.ActivityMainBinding
20+
import com.example.test_app.databinding.ActivityMainBinding
21+
// Refactoring needed
22+
//import com.google.firebase.testing.crashlytics.databinding.ActivityMainBinding
2123

2224
class MainActivity : BaseActivity() {
2325

firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/SecondActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
2222
import android.os.Build
2323
import android.os.Bundle
2424
import android.widget.Button
25+
import com.example.test_app.R
2526

2627
/** Second activity from the MainActivity that runs on a different process. */
2728
class SecondActivity : BaseActivity() {

0 commit comments

Comments
 (0)