Skip to content

Commit

Permalink
41: Manually fix merge/rebase errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJette committed Oct 9, 2023
1 parent 8aebf90 commit 7a5671c
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 53 deletions.
5 changes: 0 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Annotation>.getAnnotation()` renamed to `Collection<Annotation>.findAnnotation()`.
- Package for `getScreenshotAnnotationName()` changed from `dev.testify.internal.extensions` to `dev.testify.annotation`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ open class ComposableScreenshotRule(
activity.disposeComposition()
}

@Deprecated(
message = "Please use configure()",
replaceWith = ReplaceWith("configure { [email protected] = 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ class AssertExpectedDeviceTest {
@ScreenshotInstrumentation
@Test
fun testMissingBaselineRecordMode() {
rule.setRecordModeEnabled(true)
rule
.configure {
isRecordMode = true
}
.assertSame()
}

Expand Down
6 changes: 6 additions & 0 deletions Library/src/main/java/dev/testify/CompatibilityMethods.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,10 @@ interface CompatibilityMethods<TRule : ScreenshotRule<TActivity>, TActivity : Ac
replaceWith = ReplaceWith("configure { [email protected] = compareMethod }")
)
fun setCompareMethod(compareMethod: CompareMethod?): TRule

@Deprecated(
message = "Please use configure()",
replaceWith = ReplaceWith("configure { [email protected] = isRecordMode }")
)
fun setRecordModeEnabled(isRecordMode: Boolean): TRule
}
37 changes: 2 additions & 35 deletions Library/src/main/java/dev/testify/ScreenshotRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<T : Activity> @JvmOverloads constructor(
Expand Down Expand Up @@ -114,7 +107,6 @@ open class ScreenshotRule<T : Activity> @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
Expand Down Expand Up @@ -163,14 +155,6 @@ open class ScreenshotRule<T : Activity> @JvmOverloads constructor(
return this
}

/**
* Record a new baseline when running the test
*/
fun setRecordModeEnabled(isRecordMode: Boolean): ScreenshotRule<T> {
this.isRecordMode = isRecordMode
return this
}

/**
* Set the configuration for the ScreenshotRule
*
Expand Down Expand Up @@ -213,7 +197,8 @@ open class ScreenshotRule<T : Activity> @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)
}

Expand Down Expand Up @@ -344,24 +329,6 @@ open class ScreenshotRule<T : Activity> @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 {
Expand Down
1 change: 0 additions & 1 deletion Library/src/main/java/dev/testify/ScreenshotUtility.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,15 @@ internal class ScreenshotRuleCompatibilityMethods<TRule : ScreenshotRule<TActivi
}
return rule
}

@Deprecated(
message = "Please use configure()",
replaceWith = ReplaceWith("configure { [email protected] = isRecordMode }")
)
override fun setRecordModeEnabled(isRecordMode: Boolean): TRule {
rule.configure {
this@configure.isRecordMode = isRecordMode
}
return rule
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ typealias ExclusionRectProvider = (rootView: ViewGroup, exclusionRects: MutableS
* The provided [captureMethod] will be used to create and save a [Bitmap] of the Activity and View
* under test.
* @param compareMethod: Allow the test to define a custom bitmap comparison method.
*
* @param isRecordMode: Record a new baseline when running the test
*/
data class TestifyConfiguration(
var exclusionRectProvider: ExclusionRectProvider? = null,
Expand All @@ -84,7 +86,8 @@ data class TestifyConfiguration(
var pauseForInspection: Boolean = false,
var hideSoftKeyboard: Boolean = true,
var captureMethod: CaptureMethod? = null,
var compareMethod: CompareMethod? = null
var compareMethod: CompareMethod? = null,
var isRecordMode: Boolean = false
) {

init {
Expand Down Expand Up @@ -120,6 +123,7 @@ data class TestifyConfiguration(
(this != null) &&
(orientationToIgnore != SCREEN_ORIENTATION_UNSPECIFIED) &&
(activity.isRequestedOrientation(orientationToIgnore)) -> true

else -> false
}
}
Expand Down
19 changes: 16 additions & 3 deletions Library/src/main/java/dev/testify/internal/logic/AssertSame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,6 +83,9 @@ internal fun <TActivity : Activity> assertSame(
activityIntent: Intent?,
reporter: Reporter?
) {
fun isRecordMode(): Boolean =
TestInstrumentationRegistry.isRecordMode || configuration.isRecordMode

state.assertSameInvoked = true

screenshotLifecycleHost.notifyObservers { it.beforeAssertSame() }
Expand Down Expand Up @@ -129,13 +133,19 @@ internal fun <TActivity : Activity> 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(
Expand All @@ -155,9 +165,12 @@ internal fun <TActivity : Activity> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,7 +53,6 @@ class HighContrastDiff(private val exclusionRects: Set<Rect>) {
private lateinit var baselineBitmap: Bitmap
private lateinit var currentBitmap: Bitmap

@ExperimentalTestApi
fun generate(context: Context) {
val transformResult = ParallelPixelProcessor
.create()
Expand Down
4 changes: 3 additions & 1 deletion Library/src/test/java/dev/testify/ScreenshotRuleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ class ScreenshotRuleTest {
every { loadBaselineBitmapForComparison(any(), any()) } returns null

subject
.setRecordModeEnabled(true)
.configure {
isRecordMode = true
}
.assertSame()

verifyReporter()
Expand Down
11 changes: 7 additions & 4 deletions Library/src/test/java/dev/testify/output/DestinationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
)
}

Expand All @@ -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()
)
}

Expand All @@ -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()
)
}

Expand Down Expand Up @@ -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()
)
}
}

0 comments on commit 7a5671c

Please sign in to comment.