|
| 1 | +package com.squareup.workflow1.testing |
| 2 | + |
| 3 | +import androidx.compose.runtime.Composable |
| 4 | +import com.squareup.workflow1.NullableInitBox |
| 5 | +import com.squareup.workflow1.RuntimeConfig |
| 6 | +import com.squareup.workflow1.Workflow |
| 7 | +import com.squareup.workflow1.WorkflowAction |
| 8 | +import com.squareup.workflow1.WorkflowExperimentalApi |
| 9 | +import com.squareup.workflow1.WorkflowIdentifier |
| 10 | +import com.squareup.workflow1.WorkflowOutput |
| 11 | +import com.squareup.workflow1.compose.ComposeWorkflow |
| 12 | +import com.squareup.workflow1.compose.LocalWorkflowComposableRenderer |
| 13 | +import com.squareup.workflow1.compose.WorkflowComposableRenderer |
| 14 | +import com.squareup.workflow1.identifier |
| 15 | +import com.squareup.workflow1.internal.compose.ComposeWorkflowState |
| 16 | +import com.squareup.workflow1.internal.compose.runtime.launchSynchronizedMolecule |
| 17 | +import com.squareup.workflow1.internal.compose.withCompositionLocals |
| 18 | +import com.squareup.workflow1.testing.RealRenderTester.Expectation |
| 19 | +import com.squareup.workflow1.testing.RealRenderTester.Expectation.ExpectedWorkflow |
| 20 | +import com.squareup.workflow1.testing.RenderTester.ChildWorkflowMatch.Matched |
| 21 | +import kotlinx.coroutines.CoroutineScope |
| 22 | +import kotlinx.coroutines.Dispatchers |
| 23 | +import kotlinx.coroutines.cancel |
| 24 | + |
| 25 | +// TODO move this to RealComposeRenderTester |
| 26 | +@OptIn(WorkflowExperimentalApi::class) |
| 27 | +internal class ComposeRenderTester<PropsT, OutputT, RenderingT>( |
| 28 | + private val workflow: ComposeWorkflow<PropsT, OutputT, RenderingT>, |
| 29 | + private val props: PropsT, |
| 30 | + private val runtimeConfig: RuntimeConfig, |
| 31 | +) : RenderTester<PropsT, ComposeWorkflowState, OutputT, RenderingT>(), |
| 32 | + WorkflowComposableRenderer, |
| 33 | + RenderTestResult<PropsT, ComposeWorkflowState, OutputT, RenderingT> { |
| 34 | + |
| 35 | + private data class OutputWithHandler<ChildOutputT>( |
| 36 | + val output: ChildOutputT, |
| 37 | + val handler: (ChildOutputT) -> Unit |
| 38 | + ) |
| 39 | + |
| 40 | + /** |
| 41 | + * List of [Expectation]s that are expected when the workflow is rendered. New expectations are |
| 42 | + * registered into this list. Once the render pass has started, expectations are moved from this |
| 43 | + * list to [consumedExpectations] as soon as they're matched. |
| 44 | + */ |
| 45 | + private val expectations: MutableList<ExpectedWorkflow> = mutableListOf() |
| 46 | + |
| 47 | + /** |
| 48 | + * Empty until the render pass starts, then every time the workflow matches an expectation that |
| 49 | + * has `exactMatch` set to true, it is moved from [expectations] to this list. |
| 50 | + */ |
| 51 | + private val consumedExpectations: MutableList<Expectation<*>> = mutableListOf() |
| 52 | + |
| 53 | + private var processedOutputHandler: OutputWithHandler<*>? = null |
| 54 | + |
| 55 | + /** |
| 56 | + * Tracks the identifier/key pairs of all calls to [renderChild], so it can emulate the behavior |
| 57 | + * of the real runtime and throw if a workflow is rendered twice in the same pass. |
| 58 | + */ |
| 59 | + private val renderedChildren: MutableList<WorkflowIdentifier> = mutableListOf() |
| 60 | + |
| 61 | + override fun expectWorkflow( |
| 62 | + description: String, |
| 63 | + exactMatch: Boolean, |
| 64 | + matcher: (RenderChildInvocation) -> ChildWorkflowMatch |
| 65 | + ): RenderTester<PropsT, ComposeWorkflowState, OutputT, RenderingT> = apply { |
| 66 | + expectations += ExpectedWorkflow(matcher, exactMatch, description) |
| 67 | + } |
| 68 | + |
| 69 | + override fun expectSideEffect( |
| 70 | + description: String, |
| 71 | + exactMatch: Boolean, |
| 72 | + matcher: (key: String) -> Boolean |
| 73 | + ): RenderTester<PropsT, ComposeWorkflowState, OutputT, RenderingT> { |
| 74 | + throw AssertionError( |
| 75 | + "Expected ComposeWorkflow to have side effect $description, " + |
| 76 | + "but ComposeWorkflows use Compose effects." |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + override fun expectRemember( |
| 81 | + description: String, |
| 82 | + exactMatch: Boolean, |
| 83 | + matcher: (RememberInvocation) -> Boolean |
| 84 | + ): RenderTester<PropsT, ComposeWorkflowState, OutputT, RenderingT> { |
| 85 | + throw AssertionError( |
| 86 | + "Cannot validate calls to Compose's remember {} function through RenderTester." |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + override fun requireExplicitWorkerExpectations(): |
| 91 | + RenderTester<PropsT, ComposeWorkflowState, OutputT, RenderingT> { |
| 92 | + // Noop |
| 93 | + return this |
| 94 | + } |
| 95 | + |
| 96 | + override fun requireExplicitSideEffectExpectations(): |
| 97 | + RenderTester<PropsT, ComposeWorkflowState, OutputT, RenderingT> { |
| 98 | + // Noop |
| 99 | + return this |
| 100 | + } |
| 101 | + |
| 102 | + override fun requireExplicitRememberExpectations(): |
| 103 | + RenderTester<PropsT, ComposeWorkflowState, OutputT, RenderingT> { |
| 104 | + // Noop |
| 105 | + return this |
| 106 | + } |
| 107 | + |
| 108 | + override fun render( |
| 109 | + block: (rendering: RenderingT) -> Unit |
| 110 | + ): RenderTestResult<PropsT, ComposeWorkflowState, OutputT, RenderingT> { |
| 111 | + val emitOutput: (OutputT) -> Unit = { output -> |
| 112 | + TODO() |
| 113 | + } |
| 114 | + |
| 115 | + val scope = CoroutineScope(Dispatchers.Unconfined) |
| 116 | + try { |
| 117 | + val molecule = scope.launchSynchronizedMolecule(onNeedsRecomposition = {}) |
| 118 | + val rendering = molecule.recomposeWithContent { |
| 119 | + withCompositionLocals(LocalWorkflowComposableRenderer provides this) { |
| 120 | + workflow.produceRendering(props, emitOutput) |
| 121 | + } |
| 122 | + } |
| 123 | + block(rendering) |
| 124 | + } finally { |
| 125 | + scope.cancel() |
| 126 | + } |
| 127 | + return this |
| 128 | + } |
| 129 | + |
| 130 | + @Composable |
| 131 | + override fun <ChildPropsT, ChildOutputT, ChildRenderingT> renderChild( |
| 132 | + childWorkflow: Workflow<ChildPropsT, ChildOutputT, ChildRenderingT>, |
| 133 | + props: ChildPropsT, |
| 134 | + onOutput: ((ChildOutputT) -> Unit)? |
| 135 | + ): ChildRenderingT { |
| 136 | + val identifier = childWorkflow.identifier |
| 137 | + require(identifier !in renderedChildren) { |
| 138 | + "Expected keys to be unique for ${childWorkflow.identifier}" |
| 139 | + } |
| 140 | + renderedChildren += identifier |
| 141 | + |
| 142 | + val description = buildString { |
| 143 | + append("child ") |
| 144 | + append(childWorkflow.identifier) |
| 145 | + // if (key.isNotEmpty()) { |
| 146 | + // append(" with key \"$key\"") |
| 147 | + // } |
| 148 | + } |
| 149 | + val invocation = createRenderChildInvocation(childWorkflow, props, renderKey = "") |
| 150 | + val matches = expectations.mapNotNull { |
| 151 | + val matchResult = it.matcher(invocation) |
| 152 | + if (matchResult is Matched) Pair(it, matchResult) else null |
| 153 | + } |
| 154 | + if (matches.isEmpty()) { |
| 155 | + throw AssertionError("Tried to render unexpected $description") |
| 156 | + } |
| 157 | + |
| 158 | + val exactMatches = matches.filter { it.first.exactMatch } |
| 159 | + val (_, match) = when { |
| 160 | + exactMatches.size == 1 -> { |
| 161 | + exactMatches.single() |
| 162 | + .also { (expected, _) -> |
| 163 | + expectations -= expected |
| 164 | + consumedExpectations += expected |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + exactMatches.size > 1 -> { |
| 169 | + throw AssertionError( |
| 170 | + "Multiple expectations matched $description:\n" + |
| 171 | + exactMatches.joinToString(separator = "\n") { " ${it.first.describe()}" } |
| 172 | + ) |
| 173 | + } |
| 174 | + // Inexact matches are not consumable. |
| 175 | + else -> matches.first() |
| 176 | + } |
| 177 | + |
| 178 | + if (match.output != null) { |
| 179 | + check(processedOutputHandler == null) { |
| 180 | + "Expected only one output to be expected: $description expected to emit " + |
| 181 | + "${match.output.value} but ${emittedOutput?.debuggingName} was already processed." |
| 182 | + } |
| 183 | + processedOutputHandler = OutputWithHandler(match.output, onOutput) |
| 184 | + @Suppress("UNCHECKED_CAST") |
| 185 | + processedAction = handler(match.output.value as ChildOutputT) |
| 186 | + } |
| 187 | + |
| 188 | + @Suppress("UNCHECKED_CAST") |
| 189 | + return match.childRendering as ChildRenderingT |
| 190 | + } |
| 191 | + |
| 192 | + override fun verifyAction( |
| 193 | + block: (WorkflowAction<PropsT, ComposeWorkflowState, OutputT>) -> Unit |
| 194 | + ): RenderTestResult<PropsT, ComposeWorkflowState, OutputT, RenderingT> { |
| 195 | + TODO("Not yet implemented") |
| 196 | + } |
| 197 | + |
| 198 | + override fun verifyActionResult( |
| 199 | + block: (newState: ComposeWorkflowState, appliedResult: WorkflowOutput<OutputT>?) -> Unit |
| 200 | + ): RenderTestResult<PropsT, ComposeWorkflowState, OutputT, RenderingT> { |
| 201 | + TODO("Not yet implemented") |
| 202 | + } |
| 203 | + |
| 204 | + override fun testNextRender(): RenderTester<PropsT, ComposeWorkflowState, OutputT, RenderingT> = |
| 205 | + testNextRenderWithProps(props) |
| 206 | + |
| 207 | + override fun testNextRenderWithProps( |
| 208 | + newProps: PropsT |
| 209 | + ): RenderTester<PropsT, ComposeWorkflowState, OutputT, RenderingT> { |
| 210 | + TODO("Not yet implemented") |
| 211 | + } |
| 212 | +} |
0 commit comments