Skip to content

Commit

Permalink
POC roborazzi split ButtonKtTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jeprubio committed Sep 28, 2023
1 parent da6d038 commit 8ef2191
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.telefonica.mistica.compose.button

import androidx.compose.foundation.layout.padding
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.unit.dp
import com.telefonica.mistica.compose.theme.MisticaTheme
import com.telefonica.mistica.compose.theme.brand.MovistarBrand
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
internal class ButtonBehaviourTest {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun `check the button is clicked`() = test {
`given Button`()

`when the button is clicked`()

`then the onClickListener has been invoked`()
}

private fun TestScope.`given Button`() {
`when Button`()
}

private fun TestScope.`when Button`() {
composeTestRule.setContent {
MisticaTheme(brand = MovistarBrand) {
Button(
text = textValue,
onClickListener = onClickListener,
modifier = Modifier.padding(16.dp)
)
}
}
}

private fun TestScope.`when the button is clicked`() {
composeTestRule.onNodeWithText(textValue).performClick()
}

private fun TestScope.`then the onClickListener has been invoked`() {
assertTrue(clicked)
}

private fun test(block: TestScope.() -> Unit) {
TestScope().block()
}

private class TestScope {
val textValue = "textValue"
var clicked = false
val onClickListener: () -> Unit = { clicked = true }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package com.telefonica.mistica.compose.button
import androidx.compose.foundation.layout.padding
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.test.performClick
import androidx.compose.ui.unit.dp
import com.github.takahirom.roborazzi.RobolectricDeviceQualifiers
import com.github.takahirom.roborazzi.captureRoboImage
Expand All @@ -17,107 +15,51 @@ 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 org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.experimental.runners.Enclosed
import org.junit.runner.RunWith
import org.robolectric.ParameterizedRobolectricTestRunner
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(Enclosed::class)
internal class ButtonKtTest {
@RunWith(ParameterizedRobolectricTestRunner::class)
@Config(qualifiers = RobolectricDeviceQualifiers.Pixel5)
internal class ButtonKtTest(private val brand: Brand) {
@get:Rule
val composeTestRule = createComposeRule()

@RunWith(ParameterizedRobolectricTestRunner::class)
@Config(qualifiers = RobolectricDeviceQualifiers.Pixel5)
internal class ButtonKtTestParametrized(private val brand: Brand) {
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun `check the button screenshot`() {
`when Button`(brand)

@Test
fun `check the button screenshot`() {
`when Button`(brand)

`then screenshot is OK`(brand)
}

private fun `when Button`(brand: Brand = MovistarBrand) {
composeTestRule.setContent {
MisticaTheme(brand = brand) {
Button(
text = "textValue",
onClickListener = { },
modifier = Modifier.padding(16.dp)
)
}
}
}

private fun `then screenshot is OK`(brand: Brand) {
composeTestRule.onRoot()
.captureRoboImage(ScreenshotUtils.getScreenshotName(brand = brand))
}

companion object {
@JvmStatic
@ParameterizedRobolectricTestRunner.Parameters(name = "Input: {0}")
fun brands() = listOf(
arrayOf(MovistarBrand),
arrayOf(VivoBrand),
arrayOf(O2Brand),
arrayOf(BlauBrand),
arrayOf(TelefonicaBrand),
)
}
`then screenshot is OK`(brand)
}

@RunWith(RobolectricTestRunner::class)
internal class ButtonKtTestNotParametrized {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun `check the button is clicked`() = test {
`given Button`()

`when the button is clicked`()

`then the onClickListener has been invoked`()
}

private fun TestScope.`given Button`() {
`when Button`()
}

private fun TestScope.`when Button`() {
composeTestRule.setContent {
MisticaTheme(brand = MovistarBrand) {
Button(
text = textValue,
onClickListener = onClickListener,
modifier = Modifier.padding(16.dp)
)
}
private fun `when Button`(brand: Brand = MovistarBrand) {
composeTestRule.setContent {
MisticaTheme(brand = brand) {
Button(
text = "textValue",
onClickListener = { },
modifier = Modifier.padding(16.dp)
)
}
}
}

private fun TestScope.`when the button is clicked`() {
composeTestRule.onNodeWithText(textValue).performClick()
}

private fun TestScope.`then the onClickListener has been invoked`() {
assertTrue(clicked)
}

private fun test(block: TestScope.() -> Unit) {
TestScope().block()
}
private fun `then screenshot is OK`(brand: Brand) {
composeTestRule.onRoot()
.captureRoboImage(ScreenshotUtils.getScreenshotName(brand = brand))
}

private class TestScope {
val textValue = "textValue"
var clicked = false
val onClickListener: () -> Unit = { clicked = true }
}
companion object {
@JvmStatic
@ParameterizedRobolectricTestRunner.Parameters(name = "Input: {0}")
fun brands() = listOf(
arrayOf(MovistarBrand),
arrayOf(VivoBrand),
arrayOf(O2Brand),
arrayOf(BlauBrand),
arrayOf(TelefonicaBrand),
)
}
}
2 changes: 1 addition & 1 deletion screenshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ or
@RunWith(ParameterizedRobolectricTestRunner::class)
depending if it's a parametrized test or not. Use
@RunWith(Enclosed::class)
in case multiple of them must be on the same file (`ButtonKtTest.kt` is an example of this).
in case multiple of them must be on the same file.

Then use `captureRoboImage()` function to take the screenshot and use `ScreenshotUtils.getScreenshotName()` to set the name of the test.:

Expand Down

0 comments on commit 8ef2191

Please sign in to comment.