-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement - Add support for content checking and tint color for com…
…pose icon
- Loading branch information
Evgeny Meltsaykin
committed
Dec 6, 2023
1 parent
59331ea
commit 7eb162b
Showing
13 changed files
with
315 additions
and
4 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
compose/src/main/kotlin/io/github/kakaocup/compose/node/assertion/ImageContentAssertions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.github.kakaocup.compose.node.assertion | ||
|
||
import androidx.annotation.DrawableRes | ||
import androidx.compose.ui.graphics.vector.ImageVector | ||
import androidx.compose.ui.semantics.SemanticsPropertyKey | ||
import androidx.compose.ui.semantics.getOrNull | ||
import androidx.compose.ui.test.SemanticsMatcher | ||
import androidx.compose.ui.test.assert | ||
|
||
interface ImageContentAssertions : NodeAssertions { | ||
val imageContentSemanticsPropertyKey: SemanticsPropertyKey<Any> | ||
|
||
/** | ||
* Asserts that the image or icon content contains the given [imageVector]. | ||
* | ||
* Throws [AssertionError] if the image or icon content value is not equal to `imageVector`. | ||
* Throws [IllegalStateException] if the image or icon does not contain the [imageContentSemanticsPropertyKey] modifier. | ||
*/ | ||
fun assertContentEquals(imageVector: ImageVector) { | ||
delegate.check(NodeAssertions.ComposeBaseAssertionType.ASSERT_VALUE_EQUALS) { assert(hasImageContent(imageVector)) } | ||
} | ||
|
||
/** | ||
* Asserts that the image or icon content contains the given [drawableRes]. | ||
* | ||
* Throws [AssertionError] if the image or icon content value is not equal to `drawableRes`. | ||
* Throws [IllegalStateException] if the image or icon does not contain the [imageContentSemanticsPropertyKey] modifier. | ||
*/ | ||
fun assertContentEquals(@DrawableRes drawableRes: Int) { | ||
delegate.check(NodeAssertions.ComposeBaseAssertionType.ASSERT_VALUE_EQUALS) { assert(hasImageContent(drawableRes)) } | ||
} | ||
|
||
private fun hasImageContent(expectedContent: Any): SemanticsMatcher = SemanticsMatcher( | ||
"The content is expected to be $expectedContent, but the actual content is different" | ||
) { node -> | ||
val actual = node.config.getOrNull(imageContentSemanticsPropertyKey) | ||
?: error("Compose view does not contain $imageContentSemanticsPropertyKey modifier") | ||
|
||
return@SemanticsMatcher actual == expectedContent | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
compose/src/main/kotlin/io/github/kakaocup/compose/node/assertion/TintColorAssertions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.github.kakaocup.compose.node.assertion | ||
|
||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.semantics.SemanticsPropertyKey | ||
import androidx.compose.ui.semantics.getOrNull | ||
import androidx.compose.ui.test.SemanticsMatcher | ||
import androidx.compose.ui.test.assert | ||
import io.github.kakaocup.compose.utilities.getComposeColor | ||
|
||
interface TintColorAssertions : NodeAssertions { | ||
val tintColorSemanticsPropertyKey: SemanticsPropertyKey<Color> | ||
|
||
/** | ||
* Asserts that the compose view tint color contains the given [color]. | ||
* | ||
* Throws [AssertionError] if the compose view tint color value is not equal to `color`. | ||
* Throws [IllegalStateException] if the compose view does not contain the [tintColorSemanticsPropertyKey] modifier. | ||
*/ | ||
fun assertTintColorEquals(color: Color) { | ||
delegate.check(NodeAssertions.ComposeBaseAssertionType.ASSERT_VALUE_EQUALS) { assert(hasColor(color)) } | ||
} | ||
|
||
/** | ||
* Asserts that the compose view tint color contains the given [color]. | ||
* | ||
* Throws [AssertionError] if the compose view tint color value is not equal to `color`. | ||
* Throws [IllegalStateException] if the compose view does not contain the [tintColorSemanticsPropertyKey] modifier. | ||
* Throws [IllegalArgumentException] if the color value is incorrect. | ||
*/ | ||
fun assertTintColorEquals(color: String) { | ||
delegate.check(NodeAssertions.ComposeBaseAssertionType.ASSERT_VALUE_EQUALS) { assert(hasColor(color)) } | ||
} | ||
|
||
/** | ||
* Asserts that the compose view tint color contains the given [color]. | ||
* | ||
* Throws [AssertionError] if the compose view tint color value is not equal to `color`. | ||
* Throws [IllegalStateException] if the compose view does not contain the [tintColorSemanticsPropertyKey] modifier. | ||
*/ | ||
fun assertTintColorEquals(color: Long) { | ||
delegate.check(NodeAssertions.ComposeBaseAssertionType.ASSERT_VALUE_EQUALS) { assert(hasColor(color)) } | ||
} | ||
|
||
private fun hasColor(expectedColor: Color): SemanticsMatcher = SemanticsMatcher( | ||
"The color is expected to be $expectedColor, but the actual color is different" | ||
) { node -> | ||
val actualColor = node.config.getOrNull(tintColorSemanticsPropertyKey) | ||
?: error("Compose view does not contain $tintColorSemanticsPropertyKey modifier") | ||
|
||
return@SemanticsMatcher actualColor == expectedColor | ||
} | ||
|
||
private fun hasColor(expectedColor: String): SemanticsMatcher = SemanticsMatcher( | ||
"The color is expected to be $expectedColor, but the actual color is different" | ||
) { node -> | ||
val actualColor = node.config.getOrNull(tintColorSemanticsPropertyKey) | ||
?: error("Compose view does not contain $tintColorSemanticsPropertyKey modifier") | ||
|
||
return@SemanticsMatcher actualColor == getComposeColor(expectedColor) | ||
} | ||
|
||
private fun hasColor(expectedColor: Long): SemanticsMatcher = SemanticsMatcher( | ||
"The color is expected to be $expectedColor, but the actual color is different" | ||
) { node -> | ||
val actualColor = node.config.getOrNull(tintColorSemanticsPropertyKey) | ||
?: error("Compose view does not contain $tintColorSemanticsPropertyKey modifier") | ||
|
||
return@SemanticsMatcher actualColor == Color(expectedColor) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
compose/src/main/kotlin/io/github/kakaocup/compose/node/element/KIconNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.github.kakaocup.compose.node.element | ||
|
||
import androidx.compose.ui.test.SemanticsNodeInteractionsProvider | ||
import io.github.kakaocup.compose.node.builder.NodeMatcher | ||
import io.github.kakaocup.compose.node.core.BaseNode | ||
import io.github.kakaocup.compose.node.assertion.ImageContentAssertions | ||
import io.github.kakaocup.compose.node.assertion.TintColorAssertions | ||
|
||
abstract class KIconNode( | ||
semanticsProvider: SemanticsNodeInteractionsProvider, | ||
nodeMatcher: NodeMatcher, | ||
parentNode: BaseNode<*>?, | ||
useUnmergedTree: Boolean = false | ||
) : BaseNode<KIconNode>( | ||
semanticsProvider = semanticsProvider, | ||
nodeMatcher = nodeMatcher.copy(useUnmergedTree = useUnmergedTree), | ||
parentNode = parentNode | ||
), ImageContentAssertions, TintColorAssertions |
8 changes: 8 additions & 0 deletions
8
compose/src/main/kotlin/io/github/kakaocup/compose/utilities/ColorUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.github.kakaocup.compose.utilities | ||
|
||
import androidx.compose.ui.graphics.Color | ||
|
||
internal fun getComposeColor(color: String): Color { | ||
val normalizeColor = if (color.contains("#")) color else "#$color" | ||
return Color(android.graphics.Color.parseColor(normalizeColor)) | ||
} |
24 changes: 24 additions & 0 deletions
24
sample/src/androidTest/java/io/github/kakaocup/compose/node/KAppIconNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.github.kakaocup.compose.node | ||
|
||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.semantics.SemanticsPropertyKey | ||
import androidx.compose.ui.test.SemanticsNodeInteractionsProvider | ||
import io.github.kakaocup.compose.node.builder.NodeMatcher | ||
import io.github.kakaocup.compose.node.core.BaseNode | ||
import io.github.kakaocup.compose.node.element.KIconNode | ||
import io.github.kakaocup.compose.sample.semantics.ImageContentSemanticKey | ||
import io.github.kakaocup.compose.sample.semantics.TintColorSemanticKey | ||
|
||
class KAppIconNode( | ||
semanticsProvider: SemanticsNodeInteractionsProvider, | ||
nodeMatcher: NodeMatcher, | ||
parentNode: BaseNode<*>? = null, | ||
) : KIconNode( | ||
semanticsProvider = semanticsProvider, | ||
nodeMatcher = nodeMatcher, | ||
parentNode = parentNode, | ||
useUnmergedTree = true | ||
) { | ||
override val imageContentSemanticsPropertyKey: SemanticsPropertyKey<Any> = ImageContentSemanticKey | ||
override val tintColorSemanticsPropertyKey: SemanticsPropertyKey<Color> = TintColorSemanticKey | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
sample/src/main/java/io/github/kakaocup/compose/sample/semantics/Color.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.github.kakaocup.compose.sample.semantics | ||
|
||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.semantics.SemanticsPropertyKey | ||
import androidx.compose.ui.semantics.SemanticsPropertyReceiver | ||
import androidx.compose.ui.semantics.semantics | ||
|
||
val TintColorSemanticKey = SemanticsPropertyKey<Color>("TintColor") | ||
var SemanticsPropertyReceiver.tintColor by TintColorSemanticKey | ||
fun Modifier.tintColorSemantic(color: Color): Modifier { | ||
return semantics { tintColor = color } | ||
} |
12 changes: 12 additions & 0 deletions
12
sample/src/main/java/io/github/kakaocup/compose/sample/semantics/Image.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.github.kakaocup.compose.sample.semantics | ||
|
||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.semantics.SemanticsPropertyKey | ||
import androidx.compose.ui.semantics.SemanticsPropertyReceiver | ||
import androidx.compose.ui.semantics.semantics | ||
|
||
val ImageContentSemanticKey = SemanticsPropertyKey<Any>("ImageContent") | ||
var SemanticsPropertyReceiver.imageContent by ImageContentSemanticKey | ||
fun Modifier.imageContentSemantic(imageContent: Any): Modifier { | ||
return semantics { this.imageContent = imageContent } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:height="24dp" android:tint="#000000" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M5,16c0,3.87 3.13,7 7,7s7,-3.13 7,-7v-4L5,12v4zM16.12,4.37l2.1,-2.1 -0.82,-0.83 -2.3,2.31C14.16,3.28 13.12,3 12,3s-2.16,0.28 -3.09,0.75L6.6,1.44l-0.82,0.83 2.1,2.1C6.14,5.64 5,7.68 5,10v1h14v-1c0,-2.32 -1.14,-4.36 -2.88,-5.63zM9,9c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1 1,0.45 1,1 -0.45,1 -1,1zM15,9c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1 1,0.45 1,1 -0.45,1 -1,1z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:height="24dp" android:tint="#000000" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="#FF000000" android:pathData="M17.6,11.48 L19.44,8.3a0.63,0.63 0,0 0,-1.09 -0.63l-1.88,3.24a11.43,11.43 0,0 0,-8.94 0L5.65,7.67a0.63,0.63 0,0 0,-1.09 0.63L6.4,11.48A10.81,10.81 0,0 0,1 20L23,20A10.81,10.81 0,0 0,17.6 11.48ZM7,17.25A1.25,1.25 0,1 1,8.25 16,1.25 1.25,0 0,1 7,17.25ZM17,17.25A1.25,1.25 0,1 1,18.25 16,1.25 1.25,0 0,1 17,17.25Z"/> | ||
</vector> |
Oops, something went wrong.