Skip to content

Commit 039f1c8

Browse files
Merge pull request #15 from square/zachklipp/compose-tooling
Introduce tooling module with support for previewing ViewBindings with Compose's Preview.
2 parents 275080a + d3e4f38 commit 039f1c8

File tree

19 files changed

+839
-24
lines changed

19 files changed

+839
-24
lines changed

.buildscript/android-sample-app.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apply from: rootProject.file('.buildscript/configure-android-defaults.gradle')
22

33
dependencies {
4+
implementation(project(":compose-tooling"))
45
implementation(Deps.get("androidx.appcompat"))
5-
implementation(Deps.get("compose.tooling"))
66
implementation(Deps.get("timber"))
77
implementation(Deps.get("workflow.core"))
88
implementation(Deps.get("workflow.runtime"))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public final class com/squareup/workflow/ui/compose/tooling/BuildConfig {
2+
public static final field BUILD_TYPE Ljava/lang/String;
3+
public static final field DEBUG Z
4+
public static final field LIBRARY_PACKAGE_NAME Ljava/lang/String;
5+
public fun <init> ()V
6+
}
7+
8+
public final class com/squareup/workflow/ui/compose/tooling/ComposeWorkflowsKt {
9+
public static final fun preview (Lcom/squareup/workflow/compose/ComposeWorkflow;Ljava/lang/Object;Landroidx/ui/core/Modifier;Landroidx/ui/core/Modifier;Lkotlin/jvm/functions/Function1;Landroidx/compose/Composer;)V
10+
public static synthetic fun preview$default (Lcom/squareup/workflow/compose/ComposeWorkflow;Ljava/lang/Object;Landroidx/ui/core/Modifier;Landroidx/ui/core/Modifier;Lkotlin/jvm/functions/Function1;Landroidx/compose/Composer;ILjava/lang/Object;)V
11+
}
12+
13+
public final class com/squareup/workflow/ui/compose/tooling/ViewFactoriesKt {
14+
public static final fun preview (Lcom/squareup/workflow/ui/ViewFactory;Ljava/lang/Object;Landroidx/ui/core/Modifier;Landroidx/ui/core/Modifier;Lkotlin/jvm/functions/Function1;Landroidx/compose/Composer;)V
15+
public static synthetic fun preview$default (Lcom/squareup/workflow/ui/ViewFactory;Ljava/lang/Object;Landroidx/ui/core/Modifier;Landroidx/ui/core/Modifier;Lkotlin/jvm/functions/Function1;Landroidx/compose/Composer;ILjava/lang/Object;)V
16+
}
17+

compose-tooling/build.gradle.kts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
3+
/*
4+
* Copyright 2020 Square Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
plugins {
19+
id("com.android.library")
20+
kotlin("android")
21+
}
22+
23+
java {
24+
sourceCompatibility = JavaVersion.VERSION_1_8
25+
targetCompatibility = JavaVersion.VERSION_1_8
26+
}
27+
28+
apply(from = rootProject.file(".buildscript/configure-maven-publish.gradle"))
29+
apply(from = rootProject.file(".buildscript/configure-android-defaults.gradle"))
30+
apply(from = rootProject.file(".buildscript/android-ui-tests.gradle"))
31+
32+
apply(from = rootProject.file(".buildscript/configure-compose.gradle"))
33+
tasks.withType<KotlinCompile> {
34+
kotlinOptions.apiVersion = "1.3"
35+
}
36+
37+
dependencies {
38+
api(project(":core-compose"))
39+
api(Dependencies.Compose.tooling)
40+
api(Dependencies.Kotlin.stdlib)
41+
42+
implementation(Dependencies.Compose.foundation)
43+
}

compose-tooling/gradle.properties

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Copyright 2020 Square Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
POM_ARTIFACT_ID=workflow-ui-compose-tooling
17+
POM_NAME=Workflow UI Compose Tooling
18+
POM_PACKAGING=aar
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright 2020 Square Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
@file:Suppress("TestFunctionName", "PrivatePropertyName")
17+
18+
package com.squareup.workflow.ui.compose.tooling
19+
20+
import androidx.compose.Composable
21+
import androidx.test.ext.junit.runners.AndroidJUnit4
22+
import androidx.ui.core.Modifier
23+
import androidx.ui.foundation.Text
24+
import androidx.ui.layout.Column
25+
import androidx.ui.layout.size
26+
import androidx.ui.semantics.Semantics
27+
import androidx.ui.test.assertIsDisplayed
28+
import androidx.ui.test.assertIsNotDisplayed
29+
import androidx.ui.test.createComposeRule
30+
import androidx.ui.test.findByText
31+
import androidx.ui.tooling.preview.Preview
32+
import androidx.ui.unit.dp
33+
import com.squareup.workflow.Workflow
34+
import com.squareup.workflow.compose.composed
35+
import com.squareup.workflow.ui.ViewEnvironmentKey
36+
import com.squareup.workflow.ui.compose.showRendering
37+
import org.junit.Rule
38+
import org.junit.Test
39+
import org.junit.runner.RunWith
40+
41+
/**
42+
* Duplicate of [PreviewViewFactoryTest] but for [com.squareup.workflow.compose.ComposeWorkflow].
43+
*/
44+
@RunWith(AndroidJUnit4::class)
45+
class PreviewComposeWorkflowTest {
46+
47+
@Rule @JvmField val composeRule = createComposeRule()
48+
49+
@Test fun singleChild() {
50+
composeRule.setContent {
51+
ParentWithOneChildPreview()
52+
}
53+
54+
findByText("one").assertIsDisplayed()
55+
findByText("two").assertIsDisplayed()
56+
}
57+
58+
@Test fun twoChildren() {
59+
composeRule.setContent {
60+
ParentWithTwoChildrenPreview()
61+
}
62+
63+
findByText("one").assertIsDisplayed()
64+
findByText("two").assertIsDisplayed()
65+
findByText("three").assertIsDisplayed()
66+
}
67+
68+
@Test fun modifierIsApplied() {
69+
composeRule.setContent {
70+
ParentWithModifier()
71+
}
72+
73+
// The view factory will be rendered with size (0,0), so it should be reported as not displayed.
74+
findByText("one").assertIsNotDisplayed()
75+
findByText("two").assertIsNotDisplayed()
76+
}
77+
78+
@Test fun placeholderModifierIsApplied() {
79+
composeRule.setContent {
80+
ParentWithPlaceholderModifier()
81+
}
82+
83+
// The child will be rendered with size (0,0), so it should be reported as not displayed.
84+
findByText("one").assertIsDisplayed()
85+
findByText("two").assertIsNotDisplayed()
86+
}
87+
88+
@Test fun customViewEnvironment() {
89+
composeRule.setContent {
90+
ParentConsumesCustomKeyPreview()
91+
}
92+
93+
findByText("foo").assertIsDisplayed()
94+
}
95+
96+
private val ParentWithOneChild =
97+
Workflow.composed<Pair<String, String>, Nothing> { props, _, environment ->
98+
Column {
99+
Text(props.first)
100+
Semantics(container = true, mergeAllDescendants = true) {
101+
environment.showRendering(rendering = props.second)
102+
}
103+
}
104+
}
105+
106+
@Preview @Composable private fun ParentWithOneChildPreview() {
107+
ParentWithOneChild.preview(Pair("one", "two"))
108+
}
109+
110+
private val ParentWithTwoChildren =
111+
Workflow.composed<Triple<String, String, String>, Nothing> { props, _, environment ->
112+
Column {
113+
Semantics(container = true) {
114+
environment.showRendering(rendering = props.first)
115+
}
116+
Text(props.second)
117+
Semantics(container = true) {
118+
environment.showRendering(rendering = props.third)
119+
}
120+
}
121+
}
122+
123+
@Preview @Composable private fun ParentWithTwoChildrenPreview() {
124+
ParentWithTwoChildren.preview(Triple("one", "two", "three"))
125+
}
126+
127+
@Preview @Composable private fun ParentWithModifier() {
128+
ParentWithOneChild.preview(
129+
Pair("one", "two"),
130+
modifier = Modifier.size(0.dp)
131+
)
132+
}
133+
134+
@Preview @Composable private fun ParentWithPlaceholderModifier() {
135+
ParentWithOneChild.preview(
136+
Pair("one", "two"),
137+
placeholderModifier = Modifier.size(0.dp)
138+
)
139+
}
140+
141+
object TestEnvironmentKey : ViewEnvironmentKey<String>(String::class) {
142+
override val default: String get() = error("Not specified")
143+
}
144+
145+
private val ParentConsumesCustomKey = Workflow.composed<Unit, Nothing> { _, _, environment ->
146+
Text(environment[TestEnvironmentKey])
147+
}
148+
149+
@Preview @Composable private fun ParentConsumesCustomKeyPreview() {
150+
ParentConsumesCustomKey.preview(Unit) {
151+
it + (TestEnvironmentKey to "foo")
152+
}
153+
}
154+
}

0 commit comments

Comments
 (0)