Skip to content

Commit

Permalink
Workaround androidx.test 45 second timeout in ActivityScenario (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
temcguir authored Sep 12, 2024
1 parent 575ffcf commit d7942c6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ internal class ImageCaptureDeviceTest {
.assertExists()
.performClick()
}
Truth.assertThat(result?.resultCode).isEqualTo(Activity.RESULT_OK)
Truth.assertThat(result.resultCode).isEqualTo(Activity.RESULT_OK)
Truth.assertThat(doesImageFileExist(uri, "image")).isTrue()
deleteFilesInDirAfterTimestamp(DIR_PATH, instrumentation, timeStamp)
}
Expand All @@ -123,7 +123,7 @@ internal class ImageCaptureDeviceTest {
}
uiDevice.pressBack()
}
Truth.assertThat(result?.resultCode).isEqualTo(Activity.RESULT_CANCELED)
Truth.assertThat(result.resultCode).isEqualTo(Activity.RESULT_CANCELED)
Truth.assertThat(doesImageFileExist(uri, "image")).isFalse()
}

Expand All @@ -149,7 +149,7 @@ internal class ImageCaptureDeviceTest {
}
uiDevice.pressBack()
}
Truth.assertThat(result?.resultCode).isEqualTo(Activity.RESULT_CANCELED)
Truth.assertThat(result.resultCode).isEqualTo(Activity.RESULT_CANCELED)
Truth.assertThat(doesImageFileExist(uri, "video")).isFalse()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ internal class VideoRecordingDeviceTest {
}
longClickForVideoRecording()
}
Truth.assertThat(result?.resultCode).isEqualTo(Activity.RESULT_OK)
Truth.assertThat(result.resultCode).isEqualTo(Activity.RESULT_OK)
Truth.assertThat(doesImageFileExist(uri, "video")).isTrue()
deleteFilesInDirAfterTimestamp(DIR_PATH, instrumentation, timeStamp)
}
Expand All @@ -113,7 +113,7 @@ internal class VideoRecordingDeviceTest {
}
uiDevice.pressBack()
}
Truth.assertThat(result?.resultCode).isEqualTo(Activity.RESULT_CANCELED)
Truth.assertThat(result.resultCode).isEqualTo(Activity.RESULT_CANCELED)
Truth.assertThat(doesImageFileExist(uri, "video")).isFalse()
}

Expand All @@ -140,7 +140,7 @@ internal class VideoRecordingDeviceTest {
}
uiDevice.pressBack()
}
Truth.assertThat(result?.resultCode).isEqualTo(Activity.RESULT_CANCELED)
Truth.assertThat(result.resultCode).isEqualTo(Activity.RESULT_CANCELED)
Truth.assertThat(doesImageFileExist(uri, "image")).isFalse()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ import androidx.compose.ui.test.isDisplayed
import androidx.compose.ui.test.junit4.ComposeTestRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.ActivityScenario
import com.google.jetpackcamera.MainActivity
import com.google.jetpackcamera.feature.preview.R
import com.google.jetpackcamera.feature.preview.quicksettings.ui.QUICK_SETTINGS_FLIP_CAMERA_BUTTON
import com.google.jetpackcamera.settings.model.LensFacing
import java.io.File
import java.net.URLConnection
import java.util.concurrent.TimeoutException
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeoutOrNull

const val APP_START_TIMEOUT_MILLIS = 10_000L
const val IMAGE_CAPTURE_TIMEOUT_MILLIS = 5_000L
Expand All @@ -50,13 +57,30 @@ inline fun <reified T : Activity> runScenarioTest(
inline fun <reified T : Activity> runScenarioTestForResult(
intent: Intent,
crossinline block: ActivityScenario<T>.() -> Unit
): Instrumentation.ActivityResult? {
): Instrumentation.ActivityResult {
ActivityScenario.launchActivityForResult<T>(intent).use { scenario ->
scenario.apply(block)
return scenario.result
return runBlocking { scenario.pollResult() }
}
}

// Workaround for https://github.com/android/android-test/issues/676
suspend inline fun <reified T : Activity> ActivityScenario<T>.pollResult(
// Choose timeout to match
// https://github.com/android/android-test/blob/67fa7cb12b9a14dc790b75947f4241c3063e80dc/runner/monitor/java/androidx/test/internal/platform/app/ActivityLifecycleTimeout.java#L22
timeout: Duration = 45.seconds
): Instrumentation.ActivityResult = withTimeoutOrNull(timeout) {
// Poll for the state to be destroyed before we return the result
while (state != Lifecycle.State.DESTROYED) {
delay(100)
}
checkNotNull(result)
} ?: run {
throw TimeoutException(
"Timed out while waiting for activity result. Waited $timeout."
)
}

context(ActivityScenario<MainActivity>)
fun ComposeTestRule.getCurrentLensFacing(): LensFacing {
var needReturnFromQuickSettings = false
Expand Down

0 comments on commit d7942c6

Please sign in to comment.