Skip to content

Commit

Permalink
Merge pull request #506 from takahirom/takahirom/fix-diff-crash-when-…
Browse files Browse the repository at this point in the history
…size-change/2024-10-12

Add change size test
  • Loading branch information
takahirom authored Oct 12, 2024
2 parents 828760f + 2234f1c commit 2ec3435
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,8 @@ object DefaultFileNameGenerator {
return testName
}

@InternalRoborazziApi
fun reset() {
testNameToTakenCount.clear()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.takahirom.roborazzi

import com.dropbox.differ.ImageComparator
import java.io.File
import kotlin.properties.Delegates

fun interface EmptyCanvasFactory {
operator fun invoke(
Expand Down Expand Up @@ -87,7 +86,7 @@ fun processOutputImageAndReport(
}

// Only used by CaptureResult.Changed
var diffPercentage by Delegates.notNull<Float>()
var diffPercentage: Float? = null

val changed = if (height == goldenRoboCanvas.height && width == goldenRoboCanvas.width) {
val comparisonResult: ImageComparator.ComparisonResult =
Expand All @@ -101,7 +100,6 @@ fun processOutputImageAndReport(
reportLog("${goldenFile.name} The differ result :$comparisonResult changed:$changed")
changed
} else {
diffPercentage = Float.NaN // diff. percentage is not defined if new canvas and golden canvas dimensions differ
reportLog("${goldenFile.name} The image size is changed. actual = (${goldenRoboCanvas.width}, ${goldenRoboCanvas.height}), golden = (${newRoboCanvas.croppedWidth}, ${newRoboCanvas.croppedHeight})")
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ sealed interface CaptureResult {
@SerialName("timestamp")
override val timestampNs: Long,
@SerialName("diff_percentage")
val diffPercentage: Float,
val diffPercentage: Float?,
@SerialName("context_data")
override val contextData: Map<String,@Contextual Any>
) : CaptureResult {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.github.takahirom.roborazzi.sample.boxed

import android.widget.TextView
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.takahirom.roborazzi.DefaultFileNameGenerator
import com.github.takahirom.roborazzi.ROBORAZZI_DEBUG
import com.github.takahirom.roborazzi.RobolectricDeviceQualifiers
import com.github.takahirom.roborazzi.RoborazziOptions
import com.github.takahirom.roborazzi.RoborazziTaskType
import com.github.takahirom.roborazzi.captureRoboImage
import com.github.takahirom.roborazzi.roborazziSystemPropertyOutputDirectory
import com.github.takahirom.roborazzi.sample.MainActivity
import com.github.takahirom.roborazzi.sample.R
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import java.io.File

@RunWith(AndroidJUnit4::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
@Config(
sdk = [30],
qualifiers = RobolectricDeviceQualifiers.NexusOne
)
class SizeChangeTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()

@Test
fun sizeChangeTest() {
boxedEnvironment {
ROBORAZZI_DEBUG = true
val prefix =
"${roborazziSystemPropertyOutputDirectory()}/${this::class.qualifiedName}.sizeChangeTest"
val expectedOutput =
File("$prefix.png")
val expectedCompareOutput = File("${prefix}_compare.png")
expectedOutput.delete()
try {
onView(ViewMatchers.withId(R.id.textview_first))
.captureRoboImage(
roborazziOptions = RoborazziOptions(
taskType = RoborazziTaskType.Record
)
)
composeTestRule.activity.findViewById<TextView>(R.id.textview_first)
.text = "Hello, Roborazzi! This is a test for size change."
DefaultFileNameGenerator.reset()

onView(ViewMatchers.withId(R.id.textview_first))
.captureRoboImage(
roborazziOptions = RoborazziOptions(
taskType = RoborazziTaskType.Compare
)
)
println(expectedCompareOutput.absolutePath + ":" + expectedCompareOutput.exists())
assert(expectedCompareOutput.exists())
} finally {
expectedCompareOutput.delete()
}
}
}
}

0 comments on commit 2ec3435

Please sign in to comment.