Skip to content

Add iOS change list renderer tests #1197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions redwood-treehouse-host/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ apply plugin: 'app.cash.zipline'
apply plugin: 'app.cash.redwood.build'

redwoodBuild {
composeCompiler()
publishing()
}

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<UIView>(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<UIView>(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")
}
}
}
Original file line number Diff line number Diff line change
@@ -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<UIView> {
override fun Row(): Row<UIView> = RowBinding()
override fun ScopedRow(): ScopedRow<UIView> = TODO("Not yet implemented")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We MIGHT want to change these to error("unexpected call") because I think there’s a robot that creates GitHub issues for all of our TODOs in this repo. I don’t think we ever want to actually do this work.

override fun Text(): Text<UIView> = TextBinding()
override fun Button(): Button<UIView> = TODO("Not yet implemented")
override fun Button2(): Button2<UIView> = TODO("Not yet implemented")
override fun TextInput(): TextInput<UIView> = TODO("Not yet implemented")
override fun Space(): Space<UIView> = TODO("Not yet implemented")
}
Original file line number Diff line number Diff line change
@@ -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(),
),
)
}
}
Original file line number Diff line number Diff line change
@@ -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<UIView> {
override val value = UIStackView()

override var modifier: Modifier = Modifier

override val children: Widget.Children<UIView> = UIViewChildren(value)
}
Original file line number Diff line number Diff line change
@@ -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<UIView> {
override val value = UILabel()
override var modifier: Modifier = Modifier

override fun text(text: String?) {
value.text = text ?: ""
}
}