Merge "Test: atest -c MyRoboTests Bug: 266743080 Add support for Robolectric based RNG screenshot diffing" into udc-dev

This commit is contained in:
Ram Peri
2023-03-16 09:58:36 +00:00
committed by Android (Google) Code Review
2 changed files with 21 additions and 9 deletions

View File

@@ -53,6 +53,7 @@ import kotlinx.coroutines.runBlocking
fun View.captureToBitmap(window: Window? = null): ListenableFuture<Bitmap> {
val bitmapFuture: ResolvableFuture<Bitmap> = ResolvableFuture.create()
val mainExecutor = HandlerExecutor(Handler(Looper.getMainLooper()))
val isRobolectric = if (Build.FINGERPRINT.contains("robolectric")) true else false
// disable drawing again if necessary once work is complete
if (!HardwareRendererCompat.isDrawingEnabled()) {
@@ -61,8 +62,12 @@ fun View.captureToBitmap(window: Window? = null): ListenableFuture<Bitmap> {
}
mainExecutor.execute {
val forceRedrawFuture = forceRedraw()
forceRedrawFuture.addListener({ generateBitmap(bitmapFuture, window) }, mainExecutor)
if (isRobolectric) {
generateBitmap(bitmapFuture)
} else {
val forceRedrawFuture = forceRedraw()
forceRedrawFuture.addListener({ generateBitmap(bitmapFuture, window) }, mainExecutor)
}
}
return bitmapFuture

View File

@@ -19,6 +19,7 @@ package com.android.systemui.testing.screenshot
import android.app.Activity
import android.app.Dialog
import android.graphics.Bitmap
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
@@ -26,6 +27,7 @@ import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import androidx.activity.ComponentActivity
import androidx.test.ext.junit.rules.ActivityScenarioRule
import java.util.concurrent.TimeUnit
import org.junit.Assert.assertEquals
import org.junit.rules.RuleChain
import org.junit.rules.TestRule
@@ -54,14 +56,14 @@ open class ViewScreenshotTestRule(
)
)
private val activityRule = ActivityScenarioRule(ScreenshotActivity::class.java)
private val delegateRule =
RuleChain.outerRule(colorsRule)
.around(deviceEmulationRule)
.around(screenshotRule)
.around(activityRule)
private val roboRule =
RuleChain.outerRule(deviceEmulationRule).around(screenshotRule).around(activityRule)
private val delegateRule = RuleChain.outerRule(colorsRule).around(roboRule)
private val isRobolectric = if (Build.FINGERPRINT.contains("robolectric")) true else false
override fun apply(base: Statement, description: Description): Statement {
return delegateRule.apply(base, description)
val ruleToApply = if (isRobolectric) roboRule else delegateRule
return ruleToApply.apply(base, description)
}
protected fun takeScreenshot(
@@ -94,7 +96,12 @@ open class ViewScreenshotTestRule(
contentView = content.getChildAt(0)
}
return contentView?.toBitmap() ?: error("contentView is null")
return if (isRobolectric) {
contentView?.captureToBitmap()?.get(10, TimeUnit.SECONDS)
?: error("timeout while trying to capture view to bitmap")
} else {
contentView?.toBitmap() ?: error("contentView is null")
}
}
/**