From a19421ee930851c419b6ce6343df74a84e19231f Mon Sep 17 00:00:00 2001 From: Josep Rubio Date: Mon, 9 Oct 2023 12:17:31 +0200 Subject: [PATCH] ANDROID-13796 add compareScreenshot methods in ScreenshotsTest --- .../mistica/compose/button/ButtonKtTest.kt | 10 +------ .../compose/input/PasswordInputKtTest.kt | 5 +--- .../telefonica/mistica/input/TextInputTest.kt | 6 ++-- .../mistica/testutils/ScreenshotsTest.kt | 29 ++++++++++++++++++- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/library/src/test/java/com/telefonica/mistica/compose/button/ButtonKtTest.kt b/library/src/test/java/com/telefonica/mistica/compose/button/ButtonKtTest.kt index 117030b1b..6f55080c4 100644 --- a/library/src/test/java/com/telefonica/mistica/compose/button/ButtonKtTest.kt +++ b/library/src/test/java/com/telefonica/mistica/compose/button/ButtonKtTest.kt @@ -6,7 +6,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onRoot import androidx.compose.ui.unit.dp -import com.github.takahirom.roborazzi.captureRoboImage import com.telefonica.mistica.compose.theme.MisticaTheme import com.telefonica.mistica.compose.theme.brand.BlauBrand import com.telefonica.mistica.compose.theme.brand.Brand @@ -14,7 +13,6 @@ import com.telefonica.mistica.compose.theme.brand.MovistarBrand import com.telefonica.mistica.compose.theme.brand.O2Brand import com.telefonica.mistica.compose.theme.brand.TelefonicaBrand import com.telefonica.mistica.compose.theme.brand.VivoBrand -import com.telefonica.mistica.testutils.ScreenshotUtils import com.telefonica.mistica.testutils.ScreenshotsTest import org.junit.Rule import org.junit.Test @@ -48,13 +46,7 @@ internal class ButtonKtTest(private val brand: Brand, private val darkTheme: Boo } private fun `then screenshot is OK`(brand: Brand, darkTheme: Boolean) { - composeTestRule.onRoot() - .captureRoboImage( - ScreenshotUtils.getScreenshotName( - brand = brand, - extra = if (darkTheme) "_dark" else "" - ) - ) + compareScreenshot(composeTestRule.onRoot(), brand, darkTheme) } companion object { diff --git a/library/src/test/java/com/telefonica/mistica/compose/input/PasswordInputKtTest.kt b/library/src/test/java/com/telefonica/mistica/compose/input/PasswordInputKtTest.kt index 0d8dd3a37..579ad96e5 100644 --- a/library/src/test/java/com/telefonica/mistica/compose/input/PasswordInputKtTest.kt +++ b/library/src/test/java/com/telefonica/mistica/compose/input/PasswordInputKtTest.kt @@ -4,10 +4,8 @@ import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performClick -import com.github.takahirom.roborazzi.captureRoboImage import com.telefonica.mistica.compose.theme.MisticaTheme import com.telefonica.mistica.compose.theme.brand.MovistarBrand -import com.telefonica.mistica.testutils.ScreenshotUtils import com.telefonica.mistica.testutils.ScreenshotsTest import org.junit.Rule import org.junit.Test @@ -67,8 +65,7 @@ internal class PasswordInputKtTest: ScreenshotsTest() { } private fun `then screenshot is OK`() { - composeTestRule.onRoot() - .captureRoboImage(ScreenshotUtils.getScreenshotName()) + compareScreenshot(composeTestRule.onRoot()) } private fun test(block: TestScope.() -> Unit) { diff --git a/library/src/test/java/com/telefonica/mistica/input/TextInputTest.kt b/library/src/test/java/com/telefonica/mistica/input/TextInputTest.kt index 7342ebfef..3043a4417 100644 --- a/library/src/test/java/com/telefonica/mistica/input/TextInputTest.kt +++ b/library/src/test/java/com/telefonica/mistica/input/TextInputTest.kt @@ -8,10 +8,8 @@ import androidx.test.espresso.Espresso.onView import androidx.test.espresso.matcher.ViewMatchers import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.ext.junit.rules.activityScenarioRule -import com.github.takahirom.roborazzi.captureRoboImage import com.telefonica.mistica.DummyActivity import com.telefonica.mistica.R -import com.telefonica.mistica.testutils.ScreenshotUtils import com.telefonica.mistica.testutils.ScreenshotsTest import org.junit.Before import org.junit.Rule @@ -39,7 +37,7 @@ internal class TextInputTest: ScreenshotsTest() { wrapper.addView(textInput) textInput.text = "Hello android devs!" - onView(ViewMatchers.withId(R.id.dummy_activity_wrapper)).captureRoboImage(ScreenshotUtils.getScreenshotName()) + compareScreenshot(onView(ViewMatchers.withId(R.id.dummy_activity_wrapper))) } } @@ -57,7 +55,7 @@ internal class TextInputTest: ScreenshotsTest() { val scrollView: ScrollView = activity.findViewById(R.id.scroll_view) scrollView.scrollTo(0, wrapper.height * i) - onView(withId(R.id.dummy_activity_wrapper)).captureRoboImage(ScreenshotUtils.getScreenshotName(extra = "_${i + 1}")) + compareScreenshot(onView(withId(R.id.dummy_activity_wrapper)), extra = "_${i + 1}") } } } diff --git a/library/src/test/java/com/telefonica/mistica/testutils/ScreenshotsTest.kt b/library/src/test/java/com/telefonica/mistica/testutils/ScreenshotsTest.kt index 5e19565d8..e147b6d2e 100644 --- a/library/src/test/java/com/telefonica/mistica/testutils/ScreenshotsTest.kt +++ b/library/src/test/java/com/telefonica/mistica/testutils/ScreenshotsTest.kt @@ -1,7 +1,34 @@ package com.telefonica.mistica.testutils +import androidx.compose.ui.test.SemanticsNodeInteraction +import androidx.test.espresso.ViewInteraction import com.github.takahirom.roborazzi.RobolectricDeviceQualifiers +import com.github.takahirom.roborazzi.captureRoboImage +import com.telefonica.mistica.compose.theme.brand.Brand import org.robolectric.annotation.Config @Config(qualifiers = RobolectricDeviceQualifiers.Pixel5) -open class ScreenshotsTest +open class ScreenshotsTest { + fun compareScreenshot( + node: SemanticsNodeInteraction, + brand: Brand? = null, + darkTheme: Boolean = false, + extra: String = "", + ) { + node.captureRoboImage(screenshotName(brand, darkTheme, extra)) + } + + fun compareScreenshot( + node: ViewInteraction, + brand: Brand? = null, + darkTheme: Boolean = false, + extra: String = "", + ) { + node.captureRoboImage(screenshotName(brand, darkTheme, extra)) + } + + private fun screenshotName(brand: Brand?, darkTheme: Boolean, extra: String) = ScreenshotUtils.getScreenshotName( + brand = brand, + extra = if (darkTheme) "_dark$extra" else extra + ) +}