How to write compose UI test to verify background color of a button

347 Views Asked by At

I'm new into JetPackCompose and trying to test the background color of a button but I don't get the expected result. I'm asserting that the button's color with the expected color as:

assertDisplayed("Bags:")
assertDisplayedWithTag(Constants.TAG).equals(BagsBackground)
Assert.assertEquals(BagsBackground, MyColor.Gray)

Below mentioned the my composable function of the button:

 @Composable
fun RoundedMenuButton(
    title: String,
    modifier: Modifier = Modifier,
    subTitle: String,
    showSubTitle: Boolean,
    titleFontSize: TextUnit = 24.sp,
    enabled: Boolean = true,
    isBag: Boolean,
    hasBags: Boolean,
    onClick: () -> Unit
) {
    val menuButtonColors = ButtonDefaults.buttonColors(
        backgroundColor = if (isBag) {
            BagsBackground
        } else MenuButtonBackground
    )
    val defaultSizeModifier = modifier
        .height(60.dp)
        .width(155.dp)

    Button(
        onClick = onClick,
        colors = menuButtonColors,
        border = BorderStroke(1.dp, Color.Black),
        modifier = defaultSizeModifier,
        shape = RectangleShape,
        enabled = enabled

    ) {
        Column(
            verticalArrangement = Arrangement.SpaceBetween,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {

            Text(
                text = title,
                textAlign = TextAlign.Center,
                fontWeight = FontWeight.Bold,
                fontSize = titleFontSize,
                color = if (isBag) Color.White else Color.Black
            )
            if (showSubTitle) {
                Text(
                    text = subTitle,
                    textAlign = TextAlign.Center,
                    fontWeight = FontWeight.Bold,
                    fontSize = 18.sp,
                    color = if (isBag) Color.White else Color.Black
                )
            }
        }
    }
}
0

There are 0 best solutions below