From c282661aca610f1efa261d235b5c632159c1266b Mon Sep 17 00:00:00 2001 From: Matthew Robertson Date: Thu, 29 May 2025 16:28:40 -0400 Subject: [PATCH] Fix the profile case with clear app data --- .../benchmark/sessions/StartupBenchmark.kt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/firebase-sessions/benchmark/src/main/kotlin/com/google/firebase/benchmark/sessions/StartupBenchmark.kt b/firebase-sessions/benchmark/src/main/kotlin/com/google/firebase/benchmark/sessions/StartupBenchmark.kt index 841cd073ba0..2d517a231e4 100644 --- a/firebase-sessions/benchmark/src/main/kotlin/com/google/firebase/benchmark/sessions/StartupBenchmark.kt +++ b/firebase-sessions/benchmark/src/main/kotlin/com/google/firebase/benchmark/sessions/StartupBenchmark.kt @@ -21,6 +21,7 @@ import androidx.benchmark.macro.StartupTimingMetric import androidx.benchmark.macro.junit4.MacrobenchmarkRule import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry +import java.io.FileInputStream import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -48,14 +49,25 @@ class StartupBenchmark { metrics = listOf(StartupTimingMetric()), iterations = 5, startupMode = StartupMode.COLD, + setupBlock = { clearAppData(packageName) }, ) { pressHome() + startActivityAndWait() + } + + private fun clearAppData(packageName: String) { + val fileDescriptor = InstrumentationRegistry.getInstrumentation() .uiAutomation - .executeShellCommand("pm clear $PACKAGE_NAME") - .close() - startActivityAndWait() + .executeShellCommand("pm clear $packageName") + val fileInputStream = FileInputStream(fileDescriptor.fileDescriptor) + // Read the output to ensure the app data was cleared successfully + val result = fileInputStream.bufferedReader().use { it.readText().trim() } + fileDescriptor.close() + if (result != "Success") { + throw IllegalStateException("Unable to clear app data for $packageName - $result") } + } private companion object { const val PACKAGE_NAME = "com.google.firebase.testing.sessions"