Skip to content

Commit

Permalink
Update to Compose 1.7 + implement missing APIs for ComposeContext (#354)
Browse files Browse the repository at this point in the history
* Update to Compose BOM

* Add a unit test that verifies complete coverage of ComposeTestRule API for the JUnit 5 version

* Add missing API methods from JUnit 4's ComposeTestRule to JUnit 5's ComposeContext

* Update instrumentation/compose/src/main/java/de/mannodermaus/junit5/compose/ComposeContext.kt

* Update instrumentation/compose/src/main/java/de/mannodermaus/junit5/compose/ComposeContext.kt

* Update instrumentation/compose/src/test/java/de/mannodermaus/junit5/compose/ComposeContextTests.kt
  • Loading branch information
mannodermaus authored Sep 6, 2024
1 parent 6a874bc commit 0a5c122
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 46 deletions.
2 changes: 1 addition & 1 deletion build-logic/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object libs {
const val junitVintage = "5.11.0"
const val junitPlatform = "1.11.0"

const val composeBom = "2024.08.00"
const val composeBom = "2024.09.00"
const val androidXTestAnnotation = "1.0.1"
const val androidXTestCore = "1.6.1"
const val androidXTestMonitor = "1.7.2"
Expand Down
2 changes: 1 addition & 1 deletion build-logic/src/main/kotlin/Environment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ object Artifacts {
*/
object Instrumentation {
const val groupId = "de.mannodermaus.junit5"
private const val currentVersion = "1.5.1-SNAPSHOT"
private const val currentVersion = "1.6.0-SNAPSHOT"
private const val latestStableVersion = "1.5.0"

val Core = Deployed(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions instrumentation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Change Log
## Unreleased

- Use square brackets for parameterized tests to ensure that their logs show correctly in the IDE (#350)
- Add missing API methods from JUnit 4's ComposeTestRule to JUnit 5's ComposeContext (#353)

## 1.5.0 (2024-07-25)

Expand Down
14 changes: 14 additions & 0 deletions instrumentation/compose/api/compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ public abstract interface class de/mannodermaus/junit5/compose/ComposeContext :
public abstract fun unregisterIdlingResource (Landroidx/compose/ui/test/IdlingResource;)V
public abstract fun waitForIdle ()V
public abstract fun waitUntil (JLkotlin/jvm/functions/Function0;)V
public abstract fun waitUntil (Ljava/lang/String;JLkotlin/jvm/functions/Function0;)V
public abstract fun waitUntilAtLeastOneExists (Landroidx/compose/ui/test/SemanticsMatcher;J)V
public abstract fun waitUntilDoesNotExist (Landroidx/compose/ui/test/SemanticsMatcher;J)V
public abstract fun waitUntilExactlyOneExists (Landroidx/compose/ui/test/SemanticsMatcher;J)V
public abstract fun waitUntilNodeCount (Landroidx/compose/ui/test/SemanticsMatcher;IJ)V
}

public final class de/mannodermaus/junit5/compose/ComposeContext$DefaultImpls {
public static synthetic fun waitUntil$default (Lde/mannodermaus/junit5/compose/ComposeContext;JLkotlin/jvm/functions/Function0;ILjava/lang/Object;)V
public static synthetic fun waitUntil$default (Lde/mannodermaus/junit5/compose/ComposeContext;Ljava/lang/String;JLkotlin/jvm/functions/Function0;ILjava/lang/Object;)V
public static synthetic fun waitUntilAtLeastOneExists$default (Lde/mannodermaus/junit5/compose/ComposeContext;Landroidx/compose/ui/test/SemanticsMatcher;JILjava/lang/Object;)V
public static synthetic fun waitUntilDoesNotExist$default (Lde/mannodermaus/junit5/compose/ComposeContext;Landroidx/compose/ui/test/SemanticsMatcher;JILjava/lang/Object;)V
public static synthetic fun waitUntilExactlyOneExists$default (Lde/mannodermaus/junit5/compose/ComposeContext;Landroidx/compose/ui/test/SemanticsMatcher;JILjava/lang/Object;)V
public static synthetic fun waitUntilNodeCount$default (Lde/mannodermaus/junit5/compose/ComposeContext;Landroidx/compose/ui/test/SemanticsMatcher;IJILjava/lang/Object;)V
}

public abstract interface class de/mannodermaus/junit5/compose/ComposeExtension : org/junit/jupiter/api/extension/Extension {
Expand Down
4 changes: 4 additions & 0 deletions instrumentation/compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ dependencies {
api(libs.composeUiTestJUnit4)
implementation(libs.composeUiTestManifest)

testImplementation(libs.junitJupiterApi)
testImplementation(libs.junitJupiterParams)
testRuntimeOnly(libs.junitJupiterEngine)

androidTestImplementation(libs.junitJupiterApi)
androidTestImplementation(libs.junitJupiterParams)
androidTestImplementation(libs.espressoCore)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.mannodermaus.junit5.compose

import androidx.activity.ComponentActivity
import androidx.compose.runtime.Composable
import androidx.compose.ui.test.ComposeUiTest
import androidx.compose.ui.test.ExperimentalTestApi
Expand All @@ -10,23 +9,41 @@ import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.SemanticsNodeInteraction
import androidx.compose.ui.test.SemanticsNodeInteractionCollection
import androidx.compose.ui.test.SemanticsNodeInteractionsProvider
import androidx.compose.ui.test.waitUntilAtLeastOneExists
import androidx.compose.ui.test.waitUntilDoesNotExist
import androidx.compose.ui.test.waitUntilExactlyOneExists
import androidx.compose.ui.test.waitUntilNodeCount
import androidx.compose.ui.unit.Density
import androidx.test.core.app.ActivityScenario

/**
* A context through which composable blocks can be orchestrated within a [ComposeExtension].
*/
public sealed interface ComposeContext : SemanticsNodeInteractionsProvider {
// Internal note: The below method list is a copy of `ComposeUiTest`,
// Internal note: The below method list is a copy of `ComposeContentTestRule`,
// preventing the viral spread of its ExperimentalTestApi annotation
// into the consumer's codebase
// into the consumer's codebase and separating it from JUnit 4's TestRule
public val density: Density
public val mainClock: MainTestClock
public fun <T> runOnUiThread(action: () -> T): T
public fun <T> runOnIdle(action: () -> T): T
public fun waitForIdle()
public suspend fun awaitIdle()
public fun waitUntil(timeoutMillis: Long, condition: () -> Boolean)
public fun waitUntil(timeoutMillis: Long = 1_000L, condition: () -> Boolean)
public fun waitUntil(
conditionDescription: String,
timeoutMillis: Long = 1_000L,
condition: () -> Boolean
)

public fun waitUntilNodeCount(
matcher: SemanticsMatcher,
count: Int,
timeoutMillis: Long = 1_000L
)

public fun waitUntilAtLeastOneExists(matcher: SemanticsMatcher, timeoutMillis: Long = 1_000L)
public fun waitUntilExactlyOneExists(matcher: SemanticsMatcher, timeoutMillis: Long = 1_000L)
public fun waitUntilDoesNotExist(matcher: SemanticsMatcher, timeoutMillis: Long = 1_000L)
public fun registerIdlingResource(idlingResource: IdlingResource)
public fun unregisterIdlingResource(idlingResource: IdlingResource)
public fun setContent(composable: @Composable () -> Unit)
Expand All @@ -37,52 +54,56 @@ internal class ComposeContextImpl(
private val delegate: ComposeUiTest
) : ComposeContext, SemanticsNodeInteractionsProvider by delegate {

override fun onAllNodes(
matcher: SemanticsMatcher,
useUnmergedTree: Boolean
): SemanticsNodeInteractionCollection {
return delegate.onAllNodes(matcher, useUnmergedTree)
}

override fun onNode(
matcher: SemanticsMatcher,
useUnmergedTree: Boolean
): SemanticsNodeInteraction {
return delegate.onNode(matcher, useUnmergedTree)
}

override val density: Density get() = delegate.density

override val mainClock: MainTestClock get() = delegate.mainClock

override fun <T> runOnUiThread(action: () -> T): T {
return delegate.runOnUiThread(action)
}
override fun <T> runOnUiThread(action: () -> T): T = delegate.runOnUiThread(action)

override fun <T> runOnIdle(action: () -> T): T {
return delegate.runOnIdle(action)
}
override fun <T> runOnIdle(action: () -> T): T = delegate.runOnIdle(action)

override fun waitForIdle() {
delegate.waitForIdle()
}
override fun waitForIdle() = delegate.waitForIdle()

override suspend fun awaitIdle() {
delegate.awaitIdle()
}
override suspend fun awaitIdle() = delegate.awaitIdle()

override fun waitUntil(timeoutMillis: Long, condition: () -> Boolean) =
delegate.waitUntil(conditionDescription = null, timeoutMillis, condition)

override fun waitUntil(timeoutMillis: Long, condition: () -> Boolean) {
delegate.waitUntil(timeoutMillis, condition)
override fun waitUntil(
conditionDescription: String,
timeoutMillis: Long,
condition: () -> Boolean
) {
delegate.waitUntil(conditionDescription, timeoutMillis, condition)
}

override fun registerIdlingResource(idlingResource: IdlingResource) {
override fun waitUntilNodeCount(matcher: SemanticsMatcher, count: Int, timeoutMillis: Long) =
delegate.waitUntilNodeCount(matcher, count, timeoutMillis)

override fun waitUntilAtLeastOneExists(matcher: SemanticsMatcher, timeoutMillis: Long) =
delegate.waitUntilAtLeastOneExists(matcher, timeoutMillis)

override fun waitUntilExactlyOneExists(matcher: SemanticsMatcher, timeoutMillis: Long) =
delegate.waitUntilExactlyOneExists(matcher, timeoutMillis)

override fun waitUntilDoesNotExist(matcher: SemanticsMatcher, timeoutMillis: Long) =
delegate.waitUntilDoesNotExist(matcher, timeoutMillis)

override fun registerIdlingResource(idlingResource: IdlingResource) =
delegate.registerIdlingResource(idlingResource)
}

override fun unregisterIdlingResource(idlingResource: IdlingResource) {
override fun unregisterIdlingResource(idlingResource: IdlingResource) =
delegate.unregisterIdlingResource(idlingResource)
}

override fun setContent(composable: @Composable () -> Unit) {
delegate.setContent(composable)
}
override fun onNode(
matcher: SemanticsMatcher,
useUnmergedTree: Boolean
): SemanticsNodeInteraction = delegate.onNode(matcher, useUnmergedTree)

override fun onAllNodes(
matcher: SemanticsMatcher,
useUnmergedTree: Boolean
): SemanticsNodeInteractionCollection = delegate.onAllNodes(matcher, useUnmergedTree)

override fun setContent(composable: @Composable () -> Unit) = delegate.setContent(composable)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.mannodermaus.junit5.compose

import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.junit4.ComposeTestRule
import org.junit.jupiter.api.Assertions.fail
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import java.lang.reflect.Method

class ComposeContextTests {
companion object {
@JvmStatic
fun relevantMethods() = buildList {
addAll(ComposeTestRule::class.java.relevantMethods)
addAll(ComposeContentTestRule::class.java.relevantMethods)
}

private val <T> Class<T>.relevantMethods
get() = declaredMethods.filter { '$' !in it.name }
}

@MethodSource("relevantMethods")
@ParameterizedTest(name = "ComposeContext defines {0} correctly")
fun test(method: Method) {
try {
ComposeContext::class.java.getDeclaredMethod(method.name, *method.parameterTypes)
} catch (ignored: NoSuchMethodException) {
fail("ComposeContext does not define method $method")
}
}
}

0 comments on commit 0a5c122

Please sign in to comment.