Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI test for read-only tag #25

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.gabrieldrn.carbon.Carbon
Expand Down Expand Up @@ -97,13 +98,15 @@ public fun ReadOnlyTag(
TagIcon(
icon = icon,
color = contentColor,
modifier = Modifier.padding(
horizontal = when (size) {
TagSize.Small -> SpacingScale.spacing02
TagSize.Medium -> SpacingScale.spacing02
TagSize.Large -> SpacingScale.spacing03
}
)
modifier = Modifier
.padding(
horizontal = when (size) {
TagSize.Small -> SpacingScale.spacing02
TagSize.Medium -> SpacingScale.spacing02
TagSize.Large -> SpacingScale.spacing03
}
)
.testTag(TagTestTags.ICON)
)

Text(
Expand All @@ -112,23 +115,27 @@ public fun ReadOnlyTag(
color = contentColor,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = if (icon == null) {
Modifier.padding(
horizontal = when (size) {
TagSize.Small -> SpacingScale.spacing03
TagSize.Medium -> SpacingScale.spacing03
TagSize.Large -> SpacingScale.spacing04
}
)
} else {
Modifier.padding(
end = when (size) {
TagSize.Small -> SpacingScale.spacing03
TagSize.Medium -> SpacingScale.spacing03
TagSize.Large -> SpacingScale.spacing04
modifier = Modifier
.testTag(TagTestTags.LABEL)
.then(
if (icon == null) {
Modifier.padding(
horizontal = when (size) {
TagSize.Small -> SpacingScale.spacing03
TagSize.Medium -> SpacingScale.spacing03
TagSize.Large -> SpacingScale.spacing04
}
)
} else {
Modifier.padding(
end = when (size) {
TagSize.Small -> SpacingScale.spacing03
TagSize.Medium -> SpacingScale.spacing03
TagSize.Large -> SpacingScale.spacing04
}
)
}
)
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 Gabriel Derrien
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.gabrieldrn.carbon.tag

internal object TagTestTags {
const val LABEL = "carbon_tag_label"
const val ICON = "carbon_tag_icon"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2024 Gabriel Derrien
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.gabrieldrn.carbon.tag

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.ColorPainter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.assertHeightIsEqualTo
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.runComposeUiTest
import com.gabrieldrn.carbon.CarbonDesignSystem
import kotlin.test.Test

class ReadOnlyTagTest {

private var _text by mutableStateOf("")
private var _icon by mutableStateOf<(@Composable () -> Painter)?>(null)
private var _type by mutableStateOf(TagType.Gray)
private var _size by mutableStateOf(TagSize.Medium)

private val rootTag = "root"

@Test
fun readOnlyTag_validateLayout() = runComposeUiTest {
setContent {
CarbonDesignSystem {
ReadOnlyTag(
text = _text,
icon = _icon,
type = _type,
size = _size,
modifier = Modifier.testTag(rootTag)
)
}
}

forEachParameter { text, painter, tagType, tagSize ->
_text = text
_icon = painter
_type = tagType
_size = tagSize

onNodeWithTag(rootTag)
.assertHeightIsEqualTo(tagSize.height)

onNodeWithTag(TagTestTags.LABEL, useUnmergedTree = false)
.assertIsDisplayed()

onNodeWithTag(TagTestTags.ICON, useUnmergedTree = false).run {
if (painter != null) {
assertIsDisplayed()
} else {
assertDoesNotExist()
}
}
}
}

@Suppress("NestedBlockDepth")
private fun forEachParameter(
testBlock: (String, (@Composable () -> Painter)?, TagType, TagSize) -> Unit
) {
val painterLambda = @Composable { ColorPainter(Color.Black) }

arrayOf("", "Tag").forEach { text ->
arrayOf(null, painterLambda).forEach { painter ->
TagType.entries.forEach { type ->
TagSize.entries.forEach { size ->
testBlock(text, painter, type, size)
}
}
}
}
}
}