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

Add an example compose ui test #17

Merged
merged 2 commits into from
Mar 17, 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
5 changes: 4 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ circuit = "0.19.1"
androidx-activity-compose = "1.8.2"
androidx-appcompat = "1.6.1"
androidx-core-ktx = "1.12.0"
datastore-version = "1.1.0-beta01"
datastore-version = "1.1.0-beta02"
compose-ui-test = "1.6.3"

[libraries]
androidx-core-splashscreen = { module = "androidx.core:core-splashscreen", version.ref = "core-splashscreen" }
Expand All @@ -23,6 +24,8 @@ androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "a
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidx-core-ktx" }
androidx-datastore-core-okio = { group = "androidx.datastore", name = "datastore-core-okio", version.ref = "datastore-version" }
androidx-datastore-preferences-core = { group = "androidx.datastore", name = "datastore-preferences-core", version.ref = "datastore-version" }
androidx-compose-ui-test-junit4-android = { group = "androidx.compose.ui" , name = "ui-test-junit4-android", version.ref = "compose-ui-test" }
androidx-compose-ui-test-manifest = { group = "androidx.compose.ui" , name = "ui-test-manifest", version.ref = "compose-ui-test" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Expand Down
20 changes: 19 additions & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetTree

plugins {
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.android.library)
Expand All @@ -8,7 +11,19 @@ plugins {
}

kotlin {
androidTarget()
androidTarget {
@OptIn(ExperimentalKotlinGradlePluginApi::class)
instrumentedTestVariant {
sourceSetTree.set(KotlinSourceSetTree.test)

dependencies {
implementation(libs.androidx.compose.ui.test.junit4.android)
debugImplementation(libs.androidx.compose.ui.test.manifest)
}
}
@OptIn(ExperimentalKotlinGradlePluginApi::class)
unitTestVariant.sourceSetTree.set(KotlinSourceSetTree.unitTest)
}
iosX64()
iosArm64()
iosSimulatorArm64()
Expand Down Expand Up @@ -43,6 +58,8 @@ kotlin {
commonTest {
dependencies {
implementation(libs.kotlin.test)
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.uiTest)
}
}
androidMain {
Expand All @@ -65,6 +82,7 @@ android {
defaultConfig {
minSdk = (findProperty("android.minSdk") as String).toInt()
lint.targetSdk = (findProperty("android.targetSdk") as String).toInt()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package xyz.alaniz.aaron.lightsaber.ui

import androidx.compose.ui.test.SemanticsNodeInteractionsProvider
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.hasContentDescription
import androidx.compose.ui.test.onNodeWithContentDescription

fun SemanticsNodeInteractionsProvider.lightsaber(block: LightsaberRobot.() -> Unit) =
LightsaberRobot(nodeInteractionsProvider = this).apply { block() }

class LightsaberRobot(private val nodeInteractionsProvider: SemanticsNodeInteractionsProvider) {

fun seeHandle() = apply {
nodeInteractionsProvider.onNodeWithContentDescription(label = "lightsaber handle")
.assertIsDisplayed()
.assertIsEnabled()
}

fun seeBlade() = apply {
nodeInteractionsProvider.onNodeWithContentDescription(label = "lightsaber blade")
.assertIsDisplayed()
}

fun doNotSeeBlade() = apply {
nodeInteractionsProvider.onNode(hasContentDescription(value = "lightsaber blade")).assertDoesNotExist()
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package xyz.alaniz.aaron.lightsaber.ui

import androidx.compose.ui.test.ComposeUiTest
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.runComposeUiTest
import xyz.alaniz.aaron.lightsaber.ui.common.LightsaberGreen
import xyz.alaniz.aaron.lightsaber.ui.lightsaber.BladeState
import xyz.alaniz.aaron.lightsaber.ui.lightsaber.Lightsaber
import xyz.alaniz.aaron.lightsaber.ui.lightsaber.LightsaberState
import kotlin.test.Test

@OptIn(ExperimentalTestApi::class)
class LightsaberUiTest {
private val greenDeactivatedLightsaberState = LightsaberState(
bladeState = BladeState.Deactivated,
bladeColor = LightsaberGreen,
onEvent = {}
)
private val greenActivatedLightsaberState = LightsaberState(
bladeState = BladeState.Activated,
bladeColor = LightsaberGreen,
onEvent = {}
)

@Test
fun `given a deactivated lightsaber when the lightsaber is shown then the blade should not be visible`() =
runLightsaberUiTest(initialState = greenDeactivatedLightsaberState) {
lightsaber {
seeHandle()
doNotSeeBlade()
}
}

@Test
fun `given an activated lightsaber when the lightsaber is shown then the blade should be visible`() =
runLightsaberUiTest(initialState = greenActivatedLightsaberState) {
lightsaber {
seeHandle()
seeBlade()
}
}

private fun runLightsaberUiTest(initialState: LightsaberState, testBody: ComposeUiTest.() -> Unit) = runComposeUiTest {
setContent { Lightsaber(lightsaberState = initialState) }
testBody()
}
}
Loading