diff --git a/web/benchmark-core/build.gradle.kts b/web/benchmark-core/build.gradle.kts index a24d72c0c6..be42e07216 100644 --- a/web/benchmark-core/build.gradle.kts +++ b/web/benchmark-core/build.gradle.kts @@ -35,6 +35,7 @@ kotlin { val jsTest by getting { dependencies { + implementation(project(":test-utils")) implementation(kotlin("test-js")) } } diff --git a/web/benchmark-core/src/jsTest/kotlin/BenchmarkTests.kt b/web/benchmark-core/src/jsTest/kotlin/BenchmarkTests.kt index e607f80414..db3b0d513b 100644 --- a/web/benchmark-core/src/jsTest/kotlin/BenchmarkTests.kt +++ b/web/benchmark-core/src/jsTest/kotlin/BenchmarkTests.kt @@ -4,6 +4,9 @@ import kotlinx.browser.window import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.toList +import org.jetbrains.compose.web.testutils.ComposeWebExperimentalTestsApi +import org.jetbrains.compose.web.testutils.TestScope +import org.jetbrains.compose.web.testutils.runTest import org.w3c.dom.get import kotlin.test.Test import kotlin.test.assertEquals @@ -13,6 +16,7 @@ import kotlin.time.ExperimentalTime import kotlin.time.measureTime @OptIn(ExperimentalTime::class) +@ComposeWebExperimentalTestsApi class BenchmarkTests { private fun flowRepeat(count: Int, calculate: suspend () -> Duration): Flow { @@ -48,7 +52,7 @@ class BenchmarkTests { suspend private fun TestScope.addNItems(n: Int): Duration { val addItemsCount = mutableStateOf(0) - val composition = composition { + composition { AddItems(addItemsCount.value) } @@ -60,7 +64,6 @@ class BenchmarkTests { } assertEquals(n, root.childElementCount) - composition.dispose() return duration } @@ -89,7 +92,7 @@ class BenchmarkTests { fun remove1000Items() = runBenchmark("remove1000Items") { val addItemsCount = mutableStateOf(1000) - val composition = composition { + composition { AddItems(addItemsCount.value) } @@ -101,7 +104,6 @@ class BenchmarkTests { } assertEquals(0, root.childElementCount) - composition.dispose() duration } @@ -111,7 +113,7 @@ class BenchmarkTests { val items = mutableStateListOf() items.addAll(generateSequence(0) { it + 1 }.map { it.toString() }.take(1000)) - val composition = composition { + composition { AddItems(items) } @@ -129,14 +131,13 @@ class BenchmarkTests { repeat(items.size) { if (it % 10 == 0) items[it] = "${items[it]}-$it" } + waitForAnimationFrame() assertEquals(1000, root.childElementCount) assertEquals("1", root.children[1]!!.firstChild!!.textContent) assertEquals("10-10", root.children[10]!!.firstChild!!.textContent) - composition.dispose() - duration } } diff --git a/web/benchmark-core/src/jsTest/kotlin/TestUtils.kt b/web/benchmark-core/src/jsTest/kotlin/TestUtils.kt index fb5ca0b4de..91e819a732 100644 --- a/web/benchmark-core/src/jsTest/kotlin/TestUtils.kt +++ b/web/benchmark-core/src/jsTest/kotlin/TestUtils.kt @@ -17,35 +17,6 @@ import org.w3c.dom.MutationObserverInit import kotlin.coroutines.resume import kotlin.coroutines.suspendCoroutine -private val testScope = MainScope() - -class TestScope : CoroutineScope by testScope { - - val root = "div".asHtmlElement() - - fun composition(content: @Composable () -> Unit): Composition { - root.clear() - return renderComposable(root) { - content() - } - } - - suspend fun waitChanges() { - waitForChanges(root) - } -} - -internal fun runTest(block: suspend TestScope.() -> Unit): dynamic { - val scope = TestScope() - return scope.promise { block(scope) } -} - -internal fun runBlockingTest( - block: suspend CoroutineScope.() -> Unit -): dynamic = testScope.promise { this.block() } - -internal fun String.asHtmlElement() = document.createElement(this) as HTMLElement - /* Currently, the recompositionRunner relies on AnimationFrame to run the recomposition and applyChanges. Therefore we can use this method after updating the state and before making assertions.