Fix waitForStableInActiveWindow

Bug: 420349130
Test: ./gradlew test:uiautomator:uiautomator:cC
Change-Id: If29826c39502db03c4d49b7cd3ad338bed3c7559
diff --git a/test/uiautomator/uiautomator/src/main/java/androidx/test/uiautomator/AccessibilityNodeInfoExt.kt b/test/uiautomator/uiautomator/src/main/java/androidx/test/uiautomator/AccessibilityNodeInfoExt.kt
index 77551b4..5fbf7bd 100644
--- a/test/uiautomator/uiautomator/src/main/java/androidx/test/uiautomator/AccessibilityNodeInfoExt.kt
+++ b/test/uiautomator/uiautomator/src/main/java/androidx/test/uiautomator/AccessibilityNodeInfoExt.kt
@@ -90,7 +90,7 @@
  * @return a bitmap containing the image of this node.
  */
 public fun AccessibilityNodeInfo.takeScreenshot(): Bitmap =
-    takeScreenshotBitmap(bounds = Rect().apply { getBoundsInScreen(this) })
+    takeScreenshotBitmap(nodeBounds = Rect().apply { getBoundsInScreen(this) })
 
 /**
  * Waits for the node to become stable. A node is considered stable when it and its descendants have
diff --git a/test/uiautomator/uiautomator/src/main/java/androidx/test/uiautomator/AccessibilityWindowInfoExt.kt b/test/uiautomator/uiautomator/src/main/java/androidx/test/uiautomator/AccessibilityWindowInfoExt.kt
index e9ed552..31bfb01 100644
--- a/test/uiautomator/uiautomator/src/main/java/androidx/test/uiautomator/AccessibilityWindowInfoExt.kt
+++ b/test/uiautomator/uiautomator/src/main/java/androidx/test/uiautomator/AccessibilityWindowInfoExt.kt
@@ -37,7 +37,7 @@
  * @return a bitmap containing the image of this node.
  */
 public fun AccessibilityWindowInfo.takeScreenshot(): Bitmap =
-    takeScreenshotBitmap(bounds = Rect().apply { getBoundsInScreen(this) })
+    takeScreenshotBitmap(nodeBounds = Rect().apply { getBoundsInScreen(this) })
 
 /**
  * Waits for the root node to become available in this window.
diff --git a/test/uiautomator/uiautomator/src/main/java/androidx/test/uiautomator/internal/Utils.kt b/test/uiautomator/uiautomator/src/main/java/androidx/test/uiautomator/internal/Utils.kt
index 5157c2e..6d877f3 100644
--- a/test/uiautomator/uiautomator/src/main/java/androidx/test/uiautomator/internal/Utils.kt
+++ b/test/uiautomator/uiautomator/src/main/java/androidx/test/uiautomator/internal/Utils.kt
@@ -56,18 +56,22 @@
 internal fun joinLines(vararg lines: String) =
     lines.joinToString(separator = System.lineSeparator())
 
-internal fun takeScreenshotBitmap(bounds: Rect): Bitmap {
-    val bitmap = uiAutomation.takeScreenshot()
-    val actualBounds =
+internal fun takeScreenshotBitmap(nodeBounds: Rect): Bitmap {
+    val screenBitmap = uiAutomation.takeScreenshot()
+    val screenBounds =
         Rect().apply {
             left = 0
             top = 0
-            right = bitmap.width
-            bottom = bitmap.height
+            right = screenBitmap.width
+            bottom = screenBitmap.height
         }
-    bounds.intersect(actualBounds)
-    val cropped =
-        Bitmap.createBitmap(bitmap, bounds.left, bounds.top, bounds.width(), bounds.height())
+    nodeBounds.intersect(screenBounds)
+
+    val x = nodeBounds.left.coerceIn(0, screenBounds.right)
+    val y = nodeBounds.top.coerceIn(0, screenBounds.top)
+    val w = nodeBounds.width().coerceIn(0, screenBounds.width() - x)
+    val h = nodeBounds.height().coerceIn(0, screenBounds.height() - y)
+    val cropped = Bitmap.createBitmap(screenBitmap, x, y, w, h)
     return cropped
 }