From c1e728d0fcbb2c308d1a7ae3c26a525ce83db283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandrina=20Patr=C3=B3n?= Date: Wed, 7 Jun 2023 12:34:02 -0400 Subject: [PATCH] Add iOS change list renderer tests --- redwood-treehouse-host/build.gradle | 8 ++ .../treehouse/ChangeListRendererTest.kt | 76 +++++++++++++++++++ .../DefaultExampleSchemaWidgetFactory.kt | 36 +++++++++ .../treehouse/ExampleSchemaWidgetSystem.kt | 38 ++++++++++ .../app/cash/redwood/treehouse/RowBinding.kt | 32 ++++++++ .../app/cash/redwood/treehouse/TextBinding.kt | 30 ++++++++ 6 files changed, 220 insertions(+) create mode 100644 redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/ChangeListRendererTest.kt create mode 100644 redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/DefaultExampleSchemaWidgetFactory.kt create mode 100644 redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/ExampleSchemaWidgetSystem.kt create mode 100644 redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/RowBinding.kt create mode 100644 redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/TextBinding.kt diff --git a/redwood-treehouse-host/build.gradle b/redwood-treehouse-host/build.gradle index 86be5ca72c..871caeccda 100644 --- a/redwood-treehouse-host/build.gradle +++ b/redwood-treehouse-host/build.gradle @@ -7,6 +7,7 @@ apply plugin: 'app.cash.zipline' apply plugin: 'app.cash.redwood.build' redwoodBuild { + composeCompiler() publishing() } @@ -40,6 +41,13 @@ kotlin { implementation libs.assertk implementation libs.kotlinx.coroutines.test implementation libs.turbine + implementation projects.redwoodCompose + implementation projects.redwoodLayoutUiview + implementation projects.redwoodTesting + implementation projects.testSchema.compose + implementation projects.testSchema.composeProtocol + implementation projects.testSchema.testing + implementation projects.testSchema.widgetProtocol } } androidUnitTest { diff --git a/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/ChangeListRendererTest.kt b/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/ChangeListRendererTest.kt new file mode 100644 index 0000000000..14205475af --- /dev/null +++ b/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/ChangeListRendererTest.kt @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2023 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.cash.redwood.treehouse + +import app.cash.redwood.testing.toChangeList +import assertk.assertThat +import assertk.assertions.isEqualTo +import example.redwood.compose.ExampleSchemaProtocolBridge +import example.redwood.compose.Row +import example.redwood.compose.Text +import example.redwood.widget.ExampleSchemaTester +import kotlin.test.Test +import kotlinx.coroutines.test.runTest +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json +import platform.UIKit.UILabel +import platform.UIKit.UIStackView +import platform.UIKit.UIView + +class ChangeListRendererTest { + @Test fun createSnapshotChangeListAndRenderIt() = runTest { + ExampleSchemaTester { + setContent { + Text("hello") + } + val snapshot = awaitSnapshot() + val snapshotChangeList = snapshot.toChangeList(ExampleSchemaProtocolBridge) + + val treehouseUIKitView = TreehouseUIKitView(ExampleSchemaWidgetSystem()) + + val renderer = ChangeListRenderer(Json) + renderer.render(treehouseUIKitView, snapshotChangeList) + + val uiView = treehouseUIKitView.view + assertThat((uiView.subviews.firstOrNull() as? UILabel)?.text).isEqualTo("hello") + } + } + + @Test fun renderSnapshotChangeListWithSubviews() = runTest { + ExampleSchemaTester { + setContent { + Row { + Text("red") + Text("orange") + Text("yellow") + } + } + val snapshot = awaitSnapshot() + val snapshotChangeList = snapshot.toChangeList(ExampleSchemaProtocolBridge) + + val treehouseUIKitView = TreehouseUIKitView(ExampleSchemaWidgetSystem()) + + val renderer = ChangeListRenderer(Json) + renderer.render(treehouseUIKitView, snapshotChangeList) + + val uiView = treehouseUIKitView.view + val row = uiView.subviews.firstOrNull() as UIStackView + assertThat((row.subviews[0] as? UILabel)?.text).isEqualTo("red") + assertThat((row.subviews[1] as? UILabel)?.text).isEqualTo("orange") + assertThat((row.subviews[2] as? UILabel)?.text).isEqualTo("yellow") + } + } +} diff --git a/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/DefaultExampleSchemaWidgetFactory.kt b/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/DefaultExampleSchemaWidgetFactory.kt new file mode 100644 index 0000000000..5553ba70f6 --- /dev/null +++ b/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/DefaultExampleSchemaWidgetFactory.kt @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2023 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.cash.redwood.treehouse + +import example.redwood.widget.Button +import example.redwood.widget.Button2 +import example.redwood.widget.ExampleSchemaWidgetFactory +import example.redwood.widget.Row +import example.redwood.widget.ScopedRow +import example.redwood.widget.Space +import example.redwood.widget.Text +import example.redwood.widget.TextInput +import platform.UIKit.UIView + +class DefaultExampleSchemaWidgetFactory : ExampleSchemaWidgetFactory { + override fun Row(): Row = RowBinding() + override fun ScopedRow(): ScopedRow = TODO("Not yet implemented") + override fun Text(): Text = TextBinding() + override fun Button(): Button = TODO("Not yet implemented") + override fun Button2(): Button2 = TODO("Not yet implemented") + override fun TextInput(): TextInput = TODO("Not yet implemented") + override fun Space(): Space = TODO("Not yet implemented") +} diff --git a/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/ExampleSchemaWidgetSystem.kt b/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/ExampleSchemaWidgetSystem.kt new file mode 100644 index 0000000000..1100d43f4d --- /dev/null +++ b/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/ExampleSchemaWidgetSystem.kt @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.cash.redwood.treehouse + +import app.cash.redwood.layout.uiview.UIViewRedwoodLayoutWidgetFactory +import app.cash.redwood.protocol.widget.ProtocolMismatchHandler +import app.cash.redwood.protocol.widget.ProtocolNode +import example.redwood.widget.ExampleSchemaProtocolNodeFactory +import example.redwood.widget.ExampleSchemaWidgetFactories +import kotlinx.serialization.json.Json + +/** Basic UIView implementation of the example schema, for testing. */ +class ExampleSchemaWidgetSystem : TreehouseView.WidgetSystem { + override fun widgetFactory( + json: Json, + protocolMismatchHandler: ProtocolMismatchHandler, + ): ProtocolNode.Factory<*> { + return ExampleSchemaProtocolNodeFactory( + provider = ExampleSchemaWidgetFactories( + ExampleSchema = DefaultExampleSchemaWidgetFactory(), + RedwoodLayout = UIViewRedwoodLayoutWidgetFactory(), + ), + ) + } +} diff --git a/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/RowBinding.kt b/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/RowBinding.kt new file mode 100644 index 0000000000..7536edc5ac --- /dev/null +++ b/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/RowBinding.kt @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.cash.redwood.treehouse + +import app.cash.redwood.Modifier +import app.cash.redwood.widget.UIViewChildren +import app.cash.redwood.widget.Widget +import example.redwood.widget.Row +import platform.UIKit.UIStackView +import platform.UIKit.UIView + +/** Note: this doesn't bother to arrange child views horizontally! */ +class RowBinding : Row { + override val value = UIStackView() + + override var modifier: Modifier = Modifier + + override val children: Widget.Children = UIViewChildren(value) +} diff --git a/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/TextBinding.kt b/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/TextBinding.kt new file mode 100644 index 0000000000..dc3fac13f3 --- /dev/null +++ b/redwood-treehouse-host/src/iosTest/kotlin/app/cash/redwood/treehouse/TextBinding.kt @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2023 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.cash.redwood.treehouse + +import app.cash.redwood.Modifier +import example.redwood.widget.Text +import platform.UIKit.UILabel +import platform.UIKit.UIView + +class TextBinding : Text { + override val value = UILabel() + override var modifier: Modifier = Modifier + + override fun text(text: String?) { + value.text = text ?: "" + } +}