Skip to content

Commit

Permalink
Try to avoid anr
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltroger committed Nov 3, 2023
1 parent 28ebb9d commit 0cac0ec
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ fun waitUntilIdle() {
.waitForIdle()
}

fun waitUntilNoException(timeoutMs: Long = 50000, function: () -> Any?) {
val startTimeMs = System.currentTimeMillis()
fun waitUntilNoException(timeoutMs: Long = 5000, function: () -> Any?) {
var startTimeMs = System.currentTimeMillis()
val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
while (true) {
try {
function()
return
} catch (e: Exception) {
// if exception due to ANR then reset timer
if (e.toString().contains("RootViewWithoutFocusException")) {
val waitButton = uiDevice.findObject(UiSelector().textContains("Wait"))
if (waitButton.exists()) {
waitButton.click()
}
startTimeMs = System.currentTimeMillis()
}
if (System.currentTimeMillis() > startTimeMs + timeoutMs) {
throw TimeoutException(e)
}
Expand Down

0 comments on commit 0cac0ec

Please sign in to comment.