Skip to content

Commit 137df13

Browse files
brettchabotcopybara-androidxtest
authored andcommitted
Add *Async variants of capture*ToBitmap methods.
This is a precursor change to modifying the captureToBitmap functions to be suspend functions. PiperOrigin-RevId: 614867086
1 parent 2d8d4b2 commit 137df13

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
* Added ApplicationInfoBuilder.setFlags(int)
1616
* Make Bitmap.writeToTestStorage use the registered PlatformTestStorage instead of hardcoding TestStorage
17+
* Add *Async variants of capture*ToBitmap methods
1718

1819
**Breaking API Changes**
1920

core/java/androidx/test/core/view/ViewCapture.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ import java.util.function.Consumer
5757
* bitmap will be a 5x5 bitmap that spans (15, 15 - 20, 20). This is particularly useful for
5858
* Compose, which only has a singular view that contains a hierarchy of nodes.
5959
*
60+
* Note: This API will likely change in the future to a kotlin suspend function. If you want a
61+
* method that returns a ListenableFuture instead, use [captureToBitmapAsync].
62+
*
6063
* This API is currently experimental and subject to change or removal.
6164
*/
6265
@ExperimentalTestApi
@@ -82,6 +85,15 @@ fun View.captureToBitmap(rect: Rect? = null): ListenableFuture<Bitmap> {
8285
return bitmapFuture
8386
}
8487

88+
/**
89+
* Equivalent to [captureToBitmap] for now. In the future [captureToBitmap] will be changed to a
90+
* suspend function.
91+
*/
92+
@ExperimentalTestApi
93+
fun View.captureToBitmapAsync(rect: Rect? = null): ListenableFuture<Bitmap> {
94+
return captureToBitmap(rect)
95+
}
96+
8597
/**
8698
* Trigger a redraw of the given view.
8799
*
@@ -195,7 +207,7 @@ private fun View.generateBitmapFromPixelCopy(
195207
bounds.left + rect.left,
196208
bounds.top + rect.top,
197209
bounds.left + rect.right,
198-
bounds.top + rect.bottom
210+
bounds.top + rect.bottom,
199211
)
200212
}
201213
PixelCopy.request(surface, bounds, destBitmap, onCopyFinished, Handler(Looper.getMainLooper()))
@@ -256,7 +268,7 @@ private fun View.reflectivelyGetLocationInSurface(locationInSurface: IntArray) {
256268
// ART restrictions introduced in API 29 disallow reflective access to mWindowAttributes
257269
Log.w(
258270
"ViewCapture",
259-
"Could not calculate offset of view in surface on API 28, resulting image may have incorrect positioning"
271+
"Could not calculate offset of view in surface on API 28, resulting image may have incorrect positioning",
260272
)
261273
}
262274
}

core/java/androidx/test/core/view/WindowCapture.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ import com.google.common.util.concurrent.ListenableFuture
4141
* This API is primarily intended for use in lower layer libraries or frameworks. For test authors,
4242
* its recommended to use espresso or compose's captureToImage.
4343
*
44+
* Note: This API will likely change in the future to a kotlin suspend function. If you want a
45+
* method that returns a ListenableFuture instead, use [captureRegionToBitmapAsync].
46+
*
4447
* This API is currently experimental and subject to change or removal.
4548
*/
4649
@ExperimentalTestApi
@@ -62,15 +65,24 @@ fun Window.captureRegionToBitmap(boundsInWindow: Rect? = null): ListenableFuture
6265
return bitmapFuture
6366
}
6467

68+
/**
69+
* Equivalent to [captureRegionToBitmap] for now. In the future [captureRegionToBitmap] will be
70+
* changed to a suspend function.
71+
*/
72+
@ExperimentalTestApi
73+
fun Window.captureRegionToBitmapAsync(boundsInWindow: Rect? = null): ListenableFuture<Bitmap> {
74+
return captureRegionToBitmap(boundsInWindow)
75+
}
76+
6577
internal fun Window.generateBitmap(
6678
boundsInWindow: Rect? = null,
67-
bitmapFuture: ResolvableFuture<Bitmap>
79+
bitmapFuture: ResolvableFuture<Bitmap>,
6880
) {
6981
val destBitmap =
7082
Bitmap.createBitmap(
7183
boundsInWindow?.width() ?: decorView.width,
7284
boundsInWindow?.height() ?: decorView.height,
73-
Bitmap.Config.ARGB_8888
85+
Bitmap.Config.ARGB_8888,
7486
)
7587
when {
7688
Build.VERSION.SDK_INT < 26 ->
@@ -84,7 +96,7 @@ internal fun Window.generateBitmap(
8496
internal fun Window.generateBitmapFromPixelCopy(
8597
boundsInWindow: Rect? = null,
8698
destBitmap: Bitmap,
87-
bitmapFuture: ResolvableFuture<Bitmap>
99+
bitmapFuture: ResolvableFuture<Bitmap>,
88100
) {
89101
val onCopyFinished =
90102
PixelCopy.OnPixelCopyFinishedListener { result ->
@@ -99,6 +111,6 @@ internal fun Window.generateBitmapFromPixelCopy(
99111
boundsInWindow,
100112
destBitmap,
101113
onCopyFinished,
102-
Handler(Looper.getMainLooper())
114+
Handler(Looper.getMainLooper()),
103115
)
104116
}

0 commit comments

Comments
 (0)