Add stress test to continuously take pictures without waiting for previous results

Bug: 259326443
Test: ImageCaptureStressTest
Change-Id: I607377b9c46bd5a166b3359fe0fc522d5de58cce
diff --git a/camera/integration-tests/coretestapp/src/androidTest/java/androidx/camera/integration/core/CameraXActivityTestExtensions.kt b/camera/integration-tests/coretestapp/src/androidTest/java/androidx/camera/integration/core/CameraXActivityTestExtensions.kt
index 1781ca6..d13c4c4 100644
--- a/camera/integration-tests/coretestapp/src/androidTest/java/androidx/camera/integration/core/CameraXActivityTestExtensions.kt
+++ b/camera/integration-tests/coretestapp/src/androidTest/java/androidx/camera/integration/core/CameraXActivityTestExtensions.kt
@@ -61,16 +61,29 @@
 
 /**
  * Waits until an image has been saved and its idling resource has become idle.
+ *
+ * @param captureRequestsCount the capture requests count to issue to continuously take pictures
+ * without waiting for the previous capture requests to be done.
  */
-internal fun ActivityScenario<CameraXActivity>.takePictureAndWaitForImageSavedIdle() {
+internal fun ActivityScenario<CameraXActivity>.takePictureAndWaitForImageSavedIdle(
+    captureRequestsCount: Int = 1
+) {
     val idlingResource = withActivity {
         cleanTakePictureErrorMessage()
         imageSavedIdlingResource
     }
     try {
-        IdlingRegistry.getInstance().register(idlingResource)
         // Perform click to take a picture.
-        Espresso.onView(ViewMatchers.withId(R.id.Picture)).perform(click())
+        Espresso.onView(ViewMatchers.withId(R.id.Picture)).apply {
+            repeat(captureRequestsCount) {
+                perform(click())
+            }
+        }
+        // Registers the idling resource and wait for it being idle after performing the click
+        // operations. So that the click operations can be performed continuously without wait for
+        // previous capture results.
+        IdlingRegistry.getInstance().register(idlingResource)
+        Espresso.onIdle()
     } finally { // Always release the idling resource, in case of timeout exceptions.
         IdlingRegistry.getInstance().unregister(idlingResource)
         withActivity {
diff --git a/camera/integration-tests/coretestapp/src/androidTest/java/androidx/camera/integration/core/stresstest/ImageCaptureStressTest.kt b/camera/integration-tests/coretestapp/src/androidTest/java/androidx/camera/integration/core/stresstest/ImageCaptureStressTest.kt
index a447c7f..578ae88 100644
--- a/camera/integration-tests/coretestapp/src/androidTest/java/androidx/camera/integration/core/stresstest/ImageCaptureStressTest.kt
+++ b/camera/integration-tests/coretestapp/src/androidTest/java/androidx/camera/integration/core/stresstest/ImageCaptureStressTest.kt
@@ -273,4 +273,24 @@
         val randomDelayDuration = (Random.nextInt(maxDelaySeconds) + 1).toLong()
         delay(TimeUnit.SECONDS.toMillis(randomDelayDuration))
     }
+
+    @LabTestRule.LabTestOnly
+    @Test
+    @RepeatRule.Repeat(times = LARGE_STRESS_TEST_REPEAT_COUNT)
+    fun launchActivity_thenTakeMultiplePictures_withoutWaitingPreviousResults() {
+        val useCaseCombination = BIND_PREVIEW or BIND_IMAGE_CAPTURE
+
+        // Launches CameraXActivity and wait for the preview ready.
+        val activityScenario =
+            launchCameraXActivityAndWaitForPreviewReady(cameraId, useCaseCombination)
+
+        with(activityScenario) {
+            use {
+                // Checks whether multiple images can be captured successfully
+                repeat(STRESS_TEST_OPERATION_REPEAT_COUNT) {
+                    takePictureAndWaitForImageSavedIdle(3)
+                }
+            }
+        }
+    }
 }
\ No newline at end of file