Skip to content

Commit

Permalink
ANDROID-13796 add compareScreenshot methods in ScreenshotsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jeprubio committed Oct 9, 2023
1 parent 36b3aea commit a19421e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ 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
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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
}
}

Expand All @@ -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}")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)
}

0 comments on commit a19421e

Please sign in to comment.