Skip to content

Commit

Permalink
ANDROID-13871 Add screenshot tests for all types of buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
pmartinbTEF committed Oct 23, 2023
1 parent a53c368 commit a7713f2
Show file tree
Hide file tree
Showing 244 changed files with 78 additions and 36 deletions.
Binary file added library/screenshots/Button_DANGER_BlauBrand.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added library/screenshots/Button_DANGER_O2Brand.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added library/screenshots/Button_DANGER_VivoBrand.png
Binary file added library/screenshots/Button_LINK_BlauBrand.png
Binary file added library/screenshots/Button_LINK_INVERSE_O2Brand.png
Binary file added library/screenshots/Button_LINK_O2Brand.png
Binary file added library/screenshots/Button_LINK_O2Brand_dark.png
Binary file added library/screenshots/Button_LINK_O2Brand_icon.png
Binary file added library/screenshots/Button_LINK_VivoBrand.png
Binary file added library/screenshots/Button_SECONDARY_O2Brand.png
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,26 @@ import org.junit.runner.RunWith
import org.robolectric.ParameterizedRobolectricTestRunner

@RunWith(ParameterizedRobolectricTestRunner::class)
internal class ButtonKtTest(private val brand: Brand, private val darkTheme: Boolean): ScreenshotsTest() {
internal class ButtonKtTest(private val brand: Brand, private val style: ButtonStyle, private val icon: Boolean, private val darkTheme: Boolean):
ScreenshotsTest() {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun `check the button screenshot`() {
`when Button`(brand, darkTheme)
`when Button`(brand, style, icon, darkTheme)

`then screenshot is OK`(brand, darkTheme)
`then screenshot is OK`(brand, style, icon, darkTheme)
}

private fun `when Button`(brand: Brand = MovistarBrand, darkTheme: Boolean) {
private fun `when Button`(brand: Brand = MovistarBrand, style: ButtonStyle, icon: Boolean, darkTheme: Boolean) {
composeTestRule.setContent {
MisticaTheme(brand = brand, darkTheme = darkTheme) {
Surface {
Button(
text = "textValue",
buttonStyle = style,
icon = android.R.drawable.ic_lock_power_off.takeIf { icon },
onClickListener = { },
modifier = Modifier.padding(16.dp)
)
Expand All @@ -45,24 +48,44 @@ internal class ButtonKtTest(private val brand: Brand, private val darkTheme: Boo
}
}

private fun `then screenshot is OK`(brand: Brand, darkTheme: Boolean) {
compareScreenshot(composeTestRule.onRoot(), brand, darkTheme)
private fun `then screenshot is OK`(brand: Brand, style: ButtonStyle, icon: Boolean, darkTheme: Boolean) {
val iconSuffix = if(icon) {
"icon"
} else {
null
}
compareScreenshot(composeTestRule.onRoot(), "Button", style.toString(), brand, darkTheme, iconSuffix)
}

companion object {
@JvmStatic
@ParameterizedRobolectricTestRunner.Parameters(name = "Input: {0}")
fun brands() = listOf(
arrayOf(MovistarBrand, false),
arrayOf(VivoBrand, false),
arrayOf(O2Brand, false),
arrayOf(BlauBrand, false),
arrayOf(TelefonicaBrand, false),
arrayOf(MovistarBrand, true),
arrayOf(VivoBrand, true),
arrayOf(O2Brand, true),
arrayOf(BlauBrand, true),
arrayOf(TelefonicaBrand, true),
)
fun brands(): List<Array<Any>> {
val brands2 = listOf(
MovistarBrand,
VivoBrand,
O2Brand,
BlauBrand,
TelefonicaBrand,
)
val themes = listOf(false, true)
val buttonStyles = listOf(
ButtonStyle.PRIMARY, ButtonStyle.PRIMARY_SMALL, ButtonStyle.SECONDARY, ButtonStyle.SECONDARY_SMALL, ButtonStyle.DANGER,
ButtonStyle.DANGER_SMALL, ButtonStyle.LINK, ButtonStyle.PRIMARY_INVERSE, ButtonStyle.PRIMARY_SMALL_INVERSE, ButtonStyle.SECONDARY_INVERSE,
ButtonStyle.SECONDARY_SMALL_INVERSE, ButtonStyle.LINK_INVERSE
)
val icons = listOf(false, true)

return brands2.flatMap { brand ->
buttonStyles.flatMap { buttonStyle ->
icons.flatMap { hasIcon ->
themes.map { darkTheme ->
arrayOf(brand, buttonStyle, hasIcon, darkTheme)
}
}
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.rules.activityScenarioRule
import com.telefonica.mistica.DummyActivity
import com.telefonica.mistica.R
import com.telefonica.mistica.compose.theme.brand.MovistarBrand
import com.telefonica.mistica.testutils.ScreenshotsTest
import org.junit.Rule
import org.junit.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@ package com.telefonica.mistica.testutils
import com.telefonica.mistica.compose.theme.brand.Brand

object ScreenshotUtils {
fun getScreenshotName(brand: Brand? = null, extra: String = ""): String {
return """screenshots/
${TestUtils.findRunningTestMethodName()}
${brandName(brand, prepend = "_")}${extra}.png"""
.replace("\\s+".toRegex(), "")
}

private fun brandName(brand: Brand?, prepend: String = "", append: String = ""): String {
return if (brand != null) {
"$prepend${brand::class.java.simpleName}$append"
fun getScreenshotName(component: String?, style: String?, brand: Brand?, darkTheme: Boolean, extra: String?): String {
val componentOrTestName = if(component == null) {
TestUtils.findRunningTestMethodName()
} else {
"${component}_${style}"
}
val brandValue = if (brand != null) {
"${brand::class.java.simpleName}"
} else {
""
null
}
val asasda = listOfNotNull(
componentOrTestName,
brandValue,
extra,
"dark".takeIf { darkTheme }
).joinToString(separator = "_")
return """screenshots/
$asasda"""
.plus(".png").replace("\\s+".toRegex(), "")

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,32 @@ import org.robolectric.annotation.Config
open class ScreenshotsTest {
fun compareScreenshot(
node: SemanticsNodeInteraction,
component: String? = null,
style: String? = null,
brand: Brand? = null,
darkTheme: Boolean = false,
extra: String = "",
extra: String? = null,
) {
node.captureRoboImage(screenshotName(brand, darkTheme, extra))
node.captureRoboImage(screenshotName(component, style, brand, darkTheme, extra))
}

fun compareScreenshot(
node: ViewInteraction,
component: String? = null,
style: String? = null,
brand: Brand? = null,
darkTheme: Boolean = false,
extra: String = "",
extra: String? = null,
) {
node.captureRoboImage(screenshotName(brand, darkTheme, extra))
node.captureRoboImage(screenshotName(component, style, brand, darkTheme, extra))
}

private fun screenshotName(brand: Brand?, darkTheme: Boolean, extra: String) = ScreenshotUtils.getScreenshotName(
brand = brand,
extra = if (darkTheme) "_dark$extra" else extra
)
private fun screenshotName(component: String?, style: String?, brand: Brand?, darkTheme: Boolean, extra: String?) =
ScreenshotUtils.getScreenshotName(
component = component,
style = style,
brand = brand,
darkTheme = darkTheme,
extra = extra
)
}

0 comments on commit a7713f2

Please sign in to comment.