Skip to content

Commit

Permalink
fix test to work around Compose's bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny-chung committed Nov 24, 2024
1 parent 123149d commit 1e66292
Showing 1 changed file with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package com.sunnychung.application.multiplatform.hellohttp.test

import androidx.compose.ui.graphics.asSkiaBitmap
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.semantics.SemanticsNode
import androidx.compose.ui.semantics.SemanticsProperties
Expand All @@ -11,6 +10,7 @@ import androidx.compose.ui.test.ComposeTimeoutException
import androidx.compose.ui.test.ComposeUiTest
import androidx.compose.ui.test.DesktopComposeUiTest
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.SemanticsNodeInteraction
import androidx.compose.ui.test.SemanticsNodeInteractionCollection
import androidx.compose.ui.test.assertIsDisplayed
Expand All @@ -28,7 +28,6 @@ import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollToNode
import androidx.compose.ui.test.performTextClearance
import androidx.compose.ui.test.performTextInput
import androidx.compose.ui.test.runComposeUiTest
import androidx.compose.ui.test.runDesktopComposeUiTest
import androidx.compose.ui.test.waitUntilExactlyOneExists
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -64,8 +63,6 @@ import com.sunnychung.lib.multiplatform.kdatetime.extension.seconds
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.jetbrains.skia.EncodedImageFormat
import org.jetbrains.skia.Image
import org.jetbrains.skiko.toBufferedImage
import org.jetbrains.skiko.toImage
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
Expand Down Expand Up @@ -136,7 +133,7 @@ suspend fun DesktopComposeUiTest.createProjectIfNeeded() {
// create first project
onNodeWithTag(TestTag.FirstTimeCreateProjectButton.name)
.performClickWithRetry(this)
waitUntilExactlyOneExists(hasTestTag(TestTag.ProjectNameAndSubprojectNameDialogTextField.name), 1500L)
waitUntilExactlyOneExists(this, hasTestTag(TestTag.ProjectNameAndSubprojectNameDialogTextField.name), 1500L)
onNodeWithTag(TestTag.ProjectNameAndSubprojectNameDialogTextField.name)
.performTextInput(this, "Test Project ${KZonedInstant.nowAtLocalZoneOffset().format("HH:mm:ss")}")
waitForIdle()
Expand All @@ -146,10 +143,10 @@ suspend fun DesktopComposeUiTest.createProjectIfNeeded() {
// create first subproject
delayShort()
waitForIdle()
waitUntilExactlyOneExists(hasTestTag(TestTag.FirstTimeCreateSubprojectButton.name), 1500L)
waitUntilExactlyOneExists(this, hasTestTag(TestTag.FirstTimeCreateSubprojectButton.name), 1500L)
onNodeWithTag(TestTag.FirstTimeCreateSubprojectButton.name)
.performClickWithRetry(this)
waitUntilExactlyOneExists(hasTestTag(TestTag.ProjectNameAndSubprojectNameDialogTextField.name), 1500L)
waitUntilExactlyOneExists(this, hasTestTag(TestTag.ProjectNameAndSubprojectNameDialogTextField.name), 1500L)
onNodeWithTag(TestTag.ProjectNameAndSubprojectNameDialogTextField.name)
.performTextInput(this, "Test Subproject")
waitForIdle()
Expand All @@ -170,7 +167,7 @@ suspend fun DesktopComposeUiTest.createProjectIfNeeded() {
onNodeWithTag(TestTag.EditEnvironmentsButton.name)
.assertIsDisplayedWithRetry(this)
.performClickWithRetry(this)
waitUntilExactlyOneExists(hasTestTag(TestTag.EnvironmentDialogCreateButton.name))
waitUntilExactlyOneExists(this, hasTestTag(TestTag.EnvironmentDialogCreateButton.name))

createEnvironmentInEnvDialog(TestEnvironment.LocalDefault.displayName)

Expand Down Expand Up @@ -378,7 +375,7 @@ suspend fun DesktopComposeUiTest.createProjectIfNeeded() {
}
// while (true) {
// try {
waitUntilExactlyOneExists(hasTestTag(TestTag.CreateRequestOrFolderButton.name), 5000L)
waitUntilExactlyOneExists(this, hasTestTag(TestTag.CreateRequestOrFolderButton.name), 5000L)
// } catch (_: IllegalArgumentException) {
// println("waiting")
// waitForIdle()
Expand Down Expand Up @@ -555,11 +552,11 @@ suspend fun DesktopComposeUiTest.createRequest(request: UserRequestTemplate, env
.assertIsDisplayedWithRetry(this)
.performClickWithRetry(this)
delayShort()
waitUntilExactlyOneExists(hasTextExactly("Request", includeEditableText = false))
waitUntilExactlyOneExists(this, hasTextExactly("Request", includeEditableText = false))
onNodeWithText("Request")
.assertIsDisplayedWithRetry(this)
.performClickWithRetry(this)
waitUntilExactlyOneExists(hasTestTag(TestTag.RequestUrlTextField.name), 1000L)
waitUntilExactlyOneExists(this, hasTestTag(TestTag.RequestUrlTextField.name), 1000L)

delayShort()

Expand All @@ -586,7 +583,7 @@ suspend fun DesktopComposeUiTest.createRequest(request: UserRequestTemplate, env
TestTagPart.DropdownItem,
baseExample.contentType.displayText
)!!
waitUntilExactlyOneExists(hasTestTag(nextTag))
waitUntilExactlyOneExists(this, hasTestTag(nextTag))
onNodeWithTag(nextTag)
.assertIsDisplayedWithRetry(this)
.performClickWithRetry(this)
Expand All @@ -607,7 +604,7 @@ suspend fun DesktopComposeUiTest.createRequest(request: UserRequestTemplate, env
ContentType.Multipart -> {
val body = (baseExample.body as MultipartBody).value
body.forEachIndexed { index, it ->
waitUntilExactlyOneExists(
waitUntilExactlyOneExists(this,
hasTestTag(
buildTestTag(
TestTagPart.RequestBodyMultipartForm,
Expand All @@ -617,7 +614,7 @@ suspend fun DesktopComposeUiTest.createRequest(request: UserRequestTemplate, env
)!!
)
)
waitUntilExactlyOneExists(
waitUntilExactlyOneExists(this,
hasTestTag(
buildTestTag(
TestTagPart.RequestBodyMultipartForm,
Expand Down Expand Up @@ -756,7 +753,7 @@ suspend fun DesktopComposeUiTest.createRequest(request: UserRequestTemplate, env
ContentType.FormUrlEncoded -> {
val body = (baseExample.body as FormUrlEncodedBody).value
body.forEachIndexed { index, it ->
waitUntilExactlyOneExists(
waitUntilExactlyOneExists(this,
hasTestTag(
buildTestTag(
TestTagPart.RequestBodyFormUrlEncodedForm,
Expand All @@ -766,7 +763,7 @@ suspend fun DesktopComposeUiTest.createRequest(request: UserRequestTemplate, env
)!!
)
)
waitUntilExactlyOneExists(
waitUntilExactlyOneExists(this,
hasTestTag(
buildTestTag(
TestTagPart.RequestBodyFormUrlEncodedForm,
Expand Down Expand Up @@ -844,7 +841,7 @@ suspend fun DesktopComposeUiTest.createRequest(request: UserRequestTemplate, env
.performClickWithRetry(this)

baseExample.queryParameters.forEachIndexed { index, it ->
waitUntilExactlyOneExists(
waitUntilExactlyOneExists(this,
hasTestTag(
buildTestTag(
TestTagPart.RequestQueryParameter,
Expand All @@ -854,7 +851,7 @@ suspend fun DesktopComposeUiTest.createRequest(request: UserRequestTemplate, env
)!!
)
)
waitUntilExactlyOneExists(
waitUntilExactlyOneExists(this,
hasTestTag(
buildTestTag(
TestTagPart.RequestQueryParameter,
Expand Down Expand Up @@ -910,7 +907,7 @@ suspend fun DesktopComposeUiTest.createRequest(request: UserRequestTemplate, env
.assertIsDisplayedWithRetry(this)
.performClickWithRetry(this)
baseExample.headers.forEachIndexed { index, it ->
waitUntilExactlyOneExists(
waitUntilExactlyOneExists(this,
hasTestTag(
buildTestTag(
TestTagPart.RequestHeader,
Expand All @@ -920,7 +917,7 @@ suspend fun DesktopComposeUiTest.createRequest(request: UserRequestTemplate, env
)!!
)
)
waitUntilExactlyOneExists(
waitUntilExactlyOneExists(this,
hasTestTag(
buildTestTag(
TestTagPart.RequestHeader,
Expand All @@ -946,7 +943,7 @@ suspend fun DesktopComposeUiTest.createRequest(request: UserRequestTemplate, env
.assertIsDisplayedWithRetry(this)
.performClickWithRetry(this)

waitUntilExactlyOneExists(hasTestTag(TestTag.RequestPreFlightScriptTextField.name))
waitUntilExactlyOneExists(this, hasTestTag(TestTag.RequestPreFlightScriptTextField.name))

onNode(hasTestTag(TestTag.RequestPreFlightScriptTextField.name))
.assertIsDisplayedWithRetry(this)
Expand Down Expand Up @@ -1260,6 +1257,23 @@ fun SemanticsNodeInteraction.performTextInput(host: ComposeUiTest, s: String) {
}
}

fun waitUntilExactlyOneExists(
host: ComposeUiTest,
matcher: SemanticsMatcher,
timeoutMillis: Long = 1_000L
) {
// this would cause deadlock
// host.runOnUiThread {
// host.waitUntilExactlyOneExists(matcher, timeoutMillis)
// }

host.waitUntil(timeoutMillis) {
host.runOnUiThread {
host.onAllNodes(matcher).fetchSemanticsNodes().size == 1
}
}
}

fun captureScreenToFile(filename: String) {
val image = Robot().createScreenCapture(Rectangle(Toolkit.getDefaultToolkit().screenSize))
val parent = File("test-error-screenshot")
Expand Down

0 comments on commit 1e66292

Please sign in to comment.