Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround androidx.test 45 second timeout in ActivityScenario #264

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading