Merge changes from topic "cf-pixel-perfect-matching" into tm-qpr-dev

* changes:
  Do pixel-perfect mapping on x86_64 devices only
  Small improvements to dialogScreenshotTest {}
This commit is contained in:
Jordan Demeulenaere
2022-07-05 10:21:36 +00:00
committed by Android (Google) Code Review
2 changed files with 32 additions and 5 deletions

View File

@@ -20,6 +20,7 @@ import android.app.UiModeManager
import android.content.Context import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Canvas import android.graphics.Canvas
import android.os.Build
import android.os.UserHandle import android.os.UserHandle
import android.view.Display import android.view.Display
import android.view.View import android.view.View
@@ -32,6 +33,7 @@ import platform.test.screenshot.GoldenImagePathManager
import platform.test.screenshot.PathConfig import platform.test.screenshot.PathConfig
import platform.test.screenshot.PathElementNoContext import platform.test.screenshot.PathElementNoContext
import platform.test.screenshot.ScreenshotTestRule import platform.test.screenshot.ScreenshotTestRule
import platform.test.screenshot.matchers.MSSIMMatcher
import platform.test.screenshot.matchers.PixelPerfectMatcher import platform.test.screenshot.matchers.PixelPerfectMatcher
/** /**
@@ -55,7 +57,11 @@ class ScreenshotTestRule(private val testSpec: ScreenshotTestSpec) : TestRule {
currentDisplay?.name ?: error("currentDisplay is null") currentDisplay?.name ?: error("currentDisplay is null")
}, },
) )
private val defaultMatcher = PixelPerfectMatcher() private val matcher = if (shouldUsePerfectMatching()) {
PixelPerfectMatcher()
} else {
MSSIMMatcher()
}
private val screenshotRule = private val screenshotRule =
ScreenshotTestRule( ScreenshotTestRule(
@@ -67,6 +73,17 @@ class ScreenshotTestRule(private val testSpec: ScreenshotTestSpec) : TestRule {
) )
) )
private fun shouldUsePerfectMatching(): Boolean {
// Different CPU architectures can sometimes end up rendering differently, so we can't do
// pixel-perfect matching on different architectures using the same golden. Given that our
// presubmits are run on cf_x86_64_phone, our goldens should be perfectly matched on the
// x86_64 architecture and use the Structural Similarity Index on others.
// TODO(b/237511747): Run our screenshot presubmit tests on arm64 instead so that we can
// do pixel perfect matching both at presubmit time and at development time with actual
// devices.
return Build.CPU_ABI == "x86_64"
}
override fun apply(base: Statement, description: Description): Statement { override fun apply(base: Statement, description: Description): Statement {
// The statement which call beforeTest() before running the test and afterTest() afterwards. // The statement which call beforeTest() before running the test and afterTest() afterwards.
val statement = val statement =
@@ -147,7 +164,7 @@ class ScreenshotTestRule(private val testSpec: ScreenshotTestSpec) : TestRule {
// device to assertBitmapAgainstGolden instead? // device to assertBitmapAgainstGolden instead?
currentDisplay = testSpec.display currentDisplay = testSpec.display
currentGoldenIdentifier = goldenIdentifier currentGoldenIdentifier = goldenIdentifier
screenshotRule.assertBitmapAgainstGolden(bitmap, identifierWithSpec, defaultMatcher) screenshotRule.assertBitmapAgainstGolden(bitmap, identifierWithSpec, matcher)
currentDisplay = null currentDisplay = null
currentGoldenIdentifier = goldenIdentifier currentGoldenIdentifier = goldenIdentifier
} }

View File

@@ -60,11 +60,17 @@ class ViewScreenshotTestRule(testSpec: ScreenshotTestSpec) : TestRule {
) { ) {
var dialog: Dialog? = null var dialog: Dialog? = null
activityRule.scenario.onActivity { activity -> activityRule.scenario.onActivity { activity ->
// Make sure that the dialog draws full screen and fits the whole display instead of the
// system bars.
dialog = dialog =
dialogProvider(activity).apply { dialogProvider(activity).apply {
// Make sure that the dialog draws full screen and fits the whole display
// instead of the system bars.
window.setDecorFitsSystemWindows(false) window.setDecorFitsSystemWindows(false)
// Disable enter/exit animations.
create()
window.setWindowAnimations(0)
// Show the dialog.
show() show()
} }
} }
@@ -74,7 +80,11 @@ class ViewScreenshotTestRule(testSpec: ScreenshotTestSpec) : TestRule {
activityRule.scenario.onActivity { activityRule.scenario.onActivity {
// Check that the content is what we expected. // Check that the content is what we expected.
val dialog = dialog ?: error("dialog is null") val dialog = dialog ?: error("dialog is null")
try {
screenshotRule.screenshotTest(goldenIdentifier, dialog.window.decorView) screenshotRule.screenshotTest(goldenIdentifier, dialog.window.decorView)
} finally {
dialog.dismiss()
}
} }
} }
} }