Skip to content

Commit acc7d02

Browse files
brettchabotcopybara-androidxtest
authored andcommitted
Use an API to determine if draw listener logic should be invoked.
Currently ViewCapture hardcodes a Robolectric check and skips certain logic. This commit changes this logic to use a ControlledLooper API instead, so in future the real logic can be executed when Robolectric supports it. PiperOrigin-RevId: 615567737
1 parent 4aeb7a7 commit acc7d02

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

core/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
**Bug Fixes**
88

9+
* make ViewCapture use ControlledLooper API instead of hardcoding is Robolectric check
10+
911
**New Features**
1012

1113
**Breaking Changes**

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import androidx.annotation.RequiresApi
3535
import androidx.concurrent.futures.ResolvableFuture
3636
import androidx.test.annotation.ExperimentalTestApi
3737
import androidx.test.core.internal.os.HandlerExecutor
38+
import androidx.test.internal.platform.ServiceLoaderWrapper
39+
import androidx.test.internal.platform.os.ControlledLooper
3840
import androidx.test.internal.platform.reflect.ReflectiveField
3941
import androidx.test.internal.platform.reflect.ReflectiveMethod
4042
import androidx.test.platform.graphics.HardwareRendererCompat
@@ -74,17 +76,23 @@ fun View.captureToBitmap(rect: Rect? = null): ListenableFuture<Bitmap> {
7476
}
7577

7678
mainExecutor.execute {
77-
if (Build.FINGERPRINT.contains("robolectric")) {
78-
generateBitmap(bitmapFuture, rect)
79-
} else {
79+
if (getControlledLooper().areDrawCallbacksSupported()) {
8080
val forceRedrawFuture = forceRedraw()
8181
forceRedrawFuture.addListener({ generateBitmap(bitmapFuture, rect) }, mainExecutor)
82+
} else {
83+
generateBitmap(bitmapFuture, rect)
8284
}
8385
}
8486

8587
return bitmapFuture
8688
}
8789

90+
private fun getControlledLooper(): ControlledLooper {
91+
return ServiceLoaderWrapper.loadSingleService(ControlledLooper::class.java) {
92+
ControlledLooper.NO_OP_CONTROLLED_LOOPER
93+
}
94+
}
95+
8896
/**
8997
* Equivalent to [captureToBitmap] for now. In the future [captureToBitmap] will be changed to a
9098
* suspend function.

runner/monitor/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Upstream TestStorage.getInputFileUri and getOutputFileUri to PlatformTestStorage
1717
* Change PlatformTestStorage methods to throw FileNotFoundException instead of
1818
IOException
19+
* Add internal ControlledLooper#isDrawCallbacksSupported.
1920

2021
**Breaking API Changes**
2122

runner/monitor/java/androidx/test/api/current_internal.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ package androidx.test.internal.platform.content {
5353
package androidx.test.internal.platform.os {
5454

5555
@RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP) public interface ControlledLooper {
56+
method public default boolean areDrawCallbacksSupported();
5657
method public void drainMainThreadUntilIdle();
5758
method public void simulateWindowFocus(android.view.View!);
5859
field public static final androidx.test.internal.platform.os.ControlledLooper! NO_OP_CONTROLLED_LOOPER;

runner/monitor/java/androidx/test/internal/platform/os/ControlledLooper.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,22 @@ public interface ControlledLooper {
3939
/** Generate window focus event for given view. */
4040
void simulateWindowFocus(View decorView);
4141

42+
/** Returns true if registerFrameCommitCallback() and addOnDrawListener callbacks are supported */
43+
default boolean areDrawCallbacksSupported() {
44+
return false;
45+
}
46+
4247
public static final ControlledLooper NO_OP_CONTROLLED_LOOPER =
4348
new ControlledLooper() {
4449
@Override
4550
public void drainMainThreadUntilIdle() {}
4651

4752
@Override
4853
public void simulateWindowFocus(View decorView) {}
54+
55+
@Override
56+
public boolean areDrawCallbacksSupported() {
57+
return true;
58+
}
4959
};
5060
}

0 commit comments

Comments
 (0)