From 7a5671c52de8af9692959562221dcd28dbcb62f2 Mon Sep 17 00:00:00 2001 From: Daniel Jette Date: Mon, 9 Oct 2023 19:27:02 -0400 Subject: [PATCH] 41: Manually fix merge/rebase errors --- CHANGELOG.md | 5 --- .../dev/testify/ComposableScreenshotRule.kt | 9 +++++ .../dev/testify/AssertExpectedDeviceTest.kt | 5 ++- .../java/dev/testify/CompatibilityMethods.kt | 6 +++ .../main/java/dev/testify/ScreenshotRule.kt | 37 +------------------ .../java/dev/testify/ScreenshotUtility.kt | 1 - .../ScreenshotRuleCompatibilityMethods.kt | 11 ++++++ .../testify/internal/TestifyConfiguration.kt | 6 ++- .../dev/testify/internal/logic/AssertSame.kt | 19 ++++++++-- .../processor/diff/HighContrastDiff.kt | 2 - .../java/dev/testify/ScreenshotRuleTest.kt | 4 +- .../dev/testify/output/DestinationTest.kt | 11 ++++-- 12 files changed, 63 insertions(+), 53 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5831b1a8..ed11f7fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,11 +6,6 @@ #### Changed -- `ScreenshotRule.getRootView()` is now an extension function `fun Activity.findRootView(@IdRes rootViewId: Int): ViewGroup`. -- `ScreenshotRule.setCaptureMethod()` is deprecated. Use `var captureMethod: CaptureMethod?` on `TestifyConfiguration` to set the capture method. -- `ScreenshotRule.setCompareMethod()` is deprecated. Use `var compareMethod: CompareMethod?` on `TestifyConfiguration` to set the compare method. -- `ScreenshotRule.compareBitmaps()` is now a top-level function. -- `ScreenshotRule.takeScreenshot()` is now a top-level function. - `ScreenshotRule.getScreenshotInstrumentationAnnotation()` is now a top-level function. - `Collection.getAnnotation()` renamed to `Collection.findAnnotation()`. - Package for `getScreenshotAnnotationName()` changed from `dev.testify.internal.extensions` to `dev.testify.annotation`. diff --git a/Ext/Compose/src/main/java/dev/testify/ComposableScreenshotRule.kt b/Ext/Compose/src/main/java/dev/testify/ComposableScreenshotRule.kt index 248ffc67..a873262e 100644 --- a/Ext/Compose/src/main/java/dev/testify/ComposableScreenshotRule.kt +++ b/Ext/Compose/src/main/java/dev/testify/ComposableScreenshotRule.kt @@ -63,6 +63,15 @@ open class ComposableScreenshotRule( activity.disposeComposition() } + @Deprecated( + message = "Please use configure()", + replaceWith = ReplaceWith("configure { this@configure.captureMethod = captureMethod }") + ) + override fun setCaptureMethod(captureMethod: CaptureMethod?): ComposableScreenshotRule { + this.captureMethod = configuration.captureMethod ?: ::pixelCopyCapture + return this + } + /** * Set a screenshot view provider to capture only the @Composable bounds */ diff --git a/Library/src/androidTest/java/dev/testify/AssertExpectedDeviceTest.kt b/Library/src/androidTest/java/dev/testify/AssertExpectedDeviceTest.kt index efd1a186..4f422cdd 100644 --- a/Library/src/androidTest/java/dev/testify/AssertExpectedDeviceTest.kt +++ b/Library/src/androidTest/java/dev/testify/AssertExpectedDeviceTest.kt @@ -68,7 +68,10 @@ class AssertExpectedDeviceTest { @ScreenshotInstrumentation @Test fun testMissingBaselineRecordMode() { - rule.setRecordModeEnabled(true) + rule + .configure { + isRecordMode = true + } .assertSame() } diff --git a/Library/src/main/java/dev/testify/CompatibilityMethods.kt b/Library/src/main/java/dev/testify/CompatibilityMethods.kt index 48292d8f..3178e9f6 100644 --- a/Library/src/main/java/dev/testify/CompatibilityMethods.kt +++ b/Library/src/main/java/dev/testify/CompatibilityMethods.kt @@ -124,4 +124,10 @@ interface CompatibilityMethods, TActivity : Ac replaceWith = ReplaceWith("configure { this@configure.compareMethod = compareMethod }") ) fun setCompareMethod(compareMethod: CompareMethod?): TRule + + @Deprecated( + message = "Please use configure()", + replaceWith = ReplaceWith("configure { this@configure.isRecordMode = isRecordMode }") + ) + fun setRecordModeEnabled(isRecordMode: Boolean): TRule } diff --git a/Library/src/main/java/dev/testify/ScreenshotRule.kt b/Library/src/main/java/dev/testify/ScreenshotRule.kt index 8780dd0b..b6f6e709 100644 --- a/Library/src/main/java/dev/testify/ScreenshotRule.kt +++ b/Library/src/main/java/dev/testify/ScreenshotRule.kt @@ -35,7 +35,6 @@ import androidx.annotation.CallSuper import androidx.annotation.IdRes import androidx.annotation.LayoutRes import androidx.annotation.VisibleForTesting -import androidx.test.annotation.ExperimentalTestApi import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation import androidx.test.rule.ActivityTestRule import dev.testify.annotation.TestifyLayout @@ -46,14 +45,9 @@ import dev.testify.internal.ScreenshotRuleCompatibilityMethods import dev.testify.internal.TestifyConfiguration import dev.testify.internal.exception.ActivityNotRegisteredException import dev.testify.internal.exception.AssertSameMustBeLastException -import dev.testify.internal.exception.FinalizeDestinationException import dev.testify.internal.exception.MissingAssertSameException import dev.testify.internal.exception.MissingScreenshotInstrumentationAnnotationException import dev.testify.internal.exception.ScreenshotTestIgnoredException -import dev.testify.internal.extensions.TestInstrumentationRegistry.Companion.isRecordMode -import dev.testify.internal.extensions.TestInstrumentationRegistry.Companion.getModuleName -import dev.testify.internal.extensions.TestInstrumentationRegistry.Companion.instrumentationPrintln -import dev.testify.internal.extensions.cyan import dev.testify.internal.extensions.isInvokedFromPlugin import dev.testify.internal.helpers.ActivityProvider import dev.testify.internal.helpers.EspressoActions @@ -70,7 +64,6 @@ import org.junit.AssumptionViolatedException import org.junit.rules.TestRule import org.junit.runner.Description import org.junit.runners.model.Statement -import dev.testify.internal.extensions.TestInstrumentationRegistry.Companion.isRecordMode as recordMode @Suppress("unused", "MemberVisibilityCanBePrivate") open class ScreenshotRule @JvmOverloads constructor( @@ -114,7 +107,6 @@ open class ScreenshotRule @JvmOverloads constructor( override var throwable: Throwable? = null override var viewModification: ViewModification? = null private var extrasProvider: ExtrasProvider? = null - private var isRecordMode: Boolean = false @VisibleForTesting internal var reporter: Reporter? = null @@ -163,14 +155,6 @@ open class ScreenshotRule @JvmOverloads constructor( return this } - /** - * Record a new baseline when running the test - */ - fun setRecordModeEnabled(isRecordMode: Boolean): ScreenshotRule { - this.isRecordMode = isRecordMode - return this - } - /** * Set the configuration for the ScreenshotRule * @@ -213,7 +197,8 @@ open class ScreenshotRule @JvmOverloads constructor( val methodAnnotations = description.annotations apply(description.methodName, description.testClass, methodAnnotations) - statement = ScreenshotStatement(base) + val statement = ScreenshotStatement(base) + this.statement = statement return super.apply(statement, description) } @@ -344,24 +329,6 @@ open class ScreenshotRule @JvmOverloads constructor( getInstrumentation().registerActivityProvider(this) } - /** - * Given [baselineBitmap] and [currentBitmap], use [HighContrastDiff] to write a companion .diff image for the - * current test. - * - * This diff image is a high-contrast image where each difference, regardless of how minor, is indicated in red - * against a black background. - */ - @ExperimentalTestApi - open fun generateHighContrastDiff(baselineBitmap: Bitmap, currentBitmap: Bitmap) { - HighContrastDiff(configuration.exclusionRects) - .name(outputFileName) - .baseline(baselineBitmap) - .current(currentBitmap) - .exactness(configuration.exactness) - .generate(context = activity) - } - - @ExperimentalTestApi fun assertSame() { addScreenshotObserver(this) try { diff --git a/Library/src/main/java/dev/testify/ScreenshotUtility.kt b/Library/src/main/java/dev/testify/ScreenshotUtility.kt index 163f3e58..8a26a472 100644 --- a/Library/src/main/java/dev/testify/ScreenshotUtility.kt +++ b/Library/src/main/java/dev/testify/ScreenshotUtility.kt @@ -49,7 +49,6 @@ val preferredBitmapOptions: BitmapFactory.Options return options } -@ExperimentalTestApi fun saveBitmapToDestination(context: Context, bitmap: Bitmap?, destination: Destination): Boolean { if (bitmap == null) { return false diff --git a/Library/src/main/java/dev/testify/internal/ScreenshotRuleCompatibilityMethods.kt b/Library/src/main/java/dev/testify/internal/ScreenshotRuleCompatibilityMethods.kt index acece104..90e0f79f 100644 --- a/Library/src/main/java/dev/testify/internal/ScreenshotRuleCompatibilityMethods.kt +++ b/Library/src/main/java/dev/testify/internal/ScreenshotRuleCompatibilityMethods.kt @@ -216,4 +216,15 @@ internal class ScreenshotRuleCompatibilityMethods true + else -> false } } diff --git a/Library/src/main/java/dev/testify/internal/logic/AssertSame.kt b/Library/src/main/java/dev/testify/internal/logic/AssertSame.kt index ff0e8dc3..fbb28960 100644 --- a/Library/src/main/java/dev/testify/internal/logic/AssertSame.kt +++ b/Library/src/main/java/dev/testify/internal/logic/AssertSame.kt @@ -35,6 +35,7 @@ import dev.testify.internal.DeviceStringFormatter import dev.testify.internal.TestifyConfiguration import dev.testify.internal.assertExpectedDevice import dev.testify.internal.exception.FailedToCaptureBitmapException +import dev.testify.internal.exception.FinalizeDestinationException import dev.testify.internal.exception.NoScreenshotsOnUiThreadException import dev.testify.internal.exception.ScreenshotBaselineNotDefinedException import dev.testify.internal.exception.ScreenshotIsDifferentException @@ -82,6 +83,9 @@ internal fun assertSame( activityIntent: Intent?, reporter: Reporter? ) { + fun isRecordMode(): Boolean = + TestInstrumentationRegistry.isRecordMode || configuration.isRecordMode + state.assertSameInvoked = true screenshotLifecycleHost.notifyObservers { it.beforeAssertSame() } @@ -129,13 +133,19 @@ internal fun assertSame( Thread.sleep(LAYOUT_INSPECTION_TIME_MS) } - assertExpectedDevice(testContext, description.name) + val isRecordMode = isRecordMode() + + assertExpectedDevice(testContext, description.name, isRecordMode) + + val destination = getDestination(activity, outputFileName) val baselineBitmap = loadBaselineBitmapForComparison(testContext, description.name) - ?: if (TestInstrumentationRegistry.isRecordMode) { + ?: if (isRecordMode()) { TestInstrumentationRegistry.instrumentationPrintln( "\n\t✓ " + "Recording baseline for ${description.name}".cyan() ) + if (!destination.finalize()) + throw FinalizeDestinationException(destination.description) return } else { throw ScreenshotBaselineNotDefinedException( @@ -155,9 +165,12 @@ internal fun assertSame( if (compareBitmaps(baselineBitmap, currentBitmap, configuration.getBitmapCompare())) { Assert.assertTrue( "Could not delete cached bitmap ${description.name}", - deleteBitmap(getDestination(activity, outputFileName)) + deleteBitmap(destination) ) } else { + if (!destination.finalize()) + throw FinalizeDestinationException(destination.description) + if (TestifyFeatures.GenerateDiffs.isEnabled(activity)) { HighContrastDiff(configuration.exclusionRects) .name(outputFileName) diff --git a/Library/src/main/java/dev/testify/internal/processor/diff/HighContrastDiff.kt b/Library/src/main/java/dev/testify/internal/processor/diff/HighContrastDiff.kt index d2e3a493..3ce8d6ec 100644 --- a/Library/src/main/java/dev/testify/internal/processor/diff/HighContrastDiff.kt +++ b/Library/src/main/java/dev/testify/internal/processor/diff/HighContrastDiff.kt @@ -27,7 +27,6 @@ import android.content.Context import android.graphics.Bitmap import android.graphics.Color import android.graphics.Rect -import androidx.test.annotation.ExperimentalTestApi import dev.testify.internal.processor.ParallelPixelProcessor import dev.testify.internal.processor.compare.colorspace.calculateDeltaE import dev.testify.internal.processor.createBitmap @@ -54,7 +53,6 @@ class HighContrastDiff(private val exclusionRects: Set) { private lateinit var baselineBitmap: Bitmap private lateinit var currentBitmap: Bitmap - @ExperimentalTestApi fun generate(context: Context) { val transformResult = ParallelPixelProcessor .create() diff --git a/Library/src/test/java/dev/testify/ScreenshotRuleTest.kt b/Library/src/test/java/dev/testify/ScreenshotRuleTest.kt index 56daf20b..4e5b01b1 100644 --- a/Library/src/test/java/dev/testify/ScreenshotRuleTest.kt +++ b/Library/src/test/java/dev/testify/ScreenshotRuleTest.kt @@ -283,7 +283,9 @@ class ScreenshotRuleTest { every { loadBaselineBitmapForComparison(any(), any()) } returns null subject - .setRecordModeEnabled(true) + .configure { + isRecordMode = true + } .assertSame() verifyReporter() diff --git a/Library/src/test/java/dev/testify/output/DestinationTest.kt b/Library/src/test/java/dev/testify/output/DestinationTest.kt index 122a053f..6b44b4ca 100644 --- a/Library/src/test/java/dev/testify/output/DestinationTest.kt +++ b/Library/src/test/java/dev/testify/output/DestinationTest.kt @@ -54,6 +54,9 @@ class DestinationTest { @RelaxedMockK lateinit var mockInstrumentation: Instrumentation + private fun String.normalizePath() = + this.replace("\\", "/").replace(this.substring(0, this.indexOf(":") + 1), "") + @Before fun setUp() { MockKAnnotations.init(this, relaxUnitFun = true) @@ -91,7 +94,7 @@ class DestinationTest { ) assertEquals( "/data/user/0/dev.testify.sample/app_images/root/screenshots/33-1080x2200@420dp-en_CA/fileName.ext", - destination.description + destination.description.normalizePath() ) } @@ -106,7 +109,7 @@ class DestinationTest { ) assertEquals( "/data/user/0/dev.testify.sample/app_images/root/custom_key/fileName.ext", - destination.description + destination.description.normalizePath() ) } @@ -123,7 +126,7 @@ class DestinationTest { ) assertEquals( "/storage/emulated/0/Android/data/dev.testify.sample/files/root/33-1080x2200@420dp-en_CA/fileName.ext", - destination.description + destination.description.normalizePath() ) } @@ -154,7 +157,7 @@ class DestinationTest { assertTrue("destination is $destination", destination is TestStorageDestination) assertEquals( "/data/user/0/dev.testify.sample/app_images/root/screenshots/33-1080x2200@420dp-en_CA/fileName.ext", - destination.description + destination.description.normalizePath() ) } }