Skip to content

Commit

Permalink
Add synchronization declarations to ui:ui-test module
Browse files Browse the repository at this point in the history
  • Loading branch information
pjBooms committed Nov 5, 2024
1 parent 062911a commit 5f21f41
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 55 deletions.
3 changes: 3 additions & 0 deletions compose/ui/ui-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ if (AndroidXComposePlugin.isMultiplatformEnabled(project)) {

skikoMain {
dependsOn(commonMain)
dependencies {
implementation(libs.atomicFu)
}
}

desktopMain.dependsOn(skikoMain)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package androidx.compose.ui.test

import androidx.compose.ui.test.platform.makeSynchronizedObject
import androidx.compose.ui.test.platform.synchronized
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.CoroutineContext
Expand Down Expand Up @@ -43,9 +45,10 @@ internal class UncaughtExceptionHandler :
AbstractCoroutineContextElement(CoroutineExceptionHandler),
CoroutineExceptionHandler {
private var exception: Throwable? = null
private val lock = makeSynchronizedObject(this)

override fun handleException(context: CoroutineContext, exception: Throwable) {
synchronized(this) {
synchronized(lock) {
if (this.exception == null) {
this.exception = exception
} else {
Expand All @@ -67,7 +70,7 @@ internal class UncaughtExceptionHandler :
* points to fail the test asap after the exception was caught.
*/
fun throwUncaught() {
synchronized(this) {
synchronized(lock) {
val exception = exception
if (exception != null) {
this.exception = null
Expand All @@ -76,5 +79,3 @@ internal class UncaughtExceptionHandler :
}
}
}

internal expect inline fun <T> synchronized(lock: Any, block: () -> T): T
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,15 @@
* limitations under the License.
*/

package androidx.compose.ui.test
package androidx.compose.ui.test.platform

internal actual inline fun <T> synchronized(lock: Any, block: () -> T) =
kotlin.synchronized(lock, block)
internal expect class SynchronizedObject

/**
* Returns [ref] as a [SynchronizedObject] on platforms where [Any] is a valid [SynchronizedObject],
* or a new [SynchronizedObject] instance if [ref] is null or this is not supported on the current
* platform.
*/
internal expect inline fun makeSynchronizedObject(ref: Any? = null): SynchronizedObject

internal expect inline fun <R> synchronized(lock: SynchronizedObject, block: () -> R): R

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import androidx.compose.ui.platform.WindowInfo
import androidx.compose.ui.scene.ComposeScene
import androidx.compose.ui.scene.CanvasLayersComposeScene
import androidx.compose.ui.semantics.SemanticsNode
import androidx.compose.ui.test.platform.makeSynchronizedObject
import androidx.compose.ui.test.platform.synchronized
import androidx.compose.ui.text.input.EditCommand
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.ImeOptions
Expand Down Expand Up @@ -189,6 +191,7 @@ class SkikoComposeUiTest @InternalTestApi constructor(
private val testContext = TestContext(testOwner)

private val idlingResources = mutableSetOf<IdlingResource>()
private val idlingResourcesLock = makeSynchronizedObject(idlingResources)

fun <R> runTest(block: SkikoComposeUiTest.() -> R): R {
return composeRootRegistry.withRegistry {
Expand Down Expand Up @@ -331,18 +334,18 @@ class SkikoComposeUiTest @InternalTestApi constructor(
}

override fun registerIdlingResource(idlingResource: IdlingResource) {
synchronized(idlingResources) {
synchronized(idlingResourcesLock) {
idlingResources.add(idlingResource)
}
}

override fun unregisterIdlingResource(idlingResource: IdlingResource) {
synchronized(idlingResources) {
synchronized(idlingResourcesLock) {
idlingResources.remove(idlingResource)
}
}

private fun areAllResourcesIdle() = synchronized(idlingResources) {
private fun areAllResourcesIdle() = synchronized(idlingResourcesLock) {
idlingResources.all { it.isIdleNow }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.compose.ui.test.platform

package androidx.compose.ui.test
internal actual class SynchronizedObject : kotlinx.atomicfu.locks.SynchronizedObject()

internal actual inline fun <T> synchronized(lock: Any, block: () -> T) = block()
internal actual inline fun makeSynchronizedObject(ref: Any?) = SynchronizedObject()

internal actual inline fun <R> synchronized(lock: SynchronizedObject, block: () -> R): R =
kotlinx.atomicfu.locks.synchronized(lock, block)

This file was deleted.

3 changes: 1 addition & 2 deletions compose/ui/ui-text/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ if(!AndroidXComposePlugin.isMultiplatformEnabled(project)) {
*/
implementation(libs.kotlinStdlibCommon)
implementation(libs.kotlinCoroutinesCore)
implementation(libs.atomicFu)

api(project(":compose:ui:ui-graphics"))
api(project(":compose:ui:ui-unit"))
Expand Down Expand Up @@ -105,7 +104,6 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
commonMain.dependencies {
implementation(libs.kotlinStdlibCommon)
implementation(libs.kotlinCoroutinesCore)
implementation(libs.atomicFu)

api(project(":compose:ui:ui-graphics"))
api(project(":compose:ui:ui-unit"))
Expand All @@ -129,6 +127,7 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
dependsOn(commonMain)
dependencies {
api(libs.skikoCommon)
implementation(libs.atomicFu)
}
}

Expand Down
2 changes: 1 addition & 1 deletion compose/ui/ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
implementation(project(":collection:collection"))
implementation(libs.kotlinStdlibCommon)
implementation(libs.kotlinCoroutinesCore)
implementation(libs.atomicFu)

// when updating the runtime version please also update the runtime-saveable version
implementation(project(":compose:runtime:runtime"))
Expand Down Expand Up @@ -205,6 +204,7 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
api(project(":compose:ui:ui-graphics"))
api(project(":compose:ui:ui-text"))
api(libs.skikoCommon)
implementation(libs.atomicFu)
}
}
uikitMain {
Expand Down

0 comments on commit 5f21f41

Please sign in to comment.