Skip to content
This repository was archived by the owner on Feb 5, 2021. It is now read-only.

Renames ComposeViewFactoryRoot to CompositionRoot and decouples the implementation. #34

Merged
merged 1 commit into from
May 28, 2020
Merged
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
24 changes: 6 additions & 18 deletions core-compose/api/core-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,6 @@ public final class com/squareup/workflow/ui/compose/ComposeViewFactory : com/squ
public fun getType ()Lkotlin/reflect/KClass;
}

public abstract interface class com/squareup/workflow/ui/compose/ComposeViewFactoryRoot {
public static final field Companion Lcom/squareup/workflow/ui/compose/ComposeViewFactoryRoot$Companion;
public static fun <clinit> ()V
public abstract fun wrap (Lkotlin/jvm/functions/Function1;Landroidx/compose/Composer;)V
}

public final class com/squareup/workflow/ui/compose/ComposeViewFactoryRoot$Companion : com/squareup/workflow/ui/ViewEnvironmentKey {
public static final fun <clinit> ()V
public fun getDefault ()Lcom/squareup/workflow/ui/compose/ComposeViewFactoryRoot;
public synthetic fun getDefault ()Ljava/lang/Object;
}

public final class com/squareup/workflow/ui/compose/ComposeViewFactoryRootKt {
public static final fun <clinit> ()V
public static final fun ComposeViewFactoryRoot (Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow/ui/compose/ComposeViewFactoryRoot;
public static final fun withComposeViewFactoryRoot (Lcom/squareup/workflow/ui/ViewEnvironment;Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow/ui/ViewEnvironment;
}

public abstract class com/squareup/workflow/ui/compose/ComposeWorkflow : com/squareup/workflow/Workflow {
public fun <init> ()V
public fun asStatefulWorkflow ()Lcom/squareup/workflow/StatefulWorkflow;
Expand All @@ -43,6 +25,12 @@ public final class com/squareup/workflow/ui/compose/ComposeWorkflowKt {
public static final fun composed (Lcom/squareup/workflow/Workflow$Companion;Lkotlin/jvm/functions/Function4;)Lcom/squareup/workflow/ui/compose/ComposeWorkflow;
}

public final class com/squareup/workflow/ui/compose/CompositionRootKt {
public static final fun <clinit> ()V
public static final fun withCompositionRoot (Lcom/squareup/workflow/ui/ViewEnvironment;Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow/ui/ViewEnvironment;
public static final fun withCompositionRoot (Lcom/squareup/workflow/ui/ViewRegistry;Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow/ui/ViewRegistry;
}

public final class com/squareup/workflow/ui/compose/ViewEnvironmentsKt {
public static final fun WorkflowRendering (Ljava/lang/Object;Lcom/squareup/workflow/ui/ViewEnvironment;Landroidx/ui/core/Modifier;Landroidx/compose/Composer;)V
public static synthetic fun WorkflowRendering$default (Ljava/lang/Object;Lcom/squareup/workflow/ui/ViewEnvironment;Landroidx/ui/core/Modifier;Landroidx/compose/Composer;ILjava/lang/Object;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ComposeViewFactoryTest {
@Test fun wrapsFactoryWithRoot() {
val wrapperText = mutableStateOf("one")
val viewEnvironment = ViewEnvironment(ViewRegistry(TestFactory))
.withComposeViewFactoryRoot { content ->
.withCompositionRoot { content ->
Column {
Text(wrapperText.value)
content()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.workflow.ui.compose.internal
package com.squareup.workflow.ui.compose

import androidx.compose.FrameManager
import androidx.compose.mutableStateOf
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.ui.foundation.Text
import androidx.ui.layout.Column
Expand All @@ -23,28 +25,114 @@ import androidx.ui.test.assertIsDisplayed
import androidx.ui.test.createComposeRule
import androidx.ui.test.findByText
import com.google.common.truth.Truth.assertThat
import com.squareup.workflow.ui.compose.ComposeViewFactoryRoot
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertFailsWith

@RunWith(AndroidJUnit4::class)
class SafeComposeViewFactoryRootTest {
class CompositionRootTest {

@Rule @JvmField val composeRule = createComposeRule()

@Test fun wrapWithRootIfNecessary_wrapsWhenNecessary() {
val root: CompositionRoot = { content ->
Column {
Text("one")
content()
}
}

composeRule.setContent {
wrapWithRootIfNecessary(root) {
Text("two")
}
}

findByText("one\ntwo").assertIsDisplayed()
}

@Test fun wrapWithRootIfNecessary_onlyWrapsOnce() {
val root: CompositionRoot = { content ->
Column {
Text("one")
content()
}
}

composeRule.setContent {
wrapWithRootIfNecessary(root) {
Text("two")
wrapWithRootIfNecessary(root) {
Text("three")
}
}
}

findByText("one\ntwo\nthree").assertIsDisplayed()
}

@Test fun wrapWithRootIfNecessary_seesUpdatesFromRootWrapper() {
val wrapperText = mutableStateOf("one")
val root: CompositionRoot = { content ->
Column {
Text(wrapperText.value)
content()
}
}

composeRule.setContent {
wrapWithRootIfNecessary(root) {
Text("two")
}
}

findByText("one\ntwo").assertIsDisplayed()
FrameManager.framed {
wrapperText.value = "ENO"
}
findByText("ENO\ntwo").assertIsDisplayed()
}

@Test fun wrapWithRootIfNecessary_rewrapsWhenDifferentRoot() {
val root1: CompositionRoot = { content ->
Column {
Text("one")
content()
}
}
val root2: CompositionRoot = { content ->
Column {
Text("ENO")
content()
}
}
val viewEnvironment = mutableStateOf(root1)

composeRule.setContent {
wrapWithRootIfNecessary(viewEnvironment.value) {
Text("two")
}
}

findByText("one\ntwo").assertIsDisplayed()
FrameManager.framed {
viewEnvironment.value = root2
}
findByText("ENO\ntwo").assertIsDisplayed()
}

@Test fun safeComposeViewFactoryRoot_wraps_content() {
val wrapped = ComposeViewFactoryRoot { content ->
val wrapped: CompositionRoot = { content ->
Column {
Text("Parent")
content()
}
}
val safeRoot = SafeComposeViewFactoryRoot(wrapped)
val safeRoot = safeCompositionRoot(wrapped)

composeRule.setContent {
safeRoot.wrap {
safeRoot {
// Need an explicit semantics container, otherwise both Texts will be merged into a single
// Semantics object with the text "Parent\nChild".
Semantics(container = true) {
Expand All @@ -58,12 +146,12 @@ class SafeComposeViewFactoryRootTest {
}

@Test fun safeComposeViewFactoryRoot_throws_whenChildrenNotInvoked() {
val wrapped = ComposeViewFactoryRoot { }
val safeRoot = SafeComposeViewFactoryRoot(wrapped)
val wrapped: CompositionRoot = { }
val safeRoot = safeCompositionRoot(wrapped)

val error = assertFailsWith<IllegalStateException> {
composeRule.setContent {
safeRoot.wrap {}
safeRoot {}
}
}

Expand All @@ -74,15 +162,15 @@ class SafeComposeViewFactoryRootTest {
}

@Test fun safeComposeViewFactoryRoot_throws_whenChildrenInvokedMultipleTimes() {
val wrapped = ComposeViewFactoryRoot { children ->
val wrapped: CompositionRoot = { children ->
children()
children()
}
val safeRoot = SafeComposeViewFactoryRoot(wrapped)
val safeRoot = safeCompositionRoot(wrapped)

val error = assertFailsWith<IllegalStateException> {
composeRule.setContent {
safeRoot.wrap {
safeRoot {
Text("Hello")
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.squareup.workflow.ui.ViewEnvironment
import com.squareup.workflow.ui.ViewRegistry
import com.squareup.workflow.ui.compose.composedViewFactory
import com.squareup.workflow.ui.compose.WorkflowRendering
import com.squareup.workflow.ui.compose.withComposeViewFactoryRoot
import com.squareup.workflow.ui.compose.withCompositionRoot
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -37,7 +37,7 @@ class ViewFactoriesTest {

@Test fun WorkflowRendering_wrapsFactoryWithRoot_whenAlreadyInComposition() {
val viewEnvironment = ViewEnvironment(ViewRegistry(TestFactory))
.withComposeViewFactoryRoot { content ->
.withCompositionRoot { content ->
Column {
Text("one")
content()
Expand Down
Loading