Skip to content
This repository was archived by the owner on Feb 5, 2021. It is now read-only.
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
2 changes: 1 addition & 1 deletion .buildscript/android-sample-app.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apply from: rootProject.file('.buildscript/configure-android-defaults.gradle')

dependencies {
implementation(project(":compose-tooling"))
implementation(Deps.get("androidx.appcompat"))
implementation(Deps.get("compose.tooling"))
implementation(Deps.get("timber"))
implementation(Deps.get("workflow.core"))
implementation(Deps.get("workflow.runtime"))
Expand Down
17 changes: 17 additions & 0 deletions compose-tooling/api/compose-tooling.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public final class com/squareup/workflow/ui/compose/tooling/BuildConfig {
public static final field BUILD_TYPE Ljava/lang/String;
public static final field DEBUG Z
public static final field LIBRARY_PACKAGE_NAME Ljava/lang/String;
public fun <init> ()V
}

public final class com/squareup/workflow/ui/compose/tooling/ComposeWorkflowsKt {
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
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
}

public final class com/squareup/workflow/ui/compose/tooling/ViewFactoriesKt {
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
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
}

43 changes: 43 additions & 0 deletions compose-tooling/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

/*
* Copyright 2020 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.
*/
plugins {
id("com.android.library")
kotlin("android")
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

apply(from = rootProject.file(".buildscript/configure-maven-publish.gradle"))
apply(from = rootProject.file(".buildscript/configure-android-defaults.gradle"))
apply(from = rootProject.file(".buildscript/android-ui-tests.gradle"))

apply(from = rootProject.file(".buildscript/configure-compose.gradle"))
tasks.withType<KotlinCompile> {
kotlinOptions.apiVersion = "1.3"
}

dependencies {
api(project(":core-compose"))
api(Dependencies.Compose.tooling)
api(Dependencies.Kotlin.stdlib)

implementation(Dependencies.Compose.foundation)
}
18 changes: 18 additions & 0 deletions compose-tooling/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright 2020 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.
#
POM_ARTIFACT_ID=workflow-ui-compose-tooling
POM_NAME=Workflow UI Compose Tooling
POM_PACKAGING=aar
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright 2020 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.
*/
@file:Suppress("TestFunctionName", "PrivatePropertyName")

package com.squareup.workflow.ui.compose.tooling

import androidx.compose.Composable
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.ui.core.Modifier
import androidx.ui.foundation.Text
import androidx.ui.layout.Column
import androidx.ui.layout.size
import androidx.ui.semantics.Semantics
import androidx.ui.test.assertIsDisplayed
import androidx.ui.test.assertIsNotDisplayed
import androidx.ui.test.createComposeRule
import androidx.ui.test.findByText
import androidx.ui.tooling.preview.Preview
import androidx.ui.unit.dp
import com.squareup.workflow.Workflow
import com.squareup.workflow.compose.composed
import com.squareup.workflow.ui.ViewEnvironmentKey
import com.squareup.workflow.ui.compose.showRendering
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* Duplicate of [PreviewViewFactoryTest] but for [com.squareup.workflow.compose.ComposeWorkflow].
*/
@RunWith(AndroidJUnit4::class)
class PreviewComposeWorkflowTest {

@Rule @JvmField val composeRule = createComposeRule()

@Test fun singleChild() {
composeRule.setContent {
ParentWithOneChildPreview()
}

findByText("one").assertIsDisplayed()
findByText("two").assertIsDisplayed()
}

@Test fun twoChildren() {
composeRule.setContent {
ParentWithTwoChildrenPreview()
}

findByText("one").assertIsDisplayed()
findByText("two").assertIsDisplayed()
findByText("three").assertIsDisplayed()
}

@Test fun modifierIsApplied() {
composeRule.setContent {
ParentWithModifier()
}

// The view factory will be rendered with size (0,0), so it should be reported as not displayed.
findByText("one").assertIsNotDisplayed()
findByText("two").assertIsNotDisplayed()
}

@Test fun placeholderModifierIsApplied() {
composeRule.setContent {
ParentWithPlaceholderModifier()
}

// The child will be rendered with size (0,0), so it should be reported as not displayed.
findByText("one").assertIsDisplayed()
findByText("two").assertIsNotDisplayed()
}

@Test fun customViewEnvironment() {
composeRule.setContent {
ParentConsumesCustomKeyPreview()
}

findByText("foo").assertIsDisplayed()
}

private val ParentWithOneChild =
Workflow.composed<Pair<String, String>, Nothing> { props, _, environment ->
Column {
Text(props.first)
Semantics(container = true, mergeAllDescendants = true) {
environment.showRendering(rendering = props.second)
}
}
}

@Preview @Composable private fun ParentWithOneChildPreview() {
ParentWithOneChild.preview(Pair("one", "two"))
}

private val ParentWithTwoChildren =
Workflow.composed<Triple<String, String, String>, Nothing> { props, _, environment ->
Column {
Semantics(container = true) {
environment.showRendering(rendering = props.first)
}
Text(props.second)
Semantics(container = true) {
environment.showRendering(rendering = props.third)
}
}
}

@Preview @Composable private fun ParentWithTwoChildrenPreview() {
ParentWithTwoChildren.preview(Triple("one", "two", "three"))
}

@Preview @Composable private fun ParentWithModifier() {
ParentWithOneChild.preview(
Pair("one", "two"),
modifier = Modifier.size(0.dp)
)
}

@Preview @Composable private fun ParentWithPlaceholderModifier() {
ParentWithOneChild.preview(
Pair("one", "two"),
placeholderModifier = Modifier.size(0.dp)
)
}

object TestEnvironmentKey : ViewEnvironmentKey<String>(String::class) {
override val default: String get() = error("Not specified")
}

private val ParentConsumesCustomKey = Workflow.composed<Unit, Nothing> { _, _, environment ->
Text(environment[TestEnvironmentKey])
}

@Preview @Composable private fun ParentConsumesCustomKeyPreview() {
ParentConsumesCustomKey.preview(Unit) {
it + (TestEnvironmentKey to "foo")
}
}
}
Loading