Skip to content

Commit

Permalink
Remove reliance on kotlinx.coroutines.main.delay (#1164)
Browse files Browse the repository at this point in the history
  • Loading branch information
gamepro65 committed Dec 1, 2023
1 parent 7601c21 commit d96d3f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ class PaparazziPlugin : Plugin<Project> {
test.systemProperties["paparazzi.report.dir"] = reportOutputDir.get().toString()
test.systemProperties["paparazzi.snapshot.dir"] = snapshotOutputDir.toString()
test.systemProperties["paparazzi.artifacts.cache.dir"] = gradleUserHomeDir.path
test.systemProperties["kotlinx.coroutines.main.delay"] = true
test.systemProperties.putAll(project.properties.filterKeys { it.startsWith("app.cash.paparazzi") })

test.inputs.property("paparazzi.test.record", isRecordRun)
Expand Down
12 changes: 12 additions & 0 deletions paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ import android.view.ViewGroup
import androidx.activity.setViewTreeOnBackPressedDispatcherOwner
import androidx.annotation.LayoutRes
import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.InternalComposeUiApi
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.WindowRecomposerPolicy
import androidx.compose.ui.platform.createLifecycleAwareWindowRecomposer
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
Expand Down Expand Up @@ -79,7 +83,9 @@ import java.awt.image.BufferedImage
import java.util.Date
import java.util.EnumSet
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.Dispatchers

@OptIn(ExperimentalComposeUiApi::class, InternalComposeUiApi::class)
class Paparazzi @JvmOverloads constructor(
private val environment: Environment = detectEnvironment(),
private val deviceConfig: DeviceConfig = DeviceConfig.NEXUS_5,
Expand Down Expand Up @@ -289,6 +295,12 @@ class Paparazzi @JvmOverloads constructor(
// CompositionContext, which requires first finding the "content view", then using that
// to find a root view with a ViewTreeLifecycleOwner
viewGroup.id = android.R.id.content

// By default, Compose UI uses its own implementation of CoroutineDispatcher, `AndroidUiDispatcher`.
// Since this dispatcher does not provide its own implementation of Delay, it will default to using DefaultDelay which runs
// async to our test Handler. By initializing Recomposer with Dispatchers.Main, Delay will now be backed by our test Handler,
// synchronizing expected behavior.
WindowRecomposerPolicy.setFactory { it.createLifecycleAwareWindowRecomposer(Dispatchers.Main) }
}

if (hasLifecycleOwnerRuntime) {
Expand Down

0 comments on commit d96d3f1

Please sign in to comment.