Add commonUI test
Some checks failed
SonarQube Scan / SonarQube Trigger (push) Failing after 7m17s

This commit is contained in:
2025-10-12 23:51:12 +02:00
parent 054d229b97
commit efac02216e
10 changed files with 104 additions and 48 deletions

View File

@@ -1,3 +1,6 @@
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetTree
plugins { plugins {
jacoco jacoco
alias(libs.plugins.kotlin.multiplatform) alias(libs.plugins.kotlin.multiplatform)
@@ -10,11 +13,13 @@ kotlin {
jvmToolchain(libs.versions.jdk.get().toInt()) jvmToolchain(libs.versions.jdk.get().toInt())
jvm() jvm()
androidTarget() androidTarget {
@OptIn(ExperimentalKotlinGradlePluginApi::class)
instrumentedTestVariant.sourceSetTree.set(KotlinSourceSetTree.test)
}
sourceSets { sourceSets {
commonMain { commonMain.dependencies {
dependencies {
api(projects.domain) api(projects.domain)
implementation(compose.components.resources) implementation(compose.components.resources)
@@ -27,19 +32,17 @@ kotlin {
implementation(libs.bundles.logging) implementation(libs.bundles.logging)
} }
}
androidMain { commonTest.dependencies {
dependencies {
implementation(libs.androidx.compose.foundation)
}
}
commonTest {
dependencies {
implementation(libs.kotlin.test) implementation(libs.kotlin.test)
implementation(libs.atrium) implementation(libs.atrium)
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.uiTest)
} }
jvmTest.dependencies {
implementation(compose.desktop.currentOs)
} }
} }
} }
@@ -48,26 +51,28 @@ android {
namespace = "ch.dissem.yaep.common.ui" namespace = "ch.dissem.yaep.common.ui"
compileSdk = libs.versions.android.compileSdk.get().toInt() compileSdk = libs.versions.android.compileSdk.get().toInt()
dependencies {
implementation(libs.androidx.compose.foundation)
debugImplementation(libs.androidx.compose.ui.test.manifest)
debugImplementation(libs.androidx.compose.ui.test.android)
}
defaultConfig { defaultConfig {
minSdk = libs.versions.android.minSdk.get().toInt() minSdk = libs.versions.android.minSdk.get().toInt()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
} }
buildFeatures { buildFeatures {
compose = true compose = true
} }
sourceSets {
getByName("test") {
resources.srcDirs("src/commonTest/resources")
}
}
buildTypes { buildTypes {
debug { debug {
enableAndroidTestCoverage = true enableAndroidTestCoverage = true
enableUnitTestCoverage = true
} }
} }
testOptions.unitTests.isIncludeAndroidResources = true
} }
compose.resources { compose.resources {

View File

@@ -0,0 +1,5 @@
package ch.dissem.yaep.ui.common
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
@Target(allowedTargets = [AnnotationTarget.CLASS, AnnotationTarget.FUNCTION])
actual annotation class IgnoreAndroidUnitTest actual constructor()

View File

@@ -0,0 +1,4 @@
package ch.dissem.yaep.ui.common
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
actual typealias IgnoreAndroidUnitTest = org.junit.Ignore

View File

@@ -19,6 +19,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.BlurEffect import androidx.compose.ui.graphics.BlurEffect
import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
@@ -42,7 +43,8 @@ fun EndOfGame(
exit = fadeOut() exit = fadeOut()
) { ) {
val background = MaterialTheme.colorScheme.background val background = MaterialTheme.colorScheme.background
Box(modifier = Modifier Box(
modifier = Modifier
.fillMaxSize() .fillMaxSize()
.drawBehind { .drawBehind {
drawRect(color = background.copy(alpha = 0.8f)) drawRect(color = background.copy(alpha = 0.8f))
@@ -57,6 +59,7 @@ fun EndOfGame(
fontSize = 128.sp, fontSize = 128.sp,
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
.testTag("EndOfGame.solved_congrats")
) )
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
Text( Text(
@@ -64,10 +67,12 @@ fun EndOfGame(
fontSize = 64.sp, fontSize = 64.sp,
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
.testTag("EndOfGame.solved_time")
) )
Spacer(modifier = Modifier.height(32.dp)) Spacer(modifier = Modifier.height(32.dp))
Button( Button(
modifier = Modifier.align(CenterHorizontally), modifier = Modifier.align(CenterHorizontally)
.testTag("EndOfGame.restart"),
onClick = onRestart onClick = onRestart
) { ) {
Text(stringResource(Res.string.restart)) Text(stringResource(Res.string.restart))

View File

@@ -1,10 +0,0 @@
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.verbs.expect
import kotlin.test.Test
class DummyTest {
@Test
fun `ensure there is a test`() {
expect(true).toEqual(true)
}
}

View File

@@ -0,0 +1,33 @@
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.runComposeUiTest
import ch.dissem.yaep.ui.common.EndOfGame
import ch.dissem.yaep.ui.common.IgnoreAndroidUnitTest
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.verbs.expect
import kotlin.test.Test
@IgnoreAndroidUnitTest
@OptIn(ExperimentalTestApi::class)
class EndOfGameTest {
@Test
fun `ensure end of game works correctly`() = runComposeUiTest {
var restart = false
setContent {
EndOfGame(
isSolved = true,
time = "42:42",
onRestart = { restart = true }
)
}
onNodeWithTag("EndOfGame.solved_time").assertTextContains(value = "42:42", substring = true)
onNodeWithTag("EndOfGame.restart").performClick()
expect(restart).toEqual(true)
}
}

View File

@@ -0,0 +1,5 @@
package ch.dissem.yaep.ui.common
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
expect annotation class IgnoreAndroidUnitTest()

View File

@@ -0,0 +1,5 @@
package ch.dissem.yaep.ui.common
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
@Target(allowedTargets = [AnnotationTarget.CLASS, AnnotationTarget.FUNCTION])
actual annotation class IgnoreAndroidUnitTest actual constructor()

View File

@@ -37,7 +37,7 @@ class GameTest {
log.info { "Puzzle:\n$game" } log.info { "Puzzle:\n$game" }
} }
expect(solvable).toEqual(SOLVABLE) expect(solvable).toEqual(SOLVABLE)
expect(time).toBeLessThan(500.milliseconds) expect(time).toBeLessThan(1000.milliseconds)
if (time < fastest) { if (time < fastest) {
fastest = time fastest = time
} }

View File

@@ -15,13 +15,17 @@ atrium = "1.2.0"
[libraries] [libraries]
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" } androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" }
androidx-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "androidx-compose" } androidx-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "androidx-compose" }
androidx-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "androidx-compose" } androidx-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "androidx-compose" }
androidx-compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "androidx-compose" } androidx-compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "androidx-compose" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } androidx-compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest", version.ref = "androidx-compose" }
androidx-compose-ui-test-android = { module = "androidx.compose.ui:ui-test-junit4-android", version.ref = "androidx-compose" }
atrium = { module = "ch.tutteli.atrium:atrium-fluent", version.ref = "atrium" } atrium = { module = "ch.tutteli.atrium:atrium-fluent", version.ref = "atrium" }
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" }
kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version = "1.10.2" } kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
logging-jvm = { module = "io.github.oshai:kotlin-logging-jvm", version = "7.0.13" } logging-jvm = { module = "io.github.oshai:kotlin-logging-jvm", version = "7.0.13" }
logging-slf4j = { module = "org.slf4j:slf4j-simple", version = "2.0.17" } logging-slf4j = { module = "org.slf4j:slf4j-simple", version = "2.0.17" }