|
| 1 | +package co.yml.ycoreui.ui.ytag |
| 2 | + |
| 3 | +import android.R |
| 4 | +import androidx.compose.foundation.layout.PaddingValues |
| 5 | +import androidx.compose.foundation.shape.CircleShape |
| 6 | +import androidx.compose.material3.Icon |
| 7 | +import androidx.compose.material3.IconButton |
| 8 | +import androidx.compose.runtime.Composable |
| 9 | +import androidx.compose.ui.Modifier |
| 10 | +import androidx.compose.ui.graphics.Color |
| 11 | +import androidx.compose.ui.platform.testTag |
| 12 | +import androidx.compose.ui.res.painterResource |
| 13 | +import androidx.compose.ui.test.* |
| 14 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 15 | +import androidx.compose.ui.text.TextStyle |
| 16 | +import androidx.compose.ui.text.font.FontFamily |
| 17 | +import androidx.compose.ui.text.font.FontStyle |
| 18 | +import androidx.compose.ui.text.font.FontWeight |
| 19 | +import androidx.compose.ui.text.style.TextAlign |
| 20 | +import androidx.compose.ui.text.style.TextDecoration |
| 21 | +import androidx.compose.ui.text.style.TextOverflow |
| 22 | +import androidx.compose.ui.unit.dp |
| 23 | +import androidx.compose.ui.unit.sp |
| 24 | +import co.yml.ycoreui.ui.ytag.model.TagViewModifiers |
| 25 | +import org.junit.Rule |
| 26 | +import org.junit.Test |
| 27 | + |
| 28 | +class TagViewTest { |
| 29 | + @get:Rule |
| 30 | + val composeTestRule = createComposeRule() |
| 31 | + |
| 32 | + private fun launchYTag( |
| 33 | + text: String, |
| 34 | + leadingIcon: @Composable ((Boolean) -> Unit)? = null, |
| 35 | + trailingIcon: @Composable ((Boolean) -> Unit)? = null, |
| 36 | + tagViewModifiers: TagViewModifiers = TagViewModifiers.Builder().build(), |
| 37 | + enabled: Boolean = true |
| 38 | + ) { |
| 39 | + composeTestRule.setContent { |
| 40 | + TagView( |
| 41 | + text = text, |
| 42 | + leadingIcon = leadingIcon, |
| 43 | + trailingIcon = trailingIcon, |
| 44 | + tagViewModifiers = tagViewModifiers, |
| 45 | + enabled = enabled |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun tagView_shown() { |
| 52 | + |
| 53 | + launchYTag(text = "YTag") |
| 54 | + composeTestRule.onNodeWithTag("tag_view", useUnmergedTree = true).printToString() |
| 55 | + |
| 56 | + composeTestRule.onNodeWithTag("tag_view").assertIsDisplayed() |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun tagView_text_shown() { |
| 61 | + launchYTag(text = "YTag") |
| 62 | + |
| 63 | + composeTestRule.onNodeWithText("YTag").assertIsDisplayed() |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun tagView_leading_icon_shown() { |
| 68 | + launchYTag(text = "YTag", leadingIcon = { |
| 69 | + IconButton(onClick = {}, modifier = Modifier.testTag("leading_icon")) { |
| 70 | + Icon( |
| 71 | + painter = painterResource(id = R.drawable.ic_menu_mylocation), |
| 72 | + contentDescription = null |
| 73 | + ) |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + composeTestRule.onNodeWithTag("leading_icon", useUnmergedTree = true).assertIsDisplayed() |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun tagView_trailing_icon_shown() { |
| 82 | + launchYTag(text = "YTag", trailingIcon = { |
| 83 | + IconButton(onClick = {}, modifier = Modifier.testTag("trailing_icon")) { |
| 84 | + Icon( |
| 85 | + painter = painterResource(id = R.drawable.ic_menu_mylocation), |
| 86 | + contentDescription = null |
| 87 | + ) |
| 88 | + } |
| 89 | + }) |
| 90 | + |
| 91 | + composeTestRule.onNodeWithTag("trailing_icon", useUnmergedTree = true).assertIsDisplayed() |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun tagView_leading_and_trailing_icon_shown() { |
| 96 | + |
| 97 | + launchYTag(text = "YTag", leadingIcon = { |
| 98 | + IconButton(onClick = {}, modifier = Modifier.testTag("leading_icon")) { |
| 99 | + Icon( |
| 100 | + painter = painterResource(id = R.drawable.ic_menu_mylocation), |
| 101 | + contentDescription = null |
| 102 | + ) |
| 103 | + } |
| 104 | + }, trailingIcon = { |
| 105 | + IconButton(onClick = {}, modifier = Modifier.testTag("trailing_icon")) { |
| 106 | + Icon( |
| 107 | + painter = painterResource(id = R.drawable.ic_menu_mylocation), |
| 108 | + contentDescription = null |
| 109 | + ) |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + composeTestRule.onNodeWithTag("leading_icon", useUnmergedTree = true).assertIsDisplayed() |
| 114 | + composeTestRule.onNodeWithTag("trailing_icon", useUnmergedTree = true).assertIsDisplayed() |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + fun tag_with_modifiers_are_executed(){ |
| 119 | + val tagViewModifiers = TagViewModifiers.Builder() |
| 120 | + .minWidth(32.dp) |
| 121 | + .minHeight(100.dp) |
| 122 | + .width(50.dp) |
| 123 | + .height(50.dp) |
| 124 | + .textColor(Color.Black) |
| 125 | + .fontSize(14.sp) |
| 126 | + .fontFamily(FontFamily.SansSerif) |
| 127 | + .fontStyle(FontStyle.Normal) |
| 128 | + .fontWeight(FontWeight.Normal) |
| 129 | + .letterSpacing(0.05.sp) |
| 130 | + .textDecoration(TextDecoration.None) |
| 131 | + .textAlign(TextAlign.Center) |
| 132 | + .lineHeight(24.sp) |
| 133 | + .overFlow(TextOverflow.Ellipsis) |
| 134 | + .softWrap(true) |
| 135 | + .maxLines(1) |
| 136 | + .onTextLayout { } |
| 137 | + .style(TextStyle()) |
| 138 | + .enableBorder(true) |
| 139 | + .borderColor(Color.Red) |
| 140 | + .borderWidth(1.dp) |
| 141 | + .backgroundColor(Color.White) |
| 142 | + .textPadding(PaddingValues(4.dp)) |
| 143 | + .shape(CircleShape) |
| 144 | + .tonalElevation(2.dp) |
| 145 | + .shadowElevation(2.dp) |
| 146 | + .containerPaddingValues(PaddingValues(4.dp)) |
| 147 | + .onCLick { } |
| 148 | + .borderWidth(1.dp) |
| 149 | + .build() |
| 150 | + |
| 151 | + launchYTag(text = "YTag", tagViewModifiers = tagViewModifiers) |
| 152 | + |
| 153 | + composeTestRule.onNodeWithText("YTag") |
| 154 | + .assertIsDisplayed() |
| 155 | + } |
| 156 | +} |
0 commit comments