Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,6 @@ internal interface OnCanvasTests {

private fun getContainer() = document.getElementById(containerId) ?: error("failed to get canvas with id ${containerId}")

private fun getAppRoot() = getShadowRoot().children[0] as HTMLElement

fun getA11YContainer(): HTMLElement? {
return if (getAppRoot().children.length < 3) {
null
} else {
// The expected order is: canvas, interop container <div>, a11y container <div>
getAppRoot().children[2] as HTMLElement
}
}

fun getShadowRoot(): ExtendedShadowRoot =
(getContainer().shadowRoot as? ExtendedShadowRoot) ?: error("failed to get shadowRoot")
Expand All @@ -111,28 +101,6 @@ internal interface OnCanvasTests {
}
}

suspend fun awaitA11YChanges() {
val a11yContainer = getA11YContainer() ?: return

fun skipFramesUntil(condition: () -> Boolean, onTrue: () -> Unit) {
window.requestAnimationFrame {
if (!condition()) {
skipFramesUntil(condition, onTrue)
} else {
onTrue()
}
}
}

suspendCoroutine { continuation ->
val initialContent = a11yContainer.innerHTML
skipFramesUntil(
condition = { a11yContainer.innerHTML != initialContent },
onTrue = { continuation.resumeWith(Result.success(Unit)) }
)
}
}

fun dispatchEvents(vararg events: Event) {
dispatchEvents(getCanvas(), *events)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.OnCanvasTests
import androidx.compose.ui.currentTimeMillis
import androidx.compose.ui.platform.testTag
import kotlin.test.Ignore
import kotlin.coroutines.Continuation
import kotlin.coroutines.suspendCoroutine
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlinx.browser.document
import kotlinx.browser.window
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand All @@ -45,6 +46,37 @@ import org.w3c.dom.HTMLElement
import org.w3c.dom.get

class CfWA11YTest : OnCanvasTests {
private fun getAppRoot() = getShadowRoot().children[0] as HTMLElement

private suspend fun awaitA11YChanges() {
val a11yContainer = getA11YContainer() ?: return

fun Continuation<Unit>.waitUntil(waitUntilCondition: () -> Boolean) {
window.requestAnimationFrame {
if (!waitUntilCondition()) {
waitUntil(waitUntilCondition)
} else {
resumeWith(Result.success(Unit))
}
}
}

suspendCoroutine { continuation ->
val initialContent = a11yContainer.innerHTML
continuation.waitUntil {
a11yContainer.innerHTML != initialContent
}
}
}

private fun getA11YContainer(): HTMLElement? {
return if (getAppRoot().children.length < 3) {
null
} else {
// The expected order is: canvas, interop container <div>, a11y container <div>
getAppRoot().children[2] as HTMLElement
}
}

@Test
fun a11yButtonClick() = runApplicationTest {
Expand All @@ -58,6 +90,7 @@ class CfWA11YTest : OnCanvasTests {
}
}


awaitIdle()

val a11yContainer = getA11YContainer()
Expand All @@ -71,9 +104,10 @@ class CfWA11YTest : OnCanvasTests {
assertEquals("button", button1.getAttribute("role"))
assertEquals("Button1", button1.innerText)

repeat(3) {
repeat(3) { repeatCounter ->
button1.click()
assertEquals(it + 1, clickCounter)
awaitIdle()
assertEquals(repeatCounter + 1, clickCounter)
}
}

Expand Down Expand Up @@ -127,11 +161,11 @@ class CfWA11YTest : OnCanvasTests {
assertTrue(button2.isConnected)


repeat(3) {
repeat(3) { repeatCounter ->
button1.click()
button2.click()
assertEquals(it + 1, clickCounter1)
assertEquals(it + 1, clickCounter2)
assertEquals(repeatCounter + 1, clickCounter1)
assertEquals(repeatCounter + 1, clickCounter2)
}

showButton2 = false
Expand Down Expand Up @@ -206,7 +240,6 @@ class CfWA11YTest : OnCanvasTests {
assertEquals("Button3", buttonsContainer.children[2]!!.innerHTML)
}

@Ignore // Sometimes fails on latest firefox FIXME: https://youtrack.jetbrains.com/issue/CMP-8955
@Test
fun changesMustBeBatched() = runApplicationTest {
var show1 by mutableStateOf(true)
Expand Down Expand Up @@ -257,7 +290,6 @@ class CfWA11YTest : OnCanvasTests {
assertTrue(waitedForChangesMs in 50..150, "Changes must be batched, waited for $waitedForChangesMs ms. Allowed tolerance 50ms was exceeded")
}

@Ignore // Sometimes fails on latest firefox FIXME: https://youtrack.jetbrains.com/issue/CMP-8955
@Test
fun changesMustBeAppliedDespiteConstantDebounceAfter1Second() = runApplicationTest {
var show1 by mutableStateOf(true)
Expand Down Expand Up @@ -324,7 +356,6 @@ class CfWA11YTest : OnCanvasTests {
)
}

@Ignore // Sometimes fails on latest firefox FIXME: https://youtrack.jetbrains.com/issue/CMP-8955
@Test
fun noChangesFor1SecondTheDebounceShouldWork() = runApplicationTest {
var show by mutableStateOf(true)
Expand Down
Loading