fun assertImageEquals()

in platf-android/src/androidInstrumentedTest/kotlin/org/jetbrains/letsPlot/android/canvas/ImageComparer.kt [18:53]


    fun assertImageEquals(expectedFileName: String, actualBitmap: Bitmap) {
        val testName = expectedFileName.removeSuffix(".bmp")
        val expectedPath = javaClass.classLoader?.getResourceAsStream(expectedFileName)
        val actualFilePath = absPath("${testName}.bmp")
        val diffFilePath = absPath("${testName}_diff.bmp")

        val expectedBitmap = BitmapFactory.decodeStream(expectedPath)
            ?: error("Failed to read expected image: $expectedPath")

        try {
            val expected = exportPixels(expectedBitmap)
            val actual = exportPixels(actualBitmap)

            if (!comparePixelArrays(expected, actual, tolerance = 0)) {
                saveBitmap(createVisualDiff(expectedBitmap, actualBitmap), diffFilePath)
                diffImagePaths += diffFilePath // log diff image path to retrieve later from the device

                saveBitmap(actualBitmap, actualFilePath)

                error(
                    """Image mismatch.
                    |    Diff: $diffFilePath
                    |    Actual: $actualFilePath
                    |    Expected: $expectedPath""".trimMargin()
                )
            } else {
                // Clean up the files if the comparison passed
                File(actualFilePath).delete()
                File(diffFilePath).delete()

                println("Image comparison passed: $expectedPath")
            }
        } finally {
            expectedBitmap.recycle()
        }
    }